blob: 5677ad73dc5b0707534b5d3f51f02c3d861168cc [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);
148 ionAllocData.heap_mask =
149 ION_HEAP(ION_IOMMU_HEAP_ID) |
150 ION_HEAP(ION_SYSTEM_CONTIG_HEAP_ID);
151
152 result = ioctl(mem_info.ion_fd, ION_IOC_ALLOC, &ionAllocData);
153 if(result){
154 perror("ION_IOC_ALLOC Failed ");
155 close(mem_info.ion_fd);
156 return result;
157 }
158
159 fd_data.handle = ionAllocData.handle;
160 mem_info.handle_data.handle = ionAllocData.handle;
161 result = ioctl(mem_info.ion_fd, ION_IOC_MAP, &fd_data);
162 if (result) {
163 perror("ION_IOC_MAP Failed ");
164 free_ion_mem();
165 return result;
166 }
167 mem_info.mem_buf = (unsigned char *)mmap(NULL, size, PROT_READ |
168 PROT_WRITE, MAP_SHARED, fd_data.fd, 0);
169 mem_info.mem_fd = fd_data.fd;
170
171 if (!mem_info.mem_buf) {
172 perror("ERROR: mem_buf MAP_FAILED ");
173 free_ion_mem();
174 return -ENOMEM;
175 }
176
177 return 0;
178}
179
180int allocate_overlay(int fd, GGLSurface gr_fb[])
181{
182 if (!overlay_supported)
183 return -EINVAL;
184
185 struct mdp_overlay overlay;
186 int ret = 0;
187
188 memset(&overlay, 0 , sizeof (struct mdp_overlay));
189
190 /* Fill Overlay Data */
191
192 overlay.src.width = ALIGN(gr_fb[0].width, 32);
193 overlay.src.height = gr_fb[0].height;
194 overlay.src.format = map_mdp_pixel_format();
195 overlay.src_rect.w = gr_fb[0].width;
196 overlay.src_rect.h = gr_fb[0].height;
197 overlay.dst_rect.w = gr_fb[0].width;
198 overlay.dst_rect.h = gr_fb[0].height;
199 overlay.alpha = 0xFF;
200 overlay.transp_mask = MDP_TRANSP_NOP;
201 overlay.id = MSMFB_NEW_REQUEST;
202 ret = ioctl(fd, MSMFB_OVERLAY_SET, &overlay);
203 if (ret < 0) {
204 perror("Overlay Set Failed");
205 return ret;
206 }
207
208 overlay_id = overlay.id;
209 return 0;
210}
211
212int free_overlay(int fd)
213{
214 if (!overlay_supported)
215 return -EINVAL;
216
217 int ret = 0;
218 struct mdp_display_commit ext_commit;
219
220 if (overlay_id != MSMFB_NEW_REQUEST) {
221 ret = ioctl(fd, MSMFB_OVERLAY_UNSET, &overlay_id);
222 if (ret) {
223 perror("Overlay Unset Failed");
224 overlay_id = MSMFB_NEW_REQUEST;
225 return ret;
226 }
227
228 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
229 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
230 ext_commit.wait_for_finish = 1;
231 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
232 if (ret < 0) {
233 perror("ERROR: Clear MSMFB_DISPLAY_COMMIT failed!");
234 overlay_id = MSMFB_NEW_REQUEST;
235 return ret;
236 }
237
238 overlay_id = MSMFB_NEW_REQUEST;
239 }
240 return 0;
241}
242
243int overlay_display_frame(int fd, GGLubyte* data, size_t size)
244{
245 if (!overlay_supported)
246 return -EINVAL;
247 int ret = 0;
248 struct msmfb_overlay_data ovdata;
249 struct mdp_display_commit ext_commit;
250
251 if (overlay_id == MSMFB_NEW_REQUEST) {
252 perror("display_frame failed, no overlay\n");
253 return 0;
254 }
255
256 memcpy(mem_info.mem_buf, data, size);
257
258 memset(&ovdata, 0, sizeof(struct msmfb_overlay_data));
259
260 ovdata.id = overlay_id;
261 ovdata.data.flags = 0;
262 ovdata.data.offset = 0;
263 ovdata.data.memory_id = mem_info.mem_fd;
264 ret = ioctl(fd, MSMFB_OVERLAY_PLAY, &ovdata);
265 if (ret < 0) {
266 perror("overlay_display_frame failed, overlay play Failed\n");
267 return 0;
268 }
269
270 memset(&ext_commit, 0, sizeof(struct mdp_display_commit));
271 ext_commit.flags = MDP_DISPLAY_COMMIT_OVERLAY;
272 ext_commit.wait_for_finish = 1;
273 ret = ioctl(fd, MSMFB_DISPLAY_COMMIT, &ext_commit);
274 if (ret < 0) {
275 perror("overlay_display_frame failed, overlay commit Failed\n!");
276 return 0;
277 }
278
279 return 0;
280}
281
282#else
283
Andrew Doddbb687f42014-06-29 18:24:35 -0400284bool target_has_overlay(char *version) {
285 return false;
286}
287
Dees Troy62b75ab2014-05-02 13:20:33 +0000288int free_ion_mem(void) {
289 return -EINVAL;
290}
291
292int alloc_ion_mem(unsigned int size)
293{
294 return -EINVAL;
295}
296
297int allocate_overlay(int fd, GGLSurface gr_fb[])
298{
299 return -EINVAL;
300}
301
302int free_overlay(int fd)
303{
304 return -EINVAL;
305}
306
307int overlay_display_frame(int fd, GGLubyte* data, size_t size)
308{
309 return -EINVAL;
310}
311
312#endif //#ifdef MSM_BSP