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