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