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
23 #include <sys/types.h>
26 #include <utils/CallStack.h>
27 #include <utils/Errors.h>
28 #include <utils/misc.h>
29 #include <utils/String8.h>
30 #include <utils/Thread.h>
31 #include <utils/Trace.h>
32 #include <utils/Vector.h>
34 #include <ui/GraphicBuffer.h>
36 #include <hardware/hardware.h>
37 #include <hardware/hwcomposer.h>
39 #include <android/configuration.h>
41 #include <cutils/log.h>
42 #include <cutils/properties.h>
44 #include "HWComposer.h"
46 #include "../Layer.h" // needed only for debugging
47 #include "../SurfaceFlinger.h"
51 #define MIN_HWC_HEADER_VERSION HWC_HEADER_VERSION
53 static uint32_t hwcApiVersion(const hwc_composer_device_1_t* hwc) {
54 uint32_t hwcVersion = hwc->common.version;
55 return hwcVersion & HARDWARE_API_VERSION_2_MAJ_MIN_MASK;
58 static uint32_t hwcHeaderVersion(const hwc_composer_device_1_t* hwc) {
59 uint32_t hwcVersion = hwc->common.version;
60 return hwcVersion & HARDWARE_API_VERSION_2_HEADER_MASK;
63 static bool hwcHasApiVersion(const hwc_composer_device_1_t* hwc,
65 return hwcApiVersion(hwc) >= (version & HARDWARE_API_VERSION_2_MAJ_MIN_MASK);
68 // ---------------------------------------------------------------------------
70 struct HWComposer::cb_context {
71 struct callbacks : public hwc_procs_t {
72 // these are here to facilitate the transition when adding
73 // new callbacks (an implementation can check for NULL before
74 // calling a new callback).
75 void (*zero[4])(void);
81 // ---------------------------------------------------------------------------
83 HWComposer::HWComposer(
84 const sp<SurfaceFlinger>& flinger,
85 EventHandler& handler)
87 mFbDev(0), mHwc(0), mNumDisplays(1),
88 mCBContext(new cb_context),
89 mEventHandler(handler),
90 mDebugForceFakeVSync(false)
92 for (size_t i =0 ; i<MAX_HWC_DISPLAYS ; i++) {
96 for (size_t i=0 ; i<HWC_NUM_PHYSICAL_DISPLAY_TYPES ; i++) {
101 char value[PROPERTY_VALUE_MAX];
102 property_get("debug.sf.no_hw_vsync", value, "0");
103 mDebugForceFakeVSync = atoi(value);
105 bool needVSyncThread = true;
107 // Note: some devices may insist that the FB HAL be opened before HWC.
108 int fberr = loadFbHalModule();
111 if (mFbDev && mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
112 // close FB HAL if we don't needed it.
113 // FIXME: this is temporary until we're not forced to open FB HAL
115 framebuffer_close(mFbDev);
119 // If we have no HWC, or a pre-1.1 HWC, an FB dev is mandatory.
120 if ((!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
122 ALOGE("ERROR: failed to open framebuffer (%s), aborting",
127 // these display IDs are always reserved
128 for (size_t i=0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
129 mAllocatedDisplayIDs.markBit(i);
133 ALOGI("Using %s version %u.%u", HWC_HARDWARE_COMPOSER,
134 (hwcApiVersion(mHwc) >> 24) & 0xff,
135 (hwcApiVersion(mHwc) >> 16) & 0xff);
136 if (mHwc->registerProcs) {
137 mCBContext->hwc = this;
138 mCBContext->procs.invalidate = &hook_invalidate;
139 mCBContext->procs.vsync = &hook_vsync;
140 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
141 mCBContext->procs.hotplug = &hook_hotplug;
143 mCBContext->procs.hotplug = NULL;
144 memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero));
145 mHwc->registerProcs(mHwc, &mCBContext->procs);
148 // don't need a vsync thread if we have a hardware composer
149 needVSyncThread = false;
150 // always turn vsync off when we start
151 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
153 // the number of displays we actually have depends on the
154 // hw composer version
155 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
156 // 1.3 adds support for virtual displays
157 mNumDisplays = MAX_HWC_DISPLAYS;
158 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
159 // 1.1 adds support for multiple displays
160 mNumDisplays = NUM_BUILTIN_DISPLAYS;
167 ALOG_ASSERT(!(mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)),
168 "should only have fbdev if no hwc or hwc is 1.0");
170 DisplayData& disp(mDisplayData[HWC_DISPLAY_PRIMARY]);
171 disp.connected = true;
172 disp.width = mFbDev->width;
173 disp.height = mFbDev->height;
174 disp.format = mFbDev->format;
175 disp.xdpi = mFbDev->xdpi;
176 disp.ydpi = mFbDev->ydpi;
177 if (disp.refresh == 0) {
178 disp.refresh = nsecs_t(1e9 / mFbDev->fps);
179 ALOGW("getting VSYNC period from fb HAL: %lld", disp.refresh);
181 if (disp.refresh == 0) {
182 disp.refresh = nsecs_t(1e9 / 60.0);
183 ALOGW("getting VSYNC period from thin air: %lld",
184 mDisplayData[HWC_DISPLAY_PRIMARY].refresh);
187 // here we're guaranteed to have at least HWC 1.1
188 for (size_t i =0 ; i<NUM_BUILTIN_DISPLAYS ; i++) {
189 queryDisplayProperties(i);
193 if (needVSyncThread) {
194 // we don't have VSYNC support, we need to fake it
195 mVSyncThread = new VSyncThread(*this);
199 HWComposer::~HWComposer() {
201 eventControl(HWC_DISPLAY_PRIMARY, HWC_EVENT_VSYNC, 0);
203 if (mVSyncThread != NULL) {
204 mVSyncThread->requestExitAndWait();
210 framebuffer_close(mFbDev);
215 // Load and prepare the hardware composer module. Sets mHwc.
216 void HWComposer::loadHwcModule()
218 hw_module_t const* module;
220 if (hw_get_module(HWC_HARDWARE_MODULE_ID, &module) != 0) {
221 ALOGE("%s module not found", HWC_HARDWARE_MODULE_ID);
225 int err = hwc_open_1(module, &mHwc);
227 ALOGE("%s device failed to initialize (%s)",
228 HWC_HARDWARE_COMPOSER, strerror(-err));
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);
243 // Load and prepare the FB HAL, which uses the gralloc module. Sets mFbDev.
244 int HWComposer::loadFbHalModule()
246 hw_module_t const* module;
248 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
250 ALOGE("%s module not found", GRALLOC_HARDWARE_MODULE_ID);
254 return framebuffer_open(module, &mFbDev);
257 status_t HWComposer::initCheck() const {
258 return mHwc ? NO_ERROR : NO_INIT;
261 void HWComposer::hook_invalidate(const struct hwc_procs* procs) {
262 cb_context* ctx = reinterpret_cast<cb_context*>(
263 const_cast<hwc_procs_t*>(procs));
264 ctx->hwc->invalidate();
267 void HWComposer::hook_vsync(const struct hwc_procs* procs, int disp,
269 cb_context* ctx = reinterpret_cast<cb_context*>(
270 const_cast<hwc_procs_t*>(procs));
271 ctx->hwc->vsync(disp, timestamp);
274 void HWComposer::hook_hotplug(const struct hwc_procs* procs, int disp,
276 cb_context* ctx = reinterpret_cast<cb_context*>(
277 const_cast<hwc_procs_t*>(procs));
278 ctx->hwc->hotplug(disp, connected);
281 void HWComposer::invalidate() {
282 mFlinger->repaintEverything();
285 void HWComposer::vsync(int disp, int64_t timestamp) {
286 if (uint32_t(disp) < HWC_NUM_PHYSICAL_DISPLAY_TYPES) {
288 snprintf(tag, sizeof(tag), "VSYNC_%1u", disp);
289 ATRACE_INT(tag, ++mVSyncCounts[disp] & 1);
291 mEventHandler.onVSyncReceived(disp, timestamp);
292 Mutex::Autolock _l(mLock);
293 mLastHwVSync[disp] = timestamp;
297 void HWComposer::hotplug(int disp, int connected) {
298 if (disp == HWC_DISPLAY_PRIMARY || disp >= VIRTUAL_DISPLAY_ID_BASE) {
299 ALOGE("hotplug event received for invalid display: disp=%d connected=%d",
303 queryDisplayProperties(disp);
304 mEventHandler.onHotplugReceived(disp, bool(connected));
307 static float getDefaultDensity(uint32_t height) {
308 if (height >= 1080) return ACONFIGURATION_DENSITY_XHIGH;
309 else return ACONFIGURATION_DENSITY_TV;
312 static const uint32_t DISPLAY_ATTRIBUTES[] = {
313 HWC_DISPLAY_VSYNC_PERIOD,
318 HWC_DISPLAY_NO_ATTRIBUTE,
320 #define NUM_DISPLAY_ATTRIBUTES (sizeof(DISPLAY_ATTRIBUTES) / sizeof(DISPLAY_ATTRIBUTES)[0])
322 status_t HWComposer::queryDisplayProperties(int disp) {
324 LOG_ALWAYS_FATAL_IF(!mHwc || !hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
326 // use zero as default value for unspecified attributes
327 int32_t values[NUM_DISPLAY_ATTRIBUTES - 1];
328 memset(values, 0, sizeof(values));
331 size_t numConfigs = 1;
332 status_t err = mHwc->getDisplayConfigs(mHwc, disp, &config, &numConfigs);
333 if (err != NO_ERROR) {
334 // this can happen if an unpluggable display is not connected
335 mDisplayData[disp].connected = false;
339 err = mHwc->getDisplayAttributes(mHwc, disp, config, DISPLAY_ATTRIBUTES, values);
340 if (err != NO_ERROR) {
341 // we can't get this display's info. turn it off.
342 mDisplayData[disp].connected = false;
346 int32_t w = 0, h = 0;
347 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
348 switch (DISPLAY_ATTRIBUTES[i]) {
349 case HWC_DISPLAY_VSYNC_PERIOD:
350 mDisplayData[disp].refresh = nsecs_t(values[i]);
352 case HWC_DISPLAY_WIDTH:
353 mDisplayData[disp].width = values[i];
355 case HWC_DISPLAY_HEIGHT:
356 mDisplayData[disp].height = values[i];
358 case HWC_DISPLAY_DPI_X:
359 mDisplayData[disp].xdpi = values[i] / 1000.0f;
361 case HWC_DISPLAY_DPI_Y:
362 mDisplayData[disp].ydpi = values[i] / 1000.0f;
365 ALOG_ASSERT(false, "unknown display attribute[%d] %#x",
366 i, DISPLAY_ATTRIBUTES[i]);
371 // FIXME: what should we set the format to?
372 mDisplayData[disp].format = HAL_PIXEL_FORMAT_RGBA_8888;
373 mDisplayData[disp].connected = true;
374 if (mDisplayData[disp].xdpi == 0.0f || mDisplayData[disp].ydpi == 0.0f) {
375 float dpi = getDefaultDensity(h);
376 mDisplayData[disp].xdpi = dpi;
377 mDisplayData[disp].ydpi = dpi;
382 status_t HWComposer::setVirtualDisplayProperties(int32_t id,
383 uint32_t w, uint32_t h, uint32_t format) {
384 if (id < VIRTUAL_DISPLAY_ID_BASE || id >= int32_t(mNumDisplays) ||
385 !mAllocatedDisplayIDs.hasBit(id)) {
388 mDisplayData[id].width = w;
389 mDisplayData[id].height = h;
390 mDisplayData[id].format = format;
391 mDisplayData[id].xdpi = mDisplayData[id].ydpi = getDefaultDensity(h);
395 int32_t HWComposer::allocateDisplayId() {
396 if (mAllocatedDisplayIDs.count() >= mNumDisplays) {
399 int32_t id = mAllocatedDisplayIDs.firstUnmarkedBit();
400 mAllocatedDisplayIDs.markBit(id);
401 mDisplayData[id].connected = true;
405 status_t HWComposer::freeDisplayId(int32_t id) {
406 if (id < NUM_BUILTIN_DISPLAYS) {
407 // cannot free the reserved IDs
410 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
413 mAllocatedDisplayIDs.clearBit(id);
414 mDisplayData[id].connected = false;
418 nsecs_t HWComposer::getRefreshPeriod(int disp) const {
419 return mDisplayData[disp].refresh;
422 nsecs_t HWComposer::getRefreshTimestamp(int disp) const {
423 // this returns the last refresh timestamp.
424 // if the last one is not available, we estimate it based on
425 // the refresh period and whatever closest timestamp we have.
426 Mutex::Autolock _l(mLock);
427 nsecs_t now = systemTime(CLOCK_MONOTONIC);
428 return now - ((now - mLastHwVSync[disp]) % mDisplayData[disp].refresh);
431 sp<Fence> HWComposer::getDisplayFence(int disp) const {
432 return mDisplayData[disp].lastDisplayFence;
435 uint32_t HWComposer::getWidth(int disp) const {
436 return mDisplayData[disp].width;
439 uint32_t HWComposer::getHeight(int disp) const {
440 return mDisplayData[disp].height;
443 uint32_t HWComposer::getFormat(int disp) const {
444 return mDisplayData[disp].format;
447 float HWComposer::getDpiX(int disp) const {
448 return mDisplayData[disp].xdpi;
451 float HWComposer::getDpiY(int disp) const {
452 return mDisplayData[disp].ydpi;
455 bool HWComposer::isConnected(int disp) const {
456 return mDisplayData[disp].connected;
459 void HWComposer::eventControl(int disp, int event, int enabled) {
460 if (uint32_t(disp)>31 || !mAllocatedDisplayIDs.hasBit(disp)) {
461 ALOGD("eventControl ignoring event %d on unallocated disp %d (en=%d)",
462 event, disp, enabled);
465 if (event != EVENT_VSYNC) {
466 ALOGW("eventControl got unexpected event %d (disp=%d en=%d)",
467 event, disp, enabled);
470 status_t err = NO_ERROR;
471 if (mHwc && !mDebugForceFakeVSync) {
472 // NOTE: we use our own internal lock here because we have to call
473 // into the HWC with the lock held, and we want to make sure
474 // that even if HWC blocks (which it shouldn't), it won't
475 // affect other threads.
476 Mutex::Autolock _l(mEventControlLock);
477 const int32_t eventBit = 1UL << event;
478 const int32_t newValue = enabled ? eventBit : 0;
479 const int32_t oldValue = mDisplayData[disp].events & eventBit;
480 if (newValue != oldValue) {
482 err = mHwc->eventControl(mHwc, disp, event, enabled);
484 int32_t& events(mDisplayData[disp].events);
485 events = (events & ~eventBit) | newValue;
488 // error here should not happen -- not sure what we should
490 ALOGE_IF(err, "eventControl(%d, %d) failed %s",
491 event, enabled, strerror(-err));
494 if (err == NO_ERROR && mVSyncThread != NULL) {
495 mVSyncThread->setEnabled(enabled);
499 status_t HWComposer::createWorkList(int32_t id, size_t numLayers) {
500 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
505 DisplayData& disp(mDisplayData[id]);
506 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
507 // we need space for the HWC_FRAMEBUFFER_TARGET
510 if (disp.capacity < numLayers || disp.list == NULL) {
511 size_t size = sizeof(hwc_display_contents_1_t)
512 + numLayers * sizeof(hwc_layer_1_t);
514 disp.list = (hwc_display_contents_1_t*)malloc(size);
515 disp.capacity = numLayers;
517 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
518 disp.framebufferTarget = &disp.list->hwLayers[numLayers - 1];
519 memset(disp.framebufferTarget, 0, sizeof(hwc_layer_1_t));
520 const hwc_rect_t r = { 0, 0, (int) disp.width, (int) disp.height };
521 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
522 disp.framebufferTarget->hints = 0;
523 disp.framebufferTarget->flags = 0;
524 disp.framebufferTarget->handle = disp.fbTargetHandle;
525 disp.framebufferTarget->transform = 0;
526 disp.framebufferTarget->blending = HWC_BLENDING_PREMULT;
527 disp.framebufferTarget->sourceCrop = r;
528 disp.framebufferTarget->displayFrame = r;
529 disp.framebufferTarget->visibleRegionScreen.numRects = 1;
530 disp.framebufferTarget->visibleRegionScreen.rects =
531 &disp.framebufferTarget->displayFrame;
532 disp.framebufferTarget->acquireFenceFd = -1;
533 disp.framebufferTarget->releaseFenceFd = -1;
534 disp.framebufferTarget->planeAlpha = 0xFF;
536 disp.list->retireFenceFd = -1;
537 disp.list->flags = HWC_GEOMETRY_CHANGED;
538 disp.list->numHwLayers = numLayers;
543 status_t HWComposer::setFramebufferTarget(int32_t id,
544 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buf) {
545 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
548 DisplayData& disp(mDisplayData[id]);
549 if (!disp.framebufferTarget) {
550 // this should never happen, but apparently eglCreateWindowSurface()
551 // triggers a Surface::queueBuffer() on some
552 // devices (!?) -- log and ignore.
553 ALOGE("HWComposer: framebufferTarget is null");
557 int acquireFenceFd = -1;
558 if (acquireFence->isValid()) {
559 acquireFenceFd = acquireFence->dup();
562 // ALOGD("fbPost: handle=%p, fence=%d", buf->handle, acquireFenceFd);
563 disp.fbTargetHandle = buf->handle;
564 disp.framebufferTarget->handle = disp.fbTargetHandle;
565 disp.framebufferTarget->acquireFenceFd = acquireFenceFd;
569 status_t HWComposer::prepare() {
570 for (size_t i=0 ; i<mNumDisplays ; i++) {
571 DisplayData& disp(mDisplayData[i]);
572 if (disp.framebufferTarget) {
573 // make sure to reset the type to HWC_FRAMEBUFFER_TARGET
574 // DO NOT reset the handle field to NULL, because it's possible
575 // that we have nothing to redraw (eg: eglSwapBuffers() not called)
576 // in which case, we should continue to use the same buffer.
577 LOG_FATAL_IF(disp.list == NULL);
578 disp.framebufferTarget->compositionType = HWC_FRAMEBUFFER_TARGET;
580 if (!disp.connected && disp.list != NULL) {
581 ALOGW("WARNING: disp %d: connected, non-null list, layers=%d",
582 i, disp.list->numHwLayers);
584 mLists[i] = disp.list;
586 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
587 mLists[i]->outbuf = NULL;
588 mLists[i]->outbufAcquireFenceFd = -1;
589 } else if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
590 // garbage data to catch improper use
591 mLists[i]->dpy = (hwc_display_t)0xDEADBEEF;
592 mLists[i]->sur = (hwc_surface_t)0xDEADBEEF;
594 mLists[i]->dpy = EGL_NO_DISPLAY;
595 mLists[i]->sur = EGL_NO_SURFACE;
600 int err = mHwc->prepare(mHwc, mNumDisplays, mLists);
601 ALOGE_IF(err, "HWComposer: prepare failed (%s)", strerror(-err));
603 if (err == NO_ERROR) {
604 // here we're just making sure that "skip" layers are set
605 // to HWC_FRAMEBUFFER and we're also counting how many layers
606 // we have of each type.
607 for (size_t i=0 ; i<mNumDisplays ; i++) {
608 DisplayData& disp(mDisplayData[i]);
609 disp.hasFbComp = false;
610 disp.hasOvComp = false;
612 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
613 hwc_layer_1_t& l = disp.list->hwLayers[i];
615 //ALOGD("prepare: %d, type=%d, handle=%p",
616 // i, l.compositionType, l.handle);
618 if (l.flags & HWC_SKIP_LAYER) {
619 l.compositionType = HWC_FRAMEBUFFER;
621 if (l.compositionType == HWC_FRAMEBUFFER) {
622 disp.hasFbComp = true;
624 if (l.compositionType == HWC_OVERLAY) {
625 disp.hasOvComp = true;
631 return (status_t)err;
634 bool HWComposer::hasHwcComposition(int32_t id) const {
635 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
637 return mDisplayData[id].hasOvComp;
640 bool HWComposer::hasGlesComposition(int32_t id) const {
641 if (!mHwc || uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
643 return mDisplayData[id].hasFbComp;
646 sp<Fence> HWComposer::getAndResetReleaseFence(int32_t id) {
647 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
648 return Fence::NO_FENCE;
650 int fd = INVALID_OPERATION;
651 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
652 const DisplayData& disp(mDisplayData[id]);
653 if (disp.framebufferTarget) {
654 fd = disp.framebufferTarget->releaseFenceFd;
655 disp.framebufferTarget->acquireFenceFd = -1;
656 disp.framebufferTarget->releaseFenceFd = -1;
659 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
662 status_t HWComposer::commit() {
665 if (!hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
666 // On version 1.0, the OpenGL ES target surface is communicated
667 // by the (dpy, sur) fields and we are guaranteed to have only
669 mLists[0]->dpy = eglGetCurrentDisplay();
670 mLists[0]->sur = eglGetCurrentSurface(EGL_DRAW);
673 for (size_t i=VIRTUAL_DISPLAY_ID_BASE; i<mNumDisplays; i++) {
674 DisplayData& disp(mDisplayData[i]);
675 if (disp.outbufHandle) {
676 mLists[i]->outbuf = disp.outbufHandle;
677 mLists[i]->outbufAcquireFenceFd =
678 disp.outbufAcquireFence->dup();
682 err = mHwc->set(mHwc, mNumDisplays, mLists);
684 for (size_t i=0 ; i<mNumDisplays ; i++) {
685 DisplayData& disp(mDisplayData[i]);
686 disp.lastDisplayFence = disp.lastRetireFence;
687 disp.lastRetireFence = Fence::NO_FENCE;
689 if (disp.list->retireFenceFd != -1) {
690 disp.lastRetireFence = new Fence(disp.list->retireFenceFd);
691 disp.list->retireFenceFd = -1;
693 disp.list->flags &= ~HWC_GEOMETRY_CHANGED;
697 return (status_t)err;
700 status_t HWComposer::release(int disp) {
701 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
703 eventControl(disp, HWC_EVENT_VSYNC, 0);
704 return (status_t)mHwc->blank(mHwc, disp, 1);
709 status_t HWComposer::acquire(int disp) {
710 LOG_FATAL_IF(disp >= VIRTUAL_DISPLAY_ID_BASE);
712 return (status_t)mHwc->blank(mHwc, disp, 0);
717 void HWComposer::disconnectDisplay(int disp) {
718 LOG_ALWAYS_FATAL_IF(disp < 0 || disp == HWC_DISPLAY_PRIMARY);
719 DisplayData& dd(mDisplayData[disp]);
722 dd.framebufferTarget = NULL; // points into dd.list
723 dd.fbTargetHandle = NULL;
724 dd.outbufHandle = NULL;
725 dd.lastRetireFence = Fence::NO_FENCE;
726 dd.lastDisplayFence = Fence::NO_FENCE;
727 dd.outbufAcquireFence = Fence::NO_FENCE;
730 int HWComposer::getVisualID() const {
731 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
732 // FIXME: temporary hack until HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
733 // is supported by the implementation. we can only be in this case
734 // if we have HWC 1.1
735 return HAL_PIXEL_FORMAT_RGBA_8888;
736 //return HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
738 return mFbDev->format;
742 bool HWComposer::supportsFramebufferTarget() const {
743 return (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1));
746 int HWComposer::fbPost(int32_t id,
747 const sp<Fence>& acquireFence, const sp<GraphicBuffer>& buffer) {
748 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
749 return setFramebufferTarget(id, acquireFence, buffer);
751 acquireFence->waitForever("HWComposer::fbPost");
752 return mFbDev->post(mFbDev, buffer->handle);
756 int HWComposer::fbCompositionComplete() {
757 if (mHwc && hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1))
760 if (mFbDev->compositionComplete) {
761 return mFbDev->compositionComplete(mFbDev);
763 return INVALID_OPERATION;
767 void HWComposer::fbDump(String8& result) {
768 if (mFbDev && mFbDev->common.version >= 1 && mFbDev->dump) {
769 const size_t SIZE = 4096;
771 mFbDev->dump(mFbDev, buffer, SIZE);
772 result.append(buffer);
776 status_t HWComposer::setOutputBuffer(int32_t id, const sp<Fence>& acquireFence,
777 const sp<GraphicBuffer>& buf) {
778 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
780 if (id < VIRTUAL_DISPLAY_ID_BASE)
781 return INVALID_OPERATION;
783 DisplayData& disp(mDisplayData[id]);
784 disp.outbufHandle = buf->handle;
785 disp.outbufAcquireFence = acquireFence;
789 sp<Fence> HWComposer::getLastRetireFence(int32_t id) {
790 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id))
791 return Fence::NO_FENCE;
792 return mDisplayData[id].lastRetireFence;
796 * Helper template to implement a concrete HWCLayer
797 * This holds the pointer to the concrete hwc layer type
798 * and implements the "iterable" side of HWCLayer.
800 template<typename CONCRETE, typename HWCTYPE>
801 class Iterable : public HWComposer::HWCLayer {
803 HWCTYPE* const mLayerList;
804 HWCTYPE* mCurrentLayer;
805 Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { }
806 inline HWCTYPE const * getLayer() const { return mCurrentLayer; }
807 inline HWCTYPE* getLayer() { return mCurrentLayer; }
808 virtual ~Iterable() { }
810 // returns a copy of ourselves
811 virtual HWComposer::HWCLayer* dup() {
812 return new CONCRETE( static_cast<const CONCRETE&>(*this) );
814 virtual status_t setLayer(size_t index) {
815 mCurrentLayer = &mLayerList[index];
821 * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0.
822 * This implements the HWCLayer side of HWCIterableLayer.
824 class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> {
825 struct hwc_composer_device_1* mHwc;
827 HWCLayerVersion1(struct hwc_composer_device_1* hwc, hwc_layer_1_t* layer)
828 : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer), mHwc(hwc) { }
830 virtual int32_t getCompositionType() const {
831 return getLayer()->compositionType;
833 virtual uint32_t getHints() const {
834 return getLayer()->hints;
836 virtual sp<Fence> getAndResetReleaseFence() {
837 int fd = getLayer()->releaseFenceFd;
838 getLayer()->releaseFenceFd = -1;
839 return fd >= 0 ? new Fence(fd) : Fence::NO_FENCE;
841 virtual void setAcquireFenceFd(int fenceFd) {
842 getLayer()->acquireFenceFd = fenceFd;
844 virtual void setPerFrameDefaultState() {
845 //getLayer()->compositionType = HWC_FRAMEBUFFER;
847 virtual void setPlaneAlpha(uint8_t alpha) {
848 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_2)) {
849 getLayer()->planeAlpha = alpha;
852 getLayer()->flags |= HWC_SKIP_LAYER;
856 virtual void setDefaultState() {
857 hwc_layer_1_t* const l = getLayer();
858 l->compositionType = HWC_FRAMEBUFFER;
860 l->flags = HWC_SKIP_LAYER;
863 l->blending = HWC_BLENDING_NONE;
864 l->visibleRegionScreen.numRects = 0;
865 l->visibleRegionScreen.rects = NULL;
866 l->acquireFenceFd = -1;
867 l->releaseFenceFd = -1;
868 l->planeAlpha = 0xFF;
870 virtual void setSkip(bool skip) {
872 getLayer()->flags |= HWC_SKIP_LAYER;
874 getLayer()->flags &= ~HWC_SKIP_LAYER;
877 virtual void setBlending(uint32_t blending) {
878 getLayer()->blending = blending;
880 virtual void setTransform(uint32_t transform) {
881 getLayer()->transform = transform;
883 virtual void setFrame(const Rect& frame) {
884 getLayer()->displayFrame = reinterpret_cast<hwc_rect_t const&>(frame);
886 virtual void setCrop(const FloatRect& crop) {
887 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
888 getLayer()->sourceCropf = reinterpret_cast<hwc_frect_t const&>(crop);
891 * Since h/w composer didn't support a flot crop rect before version 1.3,
892 * using integer coordinates instead produces a different output from the GL code in
893 * Layer::drawWithOpenGL(). The difference can be large if the buffer crop to
894 * window size ratio is large and a window crop is defined
895 * (i.e.: if we scale the buffer a lot and we also crop it with a window crop).
897 hwc_rect_t& r = getLayer()->sourceCrop;
898 r.left = int(ceilf(crop.left));
899 r.top = int(ceilf(crop.top));
900 r.right = int(floorf(crop.right));
901 r.bottom= int(floorf(crop.bottom));
904 virtual void setVisibleRegionScreen(const Region& reg) {
905 // Region::getSharedBuffer creates a reference to the underlying
906 // SharedBuffer of this Region, this reference is freed
908 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
909 SharedBuffer const* sb = reg.getSharedBuffer(&visibleRegion.numRects);
910 visibleRegion.rects = reinterpret_cast<hwc_rect_t const *>(sb->data());
912 virtual void setBuffer(const sp<GraphicBuffer>& buffer) {
913 if (buffer == 0 || buffer->handle == 0) {
914 getLayer()->compositionType = HWC_FRAMEBUFFER;
915 getLayer()->flags |= HWC_SKIP_LAYER;
916 getLayer()->handle = 0;
918 getLayer()->handle = buffer->handle;
921 virtual void onDisplayed() {
922 hwc_region_t& visibleRegion = getLayer()->visibleRegionScreen;
923 SharedBuffer const* sb = SharedBuffer::bufferFromData(visibleRegion.rects);
926 // not technically needed but safer
927 visibleRegion.numRects = 0;
928 visibleRegion.rects = NULL;
931 getLayer()->acquireFenceFd = -1;
936 * returns an iterator initialized at a given index in the layer list
938 HWComposer::LayerListIterator HWComposer::getLayerIterator(int32_t id, size_t index) {
939 if (uint32_t(id)>31 || !mAllocatedDisplayIDs.hasBit(id)) {
940 return LayerListIterator();
942 const DisplayData& disp(mDisplayData[id]);
943 if (!mHwc || !disp.list || index > disp.list->numHwLayers) {
944 return LayerListIterator();
946 return LayerListIterator(new HWCLayerVersion1(mHwc, disp.list->hwLayers), index);
950 * returns an iterator on the beginning of the layer list
952 HWComposer::LayerListIterator HWComposer::begin(int32_t id) {
953 return getLayerIterator(id, 0);
957 * returns an iterator on the end of the layer list
959 HWComposer::LayerListIterator HWComposer::end(int32_t id) {
960 size_t numLayers = 0;
961 if (uint32_t(id) <= 31 && mAllocatedDisplayIDs.hasBit(id)) {
962 const DisplayData& disp(mDisplayData[id]);
963 if (mHwc && disp.list) {
964 numLayers = disp.list->numHwLayers;
965 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_1)) {
966 // with HWC 1.1, the last layer is always the HWC_FRAMEBUFFER_TARGET,
967 // which we ignore when iterating through the layer list.
968 ALOGE_IF(!numLayers, "mDisplayData[%d].list->numHwLayers is 0", id);
975 return getLayerIterator(id, numLayers);
978 void HWComposer::dump(String8& result) const {
980 result.appendFormat("Hardware Composer state (version %8x):\n", hwcApiVersion(mHwc));
981 result.appendFormat(" mDebugForceFakeVSync=%d\n", mDebugForceFakeVSync);
982 for (size_t i=0 ; i<mNumDisplays ; i++) {
983 const DisplayData& disp(mDisplayData[i]);
987 const Vector< sp<Layer> >& visibleLayersSortedByZ =
988 mFlinger->getLayerSortedByZForHwcDisplay(i);
991 " Display[%d] : %ux%u, xdpi=%f, ydpi=%f, refresh=%lld\n",
992 i, disp.width, disp.height, disp.xdpi, disp.ydpi, disp.refresh);
996 " numHwLayers=%u, flags=%08x\n",
997 disp.list->numHwLayers, disp.list->flags);
1000 " type | handle | hints | flags | tr | blend | format | source crop | frame name \n"
1001 "------------+----------+----------+----------+----+-------+----------+---------------------------------+--------------------------------\n");
1002 // " __________ | ________ | ________ | ________ | __ | _____ | ________ | [_____._,_____._,_____._,_____._] | [_____,_____,_____,_____]
1003 for (size_t i=0 ; i<disp.list->numHwLayers ; i++) {
1004 const hwc_layer_1_t&l = disp.list->hwLayers[i];
1005 int32_t format = -1;
1006 String8 name("unknown");
1008 if (i < visibleLayersSortedByZ.size()) {
1009 const sp<Layer>& layer(visibleLayersSortedByZ[i]);
1010 const sp<GraphicBuffer>& buffer(
1011 layer->getActiveBuffer());
1012 if (buffer != NULL) {
1013 format = buffer->getPixelFormat();
1015 name = layer->getName();
1018 int type = l.compositionType;
1019 if (type == HWC_FRAMEBUFFER_TARGET) {
1020 name = "HWC_FRAMEBUFFER_TARGET";
1021 format = disp.format;
1024 static char const* compositionTypeName[] = {
1030 if (type >= NELEM(compositionTypeName))
1031 type = NELEM(compositionTypeName) - 1;
1033 if (hwcHasApiVersion(mHwc, HWC_DEVICE_API_VERSION_1_3)) {
1034 result.appendFormat(
1035 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%7.1f,%7.1f,%7.1f,%7.1f] | [%5d,%5d,%5d,%5d] %s\n",
1036 compositionTypeName[type],
1037 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1038 l.sourceCropf.left, l.sourceCropf.top, l.sourceCropf.right, l.sourceCropf.bottom,
1039 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1042 result.appendFormat(
1043 " %10s | %08x | %08x | %08x | %02x | %05x | %08x | [%7d,%7d,%7d,%7d] | [%5d,%5d,%5d,%5d] %s\n",
1044 compositionTypeName[type],
1045 intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format,
1046 l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom,
1047 l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom,
1055 if (mHwc && mHwc->dump) {
1056 const size_t SIZE = 4096;
1058 mHwc->dump(mHwc, buffer, SIZE);
1059 result.append(buffer);
1063 // ---------------------------------------------------------------------------
1065 HWComposer::VSyncThread::VSyncThread(HWComposer& hwc)
1066 : mHwc(hwc), mEnabled(false),
1068 mRefreshPeriod(hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY))
1072 void HWComposer::VSyncThread::setEnabled(bool enabled) {
1073 Mutex::Autolock _l(mLock);
1074 if (mEnabled != enabled) {
1076 mCondition.signal();
1080 void HWComposer::VSyncThread::onFirstRef() {
1081 run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
1084 bool HWComposer::VSyncThread::threadLoop() {
1086 Mutex::Autolock _l(mLock);
1088 mCondition.wait(mLock);
1092 const nsecs_t period = mRefreshPeriod;
1093 const nsecs_t now = systemTime(CLOCK_MONOTONIC);
1094 nsecs_t next_vsync = mNextFakeVSync;
1095 nsecs_t sleep = next_vsync - now;
1097 // we missed, find where the next vsync should be
1098 sleep = (period - ((now - next_vsync) % period));
1099 next_vsync = now + sleep;
1101 mNextFakeVSync = next_vsync + period;
1103 struct timespec spec;
1104 spec.tv_sec = next_vsync / 1000000000;
1105 spec.tv_nsec = next_vsync % 1000000000;
1109 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
1110 } while (err<0 && errno == EINTR);
1113 mHwc.mEventHandler.onVSyncReceived(0, next_vsync);
1119 HWComposer::DisplayData::DisplayData()
1120 : width(0), height(0), format(0),
1121 xdpi(0.0f), ydpi(0.0f),
1124 hasFbComp(false), hasOvComp(false),
1125 capacity(0), list(NULL),
1126 framebufferTarget(NULL), fbTargetHandle(0),
1127 lastRetireFence(Fence::NO_FENCE), lastDisplayFence(Fence::NO_FENCE),
1128 outbufHandle(NULL), outbufAcquireFence(Fence::NO_FENCE),
1132 HWComposer::DisplayData::~DisplayData() {
1136 // ---------------------------------------------------------------------------
1137 }; // namespace android