blob: 39ec5e4947a4df8bddfcf3a73a377b13db643399 [file] [log] [blame]
Ethan Yonkera59da092015-10-13 19:35:05 -05001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ethan Yonker8373cfe2017-09-08 06:50:54 -050017#include "graphics_overlay.h"
18
Ethan Yonkera59da092015-10-13 19:35:05 -050019#include <stdbool.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <errno.h>
24
25#include <fcntl.h>
26#include <stdio.h>
27
28#include <sys/cdefs.h>
29#include <sys/ioctl.h>
30#include <sys/mman.h>
31#include <sys/types.h>
32
33#include <linux/fb.h>
34#include <linux/kd.h>
35
36#ifdef MSM_BSP
37#include <linux/msm_mdp.h>
38#include <linux/msm_ion.h>
Ethan Yonker8373cfe2017-09-08 06:50:54 -050039#else
40#define MSMFB_NEW_REQUEST 0
Ethan Yonkera59da092015-10-13 19:35:05 -050041#endif
42
Ethan Yonker8373cfe2017-09-08 06:50:54 -050043#include "minui/minui.h"
Ethan Yonkera59da092015-10-13 19:35:05 -050044
45#define MDP_V4_0 400
46#define MAX_DISPLAY_DIM 2048
Ethan Yonkera59da092015-10-13 19:35:05 -050047#define ALIGN(x, align) (((x) + ((align)-1)) & ~((align)-1))
48
Ethan Yonker8373cfe2017-09-08 06:50:54 -050049MinuiBackendOverlay::MinuiBackendOverlay() :
50 gr_draw(nullptr),
51 fb_fd(-1),
52 isMDP5(false),
53 leftSplit(0),
54 rightSplit(0),
55 frame_size(0),
56 overlayL_id(MSMFB_NEW_REQUEST),
57 overlayR_id(MSMFB_NEW_REQUEST) {}
Ethan Yonkera59da092015-10-13 19:35:05 -050058
59#ifdef MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -050060int MinuiBackendOverlay::map_mdp_pixel_format()
Ethan Yonkera59da092015-10-13 19:35:05 -050061{
62 int format = MDP_RGB_565;
63#if defined(RECOVERY_BGRA)
64 format = MDP_BGRA_8888;
Kra1o577568592015-10-14 18:09:54 +020065#elif defined(RECOVERY_RGBA)
66 format = MDP_RGBA_8888;
Ethan Yonkera59da092015-10-13 19:35:05 -050067#elif defined(RECOVERY_RGBX)
68 format = MDP_RGBA_8888;
69#endif
70 return format;
71}
72#endif // MSM_BSP
73
Ethan Yonker8373cfe2017-09-08 06:50:54 -050074bool MinuiBackendOverlay::target_has_overlay()
Ethan Yonkera59da092015-10-13 19:35:05 -050075{
76 int ret;
77 int mdp_version;
78 bool overlay_supported = false;
Ethan Yonkera59da092015-10-13 19:35:05 -050079 fb_fix_screeninfo fi;
80 int fd;
81
82 fd = open("/dev/graphics/fb0", O_RDWR);
83 if (fd < 0) {
84 perror("open_overlay cannot open fb0");
Ethan Yonker8373cfe2017-09-08 06:50:54 -050085 return false;
Ethan Yonkera59da092015-10-13 19:35:05 -050086 }
87
88 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
89 perror("failed to get fb0 info");
90 close(fd);
Ethan Yonker8373cfe2017-09-08 06:50:54 -050091 return false;
Ethan Yonkera59da092015-10-13 19:35:05 -050092 }
93 close(fd);
Ethan Yonker8373cfe2017-09-08 06:50:54 -050094
95 if (strlen(fi.id) >= 8) {
96 if(!strncmp(fi.id, "msmfb", strlen("msmfb"))) {
97 char str_ver[4];
98 memcpy(str_ver, fi.id + strlen("msmfb"), 3);
99 str_ver[3] = '\0';
100 mdp_version = atoi(str_ver);
101 if (mdp_version >= MDP_V4_0) {
102 overlay_supported = true;
103 }
104 } else if (!strncmp(fi.id, "mdssfb", strlen("mdssfb"))) {
105 overlay_supported = true;
106 isMDP5 = true;
107 }
108 }
109#ifndef MSM_BSP
110 if (overlay_supported)
111 printf("Overlay graphics may work (%s), but not enabled. Use TW_TARGET_USES_QCOM_BSP := true to enable.\n", fi.id);
112#endif
113 return overlay_supported;
Ethan Yonkera59da092015-10-13 19:35:05 -0500114}
115
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500116void MinuiBackendOverlay::Blank(bool blank)
Ethan Yonkera59da092015-10-13 19:35:05 -0500117{
118#if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS)
119 int fd;
120 char brightness[4];
121 snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2);
122
123 fd = open(TW_BRIGHTNESS_PATH, O_RDWR);
124 if (fd < 0) {
125 perror("cannot open LCD backlight");
126 return;
127 }
128 write(fd, blank ? "000" : brightness, 3);
129 close(fd);
130#else
131 int ret;
132
133 ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
134 if (ret < 0)
135 perror("ioctl(): blank");
136#endif
137}
138
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500139void MinuiBackendOverlay::SetDisplayedFramebuffer(unsigned n)
Ethan Yonkera59da092015-10-13 19:35:05 -0500140{
141 if (n > 1 || !double_buffered) return;
142
143 vi.yres_virtual = gr_framebuffer[0].height * 2;
144 vi.yoffset = n * gr_framebuffer[0].height;
145 vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
146 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
147 perror("active fb swap failed");
148 }
149 displayed_buffer = n;
150}
151
152#ifdef MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500153void MinuiBackendOverlay::setDisplaySplit(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500154 char split[64] = {0};
155 if (!isMDP5)
156 return;
157 FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r");
158 if (fp) {
159 //Format "left right" space as delimiter
160 if(fread(split, sizeof(char), 64, fp)) {
161 leftSplit = atoi(split);
162 printf("Left Split=%d\n",leftSplit);
163 char *rght = strpbrk(split, " ");
164 if (rght)
165 rightSplit = atoi(rght + 1);
166 printf("Right Split=%d\n", rightSplit);
167 }
168 } else {
169 printf("Failed to open mdss_fb_split node\n");
170 }
171 if (fp)
172 fclose(fp);
173}
174
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500175int MinuiBackendOverlay::getLeftSplit(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500176 //Default even split for all displays with high res
177 int lSplit = vi.xres / 2;
178
179 //Override if split published by driver
180 if (leftSplit)
181 lSplit = leftSplit;
182
183 return lSplit;
184}
185
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500186int MinuiBackendOverlay::getRightSplit(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500187 return rightSplit;
188}
189
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500190int MinuiBackendOverlay::free_ion_mem(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500191 int ret = 0;
192
193 if (mem_info.mem_buf)
194 munmap(mem_info.mem_buf, mem_info.size);
195
196 if (mem_info.ion_fd >= 0) {
197 ret = ioctl(mem_info.ion_fd, ION_IOC_FREE, &mem_info.handle_data);
198 if (ret < 0)
199 perror("free_mem failed ");
200 }
201
202 if (mem_info.mem_fd >= 0)
203 close(mem_info.mem_fd);
204 if (mem_info.ion_fd >= 0)
205 close(mem_info.ion_fd);
206
207 memset(&mem_info, 0, sizeof(mem_info));
208 mem_info.mem_fd = -1;
209 mem_info.ion_fd = -1;
210 return 0;
211}
212
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500213int MinuiBackendOverlay::alloc_ion_mem(unsigned int size)
Ethan Yonkera59da092015-10-13 19:35:05 -0500214{
215 int result;
216 struct ion_fd_data fd_data;
217 struct ion_allocation_data ionAllocData;
218
219 mem_info.ion_fd = open("/dev/ion", O_RDWR|O_DSYNC);
220 if (mem_info.ion_fd < 0) {
221 perror("ERROR: Can't open ion ");
222 return -errno;
223 }
224
225 ionAllocData.flags = 0;
226 ionAllocData.len = size;
227 ionAllocData.align = sysconf(_SC_PAGESIZE);
228#ifdef NEW_ION_HEAP
229 ionAllocData.heap_id_mask =
230#else
231 ionAllocData.heap_mask =
232#endif
233 ION_HEAP(ION_IOMMU_HEAP_ID) |
234 ION_HEAP(ION_SYSTEM_CONTIG_HEAP_ID);
235
236 result = ioctl(mem_info.ion_fd, ION_IOC_ALLOC, &ionAllocData);
237 if(result){
238 perror("ION_IOC_ALLOC Failed ");
239 close(mem_info.ion_fd);
240 return result;
241 }
242
243 fd_data.handle = ionAllocData.handle;
244 mem_info.handle_data.handle = ionAllocData.handle;
245 result = ioctl(mem_info.ion_fd, ION_IOC_MAP, &fd_data);
246 if (result) {
247 perror("ION_IOC_MAP Failed ");
248 free_ion_mem();
249 return result;
250 }
251 mem_info.mem_buf = (unsigned char *)mmap(NULL, size, PROT_READ |
252 PROT_WRITE, MAP_SHARED, fd_data.fd, 0);
253 mem_info.mem_fd = fd_data.fd;
254
255 if (!mem_info.mem_buf) {
256 perror("ERROR: mem_buf MAP_FAILED ");
257 free_ion_mem();
258 return -ENOMEM;
259 }
260
261 return 0;
262}
263
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500264bool MinuiBackendOverlay::isDisplaySplit(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500265 if (vi.xres > MAX_DISPLAY_DIM)
266 return true;
267 //check if right split is set by driver
268 if (getRightSplit())
269 return true;
270
271 return false;
272}
273
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500274int MinuiBackendOverlay::allocate_overlay(int fd, GRSurface gr_fb[])
Ethan Yonkera59da092015-10-13 19:35:05 -0500275{
276 int ret = 0;
277
278 if (!isDisplaySplit()) {
279 // Check if overlay is already allocated
280 if (MSMFB_NEW_REQUEST == overlayL_id) {
281 struct mdp_overlay overlayL;
282
283 memset(&overlayL, 0 , sizeof (struct mdp_overlay));
284
285 /* Fill Overlay Data */
286 overlayL.src.width = ALIGN(gr_fb[0].width, 32);
287 overlayL.src.height = gr_fb[0].height;
288 overlayL.src.format = map_mdp_pixel_format();
289 overlayL.src_rect.w = gr_fb[0].width;
290 overlayL.src_rect.h = gr_fb[0].height;
291 overlayL.dst_rect.w = gr_fb[0].width;
292 overlayL.dst_rect.h = gr_fb[0].height;
293 overlayL.alpha = 0xFF;
294 overlayL.transp_mask = MDP_TRANSP_NOP;
295 overlayL.id = MSMFB_NEW_REQUEST;
296 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayL);
297 if (ret < 0) {
298 perror("Overlay Set Failed");
299 return ret;
300 }
301 overlayL_id = overlayL.id;
302 }
303 } else {
304 float xres = vi.xres;
305 int lSplit = getLeftSplit();
306 float lSplitRatio = lSplit / xres;
307 float lCropWidth = gr_fb[0].width * lSplitRatio;
308 int lWidth = lSplit;
309 int rWidth = gr_fb[0].width - lSplit;
310 int height = gr_fb[0].height;
311
312 if (MSMFB_NEW_REQUEST == overlayL_id) {
313
314 struct mdp_overlay overlayL;
315
316 memset(&overlayL, 0 , sizeof (struct mdp_overlay));
317
318 /* Fill OverlayL Data */
319 overlayL.src.width = ALIGN(gr_fb[0].width, 32);
320 overlayL.src.height = gr_fb[0].height;
321 overlayL.src.format = map_mdp_pixel_format();
322 overlayL.src_rect.x = 0;
323 overlayL.src_rect.y = 0;
324 overlayL.src_rect.w = lCropWidth;
325 overlayL.src_rect.h = gr_fb[0].height;
326 overlayL.dst_rect.x = 0;
327 overlayL.dst_rect.y = 0;
328 overlayL.dst_rect.w = lWidth;
329 overlayL.dst_rect.h = height;
330 overlayL.alpha = 0xFF;
331 overlayL.transp_mask = MDP_TRANSP_NOP;
332 overlayL.id = MSMFB_NEW_REQUEST;
333 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayL);
334 if (ret < 0) {
335 perror("OverlayL Set Failed");
336 return ret;
337 }
338 overlayL_id = overlayL.id;
339 }
340 if (MSMFB_NEW_REQUEST == overlayR_id) {
341 struct mdp_overlay overlayR;
342
343 memset(&overlayR, 0 , sizeof (struct mdp_overlay));
344
345 /* Fill OverlayR Data */
346 overlayR.src.width = ALIGN(gr_fb[0].width, 32);
347 overlayR.src.height = gr_fb[0].height;
348 overlayR.src.format = map_mdp_pixel_format();
349 overlayR.src_rect.x = lCropWidth;
350 overlayR.src_rect.y = 0;
351 overlayR.src_rect.w = gr_fb[0].width - lCropWidth;
352 overlayR.src_rect.h = gr_fb[0].height;
353 overlayR.dst_rect.x = 0;
354 overlayR.dst_rect.y = 0;
355 overlayR.dst_rect.w = rWidth;
356 overlayR.dst_rect.h = height;
357 overlayR.alpha = 0xFF;
358 overlayR.flags = MDSS_MDP_RIGHT_MIXER;
359 overlayR.transp_mask = MDP_TRANSP_NOP;
360 overlayR.id = MSMFB_NEW_REQUEST;
361 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayR);
362 if (ret < 0) {
363 perror("OverlayR Set Failed");
364 return ret;
365 }
366 overlayR_id = overlayR.id;
367 }
368
369 }
370 return 0;
371}
372
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500373int MinuiBackendOverlay::overlay_display_frame(int fd, void* data, size_t size)
Ethan Yonkera59da092015-10-13 19:35:05 -0500374{
375 int ret = 0;
376 struct msmfb_overlay_data ovdataL, ovdataR;
377 struct mdp_display_commit ext_commit;
378
379 if (!isDisplaySplit()) {
380 if (overlayL_id == MSMFB_NEW_REQUEST) {
381 perror("display_frame failed, no overlay\n");
382 return -EINVAL;
383 }
384
385 memcpy(mem_info.mem_buf, data, size);
386
387 memset(&ovdataL, 0, sizeof(struct msmfb_overlay_data));
388
389 ovdataL.id = overlayL_id;
390 ovdataL.data.flags = 0;
391 ovdataL.data.offset = 0;
392 ovdataL.data.memory_id = mem_info.mem_fd;
393 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataL);
394 if (ret < 0) {
395 perror("overlay_display_frame failed, overlay play Failed\n");
396 return ret;
397 }
398 } else {
399
400 if (overlayL_id == MSMFB_NEW_REQUEST) {
401 perror("display_frame failed, no overlayL \n");
402 return -EINVAL;
403 }
404
405 memcpy(mem_info.mem_buf, data, size);
406
407 memset(&ovdataL, 0, sizeof(struct msmfb_overlay_data));
408
409 ovdataL.id = overlayL_id;
410 ovdataL.data.flags = 0;
411 ovdataL.data.offset = 0;
412 ovdataL.data.memory_id = mem_info.mem_fd;
413 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataL);
414 if (ret < 0) {
415 perror("overlay_display_frame failed, overlayL play Failed\n");
416 return ret;
417 }
418
419 if (overlayR_id == MSMFB_NEW_REQUEST) {
420 perror("display_frame failed, no overlayR \n");
421 return -EINVAL;
422 }
423 memset(&ovdataR, 0, sizeof(struct msmfb_overlay_data));
424
425 ovdataR.id = overlayR_id;
426 ovdataR.data.flags = 0;
427 ovdataR.data.offset = 0;
428 ovdataR.data.memory_id = mem_info.mem_fd;
429 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataR);
430 if (ret < 0) {
431 perror("overlay_display_frame failed, overlayR play Failed\n");
432 return ret;
433 }
434 }
435 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
436 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
437 ext_commit.wait_for_finish = 1;
438 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
439 if (ret < 0) {
440 perror("overlay_display_frame failed, overlay commit Failed\n!");
441 }
442
443 return ret;
444}
445
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500446GRSurface* MinuiBackendOverlay::Flip() {
Ethan Yonkera59da092015-10-13 19:35:05 -0500447 if (double_buffered) {
448#if defined(RECOVERY_BGRA)
449 // In case of BGRA, do some byte swapping
450 unsigned int idx;
451 unsigned char tmp;
452 unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data;
453 for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes);
454 idx += 4) {
455 tmp = ucfb_vaddr[idx];
456 ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2];
457 ucfb_vaddr[idx + 2] = tmp;
458 }
459#endif
460 // Change gr_draw to point to the buffer currently displayed,
461 // then flip the driver so we're displaying the other buffer
462 // instead.
463 gr_draw = gr_framebuffer + displayed_buffer;
464 set_displayed_framebuffer(1-displayed_buffer);
465 overlay_display_frame(fb_fd, gr_draw->data, frame_size);
466 } else {
467 // Copy from the in-memory surface to the framebuffer.
468 overlay_display_frame(fb_fd, gr_draw->data, frame_size);
469 }
470 return gr_draw;
471}
472
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500473int MinuiBackendOverlay::free_overlay(int fd)
Ethan Yonkera59da092015-10-13 19:35:05 -0500474{
475 int ret = 0;
476 struct mdp_display_commit ext_commit;
477
478 if (!isDisplaySplit()) {
479 if (overlayL_id != MSMFB_NEW_REQUEST) {
480 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayL_id);
481 if (ret) {
482 perror("Overlay Unset Failed");
483 overlayL_id = MSMFB_NEW_REQUEST;
484 return ret;
485 }
486 }
487 } else {
488
489 if (overlayL_id != MSMFB_NEW_REQUEST) {
490 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayL_id);
491 if (ret) {
492 perror("OverlayL Unset Failed");
493 overlayL_id = MSMFB_NEW_REQUEST;
494 return ret;
495 }
496 }
497
498 if (overlayR_id != MSMFB_NEW_REQUEST) {
499 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayR_id);
500 if (ret) {
501 perror("OverlayR Unset Failed");
502 overlayR_id = MSMFB_NEW_REQUEST;
503 return ret;
504 }
505 }
506 }
507 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
508 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
509 ext_commit.wait_for_finish = 1;
510 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
511 if (ret < 0) {
512 perror("ERROR: Clear MSMFB_DISPLAY_COMMIT failed!");
513 overlayL_id = MSMFB_NEW_REQUEST;
514 overlayR_id = MSMFB_NEW_REQUEST;
515 return ret;
516 }
517 overlayL_id = MSMFB_NEW_REQUEST;
518 overlayR_id = MSMFB_NEW_REQUEST;
519
520 return 0;
521}
522
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500523GRSurface* MinuiBackendOverlay::Init() {
524 if (!target_has_overlay())
525 return NULL;
526
Ethan Yonkera59da092015-10-13 19:35:05 -0500527 int fd = open("/dev/graphics/fb0", O_RDWR);
528 if (fd == -1) {
529 perror("cannot open fb0");
530 return NULL;
531 }
532
533 fb_fix_screeninfo fi;
534 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
535 perror("failed to get fb0 info");
536 close(fd);
537 return NULL;
538 }
539
540 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
541 perror("failed to get fb0 info");
542 close(fd);
543 return NULL;
544 }
545
546 // We print this out for informational purposes only, but
547 // throughout we assume that the framebuffer device uses an RGBX
548 // pixel format. This is the case for every development device I
549 // have access to. For some of those devices (eg, hammerhead aka
550 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
551 // different format (XBGR) but actually produces the correct
552 // results on the display when you write RGBX.
553 //
554 // If you have a device that actually *needs* another pixel format
555 // (ie, BGRX, or 565), patches welcome...
556
557 printf("fb0 reports (possibly inaccurate):\n"
558 " vi.bits_per_pixel = %d\n"
559 " vi.red.offset = %3d .length = %3d\n"
560 " vi.green.offset = %3d .length = %3d\n"
561 " vi.blue.offset = %3d .length = %3d\n",
562 vi.bits_per_pixel,
563 vi.red.offset, vi.red.length,
564 vi.green.offset, vi.green.length,
565 vi.blue.offset, vi.blue.length);
566
567 void* bits = malloc(vi.xres_virtual * vi.yres * (vi.bits_per_pixel / 8));
568 if (bits == NULL) {
569 perror("failed to malloc framebuffer");
570 close(fd);
571 return NULL;
572 }
573
574 memset(bits, 0, fi.smem_len);
575
576 gr_framebuffer[0].width = vi.xres;
577 gr_framebuffer[0].height = vi.yres;
578 gr_framebuffer[0].row_bytes = fi.line_length;
579 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
580 gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits);
581 memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
582
583 /* check if we can use double buffering */
584 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
585 double_buffered = true;
586 printf("double buffered.\n");
587 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
588 gr_framebuffer[1].data = gr_framebuffer[0].data +
589 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
590
591 gr_draw = gr_framebuffer+1;
592
593 } else {
594 double_buffered = false;
595 printf("single buffered.\n");
596 // Without double-buffering, we allocate RAM for a buffer to
597 // draw in, and then "flipping" the buffer consists of a
598 // memcpy from the buffer we allocated to the framebuffer.
599
600 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
601 if (gr_draw == NULL) {
602 printf("Failed to malloc gr_draw for single buffering.\n");
603 return NULL;
604 } else {
605 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
606 gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes);
607 if (!gr_draw->data) {
608 perror("failed to allocate in-memory surface");
609 return NULL;
610 }
611 }
612 }
613
614 memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
615 fb_fd = fd;
616 set_displayed_framebuffer(0);
617
618 frame_size = fi.line_length * vi.yres;
619
620 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
621
622 overlay_blank(backend, true);
623 overlay_blank(backend, false);
624
625 if (!alloc_ion_mem(fi.line_length * vi.yres))
626 allocate_overlay(fb_fd, gr_framebuffer);
627
628 return gr_draw;
629}
630
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500631MinuiBackendOverlay::~MinuiBackendOverlay() {
Ethan Yonkera59da092015-10-13 19:35:05 -0500632 free_overlay(fb_fd);
633 free_ion_mem();
634
635 close(fb_fd);
636 fb_fd = -1;
637
638 if (!double_buffered && gr_draw) {
639 free(gr_draw->data);
640 free(gr_draw);
641 }
642 gr_draw = NULL;
643 if (gr_framebuffer[0].data) {
644 free(gr_framebuffer[0].data);
645 gr_framebuffer[0].data = NULL;
646 }
647}
648#else // MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500649
650GRSurface* MinuiBackendOverlay::Flip() {
Ethan Yonkera59da092015-10-13 19:35:05 -0500651 return NULL;
652}
653
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500654GRSurface* MinuiBackendOverlay::Init() {
655 target_has_overlay(); // Don't care about return value, just for logging
Ethan Yonkera59da092015-10-13 19:35:05 -0500656 return NULL;
657}
658
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500659MinuiBackendOverlay::~MinuiBackendOverlay() {
Ethan Yonkera59da092015-10-13 19:35:05 -0500660 return;
661}
662#endif // MSM_BSP