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