1 /* 2 * Copyright (c) 2011-2014, 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 #include <hardware/hwcomposer_defs.h> 40 41 // just a helper static thingy 42 namespace { 43 struct 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 72 namespace overlay { 73 74 //----------From class Res ------------------------------ 75 const char* const Res::fbPath = "/dev/graphics/fb%u"; 76 const char* const Res::rotPath = "/dev/msm_rotator"; 77 //-------------------------------------------------------- 78 79 80 81 namespace utils { 82 83 //-------------------------------------------------------- 84 //Refer to graphics.h, gralloc_priv.h, msm_mdp.h 85 int getMdpFormat(int format) { 86 switch (format) { 87 //From graphics.h 88 case HAL_PIXEL_FORMAT_RGBA_8888 : 89 return MDP_RGBA_8888; 90 case HAL_PIXEL_FORMAT_RGBX_8888: 91 return MDP_RGBX_8888; 92 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; 98 case HAL_PIXEL_FORMAT_BGRX_8888: 99 return MDP_BGRX_8888; 100 case HAL_PIXEL_FORMAT_YV12: 101 return MDP_Y_CR_CB_GH2V2; 102 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; 114 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; 118 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; 122 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: 123 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: 124 //NV12 encodeable format maps to the venus format on 125 //B-Family targets 126 return MDP_Y_CBCR_H2V2_VENUS; 127 default: 128 //Unsupported by MDP 129 //---graphics.h-------- 130 //HAL_PIXEL_FORMAT_RGBA_5551 131 //HAL_PIXEL_FORMAT_RGBA_4444 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 // This function returns corresponding tile format 144 // MDSS support following RGB tile formats 145 // 32 bit formats 146 // 16 bit formats 147 int 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: 158 return MDP_RGB_565_TILE; 159 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 170 //Takes mdp format as input and translates to equivalent HAL format 171 //Refer to graphics.h, gralloc_priv.h, msm_mdp.h for formats. 172 int 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; 199 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: 204 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); 211 return -1; 212 } 213 // not reached 214 return -1; 215 } 216 217 int 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 251 int getDownscaleFactor(const int& src_w, const int& src_h, 252 const int& dst_w, const int& dst_h) { 253 int dscale_factor = utils::ROT_DS_NONE; 254 // We need this check to engage the rotator whenever possible to assist MDP 255 // in performing video downscale. 256 // This saves bandwidth and avoids causing the driver to make too many panel 257 // -mode switches between BLT (writeback) and non-BLT (Direct) modes. 258 // Use-case: Video playback [with downscaling and rotation]. 259 if (dst_w && dst_h) 260 { 261 float fDscale = (float)(src_w * src_h) / (float)(dst_w * dst_h); 262 uint32_t dscale = (int)sqrtf(fDscale); 263 264 if(dscale < 2) { 265 // Down-scale to > 50% of orig. 266 dscale_factor = utils::ROT_DS_NONE; 267 } else if(dscale < 4) { 268 // Down-scale to between > 25% to <= 50% of orig. 269 dscale_factor = utils::ROT_DS_HALF; 270 } else if(dscale < 8) { 271 // Down-scale to between > 12.5% to <= 25% of orig. 272 dscale_factor = utils::ROT_DS_FOURTH; 273 } else { 274 // Down-scale to <= 12.5% of orig. 275 dscale_factor = utils::ROT_DS_EIGHTH; 276 } 277 } 278 return dscale_factor; 279 } 280 281 void getDecimationFactor(const int& src_w, const int& src_h, 282 const int& dst_w, const int& dst_h, uint8_t& horzDeci, 283 uint8_t& vertDeci) { 284 horzDeci = 0; 285 vertDeci = 0; 286 float horDscale = ceilf((float)src_w / (float)dst_w); 287 float verDscale = ceilf((float)src_h / (float)dst_h); 288 289 //Next power of 2, if not already 290 horDscale = powf(2.0f, ceilf(log2f(horDscale))); 291 verDscale = powf(2.0f, ceilf(log2f(verDscale))); 292 293 //Since MDP can do 1/4 dscale and has better quality, split the task 294 //between decimator and MDP downscale 295 horDscale /= 4.0f; 296 verDscale /= 4.0f; 297 298 if((int)horDscale) 299 horzDeci = (uint8_t)log2f(horDscale); 300 301 if((int)verDscale) 302 vertDeci = (uint8_t)log2f(verDscale); 303 304 if(src_w > 2048) { 305 //If the client sends us something > what a layer mixer supports 306 //then it means it doesn't want to use split-pipe but wants us to 307 //decimate. A minimum decimation of 2 will ensure that the width is 308 //always within layer mixer limits. 309 if(horzDeci < 2) 310 horzDeci = 2; 311 } 312 } 313 314 static inline int compute(const uint32_t& x, const uint32_t& y, 315 const uint32_t& z) { 316 return x - ( y + z ); 317 } 318 319 void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop) { 320 if(tr & OVERLAY_TRANSFORM_FLIP_H) { 321 srcCrop.x = compute(whf.w, srcCrop.x, srcCrop.w); 322 } 323 if(tr & OVERLAY_TRANSFORM_FLIP_V) { 324 srcCrop.y = compute(whf.h, srcCrop.y, srcCrop.h); 325 } 326 if(tr & OVERLAY_TRANSFORM_ROT_90) { 327 int tmp = srcCrop.x; 328 srcCrop.x = compute(whf.h, 329 srcCrop.y, 330 srcCrop.h); 331 srcCrop.y = tmp; 332 swap(whf.w, whf.h); 333 swap(srcCrop.w, srcCrop.h); 334 } 335 } 336 337 void getDump(char *buf, size_t len, const char *prefix, 338 const mdp_overlay& ov) { 339 char str[256] = {'\0'}; 340 snprintf(str, 256, 341 "%s id=%d z=%d fg=%d alpha=%d mask=%d flags=0x%x H.Deci=%d," 342 "V.Deci=%d\n", 343 prefix, ov.id, ov.z_order, ov.is_fg, ov.alpha, 344 ov.transp_mask, ov.flags, ov.horz_deci, ov.vert_deci); 345 strlcat(buf, str, len); 346 getDump(buf, len, "\tsrc", ov.src); 347 getDump(buf, len, "\tsrc_rect", ov.src_rect); 348 getDump(buf, len, "\tdst_rect", ov.dst_rect); 349 } 350 351 void getDump(char *buf, size_t len, const char *prefix, 352 const msmfb_img& ov) { 353 char str_src[256] = {'\0'}; 354 snprintf(str_src, 256, 355 "%s w=%d h=%d format=%d %s\n", 356 prefix, ov.width, ov.height, ov.format, 357 overlay::utils::getFormatString(ov.format)); 358 strlcat(buf, str_src, len); 359 } 360 361 void getDump(char *buf, size_t len, const char *prefix, 362 const mdp_rect& ov) { 363 char str_rect[256] = {'\0'}; 364 snprintf(str_rect, 256, 365 "%s x=%d y=%d w=%d h=%d\n", 366 prefix, ov.x, ov.y, ov.w, ov.h); 367 strlcat(buf, str_rect, len); 368 } 369 370 void getDump(char *buf, size_t len, const char *prefix, 371 const msmfb_overlay_data& ov) { 372 char str[256] = {'\0'}; 373 snprintf(str, 256, 374 "%s id=%d\n", 375 prefix, ov.id); 376 strlcat(buf, str, len); 377 getDump(buf, len, "\tdata", ov.data); 378 } 379 380 void getDump(char *buf, size_t len, const char *prefix, 381 const msmfb_data& ov) { 382 char str_data[256] = {'\0'}; 383 snprintf(str_data, 256, 384 "%s offset=%d memid=%d id=%d flags=0x%x\n", 385 prefix, ov.offset, ov.memory_id, ov.id, ov.flags); 386 strlcat(buf, str_data, len); 387 } 388 389 void getDump(char *buf, size_t len, const char *prefix, 390 const msm_rotator_img_info& rot) { 391 char str[256] = {'\0'}; 392 snprintf(str, 256, "%s sessid=%u rot=%d, enable=%d downscale=%d\n", 393 prefix, rot.session_id, rot.rotations, rot.enable, 394 rot.downscale_ratio); 395 strlcat(buf, str, len); 396 getDump(buf, len, "\tsrc", rot.src); 397 getDump(buf, len, "\tdst", rot.dst); 398 getDump(buf, len, "\tsrc_rect", rot.src_rect); 399 } 400 401 void getDump(char *buf, size_t len, const char *prefix, 402 const msm_rotator_data_info& rot) { 403 char str[256] = {'\0'}; 404 snprintf(str, 256, 405 "%s sessid=%u\n", 406 prefix, rot.session_id); 407 strlcat(buf, str, len); 408 getDump(buf, len, "\tsrc", rot.src); 409 getDump(buf, len, "\tdst", rot.dst); 410 } 411 412 //Helper to even out x,w and y,h pairs 413 //x,y are always evened to ceil and w,h are evened to floor 414 void normalizeCrop(uint32_t& xy, uint32_t& wh) { 415 if(xy & 1) { 416 even_ceil(xy); 417 if(wh & 1) 418 even_floor(wh); 419 else 420 wh -= 2; 421 } else { 422 even_floor(wh); 423 } 424 } 425 426 } // utils 427 428 } // overlay 429