blob: b7e62d945030ac4304707a2cc893d4f27c5fe59a [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
451 unsigned int idx;
452 unsigned char tmp;
453 unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data;
454 for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes);
455 idx += 4) {
456 tmp = ucfb_vaddr[idx];
457 ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2];
458 ucfb_vaddr[idx + 2] = tmp;
459 }
460#endif
461 // Change gr_draw to point to the buffer currently displayed,
462 // then flip the driver so we're displaying the other buffer
463 // instead.
464 gr_draw = gr_framebuffer + displayed_buffer;
Andreas Schneider06c62192017-11-02 17:57:37 +0100465 SetDisplayedFramebuffer(1-displayed_buffer);
Ethan Yonkera59da092015-10-13 19:35:05 -0500466 overlay_display_frame(fb_fd, gr_draw->data, frame_size);
467 } else {
468 // Copy from the in-memory surface to the framebuffer.
469 overlay_display_frame(fb_fd, gr_draw->data, frame_size);
470 }
471 return gr_draw;
472}
473
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500474int MinuiBackendOverlay::free_overlay(int fd)
Ethan Yonkera59da092015-10-13 19:35:05 -0500475{
476 int ret = 0;
477 struct mdp_display_commit ext_commit;
478
479 if (!isDisplaySplit()) {
480 if (overlayL_id != MSMFB_NEW_REQUEST) {
481 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayL_id);
482 if (ret) {
483 perror("Overlay Unset Failed");
484 overlayL_id = MSMFB_NEW_REQUEST;
485 return ret;
486 }
487 }
488 } else {
489
490 if (overlayL_id != MSMFB_NEW_REQUEST) {
491 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayL_id);
492 if (ret) {
493 perror("OverlayL Unset Failed");
494 overlayL_id = MSMFB_NEW_REQUEST;
495 return ret;
496 }
497 }
498
499 if (overlayR_id != MSMFB_NEW_REQUEST) {
500 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayR_id);
501 if (ret) {
502 perror("OverlayR Unset Failed");
503 overlayR_id = MSMFB_NEW_REQUEST;
504 return ret;
505 }
506 }
507 }
508 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
509 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
510 ext_commit.wait_for_finish = 1;
511 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
512 if (ret < 0) {
513 perror("ERROR: Clear MSMFB_DISPLAY_COMMIT failed!");
514 overlayL_id = MSMFB_NEW_REQUEST;
515 overlayR_id = MSMFB_NEW_REQUEST;
516 return ret;
517 }
518 overlayL_id = MSMFB_NEW_REQUEST;
519 overlayR_id = MSMFB_NEW_REQUEST;
520
521 return 0;
522}
523
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500524GRSurface* MinuiBackendOverlay::Init() {
Ethan Yonker58f21322018-08-24 11:17:36 -0500525 gr_draw = NULL; // this should be in the constructor but 9.0 was throwing a compile error
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500526 if (!target_has_overlay())
527 return NULL;
528
Ethan Yonkera59da092015-10-13 19:35:05 -0500529 int fd = open("/dev/graphics/fb0", O_RDWR);
530 if (fd == -1) {
531 perror("cannot open fb0");
532 return NULL;
533 }
534
535 fb_fix_screeninfo fi;
536 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
537 perror("failed to get fb0 info");
538 close(fd);
539 return NULL;
540 }
541
542 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
543 perror("failed to get fb0 info");
544 close(fd);
545 return NULL;
546 }
547
548 // We print this out for informational purposes only, but
549 // throughout we assume that the framebuffer device uses an RGBX
550 // pixel format. This is the case for every development device I
551 // have access to. For some of those devices (eg, hammerhead aka
552 // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a
553 // different format (XBGR) but actually produces the correct
554 // results on the display when you write RGBX.
555 //
556 // If you have a device that actually *needs* another pixel format
557 // (ie, BGRX, or 565), patches welcome...
558
559 printf("fb0 reports (possibly inaccurate):\n"
560 " vi.bits_per_pixel = %d\n"
561 " vi.red.offset = %3d .length = %3d\n"
562 " vi.green.offset = %3d .length = %3d\n"
563 " vi.blue.offset = %3d .length = %3d\n",
564 vi.bits_per_pixel,
565 vi.red.offset, vi.red.length,
566 vi.green.offset, vi.green.length,
567 vi.blue.offset, vi.blue.length);
568
569 void* bits = malloc(vi.xres_virtual * vi.yres * (vi.bits_per_pixel / 8));
570 if (bits == NULL) {
571 perror("failed to malloc framebuffer");
572 close(fd);
573 return NULL;
574 }
575
576 memset(bits, 0, fi.smem_len);
577
578 gr_framebuffer[0].width = vi.xres;
579 gr_framebuffer[0].height = vi.yres;
580 gr_framebuffer[0].row_bytes = fi.line_length;
581 gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
582 gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits);
583 memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
584
585 /* check if we can use double buffering */
586 if (vi.yres * fi.line_length * 2 <= fi.smem_len) {
587 double_buffered = true;
588 printf("double buffered.\n");
589 memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface));
590 gr_framebuffer[1].data = gr_framebuffer[0].data +
591 gr_framebuffer[0].height * gr_framebuffer[0].row_bytes;
592
593 gr_draw = gr_framebuffer+1;
594
595 } else {
596 double_buffered = false;
597 printf("single buffered.\n");
598 // Without double-buffering, we allocate RAM for a buffer to
599 // draw in, and then "flipping" the buffer consists of a
600 // memcpy from the buffer we allocated to the framebuffer.
601
602 gr_draw = (GRSurface*) malloc(sizeof(GRSurface));
603 if (gr_draw == NULL) {
604 printf("Failed to malloc gr_draw for single buffering.\n");
605 return NULL;
606 } else {
607 memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface));
608 gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes);
609 if (!gr_draw->data) {
610 perror("failed to allocate in-memory surface");
611 return NULL;
612 }
613 }
614 }
615
616 memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes);
617 fb_fd = fd;
Andreas Schneider06c62192017-11-02 17:57:37 +0100618 SetDisplayedFramebuffer(0);
Ethan Yonkera59da092015-10-13 19:35:05 -0500619
620 frame_size = fi.line_length * vi.yres;
621
622 printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height);
623
Andreas Schneider06c62192017-11-02 17:57:37 +0100624 Blank(true);
625 Blank(false);
Ethan Yonkera59da092015-10-13 19:35:05 -0500626
627 if (!alloc_ion_mem(fi.line_length * vi.yres))
628 allocate_overlay(fb_fd, gr_framebuffer);
629
630 return gr_draw;
631}
632
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500633MinuiBackendOverlay::~MinuiBackendOverlay() {
Ethan Yonkera59da092015-10-13 19:35:05 -0500634 free_overlay(fb_fd);
635 free_ion_mem();
636
637 close(fb_fd);
638 fb_fd = -1;
639
640 if (!double_buffered && gr_draw) {
641 free(gr_draw->data);
642 free(gr_draw);
643 }
644 gr_draw = NULL;
645 if (gr_framebuffer[0].data) {
646 free(gr_framebuffer[0].data);
647 gr_framebuffer[0].data = NULL;
648 }
649}
650#else // MSM_BSP
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500651
652GRSurface* MinuiBackendOverlay::Flip() {
Ethan Yonkera59da092015-10-13 19:35:05 -0500653 return NULL;
654}
655
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500656GRSurface* MinuiBackendOverlay::Init() {
657 target_has_overlay(); // Don't care about return value, just for logging
Ethan Yonkera59da092015-10-13 19:35:05 -0500658 return NULL;
659}
660
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500661MinuiBackendOverlay::~MinuiBackendOverlay() {
Ethan Yonkera59da092015-10-13 19:35:05 -0500662 return;
663}
664#endif // MSM_BSP