hwc_utils.cpp revision 93393d36207ef501e851c98491befd9e528b6b1a
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 <binder/IServiceManager.h> 23#include <EGL/egl.h> 24#include <cutils/properties.h> 25#include <gralloc_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 "hwc_qclient.h" 34#include "QService.h" 35#include "comptype.h" 36 37using namespace qClient; 38using namespace qService; 39using namespace android; 40 41namespace qhwc { 42 43static int openFramebufferDevice(hwc_context_t *ctx) 44{ 45 struct fb_fix_screeninfo finfo; 46 struct fb_var_screeninfo info; 47 48 int fb_fd = openFb(HWC_DISPLAY_PRIMARY); 49 50 if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &info) == -1) 51 return -errno; 52 53 if (int(info.width) <= 0 || int(info.height) <= 0) { 54 // the driver doesn't return that information 55 // default to 160 dpi 56 info.width = ((info.xres * 25.4f)/160.0f + 0.5f); 57 info.height = ((info.yres * 25.4f)/160.0f + 0.5f); 58 } 59 60 float xdpi = (info.xres * 25.4f) / info.width; 61 float ydpi = (info.yres * 25.4f) / info.height; 62 63#ifdef MSMFB_METADATA_GET 64 struct msmfb_metadata metadata; 65 memset(&metadata, 0 , sizeof(metadata)); 66 metadata.op = metadata_op_frame_rate; 67 68 if (ioctl(fb_fd, MSMFB_METADATA_GET, &metadata) == -1) { 69 ALOGE("Error retrieving panel frame rate"); 70 return -errno; 71 } 72 73 float fps = metadata.data.panel_frame_rate; 74#else 75 //XXX: Remove reserved field usage on all baselines 76 //The reserved[3] field is used to store FPS by the driver. 77 float fps = info.reserved[3] & 0xFF; 78#endif 79 80 if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) == -1) 81 return -errno; 82 83 if (finfo.smem_len <= 0) 84 return -errno; 85 86 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = fb_fd; 87 //xres, yres may not be 32 aligned 88 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = finfo.line_length /(info.xres/8); 89 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = info.xres; 90 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = info.yres; 91 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = xdpi; 92 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ydpi; 93 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period = 1000000000l / fps; 94 95 return 0; 96} 97 98void initContext(hwc_context_t *ctx) 99{ 100 if(openFramebufferDevice(ctx) < 0) { 101 ALOGE("%s: failed to open framebuffer!!", __FUNCTION__); 102 } 103 104 overlay::Overlay::initOverlay(); 105 ctx->mOverlay = overlay::Overlay::getInstance(); 106 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion(); 107 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay(); 108 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType(); 109 //Is created and destroyed only once for primary 110 //For external it could get created and destroyed multiple times depending 111 //on what external we connect to. 112 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] = 113 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres, 114 HWC_DISPLAY_PRIMARY); 115 116 char value[PROPERTY_VALUE_MAX]; 117 // Check if the target supports copybit compostion (dyn/mdp/c2d) to 118 // decide if we need to open the copybit module. 119 int compositionType = 120 qdutils::QCCompositionType::getInstance().getCompositionType(); 121 122 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN | 123 qdutils::COMPOSITION_TYPE_MDP | 124 qdutils::COMPOSITION_TYPE_C2D)) { 125 ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit(); 126 } 127 128 ctx->mExtDisplay = new ExternalDisplay(ctx); 129 for (uint32_t i = 0; i < MAX_DISPLAYS; i++) 130 ctx->mLayerCache[i] = new LayerCache(); 131 ctx->mMDPComp = MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres); 132 MDPComp::init(ctx); 133 134 pthread_mutex_init(&(ctx->vstate.lock), NULL); 135 pthread_cond_init(&(ctx->vstate.cond), NULL); 136 ctx->vstate.enable = false; 137 ctx->mExtDispConfiguring = false; 138 139 //Right now hwc starts the service but anybody could do it, or it could be 140 //independent process as well. 141 QService::init(); 142 sp<IQClient> client = new QClient(ctx); 143 interface_cast<IQService>( 144 defaultServiceManager()->getService( 145 String16("display.qservice")))->connect(client); 146 147 ALOGI("Initializing Qualcomm Hardware Composer"); 148 ALOGI("MDP version: %d", ctx->mMDP.version); 149} 150 151void closeContext(hwc_context_t *ctx) 152{ 153 if(ctx->mOverlay) { 154 delete ctx->mOverlay; 155 ctx->mOverlay = NULL; 156 } 157 158 for(int i = 0; i < MAX_DISPLAYS; i++) { 159 if(ctx->mCopyBit[i]) { 160 delete ctx->mCopyBit[i]; 161 ctx->mCopyBit[i] = NULL; 162 } 163 } 164 165 if(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd) { 166 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd); 167 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1; 168 } 169 170 if(ctx->mExtDisplay) { 171 delete ctx->mExtDisplay; 172 ctx->mExtDisplay = NULL; 173 } 174 175 for(int i = 0; i < MAX_DISPLAYS; i++) { 176 if(ctx->mFBUpdate[i]) { 177 delete ctx->mFBUpdate[i]; 178 ctx->mFBUpdate[i] = NULL; 179 } 180 } 181 182 if(ctx->mMDPComp) { 183 delete ctx->mMDPComp; 184 ctx->mMDPComp = NULL; 185 } 186 187 pthread_mutex_destroy(&(ctx->vstate.lock)); 188 pthread_cond_destroy(&(ctx->vstate.cond)); 189} 190 191 192void dumpsys_log(android::String8& buf, const char* fmt, ...) 193{ 194 va_list varargs; 195 va_start(varargs, fmt); 196 buf.appendFormatV(fmt, varargs); 197 va_end(varargs); 198} 199 200/* Calculates the destination position based on the action safe rectangle */ 201void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x, 202 uint32_t& y, uint32_t& w, uint32_t& h) { 203 204 // if external supports underscan, do nothing 205 // it will be taken care in the driver 206 if(ctx->mExtDisplay->isCEUnderscanSupported()) 207 return; 208 209 float wRatio = 1.0; 210 float hRatio = 1.0; 211 float xRatio = 1.0; 212 float yRatio = 1.0; 213 214 float fbWidth = ctx->dpyAttr[dpy].xres; 215 float fbHeight = ctx->dpyAttr[dpy].yres; 216 217 float asX = 0; 218 float asY = 0; 219 float asW = fbWidth; 220 float asH= fbHeight; 221 char value[PROPERTY_VALUE_MAX]; 222 223 // Apply action safe parameters 224 property_get("hw.actionsafe.width", value, "0"); 225 int asWidthRatio = atoi(value); 226 property_get("hw.actionsafe.height", value, "0"); 227 int asHeightRatio = atoi(value); 228 // based on the action safe ratio, get the Action safe rectangle 229 asW = fbWidth * (1.0f - asWidthRatio / 100.0f); 230 asH = fbHeight * (1.0f - asHeightRatio / 100.0f); 231 asX = (fbWidth - asW) / 2; 232 asY = (fbHeight - asH) / 2; 233 234 // calculate the position ratio 235 xRatio = (float)x/fbWidth; 236 yRatio = (float)y/fbHeight; 237 wRatio = (float)w/fbWidth; 238 hRatio = (float)h/fbHeight; 239 240 //Calculate the position... 241 x = (xRatio * asW) + asX; 242 y = (yRatio * asH) + asY; 243 w = (wRatio * asW); 244 h = (hRatio * asH); 245 246 return; 247} 248 249bool needsScaling(hwc_layer_1_t const* layer) { 250 int dst_w, dst_h, src_w, src_h; 251 252 hwc_rect_t displayFrame = layer->displayFrame; 253 hwc_rect_t sourceCrop = layer->sourceCrop; 254 255 dst_w = displayFrame.right - displayFrame.left; 256 dst_h = displayFrame.bottom - displayFrame.top; 257 258 src_w = sourceCrop.right - sourceCrop.left; 259 src_h = sourceCrop.bottom - sourceCrop.top; 260 261 if(((src_w != dst_w) || (src_h != dst_h))) 262 return true; 263 264 return false; 265} 266 267bool isAlphaScaled(hwc_layer_1_t const* layer) { 268 if(needsScaling(layer)) { 269 if(layer->blending != HWC_BLENDING_NONE) 270 return true; 271 } 272 return false; 273} 274 275void setListStats(hwc_context_t *ctx, 276 const hwc_display_contents_1_t *list, int dpy) { 277 278 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1; 279 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1; 280 ctx->listStats[dpy].skipCount = 0; 281 ctx->listStats[dpy].needsAlphaScale = false; 282 ctx->listStats[dpy].yuvCount = 0; 283 ctx->mDMAInUse = false; 284 285 for (size_t i = 0; i < list->numHwLayers; i++) { 286 hwc_layer_1_t const* layer = &list->hwLayers[i]; 287 private_handle_t *hnd = (private_handle_t *)layer->handle; 288 289 //reset stored yuv index 290 ctx->listStats[dpy].yuvIndices[i] = -1; 291 292 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 293 continue; 294 //We disregard FB being skip for now! so the else if 295 } else if (isSkipLayer(&list->hwLayers[i])) { 296 ctx->listStats[dpy].skipCount++; 297 } else if (UNLIKELY(isYuvBuffer(hnd))) { 298 int& yuvCount = ctx->listStats[dpy].yuvCount; 299 ctx->listStats[dpy].yuvIndices[yuvCount] = i; 300 yuvCount++; 301 302 if((layer->transform & HWC_TRANSFORM_ROT_90) && !ctx->mDMAInUse) 303 ctx->mDMAInUse = true; 304 } 305 306 if(!ctx->listStats[dpy].needsAlphaScale) 307 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer); 308 } 309} 310 311 312static inline void calc_cut(float& leftCutRatio, float& topCutRatio, 313 float& rightCutRatio, float& bottomCutRatio, int orient) { 314 if(orient & HAL_TRANSFORM_FLIP_H) { 315 swap(leftCutRatio, rightCutRatio); 316 } 317 if(orient & HAL_TRANSFORM_FLIP_V) { 318 swap(topCutRatio, bottomCutRatio); 319 } 320 if(orient & HAL_TRANSFORM_ROT_90) { 321 //Anti clock swapping 322 float tmpCutRatio = leftCutRatio; 323 leftCutRatio = topCutRatio; 324 topCutRatio = rightCutRatio; 325 rightCutRatio = bottomCutRatio; 326 bottomCutRatio = tmpCutRatio; 327 } 328} 329 330bool isSecuring(hwc_context_t* ctx) { 331 if((ctx->mMDP.version < qdutils::MDSS_V5) && 332 (ctx->mMDP.version > qdutils::MDP_V3_0) && 333 ctx->mSecuring) { 334 return true; 335 } 336 return false; 337} 338 339bool isSecureModePolicy(int mdpVersion) { 340 if (mdpVersion < qdutils::MDSS_V5) 341 return true; 342 else 343 return false; 344} 345 346//Crops source buffer against destination and FB boundaries 347void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst, 348 const hwc_rect_t& scissor, int orient) { 349 350 int& crop_l = crop.left; 351 int& crop_t = crop.top; 352 int& crop_r = crop.right; 353 int& crop_b = crop.bottom; 354 int crop_w = crop.right - crop.left; 355 int crop_h = crop.bottom - crop.top; 356 357 int& dst_l = dst.left; 358 int& dst_t = dst.top; 359 int& dst_r = dst.right; 360 int& dst_b = dst.bottom; 361 int dst_w = abs(dst.right - dst.left); 362 int dst_h = abs(dst.bottom - dst.top); 363 364 const int& sci_l = scissor.left; 365 const int& sci_t = scissor.top; 366 const int& sci_r = scissor.right; 367 const int& sci_b = scissor.bottom; 368 int sci_w = abs(sci_r - sci_l); 369 int sci_h = abs(sci_b - sci_t); 370 371 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f, 372 bottomCutRatio = 0.0f; 373 374 if(dst_l < sci_l) { 375 leftCutRatio = (float)(sci_l - dst_l) / (float)dst_w; 376 dst_l = sci_l; 377 } 378 379 if(dst_r > sci_r) { 380 rightCutRatio = (float)(dst_r - sci_r) / (float)dst_w; 381 dst_r = sci_r; 382 } 383 384 if(dst_t < sci_t) { 385 topCutRatio = (float)(sci_t - dst_t) / (float)dst_h; 386 dst_t = sci_t; 387 } 388 389 if(dst_b > sci_b) { 390 bottomCutRatio = (float)(dst_b - sci_b) / (float)dst_h; 391 dst_b = sci_b; 392 } 393 394 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient); 395 crop_l += crop_w * leftCutRatio; 396 crop_t += crop_h * topCutRatio; 397 crop_r -= crop_w * rightCutRatio; 398 crop_b -= crop_h * bottomCutRatio; 399} 400 401void getNonWormholeRegion(hwc_display_contents_1_t* list, 402 hwc_rect_t& nwr) 403{ 404 uint32_t last = list->numHwLayers - 1; 405 hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame; 406 //Initiliaze nwr to first frame 407 nwr.left = list->hwLayers[0].displayFrame.left; 408 nwr.top = list->hwLayers[0].displayFrame.top; 409 nwr.right = list->hwLayers[0].displayFrame.right; 410 nwr.bottom = list->hwLayers[0].displayFrame.bottom; 411 412 for (uint32_t i = 1; i < last; i++) { 413 hwc_rect_t displayFrame = list->hwLayers[i].displayFrame; 414 nwr.left = min(nwr.left, displayFrame.left); 415 nwr.top = min(nwr.top, displayFrame.top); 416 nwr.right = max(nwr.right, displayFrame.right); 417 nwr.bottom = max(nwr.bottom, displayFrame.bottom); 418 } 419 420 //Intersect with the framebuffer 421 nwr.left = max(nwr.left, fbDisplayFrame.left); 422 nwr.top = max(nwr.top, fbDisplayFrame.top); 423 nwr.right = min(nwr.right, fbDisplayFrame.right); 424 nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom); 425 426} 427 428bool isExternalActive(hwc_context_t* ctx) { 429 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive; 430} 431 432void closeAcquireFds(hwc_display_contents_1_t* list) { 433 for(uint32_t i = 0; list && i < list->numHwLayers; i++) { 434 //Close the acquireFenceFds 435 //HWC_FRAMEBUFFER are -1 already by SF, rest we close. 436 if(list->hwLayers[i].acquireFenceFd >= 0) { 437 close(list->hwLayers[i].acquireFenceFd); 438 list->hwLayers[i].acquireFenceFd = -1; 439 } 440 } 441} 442 443int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy, 444 int fd) { 445 int ret = 0; 446 struct mdp_buf_sync data; 447 int acquireFd[MAX_NUM_LAYERS]; 448 int count = 0; 449 int releaseFd = -1; 450 int fbFd = -1; 451 memset(&data, 0, sizeof(data)); 452 bool swapzero = false; 453 data.flags = MDP_BUF_SYNC_FLAG_WAIT; 454 data.acq_fen_fd = acquireFd; 455 data.rel_fen_fd = &releaseFd; 456 char property[PROPERTY_VALUE_MAX]; 457 if(property_get("debug.egl.swapinterval", property, "1") > 0) { 458 if(atoi(property) == 0) 459 swapzero = true; 460 } 461 462 //Accumulate acquireFenceFds 463 for(uint32_t i = 0; i < list->numHwLayers; i++) { 464 if(list->hwLayers[i].compositionType == HWC_OVERLAY && 465 list->hwLayers[i].acquireFenceFd != -1) { 466 if(UNLIKELY(swapzero)) 467 acquireFd[count++] = -1; 468 else 469 acquireFd[count++] = list->hwLayers[i].acquireFenceFd; 470 } 471 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 472 if(UNLIKELY(swapzero)) 473 acquireFd[count++] = -1; 474 else if(fd != -1) { 475 //set the acquireFD from fd - which is coming from c2d 476 acquireFd[count++] = fd; 477 // Buffer sync IOCTL should be async when using c2d fence is 478 // used 479 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT; 480 } else if(list->hwLayers[i].acquireFenceFd != -1) 481 acquireFd[count++] = list->hwLayers[i].acquireFenceFd; 482 } 483 } 484 485 data.acq_fen_fd_cnt = count; 486 fbFd = ctx->dpyAttr[dpy].fd; 487 //Waits for acquire fences, returns a release fence 488 if(LIKELY(!swapzero)) { 489 uint64_t start = systemTime(); 490 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data); 491 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d", 492 __FUNCTION__, (size_t) ns2ms(systemTime() - start)); 493 } 494 495 if(ret < 0) { 496 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s", 497 strerror(errno)); 498 } 499 500 for(uint32_t i = 0; i < list->numHwLayers; i++) { 501 if(list->hwLayers[i].compositionType == HWC_OVERLAY || 502 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 503 //Populate releaseFenceFds. 504 if(UNLIKELY(swapzero)) 505 list->hwLayers[i].releaseFenceFd = -1; 506 else 507 list->hwLayers[i].releaseFenceFd = dup(releaseFd); 508 } 509 } 510 511 if(fd >= 0) { 512 close(fd); 513 fd = -1; 514 } 515 516 if (ctx->mCopyBit[dpy]) 517 ctx->mCopyBit[dpy]->setReleaseFd(releaseFd); 518 if(UNLIKELY(swapzero)){ 519 list->retireFenceFd = -1; 520 close(releaseFd); 521 } else { 522 list->retireFenceFd = releaseFd; 523 } 524 525 return ret; 526} 527 528void LayerCache::resetLayerCache(int num) { 529 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) { 530 hnd[i] = NULL; 531 } 532 numHwLayers = num; 533} 534 535void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) { 536 537 int numFbLayers = 0; 538 int numCacheableLayers = 0; 539 540 canUseLayerCache = false; 541 //Bail if geometry changed or num of layers changed 542 if(list->flags & HWC_GEOMETRY_CHANGED || 543 list->numHwLayers != numHwLayers ) { 544 resetLayerCache(list->numHwLayers); 545 return; 546 } 547 548 for(uint32_t i = 0; i < list->numHwLayers; i++) { 549 //Bail on skip layers 550 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) { 551 resetLayerCache(list->numHwLayers); 552 return; 553 } 554 555 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) { 556 numFbLayers++; 557 if(hnd[i] == NULL) { 558 hnd[i] = list->hwLayers[i].handle; 559 } else if (hnd[i] == 560 list->hwLayers[i].handle) { 561 numCacheableLayers++; 562 } else { 563 hnd[i] = NULL; 564 return; 565 } 566 } else { 567 hnd[i] = NULL; 568 } 569 } 570 if(numFbLayers == numCacheableLayers) 571 canUseLayerCache = true; 572 573 //XXX: The marking part is separate, if MDP comp wants 574 // to use it in the future. Right now getting MDP comp 575 // to use this is more trouble than it is worth. 576 markCachedLayersAsOverlay(list); 577} 578 579void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) { 580 //This optimization only works if ALL the layer handles 581 //that were on the framebuffer didn't change. 582 if(canUseLayerCache){ 583 for(uint32_t i = 0; i < list->numHwLayers; i++) { 584 if (list->hwLayers[i].handle && 585 list->hwLayers[i].handle == hnd[i] && 586 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET) 587 { 588 list->hwLayers[i].compositionType = HWC_OVERLAY; 589 } 590 } 591 } 592 593} 594 595};//namespace 596