blob: f9ee326672c189b65634c514480b6cbb1653ceee [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;
96 case HAL_PIXEL_FORMAT_BGRA_8888:
97 return MDP_BGRA_8888;
Sushil Chauhan1f6d68f2013-10-11 11:49:35 -070098 case HAL_PIXEL_FORMAT_BGRX_8888:
99 return MDP_BGRX_8888;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700100 case HAL_PIXEL_FORMAT_YV12:
Saurabh Shahae1044e2012-08-21 16:03:32 -0700101 return MDP_Y_CR_CB_GH2V2;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700102 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;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700114 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;
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700118 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;
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700122 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
Naseer Ahmed8f8cb8a2013-08-22 15:34:30 -0400123 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
124 //NV12 encodeable format maps to the venus format on
125 //B-Family targets
Sushil Chauhanc5e61482012-08-22 17:13:32 -0700126 return MDP_Y_CBCR_H2V2_VENUS;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127 default:
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700128 //Unsupported by MDP
129 //---graphics.h--------
130 //HAL_PIXEL_FORMAT_RGBA_5551
131 //HAL_PIXEL_FORMAT_RGBA_4444
Naseer Ahmedaf49e212012-07-31 19:13:41 -0700132 //---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
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800136 ALOGE("%s: Unsupported HAL format = 0x%x", __func__, format);
137 return -1;
138 }
139 // not reached
140 return -1;
141}
142
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700143// This function returns corresponding tile format
144// MDSS support following RGB tile formats
145// 32 bit formats
146// 16 bit formats
147int 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:
Manoj Kumar AVM5a5529b2014-02-24 18:16:37 -0800158 return MDP_RGB_565_TILE;
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700159 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
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800170//Takes mdp format as input and translates to equivalent HAL format
171//Refer to graphics.h, gralloc_priv.h, msm_mdp.h for formats.
172int 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;
Ramkumar Radhakrishnanb52399c2013-08-06 20:17:29 -0700199 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:
Saurabh Shahfc3652f2013-02-15 13:15:45 -0800204 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);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700211 return -1;
212 }
213 // not reached
214 return -1;
215}
216
Saurabh Shahd1a22782013-04-10 09:09:45 -0700217int 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
Saurabh Shah1a03d482013-05-29 13:44:20 -0700251void getDecimationFactor(const int& src_w, const int& src_h,
Saurabh Shahb6810df2014-06-17 16:00:22 -0700252 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);
Raj Kamal389d6e32014-08-04 14:43:24 +0530258 qdutils::MDPVersion& mdpHw = qdutils::MDPVersion::getInstance();
Saurabh Shah1a03d482013-05-29 13:44:20 -0700259
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
Raj Kamal389d6e32014-08-04 14:43:24 +0530264 //Since MDP can do downscale and has better quality, split the task
Saurabh Shah1a03d482013-05-29 13:44:20 -0700265 //between decimator and MDP downscale
Raj Kamal389d6e32014-08-04 14:43:24 +0530266 horDscale /= (float)mdpHw.getMaxMDPDownscale();
267 verDscale /= (float)mdpHw.getMaxMDPDownscale();
Saurabh Shahb6810df2014-06-17 16:00:22 -0700268
269 if((int)horDscale)
270 horzDeci = (uint8_t)log2f(horDscale);
271
272 if((int)verDscale)
273 vertDeci = (uint8_t)log2f(verDscale);
274
Raj Kamal389d6e32014-08-04 14:43:24 +0530275 if(src_w > mdpHw.getMaxMixerWidth()) {
Saurabh Shahb6810df2014-06-17 16:00:22 -0700276 //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 }
Saurabh Shah1a03d482013-05-29 13:44:20 -0700283}
284
Saurabh Shahacf10202013-02-26 10:15:15 -0800285static inline int compute(const uint32_t& x, const uint32_t& y,
286 const uint32_t& z) {
287 return x - ( y + z );
288}
289
Saurabh Shah76fd6552013-03-14 14:45:38 -0700290void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800291 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 }
Saurabh Shahacf10202013-02-26 10:15:15 -0800306}
307
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800308void getDump(char *buf, size_t len, const char *prefix,
309 const mdp_overlay& ov) {
310 char str[256] = {'\0'};
311 snprintf(str, 256,
Saurabh Shah2c8ad052014-08-15 13:27:46 -0700312 "%s id=%d z=%d alpha=%d mask=%d flags=0x%x H.Deci=%d,"
Saurabh Shahbd2d0832013-04-04 14:33:08 -0700313 "V.Deci=%d\n",
Saurabh Shah2c8ad052014-08-15 13:27:46 -0700314 prefix, ov.id, ov.z_order, ov.alpha,
Saurabh Shahbd2d0832013-04-04 14:33:08 -0700315 ov.transp_mask, ov.flags, ov.horz_deci, ov.vert_deci);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800316 strlcat(buf, str, len);
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700317 getDump(buf, len, "\tsrc", ov.src);
318 getDump(buf, len, "\tsrc_rect", ov.src_rect);
319 getDump(buf, len, "\tdst_rect", ov.dst_rect);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800320}
321
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800322void getDump(char *buf, size_t len, const char *prefix,
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800323 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));
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800329 strlcat(buf, str_src, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800330}
331
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800332void getDump(char *buf, size_t len, const char *prefix,
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800333 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);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800338 strlcat(buf, str_rect, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800339}
340
341void 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);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800347 strlcat(buf, str, len);
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700348 getDump(buf, len, "\tdata", ov.data);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800349}
350
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800351void getDump(char *buf, size_t len, const char *prefix,
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800352 const msmfb_data& ov) {
353 char str_data[256] = {'\0'};
354 snprintf(str_data, 256,
Saurabh Shah7ef9ec92013-10-29 10:32:23 -0700355 "%s offset=%d memid=%d id=%d flags=0x%x\n",
356 prefix, ov.offset, ov.memory_id, ov.id, ov.flags);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800357 strlcat(buf, str_data, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800358}
359
360void 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);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800366 strlcat(buf, str, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800367 getDump(buf, len, "\tsrc", rot.src);
368 getDump(buf, len, "\tdst", rot.dst);
369 getDump(buf, len, "\tsrc_rect", rot.src_rect);
370}
371
372void 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,
Saurabh Shahae61b2b2013-04-10 16:37:25 -0700376 "%s sessid=%u\n",
377 prefix, rot.session_id);
Ramkumar Radhakrishnan36bd5272014-01-31 20:03:01 -0800378 strlcat(buf, str, len);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800379 getDump(buf, len, "\tsrc", rot.src);
380 getDump(buf, len, "\tdst", rot.dst);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800381}
382
Sushil Chauhan1cac8152013-05-08 15:53:51 -0700383//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
385void 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
Naseer Ahmed29a26812012-06-14 00:56:20 -0700397} // utils
398
399} // overlay