blob: dd030efa40f3102a636826b9a201924d363b8f45 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
Arun Kumar K.R7e5a1f82014-01-22 10:36:16 -08002* Copyright (c) 2011-2014, 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"
Manoj Kumar AVM8a220812013-10-10 11:46:06 -070039#include <hardware/hwcomposer_defs.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070040
41// just a helper static thingy
42namespace {
43struct IOFile {
44 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>
51 size_t read(T& r, size_t elem) {
52 if(fp) {
53 return ::fread(&r, sizeof(T), elem, fp);
54 }
55 return 0;
56 }
57 size_t write(const char* s, uint32_t val) {
58 if(fp) {
59 return ::fprintf(fp, s, val);
60 }
61 return 0;
62 }
63 bool valid() const { return fp != 0; }
64 ~IOFile() {
65 if(fp) ::fclose(fp);
66 fp=0;
67 }
68 FILE* fp;
69};
70}
71
72namespace overlay {
73
74//----------From class Res ------------------------------
Naseer Ahmedf48aef62012-07-20 09:05:53 -070075const char* const Res::fbPath = "/dev/graphics/fb%u";
Naseer Ahmed29a26812012-06-14 00:56:20 -070076const char* const Res::rotPath = "/dev/msm_rotator";
Naseer Ahmed29a26812012-06-14 00:56:20 -070077//--------------------------------------------------------
78
79
80
81namespace utils {
Ajay Dudanicb3da0a2012-09-07 13:37:49 -070082
Naseer Ahmed29a26812012-06-14 00:56:20 -070083//--------------------------------------------------------
Naseer Ahmedaf49e212012-07-31 19:13:41 -070084//Refer to graphics.h, gralloc_priv.h, msm_mdp.h
Naseer Ahmed29a26812012-06-14 00:56:20 -070085int getMdpFormat(int format) {
86 switch (format) {
Naseer Ahmedaf49e212012-07-31 19:13:41 -070087 //From graphics.h
Naseer Ahmed29a26812012-06-14 00:56:20 -070088 case HAL_PIXEL_FORMAT_RGBA_8888 :
89 return MDP_RGBA_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -070090 case HAL_PIXEL_FORMAT_RGBX_8888:
91 return MDP_RGBX_8888;
Naseer Ahmedaf49e212012-07-31 19:13:41 -070092 case HAL_PIXEL_FORMAT_RGB_888:
93 return MDP_RGB_888;
94 case HAL_PIXEL_FORMAT_RGB_565:
95 return MDP_RGB_565;
Ramkumar Radhakrishnanb8eb16d2014-10-09 13:54:07 -070096 case HAL_PIXEL_FORMAT_RGBA_5551:
97 return MDP_RGBA_5551;
98 case HAL_PIXEL_FORMAT_RGBA_4444:
99 return MDP_RGBA_4444;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700100 case HAL_PIXEL_FORMAT_BGRA_8888:
101 return MDP_BGRA_8888;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -0700102 case HAL_PIXEL_FORMAT_BGRX_8888:
103 return MDP_BGRX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700104 case HAL_PIXEL_FORMAT_YV12:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700105 return MDP_Y_CR_CB_GH2V2;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700106 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
107 return MDP_Y_CBCR_H2V1;
108 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
109 return MDP_Y_CRCB_H2V2;
110
111 //From gralloc_priv.h
112 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
113 return MDP_Y_CBCR_H2V2_TILE;
114 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
115 return MDP_Y_CBCR_H2V2;
116 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
117 return MDP_Y_CRCB_H2V1;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700118 case HAL_PIXEL_FORMAT_YCbCr_422_I:
119 return MDP_YCBYCR_H2V1;
120 case HAL_PIXEL_FORMAT_YCrCb_422_I:
121 return MDP_YCRYCB_H2V1;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700122 case HAL_PIXEL_FORMAT_YCbCr_444_SP:
123 return MDP_Y_CBCR_H1V1;
124 case HAL_PIXEL_FORMAT_YCrCb_444_SP:
125 return MDP_Y_CRCB_H1V1;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700126 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmed8f8cb8a2013-08-22 15:34:30 -0400127 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
128 //NV12 encodeable format maps to the venus format on
129 //B-Family targets
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700130 return MDP_Y_CBCR_H2V2_VENUS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131 default:
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700132 //Unsupported by MDP
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700133 //---gralloc_priv.h-----
134 //HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO = 0x7FA30C01
135 //HAL_PIXEL_FORMAT_R_8 = 0x10D
136 //HAL_PIXEL_FORMAT_RG_88 = 0x10E
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800137 ALOGE("%s: Unsupported HAL format = 0x%x", __func__, format);
138 return -1;
139 }
140 // not reached
141 return -1;
142}
143
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700144// This function returns corresponding tile format
145// MDSS support following RGB tile formats
146// 32 bit formats
147// 16 bit formats
148int getMdpFormat(int format, bool tileEnabled)
149{
150 if(!tileEnabled) {
151 return getMdpFormat(format);
152 }
153 switch (format) {
154 case HAL_PIXEL_FORMAT_RGBA_8888 :
155 return MDP_RGBA_8888_TILE;
156 case HAL_PIXEL_FORMAT_RGBX_8888:
157 return MDP_RGBX_8888_TILE;
158 case HAL_PIXEL_FORMAT_RGB_565:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800159 return MDP_RGB_565_TILE;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700160 case HAL_PIXEL_FORMAT_BGRA_8888:
161 return MDP_BGRA_8888_TILE;
162 case HAL_PIXEL_FORMAT_BGRX_8888:
163 return MDP_BGRX_8888_TILE;
164 default:
165 return getMdpFormat(format);
166 }
167}
168
169
170
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800171//Takes mdp format as input and translates to equivalent HAL format
172//Refer to graphics.h, gralloc_priv.h, msm_mdp.h for formats.
173int getHALFormat(int mdpFormat) {
174 switch (mdpFormat) {
175 //From graphics.h
176 case MDP_RGBA_8888:
177 return HAL_PIXEL_FORMAT_RGBA_8888;
178 case MDP_RGBX_8888:
179 return HAL_PIXEL_FORMAT_RGBX_8888;
180 case MDP_RGB_888:
181 return HAL_PIXEL_FORMAT_RGB_888;
182 case MDP_RGB_565:
183 return HAL_PIXEL_FORMAT_RGB_565;
Ramkumar Radhakrishnanb8eb16d2014-10-09 13:54:07 -0700184 case MDP_RGBA_5551:
185 return HAL_PIXEL_FORMAT_RGBA_5551;
186 case MDP_RGBA_4444:
187 return HAL_PIXEL_FORMAT_RGBA_4444;
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800188 case MDP_BGRA_8888:
189 return HAL_PIXEL_FORMAT_BGRA_8888;
190 case MDP_Y_CR_CB_GH2V2:
191 return HAL_PIXEL_FORMAT_YV12;
192 case MDP_Y_CBCR_H2V1:
193 return HAL_PIXEL_FORMAT_YCbCr_422_SP;
194 case MDP_Y_CRCB_H2V2:
195 return HAL_PIXEL_FORMAT_YCrCb_420_SP;
196
197 //From gralloc_priv.h
198 case MDP_Y_CBCR_H2V2_TILE:
199 return HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED;
200 case MDP_Y_CBCR_H2V2:
201 return HAL_PIXEL_FORMAT_YCbCr_420_SP;
202 case MDP_Y_CRCB_H2V1:
203 return HAL_PIXEL_FORMAT_YCrCb_422_SP;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700204 case MDP_YCBYCR_H2V1:
205 return HAL_PIXEL_FORMAT_YCbCr_422_I;
206 case MDP_YCRYCB_H2V1:
207 return HAL_PIXEL_FORMAT_YCrCb_422_I;
208 case MDP_Y_CBCR_H1V1:
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800209 return HAL_PIXEL_FORMAT_YCbCr_444_SP;
210 case MDP_Y_CRCB_H1V1:
211 return HAL_PIXEL_FORMAT_YCrCb_444_SP;
212 case MDP_Y_CBCR_H2V2_VENUS:
213 return HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
214 default:
215 ALOGE("%s: Unsupported MDP format = 0x%x", __func__, mdpFormat);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700216 return -1;
217 }
218 // not reached
219 return -1;
220}
221
Saurabh Shahd1a22782013-04-10 09:09:45 -0700222int getMdpOrient(eTransform rotation) {
223 int retTrans = 0;
224 bool trans90 = false;
225 int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
226 bool aFamily = (mdpVersion < qdutils::MDSS_V5);
227
228 ALOGD_IF(DEBUG_OVERLAY, "%s: In rotation = %d", __FUNCTION__, rotation);
229 if(rotation & OVERLAY_TRANSFORM_ROT_90) {
230 retTrans |= MDP_ROT_90;
231 trans90 = true;
232 }
233
234 if(rotation & OVERLAY_TRANSFORM_FLIP_H) {
235 if(trans90 && aFamily) {
236 //Swap for a-family, since its driver does 90 first
237 retTrans |= MDP_FLIP_UD;
238 } else {
239 retTrans |= MDP_FLIP_LR;
240 }
241 }
242
243 if(rotation & OVERLAY_TRANSFORM_FLIP_V) {
244 if(trans90 && aFamily) {
245 //Swap for a-family, since its driver does 90 first
246 retTrans |= MDP_FLIP_LR;
247 } else {
248 retTrans |= MDP_FLIP_UD;
249 }
250 }
251
252 ALOGD_IF(DEBUG_OVERLAY, "%s: Out rotation = %d", __FUNCTION__, retTrans);
253 return retTrans;
254}
255
Saurabh Shah1a03d482013-05-29 13:44:20 -0700256void getDecimationFactor(const int& src_w, const int& src_h,
Saurabh Shahb6810df2014-06-17 16:00:22 -0700257 const int& dst_w, const int& dst_h, uint8_t& horzDeci,
258 uint8_t& vertDeci) {
259 horzDeci = 0;
260 vertDeci = 0;
261 float horDscale = ceilf((float)src_w / (float)dst_w);
262 float verDscale = ceilf((float)src_h / (float)dst_h);
Raj Kamal389d6e32014-08-04 14:43:24 +0530263 qdutils::MDPVersion& mdpHw = qdutils::MDPVersion::getInstance();
Saurabh Shah1a03d482013-05-29 13:44:20 -0700264
265 //Next power of 2, if not already
266 horDscale = powf(2.0f, ceilf(log2f(horDscale)));
267 verDscale = powf(2.0f, ceilf(log2f(verDscale)));
268
Raj Kamal389d6e32014-08-04 14:43:24 +0530269 //Since MDP can do downscale and has better quality, split the task
Saurabh Shah1a03d482013-05-29 13:44:20 -0700270 //between decimator and MDP downscale
Raj Kamal389d6e32014-08-04 14:43:24 +0530271 horDscale /= (float)mdpHw.getMaxMDPDownscale();
272 verDscale /= (float)mdpHw.getMaxMDPDownscale();
Saurabh Shahb6810df2014-06-17 16:00:22 -0700273
274 if((int)horDscale)
275 horzDeci = (uint8_t)log2f(horDscale);
276
277 if((int)verDscale)
278 vertDeci = (uint8_t)log2f(verDscale);
279
Jeykumar Sankaran39305802014-12-12 17:55:57 -0800280 if(src_w > (int) mdpHw.getMaxPipeWidth()) {
Saurabh Shahb6810df2014-06-17 16:00:22 -0700281 //If the client sends us something > what a layer mixer supports
282 //then it means it doesn't want to use split-pipe but wants us to
Saurabh Shahdbbf2aa2014-12-29 10:54:02 -0800283 //decimate. A minimum decimation of 1 will ensure that the width is
Saurabh Shahb6810df2014-06-17 16:00:22 -0700284 //always within layer mixer limits.
Saurabh Shahdbbf2aa2014-12-29 10:54:02 -0800285 const uint8_t minDeci = 1;
286 if(horzDeci < minDeci)
287 horzDeci = minDeci;
Saurabh Shahb6810df2014-06-17 16:00:22 -0700288 }
Saurabh Shah1a03d482013-05-29 13:44:20 -0700289}
290
Saurabh Shahacf10202013-02-26 10:15:15 -0800291static inline int compute(const uint32_t& x, const uint32_t& y,
292 const uint32_t& z) {
293 return x - ( y + z );
294}
295
Saurabh Shah76fd6552013-03-14 14:45:38 -0700296void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800297 if(tr & OVERLAY_TRANSFORM_FLIP_H) {
298 srcCrop.x = compute(whf.w, srcCrop.x, srcCrop.w);
299 }
300 if(tr & OVERLAY_TRANSFORM_FLIP_V) {
301 srcCrop.y = compute(whf.h, srcCrop.y, srcCrop.h);
302 }
303 if(tr & OVERLAY_TRANSFORM_ROT_90) {
304 int tmp = srcCrop.x;
305 srcCrop.x = compute(whf.h,
306 srcCrop.y,
307 srcCrop.h);
308 srcCrop.y = tmp;
309 swap(whf.w, whf.h);
310 swap(srcCrop.w, srcCrop.h);
311 }
Saurabh Shahacf10202013-02-26 10:15:15 -0800312}
313
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800314void getDump(char *buf, size_t len, const char *prefix,
315 const mdp_overlay& ov) {
316 char str[256] = {'\0'};
317 snprintf(str, 256,
Saurabh Shah2c8ad052014-08-15 13:27:46 -0700318 "%s id=%d z=%d alpha=%d mask=%d flags=0x%x H.Deci=%d,"
Saurabh Shahbd2d0832013-04-04 14:33:08 -0700319 "V.Deci=%d\n",
Saurabh Shah2c8ad052014-08-15 13:27:46 -0700320 prefix, ov.id, ov.z_order, ov.alpha,
Saurabh Shahbd2d0832013-04-04 14:33:08 -0700321 ov.transp_mask, ov.flags, ov.horz_deci, ov.vert_deci);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800322 strlcat(buf, str, len);
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700323 getDump(buf, len, "\tsrc", ov.src);
324 getDump(buf, len, "\tsrc_rect", ov.src_rect);
325 getDump(buf, len, "\tdst_rect", ov.dst_rect);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800326}
327
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800328void getDump(char *buf, size_t len, const char *prefix,
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800329 const msmfb_img& ov) {
330 char str_src[256] = {'\0'};
331 snprintf(str_src, 256,
332 "%s w=%d h=%d format=%d %s\n",
333 prefix, ov.width, ov.height, ov.format,
334 overlay::utils::getFormatString(ov.format));
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800335 strlcat(buf, str_src, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800336}
337
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800338void getDump(char *buf, size_t len, const char *prefix,
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800339 const mdp_rect& ov) {
340 char str_rect[256] = {'\0'};
341 snprintf(str_rect, 256,
342 "%s x=%d y=%d w=%d h=%d\n",
343 prefix, ov.x, ov.y, ov.w, ov.h);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800344 strlcat(buf, str_rect, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800345}
346
347void getDump(char *buf, size_t len, const char *prefix,
348 const msmfb_overlay_data& ov) {
349 char str[256] = {'\0'};
350 snprintf(str, 256,
351 "%s id=%d\n",
352 prefix, ov.id);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800353 strlcat(buf, str, len);
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700354 getDump(buf, len, "\tdata", ov.data);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800355}
356
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800357void getDump(char *buf, size_t len, const char *prefix,
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800358 const msmfb_data& ov) {
359 char str_data[256] = {'\0'};
360 snprintf(str_data, 256,
Saurabh Shah7ef9ec92013-10-29 10:32:23 -0700361 "%s offset=%d memid=%d id=%d flags=0x%x\n",
362 prefix, ov.offset, ov.memory_id, ov.id, ov.flags);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800363 strlcat(buf, str_data, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800364}
365
366void getDump(char *buf, size_t len, const char *prefix,
367 const msm_rotator_img_info& rot) {
368 char str[256] = {'\0'};
369 snprintf(str, 256, "%s sessid=%u rot=%d, enable=%d downscale=%d\n",
370 prefix, rot.session_id, rot.rotations, rot.enable,
371 rot.downscale_ratio);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800372 strlcat(buf, str, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800373 getDump(buf, len, "\tsrc", rot.src);
374 getDump(buf, len, "\tdst", rot.dst);
375 getDump(buf, len, "\tsrc_rect", rot.src_rect);
376}
377
378void getDump(char *buf, size_t len, const char *prefix,
379 const msm_rotator_data_info& rot) {
380 char str[256] = {'\0'};
381 snprintf(str, 256,
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700382 "%s sessid=%u\n",
383 prefix, rot.session_id);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800384 strlcat(buf, str, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800385 getDump(buf, len, "\tsrc", rot.src);
386 getDump(buf, len, "\tdst", rot.dst);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800387}
388
Sushil Chauhan1cac8152013-05-08 15:53:51 -0700389//Helper to even out x,w and y,h pairs
390//x,y are always evened to ceil and w,h are evened to floor
391void normalizeCrop(uint32_t& xy, uint32_t& wh) {
392 if(xy & 1) {
393 even_ceil(xy);
394 if(wh & 1)
395 even_floor(wh);
396 else
397 wh -= 2;
398 } else {
399 even_floor(wh);
400 }
401}
402
Naseer Ahmed29a26812012-06-14 00:56:20 -0700403} // utils
404
405} // overlay