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