blob: 05c5472dcdb9fb90e4f9928f6f01469c9db863d4 [file] [log] [blame]
Ethan Yonker4ee5ad72014-02-18 18:41:17 -06001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#include <stdbool.h>
31#include <stdlib.h>
32#include <unistd.h>
33
34#include <errno.h>
35#include <fcntl.h>
36#include <stdio.h>
37
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/types.h>
41
42#include <linux/fb.h>
43#include <linux/kd.h>
44
45#ifdef MSM_BSP
Dees Troy62b75ab2014-05-02 13:20:33 +000046#include <linux/msm_mdp.h>
47#include <linux/msm_ion.h>
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060048#endif
49
50#include <pixelflinger/pixelflinger.h>
51
52#include "minui.h"
53
54#define MDP_V4_0 400
55
56#ifdef MSM_BSP
57#define ALIGN(x, align) (((x) + ((align)-1)) & ~((align)-1))
58
59typedef struct {
60 unsigned char *mem_buf;
61 int size;
62 int ion_fd;
63 int mem_fd;
64 struct ion_handle_data handle_data;
65} memInfo;
66
67//Left and right overlay id
68static int overlayL_id = MSMFB_NEW_REQUEST;
69static int overlayR_id = MSMFB_NEW_REQUEST;
70
71static memInfo mem_info;
72
73static int map_mdp_pixel_format()
74{
75 int format = MDP_RGB_565;
76#if defined(RECOVERY_BGRA)
77 format = MDP_BGRA_8888;
78#elif defined(RECOVERY_RGBX)
79 format = MDP_RGBA_8888;
80#endif
81 return format;
82}
Ethan Yonker4ee5ad72014-02-18 18:41:17 -060083
84static bool overlay_supported = false;
85static bool isMDP5 = false;
86
87bool target_has_overlay(char *version)
88{
89 int ret;
90 int mdp_version;
91
92 if (strlen(version) >= 8) {
93 if(!strncmp(version, "msmfb", strlen("msmfb"))) {
94 char str_ver[4];
95 memcpy(str_ver, version + strlen("msmfb"), 3);
96 str_ver[3] = '\0';
97 mdp_version = atoi(str_ver);
98 if (mdp_version >= MDP_V4_0) {
99 overlay_supported = true;
100 }
101 } else if (!strncmp(version, "mdssfb", strlen("mdssfb"))) {
102 overlay_supported = true;
103 isMDP5 = true;
104 }
105 }
Andrew Doddbb687f42014-06-29 18:24:35 -0400106 if (overlay_supported) printf("Using qcomm overlay\n");
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600107 return overlay_supported;
108}
109
110bool isTargetMdp5()
111{
112 if (isMDP5)
113 return true;
114
115 return false;
116}
117
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600118int free_ion_mem(void) {
119 if (!overlay_supported)
120 return -EINVAL;
121
122 int ret = 0;
123
124 if (mem_info.mem_buf)
125 munmap(mem_info.mem_buf, mem_info.size);
126
127 if (mem_info.ion_fd >= 0) {
128 ret = ioctl(mem_info.ion_fd, ION_IOC_FREE, &mem_info.handle_data);
129 if (ret < 0)
130 perror("free_mem failed ");
131 }
132
133 if (mem_info.mem_fd >= 0)
134 close(mem_info.mem_fd);
135 if (mem_info.ion_fd >= 0)
136 close(mem_info.ion_fd);
137
138 memset(&mem_info, 0, sizeof(mem_info));
139 mem_info.mem_fd = -1;
140 mem_info.ion_fd = -1;
141 return 0;
142}
143
144int alloc_ion_mem(unsigned int size)
145{
146 if (!overlay_supported)
147 return -EINVAL;
148 int result;
149 struct ion_fd_data fd_data;
150 struct ion_allocation_data ionAllocData;
151
152 mem_info.ion_fd = open("/dev/ion", O_RDWR|O_DSYNC);
153 if (mem_info.ion_fd < 0) {
154 perror("ERROR: Can't open ion ");
155 return -errno;
156 }
157
158 ionAllocData.flags = 0;
159 ionAllocData.len = size;
160 ionAllocData.align = sysconf(_SC_PAGESIZE);
161 ionAllocData.heap_mask =
162 ION_HEAP(ION_IOMMU_HEAP_ID) |
163 ION_HEAP(ION_SYSTEM_CONTIG_HEAP_ID);
164
165 result = ioctl(mem_info.ion_fd, ION_IOC_ALLOC, &ionAllocData);
166 if(result){
167 perror("ION_IOC_ALLOC Failed ");
168 close(mem_info.ion_fd);
169 return result;
170 }
171
172 fd_data.handle = ionAllocData.handle;
173 mem_info.handle_data.handle = ionAllocData.handle;
174 result = ioctl(mem_info.ion_fd, ION_IOC_MAP, &fd_data);
175 if (result) {
176 perror("ION_IOC_MAP Failed ");
177 free_ion_mem();
178 return result;
179 }
180 mem_info.mem_buf = (unsigned char *)mmap(NULL, size, PROT_READ |
181 PROT_WRITE, MAP_SHARED, fd_data.fd, 0);
182 mem_info.mem_fd = fd_data.fd;
183
184 if (!mem_info.mem_buf) {
185 perror("ERROR: mem_buf MAP_FAILED ");
186 free_ion_mem();
187 return -ENOMEM;
188 }
189
190 return 0;
191}
192
193int allocate_overlay(int fd, GGLSurface gr_fb[])
194{
195 int ret = 0;
196
197 if (!overlay_supported)
198 return -EINVAL;
199
200 if (!isDisplaySplit()) {
201 // Check if overlay is already allocated
202 if (MSMFB_NEW_REQUEST == overlayL_id) {
203 struct mdp_overlay overlayL;
204
205 memset(&overlayL, 0 , sizeof (struct mdp_overlay));
206
207 /* Fill Overlay Data */
208 overlayL.src.width = ALIGN(gr_fb[0].width, 32);
209 overlayL.src.height = gr_fb[0].height;
210 overlayL.src.format = map_mdp_pixel_format();
211 overlayL.src_rect.w = gr_fb[0].width;
212 overlayL.src_rect.h = gr_fb[0].height;
213 overlayL.dst_rect.w = gr_fb[0].width;
214 overlayL.dst_rect.h = gr_fb[0].height;
215 overlayL.alpha = 0xFF;
216 overlayL.transp_mask = MDP_TRANSP_NOP;
217 overlayL.id = MSMFB_NEW_REQUEST;
218 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayL);
219 if (ret < 0) {
220 perror("Overlay Set Failed");
221 return ret;
222 }
223 overlayL_id = overlayL.id;
224 }
225 } else {
226 float xres = getFbXres();
227 int lSplit = getLeftSplit();
228 float lSplitRatio = lSplit / xres;
229 float lCropWidth = gr_fb[0].width * lSplitRatio;
230 int lWidth = lSplit;
231 int rWidth = gr_fb[0].width - lSplit;
232 int height = gr_fb[0].height;
233
234 if (MSMFB_NEW_REQUEST == overlayL_id) {
235
236 struct mdp_overlay overlayL;
237
238 memset(&overlayL, 0 , sizeof (struct mdp_overlay));
239
240 /* Fill OverlayL Data */
241 overlayL.src.width = ALIGN(gr_fb[0].width, 32);
242 overlayL.src.height = gr_fb[0].height;
243 overlayL.src.format = map_mdp_pixel_format();
244 overlayL.src_rect.x = 0;
245 overlayL.src_rect.y = 0;
246 overlayL.src_rect.w = lCropWidth;
247 overlayL.src_rect.h = gr_fb[0].height;
248 overlayL.dst_rect.x = 0;
249 overlayL.dst_rect.y = 0;
250 overlayL.dst_rect.w = lWidth;
251 overlayL.dst_rect.h = height;
252 overlayL.alpha = 0xFF;
253 overlayL.transp_mask = MDP_TRANSP_NOP;
254 overlayL.id = MSMFB_NEW_REQUEST;
255 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayL);
256 if (ret < 0) {
257 perror("OverlayL Set Failed");
258 return ret;
259 }
260 overlayL_id = overlayL.id;
261 }
262 if (MSMFB_NEW_REQUEST == overlayR_id) {
263 struct mdp_overlay overlayR;
264
265 memset(&overlayR, 0 , sizeof (struct mdp_overlay));
266
267 /* Fill OverlayR Data */
268 overlayR.src.width = ALIGN(gr_fb[0].width, 32);
269 overlayR.src.height = gr_fb[0].height;
270 overlayR.src.format = map_mdp_pixel_format();
271 overlayR.src_rect.x = lCropWidth;
272 overlayR.src_rect.y = 0;
273 overlayR.src_rect.w = gr_fb[0].width - lCropWidth;
274 overlayR.src_rect.h = gr_fb[0].height;
275 overlayR.dst_rect.x = 0;
276 overlayR.dst_rect.y = 0;
277 overlayR.dst_rect.w = rWidth;
278 overlayR.dst_rect.h = height;
279 overlayR.alpha = 0xFF;
280 overlayR.flags = MDSS_MDP_RIGHT_MIXER;
281 overlayR.transp_mask = MDP_TRANSP_NOP;
282 overlayR.id = MSMFB_NEW_REQUEST;
283 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlayR);
284 if (ret < 0) {
285 perror("OverlayR Set Failed");
286 return ret;
287 }
288 overlayR_id = overlayR.id;
289 }
290
291 }
292 return 0;
293}
294
295int free_overlay(int fd)
296{
297 if (!overlay_supported)
298 return -EINVAL;
299
300 int ret = 0;
301 struct mdp_display_commit ext_commit;
302
303 if (!isDisplaySplit()) {
304 if (overlayL_id != MSMFB_NEW_REQUEST) {
305 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayL_id);
306 if (ret) {
307 perror("Overlay Unset Failed");
308 overlayL_id = MSMFB_NEW_REQUEST;
309 return ret;
310 }
311 }
312 } else {
313
314 if (overlayL_id != MSMFB_NEW_REQUEST) {
315 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayL_id);
316 if (ret) {
317 perror("OverlayL Unset Failed");
318 overlayL_id = MSMFB_NEW_REQUEST;
319 return ret;
320 }
321 }
322
323 if (overlayR_id != MSMFB_NEW_REQUEST) {
324 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlayR_id);
325 if (ret) {
326 perror("OverlayR Unset Failed");
327 overlayR_id = MSMFB_NEW_REQUEST;
328 return ret;
329 }
330 }
331 }
332 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
333 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
334 ext_commit.wait_for_finish = 1;
335 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
336 if (ret < 0) {
337 perror("ERROR: Clear MSMFB_DISPLAY_COMMIT failed!");
338 overlayL_id = MSMFB_NEW_REQUEST;
339 overlayR_id = MSMFB_NEW_REQUEST;
340 return ret;
341 }
342 overlayL_id = MSMFB_NEW_REQUEST;
343 overlayR_id = MSMFB_NEW_REQUEST;
344
345 return 0;
346}
347
348int overlay_display_frame(int fd, GGLubyte* data, size_t size)
349{
350 if (!overlay_supported)
351 return -EINVAL;
352
353 int ret = 0;
354 struct msmfb_overlay_data ovdataL, ovdataR;
355 struct mdp_display_commit ext_commit;
356
357 if (!isDisplaySplit()) {
358 if (overlayL_id == MSMFB_NEW_REQUEST) {
359 perror("display_frame failed, no overlay\n");
360 return -EINVAL;
361 }
362
363 memcpy(mem_info.mem_buf, data, size);
364
365 memset(&ovdataL, 0, sizeof(struct msmfb_overlay_data));
366
367 ovdataL.id = overlayL_id;
368 ovdataL.data.flags = 0;
369 ovdataL.data.offset = 0;
370 ovdataL.data.memory_id = mem_info.mem_fd;
371 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataL);
372 if (ret < 0) {
373 perror("overlay_display_frame failed, overlay play Failed\n");
374 return ret;
375 }
376 } else {
377
378 if (overlayL_id == MSMFB_NEW_REQUEST) {
379 perror("display_frame failed, no overlayL \n");
380 return -EINVAL;
381 }
382
383 memcpy(mem_info.mem_buf, data, size);
384
385 memset(&ovdataL, 0, sizeof(struct msmfb_overlay_data));
386
387 ovdataL.id = overlayL_id;
388 ovdataL.data.flags = 0;
389 ovdataL.data.offset = 0;
390 ovdataL.data.memory_id = mem_info.mem_fd;
391 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataL);
392 if (ret < 0) {
393 perror("overlay_display_frame failed, overlayL play Failed\n");
394 return ret;
395 }
396
397 if (overlayR_id == MSMFB_NEW_REQUEST) {
398 perror("display_frame failed, no overlayR \n");
399 return -EINVAL;
400 }
401 memset(&ovdataR, 0, sizeof(struct msmfb_overlay_data));
402
403 ovdataR.id = overlayR_id;
404 ovdataR.data.flags = 0;
405 ovdataR.data.offset = 0;
406 ovdataR.data.memory_id = mem_info.mem_fd;
407 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdataR);
408 if (ret < 0) {
409 perror("overlay_display_frame failed, overlayR play Failed\n");
410 return ret;
411 }
412 }
413 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
414 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
415 ext_commit.wait_for_finish = 1;
416 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
417 if (ret < 0) {
418 perror("overlay_display_frame failed, overlay commit Failed\n!");
419 }
420
421 return ret;
422}
423
424#else
425
Andrew Doddbb687f42014-06-29 18:24:35 -0400426bool target_has_overlay(char *version) {
427 return false;
428}
429
430bool isTargetMdp5() {
431 return false;
432}
433
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600434int free_ion_mem(void) {
435 return -EINVAL;
436}
437
438int alloc_ion_mem(unsigned int size)
439{
440 return -EINVAL;
441}
442
443int allocate_overlay(int fd, GGLSurface gr_fb[])
444{
445 return -EINVAL;
446}
447
448int free_overlay(int fd)
449{
450 return -EINVAL;
451}
452
453int overlay_display_frame(int fd, GGLubyte* data, size_t size)
454{
455 return -EINVAL;
456}
457
458#endif //#ifdef MSM_BSP