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