1 /*
2 * Copyright (c) 2011, 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 #ifndef MDP_WRAPPER_H
31 #define MDP_WRAPPER_H
32 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
33 
34 /*
35 * In order to make overlay::mdp_wrapper shorter, please do something like:
36 * namespace mdpwrap = overlay::mdp_wrapper;
37 * */
38 
39 #include <linux/msm_mdp.h>
40 #include <linux/msm_rotator.h>
41 #include <sys/ioctl.h>
42 #include <utils/Log.h>
43 #include <errno.h>
44 #include "overlayUtils.h"
45 #include <utils/Trace.h>
46 
47 namespace overlay{
48 
49 namespace mdp_wrapper{
50 /* FBIOGET_FSCREENINFO */
51 bool getFScreenInfo(int fd, fb_fix_screeninfo& finfo);
52 
53 /* FBIOGET_VSCREENINFO */
54 bool getVScreenInfo(int fd, fb_var_screeninfo& vinfo);
55 
56 /* FBIOPUT_VSCREENINFO */
57 bool setVScreenInfo(int fd, fb_var_screeninfo& vinfo);
58 
59 /* MSM_ROTATOR_IOCTL_START */
60 bool startRotator(int fd, msm_rotator_img_info& rot);
61 
62 /* MSM_ROTATOR_IOCTL_ROTATE */
63 bool rotate(int fd, msm_rotator_data_info& rot);
64 
65 /* MSMFB_OVERLAY_SET */
66 bool setOverlay(int fd, mdp_overlay& ov);
67 
68 /* MSM_ROTATOR_IOCTL_FINISH */
69 bool endRotator(int fd, int sessionId);
70 
71 /* MSMFB_OVERLAY_UNSET */
72 bool unsetOverlay(int fd, int ovId);
73 
74 /* MSMFB_OVERLAY_GET */
75 bool getOverlay(int fd, mdp_overlay& ov);
76 
77 /* MSMFB_OVERLAY_PLAY */
78 bool play(int fd, msmfb_overlay_data& od);
79 
80 /* MSMFB_OVERLAY_3D */
81 bool set3D(int fd, msmfb_overlay_3d& ov);
82 
83 /* MSMFB_DISPLAY_COMMIT */
84 bool displayCommit(int fd);
85 
86 /* MSMFB_WRITEBACK_INIT, MSMFB_WRITEBACK_START */
87 bool wbInitStart(int fbfd);
88 
89 /* MSMFB_WRITEBACK_STOP, MSMFB_WRITEBACK_TERMINATE */
90 bool wbStopTerminate(int fbfd);
91 
92 /* MSMFB_WRITEBACK_QUEUE_BUFFER */
93 bool wbQueueBuffer(int fbfd, struct msmfb_data& fbData);
94 
95 /* MSMFB_WRITEBACK_DEQUEUE_BUFFER */
96 bool wbDequeueBuffer(int fbfd, struct msmfb_data& fbData);
97 
98 /* the following are helper functions for dumping
99  * msm_mdp and friends*/
100 void dump(const char* const s, const msmfb_overlay_data& ov);
101 void dump(const char* const s, const msmfb_data& ov);
102 void dump(const char* const s, const mdp_overlay& ov);
103 void dump(const char* const s, const msmfb_overlay_3d& ov);
104 void dump(const char* const s, const uint32_t u[], uint32_t cnt);
105 void dump(const char* const s, const msmfb_img& ov);
106 void dump(const char* const s, const mdp_rect& ov);
107 
108 /* and rotator */
109 void dump(const char* const s, const msm_rotator_img_info& rot);
110 void dump(const char* const s, const msm_rotator_data_info& rot);
111 
112 /* info */
113 void dump(const char* const s, const fb_fix_screeninfo& finfo);
114 void dump(const char* const s, const fb_var_screeninfo& vinfo);
115 
116 //---------------Inlines -------------------------------------
117 
getFScreenInfo(int fd,fb_fix_screeninfo & finfo)118 inline bool getFScreenInfo(int fd, fb_fix_screeninfo& finfo) {
119     ATRACE_CALL();
120     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) < 0) {
121         ALOGE("Failed to call ioctl FBIOGET_FSCREENINFO err=%s",
122                 strerror(errno));
123         return false;
124     }
125     return true;
126 }
127 
getVScreenInfo(int fd,fb_var_screeninfo & vinfo)128 inline bool getVScreenInfo(int fd, fb_var_screeninfo& vinfo) {
129     ATRACE_CALL();
130     if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0) {
131         ALOGE("Failed to call ioctl FBIOGET_VSCREENINFO err=%s",
132                 strerror(errno));
133         return false;
134     }
135     return true;
136 }
137 
setVScreenInfo(int fd,fb_var_screeninfo & vinfo)138 inline bool setVScreenInfo(int fd, fb_var_screeninfo& vinfo) {
139     ATRACE_CALL();
140     if (ioctl(fd, FBIOPUT_VSCREENINFO, &vinfo) < 0) {
141         ALOGE("Failed to call ioctl FBIOPUT_VSCREENINFO err=%s",
142                 strerror(errno));
143         return false;
144     }
145     return true;
146 }
147 
startRotator(int fd,msm_rotator_img_info & rot)148 inline bool startRotator(int fd, msm_rotator_img_info& rot) {
149     ATRACE_CALL();
150     if (ioctl(fd, MSM_ROTATOR_IOCTL_START, &rot) < 0){
151         ALOGE("Failed to call ioctl MSM_ROTATOR_IOCTL_START err=%s",
152                 strerror(errno));
153         return false;
154     }
155     return true;
156 }
157 
rotate(int fd,msm_rotator_data_info & rot)158 inline bool rotate(int fd, msm_rotator_data_info& rot) {
159     ATRACE_CALL();
160     if (ioctl(fd, MSM_ROTATOR_IOCTL_ROTATE, &rot) < 0) {
161         ALOGE("Failed to call ioctl MSM_ROTATOR_IOCTL_ROTATE err=%s",
162                 strerror(errno));
163         return false;
164     }
165     return true;
166 }
167 
setOverlay(int fd,mdp_overlay & ov)168 inline bool setOverlay(int fd, mdp_overlay& ov) {
169     ATRACE_CALL();
170     if (ioctl(fd, MSMFB_OVERLAY_SET, &ov) < 0) {
171         ALOGE("Failed to call ioctl MSMFB_OVERLAY_SET err=%s",
172                 strerror(errno));
173         return false;
174     }
175     return true;
176 }
177 
endRotator(int fd,uint32_t sessionId)178 inline bool endRotator(int fd, uint32_t sessionId) {
179     ATRACE_CALL();
180     if (ioctl(fd, MSM_ROTATOR_IOCTL_FINISH, &sessionId) < 0) {
181         ALOGE("Failed to call ioctl MSM_ROTATOR_IOCTL_FINISH err=%s",
182                 strerror(errno));
183         return false;
184     }
185     return true;
186 }
187 
unsetOverlay(int fd,int ovId)188 inline bool unsetOverlay(int fd, int ovId) {
189     ATRACE_CALL();
190     if (ioctl(fd, MSMFB_OVERLAY_UNSET, &ovId) < 0) {
191         ALOGE("Failed to call ioctl MSMFB_OVERLAY_UNSET err=%s",
192                 strerror(errno));
193         return false;
194     }
195     return true;
196 }
197 
getOverlay(int fd,mdp_overlay & ov)198 inline bool getOverlay(int fd, mdp_overlay& ov) {
199     ATRACE_CALL();
200     if (ioctl(fd, MSMFB_OVERLAY_GET, &ov) < 0) {
201         ALOGE("Failed to call ioctl MSMFB_OVERLAY_GET err=%s",
202                 strerror(errno));
203         return false;
204     }
205     return true;
206 }
207 
play(int fd,msmfb_overlay_data & od)208 inline bool play(int fd, msmfb_overlay_data& od) {
209     ATRACE_CALL();
210     if (ioctl(fd, MSMFB_OVERLAY_PLAY, &od) < 0) {
211         ALOGE("Failed to call ioctl MSMFB_OVERLAY_PLAY err=%s",
212                 strerror(errno));
213         return false;
214     }
215     return true;
216 }
217 
set3D(int fd,msmfb_overlay_3d & ov)218 inline bool set3D(int fd, msmfb_overlay_3d& ov) {
219     ATRACE_CALL();
220     if (ioctl(fd, MSMFB_OVERLAY_3D, &ov) < 0) {
221         ALOGE("Failed to call ioctl MSMFB_OVERLAY_3D err=%s",
222                 strerror(errno));
223         return false;
224     }
225     return true;
226 }
227 
displayCommit(int fd,mdp_display_commit & info)228 inline bool displayCommit(int fd, mdp_display_commit& info) {
229     ATRACE_CALL();
230     if(ioctl(fd, MSMFB_DISPLAY_COMMIT, &info) == -1) {
231         ALOGE("Failed to call ioctl MSMFB_DISPLAY_COMMIT err=%s",
232                 strerror(errno));
233         return false;
234     }
235     return true;
236 }
237 
wbInitStart(int fbfd)238 inline bool wbInitStart(int fbfd) {
239     ATRACE_CALL();
240     if(ioctl(fbfd, MSMFB_WRITEBACK_INIT, NULL) < 0) {
241         ALOGE("Failed to call ioctl MSMFB_WRITEBACK_INIT err=%s",
242                 strerror(errno));
243         return false;
244     }
245     if(ioctl(fbfd, MSMFB_WRITEBACK_START, NULL) < 0) {
246         ALOGE("Failed to call ioctl MSMFB_WRITEBACK_START err=%s",
247                 strerror(errno));
248         return false;
249     }
250     return true;
251 }
252 
wbStopTerminate(int fbfd)253 inline bool wbStopTerminate(int fbfd) {
254     ATRACE_CALL();
255     if(ioctl(fbfd, MSMFB_WRITEBACK_STOP, NULL) < 0) {
256         ALOGE("Failed to call ioctl MSMFB_WRITEBACK_STOP err=%s",
257                 strerror(errno));
258         return false;
259     }
260     if(ioctl(fbfd, MSMFB_WRITEBACK_TERMINATE, NULL) < 0) {
261         ALOGE("Failed to call ioctl MSMFB_WRITEBACK_TERMINATE err=%s",
262                 strerror(errno));
263         return false;
264     }
265     return true;
266 }
267 
wbQueueBuffer(int fbfd,struct msmfb_data & fbData)268 inline bool wbQueueBuffer(int fbfd, struct msmfb_data& fbData) {
269     ATRACE_CALL();
270     if(ioctl(fbfd, MSMFB_WRITEBACK_QUEUE_BUFFER, &fbData) < 0) {
271         ALOGE("Failed to call ioctl MSMFB_WRITEBACK_QUEUE_BUFFER err=%s",
272                 strerror(errno));
273         return false;
274     }
275     return true;
276 }
277 
wbDequeueBuffer(int fbfd,struct msmfb_data & fbData)278 inline bool wbDequeueBuffer(int fbfd, struct msmfb_data& fbData) {
279     ATRACE_CALL();
280     if(ioctl(fbfd, MSMFB_WRITEBACK_DEQUEUE_BUFFER, &fbData) < 0) {
281         ALOGE("Failed to call ioctl MSMFB_WRITEBACK_DEQUEUE_BUFFER err=%s",
282                 strerror(errno));
283         return false;
284     }
285     return true;
286 }
287 
288 /* dump funcs */
dump(const char * const s,const msmfb_overlay_data & ov)289 inline void dump(const char* const s, const msmfb_overlay_data& ov) {
290     ALOGE("%s msmfb_overlay_data id=%d",
291             s, ov.id);
292     dump("data", ov.data);
293 }
dump(const char * const s,const msmfb_data & ov)294 inline void dump(const char* const s, const msmfb_data& ov) {
295     ALOGE("%s msmfb_data offset=%d memid=%d id=%d flags=0x%x priv=%d",
296             s, ov.offset, ov.memory_id, ov.id, ov.flags, ov.priv);
297 }
dump(const char * const s,const mdp_overlay & ov)298 inline void dump(const char* const s, const mdp_overlay& ov) {
299     ALOGE("%s mdp_overlay z=%d fg=%d alpha=%d mask=%d flags=0x%x id=%d",
300             s, ov.z_order, ov.is_fg, ov.alpha,
301             ov.transp_mask, ov.flags, ov.id);
302     dump("src", ov.src);
303     dump("src_rect", ov.src_rect);
304     dump("dst_rect", ov.dst_rect);
305     /*
306     Commented off to prevent verbose logging, since user_data could have 8 or so
307     fields which are mostly 0
308     dump("user_data", ov.user_data,
309             sizeof(ov.user_data)/sizeof(ov.user_data[0]));
310     */
311 }
dump(const char * const s,const msmfb_img & ov)312 inline void dump(const char* const s, const msmfb_img& ov) {
313     ALOGE("%s msmfb_img w=%d h=%d format=%d %s",
314             s, ov.width, ov.height, ov.format,
315             overlay::utils::getFormatString(ov.format));
316 }
dump(const char * const s,const mdp_rect & ov)317 inline void dump(const char* const s, const mdp_rect& ov) {
318     ALOGE("%s mdp_rect x=%d y=%d w=%d h=%d",
319             s, ov.x, ov.y, ov.w, ov.h);
320 }
321 
dump(const char * const s,const msmfb_overlay_3d & ov)322 inline void dump(const char* const s, const msmfb_overlay_3d& ov) {
323     ALOGE("%s msmfb_overlay_3d 3d=%d w=%d h=%d",
324             s, ov.is_3d, ov.width, ov.height);
325 
326 }
dump(const char * const s,const uint32_t u[],uint32_t cnt)327 inline void dump(const char* const s, const uint32_t u[], uint32_t cnt) {
328     ALOGE("%s user_data cnt=%d", s, cnt);
329     for(uint32_t i=0; i < cnt; ++i) {
330         ALOGE("i=%d val=%d", i, u[i]);
331     }
332 }
dump(const char * const s,const msm_rotator_img_info & rot)333 inline void dump(const char* const s, const msm_rotator_img_info& rot) {
334     ALOGE("%s msm_rotator_img_info sessid=%u dstx=%d dsty=%d rot=%d, ena=%d scale=%d",
335             s, rot.session_id, rot.dst_x, rot.dst_y,
336             rot.rotations, rot.enable, rot.downscale_ratio);
337     dump("src", rot.src);
338     dump("dst", rot.dst);
339     dump("src_rect", rot.src_rect);
340 }
dump(const char * const s,const msm_rotator_data_info & rot)341 inline void dump(const char* const s, const msm_rotator_data_info& rot) {
342     ALOGE("%s msm_rotator_data_info sessid=%u verkey=%d",
343             s, rot.session_id, rot.version_key);
344     dump("src", rot.src);
345     dump("dst", rot.dst);
346     dump("src_chroma", rot.src_chroma);
347     dump("dst_chroma", rot.dst_chroma);
348 }
dump(const char * const s,const fb_fix_screeninfo & finfo)349 inline void dump(const char* const s, const fb_fix_screeninfo& finfo) {
350     ALOGE("%s fb_fix_screeninfo type=%d", s, finfo.type);
351 }
dump(const char * const s,const fb_var_screeninfo & vinfo)352 inline void dump(const char* const s, const fb_var_screeninfo& vinfo) {
353     ALOGE("%s fb_var_screeninfo xres=%d yres=%d",
354             s, vinfo.xres, vinfo.yres);
355 }
356 
357 } // mdp_wrapper
358 
359 } // overlay
360 
361 #endif // MDP_WRAPPER_H
362