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