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