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 |
| 20 | // #define HWC_REMOVE_DEPRECATED_VERSIONS 1 |
| 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 | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 29 | #include <utils/String8.h> |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 30 | #include <utils/Thread.h> |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 31 | #include <utils/Trace.h> |
Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 32 | #include <utils/Vector.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 34 | #include <ui/GraphicBuffer.h> |
| 35 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 36 | #include <hardware/hardware.h> |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 37 | #include <hardware/hwcomposer.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 38 | |
| 39 | #include <cutils/log.h> |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 40 | #include <cutils/properties.h> |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 41 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 42 | #include "Layer.h" // needed only for debugging |
Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 43 | #include "LayerBase.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 44 | #include "HWComposer.h" |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 45 | #include "SurfaceFlinger.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 46 | |
| 47 | namespace android { |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 48 | |
| 49 | // --------------------------------------------------------------------------- |
| 50 | // Support for HWC_DEVICE_API_VERSION_0_3 and older: |
| 51 | // Since v0.3 is deprecated and support will be dropped soon, as much as |
| 52 | // possible the code is written to target v1.0. When using a v0.3 HWC, we |
| 53 | // allocate v0.3 structures, but assign them to v1.0 pointers. Fields that |
| 54 | // exist in both versions are located at the same offset, so in most cases we |
| 55 | // can just use the v1.0 pointer without branches or casts. |
| 56 | |
| 57 | #if HWC_REMOVE_DEPRECATED_VERSIONS |
| 58 | // We need complete types with to satisfy semantic checks, even though the |
| 59 | // code paths that use these won't get executed at runtime (and will likely be |
| 60 | // dead-code-eliminated). When we remove the code to support v0.3 we can remove |
| 61 | // these as well. |
| 62 | typedef hwc_layer_1_t hwc_layer_t; |
| 63 | typedef hwc_layer_list_1_t hwc_layer_list_t; |
| 64 | typedef hwc_composer_device_1_t hwc_composer_device_t; |
| 65 | #endif |
| 66 | |
| 67 | // This function assumes we've already rejected HWC's with lower-than-required |
| 68 | // versions. Don't use it for the initial "does HWC meet requirements" check! |
| 69 | static bool hwcHasVersion(const hwc_composer_device_1_t* hwc, uint32_t version) { |
| 70 | if (HWC_REMOVE_DEPRECATED_VERSIONS && |
| 71 | version <= HWC_DEVICE_API_VERSION_1_0) { |
| 72 | return true; |
| 73 | } else { |
| 74 | return hwc->common.version >= version; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static size_t sizeofHwcLayerList(const hwc_composer_device_1_t* hwc, |
| 79 | size_t numLayers) { |
| 80 | if (hwcHasVersion(hwc, HWC_DEVICE_API_VERSION_1_0)) { |
| 81 | return sizeof(hwc_layer_list_1_t) + numLayers*sizeof(hwc_layer_1_t); |
| 82 | } else { |
| 83 | return sizeof(hwc_layer_list_t) + numLayers*sizeof(hwc_layer_t); |
| 84 | } |
| 85 | } |
| 86 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 87 | // --------------------------------------------------------------------------- |
| 88 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 89 | struct HWComposer::cb_context { |
| 90 | struct callbacks : public hwc_procs_t { |
| 91 | // these are here to facilitate the transition when adding |
| 92 | // new callbacks (an implementation can check for NULL before |
| 93 | // calling a new callback). |
| 94 | void (*zero[4])(void); |
| 95 | }; |
| 96 | callbacks procs; |
| 97 | HWComposer* hwc; |
| 98 | }; |
| 99 | |
| 100 | // --------------------------------------------------------------------------- |
| 101 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 102 | HWComposer::HWComposer( |
| 103 | const sp<SurfaceFlinger>& flinger, |
| 104 | EventHandler& handler, |
| 105 | nsecs_t refreshPeriod) |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 106 | : mFlinger(flinger), |
| 107 | mModule(0), mHwc(0), mList(0), mCapacity(0), |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 108 | mNumOVLayers(0), mNumFBLayers(0), |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 109 | mCBContext(new cb_context), |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 110 | mEventHandler(handler), |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 111 | mRefreshPeriod(refreshPeriod), |
| 112 | mVSyncCount(0), mDebugForceFakeVSync(false) |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 113 | { |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 114 | char value[PROPERTY_VALUE_MAX]; |
| 115 | property_get("debug.sf.no_hw_vsync", value, "0"); |
| 116 | mDebugForceFakeVSync = atoi(value); |
| 117 | |
Mathias Agopian | 028508c | 2012-07-25 21:12:12 -0700 | [diff] [blame] | 118 | bool needVSyncThread = true; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 119 | int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &mModule); |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 120 | ALOGW_IF(err, "%s module not found", HWC_HARDWARE_MODULE_ID); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 121 | if (err == 0) { |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 122 | err = hwc_open_1(mModule, &mHwc); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 123 | ALOGE_IF(err, "%s device failed to initialize (%s)", |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 124 | HWC_HARDWARE_COMPOSER, strerror(-err)); |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 125 | if (err == 0) { |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 126 | if (HWC_REMOVE_DEPRECATED_VERSIONS && |
| 127 | mHwc->common.version < HWC_DEVICE_API_VERSION_1_0) { |
| 128 | ALOGE("%s device version %#x too old, will not be used", |
| 129 | HWC_HARDWARE_COMPOSER, mHwc->common.version); |
| 130 | hwc_close_1(mHwc); |
| 131 | mHwc = NULL; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (mHwc) { |
Mathias Agopian | 028508c | 2012-07-25 21:12:12 -0700 | [diff] [blame] | 136 | if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) { |
| 137 | // always turn vsync off when we start |
| 138 | mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0); |
| 139 | needVSyncThread = false; |
| 140 | } |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 141 | if (mHwc->registerProcs) { |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 142 | mCBContext->hwc = this; |
| 143 | mCBContext->procs.invalidate = &hook_invalidate; |
| 144 | mCBContext->procs.vsync = &hook_vsync; |
| 145 | mHwc->registerProcs(mHwc, &mCBContext->procs); |
| 146 | memset(mCBContext->procs.zero, 0, sizeof(mCBContext->procs.zero)); |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
Mathias Agopian | 3a77871 | 2012-04-09 14:16:47 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (needVSyncThread) { |
| 152 | // we don't have VSYNC support, we need to fake it |
| 153 | mVSyncThread = new VSyncThread(*this); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | HWComposer::~HWComposer() { |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 158 | eventControl(EVENT_VSYNC, 0); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 159 | free(mList); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 160 | if (mVSyncThread != NULL) { |
| 161 | mVSyncThread->requestExitAndWait(); |
| 162 | } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 163 | if (mHwc) { |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 164 | hwc_close_1(mHwc); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 165 | } |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 166 | delete mCBContext; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | status_t HWComposer::initCheck() const { |
| 170 | return mHwc ? NO_ERROR : NO_INIT; |
| 171 | } |
| 172 | |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 173 | void HWComposer::hook_invalidate(struct hwc_procs* procs) { |
| 174 | reinterpret_cast<cb_context *>(procs)->hwc->invalidate(); |
| 175 | } |
| 176 | |
Mathias Agopian | 31d2843 | 2012-04-03 16:31:39 -0700 | [diff] [blame] | 177 | void HWComposer::hook_vsync(struct hwc_procs* procs, int dpy, int64_t timestamp) { |
| 178 | reinterpret_cast<cb_context *>(procs)->hwc->vsync(dpy, timestamp); |
| 179 | } |
| 180 | |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 181 | void HWComposer::invalidate() { |
Mathias Agopian | e2c2f92 | 2011-10-05 15:00:22 -0700 | [diff] [blame] | 182 | mFlinger->repaintEverything(); |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Mathias Agopian | 31d2843 | 2012-04-03 16:31:39 -0700 | [diff] [blame] | 185 | void HWComposer::vsync(int dpy, int64_t timestamp) { |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 186 | ATRACE_INT("VSYNC", ++mVSyncCount&1); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 187 | mEventHandler.onVSyncReceived(dpy, timestamp); |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 188 | Mutex::Autolock _l(mLock); |
| 189 | mLastHwVSync = timestamp; |
| 190 | } |
| 191 | |
| 192 | nsecs_t HWComposer::getRefreshTimestamp() const { |
| 193 | // this returns the last refresh timestamp. |
| 194 | // if the last one is not available, we estimate it based on |
| 195 | // the refresh period and whatever closest timestamp we have. |
| 196 | Mutex::Autolock _l(mLock); |
| 197 | nsecs_t now = systemTime(CLOCK_MONOTONIC); |
| 198 | return now - ((now - mLastHwVSync) % mRefreshPeriod); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Mathias Agopian | 03e4072 | 2012-04-26 16:11:59 -0700 | [diff] [blame] | 201 | void HWComposer::eventControl(int event, int enabled) { |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 202 | status_t err = NO_ERROR; |
Erik Gilling | 1a3bf41 | 2012-04-06 14:13:32 -0700 | [diff] [blame] | 203 | if (mHwc && mHwc->common.version >= HWC_DEVICE_API_VERSION_0_3) { |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 204 | if (!mDebugForceFakeVSync) { |
| 205 | err = mHwc->methods->eventControl(mHwc, event, enabled); |
Mathias Agopian | 03e4072 | 2012-04-26 16:11:59 -0700 | [diff] [blame] | 206 | // error here should not happen -- not sure what we should |
| 207 | // do if it does. |
| 208 | ALOGE_IF(err, "eventControl(%d, %d) failed %s", |
| 209 | event, enabled, strerror(-err)); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 210 | } |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 211 | } |
Mathias Agopian | 3a77871 | 2012-04-09 14:16:47 -0700 | [diff] [blame] | 212 | |
| 213 | if (err == NO_ERROR && mVSyncThread != NULL) { |
| 214 | mVSyncThread->setEnabled(enabled); |
| 215 | } |
Mathias Agopian | 31d2843 | 2012-04-03 16:31:39 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 218 | status_t HWComposer::createWorkList(size_t numLayers) { |
Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 219 | if (mHwc) { |
| 220 | if (!mList || mCapacity < numLayers) { |
| 221 | free(mList); |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 222 | size_t size = sizeofHwcLayerList(mHwc, numLayers); |
| 223 | mList = (hwc_layer_list_1_t*)malloc(size); |
Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 224 | mCapacity = numLayers; |
| 225 | } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 226 | mList->flags = HWC_GEOMETRY_CHANGED; |
| 227 | mList->numHwLayers = numLayers; |
| 228 | } |
| 229 | return NO_ERROR; |
| 230 | } |
| 231 | |
| 232 | status_t HWComposer::prepare() const { |
| 233 | int err = mHwc->prepare(mHwc, mList); |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 234 | if (err == NO_ERROR) { |
| 235 | size_t numOVLayers = 0; |
| 236 | size_t numFBLayers = 0; |
| 237 | size_t count = mList->numHwLayers; |
| 238 | for (size_t i=0 ; i<count ; i++) { |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 239 | hwc_layer_1_t* l = NULL; |
| 240 | if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) { |
| 241 | l = &mList->hwLayers[i]; |
| 242 | } else { |
| 243 | // mList really has hwc_layer_list_t memory layout |
| 244 | hwc_layer_list_t* list = (hwc_layer_list_t*)mList; |
| 245 | hwc_layer_t* layer = &list->hwLayers[i]; |
| 246 | l = (hwc_layer_1_t*)layer; |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 247 | } |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 248 | if (l->flags & HWC_SKIP_LAYER) { |
| 249 | l->compositionType = HWC_FRAMEBUFFER; |
| 250 | } |
| 251 | switch (l->compositionType) { |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 252 | case HWC_OVERLAY: |
| 253 | numOVLayers++; |
| 254 | break; |
| 255 | case HWC_FRAMEBUFFER: |
| 256 | numFBLayers++; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | mNumOVLayers = numOVLayers; |
| 261 | mNumFBLayers = numFBLayers; |
| 262 | } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 263 | return (status_t)err; |
| 264 | } |
| 265 | |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 266 | size_t HWComposer::getLayerCount(int type) const { |
| 267 | switch (type) { |
| 268 | case HWC_OVERLAY: |
| 269 | return mNumOVLayers; |
| 270 | case HWC_FRAMEBUFFER: |
| 271 | return mNumFBLayers; |
| 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | |
Jesse Hall | 34a09ba | 2012-07-29 22:35:34 -0700 | [diff] [blame] | 276 | status_t HWComposer::commit(void* fbDisplay, void* fbSurface) const { |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 277 | int err = NO_ERROR; |
| 278 | if (mHwc) { |
Jesse Hall | 34a09ba | 2012-07-29 22:35:34 -0700 | [diff] [blame] | 279 | err = mHwc->set(mHwc, fbDisplay, fbSurface, mList); |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 280 | if (mList) { |
| 281 | mList->flags &= ~HWC_GEOMETRY_CHANGED; |
| 282 | } |
Mathias Agopian | 5895934 | 2010-10-07 14:57:04 -0700 | [diff] [blame] | 283 | } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 284 | return (status_t)err; |
| 285 | } |
| 286 | |
Antti Hatala | f5f2712 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 287 | status_t HWComposer::release() const { |
Mathias Agopian | 7ee4cd5 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 288 | if (mHwc) { |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 289 | if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_3)) { |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 290 | mHwc->methods->eventControl(mHwc, HWC_EVENT_VSYNC, 0); |
| 291 | } |
Mathias Agopian | 7ee4cd5 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 292 | int err = mHwc->set(mHwc, NULL, NULL, NULL); |
Colin Cross | 10fbdb6 | 2012-07-12 17:56:34 -0700 | [diff] [blame] | 293 | if (err < 0) { |
| 294 | return (status_t)err; |
| 295 | } |
| 296 | |
| 297 | if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) { |
| 298 | if (mHwc->methods && mHwc->methods->blank) { |
| 299 | err = mHwc->methods->blank(mHwc, 1); |
| 300 | } |
| 301 | } |
Mathias Agopian | 7ee4cd5 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 302 | return (status_t)err; |
| 303 | } |
| 304 | return NO_ERROR; |
| 305 | } |
| 306 | |
Colin Cross | 10fbdb6 | 2012-07-12 17:56:34 -0700 | [diff] [blame] | 307 | status_t HWComposer::acquire() const { |
| 308 | if (mHwc) { |
| 309 | if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) { |
| 310 | if (mHwc->methods && mHwc->methods->blank) { |
| 311 | int err = mHwc->methods->blank(mHwc, 0); |
| 312 | return (status_t)err; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return NO_ERROR; |
| 318 | } |
| 319 | |
Mathias Agopian | 7ee4cd5 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 320 | status_t HWComposer::disable() { |
| 321 | if (mHwc) { |
| 322 | free(mList); |
| 323 | mList = NULL; |
| 324 | int err = mHwc->prepare(mHwc, NULL); |
| 325 | return (status_t)err; |
| 326 | } |
| 327 | return NO_ERROR; |
Antti Hatala | f5f2712 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Mathias Agopian | 4572177 | 2010-08-12 15:03:26 -0700 | [diff] [blame] | 330 | size_t HWComposer::getNumLayers() const { |
| 331 | return mList ? mList->numHwLayers : 0; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 334 | /* |
| 335 | * Helper template to implement a concrete HWCLayer |
| 336 | * This holds the pointer to the concrete hwc layer type |
| 337 | * and implements the "iterable" side of HWCLayer. |
| 338 | */ |
| 339 | template<typename CONCRETE, typename HWCTYPE> |
| 340 | class Iterable : public HWComposer::HWCLayer { |
| 341 | protected: |
| 342 | HWCTYPE* const mLayerList; |
| 343 | HWCTYPE* mCurrentLayer; |
| 344 | Iterable(HWCTYPE* layer) : mLayerList(layer), mCurrentLayer(layer) { } |
| 345 | inline HWCTYPE const * getLayer() const { return mCurrentLayer; } |
| 346 | inline HWCTYPE* getLayer() { return mCurrentLayer; } |
| 347 | virtual ~Iterable() { } |
| 348 | private: |
| 349 | // returns a copy of ourselves |
| 350 | virtual HWComposer::HWCLayer* dup() { |
| 351 | return new CONCRETE( static_cast<const CONCRETE&>(*this) ); |
| 352 | } |
| 353 | virtual status_t setLayer(size_t index) { |
| 354 | mCurrentLayer = &mLayerList[index]; |
| 355 | return NO_ERROR; |
| 356 | } |
| 357 | }; |
| 358 | |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 359 | // #if !HWC_REMOVE_DEPRECATED_VERSIONS |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 360 | /* |
| 361 | * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_0_3 |
| 362 | * This implements the HWCLayer side of HWCIterableLayer. |
| 363 | */ |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 364 | class HWCLayerVersion0 : public Iterable<HWCLayerVersion0, hwc_layer_t> { |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 365 | public: |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 366 | HWCLayerVersion0(hwc_layer_t* layer) |
| 367 | : Iterable<HWCLayerVersion0, hwc_layer_t>(layer) { } |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 368 | |
| 369 | virtual int32_t getCompositionType() const { |
| 370 | return getLayer()->compositionType; |
| 371 | } |
| 372 | virtual uint32_t getHints() const { |
| 373 | return getLayer()->hints; |
| 374 | } |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 375 | virtual int getAndResetReleaseFenceFd() { |
| 376 | // not supported on VERSION_03 |
| 377 | return -1; |
| 378 | } |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 379 | virtual void setAcquireFenceFd(int fenceFd) { |
| 380 | if (fenceFd != -1) { |
| 381 | ALOGE("HWC 0.x can't handle acquire fences"); |
| 382 | close(fenceFd); |
| 383 | } |
| 384 | } |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 385 | |
| 386 | virtual void setDefaultState() { |
| 387 | getLayer()->compositionType = HWC_FRAMEBUFFER; |
| 388 | getLayer()->hints = 0; |
| 389 | getLayer()->flags = HWC_SKIP_LAYER; |
| 390 | getLayer()->transform = 0; |
| 391 | getLayer()->blending = HWC_BLENDING_NONE; |
| 392 | getLayer()->visibleRegionScreen.numRects = 0; |
| 393 | getLayer()->visibleRegionScreen.rects = NULL; |
| 394 | } |
| 395 | virtual void setSkip(bool skip) { |
| 396 | if (skip) { |
| 397 | getLayer()->flags |= HWC_SKIP_LAYER; |
| 398 | } else { |
| 399 | getLayer()->flags &= ~HWC_SKIP_LAYER; |
| 400 | } |
| 401 | } |
| 402 | virtual void setBlending(uint32_t blending) { |
| 403 | getLayer()->blending = blending; |
| 404 | } |
| 405 | virtual void setTransform(uint32_t transform) { |
| 406 | getLayer()->transform = transform; |
| 407 | } |
| 408 | virtual void setFrame(const Rect& frame) { |
| 409 | reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame; |
| 410 | } |
| 411 | virtual void setCrop(const Rect& crop) { |
| 412 | reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop; |
| 413 | } |
| 414 | virtual void setVisibleRegionScreen(const Region& reg) { |
| 415 | getLayer()->visibleRegionScreen.rects = |
| 416 | reinterpret_cast<hwc_rect_t const *>( |
| 417 | reg.getArray(&getLayer()->visibleRegionScreen.numRects)); |
| 418 | } |
| 419 | virtual void setBuffer(const sp<GraphicBuffer>& buffer) { |
| 420 | if (buffer == 0 || buffer->handle == 0) { |
| 421 | getLayer()->compositionType = HWC_FRAMEBUFFER; |
| 422 | getLayer()->flags |= HWC_SKIP_LAYER; |
| 423 | getLayer()->handle = 0; |
| 424 | } else { |
| 425 | getLayer()->handle = buffer->handle; |
| 426 | } |
| 427 | } |
| 428 | }; |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 429 | // #endif // !HWC_REMOVE_DEPRECATED_VERSIONS |
| 430 | |
| 431 | /* |
| 432 | * Concrete implementation of HWCLayer for HWC_DEVICE_API_VERSION_1_0. |
| 433 | * This implements the HWCLayer side of HWCIterableLayer. |
| 434 | */ |
| 435 | class HWCLayerVersion1 : public Iterable<HWCLayerVersion1, hwc_layer_1_t> { |
| 436 | public: |
| 437 | HWCLayerVersion1(hwc_layer_1_t* layer) |
| 438 | : Iterable<HWCLayerVersion1, hwc_layer_1_t>(layer) { } |
| 439 | |
| 440 | virtual int32_t getCompositionType() const { |
| 441 | return getLayer()->compositionType; |
| 442 | } |
| 443 | virtual uint32_t getHints() const { |
| 444 | return getLayer()->hints; |
| 445 | } |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 446 | virtual int getAndResetReleaseFenceFd() { |
| 447 | int fd = getLayer()->releaseFenceFd; |
| 448 | getLayer()->releaseFenceFd = -1; |
| 449 | return fd; |
| 450 | } |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 451 | virtual void setAcquireFenceFd(int fenceFd) { |
| 452 | getLayer()->acquireFenceFd = fenceFd; |
| 453 | } |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 454 | |
| 455 | virtual void setDefaultState() { |
| 456 | getLayer()->compositionType = HWC_FRAMEBUFFER; |
| 457 | getLayer()->hints = 0; |
| 458 | getLayer()->flags = HWC_SKIP_LAYER; |
| 459 | getLayer()->transform = 0; |
| 460 | getLayer()->blending = HWC_BLENDING_NONE; |
| 461 | getLayer()->visibleRegionScreen.numRects = 0; |
| 462 | getLayer()->visibleRegionScreen.rects = NULL; |
| 463 | getLayer()->acquireFenceFd = -1; |
| 464 | getLayer()->releaseFenceFd = -1; |
| 465 | } |
| 466 | virtual void setSkip(bool skip) { |
| 467 | if (skip) { |
| 468 | getLayer()->flags |= HWC_SKIP_LAYER; |
| 469 | } else { |
| 470 | getLayer()->flags &= ~HWC_SKIP_LAYER; |
| 471 | } |
| 472 | } |
| 473 | virtual void setBlending(uint32_t blending) { |
| 474 | getLayer()->blending = blending; |
| 475 | } |
| 476 | virtual void setTransform(uint32_t transform) { |
| 477 | getLayer()->transform = transform; |
| 478 | } |
| 479 | virtual void setFrame(const Rect& frame) { |
| 480 | reinterpret_cast<Rect&>(getLayer()->displayFrame) = frame; |
| 481 | } |
| 482 | virtual void setCrop(const Rect& crop) { |
| 483 | reinterpret_cast<Rect&>(getLayer()->sourceCrop) = crop; |
| 484 | } |
| 485 | virtual void setVisibleRegionScreen(const Region& reg) { |
| 486 | getLayer()->visibleRegionScreen.rects = |
| 487 | reinterpret_cast<hwc_rect_t const *>( |
| 488 | reg.getArray(&getLayer()->visibleRegionScreen.numRects)); |
| 489 | } |
| 490 | virtual void setBuffer(const sp<GraphicBuffer>& buffer) { |
| 491 | if (buffer == 0 || buffer->handle == 0) { |
| 492 | getLayer()->compositionType = HWC_FRAMEBUFFER; |
| 493 | getLayer()->flags |= HWC_SKIP_LAYER; |
| 494 | getLayer()->handle = 0; |
| 495 | } else { |
| 496 | getLayer()->handle = buffer->handle; |
| 497 | } |
| 498 | } |
| 499 | }; |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 500 | |
| 501 | /* |
| 502 | * returns an iterator initialized at a given index in the layer list |
| 503 | */ |
| 504 | HWComposer::LayerListIterator HWComposer::getLayerIterator(size_t index) { |
| 505 | if (!mList || index > mList->numHwLayers) { |
| 506 | return LayerListIterator(); |
| 507 | } |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 508 | if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) { |
| 509 | return LayerListIterator(new HWCLayerVersion1(mList->hwLayers), index); |
| 510 | } else { |
| 511 | hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList; |
| 512 | return LayerListIterator(new HWCLayerVersion0(list0->hwLayers), index); |
| 513 | } |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 516 | /* |
| 517 | * returns an iterator on the beginning of the layer list |
| 518 | */ |
| 519 | HWComposer::LayerListIterator HWComposer::begin() { |
| 520 | return getLayerIterator(0); |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * returns an iterator on the end of the layer list |
| 525 | */ |
| 526 | HWComposer::LayerListIterator HWComposer::end() { |
| 527 | return getLayerIterator(getNumLayers()); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | |
Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 532 | void HWComposer::dump(String8& result, char* buffer, size_t SIZE, |
| 533 | const Vector< sp<LayerBase> >& visibleLayersSortedByZ) const { |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 534 | if (mHwc && mList) { |
| 535 | result.append("Hardware Composer state:\n"); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 536 | result.appendFormat(" mDebugForceFakeVSync=%d\n", |
| 537 | mDebugForceFakeVSync); |
| 538 | result.appendFormat(" numHwLayers=%u, flags=%08x\n", |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 539 | mList->numHwLayers, mList->flags); |
Mathias Agopian | fb4d5d5 | 2011-09-20 15:13:14 -0700 | [diff] [blame] | 540 | result.append( |
Mathias Agopian | aebac5f | 2011-09-29 18:07:08 -0700 | [diff] [blame] | 541 | " type | handle | hints | flags | tr | blend | format | source crop | frame name \n" |
| 542 | "----------+----------+----------+----------+----+-------+----------+---------------------------+--------------------------------\n"); |
| 543 | // " ________ | ________ | ________ | ________ | __ | _____ | ________ | [_____,_____,_____,_____] | [_____,_____,_____,_____] |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 544 | for (size_t i=0 ; i<mList->numHwLayers ; i++) { |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 545 | hwc_layer_1_t l; |
| 546 | if (hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_1_0)) { |
| 547 | l = mList->hwLayers[i]; |
| 548 | } else { |
| 549 | hwc_layer_list_t* list0 = (hwc_layer_list_t*)mList; |
| 550 | *(hwc_layer_t*)&l = list0->hwLayers[i]; |
| 551 | l.acquireFenceFd = l.releaseFenceFd = -1; |
| 552 | } |
Mathias Agopian | fb4d5d5 | 2011-09-20 15:13:14 -0700 | [diff] [blame] | 553 | const sp<LayerBase> layer(visibleLayersSortedByZ[i]); |
| 554 | int32_t format = -1; |
| 555 | if (layer->getLayer() != NULL) { |
| 556 | const sp<GraphicBuffer>& buffer(layer->getLayer()->getActiveBuffer()); |
| 557 | if (buffer != NULL) { |
| 558 | format = buffer->getPixelFormat(); |
| 559 | } |
| 560 | } |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 561 | result.appendFormat( |
Mathias Agopian | aebac5f | 2011-09-29 18:07:08 -0700 | [diff] [blame] | 562 | " %8s | %08x | %08x | %08x | %02x | %05x | %08x | [%5d,%5d,%5d,%5d] | [%5d,%5d,%5d,%5d] %s\n", |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 563 | l.compositionType ? "OVERLAY" : "FB", |
Mathias Agopian | aebac5f | 2011-09-29 18:07:08 -0700 | [diff] [blame] | 564 | intptr_t(l.handle), l.hints, l.flags, l.transform, l.blending, format, |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 565 | l.sourceCrop.left, l.sourceCrop.top, l.sourceCrop.right, l.sourceCrop.bottom, |
Mathias Agopian | 22da60c | 2011-09-09 00:49:11 -0700 | [diff] [blame] | 566 | l.displayFrame.left, l.displayFrame.top, l.displayFrame.right, l.displayFrame.bottom, |
Mathias Agopian | fb4d5d5 | 2011-09-20 15:13:14 -0700 | [diff] [blame] | 567 | layer->getName().string()); |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 568 | } |
Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 569 | } |
Jesse Hall | 5880cc5 | 2012-06-05 23:40:32 -0700 | [diff] [blame] | 570 | if (mHwc && hwcHasVersion(mHwc, HWC_DEVICE_API_VERSION_0_1) && mHwc->dump) { |
Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 571 | mHwc->dump(mHwc, buffer, SIZE); |
| 572 | result.append(buffer); |
Mathias Agopian | 8372785 | 2010-09-23 18:13:21 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 576 | // --------------------------------------------------------------------------- |
Mathias Agopian | 2965b26 | 2012-04-08 15:13:32 -0700 | [diff] [blame] | 577 | |
| 578 | HWComposer::VSyncThread::VSyncThread(HWComposer& hwc) |
| 579 | : mHwc(hwc), mEnabled(false), |
| 580 | mNextFakeVSync(0), |
| 581 | mRefreshPeriod(hwc.mRefreshPeriod) |
| 582 | { |
| 583 | } |
| 584 | |
| 585 | void HWComposer::VSyncThread::setEnabled(bool enabled) { |
| 586 | Mutex::Autolock _l(mLock); |
| 587 | mEnabled = enabled; |
| 588 | mCondition.signal(); |
| 589 | } |
| 590 | |
| 591 | void HWComposer::VSyncThread::onFirstRef() { |
| 592 | run("VSyncThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); |
| 593 | } |
| 594 | |
| 595 | bool HWComposer::VSyncThread::threadLoop() { |
| 596 | { // scope for lock |
| 597 | Mutex::Autolock _l(mLock); |
| 598 | while (!mEnabled) { |
| 599 | mCondition.wait(mLock); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | const nsecs_t period = mRefreshPeriod; |
| 604 | const nsecs_t now = systemTime(CLOCK_MONOTONIC); |
| 605 | nsecs_t next_vsync = mNextFakeVSync; |
| 606 | nsecs_t sleep = next_vsync - now; |
| 607 | if (sleep < 0) { |
| 608 | // we missed, find where the next vsync should be |
| 609 | sleep = (period - ((now - next_vsync) % period)); |
| 610 | next_vsync = now + sleep; |
| 611 | } |
| 612 | mNextFakeVSync = next_vsync + period; |
| 613 | |
| 614 | struct timespec spec; |
| 615 | spec.tv_sec = next_vsync / 1000000000; |
| 616 | spec.tv_nsec = next_vsync % 1000000000; |
| 617 | |
| 618 | int err; |
| 619 | do { |
| 620 | err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL); |
| 621 | } while (err<0 && errno == EINTR); |
| 622 | |
| 623 | if (err == 0) { |
| 624 | mHwc.mEventHandler.onVSyncReceived(0, next_vsync); |
| 625 | } |
| 626 | |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | // --------------------------------------------------------------------------- |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 631 | }; // namespace android |