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