1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS 18 19 #include <stdint.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 #include <sys/types.h> 24 #include <math.h> 25 26 #include <utils/CallStack.h> 27 #include <utils/Errors.h> 28 #include <utils/misc.h> 29 #include <utils/String8.h> 30 #include <utils/Thread.h> 31 #include <utils/Trace.h> 32 #include <utils/Vector.h> 33 34 #include <ui/GraphicBuffer.h> 35 36 #include <hardware/hardware.h> 37 #include <hardware/hwcomposer.h> 38 39 #include <android/configuration.h> 40 41 #include <cutils/log.h> 42 #include <cutils/properties.h> 43 44 #include "HWComposer.h" 45 46 #include "../Layer.h" // needed only for debugging 47 #include "../SurfaceFlinger.h" 48 49 namespace android { 50 51 #define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION 52 53 static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) { 54 uint32_t hwcVersion = hwc->common.version; 55 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK; 56 } 57 58 static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) { 59 uint32_t hwcVersion = hwc->common.version; 60 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK; 61 } 62 63 static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc, 64 uint32_t version) { 65 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK); 66 } 67 68 // --------------------------------------------------------------------------- 69 70 struct HWComposer::cb_context { 71 struct callbacks : public hwc_procs_t { 72 // these are here to facilitate the transition when adding 73 // new callbacks (an implementation can check for NULL before 74 // calling a new callback). 75 void (*zero[4])(void); 76 }; 77 callbacks procs; 78 HWComposer* hwc; 79 }; 80 81 // --------------------------------------------------------------------------- 82 83 HWComposer::HWComposer( 84 const sp<SurfaceFlinger>& flinger, 85 EventHandler& handler) 86 : mFlinger(flinger), 87 mFbDev(0), mHwc(0), mNumDisplays(1), 88 mCBContext(new cb_context), 89 mEventHandler(handler), 90 mDebugForceFakeVSync(false) 91 { 92 for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) { 93 mLists[i] = 0; 94 } 95 96 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) { 97 mLastHwVSync[i] = 0; 98 mVSyncCounts[i] = 0; 99 } 100 101 char value[PROPERTY_VALUE_MAX]; 102 property_get("debug.sf.no_hw_vsync", value, "0"); 103 mDebugForceFakeVSync = atoi(value); 104 105 bool needVSyncThread = true; 106 107 // Note: some devices may insist that the FB HAL be opened before HWC. 108 int fberr = loadFbHalModule(); 109 loadHwcModule(); 110 111 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 112 // close FB HAL if we don't needed it. 113 // FIXME: this is temporary until we're not forced to open FB HAL 114 // before HWC. 115 framebuffer_close(mFbDev); 116 mFbDev = NULL; 117 } 118 119 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory. 120 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) 121 && !mFbDev) { 122 ALOGE("ERROR: failed to open framebuffer (%s), aborting", 123 strerror(-fberr)); 124 abort(); 125 } 126 127 // these display IDs are always reserved 128 for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) { 129 mAllocatedDisplayIDs.markBit(i); 130 } 131 132 if (mHwc) { 133 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER, 134 (hwcApiVersion(mHwc) >> 24) & 0xff, 135 (hwcApiVersion(mHwc) >> 16) & 0xff); 136 if (mHwc->registerProcs) { 137 mCBContext->hwc = this; 138 mCBContext->procs.invalidate = &hook_invalidate; 139 mCBContext->procs.vsync = &hook_vsync; 140 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) 141 mCBContext->procs.hotplug = &hook_hotplug; 142 else 143 mCBContext->procs.hotplug = NULL; 144 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero)); 145 mHwc->registerProcs(mHwc, &mCBContext->procs); 146 } 147 148 // don't need a vsync thread if we have a hardware composer 149 needVSyncThread = false; 150 // always turn vsync off when we start 151 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0); 152 153 // the number of displays we actually have depends on the 154 // hw composer version 155 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 156 // 1.3 adds support for virtual displays 157 mNumDisplays = MAX_HWC_DISPLAYS; 158 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 159 // 1.1 adds support for multiple displays 160 mNumDisplays = NUM_BUILTIN_DISPLAYS; 161 } else { 162 mNumDisplays = 1; 163 } 164 } 165 166 if (mFbDev) { 167 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)), 168 "should only have fbdev if no hwc or hwc is 1.0"); 169 170 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]); 171 disp.connected = true; 172 disp.width = mFbDev->width; 173 disp.height = mFbDev->height; 174 disp.format = mFbDev->format; 175 disp.xdpi = mFbDev->xdpi; 176 disp.ydpi = mFbDev->ydpi; 177 if (disp.refresh == 0) { 178 disp.refresh = nsecs_t(1e9 / mFbDev->fps); 179 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh); 180 } 181 if (disp.refresh == 0) { 182 disp.refresh = nsecs_t(1e9 / 60.0); 183 ALOGW("getting VSYNC period from thin air: %lld", 184 mDisplayData[HWC_DISPLAY_PRIMARY].refresh); 185 } 186 } else if (mHwc) { 187 // here we're guaranteed to have at least HWC 1.1 188 for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) { 189 queryDisplayProperties(i); 190 } 191 } 192 193 if (needVSyncThread) { 194 // we don't have VSYNC support, we need to fake it 195 mVSyncThread = new VSyncThread(*this); 196 } 197 } 198 199 HWComposer::~HWComposer() { 200 if (mHwc) { 201 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0); 202 } 203 if (mVSyncThread != NULL) { 204 mVSyncThread->requestExitAndWait(); 205 } 206 if (mHwc) { 207 hwc_close_1(mHwc); 208 } 209 if (mFbDev) { 210 framebuffer_close(mFbDev); 211 } 212 delete mCBContext; 213 } 214 215 // Load and prepare the hardware composer module. Sets mHwc. 216 void HWComposer::loadHwcModule() 217 { 218 hw_module_t const* module; 219 220 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) { 221 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID); 222 return; 223 } 224 225 int err = hwc_open_1(module, &mHwc); 226 if (err) { 227 ALOGE("%s device failed to initialize (%s)", 228 HWC_HARDWARE_COMPOSER, strerror(-err)); 229 return; 230 } 231 232 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) || 233 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION || 234 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) { 235 ALOGE("%s device version %#x unsupported, will not be used", 236 HWC_HARDWARE_COMPOSER, mHwc->common.version); 237 hwc_close_1(mHwc); 238 mHwc = NULL; 239 return; 240 } 241 } 242 243 // Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev. 244 int HWComposer::loadFbHalModule() 245 { 246 hw_module_t const* module; 247 248 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); 249 if (err != 0) { 250 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID); 251 return err; 252 } 253 254 return framebuffer_open(module, &mFbDev); 255 } 256 257 status_t HWComposer::initCheck() const { 258 return mHwc ? NO_ERROR : NO_INIT; 259 } 260 261 void HWComposer::hook_invalidate(const struct hwc_procs* procs) { 262 cb_context* ctx = reinterpret_cast<cb_context*>( 263 const_cast<hwc_procs_t*>(procs)); 264 ctx->hwc->invalidate(); 265 } 266 267 void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp, 268 int64_t timestamp) { 269 cb_context* ctx = reinterpret_cast<cb_context*>( 270 const_cast<hwc_procs_t*>(procs)); 271 ctx->hwc->vsync(disp, timestamp); 272 } 273 274 void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp, 275 int connected) { 276 cb_context* ctx = reinterpret_cast<cb_context*>( 277 const_cast<hwc_procs_t*>(procs)); 278 ctx->hwc->hotplug(disp, connected); 279 } 280 281 void HWComposer::invalidate() { 282 mFlinger->repaintEverything(); 283 } 284 285 void HWComposer::vsync(int disp, int64_t timestamp) { 286 if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) { 287 { 288 Mutex::Autolock _l(mLock); 289 290 // There have been reports of HWCs that signal several vsync events 291 // with the same timestamp when turning the display off and on. This 292 // is a bug in the HWC implementation, but filter the extra events 293 // out here so they don't cause havoc downstream. 294 if (timestamp == mLastHwVSync[disp]) { 295 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%lld)", 296 timestamp); 297 return; 298 } 299 300 mLastHwVSync[disp] = timestamp; 301 } 302 303 char tag[16]; 304 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp); 305 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1); 306 307 mEventHandler.onVSyncReceived(disp, timestamp); 308 } 309 } 310 311 void HWComposer::hotplug(int disp, int connected) { 312 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) { 313 ALOGE("hotplug event received for invalid display: disp=%d connected=%d", 314 disp, connected); 315 return; 316 } 317 queryDisplayProperties(disp); 318 mEventHandler.onHotplugReceived(disp, bool(connected)); 319 } 320 321 static float getDefaultDensity(uint32_t height) { 322 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH; 323 else return ACONFIGURATION_DENSITY_TV; 324 } 325 326 static const uint32_t DISPLAY_ATTRIBUTES[] = { 327 HWC_DISPLAY_VSYNC_PERIOD, 328 HWC_DISPLAY_WIDTH, 329 HWC_DISPLAY_HEIGHT, 330 HWC_DISPLAY_DPI_X, 331 HWC_DISPLAY_DPI_Y, 332 HWC_DISPLAY_NO_ATTRIBUTE, 333 }; 334 #define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0]) 335 336 status_t HWComposer::queryDisplayProperties(int disp) { 337 338 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)); 339 340 // use zero as default value for unspecified attributes 341 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1]; 342 memset(values, 0, sizeof(values)); 343 344 uint32_t config; 345 size_t numConfigs = 1; 346 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs); 347 if (err != NO_ERROR) { 348 // this can happen if an unpluggable display is not connected 349 mDisplayData[disp].connected = false; 350 return err; 351 } 352 353 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values); 354 if (err != NO_ERROR) { 355 // we can't get this display's info. turn it off. 356 mDisplayData[disp].connected = false; 357 return err; 358 } 359 360 int32_t w = 0, h = 0; 361 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) { 362 switch (DISPLAY_ATTRIBUTES[i]) { 363 case HWC_DISPLAY_VSYNC_PERIOD: 364 mDisplayData[disp].refresh = nsecs_t(values[i]); 365 break; 366 case HWC_DISPLAY_WIDTH: 367 mDisplayData[disp].width = values[i]; 368 break; 369 case HWC_DISPLAY_HEIGHT: 370 mDisplayData[disp].height = values[i]; 371 break; 372 case HWC_DISPLAY_DPI_X: 373 mDisplayData[disp].xdpi = values[i] / 1000.0f; 374 break; 375 case HWC_DISPLAY_DPI_Y: 376 mDisplayData[disp].ydpi = values[i] / 1000.0f; 377 break; 378 default: 379 ALOG_ASSERT(false, "unknown display attribute[%d] %#x", 380 i, DISPLAY_ATTRIBUTES[i]); 381 break; 382 } 383 } 384 385 // FIXME: what should we set the format to? 386 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888; 387 mDisplayData[disp].connected = true; 388 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) { 389 float dpi = getDefaultDensity(h); 390 mDisplayData[disp].xdpi = dpi; 391 mDisplayData[disp].ydpi = dpi; 392 } 393 return NO_ERROR; 394 } 395 396 status_t HWComposer::setVirtualDisplayProperties(int32_t id, 397 uint32_t w, uint32_t h, uint32_t format) { 398 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) || 399 !mAllocatedDisplayIDs.hasBit(id)) { 400 return BAD_INDEX; 401 } 402 mDisplayData[id].width = w; 403 mDisplayData[id].height = h; 404 mDisplayData[id].format = format; 405 mDisplayData[id].xdpi = mDisplayData[id].ydpi = getDefaultDensity(h); 406 return NO_ERROR; 407 } 408 409 int32_t HWComposer::allocateDisplayId() { 410 if (mAllocatedDisplayIDs.count() >= mNumDisplays) { 411 return NO_MEMORY; 412 } 413 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit(); 414 mAllocatedDisplayIDs.markBit(id); 415 mDisplayData[id].connected = true; 416 return id; 417 } 418 419 status_t HWComposer::freeDisplayId(int32_t id) { 420 if (id < NUM_BUILTIN_DISPLAYS) { 421 // cannot free the reserved IDs 422 return BAD_VALUE; 423 } 424 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 425 return BAD_INDEX; 426 } 427 mAllocatedDisplayIDs.clearBit(id); 428 mDisplayData[id].connected = false; 429 return NO_ERROR; 430 } 431 432 nsecs_t HWComposer::getRefreshPeriod(int disp) const { 433 return mDisplayData[disp].refresh; 434 } 435 436 nsecs_t HWComposer::getRefreshTimestamp(int disp) const { 437 // this returns the last refresh timestamp. 438 // if the last one is not available, we estimate it based on 439 // the refresh period and whatever closest timestamp we have. 440 Mutex::Autolock _l(mLock); 441 nsecs_t now = systemTime(CLOCK_MONOTONIC); 442 return now - ((now - mLastHwVSync[disp]) % mDisplayData[disp].refresh); 443 } 444 445 sp<Fence> HWComposer::getDisplayFence(int disp) const { 446 return mDisplayData[disp].lastDisplayFence; 447 } 448 449 uint32_t HWComposer::getWidth(int disp) const { 450 return mDisplayData[disp].width; 451 } 452 453 uint32_t HWComposer::getHeight(int disp) const { 454 return mDisplayData[disp].height; 455 } 456 457 uint32_t HWComposer::getFormat(int disp) const { 458 return mDisplayData[disp].format; 459 } 460 461 float HWComposer::getDpiX(int disp) const { 462 return mDisplayData[disp].xdpi; 463 } 464 465 float HWComposer::getDpiY(int disp) const { 466 return mDisplayData[disp].ydpi; 467 } 468 469 bool HWComposer::isConnected(int disp) const { 470 return mDisplayData[disp].connected; 471 } 472 473 void HWComposer::eventControl(int disp, int event, int enabled) { 474 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) { 475 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)", 476 event, disp, enabled); 477 return; 478 } 479 if (event != EVENT_VSYNC) { 480 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)", 481 event, disp, enabled); 482 return; 483 } 484 status_t err = NO_ERROR; 485 if (mHwc && !mDebugForceFakeVSync) { 486 // NOTE: we use our own internal lock here because we have to call 487 // into the HWC with the lock held, and we want to make sure 488 // that even if HWC blocks (which it shouldn't), it won't 489 // affect other threads. 490 Mutex::Autolock _l(mEventControlLock); 491 const int32_t eventBit = 1UL << event; 492 const int32_t newValue = enabled ? eventBit : 0; 493 const int32_t oldValue = mDisplayData[disp].events & eventBit; 494 if (newValue != oldValue) { 495 ATRACE_CALL(); 496 err = mHwc->eventControl(mHwc, disp, event, enabled); 497 if (!err) { 498 int32_t& events(mDisplayData[disp].events); 499 events = (events & ~eventBit) | newValue; 500 } 501 } 502 // error here should not happen -- not sure what we should 503 // do if it does. 504 ALOGE_IF(err, "eventControl(%d, %d) failed %s", 505 event, enabled, strerror(-err)); 506 } 507 508 if (err == NO_ERROR && mVSyncThread != NULL) { 509 mVSyncThread->setEnabled(enabled); 510 } 511 } 512 513 status_t HWComposer::createWorkList(int32_t id, size_t numLayers) { 514 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 515 return BAD_INDEX; 516 } 517 518 if (mHwc) { 519 DisplayData& disp(mDisplayData[id]); 520 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 521 // we need space for the HWC_FRAMEBUFFER_TARGET 522 numLayers++; 523 } 524 if (disp.capacity < numLayers || disp.list == NULL) { 525 size_t size = sizeof(hwc_display_contents_1_t) 526 + numLayers * sizeof(hwc_layer_1_t); 527 free(disp.list); 528 disp.list = (hwc_display_contents_1_t*)malloc(size); 529 disp.capacity = numLayers; 530 } 531 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 532 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1]; 533 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t)); 534 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height }; 535 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET; 536 disp.framebufferTarget->hints = 0; 537 disp.framebufferTarget->flags = 0; 538 disp.framebufferTarget->handle = disp.fbTargetHandle; 539 disp.framebufferTarget->transform = 0; 540 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT; 541 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 542 disp.framebufferTarget->sourceCropf.left = 0; 543 disp.framebufferTarget->sourceCropf.top = 0; 544 disp.framebufferTarget->sourceCropf.right = disp.width; 545 disp.framebufferTarget->sourceCropf.bottom = disp.height; 546 } else { 547 disp.framebufferTarget->sourceCrop = r; 548 } 549 disp.framebufferTarget->displayFrame = r; 550 disp.framebufferTarget->visibleRegionScreen.numRects = 1; 551 disp.framebufferTarget->visibleRegionScreen.rects = 552 &disp.framebufferTarget->displayFrame; 553 disp.framebufferTarget->acquireFenceFd = -1; 554 disp.framebufferTarget->releaseFenceFd = -1; 555 disp.framebufferTarget->planeAlpha = 0xFF; 556 } 557 disp.list->retireFenceFd = -1; 558 disp.list->flags = HWC_GEOMETRY_CHANGED; 559 disp.list->numHwLayers = numLayers; 560 } 561 return NO_ERROR; 562 } 563 564 status_t HWComposer::setFramebufferTarget(int32_t id, 565 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) { 566 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 567 return BAD_INDEX; 568 } 569 DisplayData& disp(mDisplayData[id]); 570 if (!disp.framebufferTarget) { 571 // this should never happen, but apparently eglCreateWindowSurface() 572 // triggers a Surface::queueBuffer() on some 573 // devices (!?) -- log and ignore. 574 ALOGE("HWComposer: framebufferTarget is null"); 575 return NO_ERROR; 576 } 577 578 int acquireFenceFd = -1; 579 if (acquireFence->isValid()) { 580 acquireFenceFd = acquireFence->dup(); 581 } 582 583 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd); 584 disp.fbTargetHandle = buf->handle; 585 disp.framebufferTarget->handle = disp.fbTargetHandle; 586 disp.framebufferTarget->acquireFenceFd = acquireFenceFd; 587 return NO_ERROR; 588 } 589 590 status_t HWComposer::prepare() { 591 for (size_t i=0 ; i<mNumDisplays ; i++) { 592 DisplayData& disp(mDisplayData[i]); 593 if (disp.framebufferTarget) { 594 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET 595 // DO NOT reset the handle field to NULL, because it's possible 596 // that we have nothing to redraw (eg: eglSwapBuffers() not called) 597 // in which case, we should continue to use the same buffer. 598 LOG_FATAL_IF(disp.list == NULL); 599 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET; 600 } 601 if (!disp.connected && disp.list != NULL) { 602 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d", 603 i, disp.list->numHwLayers); 604 } 605 mLists[i] = disp.list; 606 if (mLists[i]) { 607 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 608 mLists[i]->outbuf = disp.outbufHandle; 609 mLists[i]->outbufAcquireFenceFd = -1; 610 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 611 // garbage data to catch improper use 612 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF; 613 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF; 614 } else { 615 mLists[i]->dpy = EGL_NO_DISPLAY; 616 mLists[i]->sur = EGL_NO_SURFACE; 617 } 618 } 619 } 620 621 int err = mHwc->prepare(mHwc, mNumDisplays, mLists); 622 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err)); 623 624 if (err == NO_ERROR) { 625 // here we're just making sure that "skip" layers are set 626 // to HWC_FRAMEBUFFER and we're also counting how many layers 627 // we have of each type. 628 // 629 // If there are no window layers, we treat the display has having FB 630 // composition, because SurfaceFlinger will use GLES to draw the 631 // wormhole region. 632 for (size_t i=0 ; i<mNumDisplays ; i++) { 633 DisplayData& disp(mDisplayData[i]); 634 disp.hasFbComp = false; 635 disp.hasOvComp = false; 636 if (disp.list) { 637 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) { 638 hwc_layer_1_t& l = disp.list->hwLayers[i]; 639 640 //ALOGD("prepare: %d, type=%d, handle=%p", 641 // i, l.compositionType, l.handle); 642 643 if (l.flags & HWC_SKIP_LAYER) { 644 l.compositionType = HWC_FRAMEBUFFER; 645 } 646 if (l.compositionType == HWC_FRAMEBUFFER) { 647 disp.hasFbComp = true; 648 } 649 if (l.compositionType == HWC_OVERLAY) { 650 disp.hasOvComp = true; 651 } 652 } 653 if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) { 654 disp.hasFbComp = true; 655 } 656 } else { 657 disp.hasFbComp = true; 658 } 659 } 660 } 661 return (status_t)err; 662 } 663 664 bool HWComposer::hasHwcComposition(int32_t id) const { 665 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 666 return false; 667 return mDisplayData[id].hasOvComp; 668 } 669 670 bool HWComposer::hasGlesComposition(int32_t id) const { 671 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 672 return true; 673 return mDisplayData[id].hasFbComp; 674 } 675 676 sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) { 677 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 678 return Fence::NO_FENCE; 679 680 int fd = INVALID_OPERATION; 681 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 682 const DisplayData& disp(mDisplayData[id]); 683 if (disp.framebufferTarget) { 684 fd = disp.framebufferTarget->releaseFenceFd; 685 disp.framebufferTarget->acquireFenceFd = -1; 686 disp.framebufferTarget->releaseFenceFd = -1; 687 } 688 } 689 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE; 690 } 691 692 status_t HWComposer::commit() { 693 int err = NO_ERROR; 694 if (mHwc) { 695 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 696 // On version 1.0, the OpenGL ES target surface is communicated 697 // by the (dpy, sur) fields and we are guaranteed to have only 698 // a single display. 699 mLists[0]->dpy = eglGetCurrentDisplay(); 700 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW); 701 } 702 703 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) { 704 DisplayData& disp(mDisplayData[i]); 705 if (disp.outbufHandle) { 706 mLists[i]->outbuf = disp.outbufHandle; 707 mLists[i]->outbufAcquireFenceFd = 708 disp.outbufAcquireFence->dup(); 709 } 710 } 711 712 err = mHwc->set(mHwc, mNumDisplays, mLists); 713 714 for (size_t i=0 ; i<mNumDisplays ; i++) { 715 DisplayData& disp(mDisplayData[i]); 716 disp.lastDisplayFence = disp.lastRetireFence; 717 disp.lastRetireFence = Fence::NO_FENCE; 718 if (disp.list) { 719 if (disp.list->retireFenceFd != -1) { 720 disp.lastRetireFence = new Fence(disp.list->retireFenceFd); 721 disp.list->retireFenceFd = -1; 722 } 723 disp.list->flags &= ~HWC_GEOMETRY_CHANGED; 724 } 725 } 726 } 727 return (status_t)err; 728 } 729 730 status_t HWComposer::release(int disp) { 731 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE); 732 if (mHwc) { 733 eventControl(disp, HWC_EVENT_VSYNC, 0); 734 return (status_t)mHwc->blank(mHwc, disp, 1); 735 } 736 return NO_ERROR; 737 } 738 739 status_t HWComposer::acquire(int disp) { 740 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE); 741 if (mHwc) { 742 return (status_t)mHwc->blank(mHwc, disp, 0); 743 } 744 return NO_ERROR; 745 } 746 747 void HWComposer::disconnectDisplay(int disp) { 748 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY); 749 DisplayData& dd(mDisplayData[disp]); 750 free(dd.list); 751 dd.list = NULL; 752 dd.framebufferTarget = NULL; // points into dd.list 753 dd.fbTargetHandle = NULL; 754 dd.outbufHandle = NULL; 755 dd.lastRetireFence = Fence::NO_FENCE; 756 dd.lastDisplayFence = Fence::NO_FENCE; 757 dd.outbufAcquireFence = Fence::NO_FENCE; 758 } 759 760 int HWComposer::getVisualID() const { 761 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 762 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED 763 // is supported by the implementation. we can only be in this case 764 // if we have HWC 1.1 765 return HAL_PIXEL_FORMAT_RGBA_8888; 766 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; 767 } else { 768 return mFbDev->format; 769 } 770 } 771 772 bool HWComposer::supportsFramebufferTarget() const { 773 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)); 774 } 775 776 int HWComposer::fbPost(int32_t id, 777 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) { 778 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 779 return setFramebufferTarget(id, acquireFence, buffer); 780 } else { 781 acquireFence->waitForever("HWComposer::fbPost"); 782 return mFbDev->post(mFbDev, buffer->handle); 783 } 784 } 785 786 int HWComposer::fbCompositionComplete() { 787 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) 788 return NO_ERROR; 789 790 if (mFbDev->compositionComplete) { 791 return mFbDev->compositionComplete(mFbDev); 792 } else { 793 return INVALID_OPERATION; 794 } 795 } 796 797 void HWComposer::fbDump(String8& result) { 798 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) { 799 const size_t SIZE = 4096; 800 char buffer[SIZE]; 801 mFbDev->dump(mFbDev, buffer, SIZE); 802 result.append(buffer); 803 } 804 } 805 806 status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence, 807 const sp<GraphicBuffer>& buf) { 808 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 809 return BAD_INDEX; 810 if (id < VIRTUAL_DISPLAY_ID_BASE) 811 return INVALID_OPERATION; 812 813 DisplayData& disp(mDisplayData[id]); 814 disp.outbufHandle = buf->handle; 815 disp.outbufAcquireFence = acquireFence; 816 return NO_ERROR; 817 } 818 819 sp<Fence> HWComposer::getLastRetireFence(int32_t id) { 820 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 821 return Fence::NO_FENCE; 822 return mDisplayData[id].lastRetireFence; 823 } 824 825 /* 826 * Helper template to implement a concrete HWCLayer 827 * This holds the pointer to the concrete hwc layer type 828 * and implements the "iterable" side of HWCLayer. 829 */ 830 template<typename CONCRETE, typename HWCTYPE> 831 class Iterable : public HWComposer::HWCLayer { 832 protected: 833 HWCTYPE* const mLayerList; 834 HWCTYPE* mCurrentLayer; 835 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { } 836 inline HWCTYPE const * getLayer() const { return mCurrentLayer; } 837 inline HWCTYPE* getLayer() { return mCurrentLayer; } 838 virtual ~Iterable() { } 839 private: 840 // returns a copy of ourselves 841 virtual HWComposer::HWCLayer* dup() { 842 return new CONCRETE( static_cast<const CONCRETE&>(*this) ); 843 } 844 virtual status_t setLayer(size_t index) { 845 mCurrentLayer = &mLayerList[index]; 846 return NO_ERROR; 847 } 848 }; 849 850 /* 851 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0. 852 * This implements the HWCLayer side of HWCIterableLayer. 853 */ 854 class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> { 855 struct hwc_composer_device_1* mHwc; 856 public: 857 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer) 858 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { } 859 860 virtual int32_t getCompositionType() const { 861 return getLayer()->compositionType; 862 } 863 virtual uint32_t getHints() const { 864 return getLayer()->hints; 865 } 866 virtual sp<Fence> getAndResetReleaseFence() { 867 int fd = getLayer()->releaseFenceFd; 868 getLayer()->releaseFenceFd = -1; 869 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE; 870 } 871 virtual void setAcquireFenceFd(int fenceFd) { 872 getLayer()->acquireFenceFd = fenceFd; 873 } 874 virtual void setPerFrameDefaultState() { 875 //getLayer()->compositionType = HWC_FRAMEBUFFER; 876 } 877 virtual void setPlaneAlpha(uint8_t alpha) { 878 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) { 879 getLayer()->planeAlpha = alpha; 880 } else { 881 if (alpha < 0xFF) { 882 getLayer()->flags |= HWC_SKIP_LAYER; 883 } 884 } 885 } 886 virtual void setDefaultState() { 887 hwc_layer_1_t* const l = getLayer(); 888 l->compositionType = HWC_FRAMEBUFFER; 889 l->hints = 0; 890 l->flags = HWC_SKIP_LAYER; 891 l->handle = 0; 892 l->transform = 0; 893 l->blending = HWC_BLENDING_NONE; 894 l->visibleRegionScreen.numRects = 0; 895 l->visibleRegionScreen.rects = NULL; 896 l->acquireFenceFd = -1; 897 l->releaseFenceFd = -1; 898 l->planeAlpha = 0xFF; 899 } 900 virtual void setSkip(bool skip) { 901 if (skip) { 902 getLayer()->flags |= HWC_SKIP_LAYER; 903 } else { 904 getLayer()->flags &= ~HWC_SKIP_LAYER; 905 } 906 } 907 virtual void setBlending(uint32_t blending) { 908 getLayer()->blending = blending; 909 } 910 virtual void setTransform(uint32_t transform) { 911 getLayer()->transform = transform; 912 } 913 virtual void setFrame(const Rect& frame) { 914 getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame); 915 } 916 virtual void setCrop(const FloatRect& crop) { 917 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 918 getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop); 919 } else { 920 /* 921 * Since h/w composer didn't support a flot crop rect before version 1.3, 922 * using integer coordinates instead produces a different output from the GL code in 923 * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to 924 * window size ratio is large and a window crop is defined 925 * (i.e.: if we scale the buffer a lot and we also crop it with a window crop). 926 */ 927 hwc_rect_t& r = getLayer()->sourceCrop; 928 r.left = int(ceilf(crop.left)); 929 r.top = int(ceilf(crop.top)); 930 r.right = int(floorf(crop.right)); 931 r.bottom= int(floorf(crop.bottom)); 932 } 933 } 934 virtual void setVisibleRegionScreen(const Region& reg) { 935 // Region::getSharedBuffer creates a reference to the underlying 936 // SharedBuffer of this Region, this reference is freed 937 // in onDisplayed() 938 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen; 939 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects); 940 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data()); 941 } 942 virtual void setBuffer(const sp<GraphicBuffer>& buffer) { 943 if (buffer == 0 || buffer->handle == 0) { 944 getLayer()->compositionType = HWC_FRAMEBUFFER; 945 getLayer()->flags |= HWC_SKIP_LAYER; 946 getLayer()->handle = 0; 947 } else { 948 getLayer()->handle = buffer->handle; 949 } 950 } 951 virtual void onDisplayed() { 952 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen; 953 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects); 954 if (sb) { 955 sb->release(); 956 // not technically needed but safer 957 visibleRegion.numRects = 0; 958 visibleRegion.rects = NULL; 959 } 960 961 getLayer()->acquireFenceFd = -1; 962 } 963 }; 964 965 /* 966 * returns an iterator initialized at a given index in the layer list 967 */ 968 HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) { 969 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 970 return LayerListIterator(); 971 } 972 const DisplayData& disp(mDisplayData[id]); 973 if (!mHwc || !disp.list || index > disp.list->numHwLayers) { 974 return LayerListIterator(); 975 } 976 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index); 977 } 978 979 /* 980 * returns an iterator on the beginning of the layer list 981 */ 982 HWComposer::LayerListIterator HWComposer::begin(int32_t id) { 983 return getLayerIterator(id, 0); 984 } 985 986 /* 987 * returns an iterator on the end of the layer list 988 */ 989 HWComposer::LayerListIterator HWComposer::end(int32_t id) { 990 size_t numLayers = 0; 991 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) { 992 const DisplayData& disp(mDisplayData[id]); 993 if (mHwc && disp.list) { 994 numLayers = disp.list->numHwLayers; 995 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 996 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET, 997 // which we ignore when iterating through the layer list. 998 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id); 999 if (numLayers) { 1000 numLayers--; 1001 } 1002 } 1003 } 1004 } 1005 return getLayerIterator(id, numLayers); 1006 } 1007 1008 void HWComposer::dump(String8& result) const { 1009 if (mHwc) { 1010 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc)); 1011 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync); 1012 for (size_t i=0 ; i<mNumDisplays ; i++) { 1013 const DisplayData& disp(mDisplayData[i]); 1014 if (!disp.connected) 1015 continue; 1016 1017 const Vector< sp<Layer> >& visibleLayersSortedByZ = 1018 mFlinger->getLayerSortedByZForHwcDisplay(i); 1019 1020 result.appendFormat( 1021 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n", 1022 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh); 1023 1024 if (disp.list) { 1025 result.appendFormat( 1026 " numHwLayers=%u, flags=%08x\n", 1027 disp.list->numHwLayers, disp.list->flags); 1028 1029 result.append( 1030 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n" 1031 "------------+----------+----------+----------+----+-------+----------+---------------------------------+--------------------------------\n"); 1032 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____._,_____._,_____._,_____._] | [_____,_____,_____,_____] 1033 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) { 1034 const hwc_layer_1_t&l = disp.list->hwLayers[i]; 1035 int32_t format = -1; 1036 String8 name("unknown"); 1037 1038 if (i < visibleLayersSortedByZ.size()) { 1039 const sp<Layer>& layer(visibleLayersSortedByZ[i]); 1040 const sp<GraphicBuffer>& buffer( 1041 layer->getActiveBuffer()); 1042 if (buffer != NULL) { 1043 format = buffer->getPixelFormat(); 1044 } 1045 name = layer->getName(); 1046 } 1047 1048 int type = l.compositionType; 1049 if (type == HWC_FRAMEBUFFER_TARGET) { 1050 name = "HWC_FRAMEBUFFER_TARGET"; 1051 format = disp.format; 1052 } 1053 1054 static char const* compositionTypeName[] = { 1055 "GLES", 1056 "HWC", 1057 "BACKGROUND", 1058 "FB TARGET", 1059 "UNKNOWN"}; 1060 if (type >= NELEM(compositionTypeName)) 1061 type = NELEM(compositionTypeName) - 1; 1062 1063 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 1064 result.appendFormat( 1065 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%7.1f,%7.1f,%7.1f,%7.1f] | [%5d,%5d,%5d,%5d] %s\n", 1066 compositionTypeName[type], 1067 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format, 1068 l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom, 1069 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom, 1070 name.string()); 1071 } else { 1072 result.appendFormat( 1073 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%7d,%7d,%7d,%7d] | [%5d,%5d,%5d,%5d] %s\n", 1074 compositionTypeName[type], 1075 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format, 1076 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom, 1077 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom, 1078 name.string()); 1079 } 1080 } 1081 } 1082 } 1083 } 1084 1085 if (mHwc && mHwc->dump) { 1086 const size_t SIZE = 4096; 1087 char buffer[SIZE]; 1088 mHwc->dump(mHwc, buffer, SIZE); 1089 result.append(buffer); 1090 } 1091 } 1092 1093 // --------------------------------------------------------------------------- 1094 1095 HWComposer::VSyncThread::VSyncThread(HWComposer& hwc) 1096 : mHwc(hwc), mEnabled(false), 1097 mNextFakeVSync(0), 1098 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY)) 1099 { 1100 } 1101 1102 void HWComposer::VSyncThread::setEnabled(bool enabled) { 1103 Mutex::Autolock _l(mLock); 1104 if (mEnabled != enabled) { 1105 mEnabled = enabled; 1106 mCondition.signal(); 1107 } 1108 } 1109 1110 void HWComposer::VSyncThread::onFirstRef() { 1111 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); 1112 } 1113 1114 bool HWComposer::VSyncThread::threadLoop() { 1115 { // scope for lock 1116 Mutex::Autolock _l(mLock); 1117 while (!mEnabled) { 1118 mCondition.wait(mLock); 1119 } 1120 } 1121 1122 const nsecs_t period = mRefreshPeriod; 1123 const nsecs_t now = systemTime(CLOCK_MONOTONIC); 1124 nsecs_t next_vsync = mNextFakeVSync; 1125 nsecs_t sleep = next_vsync - now; 1126 if (sleep < 0) { 1127 // we missed, find where the next vsync should be 1128 sleep = (period - ((now - next_vsync) % period)); 1129 next_vsync = now + sleep; 1130 } 1131 mNextFakeVSync = next_vsync + period; 1132 1133 struct timespec spec; 1134 spec.tv_sec = next_vsync / 1000000000; 1135 spec.tv_nsec = next_vsync % 1000000000; 1136 1137 int err; 1138 do { 1139 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL); 1140 } while (err<0 && errno == EINTR); 1141 1142 if (err == 0) { 1143 mHwc.mEventHandler.onVSyncReceived(0, next_vsync); 1144 } 1145 1146 return true; 1147 } 1148 1149 HWComposer::DisplayData::DisplayData() 1150 : width(0), height(0), format(0), 1151 xdpi(0.0f), ydpi(0.0f), 1152 refresh(0), 1153 connected(false), 1154 hasFbComp(false), hasOvComp(false), 1155 capacity(0), list(NULL), 1156 framebufferTarget(NULL), fbTargetHandle(0), 1157 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE), 1158 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE), 1159 events(0) 1160 {} 1161 1162 HWComposer::DisplayData::~DisplayData() { 1163 free(list); 1164 } 1165 1166 // --------------------------------------------------------------------------- 1167 }; // namespace android 1168