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