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