1 /*
2 * Copyright (c) 2011-2014, 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 <stdlib.h>
31 #include <math.h>
32 #include <utils/Log.h>
33 #include <linux/msm_mdp.h>
34 #include <cutils/properties.h>
35 #include "gralloc_priv.h"
36 #include "overlayUtils.h"
37 #include "mdpWrapper.h"
38 #include "mdp_version.h"
39 #include <hardware/hwcomposer_defs.h>
40 
41 // just a helper static thingy
42 namespace {
43 struct IOFile {
IOFile__anon0cbc89620111::IOFile44     IOFile(const char* s, const char* mode) : fp(0) {
45         fp = ::fopen(s, mode);
46         if(!fp) {
47             ALOGE("Failed open %s", s);
48         }
49     }
50     template <class T>
read__anon0cbc89620111::IOFile51             size_t read(T& r, size_t elem) {
52                 if(fp) {
53                     return ::fread(&r, sizeof(T), elem, fp);
54                 }
55                 return 0;
56             }
write__anon0cbc89620111::IOFile57     size_t write(const char* s, uint32_t val) {
58         if(fp) {
59             return ::fprintf(fp, s, val);
60         }
61         return 0;
62     }
valid__anon0cbc89620111::IOFile63     bool valid() const { return fp != 0; }
~IOFile__anon0cbc89620111::IOFile64     ~IOFile() {
65         if(fp) ::fclose(fp);
66         fp=0;
67     }
68     FILE* fp;
69 };
70 }
71 
72 namespace overlay {
73 
74 //----------From class Res ------------------------------
75 const char* const Res::fbPath = "/dev/graphics/fb%u";
76 const char* const Res::rotPath = "/dev/msm_rotator";
77 //--------------------------------------------------------
78 
79 
80 
81 namespace utils {
82 
83 //--------------------------------------------------------
84 //Refer to graphics.h, gralloc_priv.h, msm_mdp.h
getMdpFormat(int format)85 int getMdpFormat(int format) {
86     switch (format) {
87         //From graphics.h
88         case HAL_PIXEL_FORMAT_RGBA_8888 :
89             return MDP_RGBA_8888;
90         case HAL_PIXEL_FORMAT_RGBX_8888:
91             return MDP_RGBX_8888;
92         case HAL_PIXEL_FORMAT_RGB_888:
93             return MDP_RGB_888;
94         case HAL_PIXEL_FORMAT_RGB_565:
95             return MDP_RGB_565;
96         case HAL_PIXEL_FORMAT_BGRA_8888:
97             return MDP_BGRA_8888;
98         case HAL_PIXEL_FORMAT_BGRX_8888:
99             return MDP_BGRX_8888;
100         case HAL_PIXEL_FORMAT_YV12:
101             return MDP_Y_CR_CB_GH2V2;
102         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
103             return MDP_Y_CBCR_H2V1;
104         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
105             return MDP_Y_CRCB_H2V2;
106 
107         //From gralloc_priv.h
108         case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
109             return MDP_Y_CBCR_H2V2_TILE;
110         case HAL_PIXEL_FORMAT_YCbCr_420_SP:
111             return MDP_Y_CBCR_H2V2;
112         case HAL_PIXEL_FORMAT_YCrCb_422_SP:
113             return MDP_Y_CRCB_H2V1;
114         case HAL_PIXEL_FORMAT_YCbCr_422_I:
115             return MDP_YCBYCR_H2V1;
116         case HAL_PIXEL_FORMAT_YCrCb_422_I:
117             return MDP_YCRYCB_H2V1;
118         case HAL_PIXEL_FORMAT_YCbCr_444_SP:
119             return MDP_Y_CBCR_H1V1;
120         case HAL_PIXEL_FORMAT_YCrCb_444_SP:
121             return MDP_Y_CRCB_H1V1;
122         case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
123         case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
124             //NV12 encodeable format maps to the venus format on
125             //B-Family targets
126             return MDP_Y_CBCR_H2V2_VENUS;
127         default:
128             //Unsupported by MDP
129             //---graphics.h--------
130             //HAL_PIXEL_FORMAT_RGBA_5551
131             //HAL_PIXEL_FORMAT_RGBA_4444
132             //---gralloc_priv.h-----
133             //HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO    = 0x7FA30C01
134             //HAL_PIXEL_FORMAT_R_8                    = 0x10D
135             //HAL_PIXEL_FORMAT_RG_88                  = 0x10E
136             ALOGE("%s: Unsupported HAL format = 0x%x", __func__, format);
137             return -1;
138     }
139     // not reached
140     return -1;
141 }
142 
143 // This function returns corresponding tile format
144 // MDSS support following RGB tile formats
145 //  32 bit formats
146 //  16 bit formats
getMdpFormat(int format,bool tileEnabled)147 int getMdpFormat(int format, bool tileEnabled)
148 {
149     if(!tileEnabled) {
150         return getMdpFormat(format);
151     }
152     switch (format) {
153         case HAL_PIXEL_FORMAT_RGBA_8888 :
154             return MDP_RGBA_8888_TILE;
155         case HAL_PIXEL_FORMAT_RGBX_8888:
156             return MDP_RGBX_8888_TILE;
157         case HAL_PIXEL_FORMAT_RGB_565:
158             return MDP_RGB_565_TILE;
159         case HAL_PIXEL_FORMAT_BGRA_8888:
160             return MDP_BGRA_8888_TILE;
161         case HAL_PIXEL_FORMAT_BGRX_8888:
162             return MDP_BGRX_8888_TILE;
163         default:
164             return getMdpFormat(format);
165     }
166 }
167 
168 
169 
170 //Takes mdp format as input and translates to equivalent HAL format
171 //Refer to graphics.h, gralloc_priv.h, msm_mdp.h for formats.
getHALFormat(int mdpFormat)172 int getHALFormat(int mdpFormat) {
173     switch (mdpFormat) {
174         //From graphics.h
175         case MDP_RGBA_8888:
176             return HAL_PIXEL_FORMAT_RGBA_8888;
177         case MDP_RGBX_8888:
178             return HAL_PIXEL_FORMAT_RGBX_8888;
179         case MDP_RGB_888:
180             return HAL_PIXEL_FORMAT_RGB_888;
181         case MDP_RGB_565:
182             return HAL_PIXEL_FORMAT_RGB_565;
183         case MDP_BGRA_8888:
184             return HAL_PIXEL_FORMAT_BGRA_8888;
185         case MDP_Y_CR_CB_GH2V2:
186             return HAL_PIXEL_FORMAT_YV12;
187         case MDP_Y_CBCR_H2V1:
188             return HAL_PIXEL_FORMAT_YCbCr_422_SP;
189         case MDP_Y_CRCB_H2V2:
190             return HAL_PIXEL_FORMAT_YCrCb_420_SP;
191 
192         //From gralloc_priv.h
193         case MDP_Y_CBCR_H2V2_TILE:
194             return HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED;
195         case MDP_Y_CBCR_H2V2:
196             return HAL_PIXEL_FORMAT_YCbCr_420_SP;
197         case MDP_Y_CRCB_H2V1:
198             return HAL_PIXEL_FORMAT_YCrCb_422_SP;
199         case MDP_YCBYCR_H2V1:
200             return HAL_PIXEL_FORMAT_YCbCr_422_I;
201         case MDP_YCRYCB_H2V1:
202             return HAL_PIXEL_FORMAT_YCrCb_422_I;
203          case MDP_Y_CBCR_H1V1:
204             return HAL_PIXEL_FORMAT_YCbCr_444_SP;
205         case MDP_Y_CRCB_H1V1:
206             return HAL_PIXEL_FORMAT_YCrCb_444_SP;
207         case MDP_Y_CBCR_H2V2_VENUS:
208             return HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
209         default:
210             ALOGE("%s: Unsupported MDP format = 0x%x", __func__, mdpFormat);
211             return -1;
212     }
213     // not reached
214     return -1;
215 }
216 
getMdpOrient(eTransform rotation)217 int getMdpOrient(eTransform rotation) {
218     int retTrans = 0;
219     bool trans90 = false;
220     int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
221     bool aFamily = (mdpVersion < qdutils::MDSS_V5);
222 
223     ALOGD_IF(DEBUG_OVERLAY, "%s: In rotation = %d", __FUNCTION__, rotation);
224     if(rotation & OVERLAY_TRANSFORM_ROT_90) {
225         retTrans |= MDP_ROT_90;
226         trans90 = true;
227     }
228 
229     if(rotation & OVERLAY_TRANSFORM_FLIP_H) {
230         if(trans90 && aFamily) {
231             //Swap for a-family, since its driver does 90 first
232             retTrans |= MDP_FLIP_UD;
233         } else {
234             retTrans |= MDP_FLIP_LR;
235         }
236     }
237 
238     if(rotation & OVERLAY_TRANSFORM_FLIP_V) {
239         if(trans90 && aFamily) {
240             //Swap for a-family, since its driver does 90 first
241             retTrans |= MDP_FLIP_LR;
242         } else {
243             retTrans |= MDP_FLIP_UD;
244         }
245     }
246 
247     ALOGD_IF(DEBUG_OVERLAY, "%s: Out rotation = %d", __FUNCTION__, retTrans);
248     return retTrans;
249 }
250 
getDecimationFactor(const int & src_w,const int & src_h,const int & dst_w,const int & dst_h,uint8_t & horzDeci,uint8_t & vertDeci)251 void getDecimationFactor(const int& src_w, const int& src_h,
252         const int& dst_w, const int& dst_h, uint8_t& horzDeci,
253         uint8_t& vertDeci) {
254     horzDeci = 0;
255     vertDeci = 0;
256     float horDscale = ceilf((float)src_w / (float)dst_w);
257     float verDscale = ceilf((float)src_h / (float)dst_h);
258     qdutils::MDPVersion& mdpHw = qdutils::MDPVersion::getInstance();
259 
260     //Next power of 2, if not already
261     horDscale = powf(2.0f, ceilf(log2f(horDscale)));
262     verDscale = powf(2.0f, ceilf(log2f(verDscale)));
263 
264     //Since MDP can do downscale and has better quality, split the task
265     //between decimator and MDP downscale
266     horDscale /= (float)mdpHw.getMaxMDPDownscale();
267     verDscale /= (float)mdpHw.getMaxMDPDownscale();
268 
269     if((int)horDscale)
270         horzDeci = (uint8_t)log2f(horDscale);
271 
272     if((int)verDscale)
273         vertDeci = (uint8_t)log2f(verDscale);
274 
275     if(src_w > (int) mdpHw.getMaxMixerWidth()) {
276         //If the client sends us something > what a layer mixer supports
277         //then it means it doesn't want to use split-pipe but wants us to
278         //decimate. A minimum decimation of 2 will ensure that the width is
279         //always within layer mixer limits.
280         if(horzDeci < 2)
281             horzDeci = 2;
282     }
283 }
284 
compute(const uint32_t & x,const uint32_t & y,const uint32_t & z)285 static inline int compute(const uint32_t& x, const uint32_t& y,
286         const uint32_t& z) {
287     return x - ( y + z );
288 }
289 
preRotateSource(const eTransform & tr,Whf & whf,Dim & srcCrop)290 void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop) {
291     if(tr & OVERLAY_TRANSFORM_FLIP_H) {
292         srcCrop.x = compute(whf.w, srcCrop.x, srcCrop.w);
293     }
294     if(tr & OVERLAY_TRANSFORM_FLIP_V) {
295         srcCrop.y = compute(whf.h, srcCrop.y, srcCrop.h);
296     }
297     if(tr & OVERLAY_TRANSFORM_ROT_90) {
298         int tmp = srcCrop.x;
299         srcCrop.x = compute(whf.h,
300                 srcCrop.y,
301                 srcCrop.h);
302         srcCrop.y = tmp;
303         swap(whf.w, whf.h);
304         swap(srcCrop.w, srcCrop.h);
305     }
306 }
307 
getDump(char * buf,size_t len,const char * prefix,const mdp_overlay & ov)308 void getDump(char *buf, size_t len, const char *prefix,
309         const mdp_overlay& ov) {
310     char str[256] = {'\0'};
311     snprintf(str, 256,
312             "%s id=%d z=%d alpha=%d mask=%d flags=0x%x H.Deci=%d,"
313             "V.Deci=%d\n",
314             prefix, ov.id, ov.z_order, ov.alpha,
315             ov.transp_mask, ov.flags, ov.horz_deci, ov.vert_deci);
316     strlcat(buf, str, len);
317     getDump(buf, len, "\tsrc", ov.src);
318     getDump(buf, len, "\tsrc_rect", ov.src_rect);
319     getDump(buf, len, "\tdst_rect", ov.dst_rect);
320 }
321 
getDump(char * buf,size_t len,const char * prefix,const msmfb_img & ov)322 void getDump(char *buf, size_t len, const char *prefix,
323         const msmfb_img& ov) {
324     char str_src[256] = {'\0'};
325     snprintf(str_src, 256,
326             "%s w=%d h=%d format=%d %s\n",
327             prefix, ov.width, ov.height, ov.format,
328             overlay::utils::getFormatString(ov.format));
329     strlcat(buf, str_src, len);
330 }
331 
getDump(char * buf,size_t len,const char * prefix,const mdp_rect & ov)332 void getDump(char *buf, size_t len, const char *prefix,
333         const mdp_rect& ov) {
334     char str_rect[256] = {'\0'};
335     snprintf(str_rect, 256,
336             "%s x=%d y=%d w=%d h=%d\n",
337             prefix, ov.x, ov.y, ov.w, ov.h);
338     strlcat(buf, str_rect, len);
339 }
340 
getDump(char * buf,size_t len,const char * prefix,const msmfb_overlay_data & ov)341 void getDump(char *buf, size_t len, const char *prefix,
342         const msmfb_overlay_data& ov) {
343     char str[256] = {'\0'};
344     snprintf(str, 256,
345             "%s id=%d\n",
346             prefix, ov.id);
347     strlcat(buf, str, len);
348     getDump(buf, len, "\tdata", ov.data);
349 }
350 
getDump(char * buf,size_t len,const char * prefix,const msmfb_data & ov)351 void getDump(char *buf, size_t len, const char *prefix,
352         const msmfb_data& ov) {
353     char str_data[256] = {'\0'};
354     snprintf(str_data, 256,
355             "%s offset=%d memid=%d id=%d flags=0x%x\n",
356             prefix, ov.offset, ov.memory_id, ov.id, ov.flags);
357     strlcat(buf, str_data, len);
358 }
359 
getDump(char * buf,size_t len,const char * prefix,const msm_rotator_img_info & rot)360 void getDump(char *buf, size_t len, const char *prefix,
361         const msm_rotator_img_info& rot) {
362     char str[256] = {'\0'};
363     snprintf(str, 256, "%s sessid=%u rot=%d, enable=%d downscale=%d\n",
364             prefix, rot.session_id, rot.rotations, rot.enable,
365             rot.downscale_ratio);
366     strlcat(buf, str, len);
367     getDump(buf, len, "\tsrc", rot.src);
368     getDump(buf, len, "\tdst", rot.dst);
369     getDump(buf, len, "\tsrc_rect", rot.src_rect);
370 }
371 
getDump(char * buf,size_t len,const char * prefix,const msm_rotator_data_info & rot)372 void getDump(char *buf, size_t len, const char *prefix,
373         const msm_rotator_data_info& rot) {
374     char str[256] = {'\0'};
375     snprintf(str, 256,
376             "%s sessid=%u\n",
377             prefix, rot.session_id);
378     strlcat(buf, str, len);
379     getDump(buf, len, "\tsrc", rot.src);
380     getDump(buf, len, "\tdst", rot.dst);
381 }
382 
383 //Helper to even out x,w and y,h pairs
384 //x,y are always evened to ceil and w,h are evened to floor
normalizeCrop(uint32_t & xy,uint32_t & wh)385 void normalizeCrop(uint32_t& xy, uint32_t& wh) {
386     if(xy & 1) {
387         even_ceil(xy);
388         if(wh & 1)
389             even_floor(wh);
390         else
391             wh -= 2;
392     } else {
393         even_floor(wh);
394     }
395 }
396 
397 } // utils
398 
399 } // overlay
400