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