hwc_utils.cpp revision 5a107b4b7124301baf3b9e58089c7621fb9dfc95
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * Copyright (C) 2012-2013, The Linux Foundation All rights reserved. 4 * 5 * Not a Contribution, Apache license notifications and license are retained 6 * for attribution purposes only. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20#define HWC_UTILS_DEBUG 0 21#include <sys/ioctl.h> 22#include <EGL/egl.h> 23#include <cutils/properties.h> 24#include <gralloc_priv.h> 25#include <fb_priv.h> 26#include <overlay.h> 27#include "hwc_utils.h" 28#include "hwc_mdpcomp.h" 29#include "hwc_fbupdate.h" 30#include "mdp_version.h" 31#include "hwc_copybit.h" 32#include "external.h" 33#include "QService.h" 34#include "comptype.h" 35namespace qhwc { 36 37// Opens Framebuffer device 38static void openFramebufferDevice(hwc_context_t *ctx) 39{ 40 hw_module_t const *module; 41 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) { 42 framebuffer_open(module, &(ctx->mFbDev)); 43 private_module_t* m = reinterpret_cast<private_module_t*>( 44 ctx->mFbDev->common.module); 45 //xres, yres may not be 32 aligned 46 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = m->finfo.line_length / 47 (m->info.xres/8); 48 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = m->info.xres; 49 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = m->info.yres; 50 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = ctx->mFbDev->xdpi; 51 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ctx->mFbDev->ydpi; 52 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period = 53 1000000000l / ctx->mFbDev->fps; 54 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = openFb(HWC_DISPLAY_PRIMARY); 55 } 56} 57 58void initContext(hwc_context_t *ctx) 59{ 60 openFramebufferDevice(ctx); 61 overlay::Overlay::initOverlay(); 62 ctx->mOverlay = overlay::Overlay::getInstance(); 63 ctx->mQService = qService::QService::getInstance(ctx); 64 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion(); 65 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay(); 66 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType(); 67 //Is created and destroyed only once for primary 68 //For external it could get created and destroyed multiple times depending 69 //on what external we connect to. 70 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] = 71 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres, 72 HWC_DISPLAY_PRIMARY); 73 74 char value[PROPERTY_VALUE_MAX]; 75 // Check if the target supports copybit compostion (dyn/mdp/c2d) to 76 // decide if we need to open the copybit module. 77 int compositionType = 78 qdutils::QCCompositionType::getInstance().getCompositionType(); 79 80 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN | 81 qdutils::COMPOSITION_TYPE_MDP | 82 qdutils::COMPOSITION_TYPE_C2D)) { 83 ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit(); 84 } 85 86 ctx->mExtDisplay = new ExternalDisplay(ctx); 87 for (uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) 88 ctx->mLayerCache[i] = new LayerCache(); 89 ctx->mMDPComp = MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres); 90 MDPComp::init(ctx); 91 92 pthread_mutex_init(&(ctx->vstate.lock), NULL); 93 pthread_cond_init(&(ctx->vstate.cond), NULL); 94 ctx->vstate.enable = false; 95 ctx->mExtDispConfiguring = false; 96 ALOGI("Initializing Qualcomm Hardware Composer"); 97 ALOGI("MDP version: %d", ctx->mMDP.version); 98} 99 100void closeContext(hwc_context_t *ctx) 101{ 102 if(ctx->mOverlay) { 103 delete ctx->mOverlay; 104 ctx->mOverlay = NULL; 105 } 106 107 for(int i = 0; i< HWC_NUM_DISPLAY_TYPES; i++) { 108 if(ctx->mCopyBit[i]) { 109 delete ctx->mCopyBit[i]; 110 ctx->mCopyBit[i] = NULL; 111 } 112 } 113 114 if(ctx->mFbDev) { 115 framebuffer_close(ctx->mFbDev); 116 ctx->mFbDev = NULL; 117 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd); 118 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1; 119 } 120 121 if(ctx->mExtDisplay) { 122 delete ctx->mExtDisplay; 123 ctx->mExtDisplay = NULL; 124 } 125 126 for(int i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) { 127 if(ctx->mFBUpdate[i]) { 128 delete ctx->mFBUpdate[i]; 129 ctx->mFBUpdate[i] = NULL; 130 } 131 } 132 133 if(ctx->mMDPComp) { 134 delete ctx->mMDPComp; 135 ctx->mMDPComp = NULL; 136 } 137 138 pthread_mutex_destroy(&(ctx->vstate.lock)); 139 pthread_cond_destroy(&(ctx->vstate.cond)); 140} 141 142 143void dumpsys_log(android::String8& buf, const char* fmt, ...) 144{ 145 va_list varargs; 146 va_start(varargs, fmt); 147 buf.appendFormatV(fmt, varargs); 148 va_end(varargs); 149} 150 151static inline bool isAlphaScaled(hwc_layer_1_t const* layer) { 152 int dst_w, dst_h, src_w, src_h; 153 154 hwc_rect_t displayFrame = layer->displayFrame; 155 hwc_rect_t sourceCrop = layer->sourceCrop; 156 157 dst_w = displayFrame.right - displayFrame.left; 158 dst_h = displayFrame.bottom - displayFrame.top; 159 160 src_w = sourceCrop.right - sourceCrop.left; 161 src_h = sourceCrop.bottom - sourceCrop.top; 162 163 if(((src_w != dst_w) || (src_h != dst_h))) { 164 if(layer->blending != HWC_BLENDING_NONE) 165 return true; 166 } 167 return false; 168} 169 170void setListStats(hwc_context_t *ctx, 171 const hwc_display_contents_1_t *list, int dpy) { 172 173 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1; 174 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1; 175 ctx->listStats[dpy].skipCount = 0; 176 ctx->listStats[dpy].needsAlphaScale = false; 177 ctx->listStats[dpy].yuvCount = 0; 178 179 for (size_t i = 0; i < list->numHwLayers; i++) { 180 hwc_layer_1_t const* layer = &list->hwLayers[i]; 181 private_handle_t *hnd = (private_handle_t *)layer->handle; 182 183 //reset stored yuv index 184 ctx->listStats[dpy].yuvIndices[i] = -1; 185 186 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 187 continue; 188 //We disregard FB being skip for now! so the else if 189 } else if (isSkipLayer(&list->hwLayers[i])) { 190 ctx->listStats[dpy].skipCount++; 191 } 192 193 if(!ctx->listStats[dpy].needsAlphaScale) 194 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer); 195 196 if (UNLIKELY(isYuvBuffer(hnd))) { 197 int& yuvCount = ctx->listStats[dpy].yuvCount; 198 ctx->listStats[dpy].yuvIndices[yuvCount] = i; 199 yuvCount++; 200 } 201 } 202} 203 204 205static inline void calc_cut(float& leftCutRatio, float& topCutRatio, 206 float& rightCutRatio, float& bottomCutRatio, int orient) { 207 if(orient & HAL_TRANSFORM_FLIP_H) { 208 swap(leftCutRatio, rightCutRatio); 209 } 210 if(orient & HAL_TRANSFORM_FLIP_V) { 211 swap(topCutRatio, bottomCutRatio); 212 } 213 if(orient & HAL_TRANSFORM_ROT_90) { 214 //Anti clock swapping 215 float tmpCutRatio = leftCutRatio; 216 leftCutRatio = topCutRatio; 217 topCutRatio = rightCutRatio; 218 rightCutRatio = bottomCutRatio; 219 bottomCutRatio = tmpCutRatio; 220 } 221} 222 223bool isSecuring(hwc_context_t* ctx) { 224 if((ctx->mMDP.version < qdutils::MDSS_V5) && 225 (ctx->mMDP.version > qdutils::MDP_V3_0) && 226 ctx->mSecuring) { 227 return true; 228 } 229 return false; 230} 231 232bool isSecureModePolicy(int mdpVersion) { 233 if (mdpVersion < qdutils::MDSS_V5) 234 return true; 235 else 236 return false; 237} 238 239//Crops source buffer against destination and FB boundaries 240void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst, 241 const int fbWidth, const int fbHeight, int orient) { 242 int& crop_l = crop.left; 243 int& crop_t = crop.top; 244 int& crop_r = crop.right; 245 int& crop_b = crop.bottom; 246 int crop_w = crop.right - crop.left; 247 int crop_h = crop.bottom - crop.top; 248 249 int& dst_l = dst.left; 250 int& dst_t = dst.top; 251 int& dst_r = dst.right; 252 int& dst_b = dst.bottom; 253 int dst_w = abs(dst.right - dst.left); 254 int dst_h = abs(dst.bottom - dst.top); 255 256 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f, 257 bottomCutRatio = 0.0f; 258 259 if(dst_l < 0) { 260 leftCutRatio = (float)(0.0f - dst_l) / (float)dst_w; 261 dst_l = 0; 262 } 263 if(dst_r > fbWidth) { 264 rightCutRatio = (float)(dst_r - fbWidth) / (float)dst_w; 265 dst_r = fbWidth; 266 } 267 if(dst_t < 0) { 268 topCutRatio = (float)(0 - dst_t) / (float)dst_h; 269 dst_t = 0; 270 } 271 if(dst_b > fbHeight) { 272 bottomCutRatio = (float)(dst_b - fbHeight) / (float)dst_h; 273 dst_b = fbHeight; 274 } 275 276 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient); 277 crop_l += crop_w * leftCutRatio; 278 crop_t += crop_h * topCutRatio; 279 crop_r -= crop_w * rightCutRatio; 280 crop_b -= crop_h * bottomCutRatio; 281} 282 283bool isExternalActive(hwc_context_t* ctx) { 284 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive; 285} 286 287int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy, 288 int fd) { 289 int ret = 0; 290 struct mdp_buf_sync data; 291 int acquireFd[MAX_NUM_LAYERS]; 292 int count = 0; 293 int releaseFd = -1; 294 int fbFd = -1; 295 memset(&data, 0, sizeof(data)); 296 bool swapzero = false; 297 data.flags = MDP_BUF_SYNC_FLAG_WAIT; 298 data.acq_fen_fd = acquireFd; 299 data.rel_fen_fd = &releaseFd; 300 char property[PROPERTY_VALUE_MAX]; 301 if(property_get("debug.egl.swapinterval", property, "1") > 0) { 302 if(atoi(property) == 0) 303 swapzero = true; 304 } 305 306 //Accumulate acquireFenceFds 307 for(uint32_t i = 0; i < list->numHwLayers; i++) { 308 if(list->hwLayers[i].compositionType == HWC_OVERLAY && 309 list->hwLayers[i].acquireFenceFd != -1) { 310 if(UNLIKELY(swapzero)) 311 acquireFd[count++] = -1; 312 else 313 acquireFd[count++] = list->hwLayers[i].acquireFenceFd; 314 } 315 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 316 if(UNLIKELY(swapzero)) 317 acquireFd[count++] = -1; 318 else if(fd != -1) { 319 //set the acquireFD from fd - which is coming from c2d 320 acquireFd[count++] = fd; 321 // Buffer sync IOCTL should be async when using c2d fence is 322 // used 323 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT; 324 } else if(list->hwLayers[i].acquireFenceFd != -1) 325 acquireFd[count++] = list->hwLayers[i].acquireFenceFd; 326 } 327 } 328 329 data.acq_fen_fd_cnt = count; 330 fbFd = ctx->dpyAttr[dpy].fd; 331 //Waits for acquire fences, returns a release fence 332 if(LIKELY(!swapzero)) { 333 uint64_t start = systemTime(); 334 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data); 335 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d", 336 __FUNCTION__, (size_t) ns2ms(systemTime() - start)); 337 } 338 if(ret < 0) { 339 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s", 340 strerror(errno)); 341 } 342 for(uint32_t i = 0; i < list->numHwLayers; i++) { 343 if(list->hwLayers[i].compositionType == HWC_OVERLAY || 344 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 345 //Close the acquireFenceFds 346 if(list->hwLayers[i].acquireFenceFd > 0) { 347 close(list->hwLayers[i].acquireFenceFd); 348 list->hwLayers[i].acquireFenceFd = -1; 349 } 350 if(fd > 0) { 351 close(fd); 352 fd = -1; 353 } 354 //Populate releaseFenceFds. 355 if(UNLIKELY(swapzero)) 356 list->hwLayers[i].releaseFenceFd = -1; 357 else 358 list->hwLayers[i].releaseFenceFd = dup(releaseFd); 359 } 360 } 361 if(UNLIKELY(swapzero)){ 362 list->retireFenceFd = -1; 363 close(releaseFd); 364 } else { 365 list->retireFenceFd = releaseFd; 366 } 367 return ret; 368} 369 370void LayerCache::resetLayerCache(int num) { 371 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) { 372 hnd[i] = NULL; 373 } 374 numHwLayers = num; 375} 376 377void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) { 378 379 int numFbLayers = 0; 380 int numCacheableLayers = 0; 381 382 canUseLayerCache = false; 383 //Bail if geometry changed or num of layers changed 384 if(list->flags & HWC_GEOMETRY_CHANGED || 385 list->numHwLayers != numHwLayers ) { 386 resetLayerCache(list->numHwLayers); 387 return; 388 } 389 390 for(uint32_t i = 0; i < list->numHwLayers; i++) { 391 //Bail on skip layers 392 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) { 393 resetLayerCache(list->numHwLayers); 394 return; 395 } 396 397 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) { 398 numFbLayers++; 399 if(hnd[i] == NULL) { 400 hnd[i] = list->hwLayers[i].handle; 401 } else if (hnd[i] == 402 list->hwLayers[i].handle) { 403 numCacheableLayers++; 404 } else { 405 hnd[i] = NULL; 406 return; 407 } 408 } else { 409 hnd[i] = NULL; 410 } 411 } 412 if(numFbLayers == numCacheableLayers) 413 canUseLayerCache = true; 414 415 //XXX: The marking part is separate, if MDP comp wants 416 // to use it in the future. Right now getting MDP comp 417 // to use this is more trouble than it is worth. 418 markCachedLayersAsOverlay(list); 419} 420 421void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) { 422 //This optimization only works if ALL the layer handles 423 //that were on the framebuffer didn't change. 424 if(canUseLayerCache){ 425 for(uint32_t i = 0; i < list->numHwLayers; i++) { 426 if (list->hwLayers[i].handle && 427 list->hwLayers[i].handle == hnd[i] && 428 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET) 429 { 430 list->hwLayers[i].compositionType = HWC_OVERLAY; 431 } 432 } 433 } 434 435} 436 437};//namespace 438