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