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