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