2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 // Uncomment this to remove support for HWC_DEVICE_API_VERSION_0_3 and older
20 #define HWC_REMOVE_DEPRECATED_VERSIONS 1
26 #include <sys/types.h>
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>
35 #include <ui/GraphicBuffer.h>
37 #include <hardware/hardware.h>
38 #include <hardware/hwcomposer.h>
40 #include <cutils/log.h>
41 #include <cutils/properties.h>
43 #include "Layer.h" // needed only for debugging
44 #include "HWComposer.h"
45 #include "SurfaceFlinger.h"
46 #include <utils/CallStack.h>
50 #define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
52 static 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;
57 static 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;
62 static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
64 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
67 // ---------------------------------------------------------------------------
69 struct 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);
80 // ---------------------------------------------------------------------------
82 HWComposer::HWComposer(
83 const sp<SurfaceFlinger>& flinger,
84 EventHandler& handler)
86 mFbDev(0), mHwc(0), mNumDisplays(1),
87 mCBContext(new cb_context),
88 mEventHandler(handler),
89 mVSyncCount(0), mDebugForceFakeVSync(false)
91 for (size_t i =0 ; i<MAX_DISPLAYS ; i++) {
95 char value[PROPERTY_VALUE_MAX];
96 property_get("debug.sf.no_hw_vsync", value, "0");
97 mDebugForceFakeVSync = atoi(value);
99 bool needVSyncThread = true;
101 // Note: some devices may insist that the FB HAL be opened before HWC.
105 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
106 // close FB HAL if we don't needed it.
107 // FIXME: this is temporary until we're not forced to open FB HAL
109 framebuffer_close(mFbDev);
113 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
114 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
116 ALOGE("ERROR: failed to open framebuffer, aborting");
120 // these display IDs are always reserved
121 for (size_t i=0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
122 mAllocatedDisplayIDs.markBit(i);
126 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
127 (hwcApiVersion(mHwc) >> 24) & 0xff,
128 (hwcApiVersion(mHwc) >> 16) & 0xff);
129 if (mHwc->registerProcs) {
130 mCBContext->hwc = this;
131 mCBContext->procs.invalidate = &hook_invalidate;
132 mCBContext->procs.vsync = &hook_vsync;
133 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
134 mCBContext->procs.hotplug = &hook_hotplug;
136 mCBContext->procs.hotplug = NULL;
137 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
138 mHwc->registerProcs(mHwc, &mCBContext->procs);
141 // don't need a vsync thread if we have a hardware composer
142 needVSyncThread = false;
143 // always turn vsync off when we start
144 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
146 // the number of displays we actually have depends on the
147 // hw composer version
148 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
149 // 1.2 adds support for virtual displays
150 mNumDisplays = MAX_DISPLAYS;
151 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
152 // 1.1 adds support for multiple displays
153 mNumDisplays = HWC_NUM_DISPLAY_TYPES;
160 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
161 "should only have fbdev if no hwc or hwc is 1.0");
163 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
164 disp.connected = true;
165 disp.width = mFbDev->width;
166 disp.height = mFbDev->height;
167 disp.format = mFbDev->format;
168 disp.xdpi = mFbDev->xdpi;
169 disp.ydpi = mFbDev->ydpi;
170 if (disp.refresh == 0) {
171 disp.refresh = nsecs_t(1e9 / mFbDev->fps);
172 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
174 if (disp.refresh == 0) {
175 disp.refresh = nsecs_t(1e9 / 60.0);
176 ALOGW("getting VSYNC period from thin air: %lld",
177 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
180 // here we're guaranteed to have at least HWC 1.1
181 for (size_t i =0 ; i<HWC_NUM_DISPLAY_TYPES ; i++) {
182 queryDisplayProperties(i);
186 if (needVSyncThread) {
187 // we don't have VSYNC support, we need to fake it
188 mVSyncThread = new VSyncThread(*this);
192 HWComposer::~HWComposer() {
194 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
196 if (mVSyncThread != NULL) {
197 mVSyncThread->requestExitAndWait();
203 framebuffer_close(mFbDev);
208 // Load and prepare the hardware composer module. Sets mHwc.
209 void HWComposer::loadHwcModule()
211 hw_module_t const* module;
213 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
214 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
218 int err = hwc_open_1(module, &mHwc);
220 ALOGE("%s device failed to initialize (%s)",
221 HWC_HARDWARE_COMPOSER, strerror(-err));
225 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_0) ||
226 hwcHeaderVersion(mHwc) < MIN_HWC_HEADER_VERSION ||
227 hwcHeaderVersion(mHwc) > HWC_HEADER_VERSION) {
228 ALOGE("%s device version %#x unsupported, will not be used",
229 HWC_HARDWARE_COMPOSER, mHwc->common.version);
236 // Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
237 void HWComposer::loadFbHalModule()
239 hw_module_t const* module;
241 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) != 0) {
242 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
246 int err = framebuffer_open(module, &mFbDev);
248 ALOGE("framebuffer_open failed (%s)", strerror(-err));
253 status_t HWComposer::initCheck() const {
254 return mHwc ? NO_ERROR : NO_INIT;
257 void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
258 cb_context* ctx = reinterpret_cast<cb_context*>(
259 const_cast<hwc_procs_t*>(procs));
260 ctx->hwc->invalidate();
263 void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
265 cb_context* ctx = reinterpret_cast<cb_context*>(
266 const_cast<hwc_procs_t*>(procs));
267 ctx->hwc->vsync(disp, timestamp);
270 void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
272 cb_context* ctx = reinterpret_cast<cb_context*>(
273 const_cast<hwc_procs_t*>(procs));
274 ctx->hwc->hotplug(disp, connected);
277 void HWComposer::invalidate() {
278 mFlinger->repaintEverything();
281 void HWComposer::vsync(int disp, int64_t timestamp) {
282 ATRACE_INT("VSYNC", ++mVSyncCount&1);
283 mEventHandler.onVSyncReceived(disp, timestamp);
284 Mutex::Autolock _l(mLock);
285 mLastHwVSync = timestamp;
288 void HWComposer::hotplug(int disp, int connected) {
289 if (disp == HWC_DISPLAY_PRIMARY || disp >= HWC_NUM_DISPLAY_TYPES) {
290 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
294 queryDisplayProperties(disp);
295 mEventHandler.onHotplugReceived(disp, bool(connected));
298 static const uint32_t DISPLAY_ATTRIBUTES[] = {
299 HWC_DISPLAY_VSYNC_PERIOD,
304 HWC_DISPLAY_NO_ATTRIBUTE,
306 #define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
308 // http://developer.android.com/reference/android/util/DisplayMetrics.html
309 #define ANDROID_DENSITY_TV 213
310 #define ANDROID_DENSITY_XHIGH 320
312 status_t HWComposer::queryDisplayProperties(int disp) {
314 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
316 // use zero as default value for unspecified attributes
317 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
318 memset(values, 0, sizeof(values));
321 size_t numConfigs = 1;
322 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
323 if (err != NO_ERROR) {
324 // this can happen if an unpluggable display is not connected
325 mDisplayData[disp].connected = false;
329 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
330 if (err != NO_ERROR) {
331 // we can't get this display's info. turn it off.
332 mDisplayData[disp].connected = false;
336 int32_t w = 0, h = 0;
337 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
338 switch (DISPLAY_ATTRIBUTES[i]) {
339 case HWC_DISPLAY_VSYNC_PERIOD:
340 mDisplayData[disp].refresh = nsecs_t(values[i]);
342 case HWC_DISPLAY_WIDTH:
343 mDisplayData[disp].width = values[i];
345 case HWC_DISPLAY_HEIGHT:
346 mDisplayData[disp].height = values[i];
348 case HWC_DISPLAY_DPI_X:
349 mDisplayData[disp].xdpi = values[i] / 1000.0f;
351 case HWC_DISPLAY_DPI_Y:
352 mDisplayData[disp].ydpi = values[i] / 1000.0f;
355 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
356 i, DISPLAY_ATTRIBUTES[i]);
361 // FIXME: what should we set the format to?
362 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
363 mDisplayData[disp].connected = true;
364 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
365 // is there anything smarter we can do?
367 mDisplayData[disp].xdpi = ANDROID_DENSITY_XHIGH;
368 mDisplayData[disp].ydpi = ANDROID_DENSITY_XHIGH;
370 mDisplayData[disp].xdpi = ANDROID_DENSITY_TV;
371 mDisplayData[disp].ydpi = ANDROID_DENSITY_TV;
377 int32_t HWComposer::allocateDisplayId() {
378 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
381 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
382 mAllocatedDisplayIDs.markBit(id);
386 status_t HWComposer::freeDisplayId(int32_t id) {
387 if (id < HWC_NUM_DISPLAY_TYPES) {
388 // cannot free the reserved IDs
391 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
394 mAllocatedDisplayIDs.clearBit(id);
398 nsecs_t HWComposer::getRefreshPeriod(int disp) const {
399 return mDisplayData[disp].refresh;
402 nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
403 // this returns the last refresh timestamp.
404 // if the last one is not available, we estimate it based on
405 // the refresh period and whatever closest timestamp we have.
406 Mutex::Autolock _l(mLock);
407 nsecs_t now = systemTime(CLOCK_MONOTONIC);
408 return now - ((now - mLastHwVSync) % mDisplayData[disp].refresh);
411 sp<Fence> HWComposer::getDisplayFence(int disp) const {
412 return mDisplayData[disp].lastDisplayFence;
416 uint32_t HWComposer::getWidth(int disp) const {
417 return mDisplayData[disp].width;
420 uint32_t HWComposer::getHeight(int disp) const {
421 return mDisplayData[disp].height;
424 uint32_t HWComposer::getFormat(int disp) const {
425 return mDisplayData[disp].format;
428 float HWComposer::getDpiX(int disp) const {
429 return mDisplayData[disp].xdpi;
432 float HWComposer::getDpiY(int disp) const {
433 return mDisplayData[disp].ydpi;
436 bool HWComposer::isConnected(int disp) const {
437 return mDisplayData[disp].connected;
440 void HWComposer::eventControl(int disp, int event, int enabled) {
441 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
442 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
443 event, disp, enabled);
446 if (event != EVENT_VSYNC) {
447 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
448 event, disp, enabled);
451 status_t err = NO_ERROR;
452 if (mHwc && !mDebugForceFakeVSync) {
453 // NOTE: we use our own internal lock here because we have to call
454 // into the HWC with the lock held, and we want to make sure
455 // that even if HWC blocks (which it shouldn't), it won't
456 // affect other threads.
457 Mutex::Autolock _l(mEventControlLock);
458 const int32_t eventBit = 1UL << event;
459 const int32_t newValue = enabled ? eventBit : 0;
460 const int32_t oldValue = mDisplayData[disp].events & eventBit;
461 if (newValue != oldValue) {
463 err = mHwc->eventControl(mHwc, disp, event, enabled);
465 int32_t& events(mDisplayData[disp].events);
466 events = (events & ~eventBit) | newValue;
469 // error here should not happen -- not sure what we should
471 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
472 event, enabled, strerror(-err));
475 if (err == NO_ERROR && mVSyncThread != NULL) {
476 mVSyncThread->setEnabled(enabled);
480 status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
481 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
486 DisplayData& disp(mDisplayData[id]);
487 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
488 // we need space for the HWC_FRAMEBUFFER_TARGET
491 if (disp.capacity < numLayers || disp.list == NULL) {
492 size_t size = sizeof(hwc_display_contents_1_t)
493 + numLayers * sizeof(hwc_layer_1_t);
495 disp.list = (hwc_display_contents_1_t*)malloc(size);
496 disp.capacity = numLayers;
498 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
499 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
500 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
501 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
502 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
503 disp.framebufferTarget->hints = 0;
504 disp.framebufferTarget->flags = 0;
505 disp.framebufferTarget->handle = disp.fbTargetHandle;
506 disp.framebufferTarget->transform = 0;
507 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
508 disp.framebufferTarget->sourceCrop = r;
509 disp.framebufferTarget->displayFrame = r;
510 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
511 disp.framebufferTarget->visibleRegionScreen.rects =
512 &disp.framebufferTarget->displayFrame;
513 disp.framebufferTarget->acquireFenceFd = -1;
514 disp.framebufferTarget->releaseFenceFd = -1;
516 disp.list->retireFenceFd = -1;
517 disp.list->flags = HWC_GEOMETRY_CHANGED;
518 disp.list->numHwLayers = numLayers;
523 status_t HWComposer::setFramebufferTarget(int32_t id,
524 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
525 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
528 DisplayData& disp(mDisplayData[id]);
529 if (!disp.framebufferTarget) {
530 // this should never happen, but apparently eglCreateWindowSurface()
531 // triggers a Surface::queueBuffer() on some
532 // devices (!?) -- log and ignore.
533 ALOGE("HWComposer: framebufferTarget is null");
540 int acquireFenceFd = -1;
541 if (acquireFence->isValid()) {
542 acquireFenceFd = acquireFence->dup();
545 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
546 disp.fbTargetHandle = buf->handle;
547 disp.framebufferTarget->handle = disp.fbTargetHandle;
548 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
552 status_t HWComposer::prepare() {
553 for (size_t i=0 ; i<mNumDisplays ; i++) {
554 DisplayData& disp(mDisplayData[i]);
555 if (disp.framebufferTarget) {
556 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
557 // DO NOT reset the handle field to NULL, because it's possible
558 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
559 // in which case, we should continue to use the same buffer.
560 LOG_FATAL_IF(disp.list == NULL);
561 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
563 if (!disp.connected && disp.list != NULL) {
564 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
565 i, disp.list->numHwLayers);
567 mLists[i] = disp.list;
569 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
570 mLists[i]->outbuf = NULL;
571 mLists[i]->outbufAcquireFenceFd = -1;
572 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
573 // garbage data to catch improper use
574 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
575 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
577 mLists[i]->dpy = EGL_NO_DISPLAY;
578 mLists[i]->sur = EGL_NO_SURFACE;
583 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
584 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
586 if (err == NO_ERROR) {
587 // here we're just making sure that "skip" layers are set
588 // to HWC_FRAMEBUFFER and we're also counting how many layers
589 // we have of each type.
590 for (size_t i=0 ; i<mNumDisplays ; i++) {
591 DisplayData& disp(mDisplayData[i]);
592 disp.hasFbComp = false;
593 disp.hasOvComp = false;
595 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
596 hwc_layer_1_t& l = disp.list->hwLayers[i];
598 //ALOGD("prepare: %d, type=%d, handle=%p",
599 // i, l.compositionType, l.handle);
601 if (l.flags & HWC_SKIP_LAYER) {
602 l.compositionType = HWC_FRAMEBUFFER;
604 if (l.compositionType == HWC_FRAMEBUFFER) {
605 disp.hasFbComp = true;
607 if (l.compositionType == HWC_OVERLAY) {
608 disp.hasOvComp = true;
614 return (status_t)err;
617 bool HWComposer::hasHwcComposition(int32_t id) const {
618 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
620 return mDisplayData[id].hasOvComp;
623 bool HWComposer::hasGlesComposition(int32_t id) const {
624 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
626 return mDisplayData[id].hasFbComp;
629 int HWComposer::getAndResetReleaseFenceFd(int32_t id) {
630 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
633 int fd = INVALID_OPERATION;
634 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
635 const DisplayData& disp(mDisplayData[id]);
636 if (disp.framebufferTarget) {
637 fd = disp.framebufferTarget->releaseFenceFd;
638 disp.framebufferTarget->acquireFenceFd = -1;
639 disp.framebufferTarget->releaseFenceFd = -1;
645 status_t HWComposer::commit() {
648 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
649 // On version 1.0, the OpenGL ES target surface is communicated
650 // by the (dpy, sur) fields and we are guaranteed to have only
652 mLists[0]->dpy = eglGetCurrentDisplay();
653 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
656 err = mHwc->set(mHwc, mNumDisplays, mLists);
658 for (size_t i=0 ; i<mNumDisplays ; i++) {
659 DisplayData& disp(mDisplayData[i]);
660 disp.lastDisplayFence = disp.lastRetireFence;
661 disp.lastRetireFence = Fence::NO_FENCE;
663 if (disp.list->retireFenceFd != -1) {
664 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
665 disp.list->retireFenceFd = -1;
667 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
671 return (status_t)err;
674 status_t HWComposer::release(int disp) {
675 LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
677 eventControl(disp, HWC_EVENT_VSYNC, 0);
678 return (status_t)mHwc->blank(mHwc, disp, 1);
683 status_t HWComposer::acquire(int disp) {
684 LOG_FATAL_IF(disp >= HWC_NUM_DISPLAY_TYPES);
686 return (status_t)mHwc->blank(mHwc, disp, 0);
691 void HWComposer::disconnectDisplay(int disp) {
692 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
693 if (disp >= HWC_NUM_DISPLAY_TYPES) {
694 // nothing to do for these yet
697 DisplayData& dd(mDisplayData[disp]);
698 if (dd.list != NULL) {
701 dd.framebufferTarget = NULL; // points into dd.list
702 dd.fbTargetHandle = NULL;
706 int HWComposer::getVisualID() const {
707 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
708 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
709 // is supported by the implementation. we can only be in this case
710 // if we have HWC 1.1
711 return HAL_PIXEL_FORMAT_RGBA_8888;
712 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
714 return mFbDev->format;
718 bool HWComposer::supportsFramebufferTarget() const {
719 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
722 int HWComposer::fbPost(int32_t id,
723 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
724 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
725 return setFramebufferTarget(id, acquireFence, buffer);
727 acquireFence->waitForever(1000, "HWComposer::fbPost");
728 return mFbDev->post(mFbDev, buffer->handle);
732 int HWComposer::fbCompositionComplete() {
733 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
736 if (mFbDev->compositionComplete) {
737 return mFbDev->compositionComplete(mFbDev);
739 return INVALID_OPERATION;
743 void HWComposer::fbDump(String8& result) {
744 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
745 const size_t SIZE = 4096;
747 mFbDev->dump(mFbDev, buffer, SIZE);
748 result.append(buffer);
753 * Helper template to implement a concrete HWCLayer
754 * This holds the pointer to the concrete hwc layer type
755 * and implements the "iterable" side of HWCLayer.
757 template<typename CONCRETE, typename HWCTYPE>
758 class Iterable : public HWComposer::HWCLayer {
760 HWCTYPE* const mLayerList;
761 HWCTYPE* mCurrentLayer;
762 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
763 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
764 inline HWCTYPE* getLayer() { return mCurrentLayer; }
765 virtual ~Iterable() { }
767 // returns a copy of ourselves
768 virtual HWComposer::HWCLayer* dup() {
769 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
771 virtual status_t setLayer(size_t index) {
772 mCurrentLayer = &mLayerList[index];
778 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
779 * This implements the HWCLayer side of HWCIterableLayer.
781 class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
782 struct hwc_composer_device_1* mHwc;
784 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
785 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
787 virtual int32_t getCompositionType() const {
788 return getLayer()->compositionType;
790 virtual uint32_t getHints() const {
791 return getLayer()->hints;
793 virtual int getAndResetReleaseFenceFd() {
794 int fd = getLayer()->releaseFenceFd;
795 getLayer()->releaseFenceFd = -1;
798 virtual void setAcquireFenceFd(int fenceFd) {
799 getLayer()->acquireFenceFd = fenceFd;
801 virtual void setPerFrameDefaultState() {
802 //getLayer()->compositionType = HWC_FRAMEBUFFER;
804 virtual void setPlaneAlpha(uint8_t alpha) {
805 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
806 getLayer()->planeAlpha = alpha;
809 getLayer()->flags |= HWC_SKIP_LAYER;
813 virtual void setDefaultState() {
814 hwc_layer_1_t* const l = getLayer();
815 l->compositionType = HWC_FRAMEBUFFER;
817 l->flags = HWC_SKIP_LAYER;
820 l->blending = HWC_BLENDING_NONE;
821 l->visibleRegionScreen.numRects = 0;
822 l->visibleRegionScreen.rects = NULL;
823 l->acquireFenceFd = -1;
824 l->releaseFenceFd = -1;
825 l->planeAlpha = 0xFF;
827 virtual void setSkip(bool skip) {
829 getLayer()->flags |= HWC_SKIP_LAYER;
831 getLayer()->flags &= ~HWC_SKIP_LAYER;
834 virtual void setBlending(uint32_t blending) {
835 getLayer()->blending = blending;
837 virtual void setTransform(uint32_t transform) {
838 getLayer()->transform = transform;
840 virtual void setFrame(const Rect& frame) {
841 reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame;
843 virtual void setCrop(const Rect& crop) {
844 reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop;
846 virtual void setVisibleRegionScreen(const Region& reg) {
847 // Region::getSharedBuffer creates a reference to the underlying
848 // SharedBuffer of this Region, this reference is freed
850 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
851 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
852 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
854 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
855 if (buffer == 0 || buffer->handle == 0) {
856 getLayer()->compositionType = HWC_FRAMEBUFFER;
857 getLayer()->flags |= HWC_SKIP_LAYER;
858 getLayer()->handle = 0;
860 getLayer()->handle = buffer->handle;
863 virtual void onDisplayed() {
864 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
865 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
868 // not technically needed but safer
869 visibleRegion.numRects = 0;
870 visibleRegion.rects = NULL;
873 getLayer()->acquireFenceFd = -1;
878 * returns an iterator initialized at a given index in the layer list
880 HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
881 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
882 return LayerListIterator();
884 const DisplayData& disp(mDisplayData[id]);
885 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
886 return LayerListIterator();
888 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
892 * returns an iterator on the beginning of the layer list
894 HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
895 return getLayerIterator(id, 0);
899 * returns an iterator on the end of the layer list
901 HWComposer::LayerListIterator HWComposer::end(int32_t id) {
902 size_t numLayers = 0;
903 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
904 const DisplayData& disp(mDisplayData[id]);
905 if (mHwc && disp.list) {
906 numLayers = disp.list->numHwLayers;
907 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
908 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
909 // which we ignore when iterating through the layer list.
910 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
917 return getLayerIterator(id, numLayers);
920 void HWComposer::dump(String8& result, char* buffer, size_t SIZE) const {
922 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
923 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
924 for (size_t i=0 ; i<mNumDisplays ; i++) {
925 const DisplayData& disp(mDisplayData[i]);
927 const Vector< sp<Layer> >& visibleLayersSortedByZ =
928 mFlinger->getLayerSortedByZForHwcDisplay(i);
930 if (disp.connected) {
932 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
933 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
936 if (disp.list && disp.connected) {
938 " numHwLayers=%u, flags=%08x\n",
939 disp.list->numHwLayers, disp.list->flags);
942 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
943 "------------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n");
944 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____]
945 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
946 const hwc_layer_1_t&l = disp.list->hwLayers[i];
948 String8 name("unknown");
950 if (i < visibleLayersSortedByZ.size()) {
951 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
952 const sp<GraphicBuffer>& buffer(
953 layer->getActiveBuffer());
954 if (buffer != NULL) {
955 format = buffer->getPixelFormat();
957 name = layer->getName();
960 int type = l.compositionType;
961 if (type == HWC_FRAMEBUFFER_TARGET) {
962 name = "HWC_FRAMEBUFFER_TARGET";
963 format = disp.format;
966 static char const* compositionTypeName[] = {
972 if (type >= NELEM(compositionTypeName))
973 type = NELEM(compositionTypeName) - 1;
976 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n",
977 compositionTypeName[type],
978 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
979 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
980 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
987 if (mHwc && mHwc->dump) {
988 mHwc->dump(mHwc, buffer, SIZE);
989 result.append(buffer);
993 // ---------------------------------------------------------------------------
995 HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
996 : mHwc(hwc), mEnabled(false),
998 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
1002 void HWComposer::VSyncThread::setEnabled(bool enabled) {
1003 Mutex::Autolock _l(mLock);
1004 if (mEnabled != enabled) {
1006 mCondition.signal();
1010 void HWComposer::VSyncThread::onFirstRef() {
1011 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1014 bool HWComposer::VSyncThread::threadLoop() {
1016 Mutex::Autolock _l(mLock);
1018 mCondition.wait(mLock);
1022 const nsecs_t period = mRefreshPeriod;
1023 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1024 nsecs_t next_vsync = mNextFakeVSync;
1025 nsecs_t sleep = next_vsync - now;
1027 // we missed, find where the next vsync should be
1028 sleep = (period - ((now - next_vsync) % period));
1029 next_vsync = now + sleep;
1031 mNextFakeVSync = next_vsync + period;
1033 struct timespec spec;
1034 spec.tv_sec = next_vsync / 1000000000;
1035 spec.tv_nsec = next_vsync % 1000000000;
1039 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1040 } while (err<0 && errno == EINTR);
1043 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1049 HWComposer::DisplayData::DisplayData()
1050 : width(0), height(0), format(0),
1051 xdpi(0.0f), ydpi(0.0f),
1054 hasFbComp(false), hasOvComp(false),
1055 capacity(0), list(NULL),
1056 framebufferTarget(NULL), fbTargetHandle(0),
1057 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
1061 HWComposer::DisplayData::~DisplayData() {
1065 // ---------------------------------------------------------------------------
1066 }; // namespace android