forked from randyrossi/bmc64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbl.cpp
512 lines (432 loc) · 13.9 KB
/
fbl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//
// fbl.cpp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fbl.h"
#include <stdio.h>
#ifndef ALIGN_UP
#define ALIGN_UP(x,y) ((x + (y)-1) & ~((y)-1))
#endif
#define RGB565(r,g,b) (((r)>>3)<<11 | ((g)>>2)<<5 | (b)>>3)
#define ARGB(a,r,g,b) ((uint32_t)((uint8_t)(a)<<24 | (uint8_t)(r)<<16 | (uint8_t)(g)<<8 | (uint8_t)(b)))
// Default palette used for canvases with no transparency
static uint16_t pal_565[256] = {
RGB565(0x00, 0x00, 0x00),
RGB565(0xFF, 0xFF, 0xFF),
RGB565(0xFF, 0x00, 0x00),
RGB565(0x70, 0xa4, 0xb2),
RGB565(0x6f, 0x3d, 0x86),
RGB565(0x58, 0x8d, 0x43),
RGB565(0x35, 0x28, 0x79),
RGB565(0xb8, 0xc7, 0x6f),
RGB565(0x6f, 0x4f, 0x25),
RGB565(0x43, 0x39, 0x00),
RGB565(0x9a, 0x67, 0x59),
RGB565(0x44, 0x44, 0x44),
RGB565(0x6c, 0x6c, 0x6c),
RGB565(0x9a, 0xd2, 0x84),
RGB565(0x6c, 0x5e, 0xb5),
RGB565(0x95, 0x95, 0x95),
};
// Default palette used for canvases with transparency. This palette
// is identical to pal_565 except we include an additional
// entry for a fully transparent pixel (index 16).
static uint32_t pal_argb[256] = {
// First 16 colors are opaque
ARGB(0xFF, 0x00, 0x00, 0x00),
ARGB(0xFF, 0xFF, 0xFF, 0xFF),
ARGB(0xFF, 0xFF, 0x00, 0x00),
ARGB(0xFF, 0x70, 0xa4, 0xb2),
ARGB(0xFF, 0x6f, 0x3d, 0x86),
ARGB(0xFF, 0x58, 0x8d, 0x43),
ARGB(0xFF, 0x35, 0x28, 0x79),
ARGB(0xFF, 0xb8, 0xc7, 0x6f),
ARGB(0xFF, 0x6f, 0x4f, 0x25),
ARGB(0xFF, 0x43, 0x39, 0x00),
ARGB(0xFF, 0x9a, 0x67, 0x59),
ARGB(0xFF, 0x44, 0x44, 0x44),
ARGB(0xFF, 0x6c, 0x6c, 0x6c),
ARGB(0xFF, 0x9a, 0xd2, 0x84),
ARGB(0xFF, 0x6c, 0x5e, 0xb5),
ARGB(0xFF, 0x95, 0x95, 0x95),
// Use index 16 for fully transparent pixels
ARGB(0x00, 0x00, 0x00, 0x00),
};
static const char sNoInt[] = "scaling_kernel 0 0 0 0 0 0 0 0 1 1 1 1 255 255 255 255 255 255 255 255 1 1 1 1 0 0 0 0 0 0 0 0 1";
static char config_scaling_kernel[1024];
bool FrameBufferLayer::initialized_ = false;
DISPMANX_DISPLAY_HANDLE_T FrameBufferLayer::dispman_display_;
FrameBufferLayer::FrameBufferLayer() :
fb_width_(0), fb_height_(0), fb_pitch_(0), layer_(0), transparency_(false),
hstretch_(1.6), vstretch_(1.0),
valign_(0), vpadding_(0), halign_(0), hpadding_(0),
h_center_offset_(0), v_center_offset_(0),
rnum_(0), leftPadding_(0), rightPadding_(0), topPadding_(0),
bottomPadding_(0), showing_(false), allocated_(false),
mode_(VC_IMAGE_8BPP), bytes_per_pixel_(1) {
alpha_.flags = DISPMANX_FLAGS_ALPHA_FROM_SOURCE;
alpha_.opacity = 255;
alpha_.mask = 0;
memcpy (pal_565_, pal_565, sizeof(pal_565));
memcpy (pal_argb_, pal_argb, sizeof(pal_argb));
}
FrameBufferLayer::~FrameBufferLayer() {
if (showing_) {
Hide();
}
if (allocated_) {
Free();
}
}
// static
void FrameBufferLayer::Initialize() {
if (initialized_)
return;
bcm_host_init();
dispman_display_ = vc_dispmanx_display_open(0);
bcm_get_sclker(config_scaling_kernel, sizeof(config_scaling_kernel));
// We have to remove the '=' or else what we send back
// won't work.
for (unsigned int i=0;i<strlen(config_scaling_kernel);i++) {
if (config_scaling_kernel[i] == '=') {
config_scaling_kernel[i] = ' ';
break;
}
}
initialized_ = true;
}
int FrameBufferLayer::Allocate(int pixelmode, uint8_t **pixels,
int width, int height, int *pitch) {
int ret;
DISPMANX_MODEINFO_T dispman_info;
uint32_t vc_image_ptr;
if (allocated_) {
Free();
}
allocated_ = true;
switch (pixelmode) {
case 0:
mode_ = VC_IMAGE_8BPP;
bytes_per_pixel_ = 1;
break;
case 1:
mode_ = VC_IMAGE_RGB565;
bytes_per_pixel_ = 2;
break;
default:
mode_ = VC_IMAGE_8BPP;
bytes_per_pixel_ = 1;
break;
}
int pitch_multiplier = 1;
if (mode_ == VC_IMAGE_RGB565)
pitch_multiplier = 2;
// pitch is in bytes
*pitch = fb_pitch_ = ALIGN_UP(width * pitch_multiplier, 32);
fb_width_ = width;
fb_height_ = height;
ret = vc_dispmanx_display_get_info(dispman_display_, &dispman_info);
assert(ret == 0);
display_width_ = dispman_info.width;
display_height_ = dispman_info.height;
*pixels = pixels_ = (uint8_t*) malloc(fb_pitch_ * height * bytes_per_pixel_);
// Allocate the VC resources along with the frame buffer
dispman_resource_[0] = vc_dispmanx_resource_create(mode_,
width,
height,
&vc_image_ptr );
dispman_resource_[1] = vc_dispmanx_resource_create(mode_,
width,
height,
&vc_image_ptr );
assert(dispman_resource_[0]);
assert(dispman_resource_[1]);
// Install the default palette
if (mode_ == VC_IMAGE_8BPP) {
UpdatePalette();
}
vc_dispmanx_rect_set(©_dst_rect_, 0, 0, width, height);
dst_w_ = width;
dst_h_ = height;
assert(ret == 0);
// Default our src region to the entire buffer.
src_x_ = 0;
src_y_ = 0;
src_w_ = width;
src_h_ = height;
return 0;
}
void FrameBufferLayer::Clear() {
assert (allocated_);
memset(pixels_, 0, fb_height_ * fb_pitch_ * bytes_per_pixel_);
}
void FrameBufferLayer::Free() {
int ret;
if (!allocated_) return;
if (showing_) {
Hide();
}
fb_width_ = 0;
fb_height_ = 0;
fb_pitch_ = 0;
free(pixels_);
ret = vc_dispmanx_resource_delete(dispman_resource_[0]);
assert(ret == 0);
ret = vc_dispmanx_resource_delete(dispman_resource_[1]);
assert(ret == 0);
allocated_ = false;
}
void FrameBufferLayer::Show() {
int ret;
DISPMANX_UPDATE_HANDLE_T dispman_update;
if (showing_) return;
// Define source rect
vc_dispmanx_rect_set(&src_rect_,
src_x_ << 16, src_y_ << 16, src_w_ << 16, src_h_ << 16);
int dst_w;
int dst_h;
assert (hstretch_ != 0);
assert (vstretch_ != 0);
int lpad_abs = display_width_ * leftPadding_;
int rpad_abs = display_width_ * rightPadding_;
int tpad_abs = display_height_ * topPadding_;
int bpad_abs = display_height_ * bottomPadding_;
int avail_width = display_width_ - lpad_abs - rpad_abs;
int avail_height = display_height_ - tpad_abs - bpad_abs;
if (hstretch_ < 0) {
// Stretch horizontally to fill width * vstretch and then set height
// based on hstretch. This mode doesn't support integer stretch.
dst_w = avail_width * vstretch_;
dst_h = avail_width / -hstretch_;
if (dst_w > avail_width) {
dst_w = avail_width;
}
if (dst_h > avail_height) {
dst_h = avail_height;
}
} else {
// Stretch vertically to fill height * vstretch and then set width
// based on hstretch.
dst_h = avail_height * vstretch_;
if (use_vintstr_) {
dst_h = vintstr_;
}
dst_w = avail_height * hstretch_;
if (use_hintstr_) {
dst_w = hintstr_;
}
if (dst_h > avail_height) {
dst_h = avail_height;
}
if (dst_w > avail_width) {
dst_w = avail_width;
}
}
// Resulting image is centered
int oy;
switch (valign_) {
case 0:
// Center
oy = (avail_height - dst_h) / 2 + v_center_offset_;
break;
case -1:
// Top
oy = vpadding_;
break;
case 1:
// Bottom
oy = avail_height - dst_h - vpadding_;
break;
default:
oy = 0;
break;
}
int ox;
switch (halign_) {
case 0:
// Center
ox = (avail_width - dst_w) / 2 + h_center_offset_;
break;
case -1:
// Left
ox = hpadding_;
break;
case 1:
// Right
ox = avail_width - dst_w - hpadding_;
break;
default:
ox = 0;
break;
}
vc_dispmanx_rect_set(&scale_dst_rect_,
ox + lpad_abs,
oy + tpad_abs,
dst_w,
dst_h);
dst_w_ = dst_w;
dst_h_ = dst_h;
dispman_update = vc_dispmanx_update_start(0);
assert( dispman_update );
rnum_ = 0;
dispman_element_ = vc_dispmanx_element_add(dispman_update,
dispman_display_,
layer_, // layer
&scale_dst_rect_,
dispman_resource_[rnum_],
&src_rect_,
DISPMANX_PROTECTION_NONE,
&alpha_,
NULL, // clamp
DISPMANX_NO_ROTATE);
ret = vc_dispmanx_update_submit(dispman_update, NULL, NULL);
assert( ret == 0 );
showing_ = true;
}
void FrameBufferLayer::Hide() {
int ret;
DISPMANX_UPDATE_HANDLE_T dispman_update;
if (!showing_) return;
dispman_update = vc_dispmanx_update_start(0);
ret = vc_dispmanx_element_remove(dispman_update, dispman_element_);
assert(ret == 0);
ret = vc_dispmanx_update_submit(dispman_update, NULL, NULL);
assert(ret == 0);
showing_ = false;
}
void* FrameBufferLayer::GetPixels() {
return pixels_;
}
void FrameBufferLayer::FrameReady(int to_offscreen) {
int rnum = to_offscreen ? 1 - rnum_ : rnum_;
// Copy data into either the offscreen resource (if swap) or the
// on screen resource (if !swap).
vc_dispmanx_resource_write_data(dispman_resource_[rnum],
mode_,
fb_pitch_,
pixels_,
©_dst_rect_);
}
// Private function to change the source of this frame buffer's
// element to the off screen resource and toggle the resource
// index in preparation for the off screen data to be shown.
void FrameBufferLayer::Swap(DISPMANX_UPDATE_HANDLE_T& dispman_update) {
rnum_ = 1 - rnum_;
vc_dispmanx_element_change_source(dispman_update,
dispman_element_,
dispman_resource_[rnum_]);
}
// Static
void FrameBufferLayer::SwapResources(FrameBufferLayer* fb1,
FrameBufferLayer* fb2) {
int ret;
DISPMANX_UPDATE_HANDLE_T dispman_update;
dispman_update = vc_dispmanx_update_start(0);
fb1->Swap(dispman_update);
if (fb2) fb2->Swap(dispman_update);
ret = vc_dispmanx_update_submit_sync(dispman_update);
assert(ret == 0);
}
void FrameBufferLayer::SetPalette(uint8_t index, uint16_t rgb565) {
assert(!transparency_);
assert (mode_ == VC_IMAGE_8BPP);
pal_565_[index] = rgb565;
}
void FrameBufferLayer::SetPalette(uint8_t index, uint32_t argb) {
assert(transparency_);
assert (mode_ == VC_IMAGE_8BPP);
pal_argb_[index] = argb;
}
void FrameBufferLayer::UpdatePalette() {
if (!allocated_) return;
assert (mode_ == VC_IMAGE_8BPP);
int ret;
if (transparency_) {
ret = vc_dispmanx_resource_set_palette(dispman_resource_[0],
pal_argb_, 0, sizeof pal_argb_);
ret = vc_dispmanx_resource_set_palette(dispman_resource_[1],
pal_argb_, 0, sizeof pal_argb_);
} else {
ret = vc_dispmanx_resource_set_palette(dispman_resource_[0],
pal_565_, 0, sizeof pal_565_);
ret = vc_dispmanx_resource_set_palette(dispman_resource_[1],
pal_565_, 0, sizeof pal_565_);
}
assert( ret == 0 );
}
void FrameBufferLayer::SetLayer(int layer) {
layer_ = layer;
}
int FrameBufferLayer::GetLayer() {
return layer_;
}
void FrameBufferLayer::SetTransparency(bool transparency) {
assert (mode_ == VC_IMAGE_8BPP);
transparency_ = transparency;
}
void FrameBufferLayer::SetSrcRect(int x, int y, int w, int h) {
src_x_ = x;
src_y_ = y;
src_w_ = w;
src_h_ = h;
}
// Set horizontal/vertical multipliers
void FrameBufferLayer::SetStretch(double hstretch, double vstretch, int hintstr, int vintstr, int use_hintstr, int use_vintstr) {
hstretch_ = hstretch;
vstretch_ = vstretch;
hintstr_ = hintstr;
vintstr_ = vintstr;
use_hintstr_ = use_hintstr;
use_vintstr_ = use_vintstr;
}
void FrameBufferLayer::SetVerticalAlignment(int alignment, int padding) {
valign_ = alignment;
vpadding_ = padding;
}
void FrameBufferLayer::SetHorizontalAlignment(int alignment, int padding) {
halign_ = alignment;
hpadding_ = padding;
}
void FrameBufferLayer::SetPadding(double leftPadding,
double rightPadding,
double topPadding,
double bottomPadding) {
leftPadding_ = leftPadding;
rightPadding_ = rightPadding;
topPadding_ = topPadding;
bottomPadding_ = bottomPadding;
}
void FrameBufferLayer::SetCenterOffset(int cx, int cy) {
h_center_offset_ = cx;
v_center_offset_ = cy;
}
void FrameBufferLayer::GetDimensions(int *display_w, int *display_h,
int *fb_w, int *fb_h,
int *src_w, int *src_h,
int *dst_w, int *dst_h) {
*display_w = display_width_;
*display_h = display_height_;
*fb_w = fb_width_;
*fb_h = fb_height_;
*src_w = src_w_;
*src_h = src_h_;
*dst_w = dst_w_;
*dst_h = dst_h_;
}
void FrameBufferLayer::SetInterpolation(int enable) {
if (enable) {
bcm_set_sclker(config_scaling_kernel);
} else {
bcm_set_sclker(sNoInt);
}
}