HWComposer.cpp revision 2c9b11f0291210c9b9513a1a0cce6afebd361b3b
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#include <inttypes.h> 20#include <math.h> 21#include <stdint.h> 22#include <stdio.h> 23#include <stdlib.h> 24#include <string.h> 25#include <sys/types.h> 26 27#include <utils/Errors.h> 28#include <utils/misc.h> 29#include <utils/NativeHandle.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 <android/configuration.h> 41 42#include <cutils/log.h> 43#include <cutils/properties.h> 44 45#include "HWComposer.h" 46 47#include "../Layer.h" // needed only for debugging 48#include "../SurfaceFlinger.h" 49 50namespace android { 51 52#define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION 53 54static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) { 55 uint32_t hwcVersion = hwc->common.version; 56 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK; 57} 58 59static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) { 60 uint32_t hwcVersion = hwc->common.version; 61 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK; 62} 63 64static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc, 65 uint32_t version) { 66 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK); 67} 68 69// --------------------------------------------------------------------------- 70 71struct HWComposer::cb_context { 72 struct callbacks : public hwc_procs_t { 73 // these are here to facilitate the transition when adding 74 // new callbacks (an implementation can check for NULL before 75 // calling a new callback). 76 void (*zero[4])(void); 77 }; 78 callbacks procs; 79 HWComposer* hwc; 80}; 81 82// --------------------------------------------------------------------------- 83 84HWComposer::HWComposer( 85 const sp<SurfaceFlinger>& flinger, 86 EventHandler& handler) 87 : mFlinger(flinger), 88 mFbDev(0), mHwc(0), mNumDisplays(1), 89 mCBContext(new cb_context), 90 mEventHandler(handler), 91 mDebugForceFakeVSync(false) 92{ 93 for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) { 94 mLists[i] = 0; 95 } 96 97 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) { 98 mLastHwVSync[i] = 0; 99 mVSyncCounts[i] = 0; 100 } 101 102 char value[PROPERTY_VALUE_MAX]; 103 property_get("debug.sf.no_hw_vsync", value, "0"); 104 mDebugForceFakeVSync = atoi(value); 105 106 bool needVSyncThread = true; 107 108 // Note: some devices may insist that the FB HAL be opened before HWC. 109 int fberr = loadFbHalModule(); 110 loadHwcModule(); 111 112 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 113 // close FB HAL if we don't needed it. 114 // FIXME: this is temporary until we're not forced to open FB HAL 115 // before HWC. 116 framebuffer_close(mFbDev); 117 mFbDev = NULL; 118 } 119 120 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory. 121 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) 122 && !mFbDev) { 123 ALOGE("ERROR: failed to open framebuffer (%s), aborting", 124 strerror(-fberr)); 125 abort(); 126 } 127 128 // these display IDs are always reserved 129 for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) { 130 mAllocatedDisplayIDs.markBit(i); 131 } 132 133 if (mHwc) { 134 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER, 135 (hwcApiVersion(mHwc) >> 24) & 0xff, 136 (hwcApiVersion(mHwc) >> 16) & 0xff); 137 if (mHwc->registerProcs) { 138 mCBContext->hwc = this; 139 mCBContext->procs.invalidate = &hook_invalidate; 140 mCBContext->procs.vsync = &hook_vsync; 141 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) 142 mCBContext->procs.hotplug = &hook_hotplug; 143 else 144 mCBContext->procs.hotplug = NULL; 145 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero)); 146 mHwc->registerProcs(mHwc, &mCBContext->procs); 147 } 148 149 // don't need a vsync thread if we have a hardware composer 150 needVSyncThread = false; 151 // always turn vsync off when we start 152 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0); 153 154 // the number of displays we actually have depends on the 155 // hw composer version 156 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 157 // 1.3 adds support for virtual displays 158 mNumDisplays = MAX_HWC_DISPLAYS; 159 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 160 // 1.1 adds support for multiple displays 161 mNumDisplays = NUM_BUILTIN_DISPLAYS; 162 } else { 163 mNumDisplays = 1; 164 } 165 } 166 167 if (mFbDev) { 168 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)), 169 "should only have fbdev if no hwc or hwc is 1.0"); 170 171 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]); 172 disp.connected = true; 173 disp.format = mFbDev->format; 174 DisplayConfig config = DisplayConfig(); 175 config.width = mFbDev->width; 176 config.height = mFbDev->height; 177 config.xdpi = mFbDev->xdpi; 178 config.ydpi = mFbDev->ydpi; 179 config.refresh = nsecs_t(1e9 / mFbDev->fps); 180 disp.configs.push_back(config); 181 disp.currentConfig = 0; 182 } else if (mHwc) { 183 // here we're guaranteed to have at least HWC 1.1 184 for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) { 185 queryDisplayProperties(i); 186 } 187 } 188 189 if (needVSyncThread) { 190 // we don't have VSYNC support, we need to fake it 191 mVSyncThread = new VSyncThread(*this); 192 } 193} 194 195HWComposer::~HWComposer() { 196 if (mHwc) { 197 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0); 198 } 199 if (mVSyncThread != NULL) { 200 mVSyncThread->requestExitAndWait(); 201 } 202 if (mHwc) { 203 hwc_close_1(mHwc); 204 } 205 if (mFbDev) { 206 framebuffer_close(mFbDev); 207 } 208 delete mCBContext; 209} 210 211// Load and prepare the hardware composer module. Sets mHwc. 212void HWComposer::loadHwcModule() 213{ 214 hw_module_t const* module; 215 216 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) { 217 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID); 218 return; 219 } 220 221 int err = hwc_open_1(module, &mHwc); 222 if (err) { 223 ALOGE("%s device failed to initialize (%s)", 224 HWC_HARDWARE_COMPOSER, strerror(-err)); 225 return; 226 } 227 228 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) || 229 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION || 230 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) { 231 ALOGE("%s device version %#x unsupported, will not be used", 232 HWC_HARDWARE_COMPOSER, mHwc->common.version); 233 hwc_close_1(mHwc); 234 mHwc = NULL; 235 return; 236 } 237} 238 239// Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev. 240int HWComposer::loadFbHalModule() 241{ 242 hw_module_t const* module; 243 244 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); 245 if (err != 0) { 246 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID); 247 return err; 248 } 249 250 return framebuffer_open(module, &mFbDev); 251} 252 253status_t HWComposer::initCheck() const { 254 return mHwc ? NO_ERROR : NO_INIT; 255} 256 257void HWComposer::hook_invalidate(const struct hwc_procs* procs) { 258 cb_context* ctx = reinterpret_cast<cb_context*>( 259 const_cast<hwc_procs_t*>(procs)); 260 ctx->hwc->invalidate(); 261} 262 263void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp, 264 int64_t timestamp) { 265 cb_context* ctx = reinterpret_cast<cb_context*>( 266 const_cast<hwc_procs_t*>(procs)); 267 ctx->hwc->vsync(disp, timestamp); 268} 269 270void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp, 271 int connected) { 272 cb_context* ctx = reinterpret_cast<cb_context*>( 273 const_cast<hwc_procs_t*>(procs)); 274 ctx->hwc->hotplug(disp, connected); 275} 276 277void HWComposer::invalidate() { 278 mFlinger->repaintEverything(); 279} 280 281void HWComposer::vsync(int disp, int64_t timestamp) { 282 if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) { 283 { 284 Mutex::Autolock _l(mLock); 285 286 // There have been reports of HWCs that signal several vsync events 287 // with the same timestamp when turning the display off and on. This 288 // is a bug in the HWC implementation, but filter the extra events 289 // out here so they don't cause havoc downstream. 290 if (timestamp == mLastHwVSync[disp]) { 291 ALOGW("Ignoring duplicate VSYNC event from HWC (t=%lld)", 292 timestamp); 293 return; 294 } 295 296 mLastHwVSync[disp] = timestamp; 297 } 298 299 char tag[16]; 300 snprintf(tag, sizeof(tag), "HW_VSYNC_%1u", disp); 301 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1); 302 303 mEventHandler.onVSyncReceived(disp, timestamp); 304 } 305} 306 307void HWComposer::hotplug(int disp, int connected) { 308 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) { 309 ALOGE("hotplug event received for invalid display: disp=%d connected=%d", 310 disp, connected); 311 return; 312 } 313 queryDisplayProperties(disp); 314 mEventHandler.onHotplugReceived(disp, bool(connected)); 315} 316 317static float getDefaultDensity(uint32_t height) { 318 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH; 319 else return ACONFIGURATION_DENSITY_TV; 320} 321 322static const uint32_t DISPLAY_ATTRIBUTES[] = { 323 HWC_DISPLAY_VSYNC_PERIOD, 324 HWC_DISPLAY_WIDTH, 325 HWC_DISPLAY_HEIGHT, 326 HWC_DISPLAY_DPI_X, 327 HWC_DISPLAY_DPI_Y, 328 HWC_DISPLAY_NO_ATTRIBUTE, 329}; 330#define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0]) 331 332status_t HWComposer::queryDisplayProperties(int disp) { 333 334 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)); 335 336 // use zero as default value for unspecified attributes 337 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1]; 338 memset(values, 0, sizeof(values)); 339 340 const size_t MAX_NUM_CONFIGS = 128; 341 uint32_t configs[MAX_NUM_CONFIGS] = {0}; 342 size_t numConfigs = MAX_NUM_CONFIGS; 343 status_t err = mHwc->getDisplayConfigs(mHwc, disp, configs, &numConfigs); 344 if (err != NO_ERROR) { 345 // this can happen if an unpluggable display is not connected 346 mDisplayData[disp].connected = false; 347 return err; 348 } 349 350 mDisplayData[disp].currentConfig = 0; 351 for (size_t c = 0; c < numConfigs; ++c) { 352 err = mHwc->getDisplayAttributes(mHwc, disp, configs[c], 353 DISPLAY_ATTRIBUTES, values); 354 if (err != NO_ERROR) { 355 // we can't get this display's info. turn it off. 356 mDisplayData[disp].connected = false; 357 return err; 358 } 359 360 DisplayConfig config = DisplayConfig(); 361 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) { 362 switch (DISPLAY_ATTRIBUTES[i]) { 363 case HWC_DISPLAY_VSYNC_PERIOD: 364 config.refresh = nsecs_t(values[i]); 365 break; 366 case HWC_DISPLAY_WIDTH: 367 config.width = values[i]; 368 break; 369 case HWC_DISPLAY_HEIGHT: 370 config.height = values[i]; 371 break; 372 case HWC_DISPLAY_DPI_X: 373 config.xdpi = values[i] / 1000.0f; 374 break; 375 case HWC_DISPLAY_DPI_Y: 376 config.ydpi = values[i] / 1000.0f; 377 break; 378 default: 379 ALOG_ASSERT(false, "unknown display attribute[%d] %#x", 380 i, DISPLAY_ATTRIBUTES[i]); 381 break; 382 } 383 } 384 385 if (config.xdpi == 0.0f || config.ydpi == 0.0f) { 386 float dpi = getDefaultDensity(config.height); 387 config.xdpi = dpi; 388 config.ydpi = dpi; 389 } 390 391 mDisplayData[disp].configs.push_back(config); 392 } 393 394 // FIXME: what should we set the format to? 395 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888; 396 mDisplayData[disp].connected = true; 397 return NO_ERROR; 398} 399 400status_t HWComposer::setVirtualDisplayProperties(int32_t id, 401 uint32_t w, uint32_t h, uint32_t format) { 402 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) || 403 !mAllocatedDisplayIDs.hasBit(id)) { 404 return BAD_INDEX; 405 } 406 size_t configId = mDisplayData[id].currentConfig; 407 mDisplayData[id].format = format; 408 DisplayConfig& config = mDisplayData[id].configs.editItemAt(configId); 409 config.width = w; 410 config.height = h; 411 config.xdpi = config.ydpi = getDefaultDensity(h); 412 return NO_ERROR; 413} 414 415int32_t HWComposer::allocateDisplayId() { 416 if (mAllocatedDisplayIDs.count() >= mNumDisplays) { 417 return NO_MEMORY; 418 } 419 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit(); 420 mAllocatedDisplayIDs.markBit(id); 421 mDisplayData[id].connected = true; 422 mDisplayData[id].configs.resize(1); 423 mDisplayData[id].currentConfig = 0; 424 return id; 425} 426 427status_t HWComposer::freeDisplayId(int32_t id) { 428 if (id < NUM_BUILTIN_DISPLAYS) { 429 // cannot free the reserved IDs 430 return BAD_VALUE; 431 } 432 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 433 return BAD_INDEX; 434 } 435 mAllocatedDisplayIDs.clearBit(id); 436 mDisplayData[id].connected = false; 437 return NO_ERROR; 438} 439 440nsecs_t HWComposer::getRefreshTimestamp(int disp) const { 441 // this returns the last refresh timestamp. 442 // if the last one is not available, we estimate it based on 443 // the refresh period and whatever closest timestamp we have. 444 Mutex::Autolock _l(mLock); 445 nsecs_t now = systemTime(CLOCK_MONOTONIC); 446 size_t configId = mDisplayData[disp].currentConfig; 447 return now - ((now - mLastHwVSync[disp]) % 448 mDisplayData[disp].configs[configId].refresh); 449} 450 451sp<Fence> HWComposer::getDisplayFence(int disp) const { 452 return mDisplayData[disp].lastDisplayFence; 453} 454 455uint32_t HWComposer::getFormat(int disp) const { 456 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) { 457 return HAL_PIXEL_FORMAT_RGBA_8888; 458 } else { 459 return mDisplayData[disp].format; 460 } 461} 462 463bool HWComposer::isConnected(int disp) const { 464 return mDisplayData[disp].connected; 465} 466 467uint32_t HWComposer::getWidth(int disp) const { 468 size_t currentConfig = mDisplayData[disp].currentConfig; 469 return mDisplayData[disp].configs[currentConfig].width; 470} 471 472uint32_t HWComposer::getHeight(int disp) const { 473 size_t currentConfig = mDisplayData[disp].currentConfig; 474 return mDisplayData[disp].configs[currentConfig].height; 475} 476 477float HWComposer::getDpiX(int disp) const { 478 size_t currentConfig = mDisplayData[disp].currentConfig; 479 return mDisplayData[disp].configs[currentConfig].xdpi; 480} 481 482float HWComposer::getDpiY(int disp) const { 483 size_t currentConfig = mDisplayData[disp].currentConfig; 484 return mDisplayData[disp].configs[currentConfig].ydpi; 485} 486 487nsecs_t HWComposer::getRefreshPeriod(int disp) const { 488 size_t currentConfig = mDisplayData[disp].currentConfig; 489 return mDisplayData[disp].configs[currentConfig].refresh; 490} 491 492const Vector<HWComposer::DisplayConfig>& HWComposer::getConfigs(int disp) const { 493 return mDisplayData[disp].configs; 494} 495 496size_t HWComposer::getCurrentConfig(int disp) const { 497 return mDisplayData[disp].currentConfig; 498} 499 500void HWComposer::eventControl(int disp, int event, int enabled) { 501 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) { 502 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)", 503 event, disp, enabled); 504 return; 505 } 506 if (event != EVENT_VSYNC) { 507 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)", 508 event, disp, enabled); 509 return; 510 } 511 status_t err = NO_ERROR; 512 if (mHwc && !mDebugForceFakeVSync) { 513 // NOTE: we use our own internal lock here because we have to call 514 // into the HWC with the lock held, and we want to make sure 515 // that even if HWC blocks (which it shouldn't), it won't 516 // affect other threads. 517 Mutex::Autolock _l(mEventControlLock); 518 const int32_t eventBit = 1UL << event; 519 const int32_t newValue = enabled ? eventBit : 0; 520 const int32_t oldValue = mDisplayData[disp].events & eventBit; 521 if (newValue != oldValue) { 522 ATRACE_CALL(); 523 err = mHwc->eventControl(mHwc, disp, event, enabled); 524 if (!err) { 525 int32_t& events(mDisplayData[disp].events); 526 events = (events & ~eventBit) | newValue; 527 } 528 } 529 // error here should not happen -- not sure what we should 530 // do if it does. 531 ALOGE_IF(err, "eventControl(%d, %d) failed %s", 532 event, enabled, strerror(-err)); 533 } 534 535 if (err == NO_ERROR && mVSyncThread != NULL) { 536 mVSyncThread->setEnabled(enabled); 537 } 538} 539 540status_t HWComposer::createWorkList(int32_t id, size_t numLayers) { 541 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 542 return BAD_INDEX; 543 } 544 545 if (mHwc) { 546 DisplayData& disp(mDisplayData[id]); 547 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 548 // we need space for the HWC_FRAMEBUFFER_TARGET 549 numLayers++; 550 } 551 if (disp.capacity < numLayers || disp.list == NULL) { 552 size_t size = sizeof(hwc_display_contents_1_t) 553 + numLayers * sizeof(hwc_layer_1_t); 554 free(disp.list); 555 disp.list = (hwc_display_contents_1_t*)malloc(size); 556 disp.capacity = numLayers; 557 } 558 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 559 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1]; 560 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t)); 561 const DisplayConfig& currentConfig = 562 disp.configs[disp.currentConfig]; 563 const hwc_rect_t r = { 0, 0, 564 (int) currentConfig.width, (int) currentConfig.height }; 565 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET; 566 disp.framebufferTarget->hints = 0; 567 disp.framebufferTarget->flags = 0; 568 disp.framebufferTarget->handle = disp.fbTargetHandle; 569 disp.framebufferTarget->transform = 0; 570 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT; 571 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 572 disp.framebufferTarget->sourceCropf.left = 0; 573 disp.framebufferTarget->sourceCropf.top = 0; 574 disp.framebufferTarget->sourceCropf.right = 575 currentConfig.width; 576 disp.framebufferTarget->sourceCropf.bottom = 577 currentConfig.height; 578 } else { 579 disp.framebufferTarget->sourceCrop = r; 580 } 581 disp.framebufferTarget->displayFrame = r; 582 disp.framebufferTarget->visibleRegionScreen.numRects = 1; 583 disp.framebufferTarget->visibleRegionScreen.rects = 584 &disp.framebufferTarget->displayFrame; 585 disp.framebufferTarget->acquireFenceFd = -1; 586 disp.framebufferTarget->releaseFenceFd = -1; 587 disp.framebufferTarget->planeAlpha = 0xFF; 588 } 589 disp.list->retireFenceFd = -1; 590 disp.list->flags = HWC_GEOMETRY_CHANGED; 591 disp.list->numHwLayers = numLayers; 592 } 593 return NO_ERROR; 594} 595 596status_t HWComposer::setFramebufferTarget(int32_t id, 597 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) { 598 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 599 return BAD_INDEX; 600 } 601 DisplayData& disp(mDisplayData[id]); 602 if (!disp.framebufferTarget) { 603 // this should never happen, but apparently eglCreateWindowSurface() 604 // triggers a Surface::queueBuffer() on some 605 // devices (!?) -- log and ignore. 606 ALOGE("HWComposer: framebufferTarget is null"); 607 return NO_ERROR; 608 } 609 610 int acquireFenceFd = -1; 611 if (acquireFence->isValid()) { 612 acquireFenceFd = acquireFence->dup(); 613 } 614 615 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd); 616 disp.fbTargetHandle = buf->handle; 617 disp.framebufferTarget->handle = disp.fbTargetHandle; 618 disp.framebufferTarget->acquireFenceFd = acquireFenceFd; 619 return NO_ERROR; 620} 621 622status_t HWComposer::prepare() { 623 for (size_t i=0 ; i<mNumDisplays ; i++) { 624 DisplayData& disp(mDisplayData[i]); 625 if (disp.framebufferTarget) { 626 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET 627 // DO NOT reset the handle field to NULL, because it's possible 628 // that we have nothing to redraw (eg: eglSwapBuffers() not called) 629 // in which case, we should continue to use the same buffer. 630 LOG_FATAL_IF(disp.list == NULL); 631 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET; 632 } 633 if (!disp.connected && disp.list != NULL) { 634 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d", 635 i, disp.list->numHwLayers); 636 } 637 mLists[i] = disp.list; 638 if (mLists[i]) { 639 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 640 mLists[i]->outbuf = disp.outbufHandle; 641 mLists[i]->outbufAcquireFenceFd = -1; 642 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 643 // garbage data to catch improper use 644 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF; 645 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF; 646 } else { 647 mLists[i]->dpy = EGL_NO_DISPLAY; 648 mLists[i]->sur = EGL_NO_SURFACE; 649 } 650 } 651 } 652 653 int err = mHwc->prepare(mHwc, mNumDisplays, mLists); 654 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err)); 655 656 if (err == NO_ERROR) { 657 // here we're just making sure that "skip" layers are set 658 // to HWC_FRAMEBUFFER and we're also counting how many layers 659 // we have of each type. 660 // 661 // If there are no window layers, we treat the display has having FB 662 // composition, because SurfaceFlinger will use GLES to draw the 663 // wormhole region. 664 for (size_t i=0 ; i<mNumDisplays ; i++) { 665 DisplayData& disp(mDisplayData[i]); 666 disp.hasFbComp = false; 667 disp.hasOvComp = false; 668 if (disp.list) { 669 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) { 670 hwc_layer_1_t& l = disp.list->hwLayers[i]; 671 672 //ALOGD("prepare: %d, type=%d, handle=%p", 673 // i, l.compositionType, l.handle); 674 675 if (l.flags & HWC_SKIP_LAYER) { 676 l.compositionType = HWC_FRAMEBUFFER; 677 } 678 if (l.compositionType == HWC_FRAMEBUFFER) { 679 disp.hasFbComp = true; 680 } 681 if (l.compositionType == HWC_OVERLAY) { 682 disp.hasOvComp = true; 683 } 684 } 685 if (disp.list->numHwLayers == (disp.framebufferTarget ? 1 : 0)) { 686 disp.hasFbComp = true; 687 } 688 } else { 689 disp.hasFbComp = true; 690 } 691 } 692 } 693 return (status_t)err; 694} 695 696bool HWComposer::hasHwcComposition(int32_t id) const { 697 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 698 return false; 699 return mDisplayData[id].hasOvComp; 700} 701 702bool HWComposer::hasGlesComposition(int32_t id) const { 703 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 704 return true; 705 return mDisplayData[id].hasFbComp; 706} 707 708sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) { 709 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 710 return Fence::NO_FENCE; 711 712 int fd = INVALID_OPERATION; 713 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 714 const DisplayData& disp(mDisplayData[id]); 715 if (disp.framebufferTarget) { 716 fd = disp.framebufferTarget->releaseFenceFd; 717 disp.framebufferTarget->acquireFenceFd = -1; 718 disp.framebufferTarget->releaseFenceFd = -1; 719 } 720 } 721 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE; 722} 723 724status_t HWComposer::commit() { 725 int err = NO_ERROR; 726 if (mHwc) { 727 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 728 // On version 1.0, the OpenGL ES target surface is communicated 729 // by the (dpy, sur) fields and we are guaranteed to have only 730 // a single display. 731 mLists[0]->dpy = eglGetCurrentDisplay(); 732 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW); 733 } 734 735 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) { 736 DisplayData& disp(mDisplayData[i]); 737 if (disp.outbufHandle) { 738 mLists[i]->outbuf = disp.outbufHandle; 739 mLists[i]->outbufAcquireFenceFd = 740 disp.outbufAcquireFence->dup(); 741 } 742 } 743 744 err = mHwc->set(mHwc, mNumDisplays, mLists); 745 746 for (size_t i=0 ; i<mNumDisplays ; i++) { 747 DisplayData& disp(mDisplayData[i]); 748 disp.lastDisplayFence = disp.lastRetireFence; 749 disp.lastRetireFence = Fence::NO_FENCE; 750 if (disp.list) { 751 if (disp.list->retireFenceFd != -1) { 752 disp.lastRetireFence = new Fence(disp.list->retireFenceFd); 753 disp.list->retireFenceFd = -1; 754 } 755 disp.list->flags &= ~HWC_GEOMETRY_CHANGED; 756 } 757 } 758 } 759 return (status_t)err; 760} 761 762status_t HWComposer::setPowerMode(int disp, int mode) { 763 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE); 764 if (mHwc) { 765 if (mode == HWC_POWER_MODE_OFF) { 766 eventControl(disp, HWC_EVENT_VSYNC, 0); 767 } 768 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_4)) { 769 return (status_t)mHwc->setPowerMode(mHwc, disp, mode); 770 } else { 771 return (status_t)mHwc->blank(mHwc, disp, 772 mode == HWC_POWER_MODE_OFF ? 1 : 0); 773 } 774 } 775 return NO_ERROR; 776} 777 778void HWComposer::disconnectDisplay(int disp) { 779 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY); 780 DisplayData& dd(mDisplayData[disp]); 781 free(dd.list); 782 dd.list = NULL; 783 dd.framebufferTarget = NULL; // points into dd.list 784 dd.fbTargetHandle = NULL; 785 dd.outbufHandle = NULL; 786 dd.lastRetireFence = Fence::NO_FENCE; 787 dd.lastDisplayFence = Fence::NO_FENCE; 788 dd.outbufAcquireFence = Fence::NO_FENCE; 789} 790 791int HWComposer::getVisualID() const { 792 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 793 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED 794 // is supported by the implementation. we can only be in this case 795 // if we have HWC 1.1 796 return HAL_PIXEL_FORMAT_RGBA_8888; 797 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; 798 } else { 799 return mFbDev->format; 800 } 801} 802 803bool HWComposer::supportsFramebufferTarget() const { 804 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)); 805} 806 807int HWComposer::fbPost(int32_t id, 808 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) { 809 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 810 return setFramebufferTarget(id, acquireFence, buffer); 811 } else { 812 acquireFence->waitForever("HWComposer::fbPost"); 813 return mFbDev->post(mFbDev, buffer->handle); 814 } 815} 816 817int HWComposer::fbCompositionComplete() { 818 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) 819 return NO_ERROR; 820 821 if (mFbDev->compositionComplete) { 822 return mFbDev->compositionComplete(mFbDev); 823 } else { 824 return INVALID_OPERATION; 825 } 826} 827 828void HWComposer::fbDump(String8& result) { 829 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) { 830 const size_t SIZE = 4096; 831 char buffer[SIZE]; 832 mFbDev->dump(mFbDev, buffer, SIZE); 833 result.append(buffer); 834 } 835} 836 837status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence, 838 const sp<GraphicBuffer>& buf) { 839 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 840 return BAD_INDEX; 841 if (id < VIRTUAL_DISPLAY_ID_BASE) 842 return INVALID_OPERATION; 843 844 DisplayData& disp(mDisplayData[id]); 845 disp.outbufHandle = buf->handle; 846 disp.outbufAcquireFence = acquireFence; 847 return NO_ERROR; 848} 849 850sp<Fence> HWComposer::getLastRetireFence(int32_t id) const { 851 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 852 return Fence::NO_FENCE; 853 return mDisplayData[id].lastRetireFence; 854} 855 856/* 857 * Helper template to implement a concrete HWCLayer 858 * This holds the pointer to the concrete hwc layer type 859 * and implements the "iterable" side of HWCLayer. 860 */ 861template<typename CONCRETE, typename HWCTYPE> 862class Iterable : public HWComposer::HWCLayer { 863protected: 864 HWCTYPE* const mLayerList; 865 HWCTYPE* mCurrentLayer; 866 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { } 867 inline HWCTYPE const * getLayer() const { return mCurrentLayer; } 868 inline HWCTYPE* getLayer() { return mCurrentLayer; } 869 virtual ~Iterable() { } 870private: 871 // returns a copy of ourselves 872 virtual HWComposer::HWCLayer* dup() { 873 return new CONCRETE( static_cast<const CONCRETE&>(*this) ); 874 } 875 virtual status_t setLayer(size_t index) { 876 mCurrentLayer = &mLayerList[index]; 877 return NO_ERROR; 878 } 879}; 880 881/* 882 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0. 883 * This implements the HWCLayer side of HWCIterableLayer. 884 */ 885class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> { 886 struct hwc_composer_device_1* mHwc; 887public: 888 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer) 889 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { } 890 891 virtual int32_t getCompositionType() const { 892 return getLayer()->compositionType; 893 } 894 virtual uint32_t getHints() const { 895 return getLayer()->hints; 896 } 897 virtual sp<Fence> getAndResetReleaseFence() { 898 int fd = getLayer()->releaseFenceFd; 899 getLayer()->releaseFenceFd = -1; 900 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE; 901 } 902 virtual void setAcquireFenceFd(int fenceFd) { 903 getLayer()->acquireFenceFd = fenceFd; 904 } 905 virtual void setPerFrameDefaultState() { 906 //getLayer()->compositionType = HWC_FRAMEBUFFER; 907 } 908 virtual void setPlaneAlpha(uint8_t alpha) { 909 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) { 910 getLayer()->planeAlpha = alpha; 911 } else { 912 if (alpha < 0xFF) { 913 getLayer()->flags |= HWC_SKIP_LAYER; 914 } 915 } 916 } 917 virtual void setDefaultState() { 918 hwc_layer_1_t* const l = getLayer(); 919 l->compositionType = HWC_FRAMEBUFFER; 920 l->hints = 0; 921 l->flags = HWC_SKIP_LAYER; 922 l->handle = 0; 923 l->transform = 0; 924 l->blending = HWC_BLENDING_NONE; 925 l->visibleRegionScreen.numRects = 0; 926 l->visibleRegionScreen.rects = NULL; 927 l->acquireFenceFd = -1; 928 l->releaseFenceFd = -1; 929 l->planeAlpha = 0xFF; 930 } 931 virtual void setSkip(bool skip) { 932 if (skip) { 933 getLayer()->flags |= HWC_SKIP_LAYER; 934 } else { 935 getLayer()->flags &= ~HWC_SKIP_LAYER; 936 } 937 } 938 virtual void setBlending(uint32_t blending) { 939 getLayer()->blending = blending; 940 } 941 virtual void setTransform(uint32_t transform) { 942 getLayer()->transform = transform; 943 } 944 virtual void setFrame(const Rect& frame) { 945 getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame); 946 } 947 virtual void setCrop(const FloatRect& crop) { 948 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 949 getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop); 950 } else { 951 /* 952 * Since h/w composer didn't support a flot crop rect before version 1.3, 953 * using integer coordinates instead produces a different output from the GL code in 954 * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to 955 * window size ratio is large and a window crop is defined 956 * (i.e.: if we scale the buffer a lot and we also crop it with a window crop). 957 */ 958 hwc_rect_t& r = getLayer()->sourceCrop; 959 r.left = int(ceilf(crop.left)); 960 r.top = int(ceilf(crop.top)); 961 r.right = int(floorf(crop.right)); 962 r.bottom= int(floorf(crop.bottom)); 963 } 964 } 965 virtual void setVisibleRegionScreen(const Region& reg) { 966 // Region::getSharedBuffer creates a reference to the underlying 967 // SharedBuffer of this Region, this reference is freed 968 // in onDisplayed() 969 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen; 970 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects); 971 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data()); 972 } 973 virtual void setSidebandStream(const sp<NativeHandle>& stream) { 974 ALOG_ASSERT(stream->handle() != NULL); 975 getLayer()->compositionType = HWC_SIDEBAND; 976 getLayer()->sidebandStream = stream->handle(); 977 } 978 virtual void setBuffer(const sp<GraphicBuffer>& buffer) { 979 if (buffer == 0 || buffer->handle == 0) { 980 getLayer()->compositionType = HWC_FRAMEBUFFER; 981 getLayer()->flags |= HWC_SKIP_LAYER; 982 getLayer()->handle = 0; 983 } else { 984 if (getLayer()->compositionType == HWC_SIDEBAND) { 985 // If this was a sideband layer but the stream was removed, reset 986 // it to FRAMEBUFFER. The HWC can change it to OVERLAY in prepare. 987 getLayer()->compositionType = HWC_FRAMEBUFFER; 988 } 989 getLayer()->handle = buffer->handle; 990 } 991 } 992 virtual void onDisplayed() { 993 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen; 994 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects); 995 if (sb) { 996 sb->release(); 997 // not technically needed but safer 998 visibleRegion.numRects = 0; 999 visibleRegion.rects = NULL; 1000 } 1001 1002 getLayer()->acquireFenceFd = -1; 1003 } 1004}; 1005 1006/* 1007 * returns an iterator initialized at a given index in the layer list 1008 */ 1009HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) { 1010 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 1011 return LayerListIterator(); 1012 } 1013 const DisplayData& disp(mDisplayData[id]); 1014 if (!mHwc || !disp.list || index > disp.list->numHwLayers) { 1015 return LayerListIterator(); 1016 } 1017 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index); 1018} 1019 1020/* 1021 * returns an iterator on the beginning of the layer list 1022 */ 1023HWComposer::LayerListIterator HWComposer::begin(int32_t id) { 1024 return getLayerIterator(id, 0); 1025} 1026 1027/* 1028 * returns an iterator on the end of the layer list 1029 */ 1030HWComposer::LayerListIterator HWComposer::end(int32_t id) { 1031 size_t numLayers = 0; 1032 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) { 1033 const DisplayData& disp(mDisplayData[id]); 1034 if (mHwc && disp.list) { 1035 numLayers = disp.list->numHwLayers; 1036 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) { 1037 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET, 1038 // which we ignore when iterating through the layer list. 1039 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id); 1040 if (numLayers) { 1041 numLayers--; 1042 } 1043 } 1044 } 1045 } 1046 return getLayerIterator(id, numLayers); 1047} 1048 1049// Converts a PixelFormat to a human-readable string. Max 11 chars. 1050// (Could use a table of prefab String8 objects.) 1051static String8 getFormatStr(PixelFormat format) { 1052 switch (format) { 1053 case PIXEL_FORMAT_RGBA_8888: return String8("RGBA_8888"); 1054 case PIXEL_FORMAT_RGBX_8888: return String8("RGBx_8888"); 1055 case PIXEL_FORMAT_RGB_888: return String8("RGB_888"); 1056 case PIXEL_FORMAT_RGB_565: return String8("RGB_565"); 1057 case PIXEL_FORMAT_BGRA_8888: return String8("BGRA_8888"); 1058 case PIXEL_FORMAT_sRGB_A_8888: return String8("sRGB_A_8888"); 1059 case PIXEL_FORMAT_sRGB_X_8888: return String8("sRGB_x_8888"); 1060 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 1061 return String8("ImplDef"); 1062 default: 1063 String8 result; 1064 result.appendFormat("? %08x", format); 1065 return result; 1066 } 1067} 1068 1069void HWComposer::dump(String8& result) const { 1070 if (mHwc) { 1071 result.appendFormat("Hardware Composer state (version %08x):\n", hwcApiVersion(mHwc)); 1072 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync); 1073 for (size_t i=0 ; i<mNumDisplays ; i++) { 1074 const DisplayData& disp(mDisplayData[i]); 1075 if (!disp.connected) 1076 continue; 1077 1078 const Vector< sp<Layer> >& visibleLayersSortedByZ = 1079 mFlinger->getLayerSortedByZForHwcDisplay(i); 1080 1081 1082 result.appendFormat(" Display[%zd] configurations (* current):\n", i); 1083 for (size_t c = 0; c < disp.configs.size(); ++c) { 1084 const DisplayConfig& config(disp.configs[c]); 1085 result.appendFormat(" %s%zd: %ux%u, xdpi=%f, ydpi=%f, refresh=%" PRId64 "\n", 1086 c == disp.currentConfig ? "* " : "", c, config.width, config.height, 1087 config.xdpi, config.ydpi, config.refresh); 1088 } 1089 1090 if (disp.list) { 1091 result.appendFormat( 1092 " numHwLayers=%zu, flags=%08x\n", 1093 disp.list->numHwLayers, disp.list->flags); 1094 1095 result.append( 1096 " type | handle | hint | flag | tr | blnd | format | source crop (l,t,r,b) | frame | name \n" 1097 "-----------+----------+------+------+----+------+-------------+--------------------------------+------------------------+------\n"); 1098 // " _________ | ________ | ____ | ____ | __ | ____ | ___________ |_____._,_____._,_____._,_____._ |_____,_____,_____,_____ | ___... 1099 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) { 1100 const hwc_layer_1_t&l = disp.list->hwLayers[i]; 1101 int32_t format = -1; 1102 String8 name("unknown"); 1103 1104 if (i < visibleLayersSortedByZ.size()) { 1105 const sp<Layer>& layer(visibleLayersSortedByZ[i]); 1106 const sp<GraphicBuffer>& buffer( 1107 layer->getActiveBuffer()); 1108 if (buffer != NULL) { 1109 format = buffer->getPixelFormat(); 1110 } 1111 name = layer->getName(); 1112 } 1113 1114 int type = l.compositionType; 1115 if (type == HWC_FRAMEBUFFER_TARGET) { 1116 name = "HWC_FRAMEBUFFER_TARGET"; 1117 format = disp.format; 1118 } 1119 1120 static char const* compositionTypeName[] = { 1121 "GLES", 1122 "HWC", 1123 "BKGND", 1124 "FB TARGET", 1125 "UNKNOWN"}; 1126 if (type >= NELEM(compositionTypeName)) 1127 type = NELEM(compositionTypeName) - 1; 1128 1129 String8 formatStr = getFormatStr(format); 1130 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) { 1131 result.appendFormat( 1132 " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7.1f,%7.1f,%7.1f,%7.1f |%5d,%5d,%5d,%5d | %s\n", 1133 compositionTypeName[type], 1134 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(), 1135 l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom, 1136 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom, 1137 name.string()); 1138 } else { 1139 result.appendFormat( 1140 " %9s | %08" PRIxPTR " | %04x | %04x | %02x | %04x | %-11s |%7d,%7d,%7d,%7d |%5d,%5d,%5d,%5d | %s\n", 1141 compositionTypeName[type], 1142 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, formatStr.string(), 1143 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom, 1144 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom, 1145 name.string()); 1146 } 1147 } 1148 } 1149 } 1150 } 1151 1152 if (mHwc && mHwc->dump) { 1153 const size_t SIZE = 4096; 1154 char buffer[SIZE]; 1155 mHwc->dump(mHwc, buffer, SIZE); 1156 result.append(buffer); 1157 } 1158} 1159 1160// --------------------------------------------------------------------------- 1161 1162HWComposer::VSyncThread::VSyncThread(HWComposer& hwc) 1163 : mHwc(hwc), mEnabled(false), 1164 mNextFakeVSync(0), 1165 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY)) 1166{ 1167} 1168 1169void HWComposer::VSyncThread::setEnabled(bool enabled) { 1170 Mutex::Autolock _l(mLock); 1171 if (mEnabled != enabled) { 1172 mEnabled = enabled; 1173 mCondition.signal(); 1174 } 1175} 1176 1177void HWComposer::VSyncThread::onFirstRef() { 1178 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); 1179} 1180 1181bool HWComposer::VSyncThread::threadLoop() { 1182 { // scope for lock 1183 Mutex::Autolock _l(mLock); 1184 while (!mEnabled) { 1185 mCondition.wait(mLock); 1186 } 1187 } 1188 1189 const nsecs_t period = mRefreshPeriod; 1190 const nsecs_t now = systemTime(CLOCK_MONOTONIC); 1191 nsecs_t next_vsync = mNextFakeVSync; 1192 nsecs_t sleep = next_vsync - now; 1193 if (sleep < 0) { 1194 // we missed, find where the next vsync should be 1195 sleep = (period - ((now - next_vsync) % period)); 1196 next_vsync = now + sleep; 1197 } 1198 mNextFakeVSync = next_vsync + period; 1199 1200 struct timespec spec; 1201 spec.tv_sec = next_vsync / 1000000000; 1202 spec.tv_nsec = next_vsync % 1000000000; 1203 1204 int err; 1205 do { 1206 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL); 1207 } while (err<0 && errno == EINTR); 1208 1209 if (err == 0) { 1210 mHwc.mEventHandler.onVSyncReceived(0, next_vsync); 1211 } 1212 1213 return true; 1214} 1215 1216HWComposer::DisplayData::DisplayData() 1217: configs(), 1218 currentConfig(0), 1219 format(HAL_PIXEL_FORMAT_RGBA_8888), 1220 connected(false), 1221 hasFbComp(false), hasOvComp(false), 1222 capacity(0), list(NULL), 1223 framebufferTarget(NULL), fbTargetHandle(0), 1224 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE), 1225 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE), 1226 events(0) 1227{} 1228 1229HWComposer::DisplayData::~DisplayData() { 1230 free(list); 1231} 1232 1233// --------------------------------------------------------------------------- 1234}; // namespace android 1235