blob: 907597e8877f7f6e3e52f07397421291fdd150e2 [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>
31#include <utils/Log.h>
32#include <linux/msm_mdp.h>
33#include <cutils/properties.h>
34#include "gralloc_priv.h"
35#include "fb_priv.h"
36#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//------------------ defines -----------------------------
88#define FB_DEVICE_TEMPLATE "/dev/graphics/fb%u"
89#define NUM_FB_DEVICES 3
90
Naseer Ahmed29a26812012-06-14 00:56:20 -070091//--------------------------------------------------------
Naseer Ahmed29a26812012-06-14 00:56:20 -070092
Ajay Dudanicb3da0a2012-09-07 13:37:49 -070093/* clears any VG pipes allocated to the fb devices */
94int initOverlay() {
Naseer Ahmedff5c5302012-12-03 16:00:13 -050095 int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
96 if (mdpVersion < qdutils::MDSS_V5) {
97 msmfb_mixer_info_req req;
98 mdp_mixer_info *minfo = NULL;
99 char name[64];
100 int fd = -1;
101 for(int i = 0; i < NUM_FB_DEVICES; i++) {
102 snprintf(name, 64, FB_DEVICE_TEMPLATE, i);
103 ALOGD("initoverlay:: opening the device:: %s", name);
104 fd = ::open(name, O_RDWR, 0);
105 if(fd < 0) {
106 ALOGE("cannot open framebuffer(%d)", i);
107 return -1;
Ajay Dudanicb3da0a2012-09-07 13:37:49 -0700108 }
Naseer Ahmedff5c5302012-12-03 16:00:13 -0500109 //Get the mixer configuration */
110 req.mixer_num = i;
111 if (ioctl(fd, MSMFB_MIXER_INFO, &req) == -1) {
112 ALOGE("ERROR: MSMFB_MIXER_INFO ioctl failed");
113 close(fd);
114 return -1;
115 }
116 minfo = req.info;
117 for (int j = 0; j < req.cnt; j++) {
118 ALOGD("ndx=%d num=%d z_order=%d", minfo->pndx, minfo->pnum,
119 minfo->z_order);
120 // except the RGB base layer with z_order of -1, clear any
121 // other pipes connected to mixer.
122 if((minfo->z_order) != -1) {
123 int index = minfo->pndx;
124 ALOGD("Unset overlay with index: %d at mixer %d", index, i);
125 if(ioctl(fd, MSMFB_OVERLAY_UNSET, &index) == -1) {
126 ALOGE("ERROR: MSMFB_OVERLAY_UNSET failed");
127 close(fd);
128 return -1;
129 }
130 }
131 minfo++;
132 }
133 close(fd);
134 fd = -1;
Ajay Dudanicb3da0a2012-09-07 13:37:49 -0700135 }
Ajay Dudanicb3da0a2012-09-07 13:37:49 -0700136 }
137 return 0;
138}
139
Naseer Ahmed29a26812012-06-14 00:56:20 -0700140//--------------------------------------------------------
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700141//Refer to graphics.h, gralloc_priv.h, msm_mdp.h
Naseer Ahmed29a26812012-06-14 00:56:20 -0700142int getMdpFormat(int format) {
143 switch (format) {
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700144 //From graphics.h
Naseer Ahmed29a26812012-06-14 00:56:20 -0700145 case HAL_PIXEL_FORMAT_RGBA_8888 :
146 return MDP_RGBA_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700147 case HAL_PIXEL_FORMAT_RGBX_8888:
148 return MDP_RGBX_8888;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700149 case HAL_PIXEL_FORMAT_RGB_888:
150 return MDP_RGB_888;
151 case HAL_PIXEL_FORMAT_RGB_565:
152 return MDP_RGB_565;
153 case HAL_PIXEL_FORMAT_BGRA_8888:
154 return MDP_BGRA_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700155 case HAL_PIXEL_FORMAT_YV12:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700156 return MDP_Y_CR_CB_GH2V2;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700157 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
158 return MDP_Y_CBCR_H2V1;
159 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
160 return MDP_Y_CRCB_H2V2;
161
162 //From gralloc_priv.h
163 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
164 return MDP_Y_CBCR_H2V2_TILE;
165 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
166 return MDP_Y_CBCR_H2V2;
167 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
168 return MDP_Y_CRCB_H2V1;
169 case HAL_PIXEL_FORMAT_YCbCr_444_SP:
170 return MDP_Y_CBCR_H1V1;
171 case HAL_PIXEL_FORMAT_YCrCb_444_SP:
172 return MDP_Y_CRCB_H1V1;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700173 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
174 return MDP_Y_CBCR_H2V2_VENUS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700175 default:
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700176 //Unsupported by MDP
177 //---graphics.h--------
178 //HAL_PIXEL_FORMAT_RGBA_5551
179 //HAL_PIXEL_FORMAT_RGBA_4444
180 //HAL_PIXEL_FORMAT_YCbCr_422_I
181 //---gralloc_priv.h-----
182 //HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO = 0x7FA30C01
183 //HAL_PIXEL_FORMAT_R_8 = 0x10D
184 //HAL_PIXEL_FORMAT_RG_88 = 0x10E
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800185 ALOGE("%s: Unsupported HAL format = 0x%x", __func__, format);
186 return -1;
187 }
188 // not reached
189 return -1;
190}
191
192//Takes mdp format as input and translates to equivalent HAL format
193//Refer to graphics.h, gralloc_priv.h, msm_mdp.h for formats.
194int getHALFormat(int mdpFormat) {
195 switch (mdpFormat) {
196 //From graphics.h
197 case MDP_RGBA_8888:
198 return HAL_PIXEL_FORMAT_RGBA_8888;
199 case MDP_RGBX_8888:
200 return HAL_PIXEL_FORMAT_RGBX_8888;
201 case MDP_RGB_888:
202 return HAL_PIXEL_FORMAT_RGB_888;
203 case MDP_RGB_565:
204 return HAL_PIXEL_FORMAT_RGB_565;
205 case MDP_BGRA_8888:
206 return HAL_PIXEL_FORMAT_BGRA_8888;
207 case MDP_Y_CR_CB_GH2V2:
208 return HAL_PIXEL_FORMAT_YV12;
209 case MDP_Y_CBCR_H2V1:
210 return HAL_PIXEL_FORMAT_YCbCr_422_SP;
211 case MDP_Y_CRCB_H2V2:
212 return HAL_PIXEL_FORMAT_YCrCb_420_SP;
213
214 //From gralloc_priv.h
215 case MDP_Y_CBCR_H2V2_TILE:
216 return HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED;
217 case MDP_Y_CBCR_H2V2:
218 return HAL_PIXEL_FORMAT_YCbCr_420_SP;
219 case MDP_Y_CRCB_H2V1:
220 return HAL_PIXEL_FORMAT_YCrCb_422_SP;
221 case MDP_Y_CBCR_H1V1:
222 return HAL_PIXEL_FORMAT_YCbCr_444_SP;
223 case MDP_Y_CRCB_H1V1:
224 return HAL_PIXEL_FORMAT_YCrCb_444_SP;
225 case MDP_Y_CBCR_H2V2_VENUS:
226 return HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
227 default:
228 ALOGE("%s: Unsupported MDP format = 0x%x", __func__, mdpFormat);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700229 return -1;
230 }
231 // not reached
232 return -1;
233}
234
Saurabh Shahacf10202013-02-26 10:15:15 -0800235int getDownscaleFactor(const int& src_w, const int& src_h,
236 const int& dst_w, const int& dst_h) {
237 int dscale_factor = utils::ROT_DS_NONE;
238 // We need this check to engage the rotator whenever possible to assist MDP
239 // in performing video downscale.
240 // This saves bandwidth and avoids causing the driver to make too many panel
241 // -mode switches between BLT (writeback) and non-BLT (Direct) modes.
242 // Use-case: Video playback [with downscaling and rotation].
243 if (dst_w && dst_h)
244 {
245 uint32_t dscale = (src_w * src_h) / (dst_w * dst_h);
246 if(dscale < 2) {
247 // Down-scale to > 50% of orig.
248 dscale_factor = utils::ROT_DS_NONE;
249 } else if(dscale < 4) {
250 // Down-scale to between > 25% to <= 50% of orig.
251 dscale_factor = utils::ROT_DS_HALF;
252 } else if(dscale < 8) {
253 // Down-scale to between > 12.5% to <= 25% of orig.
254 dscale_factor = utils::ROT_DS_FOURTH;
255 } else {
256 // Down-scale to <= 12.5% of orig.
257 dscale_factor = utils::ROT_DS_EIGHTH;
258 }
259 }
260 return dscale_factor;
261}
262
263static inline int compute(const uint32_t& x, const uint32_t& y,
264 const uint32_t& z) {
265 return x - ( y + z );
266}
267
268void preRotateSource(eTransform& tr, Whf& whf, Dim& srcCrop) {
269 if(tr & OVERLAY_TRANSFORM_FLIP_H) {
270 srcCrop.x = compute(whf.w, srcCrop.x, srcCrop.w);
271 }
272 if(tr & OVERLAY_TRANSFORM_FLIP_V) {
273 srcCrop.y = compute(whf.h, srcCrop.y, srcCrop.h);
274 }
275 if(tr & OVERLAY_TRANSFORM_ROT_90) {
276 int tmp = srcCrop.x;
277 srcCrop.x = compute(whf.h,
278 srcCrop.y,
279 srcCrop.h);
280 srcCrop.y = tmp;
281 swap(whf.w, whf.h);
282 swap(srcCrop.w, srcCrop.h);
283 }
284 tr = OVERLAY_TRANSFORM_0;
285}
286
Naseer Ahmed29a26812012-06-14 00:56:20 -0700287bool is3DTV() {
288 char is3DTV = '0';
289 IOFile fp(Res::edid3dInfoFile, "r");
290 (void)fp.read(is3DTV, 1);
291 ALOGI("3DTV EDID flag: %d", is3DTV);
292 return (is3DTV == '0') ? false : true;
293}
294
295bool isPanel3D() {
296 OvFD fd;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700297 if(!overlay::open(fd, 0 /*fb*/, Res::fbPath)){
Naseer Ahmed29a26812012-06-14 00:56:20 -0700298 ALOGE("isPanel3D Can't open framebuffer 0");
299 return false;
300 }
301 fb_fix_screeninfo finfo;
302 if(!mdp_wrapper::getFScreenInfo(fd.getFD(), finfo)) {
303 ALOGE("isPanel3D read fb0 failed");
304 }
305 fd.close();
306 return (FB_TYPE_3D_PANEL == finfo.type) ? true : false;
307}
308
309bool usePanel3D() {
310 if(!isPanel3D())
311 return false;
312 char value[PROPERTY_VALUE_MAX];
313 property_get("persist.user.panel3D", value, "0");
314 int usePanel3D = atoi(value);
315 return usePanel3D ? true : false;
316}
317
318bool send3DInfoPacket (uint32_t format3D) {
319 IOFile fp(Res::format3DFile, "wb");
320 (void)fp.write("%d", format3D);
321 if(!fp.valid()) {
322 ALOGE("send3DInfoPacket: no sysfs entry for setting 3d mode");
323 return false;
324 }
325 return true;
326}
327
328bool enableBarrier (uint32_t orientation) {
329 IOFile fp(Res::barrierFile, "wb");
330 (void)fp.write("%d", orientation);
331 if(!fp.valid()) {
332 ALOGE("enableBarrier no sysfs entry for "
333 "enabling barriers on 3D panel");
334 return false;
335 }
336 return true;
337}
338
339uint32_t getS3DFormat(uint32_t fmt) {
340 // The S3D is part of the HAL_PIXEL_FORMAT_YV12 value. Add
341 // an explicit check for the format
342 if (fmt == HAL_PIXEL_FORMAT_YV12) {
343 return 0;
344 }
345 uint32_t fmt3D = format3D(fmt);
346 uint32_t fIn3D = format3DInput(fmt3D); // MSB 2 bytes - inp
347 uint32_t fOut3D = format3DOutput(fmt3D); // LSB 2 bytes - out
348 fmt3D = fIn3D | fOut3D;
349 if (!fIn3D) {
350 fmt3D |= fOut3D << SHIFT_TOT_3D; //Set the input format
351 }
352 if (!fOut3D) {
353 switch (fIn3D) {
354 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
355 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
356 // For all side by side formats, set the output
357 // format as Side-by-Side i.e 0x1
358 fmt3D |= HAL_3D_IN_SIDE_BY_SIDE_L_R >> SHIFT_TOT_3D;
359 break;
360 default:
361 fmt3D |= fIn3D >> SHIFT_TOT_3D; //Set the output format
362 }
363 }
364 return fmt3D;
365}
366
Sushil Chauhanb4d184f2013-02-11 16:13:10 -0800367bool isMdssRotator() {
368 int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
369 return (mdpVersion >= qdutils::MDSS_V5);
370}
371
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800372void getDump(char *buf, size_t len, const char *prefix,
373 const mdp_overlay& ov) {
374 char str[256] = {'\0'};
375 snprintf(str, 256,
376 "%s id=%d z=%d fg=%d alpha=%d mask=%d flags=0x%x\n",
377 prefix, ov.id, ov.z_order, ov.is_fg, ov.alpha,
378 ov.transp_mask, ov.flags);
379 strncat(buf, str, strlen(str));
380 getDump(buf, len, "\tsrc(msmfb_img)", ov.src);
381 getDump(buf, len, "\tsrc_rect(mdp_rect)", ov.src_rect);
382 getDump(buf, len, "\tdst_rect(mdp_rect)", ov.dst_rect);
383}
384
385void getDump(char *buf, size_t len, const char *prefix,
386 const msmfb_img& ov) {
387 char str_src[256] = {'\0'};
388 snprintf(str_src, 256,
389 "%s w=%d h=%d format=%d %s\n",
390 prefix, ov.width, ov.height, ov.format,
391 overlay::utils::getFormatString(ov.format));
392 strncat(buf, str_src, strlen(str_src));
393}
394
395void getDump(char *buf, size_t len, const char *prefix,
396 const mdp_rect& ov) {
397 char str_rect[256] = {'\0'};
398 snprintf(str_rect, 256,
399 "%s x=%d y=%d w=%d h=%d\n",
400 prefix, ov.x, ov.y, ov.w, ov.h);
401 strncat(buf, str_rect, strlen(str_rect));
402}
403
404void getDump(char *buf, size_t len, const char *prefix,
405 const msmfb_overlay_data& ov) {
406 char str[256] = {'\0'};
407 snprintf(str, 256,
408 "%s id=%d\n",
409 prefix, ov.id);
410 strncat(buf, str, strlen(str));
411 getDump(buf, len, "\tdata(msmfb_data)", ov.data);
412}
413
414void getDump(char *buf, size_t len, const char *prefix,
415 const msmfb_data& ov) {
416 char str_data[256] = {'\0'};
417 snprintf(str_data, 256,
418 "%s offset=%d memid=%d id=%d flags=0x%x priv=%d\n",
419 prefix, ov.offset, ov.memory_id, ov.id, ov.flags,
420 ov.priv);
421 strncat(buf, str_data, strlen(str_data));
422}
423
424void getDump(char *buf, size_t len, const char *prefix,
425 const msm_rotator_img_info& rot) {
426 char str[256] = {'\0'};
427 snprintf(str, 256, "%s sessid=%u rot=%d, enable=%d downscale=%d\n",
428 prefix, rot.session_id, rot.rotations, rot.enable,
429 rot.downscale_ratio);
430 strncat(buf, str, strlen(str));
431 getDump(buf, len, "\tsrc", rot.src);
432 getDump(buf, len, "\tdst", rot.dst);
433 getDump(buf, len, "\tsrc_rect", rot.src_rect);
434}
435
436void getDump(char *buf, size_t len, const char *prefix,
437 const msm_rotator_data_info& rot) {
438 char str[256] = {'\0'};
439 snprintf(str, 256,
440 "%s sessid=%u verkey=%d\n",
441 prefix, rot.session_id, rot.version_key);
442 strncat(buf, str, strlen(str));
443 getDump(buf, len, "\tsrc", rot.src);
444 getDump(buf, len, "\tdst", rot.dst);
445 getDump(buf, len, "\tsrc_chroma", rot.src_chroma);
446 getDump(buf, len, "\tdst_chroma", rot.dst_chroma);
447}
448
Naseer Ahmed29a26812012-06-14 00:56:20 -0700449} // utils
450
451} // overlay