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