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