• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2011-2012, 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 
40 // just a helper static thingy
41 namespace {
42 struct IOFile {
IOFile__anon0911ecfb0111::IOFile43     IOFile(const char* s, const char* mode) : fp(0) {
44         fp = ::fopen(s, mode);
45         if(!fp) {
46             ALOGE("Failed open %s", s);
47         }
48     }
49     template <class T>
read__anon0911ecfb0111::IOFile50             size_t read(T& r, size_t elem) {
51                 if(fp) {
52                     return ::fread(&r, sizeof(T), elem, fp);
53                 }
54                 return 0;
55             }
write__anon0911ecfb0111::IOFile56     size_t write(const char* s, uint32_t val) {
57         if(fp) {
58             return ::fprintf(fp, s, val);
59         }
60         return 0;
61     }
valid__anon0911ecfb0111::IOFile62     bool valid() const { return fp != 0; }
~IOFile__anon0911ecfb0111::IOFile63     ~IOFile() {
64         if(fp) ::fclose(fp);
65         fp=0;
66     }
67     FILE* fp;
68 };
69 }
70 
71 namespace overlay {
72 
73 //----------From class Res ------------------------------
74 const char* const Res::fbPath = "/dev/graphics/fb%u";
75 const char* const Res::rotPath = "/dev/msm_rotator";
76 const char* const Res::format3DFile =
77         "/sys/class/graphics/fb1/format_3d";
78 const char* const Res::edid3dInfoFile =
79         "/sys/class/graphics/fb1/3d_present";
80 const char* const Res::barrierFile =
81         "/sys/devices/platform/mipi_novatek.0/enable_3d_barrier";
82 //--------------------------------------------------------
83 
84 
85 
86 namespace utils {
87 
88 //--------------------------------------------------------
89 //Refer to graphics.h, gralloc_priv.h, msm_mdp.h
getMdpFormat(int format)90 int getMdpFormat(int format) {
91     switch (format) {
92         //From graphics.h
93         case HAL_PIXEL_FORMAT_RGBA_8888 :
94             return MDP_RGBA_8888;
95         case HAL_PIXEL_FORMAT_RGBX_8888:
96             return MDP_RGBX_8888;
97         case HAL_PIXEL_FORMAT_RGB_888:
98             return MDP_RGB_888;
99         case HAL_PIXEL_FORMAT_RGB_565:
100             return MDP_RGB_565;
101         case HAL_PIXEL_FORMAT_BGRA_8888:
102             return MDP_BGRA_8888;
103         case HAL_PIXEL_FORMAT_YV12:
104             return MDP_Y_CR_CB_GH2V2;
105         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
106             return MDP_Y_CBCR_H2V1;
107         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
108             return MDP_Y_CRCB_H2V2;
109 
110         //From gralloc_priv.h
111         case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
112             return MDP_Y_CBCR_H2V2_TILE;
113         case HAL_PIXEL_FORMAT_YCbCr_420_SP:
114             return MDP_Y_CBCR_H2V2;
115         case HAL_PIXEL_FORMAT_YCrCb_422_SP:
116             return MDP_Y_CRCB_H2V1;
117         case HAL_PIXEL_FORMAT_YCbCr_444_SP:
118             return MDP_Y_CBCR_H1V1;
119         case HAL_PIXEL_FORMAT_YCrCb_444_SP:
120             return MDP_Y_CRCB_H1V1;
121         case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
122         case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
123             //NV12 encodeable format maps to the venus format on
124             //B-Family targets
125             return MDP_Y_CBCR_H2V2_VENUS;
126         default:
127             //Unsupported by MDP
128             //---graphics.h--------
129             //HAL_PIXEL_FORMAT_RGBA_5551
130             //HAL_PIXEL_FORMAT_RGBA_4444
131             //HAL_PIXEL_FORMAT_YCbCr_422_I
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 //Takes mdp format as input and translates to equivalent HAL format
144 //Refer to graphics.h, gralloc_priv.h, msm_mdp.h for formats.
getHALFormat(int mdpFormat)145 int getHALFormat(int mdpFormat) {
146     switch (mdpFormat) {
147         //From graphics.h
148         case MDP_RGBA_8888:
149             return HAL_PIXEL_FORMAT_RGBA_8888;
150         case MDP_RGBX_8888:
151             return HAL_PIXEL_FORMAT_RGBX_8888;
152         case MDP_RGB_888:
153             return HAL_PIXEL_FORMAT_RGB_888;
154         case MDP_RGB_565:
155             return HAL_PIXEL_FORMAT_RGB_565;
156         case MDP_BGRA_8888:
157             return HAL_PIXEL_FORMAT_BGRA_8888;
158         case MDP_Y_CR_CB_GH2V2:
159             return HAL_PIXEL_FORMAT_YV12;
160         case MDP_Y_CBCR_H2V1:
161             return HAL_PIXEL_FORMAT_YCbCr_422_SP;
162         case MDP_Y_CRCB_H2V2:
163             return HAL_PIXEL_FORMAT_YCrCb_420_SP;
164 
165         //From gralloc_priv.h
166         case MDP_Y_CBCR_H2V2_TILE:
167             return HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED;
168         case MDP_Y_CBCR_H2V2:
169             return HAL_PIXEL_FORMAT_YCbCr_420_SP;
170         case MDP_Y_CRCB_H2V1:
171             return HAL_PIXEL_FORMAT_YCrCb_422_SP;
172         case MDP_Y_CBCR_H1V1:
173             return HAL_PIXEL_FORMAT_YCbCr_444_SP;
174         case MDP_Y_CRCB_H1V1:
175             return HAL_PIXEL_FORMAT_YCrCb_444_SP;
176         case MDP_Y_CBCR_H2V2_VENUS:
177             return HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
178         default:
179             ALOGE("%s: Unsupported MDP format = 0x%x", __func__, mdpFormat);
180             return -1;
181     }
182     // not reached
183     return -1;
184 }
185 
getMdpOrient(eTransform rotation)186 int getMdpOrient(eTransform rotation) {
187     int retTrans = 0;
188     bool trans90 = false;
189     int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
190     bool aFamily = (mdpVersion < qdutils::MDSS_V5);
191 
192     ALOGD_IF(DEBUG_OVERLAY, "%s: In rotation = %d", __FUNCTION__, rotation);
193     if(rotation & OVERLAY_TRANSFORM_ROT_90) {
194         retTrans |= MDP_ROT_90;
195         trans90 = true;
196     }
197 
198     if(rotation & OVERLAY_TRANSFORM_FLIP_H) {
199         if(trans90 && aFamily) {
200             //Swap for a-family, since its driver does 90 first
201             retTrans |= MDP_FLIP_UD;
202         } else {
203             retTrans |= MDP_FLIP_LR;
204         }
205     }
206 
207     if(rotation & OVERLAY_TRANSFORM_FLIP_V) {
208         if(trans90 && aFamily) {
209             //Swap for a-family, since its driver does 90 first
210             retTrans |= MDP_FLIP_LR;
211         } else {
212             retTrans |= MDP_FLIP_UD;
213         }
214     }
215 
216     ALOGD_IF(DEBUG_OVERLAY, "%s: Out rotation = %d", __FUNCTION__, retTrans);
217     return retTrans;
218 }
219 
getDownscaleFactor(const int & src_w,const int & src_h,const int & dst_w,const int & dst_h)220 int getDownscaleFactor(const int& src_w, const int& src_h,
221         const int& dst_w, const int& dst_h) {
222     int dscale_factor = utils::ROT_DS_NONE;
223     // The tolerance is an empirical grey area that needs to be adjusted
224     // manually so that we always err on the side of caution
225     float fDscaleTolerance = 0.05;
226     // We need this check to engage the rotator whenever possible to assist MDP
227     // in performing video downscale.
228     // This saves bandwidth and avoids causing the driver to make too many panel
229     // -mode switches between BLT (writeback) and non-BLT (Direct) modes.
230     // Use-case: Video playback [with downscaling and rotation].
231     if (dst_w && dst_h)
232     {
233         float fDscale =  sqrtf((float)(src_w * src_h) / (float)(dst_w * dst_h)) +
234                          fDscaleTolerance;
235 
236         // On our MTP 1080p playback case downscale after sqrt is coming to 1.87
237         // we were rounding to 1. So entirely MDP has to do the downscaling.
238         // BW requirement and clock requirement is high across MDP4 targets.
239         // It is unable to downscale 1080p video to panel resolution on 8960.
240         // round(x) will round it to nearest integer and avoids above issue.
241         uint32_t dscale = round(fDscale);
242 
243         if(dscale < 2) {
244             // Down-scale to > 50% of orig.
245             dscale_factor = utils::ROT_DS_NONE;
246         } else if(dscale < 4) {
247             // Down-scale to between > 25% to <= 50% of orig.
248             dscale_factor = utils::ROT_DS_HALF;
249         } else if(dscale < 8) {
250             // Down-scale to between > 12.5% to <= 25% of orig.
251             dscale_factor = utils::ROT_DS_FOURTH;
252         } else {
253             // Down-scale to <= 12.5% of orig.
254             dscale_factor = utils::ROT_DS_EIGHTH;
255         }
256     }
257     return dscale_factor;
258 }
259 
getDecimationFactor(const int & src_w,const int & src_h,const int & dst_w,const int & dst_h,uint8_t & horzDeci,uint8_t & vertDeci)260 void getDecimationFactor(const int& src_w, const int& src_h,
261         const int& dst_w, const int& dst_h, uint8_t& horzDeci,
262         uint8_t& vertDeci) {
263     horzDeci = 0;
264     vertDeci = 0;
265     float horDscale = ceilf((float)src_w / (float)dst_w);
266     float verDscale = ceilf((float)src_h / (float)dst_h);
267 
268     //Next power of 2, if not already
269     horDscale = powf(2.0f, ceilf(log2f(horDscale)));
270     verDscale = powf(2.0f, ceilf(log2f(verDscale)));
271 
272     //Since MDP can do 1/4 dscale and has better quality, split the task
273     //between decimator and MDP downscale
274     horDscale /= 4.0f;
275     verDscale /= 4.0f;
276 
277     if((int)horDscale)
278         horzDeci = (uint8_t)log2f(horDscale);
279 
280     if((int)verDscale)
281         vertDeci = (uint8_t)log2f(verDscale);
282 
283     if(src_w > 2048) {
284         //If the client sends us something > what a layer mixer supports
285         //then it means it doesn't want to use split-pipe but wants us to
286         //decimate. A minimum decimation of 2 will ensure that the width is
287         //always within layer mixer limits.
288         if(horzDeci < 2)
289             horzDeci = 2;
290     }
291 }
292 
compute(const uint32_t & x,const uint32_t & y,const uint32_t & z)293 static inline int compute(const uint32_t& x, const uint32_t& y,
294         const uint32_t& z) {
295     return x - ( y + z );
296 }
297 
preRotateSource(const eTransform & tr,Whf & whf,Dim & srcCrop)298 void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop) {
299     if(tr & OVERLAY_TRANSFORM_FLIP_H) {
300         srcCrop.x = compute(whf.w, srcCrop.x, srcCrop.w);
301     }
302     if(tr & OVERLAY_TRANSFORM_FLIP_V) {
303         srcCrop.y = compute(whf.h, srcCrop.y, srcCrop.h);
304     }
305     if(tr & OVERLAY_TRANSFORM_ROT_90) {
306         int tmp = srcCrop.x;
307         srcCrop.x = compute(whf.h,
308                 srcCrop.y,
309                 srcCrop.h);
310         srcCrop.y = tmp;
311         swap(whf.w, whf.h);
312         swap(srcCrop.w, srcCrop.h);
313     }
314 }
315 
is3DTV()316 bool is3DTV() {
317     char is3DTV = '0';
318     IOFile fp(Res::edid3dInfoFile, "r");
319     (void)fp.read(is3DTV, 1);
320     ALOGI("3DTV EDID flag: %d", is3DTV);
321     return (is3DTV == '0') ? false : true;
322 }
323 
isPanel3D()324 bool isPanel3D() {
325     OvFD fd;
326     if(!overlay::open(fd, 0 /*fb*/, Res::fbPath)){
327         ALOGE("isPanel3D Can't open framebuffer 0");
328         return false;
329     }
330     fb_fix_screeninfo finfo;
331     if(!mdp_wrapper::getFScreenInfo(fd.getFD(), finfo)) {
332         ALOGE("isPanel3D read fb0 failed");
333     }
334     fd.close();
335     return (FB_TYPE_3D_PANEL == finfo.type) ? true : false;
336 }
337 
usePanel3D()338 bool usePanel3D() {
339     if(!isPanel3D())
340         return false;
341     char value[PROPERTY_VALUE_MAX];
342     property_get("persist.user.panel3D", value, "0");
343     int usePanel3D = atoi(value);
344     return usePanel3D ? true : false;
345 }
346 
send3DInfoPacket(uint32_t format3D)347 bool send3DInfoPacket (uint32_t format3D) {
348     IOFile fp(Res::format3DFile, "wb");
349     (void)fp.write("%d", format3D);
350     if(!fp.valid()) {
351         ALOGE("send3DInfoPacket: no sysfs entry for setting 3d mode");
352         return false;
353     }
354     return true;
355 }
356 
enableBarrier(uint32_t orientation)357 bool enableBarrier (uint32_t orientation) {
358     IOFile fp(Res::barrierFile, "wb");
359     (void)fp.write("%d", orientation);
360     if(!fp.valid()) {
361         ALOGE("enableBarrier no sysfs entry for "
362                 "enabling barriers on 3D panel");
363         return false;
364     }
365     return true;
366 }
367 
getS3DFormat(uint32_t fmt)368 uint32_t getS3DFormat(uint32_t fmt) {
369     // The S3D is part of the HAL_PIXEL_FORMAT_YV12 value. Add
370     // an explicit check for the format
371     if (fmt == HAL_PIXEL_FORMAT_YV12) {
372         return 0;
373     }
374     uint32_t fmt3D = format3D(fmt);
375     uint32_t fIn3D = format3DInput(fmt3D); // MSB 2 bytes - inp
376     uint32_t fOut3D = format3DOutput(fmt3D); // LSB 2 bytes - out
377     fmt3D = fIn3D | fOut3D;
378     if (!fIn3D) {
379         fmt3D |= fOut3D << SHIFT_TOT_3D; //Set the input format
380     }
381     if (!fOut3D) {
382         switch (fIn3D) {
383             case HAL_3D_IN_SIDE_BY_SIDE_L_R:
384             case HAL_3D_IN_SIDE_BY_SIDE_R_L:
385                 // For all side by side formats, set the output
386                 // format as Side-by-Side i.e 0x1
387                 fmt3D |= HAL_3D_IN_SIDE_BY_SIDE_L_R >> SHIFT_TOT_3D;
388                 break;
389             default:
390                 fmt3D |= fIn3D >> SHIFT_TOT_3D; //Set the output format
391         }
392     }
393     return fmt3D;
394 }
395 
getDump(char * buf,size_t len,const char * prefix,const mdp_overlay & ov)396 void getDump(char *buf, size_t len, const char *prefix,
397         const mdp_overlay& ov) {
398     char str[256] = {'\0'};
399     snprintf(str, 256,
400             "%s id=%d z=%d fg=%d alpha=%d mask=%d flags=0x%x H.Deci=%d,"
401             "V.Deci=%d\n",
402             prefix, ov.id, ov.z_order, ov.is_fg, ov.alpha,
403             ov.transp_mask, ov.flags, ov.horz_deci, ov.vert_deci);
404     strncat(buf, str, strlen(str));
405     getDump(buf, len, "\tsrc", ov.src);
406     getDump(buf, len, "\tsrc_rect", ov.src_rect);
407     getDump(buf, len, "\tdst_rect", ov.dst_rect);
408 }
409 
getDump(char * buf,size_t len,const char * prefix,const msmfb_img & ov)410 void getDump(char *buf, size_t len, const char *prefix,
411         const msmfb_img& ov) {
412     char str_src[256] = {'\0'};
413     snprintf(str_src, 256,
414             "%s w=%d h=%d format=%d %s\n",
415             prefix, ov.width, ov.height, ov.format,
416             overlay::utils::getFormatString(ov.format));
417     strncat(buf, str_src, strlen(str_src));
418 }
419 
getDump(char * buf,size_t len,const char * prefix,const mdp_rect & ov)420 void getDump(char *buf, size_t len, const char *prefix,
421         const mdp_rect& ov) {
422     char str_rect[256] = {'\0'};
423     snprintf(str_rect, 256,
424             "%s x=%d y=%d w=%d h=%d\n",
425             prefix, ov.x, ov.y, ov.w, ov.h);
426     strncat(buf, str_rect, strlen(str_rect));
427 }
428 
getDump(char * buf,size_t len,const char * prefix,const msmfb_overlay_data & ov)429 void getDump(char *buf, size_t len, const char *prefix,
430         const msmfb_overlay_data& ov) {
431     char str[256] = {'\0'};
432     snprintf(str, 256,
433             "%s id=%d\n",
434             prefix, ov.id);
435     strncat(buf, str, strlen(str));
436     getDump(buf, len, "\tdata", ov.data);
437 }
438 
getDump(char * buf,size_t len,const char * prefix,const msmfb_data & ov)439 void getDump(char *buf, size_t len, const char *prefix,
440         const msmfb_data& ov) {
441     char str_data[256] = {'\0'};
442     snprintf(str_data, 256,
443             "%s offset=%d memid=%d id=%d flags=0x%x\n",
444             prefix, ov.offset, ov.memory_id, ov.id, ov.flags);
445     strncat(buf, str_data, strlen(str_data));
446 }
447 
getDump(char * buf,size_t len,const char * prefix,const msm_rotator_img_info & rot)448 void getDump(char *buf, size_t len, const char *prefix,
449         const msm_rotator_img_info& rot) {
450     char str[256] = {'\0'};
451     snprintf(str, 256, "%s sessid=%u rot=%d, enable=%d downscale=%d\n",
452             prefix, rot.session_id, rot.rotations, rot.enable,
453             rot.downscale_ratio);
454     strncat(buf, str, strlen(str));
455     getDump(buf, len, "\tsrc", rot.src);
456     getDump(buf, len, "\tdst", rot.dst);
457     getDump(buf, len, "\tsrc_rect", rot.src_rect);
458 }
459 
getDump(char * buf,size_t len,const char * prefix,const msm_rotator_data_info & rot)460 void getDump(char *buf, size_t len, const char *prefix,
461         const msm_rotator_data_info& rot) {
462     char str[256] = {'\0'};
463     snprintf(str, 256,
464             "%s sessid=%u\n",
465             prefix, rot.session_id);
466     strncat(buf, str, strlen(str));
467     getDump(buf, len, "\tsrc", rot.src);
468     getDump(buf, len, "\tdst", rot.dst);
469 }
470 
471 //Helper to even out x,w and y,h pairs
472 //x,y are always evened to ceil and w,h are evened to floor
normalizeCrop(uint32_t & xy,uint32_t & wh)473 void normalizeCrop(uint32_t& xy, uint32_t& wh) {
474     if(xy & 1) {
475         even_ceil(xy);
476         if(wh & 1)
477             even_floor(wh);
478         else
479             wh -= 2;
480     } else {
481         even_floor(wh);
482     }
483 }
484 
485 } // utils
486 
487 } // overlay
488