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