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