blob: 5988d70ade83153b3dd9dcfaa0b52109f2b80a30 [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
17#include <stdbool.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <errno.h>
22
23#include <fcntl.h>
24#include <stdio.h>
25
26#include <sys/cdefs.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
29#include <sys/types.h>
30
31#include <linux/fb.h>
32#include <linux/kd.h>
33
34#ifdef MSM_BSP
35#include <linux/msm_mdp.h>
36#include <linux/msm_ion.h>
Ethan Yonker8373cfe2017-09-08 06:50:54 -050037#else
38#define MSMFB_NEW_REQUEST 0
Ethan Yonkera59da092015-10-13 19:35:05 -050039#endif
40
Andreas Schneider9c820952017-11-02 17:56:56 +010041#include "graphics_overlay.h"
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() :
Ethan Yonker8373cfe2017-09-08 06:50:54 -050050 fb_fd(-1),
51 isMDP5(false),
52 leftSplit(0),
53 rightSplit(0),
54 frame_size(0),
55 overlayL_id(MSMFB_NEW_REQUEST),
56 overlayR_id(MSMFB_NEW_REQUEST) {}
Ethan Yonkera59da092015-10-13 19:35:05 -050057
58#ifdef MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -050059int MinuiBackendOverlay::map_mdp_pixel_format()
Ethan Yonkera59da092015-10-13 19:35:05 -050060{
61 int format = MDP_RGB_565;
62#if defined(RECOVERY_BGRA)
63 format = MDP_BGRA_8888;
Kra1o577568592015-10-14 18:09:54 +020064#elif defined(RECOVERY_RGBA)
65 format = MDP_RGBA_8888;
Ethan Yonkera59da092015-10-13 19:35:05 -050066#elif defined(RECOVERY_RGBX)
67 format = MDP_RGBA_8888;
68#endif
69 return format;
70}
Andreas Schneider06c62192017-11-02 17:57:37 +010071
72static memInfo mem_info;
73
Ethan Yonkera59da092015-10-13 19:35:05 -050074#endif // MSM_BSP
75
Ethan Yonker8373cfe2017-09-08 06:50:54 -050076bool MinuiBackendOverlay::target_has_overlay()
Ethan Yonkera59da092015-10-13 19:35:05 -050077{
Ethan Yonkera59da092015-10-13 19:35:05 -050078 int mdp_version;
79 bool overlay_supported = false;
Ethan Yonkera59da092015-10-13 19:35:05 -050080 fb_fix_screeninfo fi;
81 int fd;
82
83 fd = open("/dev/graphics/fb0", O_RDWR);
84 if (fd < 0) {
85 perror("open_overlay cannot open fb0");
Ethan Yonker8373cfe2017-09-08 06:50:54 -050086 return false;
Ethan Yonkera59da092015-10-13 19:35:05 -050087 }
88
89 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
90 perror("failed to get fb0 info");
91 close(fd);
Ethan Yonker8373cfe2017-09-08 06:50:54 -050092 return false;
Ethan Yonkera59da092015-10-13 19:35:05 -050093 }
94 close(fd);
Ethan Yonker8373cfe2017-09-08 06:50:54 -050095
96 if (strlen(fi.id) >= 8) {
97 if(!strncmp(fi.id, "msmfb", strlen("msmfb"))) {
98 char str_ver[4];
99 memcpy(str_ver, fi.id + strlen("msmfb"), 3);
100 str_ver[3] = '\0';
101 mdp_version = atoi(str_ver);
102 if (mdp_version >= MDP_V4_0) {
103 overlay_supported = true;
104 }
105 } else if (!strncmp(fi.id, "mdssfb", strlen("mdssfb"))) {
106 overlay_supported = true;
107 isMDP5 = true;
108 }
109 }
110#ifndef MSM_BSP
111 if (overlay_supported)
112 printf("Overlay graphics may work (%s), but not enabled. Use TW_TARGET_USES_QCOM_BSP := true to enable.\n", fi.id);
113#endif
114 return overlay_supported;
Ethan Yonkera59da092015-10-13 19:35:05 -0500115}
116
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500117void MinuiBackendOverlay::Blank(bool blank)
Ethan Yonkera59da092015-10-13 19:35:05 -0500118{
119#if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS)
120 int fd;
121 char brightness[4];
122 snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2);
123
124 fd = open(TW_BRIGHTNESS_PATH, O_RDWR);
125 if (fd < 0) {
126 perror("cannot open LCD backlight");
127 return;
128 }
129 write(fd, blank ? "000" : brightness, 3);
130 close(fd);
131#else
132 int ret;
133
134 ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
135 if (ret < 0)
136 perror("ioctl(): blank");
137#endif
138}
139
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500140void MinuiBackendOverlay::SetDisplayedFramebuffer(unsigned n)
Ethan Yonkera59da092015-10-13 19:35:05 -0500141{
142 if (n > 1 || !double_buffered) return;
143
144 vi.yres_virtual = gr_framebuffer[0].height * 2;
145 vi.yoffset = n * gr_framebuffer[0].height;
146 vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8;
147 if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
148 perror("active fb swap failed");
149 }
150 displayed_buffer = n;
151}
152
153#ifdef MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500154void MinuiBackendOverlay::setDisplaySplit(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500155 char split[64] = {0};
156 if (!isMDP5)
157 return;
158 FILE* fp = fopen("/sys/class/graphics/fb0/msm_fb_split", "r");
159 if (fp) {
160 //Format "left right" space as delimiter
161 if(fread(split, sizeof(char), 64, fp)) {
162 leftSplit = atoi(split);
163 printf("Left Split=%d\n",leftSplit);
164 char *rght = strpbrk(split, " ");
165 if (rght)
166 rightSplit = atoi(rght + 1);
167 printf("Right Split=%d\n", rightSplit);
168 }
169 } else {
170 printf("Failed to open mdss_fb_split node\n");
171 }
172 if (fp)
173 fclose(fp);
174}
175
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500176int MinuiBackendOverlay::getLeftSplit(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500177 //Default even split for all displays with high res
178 int lSplit = vi.xres / 2;
179
180 //Override if split published by driver
181 if (leftSplit)
182 lSplit = leftSplit;
183
184 return lSplit;
185}
186
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500187int MinuiBackendOverlay::getRightSplit(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500188 return rightSplit;
189}
190
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500191int MinuiBackendOverlay::free_ion_mem(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500192 int ret = 0;
193
194 if (mem_info.mem_buf)
195 munmap(mem_info.mem_buf, mem_info.size);
196
197 if (mem_info.ion_fd >= 0) {
198 ret = ioctl(mem_info.ion_fd, ION_IOC_FREE, &mem_info.handle_data);
199 if (ret < 0)
200 perror("free_mem failed ");
201 }
202
203 if (mem_info.mem_fd >= 0)
204 close(mem_info.mem_fd);
205 if (mem_info.ion_fd >= 0)
206 close(mem_info.ion_fd);
207
208 memset(&mem_info, 0, sizeof(mem_info));
209 mem_info.mem_fd = -1;
210 mem_info.ion_fd = -1;
211 return 0;
212}
213
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500214int MinuiBackendOverlay::alloc_ion_mem(unsigned int size)
Ethan Yonkera59da092015-10-13 19:35:05 -0500215{
216 int result;
217 struct ion_fd_data fd_data;
218 struct ion_allocation_data ionAllocData;
219
220 mem_info.ion_fd = open("/dev/ion", O_RDWR|O_DSYNC);
221 if (mem_info.ion_fd < 0) {
222 perror("ERROR: Can't open ion ");
223 return -errno;
224 }
225
226 ionAllocData.flags = 0;
227 ionAllocData.len = size;
228 ionAllocData.align = sysconf(_SC_PAGESIZE);
229#ifdef NEW_ION_HEAP
230 ionAllocData.heap_id_mask =
231#else
232 ionAllocData.heap_mask =
233#endif
234 ION_HEAP(ION_IOMMU_HEAP_ID) |
235 ION_HEAP(ION_SYSTEM_CONTIG_HEAP_ID);
236
237 result = ioctl(mem_info.ion_fd, ION_IOC_ALLOC, &ionAllocData);
238 if(result){
239 perror("ION_IOC_ALLOC Failed ");
240 close(mem_info.ion_fd);
241 return result;
242 }
243
244 fd_data.handle = ionAllocData.handle;
245 mem_info.handle_data.handle = ionAllocData.handle;
246 result = ioctl(mem_info.ion_fd, ION_IOC_MAP, &fd_data);
247 if (result) {
248 perror("ION_IOC_MAP Failed ");
249 free_ion_mem();
250 return result;
251 }
252 mem_info.mem_buf = (unsigned char *)mmap(NULL, size, PROT_READ |
253 PROT_WRITE, MAP_SHARED, fd_data.fd, 0);
254 mem_info.mem_fd = fd_data.fd;
255
256 if (!mem_info.mem_buf) {
257 perror("ERROR: mem_buf MAP_FAILED ");
258 free_ion_mem();
259 return -ENOMEM;
260 }
261
262 return 0;
263}
264
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500265bool MinuiBackendOverlay::isDisplaySplit(void) {
Ethan Yonkera59da092015-10-13 19:35:05 -0500266 if (vi.xres > MAX_DISPLAY_DIM)
267 return true;
268 //check if right split is set by driver
269 if (getRightSplit())
270 return true;
271
272 return false;
273}
274
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500275int MinuiBackendOverlay::allocate_overlay(int fd, GRSurface gr_fb[])
Ethan Yonkera59da092015-10-13 19:35:05 -0500276{
277 int ret = 0;
278
279 if (!isDisplaySplit()) {
280 // Check if overlay is already allocated
281 if (MSMFB_NEW_REQUEST == overlayL_id) {
282 struct mdp_overlay overlayL;
283
284 memset(&overlayL, 0 , sizeof (struct mdp_overlay));
285
286 /* Fill Overlay Data */
287 overlayL.src.width = ALIGN(gr_fb[0].width, 32);
288 overlayL.src.height = gr_fb[0].height;
289 overlayL.src.format = map_mdp_pixel_format();
290 overlayL.src_rect.w = gr_fb[0].width;
291 overlayL.src_rect.h = gr_fb[0].height;
292 overlayL.dst_rect.w = gr_fb[0].width;
293 overlayL.dst_rect.h = gr_fb[0].height;
294 overlayL.alpha = 0xFF;
295 overlayL.transp_mask = MDP_TRANSP_NOP;
296 overlayL.id = MSMFB_NEW_REQUEST;
297 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayL);
298 if (ret < 0) {
299 perror("Overlay Set Failed");
300 return ret;
301 }
302 overlayL_id = overlayL.id;
303 }
304 } else {
305 float xres = vi.xres;
306 int lSplit = getLeftSplit();
307 float lSplitRatio = lSplit / xres;
308 float lCropWidth = gr_fb[0].width * lSplitRatio;
309 int lWidth = lSplit;
310 int rWidth = gr_fb[0].width - lSplit;
311 int height = gr_fb[0].height;
312
313 if (MSMFB_NEW_REQUEST == overlayL_id) {
314
315 struct mdp_overlay overlayL;
316
317 memset(&overlayL, 0 , sizeof (struct mdp_overlay));
318
319 /* Fill OverlayL Data */
320 overlayL.src.width = ALIGN(gr_fb[0].width, 32);
321 overlayL.src.height = gr_fb[0].height;
322 overlayL.src.format = map_mdp_pixel_format();
323 overlayL.src_rect.x = 0;
324 overlayL.src_rect.y = 0;
325 overlayL.src_rect.w = lCropWidth;
326 overlayL.src_rect.h = gr_fb[0].height;
327 overlayL.dst_rect.x = 0;
328 overlayL.dst_rect.y = 0;
329 overlayL.dst_rect.w = lWidth;
330 overlayL.dst_rect.h = height;
331 overlayL.alpha = 0xFF;
332 overlayL.transp_mask = MDP_TRANSP_NOP;
333 overlayL.id = MSMFB_NEW_REQUEST;
334 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayL);
335 if (ret < 0) {
336 perror("OverlayL Set Failed");
337 return ret;
338 }
339 overlayL_id = overlayL.id;
340 }
341 if (MSMFB_NEW_REQUEST == overlayR_id) {
342 struct mdp_overlay overlayR;
343
344 memset(&overlayR, 0 , sizeof (struct mdp_overlay));
345
346 /* Fill OverlayR Data */
347 overlayR.src.width = ALIGN(gr_fb[0].width, 32);
348 overlayR.src.height = gr_fb[0].height;
349 overlayR.src.format = map_mdp_pixel_format();
350 overlayR.src_rect.x = lCropWidth;
351 overlayR.src_rect.y = 0;
352 overlayR.src_rect.w = gr_fb[0].width - lCropWidth;
353 overlayR.src_rect.h = gr_fb[0].height;
354 overlayR.dst_rect.x = 0;
355 overlayR.dst_rect.y = 0;
356 overlayR.dst_rect.w = rWidth;
357 overlayR.dst_rect.h = height;
358 overlayR.alpha = 0xFF;
359 overlayR.flags = MDSS_MDP_RIGHT_MIXER;
360 overlayR.transp_mask = MDP_TRANSP_NOP;
361 overlayR.id = MSMFB_NEW_REQUEST;
362 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayR);
363 if (ret < 0) {
364 perror("OverlayR Set Failed");
365 return ret;
366 }
367 overlayR_id = overlayR.id;
368 }
369
370 }
371 return 0;
372}
373
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500374int MinuiBackendOverlay::overlay_display_frame(int fd, void* data, size_t size)
Ethan Yonkera59da092015-10-13 19:35:05 -0500375{
376 int ret = 0;
377 struct msmfb_overlay_data ovdataL, ovdataR;
378 struct mdp_display_commit ext_commit;
379
380 if (!isDisplaySplit()) {
381 if (overlayL_id == MSMFB_NEW_REQUEST) {
382 perror("display_frame failed, no overlay\n");
383 return -EINVAL;
384 }
385
386 memcpy(mem_info.mem_buf, data, size);
387
388 memset(&ovdataL, 0, sizeof(struct msmfb_overlay_data));
389
390 ovdataL.id = overlayL_id;
391 ovdataL.data.flags = 0;
392 ovdataL.data.offset = 0;
393 ovdataL.data.memory_id = mem_info.mem_fd;
394 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataL);
395 if (ret < 0) {
396 perror("overlay_display_frame failed, overlay play Failed\n");
397 return ret;
398 }
399 } else {
400
401 if (overlayL_id == MSMFB_NEW_REQUEST) {
402 perror("display_frame failed, no overlayL \n");
403 return -EINVAL;
404 }
405
406 memcpy(mem_info.mem_buf, data, size);
407
408 memset(&ovdataL, 0, sizeof(struct msmfb_overlay_data));
409
410 ovdataL.id = overlayL_id;
411 ovdataL.data.flags = 0;
412 ovdataL.data.offset = 0;
413 ovdataL.data.memory_id = mem_info.mem_fd;
414 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataL);
415 if (ret < 0) {
416 perror("overlay_display_frame failed, overlayL play Failed\n");
417 return ret;
418 }
419
420 if (overlayR_id == MSMFB_NEW_REQUEST) {
421 perror("display_frame failed, no overlayR \n");
422 return -EINVAL;
423 }
424 memset(&ovdataR, 0, sizeof(struct msmfb_overlay_data));
425
426 ovdataR.id = overlayR_id;
427 ovdataR.data.flags = 0;
428 ovdataR.data.offset = 0;
429 ovdataR.data.memory_id = mem_info.mem_fd;
430 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataR);
431 if (ret < 0) {
432 perror("overlay_display_frame failed, overlayR play Failed\n");
433 return ret;
434 }
435 }
436 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
437 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
438 ext_commit.wait_for_finish = 1;
439 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
440 if (ret < 0) {
441 perror("overlay_display_frame failed, overlay commit Failed\n!");
442 }
443
444 return ret;
445}
446
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500447GRSurface* MinuiBackendOverlay::Flip() {
Ethan Yonkera59da092015-10-13 19:35:05 -0500448 if (double_buffered) {
449#if defined(RECOVERY_BGRA)
450 // In case of BGRA, do some byte swapping
Ethan Yonkera59da092015-10-13 19:35:05 -0500451 unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data;
Simon Shifcfc6342019-01-16 13:27:01 +0800452 for (int idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes);
Ethan Yonkera59da092015-10-13 19:35:05 -0500453 idx += 4) {
Simon Shifcfc6342019-01-16 13:27:01 +0800454 unsigned char tmp = ucfb_vaddr[idx];
Ethan Yonkera59da092015-10-13 19:35:05 -0500455 ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2];
456 ucfb_vaddr[idx + 2] = tmp;
457 }
458#endif
459 // Change gr_draw to point to the buffer currently displayed,
460 // then flip the driver so we're displaying the other buffer
461 // instead.
462 gr_draw = gr_framebuffer + displayed_buffer;
Andreas Schneider06c62192017-11-02 17:57:37 +0100463 SetDisplayedFramebuffer(1-displayed_buffer);
Ethan Yonkera59da092015-10-13 19:35:05 -0500464 overlay_display_frame(fb_fd, gr_draw->data, frame_size);
465 } else {
466 // Copy from the in-memory surface to the framebuffer.
467 overlay_display_frame(fb_fd, gr_draw->data, frame_size);
468 }
469 return gr_draw;
470}
471
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500472int MinuiBackendOverlay::free_overlay(int fd)
Ethan Yonkera59da092015-10-13 19:35:05 -0500473{
474 int ret = 0;
475 struct mdp_display_commit ext_commit;
476
477 if (!isDisplaySplit()) {
478 if (overlayL_id != MSMFB_NEW_REQUEST) {
479 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayL_id);
480 if (ret) {
481 perror("Overlay Unset Failed");
482 overlayL_id = MSMFB_NEW_REQUEST;
483 return ret;
484 }
485 }
486 } else {
487
488 if (overlayL_id != MSMFB_NEW_REQUEST) {
489 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayL_id);
490 if (ret) {
491 perror("OverlayL Unset Failed");
492 overlayL_id = MSMFB_NEW_REQUEST;
493 return ret;
494 }
495 }
496
497 if (overlayR_id != MSMFB_NEW_REQUEST) {
498 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayR_id);
499 if (ret) {
500 perror("OverlayR Unset Failed");
501 overlayR_id = MSMFB_NEW_REQUEST;
502 return ret;
503 }
504 }
505 }
506 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
507 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
508 ext_commit.wait_for_finish = 1;
509 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
510 if (ret < 0) {
511 perror("ERROR: Clear MSMFB_DISPLAY_COMMIT failed!");
512 overlayL_id = MSMFB_NEW_REQUEST;
513 overlayR_id = MSMFB_NEW_REQUEST;
514 return ret;
515 }
516 overlayL_id = MSMFB_NEW_REQUEST;
517 overlayR_id = MSMFB_NEW_REQUEST;
518
519 return 0;
520}
521
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500522GRSurface* MinuiBackendOverlay::Init() {
Ethan Yonker58f21322018-08-24 11:17:36 -0500523 gr_draw = NULL; // this should be in the constructor but 9.0 was throwing a compile error
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500524 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;
Andreas Schneider06c62192017-11-02 17:57:37 +0100616 SetDisplayedFramebuffer(0);
Ethan Yonkera59da092015-10-13 19:35:05 -0500617
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
Andreas Schneider06c62192017-11-02 17:57:37 +0100622 Blank(true);
623 Blank(false);
Ethan Yonkera59da092015-10-13 19:35:05 -0500624
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