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 <overlayRotator.h> 28 #include "hwc_utils.h" 29 #include "hwc_mdpcomp.h" 30 #include "hwc_fbupdate.h" 31 #include "mdp_version.h" 32 #include "hwc_copybit.h" 33 #include "external.h" 34 #include "hwc_qclient.h" 35 #include "QService.h" 36 #include "comptype.h" 37 38 using namespace qClient; 39 using namespace qService; 40 using namespace android; 41 using namespace overlay; 42 using namespace overlay::utils; 43 namespace ovutils = overlay::utils; 44 45 namespace qhwc { 46 47 static int openFramebufferDevice(hwc_context_t *ctx) 48 { 49 struct fb_fix_screeninfo finfo; 50 struct fb_var_screeninfo info; 51 52 int fb_fd = openFb(HWC_DISPLAY_PRIMARY); 53 54 if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &info) == -1) 55 return -errno; 56 57 if (int(info.width) <= 0 || int(info.height) <= 0) { 58 // the driver doesn't return that information 59 // default to 160 dpi 60 info.width = ((info.xres * 25.4f)/160.0f + 0.5f); 61 info.height = ((info.yres * 25.4f)/160.0f + 0.5f); 62 } 63 64 float xdpi = (info.xres * 25.4f) / info.width; 65 float ydpi = (info.yres * 25.4f) / info.height; 66 67 #ifdef MSMFB_METADATA_GET 68 struct msmfb_metadata metadata; 69 memset(&metadata, 0 , sizeof(metadata)); 70 metadata.op = metadata_op_frame_rate; 71 72 if (ioctl(fb_fd, MSMFB_METADATA_GET, &metadata) == -1) { 73 ALOGE("Error retrieving panel frame rate"); 74 return -errno; 75 } 76 77 float fps = metadata.data.panel_frame_rate; 78 #else 79 //XXX: Remove reserved field usage on all baselines 80 //The reserved[3] field is used to store FPS by the driver. 81 float fps = info.reserved[3] & 0xFF; 82 #endif 83 84 if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) == -1) 85 return -errno; 86 87 if (finfo.smem_len <= 0) 88 return -errno; 89 90 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = fb_fd; 91 //xres, yres may not be 32 aligned 92 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = finfo.line_length /(info.xres/8); 93 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = info.xres; 94 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = info.yres; 95 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = xdpi; 96 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ydpi; 97 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period = 1000000000l / fps; 98 99 //Unblank primary on first boot 100 if(ioctl(fb_fd, FBIOBLANK,FB_BLANK_UNBLANK) < 0) { 101 ALOGE("%s: Failed to unblank display", __FUNCTION__); 102 return -errno; 103 } 104 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].isActive = true; 105 106 return 0; 107 } 108 109 static int ppdComm(const char* cmd, hwc_context_t *ctx) { 110 int ret = -1; 111 ret = send(ctx->mCablProp.daemon_socket, cmd, strlen(cmd), MSG_NOSIGNAL); 112 if(ret < 0) { 113 if (errno == EPIPE) { 114 //For broken pipe case, we will close the socket and 115 //re-establish the connection 116 close(ctx->mCablProp.daemon_socket); 117 int daemon_socket = socket_local_client(DAEMON_SOCKET, 118 ANDROID_SOCKET_NAMESPACE_RESERVED, 119 SOCK_STREAM); 120 if(!daemon_socket) { 121 ALOGE("Connecting to socket failed: %s", strerror(errno)); 122 ctx->mCablProp.enabled = false; 123 return -1; 124 } 125 struct timeval timeout; 126 timeout.tv_sec = 1;//wait 1 second before timeout 127 timeout.tv_usec = 0; 128 129 if (setsockopt(daemon_socket, SOL_SOCKET, SO_SNDTIMEO, 130 (char*)&timeout, sizeof(timeout )) < 0) 131 ALOGE("setsockopt failed"); 132 133 ctx->mCablProp.daemon_socket = daemon_socket; 134 //resend the cmd after connection is re-established 135 ret = send(ctx->mCablProp.daemon_socket, cmd, strlen(cmd), 136 MSG_NOSIGNAL); 137 if (ret < 0) { 138 ALOGE("Failed to send data over socket: %s", 139 strerror(errno)); 140 return ret; 141 } 142 } else { 143 ALOGE("Failed to send data over socket: %s", 144 strerror(errno)); 145 return ret; 146 } 147 } 148 ALOGD_IF(HWC_UTILS_DEBUG, "%s: Sent command: %s", __FUNCTION__, cmd); 149 return 0; 150 } 151 152 static void connectPPDaemon(hwc_context_t *ctx) 153 { 154 int ret = -1; 155 char property[PROPERTY_VALUE_MAX]; 156 if ((property_get("ro.qualcomm.cabl", property, NULL) > 0) && 157 (atoi(property) == 1)) { 158 ALOGD("%s: CABL is enabled", __FUNCTION__); 159 ctx->mCablProp.enabled = true; 160 } else { 161 ALOGD("%s: CABL is disabled", __FUNCTION__); 162 ctx->mCablProp.enabled = false; 163 return; 164 } 165 166 if ((property_get("persist.qcom.cabl.video_only", property, NULL) > 0) && 167 (atoi(property) == 1)) { 168 ALOGD("%s: CABL is in video only mode", __FUNCTION__); 169 ctx->mCablProp.videoOnly = true; 170 } else { 171 ctx->mCablProp.videoOnly = false; 172 } 173 174 int daemon_socket = socket_local_client(DAEMON_SOCKET, 175 ANDROID_SOCKET_NAMESPACE_RESERVED, 176 SOCK_STREAM); 177 if(!daemon_socket) { 178 ALOGE("Connecting to socket failed: %s", strerror(errno)); 179 ctx->mCablProp.enabled = false; 180 return; 181 } 182 struct timeval timeout; 183 timeout.tv_sec = 1; //wait 1 second before timeout 184 timeout.tv_usec = 0; 185 186 if (setsockopt(daemon_socket, SOL_SOCKET, SO_SNDTIMEO, 187 (char*)&timeout, sizeof(timeout )) < 0) 188 ALOGE("setsockopt failed"); 189 190 ctx->mCablProp.daemon_socket = daemon_socket; 191 } 192 193 void initContext(hwc_context_t *ctx) 194 { 195 if(openFramebufferDevice(ctx) < 0) { 196 ALOGE("%s: failed to open framebuffer!!", __FUNCTION__); 197 } 198 199 overlay::Overlay::initOverlay(); 200 ctx->mOverlay = overlay::Overlay::getInstance(); 201 ctx->mRotMgr = new RotMgr(); 202 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion(); 203 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay(); 204 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType(); 205 overlay::Overlay::initOverlay(); 206 ctx->mOverlay = overlay::Overlay::getInstance(); 207 ctx->mRotMgr = new RotMgr(); 208 209 //Is created and destroyed only once for primary 210 //For external it could get created and destroyed multiple times depending 211 //on what external we connect to. 212 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] = 213 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres, 214 HWC_DISPLAY_PRIMARY); 215 216 // Check if the target supports copybit compostion (dyn/mdp/c2d) to 217 // decide if we need to open the copybit module. 218 int compositionType = 219 qdutils::QCCompositionType::getInstance().getCompositionType(); 220 221 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN | 222 qdutils::COMPOSITION_TYPE_MDP | 223 qdutils::COMPOSITION_TYPE_C2D)) { 224 ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit(); 225 } 226 227 ctx->mExtDisplay = new ExternalDisplay(ctx); 228 229 for (uint32_t i = 0; i < MAX_DISPLAYS; i++) { 230 ctx->mLayerRotMap[i] = new LayerRotMap(); 231 } 232 233 ctx->mMDPComp[HWC_DISPLAY_PRIMARY] = 234 MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres, 235 HWC_DISPLAY_PRIMARY); 236 237 MDPComp::init(ctx); 238 239 ctx->vstate.enable = false; 240 ctx->vstate.fakevsync = false; 241 ctx->mExtDispConfiguring = false; 242 ctx->mBasePipeSetup = false; 243 244 //Right now hwc starts the service but anybody could do it, or it could be 245 //independent process as well. 246 QService::init(); 247 sp<IQClient> client = new QClient(ctx); 248 interface_cast<IQService>( 249 defaultServiceManager()->getService( 250 String16("display.qservice")))->connect(client); 251 252 ALOGI("Initializing Qualcomm Hardware Composer"); 253 ALOGI("MDP version: %d", ctx->mMDP.version); 254 255 connectPPDaemon(ctx); 256 } 257 258 void closeContext(hwc_context_t *ctx) 259 { 260 if(ctx->mOverlay) { 261 delete ctx->mOverlay; 262 ctx->mOverlay = NULL; 263 } 264 265 if(ctx->mRotMgr) { 266 delete ctx->mRotMgr; 267 ctx->mRotMgr = NULL; 268 } 269 270 for(int i = 0; i < MAX_DISPLAYS; i++) { 271 if(ctx->mCopyBit[i]) { 272 delete ctx->mCopyBit[i]; 273 ctx->mCopyBit[i] = NULL; 274 } 275 } 276 277 if(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd) { 278 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd); 279 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1; 280 } 281 282 if(ctx->mExtDisplay) { 283 delete ctx->mExtDisplay; 284 ctx->mExtDisplay = NULL; 285 } 286 287 for(int i = 0; i < MAX_DISPLAYS; i++) { 288 if(ctx->mFBUpdate[i]) { 289 delete ctx->mFBUpdate[i]; 290 ctx->mFBUpdate[i] = NULL; 291 } 292 if(ctx->mMDPComp[i]) { 293 delete ctx->mMDPComp[i]; 294 ctx->mMDPComp[i] = NULL; 295 } 296 if(ctx->mLayerRotMap[i]) { 297 delete ctx->mLayerRotMap[i]; 298 ctx->mLayerRotMap[i] = NULL; 299 } 300 } 301 } 302 303 304 void dumpsys_log(android::String8& buf, const char* fmt, ...) 305 { 306 va_list varargs; 307 va_start(varargs, fmt); 308 buf.appendFormatV(fmt, varargs); 309 va_end(varargs); 310 } 311 312 /* Calculates the destination position based on the action safe rectangle */ 313 void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x, 314 uint32_t& y, uint32_t& w, uint32_t& h) { 315 316 // if external supports underscan, do nothing 317 // it will be taken care in the driver 318 if(ctx->mExtDisplay->isCEUnderscanSupported()) 319 return; 320 321 float wRatio = 1.0; 322 float hRatio = 1.0; 323 float xRatio = 1.0; 324 float yRatio = 1.0; 325 326 float fbWidth = ctx->dpyAttr[dpy].xres; 327 float fbHeight = ctx->dpyAttr[dpy].yres; 328 329 float asX = 0; 330 float asY = 0; 331 float asW = fbWidth; 332 float asH= fbHeight; 333 char value[PROPERTY_VALUE_MAX]; 334 335 // Apply action safe parameters 336 property_get("hw.actionsafe.width", value, "0"); 337 int asWidthRatio = atoi(value); 338 property_get("hw.actionsafe.height", value, "0"); 339 int asHeightRatio = atoi(value); 340 // based on the action safe ratio, get the Action safe rectangle 341 asW = fbWidth * (1.0f - asWidthRatio / 100.0f); 342 asH = fbHeight * (1.0f - asHeightRatio / 100.0f); 343 asX = (fbWidth - asW) / 2; 344 asY = (fbHeight - asH) / 2; 345 346 // calculate the position ratio 347 xRatio = (float)x/fbWidth; 348 yRatio = (float)y/fbHeight; 349 wRatio = (float)w/fbWidth; 350 hRatio = (float)h/fbHeight; 351 352 //Calculate the position... 353 x = (xRatio * asW) + asX; 354 y = (yRatio * asH) + asY; 355 w = (wRatio * asW); 356 h = (hRatio * asH); 357 358 return; 359 } 360 361 bool needsScaling(hwc_layer_1_t const* layer) { 362 int dst_w, dst_h, src_w, src_h; 363 364 hwc_rect_t displayFrame = layer->displayFrame; 365 hwc_rect_t sourceCrop = layer->sourceCrop; 366 367 dst_w = displayFrame.right - displayFrame.left; 368 dst_h = displayFrame.bottom - displayFrame.top; 369 370 src_w = sourceCrop.right - sourceCrop.left; 371 src_h = sourceCrop.bottom - sourceCrop.top; 372 373 if(((src_w != dst_w) || (src_h != dst_h))) 374 return true; 375 376 return false; 377 } 378 379 bool isAlphaScaled(hwc_layer_1_t const* layer) { 380 if(needsScaling(layer) && isAlphaPresent(layer)) { 381 return true; 382 } 383 return false; 384 } 385 386 bool isAlphaPresent(hwc_layer_1_t const* layer) { 387 private_handle_t *hnd = (private_handle_t *)layer->handle; 388 if(hnd) { 389 int format = hnd->format; 390 switch(format) { 391 case HAL_PIXEL_FORMAT_RGBA_8888: 392 case HAL_PIXEL_FORMAT_BGRA_8888: 393 // In any more formats with Alpha go here.. 394 return true; 395 default : return false; 396 } 397 } 398 return false; 399 } 400 401 // Switch ppd on/off for YUV 402 static void configurePPD(hwc_context_t *ctx, int yuvCount) { 403 if (!ctx->mCablProp.enabled) 404 return; 405 406 if (yuvCount > 0 && !ctx->mCablProp.start) { 407 ctx->mCablProp.start = true; 408 if(ctx->mCablProp.videoOnly) 409 ppdComm("cabl:on", ctx); 410 else 411 ppdComm("cabl:yuv_on", ctx); 412 413 } else if (yuvCount == 0 && ctx->mCablProp.start) { 414 ctx->mCablProp.start = false; 415 if(ctx->mCablProp.videoOnly) 416 ppdComm("cabl:off", ctx); 417 else 418 ppdComm("cabl:yuv_off", ctx); 419 return; 420 } 421 } 422 423 void setListStats(hwc_context_t *ctx, 424 const hwc_display_contents_1_t *list, int dpy) { 425 426 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1; 427 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1; 428 ctx->listStats[dpy].skipCount = 0; 429 ctx->listStats[dpy].needsAlphaScale = false; 430 ctx->listStats[dpy].preMultipliedAlpha = false; 431 ctx->listStats[dpy].planeAlpha = false; 432 ctx->listStats[dpy].yuvCount = 0; 433 434 for (size_t i = 0; i < list->numHwLayers; i++) { 435 hwc_layer_1_t const* layer = &list->hwLayers[i]; 436 private_handle_t *hnd = (private_handle_t *)layer->handle; 437 438 //reset stored yuv index 439 ctx->listStats[dpy].yuvIndices[i] = -1; 440 441 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 442 continue; 443 //We disregard FB being skip for now! so the else if 444 } else if (isSkipLayer(&list->hwLayers[i])) { 445 ctx->listStats[dpy].skipCount++; 446 } else if (UNLIKELY(isYuvBuffer(hnd))) { 447 int& yuvCount = ctx->listStats[dpy].yuvCount; 448 ctx->listStats[dpy].yuvIndices[yuvCount] = i; 449 yuvCount++; 450 451 if(layer->transform & HWC_TRANSFORM_ROT_90) 452 ctx->mNeedsRotator = true; 453 } 454 if(layer->blending == HWC_BLENDING_PREMULT) 455 ctx->listStats[dpy].preMultipliedAlpha = true; 456 if(layer->planeAlpha < 0xFF) 457 ctx->listStats[dpy].planeAlpha = true; 458 if(!ctx->listStats[dpy].needsAlphaScale) 459 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer); 460 } 461 if (dpy == HWC_DISPLAY_PRIMARY) 462 configurePPD(ctx, ctx->listStats[dpy].yuvCount); 463 } 464 465 466 static inline void calc_cut(float& leftCutRatio, float& topCutRatio, 467 float& rightCutRatio, float& bottomCutRatio, int orient) { 468 if(orient & HAL_TRANSFORM_FLIP_H) { 469 swap(leftCutRatio, rightCutRatio); 470 } 471 if(orient & HAL_TRANSFORM_FLIP_V) { 472 swap(topCutRatio, bottomCutRatio); 473 } 474 if(orient & HAL_TRANSFORM_ROT_90) { 475 //Anti clock swapping 476 float tmpCutRatio = leftCutRatio; 477 leftCutRatio = topCutRatio; 478 topCutRatio = rightCutRatio; 479 rightCutRatio = bottomCutRatio; 480 bottomCutRatio = tmpCutRatio; 481 } 482 } 483 484 bool isSecuring(hwc_context_t* ctx, hwc_layer_1_t const* layer) { 485 if((ctx->mMDP.version < qdutils::MDSS_V5) && 486 (ctx->mMDP.version > qdutils::MDP_V3_0) && 487 ctx->mSecuring) { 488 return true; 489 } 490 // On A-Family, Secure policy is applied system wide and not on 491 // buffers. 492 if (isSecureModePolicy(ctx->mMDP.version)) { 493 private_handle_t *hnd = (private_handle_t *)layer->handle; 494 if(ctx->mSecureMode) { 495 if (! isSecureBuffer(hnd)) { 496 // This code path executes for the following usecase: 497 // Some Apps in which first few seconds, framework 498 // sends non-secure buffer and with out destroying 499 // surfaces, switches to secure buffer thereby exposing 500 // vulnerability on A-family devices. Catch this situation 501 // and handle it gracefully by allowing it to be composed by 502 // GPU. 503 ALOGD_IF(HWC_UTILS_DEBUG, "%s: Handle non-secure video layer" 504 "during secure playback gracefully", __FUNCTION__); 505 return true; 506 } 507 } else { 508 if (isSecureBuffer(hnd)) { 509 // This code path executes for the following usecase: 510 // For some Apps, when User terminates playback, Framework 511 // doesnt destroy video surface and video surface still 512 // comes to Display HAL. This exposes vulnerability on 513 // A-family. Catch this situation and handle it gracefully 514 // by allowing it to be composed by GPU. 515 ALOGD_IF(HWC_UTILS_DEBUG, "%s: Handle secure video layer" 516 "during non-secure playback gracefully", __FUNCTION__); 517 return true; 518 } 519 } 520 } 521 return false; 522 } 523 524 bool isSecureModePolicy(int mdpVersion) { 525 if (mdpVersion < qdutils::MDSS_V5) 526 return true; 527 else 528 return false; 529 } 530 531 int getBlending(int blending) { 532 switch(blending) { 533 case HWC_BLENDING_NONE: 534 return overlay::utils::OVERLAY_BLENDING_OPAQUE; 535 case HWC_BLENDING_PREMULT: 536 return overlay::utils::OVERLAY_BLENDING_PREMULT; 537 case HWC_BLENDING_COVERAGE : 538 default: 539 return overlay::utils::OVERLAY_BLENDING_COVERAGE; 540 } 541 } 542 543 //Crops source buffer against destination and FB boundaries 544 void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst, 545 const hwc_rect_t& scissor, int orient) { 546 547 int& crop_l = crop.left; 548 int& crop_t = crop.top; 549 int& crop_r = crop.right; 550 int& crop_b = crop.bottom; 551 int crop_w = crop.right - crop.left; 552 int crop_h = crop.bottom - crop.top; 553 554 int& dst_l = dst.left; 555 int& dst_t = dst.top; 556 int& dst_r = dst.right; 557 int& dst_b = dst.bottom; 558 int dst_w = abs(dst.right - dst.left); 559 int dst_h = abs(dst.bottom - dst.top); 560 561 const int& sci_l = scissor.left; 562 const int& sci_t = scissor.top; 563 const int& sci_r = scissor.right; 564 const int& sci_b = scissor.bottom; 565 int sci_w = abs(sci_r - sci_l); 566 int sci_h = abs(sci_b - sci_t); 567 568 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f, 569 bottomCutRatio = 0.0f; 570 571 if(dst_l < sci_l) { 572 leftCutRatio = (float)(sci_l - dst_l) / (float)dst_w; 573 dst_l = sci_l; 574 } 575 576 if(dst_r > sci_r) { 577 rightCutRatio = (float)(dst_r - sci_r) / (float)dst_w; 578 dst_r = sci_r; 579 } 580 581 if(dst_t < sci_t) { 582 topCutRatio = (float)(sci_t - dst_t) / (float)dst_h; 583 dst_t = sci_t; 584 } 585 586 if(dst_b > sci_b) { 587 bottomCutRatio = (float)(dst_b - sci_b) / (float)dst_h; 588 dst_b = sci_b; 589 } 590 591 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient); 592 crop_l += crop_w * leftCutRatio; 593 crop_t += crop_h * topCutRatio; 594 crop_r -= crop_w * rightCutRatio; 595 crop_b -= crop_h * bottomCutRatio; 596 } 597 598 void getNonWormholeRegion(hwc_display_contents_1_t* list, 599 hwc_rect_t& nwr) 600 { 601 uint32_t last = list->numHwLayers - 1; 602 hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame; 603 //Initiliaze nwr to first frame 604 nwr.left = list->hwLayers[0].displayFrame.left; 605 nwr.top = list->hwLayers[0].displayFrame.top; 606 nwr.right = list->hwLayers[0].displayFrame.right; 607 nwr.bottom = list->hwLayers[0].displayFrame.bottom; 608 609 for (uint32_t i = 1; i < last; i++) { 610 hwc_rect_t displayFrame = list->hwLayers[i].displayFrame; 611 nwr.left = min(nwr.left, displayFrame.left); 612 nwr.top = min(nwr.top, displayFrame.top); 613 nwr.right = max(nwr.right, displayFrame.right); 614 nwr.bottom = max(nwr.bottom, displayFrame.bottom); 615 } 616 617 //Intersect with the framebuffer 618 nwr.left = max(nwr.left, fbDisplayFrame.left); 619 nwr.top = max(nwr.top, fbDisplayFrame.top); 620 nwr.right = min(nwr.right, fbDisplayFrame.right); 621 nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom); 622 623 } 624 625 bool isExternalActive(hwc_context_t* ctx) { 626 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive; 627 } 628 629 void closeAcquireFds(hwc_display_contents_1_t* list) { 630 if(LIKELY(list)) { 631 for(uint32_t i = 0; i < list->numHwLayers; i++) { 632 //Close the acquireFenceFds 633 //HWC_FRAMEBUFFER are -1 already by SF, rest we close. 634 if(list->hwLayers[i].acquireFenceFd >= 0) { 635 close(list->hwLayers[i].acquireFenceFd); 636 list->hwLayers[i].acquireFenceFd = -1; 637 } 638 } 639 } 640 } 641 642 int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy, 643 int fd) { 644 int ret = 0; 645 646 int acquireFd[MAX_NUM_LAYERS]; 647 int count = 0; 648 int releaseFd = -1; 649 int retireFd = -1; 650 int fbFd = -1; 651 int rotFd = -1; 652 bool swapzero = false; 653 int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion(); 654 655 struct mdp_buf_sync data; 656 memset(&data, 0, sizeof(data)); 657 //Until B-family supports sync for rotator 658 if(mdpVersion >= qdutils::MDSS_V5) { 659 data.flags = MDP_BUF_SYNC_FLAG_WAIT; 660 } 661 data.acq_fen_fd = acquireFd; 662 data.rel_fen_fd = &releaseFd; 663 data.retire_fen_fd = &retireFd; 664 665 char property[PROPERTY_VALUE_MAX]; 666 if(property_get("debug.egl.swapinterval", property, "1") > 0) { 667 if(atoi(property) == 0) 668 swapzero = true; 669 } 670 671 #ifndef MDSS_TARGET 672 //Send acquireFenceFds to rotator 673 if(mdpVersion < qdutils::MDSS_V5) { 674 //A-family 675 int rotFd = ctx->mRotMgr->getRotDevFd(); 676 struct msm_rotator_buf_sync rotData; 677 678 for(uint32_t i = 0; i < ctx->mLayerRotMap[dpy]->getCount(); i++) { 679 memset(&rotData, 0, sizeof(rotData)); 680 int& acquireFenceFd = 681 ctx->mLayerRotMap[dpy]->getLayer(i)->acquireFenceFd; 682 rotData.acq_fen_fd = acquireFenceFd; 683 rotData.session_id = ctx->mLayerRotMap[dpy]->getRot(i)->getSessId(); 684 ioctl(rotFd, MSM_ROTATOR_IOCTL_BUFFER_SYNC, &rotData); 685 close(acquireFenceFd); 686 //For MDP to wait on. 687 acquireFenceFd = dup(rotData.rel_fen_fd); 688 //A buffer is free to be used by producer as soon as its copied to 689 //rotator. 690 ctx->mLayerRotMap[dpy]->getLayer(i)->releaseFenceFd = 691 rotData.rel_fen_fd; 692 } 693 } else { 694 //TODO B-family 695 } 696 697 #endif 698 //Accumulate acquireFenceFds for MDP 699 for(uint32_t i = 0; i < list->numHwLayers; i++) { 700 if(list->hwLayers[i].compositionType == HWC_OVERLAY && 701 list->hwLayers[i].acquireFenceFd >= 0) { 702 if(UNLIKELY(swapzero)) 703 acquireFd[count++] = -1; 704 else 705 acquireFd[count++] = list->hwLayers[i].acquireFenceFd; 706 } 707 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 708 if(UNLIKELY(swapzero)) 709 acquireFd[count++] = -1; 710 else if(fd >= 0) { 711 //set the acquireFD from fd - which is coming from c2d 712 acquireFd[count++] = fd; 713 // Buffer sync IOCTL should be async when using c2d fence is 714 // used 715 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT; 716 } else if(list->hwLayers[i].acquireFenceFd >= 0) 717 acquireFd[count++] = list->hwLayers[i].acquireFenceFd; 718 } 719 } 720 721 data.acq_fen_fd_cnt = count; 722 fbFd = ctx->dpyAttr[dpy].fd; 723 724 //Waits for acquire fences, returns a release fence 725 if(LIKELY(!swapzero)) { 726 uint64_t start = systemTime(); 727 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data); 728 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d", 729 __FUNCTION__, (size_t) ns2ms(systemTime() - start)); 730 } 731 732 if(ret < 0) { 733 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s", 734 strerror(errno)); 735 } 736 737 for(uint32_t i = 0; i < list->numHwLayers; i++) { 738 if(list->hwLayers[i].compositionType == HWC_OVERLAY || 739 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { 740 //Populate releaseFenceFds. 741 if(UNLIKELY(swapzero)) { 742 list->hwLayers[i].releaseFenceFd = -1; 743 } else if(list->hwLayers[i].releaseFenceFd < 0) { 744 //If rotator has not already populated this field. 745 list->hwLayers[i].releaseFenceFd = dup(releaseFd); 746 } 747 } 748 } 749 750 if(fd >= 0) { 751 close(fd); 752 fd = -1; 753 } 754 755 if (ctx->mCopyBit[dpy]) 756 ctx->mCopyBit[dpy]->setReleaseFd(releaseFd); 757 758 //A-family 759 if(mdpVersion < qdutils::MDSS_V5) { 760 //Signals when MDP finishes reading rotator buffers. 761 ctx->mLayerRotMap[dpy]->setReleaseFd(releaseFd); 762 } 763 close(releaseFd); 764 if(UNLIKELY(swapzero)) 765 list->retireFenceFd = -1; 766 else 767 list->retireFenceFd = retireFd; 768 return ret; 769 } 770 771 void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform, 772 hwc_rect_t& crop, hwc_rect_t& dst) { 773 int hw_w = ctx->dpyAttr[dpy].xres; 774 int hw_h = ctx->dpyAttr[dpy].yres; 775 if(dst.left < 0 || dst.top < 0 || 776 dst.right > hw_w || dst.bottom > hw_h) { 777 hwc_rect_t scissor = {0, 0, hw_w, hw_h }; 778 qhwc::calculate_crop_rects(crop, dst, scissor, transform); 779 } 780 } 781 782 void setMdpFlags(hwc_layer_1_t *layer, 783 ovutils::eMdpFlags &mdpFlags, 784 int rotDownscale) { 785 private_handle_t *hnd = (private_handle_t *)layer->handle; 786 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; 787 const int& transform = layer->transform; 788 789 if(layer->blending == HWC_BLENDING_PREMULT) { 790 ovutils::setMdpFlags(mdpFlags, 791 ovutils::OV_MDP_BLEND_FG_PREMULT); 792 } 793 794 if(isYuvBuffer(hnd)) { 795 if(isSecureBuffer(hnd)) { 796 ovutils::setMdpFlags(mdpFlags, 797 ovutils::OV_MDP_SECURE_OVERLAY_SESSION); 798 } 799 if(metadata && (metadata->operation & PP_PARAM_INTERLACED) && 800 metadata->interlaced) { 801 ovutils::setMdpFlags(mdpFlags, 802 ovutils::OV_MDP_DEINTERLACE); 803 } 804 //Pre-rotation will be used using rotator. 805 if(transform & HWC_TRANSFORM_ROT_90) { 806 ovutils::setMdpFlags(mdpFlags, 807 ovutils::OV_MDP_SOURCE_ROTATED_90); 808 } 809 } 810 811 //No 90 component and no rot-downscale then flips done by MDP 812 //If we use rot then it might as well do flips 813 if(!(layer->transform & HWC_TRANSFORM_ROT_90) && !rotDownscale) { 814 if(layer->transform & HWC_TRANSFORM_FLIP_H) { 815 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H); 816 } 817 818 if(layer->transform & HWC_TRANSFORM_FLIP_V) { 819 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_V); 820 } 821 } 822 823 if(metadata && 824 ((metadata->operation & PP_PARAM_HSIC) 825 || (metadata->operation & PP_PARAM_IGC) 826 || (metadata->operation & PP_PARAM_SHARP2))) { 827 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_PP_EN); 828 } 829 } 830 831 static inline int configRotator(Rotator *rot, const Whf& whf, 832 const eMdpFlags& mdpFlags, const eTransform& orient, 833 const int& downscale) { 834 rot->setSource(whf); 835 rot->setFlags(mdpFlags); 836 rot->setTransform(orient); 837 rot->setDownscale(downscale); 838 if(!rot->commit()) return -1; 839 return 0; 840 } 841 842 /* 843 * Sets up BORDERFILL as default base pipe and detaches RGB0. 844 * Framebuffer is always updated using PLAY ioctl. 845 */ 846 bool setupBasePipe(hwc_context_t *ctx) { 847 const int dpy = HWC_DISPLAY_PRIMARY; 848 int fb_stride = ctx->dpyAttr[dpy].stride; 849 int fb_width = ctx->dpyAttr[dpy].xres; 850 int fb_height = ctx->dpyAttr[dpy].yres; 851 int fb_fd = ctx->dpyAttr[dpy].fd; 852 853 mdp_overlay ovInfo; 854 msmfb_overlay_data ovData; 855 memset(&ovInfo, 0, sizeof(mdp_overlay)); 856 memset(&ovData, 0, sizeof(msmfb_overlay_data)); 857 858 ovInfo.src.format = MDP_RGB_BORDERFILL; 859 ovInfo.src.width = fb_width; 860 ovInfo.src.height = fb_height; 861 ovInfo.src_rect.w = fb_width; 862 ovInfo.src_rect.h = fb_height; 863 ovInfo.dst_rect.w = fb_width; 864 ovInfo.dst_rect.h = fb_height; 865 ovInfo.id = MSMFB_NEW_REQUEST; 866 867 if (ioctl(fb_fd, MSMFB_OVERLAY_SET, &ovInfo) < 0) { 868 ALOGE("Failed to call ioctl MSMFB_OVERLAY_SET err=%s", 869 strerror(errno)); 870 return false; 871 } 872 873 ovData.id = ovInfo.id; 874 if (ioctl(fb_fd, MSMFB_OVERLAY_PLAY, &ovData) < 0) { 875 ALOGE("Failed to call ioctl MSMFB_OVERLAY_PLAY err=%s", 876 strerror(errno)); 877 return false; 878 } 879 ctx->mBasePipeSetup = true; 880 return true; 881 } 882 883 884 static inline int configMdp(Overlay *ov, const PipeArgs& parg, 885 const eTransform& orient, const hwc_rect_t& crop, 886 const hwc_rect_t& pos, const MetaData_t *metadata, 887 const eDest& dest) { 888 ov->setSource(parg, dest); 889 ov->setTransform(orient, dest); 890 891 int crop_w = crop.right - crop.left; 892 int crop_h = crop.bottom - crop.top; 893 Dim dcrop(crop.left, crop.top, crop_w, crop_h); 894 ov->setCrop(dcrop, dest); 895 896 int posW = pos.right - pos.left; 897 int posH = pos.bottom - pos.top; 898 Dim position(pos.left, pos.top, posW, posH); 899 ov->setPosition(position, dest); 900 901 if (metadata) 902 ov->setVisualParams(*metadata, dest); 903 904 if (!ov->commit(dest)) { 905 return -1; 906 } 907 return 0; 908 } 909 910 static inline void updateSource(eTransform& orient, Whf& whf, 911 hwc_rect_t& crop) { 912 Dim srcCrop(crop.left, crop.top, 913 crop.right - crop.left, 914 crop.bottom - crop.top); 915 //getMdpOrient will switch the flips if the source is 90 rotated. 916 //Clients in Android dont factor in 90 rotation while deciding the flip. 917 orient = static_cast<eTransform>(ovutils::getMdpOrient(orient)); 918 preRotateSource(orient, whf, srcCrop); 919 crop.left = srcCrop.x; 920 crop.top = srcCrop.y; 921 crop.right = srcCrop.x + srcCrop.w; 922 crop.bottom = srcCrop.y + srcCrop.h; 923 } 924 925 int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer, 926 const int& dpy, eMdpFlags& mdpFlags, const eZorder& z, 927 const eIsFg& isFg, const eDest& dest, Rotator **rot) { 928 929 private_handle_t *hnd = (private_handle_t *)layer->handle; 930 if(!hnd) { 931 ALOGE("%s: layer handle is NULL", __FUNCTION__); 932 return -1; 933 } 934 935 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; 936 937 hwc_rect_t crop = layer->sourceCrop; 938 hwc_rect_t dst = layer->displayFrame; 939 int transform = layer->transform; 940 eTransform orient = static_cast<eTransform>(transform); 941 int downscale = 0; 942 int rotFlags = ovutils::ROT_FLAGS_NONE; 943 Whf whf(getWidth(hnd), getHeight(hnd), 944 getMdpFormat(hnd->format), hnd->size); 945 946 if(isYuvBuffer(hnd) && ctx->mMDP.version >= qdutils::MDP_V4_2 && 947 ctx->mMDP.version < qdutils::MDSS_V5) { 948 downscale = getDownscaleFactor( 949 crop.right - crop.left, 950 crop.bottom - crop.top, 951 dst.right - dst.left, 952 dst.bottom - dst.top); 953 if(downscale) { 954 rotFlags = ROT_DOWNSCALE_ENABLED; 955 } 956 } 957 958 setMdpFlags(layer, mdpFlags, downscale); 959 trimLayer(ctx, dpy, transform, crop, dst); 960 961 if(isYuvBuffer(hnd) && //if 90 component or downscale, use rot 962 ((transform & HWC_TRANSFORM_ROT_90) || downscale)) { 963 *rot = ctx->mRotMgr->getNext(); 964 if(*rot == NULL) return -1; 965 //Configure rotator for pre-rotation 966 if(configRotator(*rot, whf, mdpFlags, orient, downscale) < 0) 967 return -1; 968 ctx->mLayerRotMap[dpy]->add(layer, *rot); 969 whf.format = (*rot)->getDstFormat(); 970 updateSource(orient, whf, crop); 971 rotFlags |= ovutils::ROT_PREROTATED; 972 } 973 974 //For the mdp, since either we are pre-rotating or MDP does flips 975 orient = OVERLAY_TRANSFORM_0; 976 transform = 0; 977 978 PipeArgs parg(mdpFlags, whf, z, isFg, 979 static_cast<eRotFlags>(rotFlags), layer->planeAlpha, 980 (ovutils::eBlending) getBlending(layer->blending)); 981 982 if(configMdp(ctx->mOverlay, parg, orient, crop, dst, metadata, dest) < 0) { 983 ALOGE("%s: commit failed for low res panel", __FUNCTION__); 984 ctx->mLayerRotMap[dpy]->reset(); 985 return -1; 986 } 987 return 0; 988 } 989 990 int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer, 991 const int& dpy, eMdpFlags& mdpFlagsL, const eZorder& z, 992 const eIsFg& isFg, const eDest& lDest, const eDest& rDest, 993 Rotator **rot) { 994 private_handle_t *hnd = (private_handle_t *)layer->handle; 995 if(!hnd) { 996 ALOGE("%s: layer handle is NULL", __FUNCTION__); 997 return -1; 998 } 999 1000 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; 1001 1002 int hw_w = ctx->dpyAttr[dpy].xres; 1003 int hw_h = ctx->dpyAttr[dpy].yres; 1004 hwc_rect_t crop = layer->sourceCrop; 1005 hwc_rect_t dst = layer->displayFrame; 1006 int transform = layer->transform; 1007 eTransform orient = static_cast<eTransform>(transform); 1008 const int downscale = 0; 1009 int rotFlags = ROT_FLAGS_NONE; 1010 1011 Whf whf(getWidth(hnd), getHeight(hnd), 1012 getMdpFormat(hnd->format), hnd->size); 1013 1014 setMdpFlags(layer, mdpFlagsL); 1015 trimLayer(ctx, dpy, transform, crop, dst); 1016 1017 if(isYuvBuffer(hnd) && (transform & HWC_TRANSFORM_ROT_90)) { 1018 (*rot) = ctx->mRotMgr->getNext(); 1019 if((*rot) == NULL) return -1; 1020 //Configure rotator for pre-rotation 1021 if(configRotator(*rot, whf, mdpFlagsL, orient, downscale) < 0) 1022 return -1; 1023 ctx->mLayerRotMap[dpy]->add(layer, *rot); 1024 whf.format = (*rot)->getDstFormat(); 1025 updateSource(orient, whf, crop); 1026 rotFlags |= ROT_PREROTATED; 1027 } 1028 1029 eMdpFlags mdpFlagsR = mdpFlagsL; 1030 setMdpFlags(mdpFlagsR, OV_MDSS_MDP_RIGHT_MIXER); 1031 1032 hwc_rect_t tmp_cropL, tmp_dstL; 1033 hwc_rect_t tmp_cropR, tmp_dstR; 1034 1035 if(lDest != OV_INVALID) { 1036 tmp_cropL = crop; 1037 tmp_dstL = dst; 1038 hwc_rect_t scissor = {0, 0, hw_w/2, hw_h }; 1039 qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0); 1040 } 1041 if(rDest != OV_INVALID) { 1042 tmp_cropR = crop; 1043 tmp_dstR = dst; 1044 hwc_rect_t scissor = {hw_w/2, 0, hw_w, hw_h }; 1045 qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0); 1046 } 1047 1048 //When buffer is flipped, contents of mixer config also needs to swapped. 1049 //Not needed if the layer is confined to one half of the screen. 1050 //If rotator has been used then it has also done the flips, so ignore them. 1051 if((orient & OVERLAY_TRANSFORM_FLIP_V) && lDest != OV_INVALID 1052 && rDest != OV_INVALID && rot == NULL) { 1053 hwc_rect_t new_cropR; 1054 new_cropR.left = tmp_cropL.left; 1055 new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left); 1056 1057 hwc_rect_t new_cropL; 1058 new_cropL.left = new_cropR.right; 1059 new_cropL.right = tmp_cropR.right; 1060 1061 tmp_cropL.left = new_cropL.left; 1062 tmp_cropL.right = new_cropL.right; 1063 1064 tmp_cropR.left = new_cropR.left; 1065 tmp_cropR.right = new_cropR.right; 1066 1067 } 1068 1069 //For the mdp, since either we are pre-rotating or MDP does flips 1070 orient = OVERLAY_TRANSFORM_0; 1071 transform = 0; 1072 1073 //configure left mixer 1074 if(lDest != OV_INVALID) { 1075 PipeArgs pargL(mdpFlagsL, whf, z, isFg, 1076 static_cast<eRotFlags>(rotFlags), layer->planeAlpha, 1077 (ovutils::eBlending) getBlending(layer->blending)); 1078 1079 if(configMdp(ctx->mOverlay, pargL, orient, 1080 tmp_cropL, tmp_dstL, metadata, lDest) < 0) { 1081 ALOGE("%s: commit failed for left mixer config", __FUNCTION__); 1082 return -1; 1083 } 1084 } 1085 1086 //configure right mixer 1087 if(rDest != OV_INVALID) { 1088 PipeArgs pargR(mdpFlagsR, whf, z, isFg, 1089 static_cast<eRotFlags>(rotFlags), layer->planeAlpha, 1090 (ovutils::eBlending) getBlending(layer->blending)); 1091 1092 tmp_dstR.right = tmp_dstR.right - tmp_dstR.left; 1093 tmp_dstR.left = 0; 1094 if(configMdp(ctx->mOverlay, pargR, orient, 1095 tmp_cropR, tmp_dstR, metadata, rDest) < 0) { 1096 ALOGE("%s: commit failed for right mixer config", __FUNCTION__); 1097 return -1; 1098 } 1099 } 1100 1101 return 0; 1102 } 1103 1104 void LayerRotMap::add(hwc_layer_1_t* layer, Rotator *rot) { 1105 if(mCount >= MAX_SESS) return; 1106 mLayer[mCount] = layer; 1107 mRot[mCount] = rot; 1108 mCount++; 1109 } 1110 1111 void LayerRotMap::reset() { 1112 for (int i = 0; i < MAX_SESS; i++) { 1113 mLayer[i] = 0; 1114 mRot[i] = 0; 1115 } 1116 mCount = 0; 1117 } 1118 1119 void LayerRotMap::setReleaseFd(const int& fence) { 1120 for(uint32_t i = 0; i < mCount; i++) { 1121 mRot[i]->setReleaseFd(dup(fence)); 1122 } 1123 } 1124 1125 };//namespace qhwc 1126