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