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