The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | #define LOG_TAG "Surface" |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | //#define LOG_NDEBUG 0 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 21 | #include <gui/Surface.h> |
Mathias Agopian | b0e76f4 | 2012-03-23 14:15:44 -0700 | [diff] [blame] | 22 | |
Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 23 | #include <android/native_window.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 24 | |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 26 | #include <utils/Trace.h> |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 27 | #include <utils/NativeHandle.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 28 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 29 | #include <ui/Fence.h> |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 30 | #include <ui/Region.h> |
Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 31 | #include <ui/DisplayStatInfo.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 32 | |
Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 33 | #include <gui/BufferItem.h> |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 34 | #include <gui/IProducerListener.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 35 | |
Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 36 | #include <gui/ISurfaceComposer.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 37 | #include <private/gui/ComposerService.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 38 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 41 | Surface::Surface( |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 42 | const sp<IGraphicBufferProducer>& bufferProducer, |
| 43 | bool controlledByApp) |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 44 | : mGraphicBufferProducer(bufferProducer), |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 45 | mCrop(Rect::EMPTY_RECT), |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 46 | mGenerationNumber(0), |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 47 | mSharedBufferMode(false), |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 48 | mAutoRefresh(false), |
| 49 | mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT), |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 50 | mSharedBufferHasBeenQueued(false), |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 51 | mQueriedSupportedTimestamps(false), |
| 52 | mFrameTimestampsSupportsPresent(false), |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 53 | mFrameTimestampsSupportsRetire(false), |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 54 | mEnableFrameTimestamps(false), |
| 55 | mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 56 | { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 57 | // Initialize the ANativeWindow function pointers. |
| 58 | ANativeWindow::setSwapInterval = hook_setSwapInterval; |
| 59 | ANativeWindow::dequeueBuffer = hook_dequeueBuffer; |
| 60 | ANativeWindow::cancelBuffer = hook_cancelBuffer; |
| 61 | ANativeWindow::queueBuffer = hook_queueBuffer; |
| 62 | ANativeWindow::query = hook_query; |
| 63 | ANativeWindow::perform = hook_perform; |
| 64 | |
| 65 | ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED; |
| 66 | ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED; |
| 67 | ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED; |
| 68 | ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED; |
| 69 | |
| 70 | const_cast<int&>(ANativeWindow::minSwapInterval) = 0; |
| 71 | const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; |
| 72 | |
| 73 | mReqWidth = 0; |
| 74 | mReqHeight = 0; |
| 75 | mReqFormat = 0; |
| 76 | mReqUsage = 0; |
| 77 | mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 78 | mDataSpace = HAL_DATASPACE_UNKNOWN; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 79 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 80 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 81 | mStickyTransform = 0; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 82 | mDefaultWidth = 0; |
| 83 | mDefaultHeight = 0; |
| 84 | mUserWidth = 0; |
| 85 | mUserHeight = 0; |
| 86 | mTransformHint = 0; |
| 87 | mConsumerRunningBehind = false; |
| 88 | mConnectedToCpu = false; |
Eino-Ville Talvala | 7895e90 | 2013-08-21 11:53:37 -0700 | [diff] [blame] | 89 | mProducerControlledByApp = controlledByApp; |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 90 | mSwapIntervalZero = false; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Mathias Agopian | 35ffa6a | 2013-03-12 18:45:09 -0700 | [diff] [blame] | 93 | Surface::~Surface() { |
| 94 | if (mConnectedToCpu) { |
| 95 | Surface::disconnect(NATIVE_WINDOW_API_CPU); |
| 96 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 99 | sp<ISurfaceComposer> Surface::composerService() const { |
| 100 | return ComposerService::getComposerService(); |
| 101 | } |
| 102 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 103 | nsecs_t Surface::now() const { |
| 104 | return systemTime(); |
| 105 | } |
| 106 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 107 | sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const { |
| 108 | return mGraphicBufferProducer; |
| 109 | } |
| 110 | |
Wonsik Kim | 0ee14ca | 2014-03-17 17:46:53 +0900 | [diff] [blame] | 111 | void Surface::setSidebandStream(const sp<NativeHandle>& stream) { |
| 112 | mGraphicBufferProducer->setSidebandStream(stream); |
| 113 | } |
| 114 | |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 115 | void Surface::allocateBuffers() { |
| 116 | uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 117 | uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 118 | mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight, |
| 119 | mReqFormat, mReqUsage); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 122 | status_t Surface::setGenerationNumber(uint32_t generation) { |
| 123 | status_t result = mGraphicBufferProducer->setGenerationNumber(generation); |
| 124 | if (result == NO_ERROR) { |
| 125 | mGenerationNumber = generation; |
| 126 | } |
| 127 | return result; |
| 128 | } |
| 129 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 130 | uint64_t Surface::getNextFrameNumber() const { |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 131 | Mutex::Autolock lock(mMutex); |
| 132 | return mNextFrameNumber; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 135 | String8 Surface::getConsumerName() const { |
| 136 | return mGraphicBufferProducer->getConsumerName(); |
| 137 | } |
| 138 | |
Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 139 | status_t Surface::setDequeueTimeout(nsecs_t timeout) { |
| 140 | return mGraphicBufferProducer->setDequeueTimeout(timeout); |
| 141 | } |
| 142 | |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 143 | status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 144 | sp<Fence>* outFence, float outTransformMatrix[16]) { |
| 145 | return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence, |
| 146 | outTransformMatrix); |
Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 149 | status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) { |
| 150 | ATRACE_CALL(); |
| 151 | |
| 152 | DisplayStatInfo stats; |
| 153 | status_t err = composerService()->getDisplayStats(NULL, &stats); |
| 154 | |
| 155 | *outRefreshDuration = stats.vsyncPeriod; |
| 156 | |
| 157 | return NO_ERROR; |
| 158 | } |
| 159 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 160 | void Surface::enableFrameTimestamps(bool enable) { |
| 161 | Mutex::Autolock lock(mMutex); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 162 | // If going from disabled to enabled, get the initial values for |
| 163 | // compositor and display timing. |
| 164 | if (!mEnableFrameTimestamps && enable) { |
| 165 | FrameEventHistoryDelta delta; |
| 166 | mGraphicBufferProducer->getFrameTimestamps(&delta); |
| 167 | mFrameEventHistory->applyDelta(delta); |
| 168 | } |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 169 | mEnableFrameTimestamps = enable; |
| 170 | } |
| 171 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 172 | status_t Surface::getCompositorTiming( |
| 173 | nsecs_t* compositeDeadline, nsecs_t* compositeInterval, |
| 174 | nsecs_t* compositeToPresentLatency) { |
| 175 | Mutex::Autolock lock(mMutex); |
| 176 | if (!mEnableFrameTimestamps) { |
| 177 | return INVALID_OPERATION; |
| 178 | } |
| 179 | |
| 180 | if (compositeDeadline != nullptr) { |
| 181 | *compositeDeadline = |
| 182 | mFrameEventHistory->getNextCompositeDeadline(now()); |
| 183 | } |
| 184 | if (compositeInterval != nullptr) { |
| 185 | *compositeInterval = mFrameEventHistory->getCompositeInterval(); |
| 186 | } |
| 187 | if (compositeToPresentLatency != nullptr) { |
| 188 | *compositeToPresentLatency = |
| 189 | mFrameEventHistory->getCompositeToPresentLatency(); |
| 190 | } |
| 191 | return NO_ERROR; |
| 192 | } |
| 193 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 194 | static bool checkConsumerForUpdates( |
| 195 | const FrameEvents* e, const uint64_t lastFrameNumber, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 196 | const nsecs_t* outLatchTime, |
| 197 | const nsecs_t* outFirstRefreshStartTime, |
| 198 | const nsecs_t* outLastRefreshStartTime, |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 199 | const nsecs_t* outGlCompositionDoneTime, |
| 200 | const nsecs_t* outDisplayPresentTime, |
| 201 | const nsecs_t* outDisplayRetireTime, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 202 | const nsecs_t* outDequeueReadyTime, |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 203 | const nsecs_t* outReleaseTime) { |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 204 | bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo(); |
| 205 | bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) && |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 206 | !e->hasFirstRefreshStartInfo(); |
| 207 | bool checkForGlCompositionDone = (outGlCompositionDoneTime != nullptr) && |
| 208 | !e->hasGpuCompositionDoneInfo(); |
| 209 | bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) && |
| 210 | !e->hasDisplayPresentInfo(); |
| 211 | |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 212 | // LastRefreshStart, DisplayRetire, DequeueReady, and Release are never |
| 213 | // available for the last frame. |
| 214 | bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) && |
| 215 | !e->hasLastRefreshStartInfo() && |
| 216 | (e->frameNumber != lastFrameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 217 | bool checkForDisplayRetire = (outDisplayRetireTime != nullptr) && |
| 218 | !e->hasDisplayRetireInfo() && (e->frameNumber != lastFrameNumber); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 219 | bool checkForDequeueReady = (outDequeueReadyTime != nullptr) && |
| 220 | !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 221 | bool checkForRelease = (outReleaseTime != nullptr) && |
| 222 | !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber); |
| 223 | |
| 224 | // RequestedPresent and Acquire info are always available producer-side. |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 225 | return checkForLatch || checkForFirstRefreshStart || |
| 226 | checkForLastRefreshStart || checkForGlCompositionDone || |
| 227 | checkForDisplayPresent || checkForDisplayRetire || |
| 228 | checkForDequeueReady || checkForRelease; |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 231 | static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) { |
| 232 | if (dst != nullptr) { |
| 233 | *dst = Fence::isValidTimestamp(src) ? src : 0; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | static void getFrameTimestampFence(nsecs_t *dst, const std::shared_ptr<FenceTime>& src) { |
| 238 | if (dst != nullptr) { |
| 239 | nsecs_t signalTime = src->getSignalTime(); |
| 240 | *dst = Fence::isValidTimestamp(signalTime) ? signalTime : 0; |
| 241 | } |
| 242 | } |
| 243 | |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 244 | status_t Surface::getFrameTimestamps(uint64_t frameNumber, |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 245 | nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 246 | nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime, |
| 247 | nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime, |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 248 | nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 249 | nsecs_t* outDequeueReadyTime, nsecs_t* outReleaseTime) { |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 250 | ATRACE_CALL(); |
| 251 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 252 | Mutex::Autolock lock(mMutex); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 253 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 254 | if (!mEnableFrameTimestamps) { |
| 255 | return INVALID_OPERATION; |
| 256 | } |
| 257 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 258 | // Verify the requested timestamps are supported. |
| 259 | querySupportedTimestampsLocked(); |
| 260 | if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) { |
| 261 | return BAD_VALUE; |
| 262 | } |
| 263 | if (outDisplayRetireTime != nullptr && !mFrameTimestampsSupportsRetire) { |
| 264 | return BAD_VALUE; |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 267 | FrameEvents* events = mFrameEventHistory->getFrame(frameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 268 | if (events == nullptr) { |
| 269 | // If the entry isn't available in the producer, it's definitely not |
| 270 | // available in the consumer. |
| 271 | return NAME_NOT_FOUND; |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 274 | // Update our cache of events if the requested events are not available. |
| 275 | if (checkConsumerForUpdates(events, mLastFrameNumber, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 276 | outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime, |
| 277 | outGlCompositionDoneTime, outDisplayPresentTime, |
| 278 | outDisplayRetireTime, outDequeueReadyTime, outReleaseTime)) { |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 279 | FrameEventHistoryDelta delta; |
| 280 | mGraphicBufferProducer->getFrameTimestamps(&delta); |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 281 | mFrameEventHistory->applyDelta(delta); |
| 282 | events = mFrameEventHistory->getFrame(frameNumber); |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 285 | if (events == nullptr) { |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 286 | // The entry was available before the update, but was overwritten |
| 287 | // after the update. Make sure not to send the wrong frame's data. |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 288 | return NAME_NOT_FOUND; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 289 | } |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 290 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 291 | getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 292 | getFrameTimestamp(outLatchTime, events->latchTime); |
| 293 | getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime); |
| 294 | getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime); |
| 295 | getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime); |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 296 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 297 | getFrameTimestampFence(outAcquireTime, events->acquireFence); |
| 298 | getFrameTimestampFence( |
| 299 | outGlCompositionDoneTime, events->gpuCompositionDoneFence); |
| 300 | getFrameTimestampFence( |
| 301 | outDisplayPresentTime, events->displayPresentFence); |
| 302 | getFrameTimestampFence(outDisplayRetireTime, events->displayRetireFence); |
| 303 | getFrameTimestampFence(outReleaseTime, events->releaseFence); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 304 | |
| 305 | return NO_ERROR; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 308 | int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) { |
| 309 | Surface* c = getSelf(window); |
| 310 | return c->setSwapInterval(interval); |
| 311 | } |
| 312 | |
| 313 | int Surface::hook_dequeueBuffer(ANativeWindow* window, |
| 314 | ANativeWindowBuffer** buffer, int* fenceFd) { |
| 315 | Surface* c = getSelf(window); |
| 316 | return c->dequeueBuffer(buffer, fenceFd); |
| 317 | } |
| 318 | |
| 319 | int Surface::hook_cancelBuffer(ANativeWindow* window, |
| 320 | ANativeWindowBuffer* buffer, int fenceFd) { |
| 321 | Surface* c = getSelf(window); |
| 322 | return c->cancelBuffer(buffer, fenceFd); |
| 323 | } |
| 324 | |
| 325 | int Surface::hook_queueBuffer(ANativeWindow* window, |
| 326 | ANativeWindowBuffer* buffer, int fenceFd) { |
| 327 | Surface* c = getSelf(window); |
| 328 | return c->queueBuffer(buffer, fenceFd); |
| 329 | } |
| 330 | |
| 331 | int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, |
| 332 | ANativeWindowBuffer** buffer) { |
| 333 | Surface* c = getSelf(window); |
| 334 | ANativeWindowBuffer* buf; |
| 335 | int fenceFd = -1; |
| 336 | int result = c->dequeueBuffer(&buf, &fenceFd); |
Mike Stroyan | 87709c9 | 2016-06-03 12:43:26 -0600 | [diff] [blame] | 337 | if (result != OK) { |
| 338 | return result; |
| 339 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 340 | sp<Fence> fence(new Fence(fenceFd)); |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 341 | int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED"); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 342 | if (waitResult != OK) { |
| 343 | ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d", |
| 344 | waitResult); |
| 345 | c->cancelBuffer(buf, -1); |
| 346 | return waitResult; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 347 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 348 | *buffer = buf; |
| 349 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 352 | int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window, |
| 353 | ANativeWindowBuffer* buffer) { |
| 354 | Surface* c = getSelf(window); |
| 355 | return c->cancelBuffer(buffer, -1); |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 358 | int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window, |
| 359 | ANativeWindowBuffer* buffer) { |
| 360 | Surface* c = getSelf(window); |
| 361 | return c->lockBuffer_DEPRECATED(buffer); |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 364 | int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window, |
| 365 | ANativeWindowBuffer* buffer) { |
| 366 | Surface* c = getSelf(window); |
| 367 | return c->queueBuffer(buffer, -1); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 368 | } |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 369 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 370 | int Surface::hook_query(const ANativeWindow* window, |
| 371 | int what, int* value) { |
| 372 | const Surface* c = getSelf(window); |
| 373 | return c->query(what, value); |
| 374 | } |
| 375 | |
| 376 | int Surface::hook_perform(ANativeWindow* window, int operation, ...) { |
| 377 | va_list args; |
| 378 | va_start(args, operation); |
| 379 | Surface* c = getSelf(window); |
Haixia Shi | d89c2bb | 2015-09-14 11:02:18 -0700 | [diff] [blame] | 380 | int result = c->perform(operation, args); |
| 381 | va_end(args); |
| 382 | return result; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | int Surface::setSwapInterval(int interval) { |
| 386 | ATRACE_CALL(); |
| 387 | // EGL specification states: |
| 388 | // interval is silently clamped to minimum and maximum implementation |
| 389 | // dependent values before being stored. |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 390 | |
| 391 | if (interval < minSwapInterval) |
| 392 | interval = minSwapInterval; |
| 393 | |
| 394 | if (interval > maxSwapInterval) |
| 395 | interval = maxSwapInterval; |
| 396 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 397 | mSwapIntervalZero = (interval == 0); |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 398 | mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 399 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 400 | return NO_ERROR; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 403 | int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 404 | ATRACE_CALL(); |
| 405 | ALOGV("Surface::dequeueBuffer"); |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 406 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 407 | uint32_t reqWidth; |
| 408 | uint32_t reqHeight; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 409 | PixelFormat reqFormat; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 410 | uint32_t reqUsage; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 411 | bool enableFrameTimestamps; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 412 | |
| 413 | { |
| 414 | Mutex::Autolock lock(mMutex); |
| 415 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 416 | reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 417 | reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 418 | |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 419 | reqFormat = mReqFormat; |
| 420 | reqUsage = mReqUsage; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 421 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 422 | enableFrameTimestamps = mEnableFrameTimestamps; |
| 423 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 424 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot != |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 425 | BufferItem::INVALID_BUFFER_SLOT) { |
| 426 | sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer); |
| 427 | if (gbuf != NULL) { |
| 428 | *buffer = gbuf.get(); |
| 429 | *fenceFd = -1; |
| 430 | return OK; |
| 431 | } |
| 432 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 433 | } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer |
| 434 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 435 | int buf = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 436 | sp<Fence> fence; |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 437 | nsecs_t now = systemTime(); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 438 | |
| 439 | FrameEventHistoryDelta frameTimestamps; |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 440 | status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 441 | reqWidth, reqHeight, reqFormat, reqUsage, |
| 442 | enableFrameTimestamps ? &frameTimestamps : nullptr); |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 443 | mLastDequeueDuration = systemTime() - now; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 444 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 445 | if (result < 0) { |
Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 446 | ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer" |
| 447 | "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat, |
| 448 | reqUsage, result); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 449 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 450 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 451 | |
| 452 | Mutex::Autolock lock(mMutex); |
| 453 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 454 | sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 455 | |
| 456 | // this should never happen |
| 457 | ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf); |
| 458 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 459 | if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { |
| 460 | freeAllBuffers(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 461 | } |
Ted Bonkenburg | bd050ab | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 462 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 463 | if (enableFrameTimestamps) { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 464 | mFrameEventHistory->applyDelta(frameTimestamps); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 467 | if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) { |
| 468 | result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); |
| 469 | if (result != NO_ERROR) { |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 470 | ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); |
Jesse Hall | 9f5a1b6 | 2014-10-02 11:09:03 -0700 | [diff] [blame] | 471 | mGraphicBufferProducer->cancelBuffer(buf, fence); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 472 | return result; |
| 473 | } |
| 474 | } |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 475 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 476 | if (fence->isValid()) { |
| 477 | *fenceFd = fence->dup(); |
| 478 | if (*fenceFd == -1) { |
| 479 | ALOGE("dequeueBuffer: error duping fence: %d", errno); |
| 480 | // dup() should never fail; something is badly wrong. Soldier on |
| 481 | // and hope for the best; the worst that should happen is some |
| 482 | // visible corruption that lasts until the next frame. |
| 483 | } |
Ted Bonkenburg | e5d6eb8 | 2011-08-09 22:38:41 -0700 | [diff] [blame] | 484 | } else { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 485 | *fenceFd = -1; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 486 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 487 | |
| 488 | *buffer = gbuf.get(); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 489 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 490 | if (mSharedBufferMode && mAutoRefresh) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 491 | mSharedBufferSlot = buf; |
| 492 | mSharedBufferHasBeenQueued = false; |
| 493 | } else if (mSharedBufferSlot == buf) { |
| 494 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 495 | mSharedBufferHasBeenQueued = false; |
| 496 | } |
| 497 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 498 | return OK; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 501 | int Surface::cancelBuffer(android_native_buffer_t* buffer, |
| 502 | int fenceFd) { |
| 503 | ATRACE_CALL(); |
| 504 | ALOGV("Surface::cancelBuffer"); |
| 505 | Mutex::Autolock lock(mMutex); |
| 506 | int i = getSlotFromBufferLocked(buffer); |
| 507 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 508 | if (fenceFd >= 0) { |
| 509 | close(fenceFd); |
| 510 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 511 | return i; |
| 512 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 513 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { |
| 514 | if (fenceFd >= 0) { |
| 515 | close(fenceFd); |
| 516 | } |
| 517 | return OK; |
| 518 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 519 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 520 | mGraphicBufferProducer->cancelBuffer(i, fence); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 521 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 522 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 523 | mSharedBufferHasBeenQueued = true; |
| 524 | } |
| 525 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 526 | return OK; |
| 527 | } |
| 528 | |
| 529 | int Surface::getSlotFromBufferLocked( |
| 530 | android_native_buffer_t* buffer) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 531 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 532 | if (mSlots[i].buffer != NULL && |
| 533 | mSlots[i].buffer->handle == buffer->handle) { |
| 534 | return i; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 537 | ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); |
| 538 | return BAD_VALUE; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 541 | int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 542 | ALOGV("Surface::lockBuffer"); |
| 543 | Mutex::Autolock lock(mMutex); |
| 544 | return OK; |
| 545 | } |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 546 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 547 | int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) { |
| 548 | ATRACE_CALL(); |
| 549 | ALOGV("Surface::queueBuffer"); |
| 550 | Mutex::Autolock lock(mMutex); |
| 551 | int64_t timestamp; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 552 | bool isAutoTimestamp = false; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 553 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 554 | if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 555 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 556 | isAutoTimestamp = true; |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 557 | ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", |
Colin Cross | 9a80d50 | 2016-09-27 14:12:48 -0700 | [diff] [blame] | 558 | timestamp / 1000000.0); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 559 | } else { |
| 560 | timestamp = mTimestamp; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 561 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 562 | int i = getSlotFromBufferLocked(buffer); |
| 563 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 564 | if (fenceFd >= 0) { |
| 565 | close(fenceFd); |
| 566 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 567 | return i; |
| 568 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 569 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { |
| 570 | if (fenceFd >= 0) { |
| 571 | close(fenceFd); |
| 572 | } |
| 573 | return OK; |
| 574 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 577 | // Make sure the crop rectangle is entirely inside the buffer. |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 578 | Rect crop(Rect::EMPTY_RECT); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 579 | mCrop.intersect(Rect(buffer->width, buffer->height), &crop); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 581 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 582 | IGraphicBufferProducer::QueueBufferOutput output; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 583 | IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 584 | mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform, |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 585 | fence, mStickyTransform, mEnableFrameTimestamps); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 586 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 587 | if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 588 | input.setSurfaceDamage(Region::INVALID_REGION); |
| 589 | } else { |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 590 | // Here we do two things: |
| 591 | // 1) The surface damage was specified using the OpenGL ES convention of |
| 592 | // the origin being in the bottom-left corner. Here we flip to the |
| 593 | // convention that the rest of the system uses (top-left corner) by |
| 594 | // subtracting all top/bottom coordinates from the buffer height. |
| 595 | // 2) If the buffer is coming in rotated (for example, because the EGL |
| 596 | // implementation is reacting to the transform hint coming back from |
| 597 | // SurfaceFlinger), the surface damage needs to be rotated the |
| 598 | // opposite direction, since it was generated assuming an unrotated |
| 599 | // buffer (the app doesn't know that the EGL implementation is |
| 600 | // reacting to the transform hint behind its back). The |
| 601 | // transformations in the switch statement below apply those |
| 602 | // complementary rotations (e.g., if 90 degrees, rotate 270 degrees). |
| 603 | |
| 604 | int width = buffer->width; |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 605 | int height = buffer->height; |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 606 | bool rotated90 = (mTransform ^ mStickyTransform) & |
| 607 | NATIVE_WINDOW_TRANSFORM_ROT_90; |
| 608 | if (rotated90) { |
| 609 | std::swap(width, height); |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 610 | } |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 611 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 612 | Region flippedRegion; |
| 613 | for (auto rect : mDirtyRegion) { |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 614 | int left = rect.left; |
| 615 | int right = rect.right; |
| 616 | int top = height - rect.bottom; // Flip from OpenGL convention |
| 617 | int bottom = height - rect.top; // Flip from OpenGL convention |
| 618 | switch (mTransform ^ mStickyTransform) { |
| 619 | case NATIVE_WINDOW_TRANSFORM_ROT_90: { |
| 620 | // Rotate 270 degrees |
| 621 | Rect flippedRect{top, width - right, bottom, width - left}; |
| 622 | flippedRegion.orSelf(flippedRect); |
| 623 | break; |
| 624 | } |
| 625 | case NATIVE_WINDOW_TRANSFORM_ROT_180: { |
| 626 | // Rotate 180 degrees |
| 627 | Rect flippedRect{width - right, height - bottom, |
| 628 | width - left, height - top}; |
| 629 | flippedRegion.orSelf(flippedRect); |
| 630 | break; |
| 631 | } |
| 632 | case NATIVE_WINDOW_TRANSFORM_ROT_270: { |
| 633 | // Rotate 90 degrees |
| 634 | Rect flippedRect{height - bottom, left, |
| 635 | height - top, right}; |
| 636 | flippedRegion.orSelf(flippedRect); |
| 637 | break; |
| 638 | } |
| 639 | default: { |
| 640 | Rect flippedRect{left, top, right, bottom}; |
| 641 | flippedRegion.orSelf(flippedRect); |
| 642 | break; |
| 643 | } |
| 644 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | input.setSurfaceDamage(flippedRegion); |
| 648 | } |
| 649 | |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 650 | nsecs_t now = systemTime(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 651 | status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 652 | mLastQueueDuration = systemTime() - now; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 653 | if (err != OK) { |
| 654 | ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); |
| 655 | } |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 656 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 657 | if (mEnableFrameTimestamps) { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 658 | mFrameEventHistory->applyDelta(output.frameTimestamps); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 659 | // Update timestamps with the local acquire fence. |
| 660 | // The consumer doesn't send it back to prevent us from having two |
| 661 | // file descriptors of the same fence. |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 662 | mFrameEventHistory->updateAcquireFence(mNextFrameNumber, |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 663 | std::make_shared<FenceTime>(std::move(fence))); |
| 664 | |
| 665 | // Cache timestamps of signaled fences so we can close their file |
| 666 | // descriptors. |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 667 | mFrameEventHistory->updateSignalTimes(); |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 670 | mLastFrameNumber = mNextFrameNumber; |
| 671 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 672 | mDefaultWidth = output.width; |
| 673 | mDefaultHeight = output.height; |
| 674 | mNextFrameNumber = output.nextFrameNumber; |
tedbo | 1e7fa9e | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 675 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 676 | // Disable transform hint if sticky transform is set. |
| 677 | if (mStickyTransform == 0) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 678 | mTransformHint = output.transformHint; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 681 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 682 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 683 | if (!mConnectedToCpu) { |
| 684 | // Clear surface damage back to full-buffer |
| 685 | mDirtyRegion = Region::INVALID_REGION; |
| 686 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 687 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 688 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 689 | mSharedBufferHasBeenQueued = true; |
| 690 | } |
| 691 | |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 692 | mQueueBufferCondition.broadcast(); |
| 693 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 694 | return err; |
| 695 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 696 | |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 697 | void Surface::querySupportedTimestampsLocked() const { |
| 698 | // mMutex must be locked when calling this method. |
| 699 | |
| 700 | if (mQueriedSupportedTimestamps) { |
| 701 | return; |
| 702 | } |
| 703 | mQueriedSupportedTimestamps = true; |
| 704 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 705 | std::vector<FrameEvent> supportedFrameTimestamps; |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 706 | status_t err = composerService()->getSupportedFrameTimestamps( |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 707 | &supportedFrameTimestamps); |
| 708 | |
| 709 | if (err != NO_ERROR) { |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | for (auto sft : supportedFrameTimestamps) { |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 714 | if (sft == FrameEvent::DISPLAY_PRESENT) { |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 715 | mFrameTimestampsSupportsPresent = true; |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 716 | } else if (sft == FrameEvent::DISPLAY_RETIRE) { |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 717 | mFrameTimestampsSupportsRetire = true; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 722 | int Surface::query(int what, int* value) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 723 | ATRACE_CALL(); |
| 724 | ALOGV("Surface::query"); |
| 725 | { // scope for the lock |
| 726 | Mutex::Autolock lock(mMutex); |
| 727 | switch (what) { |
| 728 | case NATIVE_WINDOW_FORMAT: |
| 729 | if (mReqFormat) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 730 | *value = static_cast<int>(mReqFormat); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 731 | return NO_ERROR; |
| 732 | } |
| 733 | break; |
| 734 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: { |
Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 735 | if (composerService()->authenticateSurfaceTexture( |
| 736 | mGraphicBufferProducer)) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 737 | *value = 1; |
| 738 | } else { |
| 739 | *value = 0; |
| 740 | } |
| 741 | return NO_ERROR; |
| 742 | } |
| 743 | case NATIVE_WINDOW_CONCRETE_TYPE: |
| 744 | *value = NATIVE_WINDOW_SURFACE; |
| 745 | return NO_ERROR; |
| 746 | case NATIVE_WINDOW_DEFAULT_WIDTH: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 747 | *value = static_cast<int>( |
| 748 | mUserWidth ? mUserWidth : mDefaultWidth); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 749 | return NO_ERROR; |
| 750 | case NATIVE_WINDOW_DEFAULT_HEIGHT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 751 | *value = static_cast<int>( |
| 752 | mUserHeight ? mUserHeight : mDefaultHeight); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 753 | return NO_ERROR; |
| 754 | case NATIVE_WINDOW_TRANSFORM_HINT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 755 | *value = static_cast<int>(mTransformHint); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 756 | return NO_ERROR; |
| 757 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: { |
| 758 | status_t err = NO_ERROR; |
| 759 | if (!mConsumerRunningBehind) { |
| 760 | *value = 0; |
| 761 | } else { |
| 762 | err = mGraphicBufferProducer->query(what, value); |
| 763 | if (err == NO_ERROR) { |
| 764 | mConsumerRunningBehind = *value; |
| 765 | } |
| 766 | } |
| 767 | return err; |
| 768 | } |
Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 769 | case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: { |
| 770 | int64_t durationUs = mLastDequeueDuration / 1000; |
| 771 | *value = durationUs > std::numeric_limits<int>::max() ? |
| 772 | std::numeric_limits<int>::max() : |
| 773 | static_cast<int>(durationUs); |
| 774 | return NO_ERROR; |
| 775 | } |
| 776 | case NATIVE_WINDOW_LAST_QUEUE_DURATION: { |
| 777 | int64_t durationUs = mLastQueueDuration / 1000; |
| 778 | *value = durationUs > std::numeric_limits<int>::max() ? |
| 779 | std::numeric_limits<int>::max() : |
| 780 | static_cast<int>(durationUs); |
| 781 | return NO_ERROR; |
| 782 | } |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 783 | case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: { |
| 784 | querySupportedTimestampsLocked(); |
| 785 | *value = mFrameTimestampsSupportsPresent ? 1 : 0; |
| 786 | return NO_ERROR; |
| 787 | } |
| 788 | case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE: { |
| 789 | querySupportedTimestampsLocked(); |
| 790 | *value = mFrameTimestampsSupportsRetire ? 1 : 0; |
| 791 | return NO_ERROR; |
| 792 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 793 | } |
Jamie Gennis | 391bbe2 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 794 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 795 | return mGraphicBufferProducer->query(what, value); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 798 | int Surface::perform(int operation, va_list args) |
| 799 | { |
| 800 | int res = NO_ERROR; |
| 801 | switch (operation) { |
| 802 | case NATIVE_WINDOW_CONNECT: |
| 803 | // deprecated. must return NO_ERROR. |
| 804 | break; |
| 805 | case NATIVE_WINDOW_DISCONNECT: |
| 806 | // deprecated. must return NO_ERROR. |
| 807 | break; |
| 808 | case NATIVE_WINDOW_SET_USAGE: |
| 809 | res = dispatchSetUsage(args); |
| 810 | break; |
| 811 | case NATIVE_WINDOW_SET_CROP: |
| 812 | res = dispatchSetCrop(args); |
| 813 | break; |
| 814 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 815 | res = dispatchSetBufferCount(args); |
| 816 | break; |
| 817 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 818 | res = dispatchSetBuffersGeometry(args); |
| 819 | break; |
| 820 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 821 | res = dispatchSetBuffersTransform(args); |
| 822 | break; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 823 | case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM: |
| 824 | res = dispatchSetBuffersStickyTransform(args); |
| 825 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 826 | case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: |
| 827 | res = dispatchSetBuffersTimestamp(args); |
| 828 | break; |
| 829 | case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: |
| 830 | res = dispatchSetBuffersDimensions(args); |
| 831 | break; |
| 832 | case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS: |
| 833 | res = dispatchSetBuffersUserDimensions(args); |
| 834 | break; |
| 835 | case NATIVE_WINDOW_SET_BUFFERS_FORMAT: |
| 836 | res = dispatchSetBuffersFormat(args); |
| 837 | break; |
| 838 | case NATIVE_WINDOW_LOCK: |
| 839 | res = dispatchLock(args); |
| 840 | break; |
| 841 | case NATIVE_WINDOW_UNLOCK_AND_POST: |
| 842 | res = dispatchUnlockAndPost(args); |
| 843 | break; |
| 844 | case NATIVE_WINDOW_SET_SCALING_MODE: |
| 845 | res = dispatchSetScalingMode(args); |
| 846 | break; |
| 847 | case NATIVE_WINDOW_API_CONNECT: |
| 848 | res = dispatchConnect(args); |
| 849 | break; |
| 850 | case NATIVE_WINDOW_API_DISCONNECT: |
| 851 | res = dispatchDisconnect(args); |
| 852 | break; |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 853 | case NATIVE_WINDOW_SET_SIDEBAND_STREAM: |
| 854 | res = dispatchSetSidebandStream(args); |
| 855 | break; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 856 | case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: |
| 857 | res = dispatchSetBuffersDataSpace(args); |
| 858 | break; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 859 | case NATIVE_WINDOW_SET_SURFACE_DAMAGE: |
| 860 | res = dispatchSetSurfaceDamage(args); |
| 861 | break; |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 862 | case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE: |
| 863 | res = dispatchSetSharedBufferMode(args); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 864 | break; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 865 | case NATIVE_WINDOW_SET_AUTO_REFRESH: |
| 866 | res = dispatchSetAutoRefresh(args); |
| 867 | break; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 868 | case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION: |
| 869 | res = dispatchGetDisplayRefreshCycleDuration(args); |
| 870 | break; |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 871 | case NATIVE_WINDOW_GET_NEXT_FRAME_ID: |
| 872 | res = dispatchGetNextFrameId(args); |
| 873 | break; |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 874 | case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS: |
| 875 | res = dispatchEnableFrameTimestamps(args); |
| 876 | break; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 877 | case NATIVE_WINDOW_GET_COMPOSITOR_TIMING: |
| 878 | res = dispatchGetCompositorTiming(args); |
| 879 | break; |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 880 | case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS: |
| 881 | res = dispatchGetFrameTimestamps(args); |
| 882 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 883 | default: |
| 884 | res = NAME_NOT_FOUND; |
| 885 | break; |
| 886 | } |
| 887 | return res; |
| 888 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 889 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 890 | int Surface::dispatchConnect(va_list args) { |
| 891 | int api = va_arg(args, int); |
| 892 | return connect(api); |
| 893 | } |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 894 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 895 | int Surface::dispatchDisconnect(va_list args) { |
| 896 | int api = va_arg(args, int); |
| 897 | return disconnect(api); |
| 898 | } |
| 899 | |
| 900 | int Surface::dispatchSetUsage(va_list args) { |
| 901 | int usage = va_arg(args, int); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 902 | return setUsage(static_cast<uint32_t>(usage)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | int Surface::dispatchSetCrop(va_list args) { |
| 906 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 907 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 908 | } |
| 909 | |
| 910 | int Surface::dispatchSetBufferCount(va_list args) { |
| 911 | size_t bufferCount = va_arg(args, size_t); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 912 | return setBufferCount(static_cast<int32_t>(bufferCount)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | int Surface::dispatchSetBuffersGeometry(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 916 | uint32_t width = va_arg(args, uint32_t); |
| 917 | uint32_t height = va_arg(args, uint32_t); |
| 918 | PixelFormat format = va_arg(args, PixelFormat); |
| 919 | int err = setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 920 | if (err != 0) { |
| 921 | return err; |
| 922 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 923 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | int Surface::dispatchSetBuffersDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 927 | uint32_t width = va_arg(args, uint32_t); |
| 928 | uint32_t height = va_arg(args, uint32_t); |
| 929 | return setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | int Surface::dispatchSetBuffersUserDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 933 | uint32_t width = va_arg(args, uint32_t); |
| 934 | uint32_t height = va_arg(args, uint32_t); |
| 935 | return setBuffersUserDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | int Surface::dispatchSetBuffersFormat(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 939 | PixelFormat format = va_arg(args, PixelFormat); |
| 940 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | int Surface::dispatchSetScalingMode(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 944 | int mode = va_arg(args, int); |
| 945 | return setScalingMode(mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | int Surface::dispatchSetBuffersTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 949 | uint32_t transform = va_arg(args, uint32_t); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 950 | return setBuffersTransform(transform); |
| 951 | } |
| 952 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 953 | int Surface::dispatchSetBuffersStickyTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 954 | uint32_t transform = va_arg(args, uint32_t); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 955 | return setBuffersStickyTransform(transform); |
| 956 | } |
| 957 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 958 | int Surface::dispatchSetBuffersTimestamp(va_list args) { |
| 959 | int64_t timestamp = va_arg(args, int64_t); |
| 960 | return setBuffersTimestamp(timestamp); |
| 961 | } |
| 962 | |
| 963 | int Surface::dispatchLock(va_list args) { |
| 964 | ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*); |
| 965 | ARect* inOutDirtyBounds = va_arg(args, ARect*); |
| 966 | return lock(outBuffer, inOutDirtyBounds); |
| 967 | } |
| 968 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 969 | int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 970 | return unlockAndPost(); |
| 971 | } |
| 972 | |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 973 | int Surface::dispatchSetSidebandStream(va_list args) { |
| 974 | native_handle_t* sH = va_arg(args, native_handle_t*); |
| 975 | sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false); |
| 976 | setSidebandStream(sidebandHandle); |
| 977 | return OK; |
| 978 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 979 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 980 | int Surface::dispatchSetBuffersDataSpace(va_list args) { |
| 981 | android_dataspace dataspace = |
| 982 | static_cast<android_dataspace>(va_arg(args, int)); |
| 983 | return setBuffersDataSpace(dataspace); |
| 984 | } |
| 985 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 986 | int Surface::dispatchSetSurfaceDamage(va_list args) { |
| 987 | android_native_rect_t* rects = va_arg(args, android_native_rect_t*); |
| 988 | size_t numRects = va_arg(args, size_t); |
| 989 | setSurfaceDamage(rects, numRects); |
| 990 | return NO_ERROR; |
| 991 | } |
| 992 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 993 | int Surface::dispatchSetSharedBufferMode(va_list args) { |
| 994 | bool sharedBufferMode = va_arg(args, int); |
| 995 | return setSharedBufferMode(sharedBufferMode); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | int Surface::dispatchSetAutoRefresh(va_list args) { |
| 999 | bool autoRefresh = va_arg(args, int); |
| 1000 | return setAutoRefresh(autoRefresh); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1003 | int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) { |
| 1004 | nsecs_t* outRefreshDuration = va_arg(args, int64_t*); |
| 1005 | return getDisplayRefreshCycleDuration(outRefreshDuration); |
| 1006 | } |
| 1007 | |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1008 | int Surface::dispatchGetNextFrameId(va_list args) { |
| 1009 | uint64_t* nextFrameId = va_arg(args, uint64_t*); |
| 1010 | *nextFrameId = getNextFrameNumber(); |
| 1011 | return NO_ERROR; |
| 1012 | } |
| 1013 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1014 | int Surface::dispatchEnableFrameTimestamps(va_list args) { |
| 1015 | bool enable = va_arg(args, int); |
| 1016 | enableFrameTimestamps(enable); |
| 1017 | return NO_ERROR; |
| 1018 | } |
| 1019 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1020 | int Surface::dispatchGetCompositorTiming(va_list args) { |
| 1021 | nsecs_t* compositeDeadline = va_arg(args, int64_t*); |
| 1022 | nsecs_t* compositeInterval = va_arg(args, int64_t*); |
| 1023 | nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*); |
| 1024 | return getCompositorTiming(compositeDeadline, compositeInterval, |
| 1025 | compositeToPresentLatency); |
| 1026 | } |
| 1027 | |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1028 | int Surface::dispatchGetFrameTimestamps(va_list args) { |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1029 | uint64_t frameId = va_arg(args, uint64_t); |
Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 1030 | nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1031 | nsecs_t* outAcquireTime = va_arg(args, int64_t*); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1032 | nsecs_t* outLatchTime = va_arg(args, int64_t*); |
| 1033 | nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*); |
| 1034 | nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1035 | nsecs_t* outGlCompositionDoneTime = va_arg(args, int64_t*); |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 1036 | nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1037 | nsecs_t* outDisplayRetireTime = va_arg(args, int64_t*); |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1038 | nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1039 | nsecs_t* outReleaseTime = va_arg(args, int64_t*); |
Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1040 | return getFrameTimestamps(frameId, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1041 | outRequestedPresentTime, outAcquireTime, outLatchTime, |
| 1042 | outFirstRefreshStartTime, outLastRefreshStartTime, |
Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 1043 | outGlCompositionDoneTime, outDisplayPresentTime, |
Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1044 | outDisplayRetireTime, outDequeueReadyTime, outReleaseTime); |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1047 | int Surface::connect(int api) { |
Dan Stoza | 966b98b | 2015-03-02 22:12:37 -0800 | [diff] [blame] | 1048 | static sp<IProducerListener> listener = new DummyProducerListener(); |
| 1049 | return connect(api, listener); |
| 1050 | } |
| 1051 | |
| 1052 | int Surface::connect(int api, const sp<IProducerListener>& listener) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1053 | ATRACE_CALL(); |
| 1054 | ALOGV("Surface::connect"); |
| 1055 | Mutex::Autolock lock(mMutex); |
| 1056 | IGraphicBufferProducer::QueueBufferOutput output; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1057 | int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1058 | if (err == NO_ERROR) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1059 | mDefaultWidth = output.width; |
| 1060 | mDefaultHeight = output.height; |
| 1061 | mNextFrameNumber = output.nextFrameNumber; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1062 | |
| 1063 | // Disable transform hint if sticky transform is set. |
| 1064 | if (mStickyTransform == 0) { |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1065 | mTransformHint = output.transformHint; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1068 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1069 | } |
| 1070 | if (!err && api == NATIVE_WINDOW_API_CPU) { |
| 1071 | mConnectedToCpu = true; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1072 | // Clear the dirty region in case we're switching from a non-CPU API |
| 1073 | mDirtyRegion.clear(); |
| 1074 | } else if (!err) { |
| 1075 | // Initialize the dirty region for tracking surface damage |
| 1076 | mDirtyRegion = Region::INVALID_REGION; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1077 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1078 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1079 | return err; |
| 1080 | } |
| 1081 | |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 1082 | |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1083 | int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1084 | ATRACE_CALL(); |
| 1085 | ALOGV("Surface::disconnect"); |
| 1086 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1087 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1088 | mSharedBufferHasBeenQueued = false; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1089 | freeAllBuffers(); |
Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1090 | int err = mGraphicBufferProducer->disconnect(api, mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1091 | if (!err) { |
| 1092 | mReqFormat = 0; |
| 1093 | mReqWidth = 0; |
| 1094 | mReqHeight = 0; |
| 1095 | mReqUsage = 0; |
| 1096 | mCrop.clear(); |
| 1097 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 1098 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1099 | mStickyTransform = 0; |
| 1100 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1101 | if (api == NATIVE_WINDOW_API_CPU) { |
| 1102 | mConnectedToCpu = false; |
| 1103 | } |
| 1104 | } |
| 1105 | return err; |
| 1106 | } |
| 1107 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1108 | int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1109 | sp<Fence>* outFence) { |
| 1110 | ATRACE_CALL(); |
| 1111 | ALOGV("Surface::detachNextBuffer"); |
| 1112 | |
| 1113 | if (outBuffer == NULL || outFence == NULL) { |
| 1114 | return BAD_VALUE; |
| 1115 | } |
| 1116 | |
| 1117 | Mutex::Autolock lock(mMutex); |
| 1118 | |
| 1119 | sp<GraphicBuffer> buffer(NULL); |
| 1120 | sp<Fence> fence(NULL); |
| 1121 | status_t result = mGraphicBufferProducer->detachNextBuffer( |
| 1122 | &buffer, &fence); |
| 1123 | if (result != NO_ERROR) { |
| 1124 | return result; |
| 1125 | } |
| 1126 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1127 | *outBuffer = buffer; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1128 | if (fence != NULL && fence->isValid()) { |
| 1129 | *outFence = fence; |
| 1130 | } else { |
| 1131 | *outFence = Fence::NO_FENCE; |
| 1132 | } |
| 1133 | |
Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1134 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 1135 | if (mSlots[i].buffer != NULL && |
| 1136 | mSlots[i].buffer->handle == buffer->handle) { |
| 1137 | mSlots[i].buffer = NULL; |
| 1138 | } |
| 1139 | } |
| 1140 | |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1141 | return NO_ERROR; |
| 1142 | } |
| 1143 | |
| 1144 | int Surface::attachBuffer(ANativeWindowBuffer* buffer) |
| 1145 | { |
| 1146 | ATRACE_CALL(); |
| 1147 | ALOGV("Surface::attachBuffer"); |
| 1148 | |
| 1149 | Mutex::Autolock lock(mMutex); |
| 1150 | |
| 1151 | sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer)); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1152 | uint32_t priorGeneration = graphicBuffer->mGenerationNumber; |
| 1153 | graphicBuffer->mGenerationNumber = mGenerationNumber; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1154 | int32_t attachedSlot = -1; |
| 1155 | status_t result = mGraphicBufferProducer->attachBuffer( |
| 1156 | &attachedSlot, graphicBuffer); |
| 1157 | if (result != NO_ERROR) { |
| 1158 | ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result); |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1159 | graphicBuffer->mGenerationNumber = priorGeneration; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1160 | return result; |
| 1161 | } |
| 1162 | mSlots[attachedSlot].buffer = graphicBuffer; |
| 1163 | |
| 1164 | return NO_ERROR; |
| 1165 | } |
| 1166 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1167 | int Surface::setUsage(uint32_t reqUsage) |
| 1168 | { |
| 1169 | ALOGV("Surface::setUsage"); |
| 1170 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1171 | if (reqUsage != mReqUsage) { |
| 1172 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1173 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1174 | mReqUsage = reqUsage; |
| 1175 | return OK; |
| 1176 | } |
| 1177 | |
| 1178 | int Surface::setCrop(Rect const* rect) |
| 1179 | { |
| 1180 | ATRACE_CALL(); |
| 1181 | |
Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 1182 | Rect realRect(Rect::EMPTY_RECT); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1183 | if (rect == NULL || rect->isEmpty()) { |
| 1184 | realRect.clear(); |
| 1185 | } else { |
| 1186 | realRect = *rect; |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1189 | ALOGV("Surface::setCrop rect=[%d %d %d %d]", |
| 1190 | realRect.left, realRect.top, realRect.right, realRect.bottom); |
| 1191 | |
| 1192 | Mutex::Autolock lock(mMutex); |
| 1193 | mCrop = realRect; |
| 1194 | return NO_ERROR; |
| 1195 | } |
| 1196 | |
| 1197 | int Surface::setBufferCount(int bufferCount) |
| 1198 | { |
| 1199 | ATRACE_CALL(); |
| 1200 | ALOGV("Surface::setBufferCount"); |
| 1201 | Mutex::Autolock lock(mMutex); |
| 1202 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 1203 | status_t err = NO_ERROR; |
| 1204 | if (bufferCount == 0) { |
| 1205 | err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1); |
| 1206 | } else { |
| 1207 | int minUndequeuedBuffers = 0; |
| 1208 | err = mGraphicBufferProducer->query( |
| 1209 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers); |
| 1210 | if (err == NO_ERROR) { |
| 1211 | err = mGraphicBufferProducer->setMaxDequeuedBufferCount( |
| 1212 | bufferCount - minUndequeuedBuffers); |
| 1213 | } |
| 1214 | } |
Mathias Agopian | 9014726 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 1215 | |
Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 1216 | ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s", |
| 1217 | bufferCount, strerror(-err)); |
| 1218 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1219 | return err; |
| 1220 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1221 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1222 | int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) { |
| 1223 | ATRACE_CALL(); |
| 1224 | ALOGV("Surface::setMaxDequeuedBufferCount"); |
| 1225 | Mutex::Autolock lock(mMutex); |
| 1226 | |
| 1227 | status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount( |
| 1228 | maxDequeuedBuffers); |
| 1229 | ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) " |
| 1230 | "returned %s", maxDequeuedBuffers, strerror(-err)); |
| 1231 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1232 | return err; |
| 1233 | } |
| 1234 | |
| 1235 | int Surface::setAsyncMode(bool async) { |
| 1236 | ATRACE_CALL(); |
| 1237 | ALOGV("Surface::setAsyncMode"); |
| 1238 | Mutex::Autolock lock(mMutex); |
| 1239 | |
| 1240 | status_t err = mGraphicBufferProducer->setAsyncMode(async); |
| 1241 | ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s", |
| 1242 | async, strerror(-err)); |
| 1243 | |
Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1244 | return err; |
| 1245 | } |
| 1246 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1247 | int Surface::setSharedBufferMode(bool sharedBufferMode) { |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1248 | ATRACE_CALL(); |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1249 | ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1250 | Mutex::Autolock lock(mMutex); |
| 1251 | |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1252 | status_t err = mGraphicBufferProducer->setSharedBufferMode( |
| 1253 | sharedBufferMode); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1254 | if (err == NO_ERROR) { |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1255 | mSharedBufferMode = sharedBufferMode; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1256 | } |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1257 | ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned" |
| 1258 | "%s", sharedBufferMode, strerror(-err)); |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1259 | |
| 1260 | return err; |
| 1261 | } |
| 1262 | |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1263 | int Surface::setAutoRefresh(bool autoRefresh) { |
| 1264 | ATRACE_CALL(); |
| 1265 | ALOGV("Surface::setAutoRefresh (%d)", autoRefresh); |
| 1266 | Mutex::Autolock lock(mMutex); |
| 1267 | |
| 1268 | status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh); |
| 1269 | if (err == NO_ERROR) { |
| 1270 | mAutoRefresh = autoRefresh; |
| 1271 | } |
| 1272 | ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s", |
| 1273 | autoRefresh, strerror(-err)); |
| 1274 | return err; |
| 1275 | } |
| 1276 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1277 | int Surface::setBuffersDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1278 | { |
| 1279 | ATRACE_CALL(); |
| 1280 | ALOGV("Surface::setBuffersDimensions"); |
| 1281 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1282 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1283 | return BAD_VALUE; |
| 1284 | |
| 1285 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1286 | if (width != mReqWidth || height != mReqHeight) { |
| 1287 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1288 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1289 | mReqWidth = width; |
| 1290 | mReqHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1291 | return NO_ERROR; |
| 1292 | } |
| 1293 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1294 | int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1295 | { |
| 1296 | ATRACE_CALL(); |
| 1297 | ALOGV("Surface::setBuffersUserDimensions"); |
| 1298 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1299 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1300 | return BAD_VALUE; |
| 1301 | |
| 1302 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1303 | if (width != mUserWidth || height != mUserHeight) { |
| 1304 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1305 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1306 | mUserWidth = width; |
| 1307 | mUserHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1308 | return NO_ERROR; |
| 1309 | } |
| 1310 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1311 | int Surface::setBuffersFormat(PixelFormat format) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1312 | { |
| 1313 | ALOGV("Surface::setBuffersFormat"); |
| 1314 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1315 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1316 | if (format != mReqFormat) { |
| 1317 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; |
| 1318 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1319 | mReqFormat = format; |
| 1320 | return NO_ERROR; |
| 1321 | } |
| 1322 | |
| 1323 | int Surface::setScalingMode(int mode) |
| 1324 | { |
| 1325 | ATRACE_CALL(); |
| 1326 | ALOGV("Surface::setScalingMode(%d)", mode); |
| 1327 | |
| 1328 | switch (mode) { |
| 1329 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 1330 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 1331 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
Robert Carr | c2e7788 | 2015-12-16 18:14:03 -0800 | [diff] [blame] | 1332 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1333 | break; |
| 1334 | default: |
| 1335 | ALOGE("unknown scaling mode: %d", mode); |
| 1336 | return BAD_VALUE; |
| 1337 | } |
| 1338 | |
| 1339 | Mutex::Autolock lock(mMutex); |
| 1340 | mScalingMode = mode; |
| 1341 | return NO_ERROR; |
| 1342 | } |
| 1343 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1344 | int Surface::setBuffersTransform(uint32_t transform) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1345 | { |
| 1346 | ATRACE_CALL(); |
| 1347 | ALOGV("Surface::setBuffersTransform"); |
| 1348 | Mutex::Autolock lock(mMutex); |
| 1349 | mTransform = transform; |
| 1350 | return NO_ERROR; |
| 1351 | } |
| 1352 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1353 | int Surface::setBuffersStickyTransform(uint32_t transform) |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1354 | { |
| 1355 | ATRACE_CALL(); |
| 1356 | ALOGV("Surface::setBuffersStickyTransform"); |
| 1357 | Mutex::Autolock lock(mMutex); |
| 1358 | mStickyTransform = transform; |
| 1359 | return NO_ERROR; |
| 1360 | } |
| 1361 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1362 | int Surface::setBuffersTimestamp(int64_t timestamp) |
| 1363 | { |
| 1364 | ALOGV("Surface::setBuffersTimestamp"); |
| 1365 | Mutex::Autolock lock(mMutex); |
| 1366 | mTimestamp = timestamp; |
| 1367 | return NO_ERROR; |
| 1368 | } |
| 1369 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1370 | int Surface::setBuffersDataSpace(android_dataspace dataSpace) |
| 1371 | { |
| 1372 | ALOGV("Surface::setBuffersDataSpace"); |
| 1373 | Mutex::Autolock lock(mMutex); |
| 1374 | mDataSpace = dataSpace; |
| 1375 | return NO_ERROR; |
| 1376 | } |
| 1377 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1378 | void Surface::freeAllBuffers() { |
| 1379 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 1380 | mSlots[i].buffer = 0; |
| 1381 | } |
| 1382 | } |
| 1383 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1384 | void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) { |
| 1385 | ATRACE_CALL(); |
| 1386 | ALOGV("Surface::setSurfaceDamage"); |
| 1387 | Mutex::Autolock lock(mMutex); |
| 1388 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 1389 | if (mConnectedToCpu || numRects == 0) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1390 | mDirtyRegion = Region::INVALID_REGION; |
| 1391 | return; |
| 1392 | } |
| 1393 | |
| 1394 | mDirtyRegion.clear(); |
| 1395 | for (size_t r = 0; r < numRects; ++r) { |
| 1396 | // We intentionally flip top and bottom here, since because they're |
| 1397 | // specified with a bottom-left origin, top > bottom, which fails |
| 1398 | // validation in the Region class. We will fix this up when we flip to a |
| 1399 | // top-left origin in queueBuffer. |
| 1400 | Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top); |
| 1401 | mDirtyRegion.orSelf(rect); |
| 1402 | } |
| 1403 | } |
| 1404 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1405 | // ---------------------------------------------------------------------- |
| 1406 | // the lock/unlock APIs must be used from the same thread |
| 1407 | |
| 1408 | static status_t copyBlt( |
| 1409 | const sp<GraphicBuffer>& dst, |
| 1410 | const sp<GraphicBuffer>& src, |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1411 | const Region& reg, |
| 1412 | int *dstFenceFd) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1413 | { |
| 1414 | // src and dst with, height and format must be identical. no verification |
| 1415 | // is done here. |
| 1416 | status_t err; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1417 | uint8_t* src_bits = NULL; |
| 1418 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), |
| 1419 | reinterpret_cast<void**>(&src_bits)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1420 | ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
| 1421 | |
| 1422 | uint8_t* dst_bits = NULL; |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1423 | err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), |
| 1424 | reinterpret_cast<void**>(&dst_bits), *dstFenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1425 | ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1426 | *dstFenceFd = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1427 | |
| 1428 | Region::const_iterator head(reg.begin()); |
| 1429 | Region::const_iterator tail(reg.end()); |
| 1430 | if (head != tail && src_bits && dst_bits) { |
| 1431 | const size_t bpp = bytesPerPixel(src->format); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1432 | const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp; |
| 1433 | const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1434 | |
| 1435 | while (head != tail) { |
| 1436 | const Rect& r(*head++); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1437 | int32_t h = r.height(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1438 | if (h <= 0) continue; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1439 | size_t size = static_cast<uint32_t>(r.width()) * bpp; |
| 1440 | uint8_t const * s = src_bits + |
| 1441 | static_cast<uint32_t>(r.left + src->stride * r.top) * bpp; |
| 1442 | uint8_t * d = dst_bits + |
| 1443 | static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1444 | if (dbpr==sbpr && size==sbpr) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1445 | size *= static_cast<size_t>(h); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1446 | h = 1; |
| 1447 | } |
| 1448 | do { |
| 1449 | memcpy(d, s, size); |
| 1450 | d += dbpr; |
| 1451 | s += sbpr; |
| 1452 | } while (--h > 0); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | if (src_bits) |
| 1457 | src->unlock(); |
| 1458 | |
| 1459 | if (dst_bits) |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1460 | dst->unlockAsync(dstFenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1461 | |
| 1462 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1465 | // ---------------------------------------------------------------------------- |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1466 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1467 | status_t Surface::lock( |
| 1468 | ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) |
| 1469 | { |
| 1470 | if (mLockedBuffer != 0) { |
| 1471 | ALOGE("Surface::lock failed, already locked"); |
| 1472 | return INVALID_OPERATION; |
| 1473 | } |
| 1474 | |
| 1475 | if (!mConnectedToCpu) { |
| 1476 | int err = Surface::connect(NATIVE_WINDOW_API_CPU); |
| 1477 | if (err) { |
| 1478 | return err; |
| 1479 | } |
| 1480 | // we're intending to do software rendering from this point |
| 1481 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 1482 | } |
| 1483 | |
| 1484 | ANativeWindowBuffer* out; |
| 1485 | int fenceFd = -1; |
| 1486 | status_t err = dequeueBuffer(&out, &fenceFd); |
| 1487 | ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); |
| 1488 | if (err == NO_ERROR) { |
| 1489 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1490 | const Rect bounds(backBuffer->width, backBuffer->height); |
| 1491 | |
| 1492 | Region newDirtyRegion; |
| 1493 | if (inOutDirtyBounds) { |
| 1494 | newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds)); |
| 1495 | newDirtyRegion.andSelf(bounds); |
| 1496 | } else { |
| 1497 | newDirtyRegion.set(bounds); |
| 1498 | } |
| 1499 | |
| 1500 | // figure out if we can copy the frontbuffer back |
| 1501 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); |
| 1502 | const bool canCopyBack = (frontBuffer != 0 && |
| 1503 | backBuffer->width == frontBuffer->width && |
| 1504 | backBuffer->height == frontBuffer->height && |
| 1505 | backBuffer->format == frontBuffer->format); |
| 1506 | |
| 1507 | if (canCopyBack) { |
| 1508 | // copy the area that is invalid and not repainted this round |
| 1509 | const Region copyback(mDirtyRegion.subtract(newDirtyRegion)); |
Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 1510 | if (!copyback.isEmpty()) { |
Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1511 | copyBlt(backBuffer, frontBuffer, copyback, &fenceFd); |
Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 1512 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1513 | } else { |
| 1514 | // if we can't copy-back anything, modify the user's dirty |
| 1515 | // region to make sure they redraw the whole buffer |
| 1516 | newDirtyRegion.set(bounds); |
| 1517 | mDirtyRegion.clear(); |
| 1518 | Mutex::Autolock lock(mMutex); |
| 1519 | for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) { |
| 1520 | mSlots[i].dirtyRegion.clear(); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | |
| 1525 | { // scope for the lock |
| 1526 | Mutex::Autolock lock(mMutex); |
| 1527 | int backBufferSlot(getSlotFromBufferLocked(backBuffer.get())); |
| 1528 | if (backBufferSlot >= 0) { |
| 1529 | Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion); |
| 1530 | mDirtyRegion.subtract(dirtyRegion); |
| 1531 | dirtyRegion = newDirtyRegion; |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | mDirtyRegion.orSelf(newDirtyRegion); |
| 1536 | if (inOutDirtyBounds) { |
| 1537 | *inOutDirtyBounds = newDirtyRegion.getBounds(); |
| 1538 | } |
| 1539 | |
| 1540 | void* vaddr; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1541 | status_t res = backBuffer->lockAsync( |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1542 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1543 | newDirtyRegion.bounds(), &vaddr, fenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1544 | |
| 1545 | ALOGW_IF(res, "failed locking buffer (handle = %p)", |
| 1546 | backBuffer->handle); |
| 1547 | |
| 1548 | if (res != 0) { |
| 1549 | err = INVALID_OPERATION; |
| 1550 | } else { |
| 1551 | mLockedBuffer = backBuffer; |
| 1552 | outBuffer->width = backBuffer->width; |
| 1553 | outBuffer->height = backBuffer->height; |
| 1554 | outBuffer->stride = backBuffer->stride; |
| 1555 | outBuffer->format = backBuffer->format; |
| 1556 | outBuffer->bits = vaddr; |
| 1557 | } |
| 1558 | } |
| 1559 | return err; |
| 1560 | } |
| 1561 | |
| 1562 | status_t Surface::unlockAndPost() |
| 1563 | { |
| 1564 | if (mLockedBuffer == 0) { |
| 1565 | ALOGE("Surface::unlockAndPost failed, no locked buffer"); |
| 1566 | return INVALID_OPERATION; |
| 1567 | } |
| 1568 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1569 | int fd = -1; |
| 1570 | status_t err = mLockedBuffer->unlockAsync(&fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1571 | ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); |
| 1572 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1573 | err = queueBuffer(mLockedBuffer.get(), fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1574 | ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", |
| 1575 | mLockedBuffer->handle, strerror(-err)); |
| 1576 | |
| 1577 | mPostedBuffer = mLockedBuffer; |
| 1578 | mLockedBuffer = 0; |
| 1579 | return err; |
| 1580 | } |
| 1581 | |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1582 | bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) { |
| 1583 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 1584 | if (mNextFrameNumber > lastFrame) { |
Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1585 | return true; |
| 1586 | } |
| 1587 | return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK; |
| 1588 | } |
| 1589 | |
Pablo Ceballos | 8e3e92b | 2016-06-27 17:56:53 -0700 | [diff] [blame] | 1590 | status_t Surface::getUniqueId(uint64_t* outId) const { |
| 1591 | Mutex::Autolock lock(mMutex); |
| 1592 | return mGraphicBufferProducer->getUniqueId(outId); |
| 1593 | } |
| 1594 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | }; // namespace android |