blob: 1377182b6d00f0f242b4a8a7ab342e19859f832d [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
Naseer Ahmed758bfc52012-11-28 17:02:08 -05002* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Naseer Ahmed29a26812012-06-14 00:56:20 -07003*
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.
Naseer Ahmed758bfc52012-11-28 17:02:08 -050013* * Neither the name of The Linux Foundation nor the names of its
Naseer Ahmed29a26812012-06-14 00:56:20 -070014* 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>
Saurabh Shahdb7ae312013-03-19 15:45:24 -070031#include <math.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070032#include <utils/Log.h>
33#include <linux/msm_mdp.h>
34#include <cutils/properties.h>
35#include "gralloc_priv.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070036#include "overlayUtils.h"
37#include "mdpWrapper.h"
Saurabh Shah94822ee2012-08-17 16:48:43 -070038#include "mdp_version.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070039
40// just a helper static thingy
41namespace {
42struct IOFile {
43 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>
50 size_t read(T& r, size_t elem) {
51 if(fp) {
52 return ::fread(&r, sizeof(T), elem, fp);
53 }
54 return 0;
55 }
56 size_t write(const char* s, uint32_t val) {
57 if(fp) {
58 return ::fprintf(fp, s, val);
59 }
60 return 0;
61 }
62 bool valid() const { return fp != 0; }
63 ~IOFile() {
64 if(fp) ::fclose(fp);
65 fp=0;
66 }
67 FILE* fp;
68};
69}
70
71namespace overlay {
72
73//----------From class Res ------------------------------
Naseer Ahmedf48aef62012-07-20 09:05:53 -070074const char* const Res::fbPath = "/dev/graphics/fb%u";
Naseer Ahmed29a26812012-06-14 00:56:20 -070075const char* const Res::rotPath = "/dev/msm_rotator";
76const char* const Res::format3DFile =
77 "/sys/class/graphics/fb1/format_3d";
78const char* const Res::edid3dInfoFile =
79 "/sys/class/graphics/fb1/3d_present";
80const char* const Res::barrierFile =
81 "/sys/devices/platform/mipi_novatek.0/enable_3d_barrier";
82//--------------------------------------------------------
83
84
85
86namespace utils {
Ajay Dudanicb3da0a2012-09-07 13:37:49 -070087
Naseer Ahmed29a26812012-06-14 00:56:20 -070088//--------------------------------------------------------
Naseer Ahmedaf49e212012-07-31 19:13:41 -070089//Refer to graphics.h, gralloc_priv.h, msm_mdp.h
Naseer Ahmed29a26812012-06-14 00:56:20 -070090int getMdpFormat(int format) {
91 switch (format) {
Naseer Ahmedaf49e212012-07-31 19:13:41 -070092 //From graphics.h
Naseer Ahmed29a26812012-06-14 00:56:20 -070093 case HAL_PIXEL_FORMAT_RGBA_8888 :
94 return MDP_RGBA_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -070095 case HAL_PIXEL_FORMAT_RGBX_8888:
96 return MDP_RGBX_8888;
Naseer Ahmedaf49e212012-07-31 19:13:41 -070097 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;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700103 case HAL_PIXEL_FORMAT_BGRX_8888:
104 return MDP_BGRX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700105 case HAL_PIXEL_FORMAT_YV12:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700106 return MDP_Y_CR_CB_GH2V2;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700107 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
108 return MDP_Y_CBCR_H2V1;
109 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
110 return MDP_Y_CRCB_H2V2;
111
112 //From gralloc_priv.h
113 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
114 return MDP_Y_CBCR_H2V2_TILE;
115 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
116 return MDP_Y_CBCR_H2V2;
117 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
118 return MDP_Y_CRCB_H2V1;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700119 case HAL_PIXEL_FORMAT_YCbCr_422_I:
120 return MDP_YCBYCR_H2V1;
121 case HAL_PIXEL_FORMAT_YCrCb_422_I:
122 return MDP_YCRYCB_H2V1;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700123 case HAL_PIXEL_FORMAT_YCbCr_444_SP:
124 return MDP_Y_CBCR_H1V1;
125 case HAL_PIXEL_FORMAT_YCrCb_444_SP:
126 return MDP_Y_CRCB_H1V1;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700127 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmed8f8cb8a2013-08-22 15:34:30 -0400128 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
129 //NV12 encodeable format maps to the venus format on
130 //B-Family targets
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700131 return MDP_Y_CBCR_H2V2_VENUS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700132 default:
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700133 //Unsupported by MDP
134 //---graphics.h--------
135 //HAL_PIXEL_FORMAT_RGBA_5551
136 //HAL_PIXEL_FORMAT_RGBA_4444
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700137 //---gralloc_priv.h-----
138 //HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO = 0x7FA30C01
139 //HAL_PIXEL_FORMAT_R_8 = 0x10D
140 //HAL_PIXEL_FORMAT_RG_88 = 0x10E
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800141 ALOGE("%s: Unsupported HAL format = 0x%x", __func__, format);
142 return -1;
143 }
144 // not reached
145 return -1;
146}
147
148//Takes mdp format as input and translates to equivalent HAL format
149//Refer to graphics.h, gralloc_priv.h, msm_mdp.h for formats.
150int getHALFormat(int mdpFormat) {
151 switch (mdpFormat) {
152 //From graphics.h
153 case MDP_RGBA_8888:
154 return HAL_PIXEL_FORMAT_RGBA_8888;
155 case MDP_RGBX_8888:
156 return HAL_PIXEL_FORMAT_RGBX_8888;
157 case MDP_RGB_888:
158 return HAL_PIXEL_FORMAT_RGB_888;
159 case MDP_RGB_565:
160 return HAL_PIXEL_FORMAT_RGB_565;
161 case MDP_BGRA_8888:
162 return HAL_PIXEL_FORMAT_BGRA_8888;
163 case MDP_Y_CR_CB_GH2V2:
164 return HAL_PIXEL_FORMAT_YV12;
165 case MDP_Y_CBCR_H2V1:
166 return HAL_PIXEL_FORMAT_YCbCr_422_SP;
167 case MDP_Y_CRCB_H2V2:
168 return HAL_PIXEL_FORMAT_YCrCb_420_SP;
169
170 //From gralloc_priv.h
171 case MDP_Y_CBCR_H2V2_TILE:
172 return HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED;
173 case MDP_Y_CBCR_H2V2:
174 return HAL_PIXEL_FORMAT_YCbCr_420_SP;
175 case MDP_Y_CRCB_H2V1:
176 return HAL_PIXEL_FORMAT_YCrCb_422_SP;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700177 case MDP_YCBYCR_H2V1:
178 return HAL_PIXEL_FORMAT_YCbCr_422_I;
179 case MDP_YCRYCB_H2V1:
180 return HAL_PIXEL_FORMAT_YCrCb_422_I;
181 case MDP_Y_CBCR_H1V1:
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800182 return HAL_PIXEL_FORMAT_YCbCr_444_SP;
183 case MDP_Y_CRCB_H1V1:
184 return HAL_PIXEL_FORMAT_YCrCb_444_SP;
185 case MDP_Y_CBCR_H2V2_VENUS:
186 return HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
187 default:
188 ALOGE("%s: Unsupported MDP format = 0x%x", __func__, mdpFormat);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700189 return -1;
190 }
191 // not reached
192 return -1;
193}
194
Saurabh Shahd1a22782013-04-10 09:09:45 -0700195int getMdpOrient(eTransform rotation) {
196 int retTrans = 0;
197 bool trans90 = false;
198 int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
199 bool aFamily = (mdpVersion < qdutils::MDSS_V5);
200
201 ALOGD_IF(DEBUG_OVERLAY, "%s: In rotation = %d", __FUNCTION__, rotation);
202 if(rotation & OVERLAY_TRANSFORM_ROT_90) {
203 retTrans |= MDP_ROT_90;
204 trans90 = true;
205 }
206
207 if(rotation & OVERLAY_TRANSFORM_FLIP_H) {
208 if(trans90 && aFamily) {
209 //Swap for a-family, since its driver does 90 first
210 retTrans |= MDP_FLIP_UD;
211 } else {
212 retTrans |= MDP_FLIP_LR;
213 }
214 }
215
216 if(rotation & OVERLAY_TRANSFORM_FLIP_V) {
217 if(trans90 && aFamily) {
218 //Swap for a-family, since its driver does 90 first
219 retTrans |= MDP_FLIP_LR;
220 } else {
221 retTrans |= MDP_FLIP_UD;
222 }
223 }
224
225 ALOGD_IF(DEBUG_OVERLAY, "%s: Out rotation = %d", __FUNCTION__, retTrans);
226 return retTrans;
227}
228
Saurabh Shahacf10202013-02-26 10:15:15 -0800229int getDownscaleFactor(const int& src_w, const int& src_h,
230 const int& dst_w, const int& dst_h) {
231 int dscale_factor = utils::ROT_DS_NONE;
232 // We need this check to engage the rotator whenever possible to assist MDP
233 // in performing video downscale.
234 // This saves bandwidth and avoids causing the driver to make too many panel
235 // -mode switches between BLT (writeback) and non-BLT (Direct) modes.
236 // Use-case: Video playback [with downscaling and rotation].
237 if (dst_w && dst_h)
238 {
Saurabh Shahdb7ae312013-03-19 15:45:24 -0700239 float fDscale = (float)(src_w * src_h) / (float)(dst_w * dst_h);
240 uint32_t dscale = (int)sqrtf(fDscale);
241
Saurabh Shahacf10202013-02-26 10:15:15 -0800242 if(dscale < 2) {
243 // Down-scale to > 50% of orig.
244 dscale_factor = utils::ROT_DS_NONE;
245 } else if(dscale < 4) {
246 // Down-scale to between > 25% to <= 50% of orig.
247 dscale_factor = utils::ROT_DS_HALF;
248 } else if(dscale < 8) {
249 // Down-scale to between > 12.5% to <= 25% of orig.
250 dscale_factor = utils::ROT_DS_FOURTH;
251 } else {
252 // Down-scale to <= 12.5% of orig.
253 dscale_factor = utils::ROT_DS_EIGHTH;
254 }
255 }
256 return dscale_factor;
257}
258
Saurabh Shah1a03d482013-05-29 13:44:20 -0700259//Since this is unavailable on Android, defining it in terms of base 10
260static inline float log2f(const float& x) {
261 return log(x) / log(2);
262}
263
264void getDecimationFactor(const int& src_w, const int& src_h,
265 const int& dst_w, const int& dst_h, float& horDscale,
266 float& verDscale) {
267 horDscale = ceilf((float)src_w / (float)dst_w);
268 verDscale = ceilf((float)src_h / (float)dst_h);
269
270 //Next power of 2, if not already
271 horDscale = powf(2.0f, ceilf(log2f(horDscale)));
272 verDscale = powf(2.0f, ceilf(log2f(verDscale)));
273
274 //Since MDP can do 1/4 dscale and has better quality, split the task
275 //between decimator and MDP downscale
276 horDscale /= 4.0f;
277 verDscale /= 4.0f;
278}
279
Saurabh Shahacf10202013-02-26 10:15:15 -0800280static inline int compute(const uint32_t& x, const uint32_t& y,
281 const uint32_t& z) {
282 return x - ( y + z );
283}
284
Saurabh Shah76fd6552013-03-14 14:45:38 -0700285void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800286 if(tr & OVERLAY_TRANSFORM_FLIP_H) {
287 srcCrop.x = compute(whf.w, srcCrop.x, srcCrop.w);
288 }
289 if(tr & OVERLAY_TRANSFORM_FLIP_V) {
290 srcCrop.y = compute(whf.h, srcCrop.y, srcCrop.h);
291 }
292 if(tr & OVERLAY_TRANSFORM_ROT_90) {
293 int tmp = srcCrop.x;
294 srcCrop.x = compute(whf.h,
295 srcCrop.y,
296 srcCrop.h);
297 srcCrop.y = tmp;
298 swap(whf.w, whf.h);
299 swap(srcCrop.w, srcCrop.h);
300 }
Saurabh Shahacf10202013-02-26 10:15:15 -0800301}
302
Naseer Ahmed29a26812012-06-14 00:56:20 -0700303bool is3DTV() {
304 char is3DTV = '0';
305 IOFile fp(Res::edid3dInfoFile, "r");
306 (void)fp.read(is3DTV, 1);
307 ALOGI("3DTV EDID flag: %d", is3DTV);
308 return (is3DTV == '0') ? false : true;
309}
310
311bool isPanel3D() {
312 OvFD fd;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700313 if(!overlay::open(fd, 0 /*fb*/, Res::fbPath)){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700314 ALOGE("isPanel3D Can't open framebuffer 0");
315 return false;
316 }
317 fb_fix_screeninfo finfo;
318 if(!mdp_wrapper::getFScreenInfo(fd.getFD(), finfo)) {
319 ALOGE("isPanel3D read fb0 failed");
320 }
321 fd.close();
322 return (FB_TYPE_3D_PANEL == finfo.type) ? true : false;
323}
324
325bool usePanel3D() {
326 if(!isPanel3D())
327 return false;
328 char value[PROPERTY_VALUE_MAX];
329 property_get("persist.user.panel3D", value, "0");
330 int usePanel3D = atoi(value);
331 return usePanel3D ? true : false;
332}
333
334bool send3DInfoPacket (uint32_t format3D) {
335 IOFile fp(Res::format3DFile, "wb");
336 (void)fp.write("%d", format3D);
337 if(!fp.valid()) {
338 ALOGE("send3DInfoPacket: no sysfs entry for setting 3d mode");
339 return false;
340 }
341 return true;
342}
343
344bool enableBarrier (uint32_t orientation) {
345 IOFile fp(Res::barrierFile, "wb");
346 (void)fp.write("%d", orientation);
347 if(!fp.valid()) {
348 ALOGE("enableBarrier no sysfs entry for "
349 "enabling barriers on 3D panel");
350 return false;
351 }
352 return true;
353}
354
355uint32_t getS3DFormat(uint32_t fmt) {
356 // The S3D is part of the HAL_PIXEL_FORMAT_YV12 value. Add
357 // an explicit check for the format
358 if (fmt == HAL_PIXEL_FORMAT_YV12) {
359 return 0;
360 }
361 uint32_t fmt3D = format3D(fmt);
362 uint32_t fIn3D = format3DInput(fmt3D); // MSB 2 bytes - inp
363 uint32_t fOut3D = format3DOutput(fmt3D); // LSB 2 bytes - out
364 fmt3D = fIn3D | fOut3D;
365 if (!fIn3D) {
366 fmt3D |= fOut3D << SHIFT_TOT_3D; //Set the input format
367 }
368 if (!fOut3D) {
369 switch (fIn3D) {
370 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
371 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
372 // For all side by side formats, set the output
373 // format as Side-by-Side i.e 0x1
374 fmt3D |= HAL_3D_IN_SIDE_BY_SIDE_L_R >> SHIFT_TOT_3D;
375 break;
376 default:
377 fmt3D |= fIn3D >> SHIFT_TOT_3D; //Set the output format
378 }
379 }
380 return fmt3D;
381}
382
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800383void getDump(char *buf, size_t len, const char *prefix,
384 const mdp_overlay& ov) {
385 char str[256] = {'\0'};
386 snprintf(str, 256,
Saurabh Shahbd2d0832013-04-04 14:33:08 -0700387 "%s id=%d z=%d fg=%d alpha=%d mask=%d flags=0x%x H.Deci=%d,"
388 "V.Deci=%d\n",
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800389 prefix, ov.id, ov.z_order, ov.is_fg, ov.alpha,
Saurabh Shahbd2d0832013-04-04 14:33:08 -0700390 ov.transp_mask, ov.flags, ov.horz_deci, ov.vert_deci);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800391 strncat(buf, str, strlen(str));
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700392 getDump(buf, len, "\tsrc", ov.src);
393 getDump(buf, len, "\tsrc_rect", ov.src_rect);
394 getDump(buf, len, "\tdst_rect", ov.dst_rect);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800395}
396
397void getDump(char *buf, size_t len, const char *prefix,
398 const msmfb_img& ov) {
399 char str_src[256] = {'\0'};
400 snprintf(str_src, 256,
401 "%s w=%d h=%d format=%d %s\n",
402 prefix, ov.width, ov.height, ov.format,
403 overlay::utils::getFormatString(ov.format));
404 strncat(buf, str_src, strlen(str_src));
405}
406
407void getDump(char *buf, size_t len, const char *prefix,
408 const mdp_rect& ov) {
409 char str_rect[256] = {'\0'};
410 snprintf(str_rect, 256,
411 "%s x=%d y=%d w=%d h=%d\n",
412 prefix, ov.x, ov.y, ov.w, ov.h);
413 strncat(buf, str_rect, strlen(str_rect));
414}
415
416void getDump(char *buf, size_t len, const char *prefix,
417 const msmfb_overlay_data& ov) {
418 char str[256] = {'\0'};
419 snprintf(str, 256,
420 "%s id=%d\n",
421 prefix, ov.id);
422 strncat(buf, str, strlen(str));
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700423 getDump(buf, len, "\tdata", ov.data);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800424}
425
426void getDump(char *buf, size_t len, const char *prefix,
427 const msmfb_data& ov) {
428 char str_data[256] = {'\0'};
429 snprintf(str_data, 256,
Saurabh Shah7ef9ec92013-10-29 10:32:23 -0700430 "%s offset=%d memid=%d id=%d flags=0x%x\n",
431 prefix, ov.offset, ov.memory_id, ov.id, ov.flags);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800432 strncat(buf, str_data, strlen(str_data));
433}
434
435void getDump(char *buf, size_t len, const char *prefix,
436 const msm_rotator_img_info& rot) {
437 char str[256] = {'\0'};
438 snprintf(str, 256, "%s sessid=%u rot=%d, enable=%d downscale=%d\n",
439 prefix, rot.session_id, rot.rotations, rot.enable,
440 rot.downscale_ratio);
441 strncat(buf, str, strlen(str));
442 getDump(buf, len, "\tsrc", rot.src);
443 getDump(buf, len, "\tdst", rot.dst);
444 getDump(buf, len, "\tsrc_rect", rot.src_rect);
445}
446
447void getDump(char *buf, size_t len, const char *prefix,
448 const msm_rotator_data_info& rot) {
449 char str[256] = {'\0'};
450 snprintf(str, 256,
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700451 "%s sessid=%u\n",
452 prefix, rot.session_id);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800453 strncat(buf, str, strlen(str));
454 getDump(buf, len, "\tsrc", rot.src);
455 getDump(buf, len, "\tdst", rot.dst);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800456}
457
Sushil Chauhan1cac8152013-05-08 15:53:51 -0700458//Helper to even out x,w and y,h pairs
459//x,y are always evened to ceil and w,h are evened to floor
460void normalizeCrop(uint32_t& xy, uint32_t& wh) {
461 if(xy & 1) {
462 even_ceil(xy);
463 if(wh & 1)
464 even_floor(wh);
465 else
466 wh -= 2;
467 } else {
468 even_floor(wh);
469 }
470}
471
Naseer Ahmed29a26812012-06-14 00:56:20 -0700472} // utils
473
474} // overlay