blob: d793b57e6a43f1759b2769c2579bb232868b9a46 [file] [log] [blame]
Dees Troy62b75ab2014-05-02 13:20:33 +00001/*
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
46#include <linux/msm_mdp.h>
47#include <linux/msm_ion.h>
48#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
67static int overlay_id = MSMFB_NEW_REQUEST;
68static memInfo mem_info;
69
70static int map_mdp_pixel_format()
71{
72 int format = MDP_RGB_565;
73#if defined(RECOVERY_BGRA)
74 format = MDP_BGRA_8888;
75#elif defined(RECOVERY_RGBX)
76 format = MDP_RGBA_8888;
77#endif
78 return format;
79}
Dees Troy62b75ab2014-05-02 13:20:33 +000080
81static bool overlay_supported = false;
82
83bool target_has_overlay(char *version)
84{
85 int ret;
86 int mdp_version;
87
88 if (strlen(version) >= 8) {
89 if(!strncmp(version, "msmfb", strlen("msmfb"))) {
90 char str_ver[4];
91 memcpy(str_ver, version + strlen("msmfb"), 3);
92 str_ver[3] = '\0';
93 mdp_version = atoi(str_ver);
94 if (mdp_version >= MDP_V4_0) {
95 overlay_supported = true;
96 }
97 } else if (!strncmp(version, "mdssfb", strlen("mdssfb"))) {
98 overlay_supported = true;
99 }
100 }
101
102 return overlay_supported;
103}
104
Dees Troy62b75ab2014-05-02 13:20:33 +0000105int free_ion_mem(void) {
106 if (!overlay_supported)
107 return -EINVAL;
108
109 int ret = 0;
110
111 if (mem_info.mem_buf)
112 munmap(mem_info.mem_buf, mem_info.size);
113
114 if (mem_info.ion_fd >= 0) {
115 ret = ioctl(mem_info.ion_fd, ION_IOC_FREE, &mem_info.handle_data);
116 if (ret < 0)
117 perror("free_mem failed ");
118 }
119
120 if (mem_info.mem_fd >= 0)
121 close(mem_info.mem_fd);
122 if (mem_info.ion_fd >= 0)
123 close(mem_info.ion_fd);
124
125 memset(&mem_info, 0, sizeof(mem_info));
126 mem_info.mem_fd = -1;
127 mem_info.ion_fd = -1;
128 return 0;
129}
130
131int alloc_ion_mem(unsigned int size)
132{
133 if (!overlay_supported)
134 return -EINVAL;
135 int result;
136 struct ion_fd_data fd_data;
137 struct ion_allocation_data ionAllocData;
138
139 mem_info.ion_fd = open("/dev/ion", O_RDWR|O_DSYNC);
140 if (mem_info.ion_fd < 0) {
141 perror("ERROR: Can't open ion ");
142 return -errno;
143 }
144
145 ionAllocData.flags = 0;
146 ionAllocData.len = size;
147 ionAllocData.align = sysconf(_SC_PAGESIZE);
Kra1o5de50c5f2015-04-23 21:46:36 +0200148#ifdef NEW_ION_HEAP
149 ionAllocData.heap_id_mask =
150#else
Dees Troy62b75ab2014-05-02 13:20:33 +0000151 ionAllocData.heap_mask =
Kra1o5de50c5f2015-04-23 21:46:36 +0200152#endif
Dees Troy62b75ab2014-05-02 13:20:33 +0000153 ION_HEAP(ION_IOMMU_HEAP_ID) |
154 ION_HEAP(ION_SYSTEM_CONTIG_HEAP_ID);
155
156 result = ioctl(mem_info.ion_fd, ION_IOC_ALLOC, &ionAllocData);
157 if(result){
158 perror("ION_IOC_ALLOC Failed ");
159 close(mem_info.ion_fd);
160 return result;
161 }
162
163 fd_data.handle = ionAllocData.handle;
164 mem_info.handle_data.handle = ionAllocData.handle;
165 result = ioctl(mem_info.ion_fd, ION_IOC_MAP, &fd_data);
166 if (result) {
167 perror("ION_IOC_MAP Failed ");
168 free_ion_mem();
169 return result;
170 }
171 mem_info.mem_buf = (unsigned char *)mmap(NULL, size, PROT_READ |
172 PROT_WRITE, MAP_SHARED, fd_data.fd, 0);
173 mem_info.mem_fd = fd_data.fd;
174
175 if (!mem_info.mem_buf) {
176 perror("ERROR: mem_buf MAP_FAILED ");
177 free_ion_mem();
178 return -ENOMEM;
179 }
180
181 return 0;
182}
183
184int allocate_overlay(int fd, GGLSurface gr_fb[])
185{
186 if (!overlay_supported)
187 return -EINVAL;
188
189 struct mdp_overlay overlay;
190 int ret = 0;
191
192 memset(&overlay, 0 , sizeof (struct mdp_overlay));
193
194 /* Fill Overlay Data */
195
196 overlay.src.width = ALIGN(gr_fb[0].width, 32);
197 overlay.src.height = gr_fb[0].height;
198 overlay.src.format = map_mdp_pixel_format();
199 overlay.src_rect.w = gr_fb[0].width;
200 overlay.src_rect.h = gr_fb[0].height;
201 overlay.dst_rect.w = gr_fb[0].width;
202 overlay.dst_rect.h = gr_fb[0].height;
203 overlay.alpha = 0xFF;
204 overlay.transp_mask = MDP_TRANSP_NOP;
205 overlay.id = MSMFB_NEW_REQUEST;
206 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlay);
207 if (ret < 0) {
208 perror("Overlay Set Failed");
209 return ret;
210 }
211
212 overlay_id = overlay.id;
213 return 0;
214}
215
216int free_overlay(int fd)
217{
218 if (!overlay_supported)
219 return -EINVAL;
220
221 int ret = 0;
222 struct mdp_display_commit ext_commit;
223
224 if (overlay_id != MSMFB_NEW_REQUEST) {
225 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlay_id);
226 if (ret) {
227 perror("Overlay Unset Failed");
228 overlay_id = MSMFB_NEW_REQUEST;
229 return ret;
230 }
231
232 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
233 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
234 ext_commit.wait_for_finish = 1;
235 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
236 if (ret < 0) {
237 perror("ERROR: Clear MSMFB_DISPLAY_COMMIT failed!");
238 overlay_id = MSMFB_NEW_REQUEST;
239 return ret;
240 }
241
242 overlay_id = MSMFB_NEW_REQUEST;
243 }
244 return 0;
245}
246
247int overlay_display_frame(int fd, GGLubyte* data, size_t size)
248{
249 if (!overlay_supported)
250 return -EINVAL;
251 int ret = 0;
252 struct msmfb_overlay_data ovdata;
253 struct mdp_display_commit ext_commit;
254
255 if (overlay_id == MSMFB_NEW_REQUEST) {
256 perror("display_frame failed, no overlay\n");
257 return 0;
258 }
259
260 memcpy(mem_info.mem_buf, data, size);
261
262 memset(&ovdata, 0, sizeof(struct msmfb_overlay_data));
263
264 ovdata.id = overlay_id;
265 ovdata.data.flags = 0;
266 ovdata.data.offset = 0;
267 ovdata.data.memory_id = mem_info.mem_fd;
268 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdata);
269 if (ret < 0) {
270 perror("overlay_display_frame failed, overlay play Failed\n");
271 return 0;
272 }
273
274 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
275 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
276 ext_commit.wait_for_finish = 1;
277 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
278 if (ret < 0) {
279 perror("overlay_display_frame failed, overlay commit Failed\n!");
280 return 0;
281 }
282
283 return 0;
284}
285
286#else
287
Andrew Doddbb687f42014-06-29 18:24:35 -0400288bool target_has_overlay(char *version) {
289 return false;
290}
291
Dees Troy62b75ab2014-05-02 13:20:33 +0000292int free_ion_mem(void) {
293 return -EINVAL;
294}
295
296int alloc_ion_mem(unsigned int size)
297{
298 return -EINVAL;
299}
300
301int allocate_overlay(int fd, GGLSurface gr_fb[])
302{
303 return -EINVAL;
304}
305
306int free_overlay(int fd)
307{
308 return -EINVAL;
309}
310
311int overlay_display_frame(int fd, GGLubyte* data, size_t size)
312{
313 return -EINVAL;
314}
315
316#endif //#ifdef MSM_BSP