HWComposer.cpp revision f435863467ab407f2a482604beed5fa6f0144c62
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/String8.h> 30#include <utils/Thread.h> 31#include <utils/Trace.h> 32#include <utils/Vector.h> 33 34#include <ui/GraphicBuffer.h> 35 36#include <hardware/hardware.h> 37#include <hardware/hwcomposer.h> 38 39#include <cutils/log.h> 40#include <cutils/properties.h> 41 42#include "Layer.h" // needed only for debugging 43#include "LayerBase.h" 44#include "HWComposer.h" 45#include "SurfaceFlinger.h" 46 47namespace android { 48 49static bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) { 50 return hwc->common.version >= version; 51} 52 53// --------------------------------------------------------------------------- 54 55struct HWComposer::cb_context { 56 struct callbacks : public hwc_procs_t { 57 // these are here to facilitate the transition when adding 58 // new callbacks (an implementation can check for NULL before 59 // calling a new callback). 60 void (*zero[4])(void); 61 }; 62 callbacks procs; 63 HWComposer* hwc; 64}; 65 66// --------------------------------------------------------------------------- 67 68HWComposer::HWComposer( 69 const sp<SurfaceFlinger>& flinger, 70 EventHandler& handler, 71 framebuffer_device_t const* fbDev) 72 : mFlinger(flinger), 73 mModule(0), mHwc(0), mNumDisplays(1), 74 mCBContext(new cb_context), 75 mEventHandler(handler), 76 mVSyncCount(0), mDebugForceFakeVSync(false) 77{ 78 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) { 79 mLists[i] = 0; 80 } 81 82 char value[PROPERTY_VALUE_MAX]; 83 property_get("debug.sf.no_hw_vsync", value, "0"); 84 mDebugForceFakeVSync = atoi(value); 85 86 bool needVSyncThread = true; 87 int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule); 88 ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID); 89 if (err == 0) { 90 err = hwc_open_1(mModule, &mHwc); 91 ALOGE_IF(err, "%s device failed to initialize (%s)", 92 HWC_HARDWARE_COMPOSER, strerror(-err)); 93 if (err == 0) { 94 if (mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) { 95 ALOGE("%s device version %#x too old, will not be used", 96 HWC_HARDWARE_COMPOSER, mHwc->common.version); 97 hwc_close_1(mHwc); 98 mHwc = NULL; 99 } 100 } 101 102 if (mHwc) { 103 if (mHwc->registerProcs) { 104 mCBContext->hwc = this; 105 mCBContext->procs.invalidate = &hook_invalidate; 106 mCBContext->procs.vsync = &hook_vsync; 107 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero)); 108 mHwc->registerProcs(mHwc, &mCBContext->procs); 109 } 110 111 // always turn vsync off when we start 112 needVSyncThread = false; 113 mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0); 114 115 int period; 116 if (mHwc->query(mHwc, HWC_VSYNC_PERIOD, &period) == NO_ERROR) { 117 mDisplayData[HWC_DISPLAY_PRIMARY].refresh = nsecs_t(period); 118 } 119 120 // these IDs are always reserved 121 for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) { 122 mAllocatedDisplayIDs.markBit(i); 123 // TODO: we query xdpi / ydpi / refresh 124 } 125 126 // the number of displays we actually have depends on the 127 // hw composer version 128 if (mHwc->common.version == HWC_DEVICE_API_VERSION_1_1) { 129 // 1.1 adds support for multiple displays 130 mNumDisplays = HWC_NUM_DISPLAY_TYPES; 131 } else if (mHwc->common.version > HWC_DEVICE_API_VERSION_1_1) { 132 // 1.2 adds support for virtual displays 133 mNumDisplays = MAX_DISPLAYS; 134 } 135 } 136 } 137 138 if (fbDev) { 139 // if we're here it means we are on version 1.0 140 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]); 141 disp.xdpi = fbDev->xdpi; 142 disp.ydpi = fbDev->ydpi; 143 if (disp.refresh == 0) { 144 disp.refresh = nsecs_t(1e9 / fbDev->fps); 145 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh); 146 } 147 if (disp.refresh == 0) { 148 disp.refresh = nsecs_t(1e9 / 60.0); 149 ALOGW("getting VSYNC period thin air: %lld", mDisplayData[HWC_DISPLAY_PRIMARY].refresh); 150 } 151 } 152 153 if (needVSyncThread) { 154 // we don't have VSYNC support, we need to fake it 155 mVSyncThread = new VSyncThread(*this); 156 } 157} 158 159HWComposer::~HWComposer() { 160 mHwc->eventControl(mHwc, 0, EVENT_VSYNC, 0); 161 if (mVSyncThread != NULL) { 162 mVSyncThread->requestExitAndWait(); 163 } 164 if (mHwc) { 165 hwc_close_1(mHwc); 166 } 167 delete mCBContext; 168} 169 170status_t HWComposer::initCheck() const { 171 return mHwc ? NO_ERROR : NO_INIT; 172} 173 174void HWComposer::hook_invalidate(const struct hwc_procs* procs) { 175 cb_context* ctx = reinterpret_cast<cb_context*>( 176 const_cast<hwc_procs_t*>(procs)); 177 ctx->hwc->invalidate(); 178} 179 180void HWComposer::hook_vsync(const struct hwc_procs* procs, int dpy, 181 int64_t timestamp) { 182 cb_context* ctx = reinterpret_cast<cb_context*>( 183 const_cast<hwc_procs_t*>(procs)); 184 ctx->hwc->vsync(dpy, timestamp); 185} 186 187void HWComposer::invalidate() { 188 mFlinger->repaintEverything(); 189} 190 191void HWComposer::vsync(int dpy, int64_t timestamp) { 192 ATRACE_INT("VSYNC", ++mVSyncCount&1); 193 mEventHandler.onVSyncReceived(dpy, timestamp); 194 Mutex::Autolock _l(mLock); 195 mLastHwVSync = timestamp; 196} 197 198int32_t HWComposer::allocateDisplayId() { 199 if (mAllocatedDisplayIDs.count() >= mNumDisplays) { 200 return NO_MEMORY; 201 } 202 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit(); 203 mAllocatedDisplayIDs.markBit(id); 204 return id; 205} 206 207status_t HWComposer::freeDisplayId(int32_t id) { 208 if (id < HWC_NUM_DISPLAY_TYPES) { 209 // cannot free the reserved IDs 210 return BAD_VALUE; 211 } 212 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 213 return BAD_INDEX; 214 } 215 mAllocatedDisplayIDs.clearBit(id); 216 return NO_ERROR; 217} 218 219nsecs_t HWComposer::getRefreshPeriod() const { 220 return mDisplayData[HWC_DISPLAY_PRIMARY].refresh; 221} 222 223nsecs_t HWComposer::getRefreshTimestamp() const { 224 // this returns the last refresh timestamp. 225 // if the last one is not available, we estimate it based on 226 // the refresh period and whatever closest timestamp we have. 227 Mutex::Autolock _l(mLock); 228 nsecs_t now = systemTime(CLOCK_MONOTONIC); 229 return now - ((now - mLastHwVSync) % mDisplayData[HWC_DISPLAY_PRIMARY].refresh); 230} 231 232float HWComposer::getDpiX() const { 233 return mDisplayData[HWC_DISPLAY_PRIMARY].xdpi; 234} 235 236float HWComposer::getDpiY() const { 237 return mDisplayData[HWC_DISPLAY_PRIMARY].ydpi; 238} 239 240void HWComposer::eventControl(int event, int enabled) { 241 status_t err = NO_ERROR; 242 if (mHwc) { 243 if (!mDebugForceFakeVSync) { 244 err = mHwc->eventControl(mHwc, 0, event, enabled); 245 // error here should not happen -- not sure what we should 246 // do if it does. 247 ALOGE_IF(err, "eventControl(%d, %d) failed %s", 248 event, enabled, strerror(-err)); 249 } 250 } 251 252 if (err == NO_ERROR && mVSyncThread != NULL) { 253 mVSyncThread->setEnabled(enabled); 254 } 255} 256 257status_t HWComposer::createWorkList(int32_t id, size_t numLayers) { 258 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 259 return BAD_INDEX; 260 } 261 262 if (mHwc) { 263 DisplayData& disp(mDisplayData[id]); 264 if (disp.capacity < numLayers) { 265 const size_t size = sizeof(hwc_display_contents_1_t) 266 + numLayers * sizeof(hwc_layer_1_t); 267 free(disp.list); 268 disp.list = (hwc_display_contents_1_t*)malloc(size); 269 disp.capacity = numLayers; 270 } 271 disp.list->flags = HWC_GEOMETRY_CHANGED; 272 disp.list->numHwLayers = numLayers; 273 disp.list->flipFenceFd = -1; 274 } 275 return NO_ERROR; 276} 277 278status_t HWComposer::prepare() { 279 for (size_t i=0 ; i<mNumDisplays ; i++) { 280 mLists[i] = mDisplayData[i].list; 281 if (mLists[i]) { 282 mLists[i]->dpy = EGL_NO_DISPLAY; 283 mLists[i]->sur = EGL_NO_SURFACE; 284 } 285 } 286 int err = mHwc->prepare(mHwc, mNumDisplays, mLists); 287 if (err == NO_ERROR) { 288 // here we're just making sure that "skip" layers are set 289 // to HWC_FRAMEBUFFER and we're also counting how many layers 290 // we have of each type. 291 for (size_t i=0 ; i<mNumDisplays ; i++) { 292 DisplayData& disp(mDisplayData[i]); 293 disp.hasFbComp = false; 294 disp.hasOvComp = false; 295 if (disp.list) { 296 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) { 297 hwc_layer_1_t& l = disp.list->hwLayers[i]; 298 if (l.flags & HWC_SKIP_LAYER) { 299 l.compositionType = HWC_FRAMEBUFFER; 300 } 301 if (l.compositionType == HWC_FRAMEBUFFER) { 302 disp.hasFbComp = true; 303 } 304 if (l.compositionType == HWC_OVERLAY) { 305 disp.hasOvComp = true; 306 } 307 } 308 } 309 } 310 } 311 return (status_t)err; 312} 313 314bool HWComposer::hasHwcComposition(int32_t id) const { 315 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 316 return false; 317 return mDisplayData[id].hasOvComp; 318} 319 320bool HWComposer::hasGlesComposition(int32_t id) const { 321 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) 322 return false; 323 return mDisplayData[id].hasFbComp; 324} 325 326status_t HWComposer::commit() { 327 int err = NO_ERROR; 328 if (mHwc) { 329 if (mHwc->common.version == HWC_DEVICE_API_VERSION_1_0) { 330 // On version 1.0, the OpenGL ES target surface is communicated 331 // by the (dpy, sur) fields and we are guaranteed to have only 332 // a single display. 333 mLists[0]->dpy = eglGetCurrentDisplay(); 334 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW); 335 } 336 337 err = mHwc->set(mHwc, mNumDisplays, mLists); 338 339 for (size_t i=0 ; i<mNumDisplays ; i++) { 340 DisplayData& disp(mDisplayData[i]); 341 if (disp.list) { 342 if (disp.list->flipFenceFd != -1) { 343 close(disp.list->flipFenceFd); 344 disp.list->flipFenceFd = -1; 345 } 346 disp.list->flags &= ~HWC_GEOMETRY_CHANGED; 347 } 348 } 349 } 350 return (status_t)err; 351} 352 353status_t HWComposer::release() const { 354 if (mHwc) { 355 mHwc->eventControl(mHwc, 0, HWC_EVENT_VSYNC, 0); 356 return (status_t)mHwc->blank(mHwc, 0, 1); 357 } 358 return NO_ERROR; 359} 360 361status_t HWComposer::acquire() const { 362 if (mHwc) { 363 return (status_t)mHwc->blank(mHwc, 0, 0); 364 } 365 return NO_ERROR; 366} 367 368size_t HWComposer::getNumLayers(int32_t id) const { 369 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 370 return 0; 371 } 372 return (mHwc && mDisplayData[id].list) ? 373 mDisplayData[id].list->numHwLayers : 0; 374} 375 376/* 377 * Helper template to implement a concrete HWCLayer 378 * This holds the pointer to the concrete hwc layer type 379 * and implements the "iterable" side of HWCLayer. 380 */ 381template<typename CONCRETE, typename HWCTYPE> 382class Iterable : public HWComposer::HWCLayer { 383protected: 384 HWCTYPE* const mLayerList; 385 HWCTYPE* mCurrentLayer; 386 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { } 387 inline HWCTYPE const * getLayer() const { return mCurrentLayer; } 388 inline HWCTYPE* getLayer() { return mCurrentLayer; } 389 virtual ~Iterable() { } 390private: 391 // returns a copy of ourselves 392 virtual HWComposer::HWCLayer* dup() { 393 return new CONCRETE( static_cast<const CONCRETE&>(*this) ); 394 } 395 virtual status_t setLayer(size_t index) { 396 mCurrentLayer = &mLayerList[index]; 397 return NO_ERROR; 398 } 399}; 400 401/* 402 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0. 403 * This implements the HWCLayer side of HWCIterableLayer. 404 */ 405class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> { 406public: 407 HWCLayerVersion1(hwc_layer_1_t* layer) 408 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { } 409 410 virtual int32_t getCompositionType() const { 411 return getLayer()->compositionType; 412 } 413 virtual uint32_t getHints() const { 414 return getLayer()->hints; 415 } 416 virtual int getAndResetReleaseFenceFd() { 417 int fd = getLayer()->releaseFenceFd; 418 getLayer()->releaseFenceFd = -1; 419 return fd; 420 } 421 virtual void setAcquireFenceFd(int fenceFd) { 422 getLayer()->acquireFenceFd = fenceFd; 423 } 424 425 virtual void setDefaultState() { 426 getLayer()->compositionType = HWC_FRAMEBUFFER; 427 getLayer()->hints = 0; 428 getLayer()->flags = HWC_SKIP_LAYER; 429 getLayer()->transform = 0; 430 getLayer()->blending = HWC_BLENDING_NONE; 431 getLayer()->visibleRegionScreen.numRects = 0; 432 getLayer()->visibleRegionScreen.rects = NULL; 433 getLayer()->acquireFenceFd = -1; 434 getLayer()->releaseFenceFd = -1; 435 } 436 virtual void setSkip(bool skip) { 437 if (skip) { 438 getLayer()->flags |= HWC_SKIP_LAYER; 439 } else { 440 getLayer()->flags &= ~HWC_SKIP_LAYER; 441 } 442 } 443 virtual void setBlending(uint32_t blending) { 444 getLayer()->blending = blending; 445 } 446 virtual void setTransform(uint32_t transform) { 447 getLayer()->transform = transform; 448 } 449 virtual void setFrame(const Rect& frame) { 450 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame; 451 } 452 virtual void setCrop(const Rect& crop) { 453 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop; 454 } 455 virtual void setVisibleRegionScreen(const Region& reg) { 456 getLayer()->visibleRegionScreen.rects = 457 reinterpret_cast<hwc_rect_t const *>( 458 reg.getArray(&getLayer()->visibleRegionScreen.numRects)); 459 } 460 virtual void setBuffer(const sp<GraphicBuffer>& buffer) { 461 if (buffer == 0 || buffer->handle == 0) { 462 getLayer()->compositionType = HWC_FRAMEBUFFER; 463 getLayer()->flags |= HWC_SKIP_LAYER; 464 getLayer()->handle = 0; 465 } else { 466 getLayer()->handle = buffer->handle; 467 } 468 } 469}; 470 471/* 472 * returns an iterator initialized at a given index in the layer list 473 */ 474HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) { 475 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) { 476 return LayerListIterator(); 477 } 478 const DisplayData& disp(mDisplayData[id]); 479 if (!mHwc || !disp.list || index > disp.list->numHwLayers) { 480 return LayerListIterator(); 481 } 482 return LayerListIterator(new HWCLayerVersion1(disp.list->hwLayers), index); 483} 484 485/* 486 * returns an iterator on the beginning of the layer list 487 */ 488HWComposer::LayerListIterator HWComposer::begin(int32_t id) { 489 return getLayerIterator(id, 0); 490} 491 492/* 493 * returns an iterator on the end of the layer list 494 */ 495HWComposer::LayerListIterator HWComposer::end(int32_t id) { 496 return getLayerIterator(id, getNumLayers(id)); 497} 498 499void HWComposer::dump(String8& result, char* buffer, size_t SIZE, 500 const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const { 501 if (mHwc) { 502 result.append("Hardware Composer state:\n"); 503 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync); 504 for (size_t i=0 ; i<mNumDisplays ; i++) { 505 const DisplayData& disp(mDisplayData[i]); 506 if (disp.list) { 507 result.appendFormat(" id=%d, numHwLayers=%u, flags=%08x\n", 508 i, disp.list->numHwLayers, disp.list->flags); 509 result.append( 510 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n" 511 "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n"); 512 // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____] 513 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) { 514 const hwc_layer_1_t&l = disp.list->hwLayers[i]; 515 const sp<LayerBase> layer(visibleLayersSortedByZ[i]); 516 int32_t format = -1; 517 if (layer->getLayer() != NULL) { 518 const sp<GraphicBuffer>& buffer( 519 layer->getLayer()->getActiveBuffer()); 520 if (buffer != NULL) { 521 format = buffer->getPixelFormat(); 522 } 523 } 524 result.appendFormat( 525 " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n", 526 l.compositionType ? "OVERLAY" : "FB", 527 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format, 528 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom, 529 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom, 530 layer->getName().string()); 531 } 532 } 533 } 534 } 535 536 if (mHwc && mHwc->dump) { 537 mHwc->dump(mHwc, buffer, SIZE); 538 result.append(buffer); 539 } 540} 541 542// --------------------------------------------------------------------------- 543 544HWComposer::VSyncThread::VSyncThread(HWComposer& hwc) 545 : mHwc(hwc), mEnabled(false), 546 mNextFakeVSync(0), 547 mRefreshPeriod(hwc.getRefreshPeriod()) 548{ 549} 550 551void HWComposer::VSyncThread::setEnabled(bool enabled) { 552 Mutex::Autolock _l(mLock); 553 mEnabled = enabled; 554 mCondition.signal(); 555} 556 557void HWComposer::VSyncThread::onFirstRef() { 558 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); 559} 560 561bool HWComposer::VSyncThread::threadLoop() { 562 { // scope for lock 563 Mutex::Autolock _l(mLock); 564 while (!mEnabled) { 565 mCondition.wait(mLock); 566 } 567 } 568 569 const nsecs_t period = mRefreshPeriod; 570 const nsecs_t now = systemTime(CLOCK_MONOTONIC); 571 nsecs_t next_vsync = mNextFakeVSync; 572 nsecs_t sleep = next_vsync - now; 573 if (sleep < 0) { 574 // we missed, find where the next vsync should be 575 sleep = (period - ((now - next_vsync) % period)); 576 next_vsync = now + sleep; 577 } 578 mNextFakeVSync = next_vsync + period; 579 580 struct timespec spec; 581 spec.tv_sec = next_vsync / 1000000000; 582 spec.tv_nsec = next_vsync % 1000000000; 583 584 int err; 585 do { 586 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL); 587 } while (err<0 && errno == EINTR); 588 589 if (err == 0) { 590 mHwc.mEventHandler.onVSyncReceived(0, next_vsync); 591 } 592 593 return true; 594} 595 596// --------------------------------------------------------------------------- 597}; // namespace android 598