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) |
Mathias Agopian | 35ffa6a | 2013-03-12 18:45:09 -0700 | [diff] [blame] | 45 | : mGraphicBufferProducer(bufferProducer) |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 46 | { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 47 | // Initialize the ANativeWindow function pointers. |
| 48 | ANativeWindow::setSwapInterval = hook_setSwapInterval; |
| 49 | ANativeWindow::dequeueBuffer = hook_dequeueBuffer; |
| 50 | ANativeWindow::cancelBuffer = hook_cancelBuffer; |
| 51 | ANativeWindow::queueBuffer = hook_queueBuffer; |
| 52 | ANativeWindow::query = hook_query; |
| 53 | ANativeWindow::perform = hook_perform; |
| 54 | |
| 55 | ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED; |
| 56 | ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED; |
| 57 | ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED; |
| 58 | ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED; |
| 59 | |
| 60 | const_cast<int&>(ANativeWindow::minSwapInterval) = 0; |
| 61 | const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; |
| 62 | |
| 63 | mReqWidth = 0; |
| 64 | mReqHeight = 0; |
| 65 | mReqFormat = 0; |
| 66 | mReqUsage = 0; |
| 67 | mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 68 | mDataSpace = HAL_DATASPACE_UNKNOWN; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 69 | mCrop.clear(); |
| 70 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 71 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 72 | mStickyTransform = 0; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 73 | mDefaultWidth = 0; |
| 74 | mDefaultHeight = 0; |
| 75 | mUserWidth = 0; |
| 76 | mUserHeight = 0; |
| 77 | mTransformHint = 0; |
| 78 | mConsumerRunningBehind = false; |
| 79 | mConnectedToCpu = false; |
Eino-Ville Talvala | 7895e90 | 2013-08-21 11:53:37 -0700 | [diff] [blame] | 80 | mProducerControlledByApp = controlledByApp; |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 81 | mSwapIntervalZero = false; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Mathias Agopian | 35ffa6a | 2013-03-12 18:45:09 -0700 | [diff] [blame] | 84 | Surface::~Surface() { |
| 85 | if (mConnectedToCpu) { |
| 86 | Surface::disconnect(NATIVE_WINDOW_API_CPU); |
| 87 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const { |
| 91 | return mGraphicBufferProducer; |
| 92 | } |
| 93 | |
Wonsik Kim | 0ee14ca | 2014-03-17 17:46:53 +0900 | [diff] [blame] | 94 | void Surface::setSidebandStream(const sp<NativeHandle>& stream) { |
| 95 | mGraphicBufferProducer->setSidebandStream(stream); |
| 96 | } |
| 97 | |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 98 | void Surface::allocateBuffers() { |
| 99 | uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 100 | uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
Andreas Gampe | 7398a5a | 2014-12-08 20:42:40 -0800 | [diff] [blame] | 101 | mGraphicBufferProducer->allocateBuffers(mSwapIntervalZero, reqWidth, |
| 102 | reqHeight, mReqFormat, mReqUsage); |
Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 105 | int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) { |
| 106 | Surface* c = getSelf(window); |
| 107 | return c->setSwapInterval(interval); |
| 108 | } |
| 109 | |
| 110 | int Surface::hook_dequeueBuffer(ANativeWindow* window, |
| 111 | ANativeWindowBuffer** buffer, int* fenceFd) { |
| 112 | Surface* c = getSelf(window); |
| 113 | return c->dequeueBuffer(buffer, fenceFd); |
| 114 | } |
| 115 | |
| 116 | int Surface::hook_cancelBuffer(ANativeWindow* window, |
| 117 | ANativeWindowBuffer* buffer, int fenceFd) { |
| 118 | Surface* c = getSelf(window); |
| 119 | return c->cancelBuffer(buffer, fenceFd); |
| 120 | } |
| 121 | |
| 122 | int Surface::hook_queueBuffer(ANativeWindow* window, |
| 123 | ANativeWindowBuffer* buffer, int fenceFd) { |
| 124 | Surface* c = getSelf(window); |
| 125 | return c->queueBuffer(buffer, fenceFd); |
| 126 | } |
| 127 | |
| 128 | int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, |
| 129 | ANativeWindowBuffer** buffer) { |
| 130 | Surface* c = getSelf(window); |
| 131 | ANativeWindowBuffer* buf; |
| 132 | int fenceFd = -1; |
| 133 | int result = c->dequeueBuffer(&buf, &fenceFd); |
| 134 | sp<Fence> fence(new Fence(fenceFd)); |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 135 | int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED"); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 136 | if (waitResult != OK) { |
| 137 | ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d", |
| 138 | waitResult); |
| 139 | c->cancelBuffer(buf, -1); |
| 140 | return waitResult; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 141 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 142 | *buffer = buf; |
| 143 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 146 | int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window, |
| 147 | ANativeWindowBuffer* buffer) { |
| 148 | Surface* c = getSelf(window); |
| 149 | return c->cancelBuffer(buffer, -1); |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 152 | int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window, |
| 153 | ANativeWindowBuffer* buffer) { |
| 154 | Surface* c = getSelf(window); |
| 155 | return c->lockBuffer_DEPRECATED(buffer); |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 158 | int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window, |
| 159 | ANativeWindowBuffer* buffer) { |
| 160 | Surface* c = getSelf(window); |
| 161 | return c->queueBuffer(buffer, -1); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 162 | } |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 163 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 164 | int Surface::hook_query(const ANativeWindow* window, |
| 165 | int what, int* value) { |
| 166 | const Surface* c = getSelf(window); |
| 167 | return c->query(what, value); |
| 168 | } |
| 169 | |
| 170 | int Surface::hook_perform(ANativeWindow* window, int operation, ...) { |
| 171 | va_list args; |
| 172 | va_start(args, operation); |
| 173 | Surface* c = getSelf(window); |
| 174 | return c->perform(operation, args); |
| 175 | } |
| 176 | |
| 177 | int Surface::setSwapInterval(int interval) { |
| 178 | ATRACE_CALL(); |
| 179 | // EGL specification states: |
| 180 | // interval is silently clamped to minimum and maximum implementation |
| 181 | // dependent values before being stored. |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 182 | |
| 183 | if (interval < minSwapInterval) |
| 184 | interval = minSwapInterval; |
| 185 | |
| 186 | if (interval > maxSwapInterval) |
| 187 | interval = maxSwapInterval; |
| 188 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 189 | mSwapIntervalZero = (interval == 0); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 190 | |
Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 191 | return NO_ERROR; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 194 | int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 195 | ATRACE_CALL(); |
| 196 | ALOGV("Surface::dequeueBuffer"); |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 197 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 198 | uint32_t reqWidth; |
| 199 | uint32_t reqHeight; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 200 | bool swapIntervalZero; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 201 | PixelFormat reqFormat; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 202 | uint32_t reqUsage; |
| 203 | |
| 204 | { |
| 205 | Mutex::Autolock lock(mMutex); |
| 206 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 207 | reqWidth = mReqWidth ? mReqWidth : mUserWidth; |
| 208 | reqHeight = mReqHeight ? mReqHeight : mUserHeight; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 209 | |
| 210 | swapIntervalZero = mSwapIntervalZero; |
| 211 | reqFormat = mReqFormat; |
| 212 | reqUsage = mReqUsage; |
| 213 | } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer |
| 214 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 215 | int buf = -1; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 216 | sp<Fence> fence; |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 217 | status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero, |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 218 | reqWidth, reqHeight, reqFormat, reqUsage); |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 219 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 220 | if (result < 0) { |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 221 | ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)" |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 222 | "failed: %d", swapIntervalZero, reqWidth, reqHeight, reqFormat, |
| 223 | reqUsage, result); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 224 | return result; |
Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 225 | } |
Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 226 | |
| 227 | Mutex::Autolock lock(mMutex); |
| 228 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 229 | sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 230 | |
| 231 | // this should never happen |
| 232 | ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf); |
| 233 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 234 | if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { |
| 235 | freeAllBuffers(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 236 | } |
Ted Bonkenburg | bd050ab | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 237 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 238 | if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) { |
| 239 | result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); |
| 240 | if (result != NO_ERROR) { |
Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 241 | ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); |
Jesse Hall | 9f5a1b6 | 2014-10-02 11:09:03 -0700 | [diff] [blame] | 242 | mGraphicBufferProducer->cancelBuffer(buf, fence); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 243 | return result; |
| 244 | } |
| 245 | } |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 246 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 247 | if (fence->isValid()) { |
| 248 | *fenceFd = fence->dup(); |
| 249 | if (*fenceFd == -1) { |
| 250 | ALOGE("dequeueBuffer: error duping fence: %d", errno); |
| 251 | // dup() should never fail; something is badly wrong. Soldier on |
| 252 | // and hope for the best; the worst that should happen is some |
| 253 | // visible corruption that lasts until the next frame. |
| 254 | } |
Ted Bonkenburg | e5d6eb8 | 2011-08-09 22:38:41 -0700 | [diff] [blame] | 255 | } else { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 256 | *fenceFd = -1; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 257 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 258 | |
| 259 | *buffer = gbuf.get(); |
| 260 | return OK; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 263 | int Surface::cancelBuffer(android_native_buffer_t* buffer, |
| 264 | int fenceFd) { |
| 265 | ATRACE_CALL(); |
| 266 | ALOGV("Surface::cancelBuffer"); |
| 267 | Mutex::Autolock lock(mMutex); |
| 268 | int i = getSlotFromBufferLocked(buffer); |
| 269 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 270 | if (fenceFd >= 0) { |
| 271 | close(fenceFd); |
| 272 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 273 | return i; |
| 274 | } |
| 275 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 276 | mGraphicBufferProducer->cancelBuffer(i, fence); |
| 277 | return OK; |
| 278 | } |
| 279 | |
| 280 | int Surface::getSlotFromBufferLocked( |
| 281 | android_native_buffer_t* buffer) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 282 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 283 | if (mSlots[i].buffer != NULL && |
| 284 | mSlots[i].buffer->handle == buffer->handle) { |
| 285 | return i; |
Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 288 | ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); |
| 289 | return BAD_VALUE; |
Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 292 | int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 293 | ALOGV("Surface::lockBuffer"); |
| 294 | Mutex::Autolock lock(mMutex); |
| 295 | return OK; |
| 296 | } |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 297 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 298 | int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) { |
| 299 | ATRACE_CALL(); |
| 300 | ALOGV("Surface::queueBuffer"); |
| 301 | Mutex::Autolock lock(mMutex); |
| 302 | int64_t timestamp; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 303 | bool isAutoTimestamp = false; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 304 | if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 305 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 306 | isAutoTimestamp = true; |
Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 307 | ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", |
| 308 | timestamp / 1000000.f); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 309 | } else { |
| 310 | timestamp = mTimestamp; |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 311 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 312 | int i = getSlotFromBufferLocked(buffer); |
| 313 | if (i < 0) { |
Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 314 | if (fenceFd >= 0) { |
| 315 | close(fenceFd); |
| 316 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 317 | return i; |
| 318 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 321 | // Make sure the crop rectangle is entirely inside the buffer. |
| 322 | Rect crop; |
| 323 | mCrop.intersect(Rect(buffer->width, buffer->height), &crop); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 325 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); |
| 326 | IGraphicBufferProducer::QueueBufferOutput output; |
Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 327 | IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 328 | mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform, |
| 329 | mSwapIntervalZero, fence, mStickyTransform); |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 330 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 331 | if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 332 | input.setSurfaceDamage(Region::INVALID_REGION); |
| 333 | } else { |
| 334 | // The surface damage was specified using the OpenGL ES convention of |
| 335 | // the origin being in the bottom-left corner. Here we flip to the |
| 336 | // convention that the rest of the system uses (top-left corner) by |
| 337 | // subtracting all top/bottom coordinates from the buffer height. |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame^] | 338 | int height = buffer->height; |
| 339 | if ((mTransform ^ mStickyTransform) & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 340 | height = buffer->width; |
| 341 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 342 | Region flippedRegion; |
| 343 | for (auto rect : mDirtyRegion) { |
Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame^] | 344 | auto top = height - rect.bottom; |
| 345 | auto bottom = height - rect.top; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 346 | Rect flippedRect{rect.left, top, rect.right, bottom}; |
| 347 | flippedRegion.orSelf(flippedRect); |
| 348 | } |
| 349 | |
| 350 | input.setSurfaceDamage(flippedRegion); |
| 351 | } |
| 352 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 353 | status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); |
| 354 | if (err != OK) { |
| 355 | ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); |
| 356 | } |
| 357 | uint32_t numPendingBuffers = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 358 | uint32_t hint = 0; |
| 359 | output.deflate(&mDefaultWidth, &mDefaultHeight, &hint, |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 360 | &numPendingBuffers); |
tedbo | 1e7fa9e | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 361 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 362 | // Disable transform hint if sticky transform is set. |
| 363 | if (mStickyTransform == 0) { |
| 364 | mTransformHint = hint; |
| 365 | } |
| 366 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 367 | mConsumerRunningBehind = (numPendingBuffers >= 2); |
Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 368 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 369 | if (!mConnectedToCpu) { |
| 370 | // Clear surface damage back to full-buffer |
| 371 | mDirtyRegion = Region::INVALID_REGION; |
| 372 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 373 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 374 | return err; |
| 375 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 376 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 377 | int Surface::query(int what, int* value) const { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 378 | ATRACE_CALL(); |
| 379 | ALOGV("Surface::query"); |
| 380 | { // scope for the lock |
| 381 | Mutex::Autolock lock(mMutex); |
| 382 | switch (what) { |
| 383 | case NATIVE_WINDOW_FORMAT: |
| 384 | if (mReqFormat) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 385 | *value = static_cast<int>(mReqFormat); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 386 | return NO_ERROR; |
| 387 | } |
| 388 | break; |
| 389 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: { |
| 390 | sp<ISurfaceComposer> composer( |
| 391 | ComposerService::getComposerService()); |
| 392 | if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) { |
| 393 | *value = 1; |
| 394 | } else { |
| 395 | *value = 0; |
| 396 | } |
| 397 | return NO_ERROR; |
| 398 | } |
| 399 | case NATIVE_WINDOW_CONCRETE_TYPE: |
| 400 | *value = NATIVE_WINDOW_SURFACE; |
| 401 | return NO_ERROR; |
| 402 | case NATIVE_WINDOW_DEFAULT_WIDTH: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 403 | *value = static_cast<int>( |
| 404 | mUserWidth ? mUserWidth : mDefaultWidth); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 405 | return NO_ERROR; |
| 406 | case NATIVE_WINDOW_DEFAULT_HEIGHT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 407 | *value = static_cast<int>( |
| 408 | mUserHeight ? mUserHeight : mDefaultHeight); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 409 | return NO_ERROR; |
| 410 | case NATIVE_WINDOW_TRANSFORM_HINT: |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 411 | *value = static_cast<int>(mTransformHint); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 412 | return NO_ERROR; |
| 413 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: { |
| 414 | status_t err = NO_ERROR; |
| 415 | if (!mConsumerRunningBehind) { |
| 416 | *value = 0; |
| 417 | } else { |
| 418 | err = mGraphicBufferProducer->query(what, value); |
| 419 | if (err == NO_ERROR) { |
| 420 | mConsumerRunningBehind = *value; |
| 421 | } |
| 422 | } |
| 423 | return err; |
| 424 | } |
| 425 | } |
Jamie Gennis | 391bbe2 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 426 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 427 | return mGraphicBufferProducer->query(what, value); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 430 | int Surface::perform(int operation, va_list args) |
| 431 | { |
| 432 | int res = NO_ERROR; |
| 433 | switch (operation) { |
| 434 | case NATIVE_WINDOW_CONNECT: |
| 435 | // deprecated. must return NO_ERROR. |
| 436 | break; |
| 437 | case NATIVE_WINDOW_DISCONNECT: |
| 438 | // deprecated. must return NO_ERROR. |
| 439 | break; |
| 440 | case NATIVE_WINDOW_SET_USAGE: |
| 441 | res = dispatchSetUsage(args); |
| 442 | break; |
| 443 | case NATIVE_WINDOW_SET_CROP: |
| 444 | res = dispatchSetCrop(args); |
| 445 | break; |
| 446 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 447 | res = dispatchSetBufferCount(args); |
| 448 | break; |
| 449 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 450 | res = dispatchSetBuffersGeometry(args); |
| 451 | break; |
| 452 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 453 | res = dispatchSetBuffersTransform(args); |
| 454 | break; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 455 | case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM: |
| 456 | res = dispatchSetBuffersStickyTransform(args); |
| 457 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 458 | case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: |
| 459 | res = dispatchSetBuffersTimestamp(args); |
| 460 | break; |
| 461 | case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: |
| 462 | res = dispatchSetBuffersDimensions(args); |
| 463 | break; |
| 464 | case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS: |
| 465 | res = dispatchSetBuffersUserDimensions(args); |
| 466 | break; |
| 467 | case NATIVE_WINDOW_SET_BUFFERS_FORMAT: |
| 468 | res = dispatchSetBuffersFormat(args); |
| 469 | break; |
| 470 | case NATIVE_WINDOW_LOCK: |
| 471 | res = dispatchLock(args); |
| 472 | break; |
| 473 | case NATIVE_WINDOW_UNLOCK_AND_POST: |
| 474 | res = dispatchUnlockAndPost(args); |
| 475 | break; |
| 476 | case NATIVE_WINDOW_SET_SCALING_MODE: |
| 477 | res = dispatchSetScalingMode(args); |
| 478 | break; |
| 479 | case NATIVE_WINDOW_API_CONNECT: |
| 480 | res = dispatchConnect(args); |
| 481 | break; |
| 482 | case NATIVE_WINDOW_API_DISCONNECT: |
| 483 | res = dispatchDisconnect(args); |
| 484 | break; |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 485 | case NATIVE_WINDOW_SET_SIDEBAND_STREAM: |
| 486 | res = dispatchSetSidebandStream(args); |
| 487 | break; |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 488 | case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: |
| 489 | res = dispatchSetBuffersDataSpace(args); |
| 490 | break; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 491 | case NATIVE_WINDOW_SET_SURFACE_DAMAGE: |
| 492 | res = dispatchSetSurfaceDamage(args); |
| 493 | break; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 494 | default: |
| 495 | res = NAME_NOT_FOUND; |
| 496 | break; |
| 497 | } |
| 498 | return res; |
| 499 | } |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 500 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 501 | int Surface::dispatchConnect(va_list args) { |
| 502 | int api = va_arg(args, int); |
| 503 | return connect(api); |
| 504 | } |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 505 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 506 | int Surface::dispatchDisconnect(va_list args) { |
| 507 | int api = va_arg(args, int); |
| 508 | return disconnect(api); |
| 509 | } |
| 510 | |
| 511 | int Surface::dispatchSetUsage(va_list args) { |
| 512 | int usage = va_arg(args, int); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 513 | return setUsage(static_cast<uint32_t>(usage)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | int Surface::dispatchSetCrop(va_list args) { |
| 517 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 518 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 519 | } |
| 520 | |
| 521 | int Surface::dispatchSetBufferCount(va_list args) { |
| 522 | size_t bufferCount = va_arg(args, size_t); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 523 | return setBufferCount(static_cast<int32_t>(bufferCount)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | int Surface::dispatchSetBuffersGeometry(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 527 | uint32_t width = va_arg(args, uint32_t); |
| 528 | uint32_t height = va_arg(args, uint32_t); |
| 529 | PixelFormat format = va_arg(args, PixelFormat); |
| 530 | int err = setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 531 | if (err != 0) { |
| 532 | return err; |
| 533 | } |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 534 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | int Surface::dispatchSetBuffersDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 538 | uint32_t width = va_arg(args, uint32_t); |
| 539 | uint32_t height = va_arg(args, uint32_t); |
| 540 | return setBuffersDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | int Surface::dispatchSetBuffersUserDimensions(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 544 | uint32_t width = va_arg(args, uint32_t); |
| 545 | uint32_t height = va_arg(args, uint32_t); |
| 546 | return setBuffersUserDimensions(width, height); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | int Surface::dispatchSetBuffersFormat(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 550 | PixelFormat format = va_arg(args, PixelFormat); |
| 551 | return setBuffersFormat(format); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | int Surface::dispatchSetScalingMode(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 555 | int mode = va_arg(args, int); |
| 556 | return setScalingMode(mode); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | int Surface::dispatchSetBuffersTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 560 | uint32_t transform = va_arg(args, uint32_t); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 561 | return setBuffersTransform(transform); |
| 562 | } |
| 563 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 564 | int Surface::dispatchSetBuffersStickyTransform(va_list args) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 565 | uint32_t transform = va_arg(args, uint32_t); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 566 | return setBuffersStickyTransform(transform); |
| 567 | } |
| 568 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 569 | int Surface::dispatchSetBuffersTimestamp(va_list args) { |
| 570 | int64_t timestamp = va_arg(args, int64_t); |
| 571 | return setBuffersTimestamp(timestamp); |
| 572 | } |
| 573 | |
| 574 | int Surface::dispatchLock(va_list args) { |
| 575 | ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*); |
| 576 | ARect* inOutDirtyBounds = va_arg(args, ARect*); |
| 577 | return lock(outBuffer, inOutDirtyBounds); |
| 578 | } |
| 579 | |
Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 580 | int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 581 | return unlockAndPost(); |
| 582 | } |
| 583 | |
Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 584 | int Surface::dispatchSetSidebandStream(va_list args) { |
| 585 | native_handle_t* sH = va_arg(args, native_handle_t*); |
| 586 | sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false); |
| 587 | setSidebandStream(sidebandHandle); |
| 588 | return OK; |
| 589 | } |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 590 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 591 | int Surface::dispatchSetBuffersDataSpace(va_list args) { |
| 592 | android_dataspace dataspace = |
| 593 | static_cast<android_dataspace>(va_arg(args, int)); |
| 594 | return setBuffersDataSpace(dataspace); |
| 595 | } |
| 596 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 597 | int Surface::dispatchSetSurfaceDamage(va_list args) { |
| 598 | android_native_rect_t* rects = va_arg(args, android_native_rect_t*); |
| 599 | size_t numRects = va_arg(args, size_t); |
| 600 | setSurfaceDamage(rects, numRects); |
| 601 | return NO_ERROR; |
| 602 | } |
| 603 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 604 | int Surface::connect(int api) { |
Dan Stoza | 966b98b | 2015-03-02 22:12:37 -0800 | [diff] [blame] | 605 | static sp<IProducerListener> listener = new DummyProducerListener(); |
| 606 | return connect(api, listener); |
| 607 | } |
| 608 | |
| 609 | int Surface::connect(int api, const sp<IProducerListener>& listener) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 610 | ATRACE_CALL(); |
| 611 | ALOGV("Surface::connect"); |
| 612 | Mutex::Autolock lock(mMutex); |
| 613 | IGraphicBufferProducer::QueueBufferOutput output; |
Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 614 | int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 615 | if (err == NO_ERROR) { |
| 616 | uint32_t numPendingBuffers = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 617 | uint32_t hint = 0; |
| 618 | output.deflate(&mDefaultWidth, &mDefaultHeight, &hint, |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 619 | &numPendingBuffers); |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 620 | |
| 621 | // Disable transform hint if sticky transform is set. |
| 622 | if (mStickyTransform == 0) { |
| 623 | mTransformHint = hint; |
| 624 | } |
| 625 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 626 | mConsumerRunningBehind = (numPendingBuffers >= 2); |
| 627 | } |
| 628 | if (!err && api == NATIVE_WINDOW_API_CPU) { |
| 629 | mConnectedToCpu = true; |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 630 | // Clear the dirty region in case we're switching from a non-CPU API |
| 631 | mDirtyRegion.clear(); |
| 632 | } else if (!err) { |
| 633 | // Initialize the dirty region for tracking surface damage |
| 634 | mDirtyRegion = Region::INVALID_REGION; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 635 | } |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 636 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 637 | return err; |
| 638 | } |
| 639 | |
Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 640 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 641 | int Surface::disconnect(int api) { |
| 642 | ATRACE_CALL(); |
| 643 | ALOGV("Surface::disconnect"); |
| 644 | Mutex::Autolock lock(mMutex); |
| 645 | freeAllBuffers(); |
| 646 | int err = mGraphicBufferProducer->disconnect(api); |
| 647 | if (!err) { |
| 648 | mReqFormat = 0; |
| 649 | mReqWidth = 0; |
| 650 | mReqHeight = 0; |
| 651 | mReqUsage = 0; |
| 652 | mCrop.clear(); |
| 653 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 654 | mTransform = 0; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 655 | mStickyTransform = 0; |
| 656 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 657 | if (api == NATIVE_WINDOW_API_CPU) { |
| 658 | mConnectedToCpu = false; |
| 659 | } |
| 660 | } |
| 661 | return err; |
| 662 | } |
| 663 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 664 | int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer, |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 665 | sp<Fence>* outFence) { |
| 666 | ATRACE_CALL(); |
| 667 | ALOGV("Surface::detachNextBuffer"); |
| 668 | |
| 669 | if (outBuffer == NULL || outFence == NULL) { |
| 670 | return BAD_VALUE; |
| 671 | } |
| 672 | |
| 673 | Mutex::Autolock lock(mMutex); |
| 674 | |
| 675 | sp<GraphicBuffer> buffer(NULL); |
| 676 | sp<Fence> fence(NULL); |
| 677 | status_t result = mGraphicBufferProducer->detachNextBuffer( |
| 678 | &buffer, &fence); |
| 679 | if (result != NO_ERROR) { |
| 680 | return result; |
| 681 | } |
| 682 | |
Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 683 | *outBuffer = buffer; |
Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 684 | if (fence != NULL && fence->isValid()) { |
| 685 | *outFence = fence; |
| 686 | } else { |
| 687 | *outFence = Fence::NO_FENCE; |
| 688 | } |
| 689 | |
| 690 | return NO_ERROR; |
| 691 | } |
| 692 | |
| 693 | int Surface::attachBuffer(ANativeWindowBuffer* buffer) |
| 694 | { |
| 695 | ATRACE_CALL(); |
| 696 | ALOGV("Surface::attachBuffer"); |
| 697 | |
| 698 | Mutex::Autolock lock(mMutex); |
| 699 | |
| 700 | sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer)); |
| 701 | int32_t attachedSlot = -1; |
| 702 | status_t result = mGraphicBufferProducer->attachBuffer( |
| 703 | &attachedSlot, graphicBuffer); |
| 704 | if (result != NO_ERROR) { |
| 705 | ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result); |
| 706 | return result; |
| 707 | } |
| 708 | mSlots[attachedSlot].buffer = graphicBuffer; |
| 709 | |
| 710 | return NO_ERROR; |
| 711 | } |
| 712 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 713 | int Surface::setUsage(uint32_t reqUsage) |
| 714 | { |
| 715 | ALOGV("Surface::setUsage"); |
| 716 | Mutex::Autolock lock(mMutex); |
| 717 | mReqUsage = reqUsage; |
| 718 | return OK; |
| 719 | } |
| 720 | |
| 721 | int Surface::setCrop(Rect const* rect) |
| 722 | { |
| 723 | ATRACE_CALL(); |
| 724 | |
| 725 | Rect realRect; |
| 726 | if (rect == NULL || rect->isEmpty()) { |
| 727 | realRect.clear(); |
| 728 | } else { |
| 729 | realRect = *rect; |
Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 732 | ALOGV("Surface::setCrop rect=[%d %d %d %d]", |
| 733 | realRect.left, realRect.top, realRect.right, realRect.bottom); |
| 734 | |
| 735 | Mutex::Autolock lock(mMutex); |
| 736 | mCrop = realRect; |
| 737 | return NO_ERROR; |
| 738 | } |
| 739 | |
| 740 | int Surface::setBufferCount(int bufferCount) |
| 741 | { |
| 742 | ATRACE_CALL(); |
| 743 | ALOGV("Surface::setBufferCount"); |
| 744 | Mutex::Autolock lock(mMutex); |
| 745 | |
| 746 | status_t err = mGraphicBufferProducer->setBufferCount(bufferCount); |
| 747 | ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s", |
| 748 | bufferCount, strerror(-err)); |
Mathias Agopian | 9014726 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 749 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 750 | if (err == NO_ERROR) { |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 751 | freeAllBuffers(); |
Mathias Agopian | 87a96ea | 2011-08-23 21:09:41 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 754 | return err; |
| 755 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 756 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 757 | int Surface::setBuffersDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 758 | { |
| 759 | ATRACE_CALL(); |
| 760 | ALOGV("Surface::setBuffersDimensions"); |
| 761 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 762 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 763 | return BAD_VALUE; |
| 764 | |
| 765 | Mutex::Autolock lock(mMutex); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 766 | mReqWidth = width; |
| 767 | mReqHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 768 | return NO_ERROR; |
| 769 | } |
| 770 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 771 | int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 772 | { |
| 773 | ATRACE_CALL(); |
| 774 | ALOGV("Surface::setBuffersUserDimensions"); |
| 775 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 776 | if ((width && !height) || (!width && height)) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 777 | return BAD_VALUE; |
| 778 | |
| 779 | Mutex::Autolock lock(mMutex); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 780 | mUserWidth = width; |
| 781 | mUserHeight = height; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 782 | return NO_ERROR; |
| 783 | } |
| 784 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 785 | int Surface::setBuffersFormat(PixelFormat format) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 786 | { |
| 787 | ALOGV("Surface::setBuffersFormat"); |
| 788 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 789 | Mutex::Autolock lock(mMutex); |
| 790 | mReqFormat = format; |
| 791 | return NO_ERROR; |
| 792 | } |
| 793 | |
| 794 | int Surface::setScalingMode(int mode) |
| 795 | { |
| 796 | ATRACE_CALL(); |
| 797 | ALOGV("Surface::setScalingMode(%d)", mode); |
| 798 | |
| 799 | switch (mode) { |
| 800 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 801 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 802 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: |
| 803 | break; |
| 804 | default: |
| 805 | ALOGE("unknown scaling mode: %d", mode); |
| 806 | return BAD_VALUE; |
| 807 | } |
| 808 | |
| 809 | Mutex::Autolock lock(mMutex); |
| 810 | mScalingMode = mode; |
| 811 | return NO_ERROR; |
| 812 | } |
| 813 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 814 | int Surface::setBuffersTransform(uint32_t transform) |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 815 | { |
| 816 | ATRACE_CALL(); |
| 817 | ALOGV("Surface::setBuffersTransform"); |
| 818 | Mutex::Autolock lock(mMutex); |
| 819 | mTransform = transform; |
| 820 | return NO_ERROR; |
| 821 | } |
| 822 | |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 823 | int Surface::setBuffersStickyTransform(uint32_t transform) |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 824 | { |
| 825 | ATRACE_CALL(); |
| 826 | ALOGV("Surface::setBuffersStickyTransform"); |
| 827 | Mutex::Autolock lock(mMutex); |
| 828 | mStickyTransform = transform; |
| 829 | return NO_ERROR; |
| 830 | } |
| 831 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 832 | int Surface::setBuffersTimestamp(int64_t timestamp) |
| 833 | { |
| 834 | ALOGV("Surface::setBuffersTimestamp"); |
| 835 | Mutex::Autolock lock(mMutex); |
| 836 | mTimestamp = timestamp; |
| 837 | return NO_ERROR; |
| 838 | } |
| 839 | |
Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 840 | int Surface::setBuffersDataSpace(android_dataspace dataSpace) |
| 841 | { |
| 842 | ALOGV("Surface::setBuffersDataSpace"); |
| 843 | Mutex::Autolock lock(mMutex); |
| 844 | mDataSpace = dataSpace; |
| 845 | return NO_ERROR; |
| 846 | } |
| 847 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 848 | void Surface::freeAllBuffers() { |
| 849 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 850 | mSlots[i].buffer = 0; |
| 851 | } |
| 852 | } |
| 853 | |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 854 | void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) { |
| 855 | ATRACE_CALL(); |
| 856 | ALOGV("Surface::setSurfaceDamage"); |
| 857 | Mutex::Autolock lock(mMutex); |
| 858 | |
Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 859 | if (mConnectedToCpu || numRects == 0) { |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 860 | mDirtyRegion = Region::INVALID_REGION; |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | mDirtyRegion.clear(); |
| 865 | for (size_t r = 0; r < numRects; ++r) { |
| 866 | // We intentionally flip top and bottom here, since because they're |
| 867 | // specified with a bottom-left origin, top > bottom, which fails |
| 868 | // validation in the Region class. We will fix this up when we flip to a |
| 869 | // top-left origin in queueBuffer. |
| 870 | Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top); |
| 871 | mDirtyRegion.orSelf(rect); |
| 872 | } |
| 873 | } |
| 874 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 875 | // ---------------------------------------------------------------------- |
| 876 | // the lock/unlock APIs must be used from the same thread |
| 877 | |
| 878 | static status_t copyBlt( |
| 879 | const sp<GraphicBuffer>& dst, |
| 880 | const sp<GraphicBuffer>& src, |
| 881 | const Region& reg) |
| 882 | { |
| 883 | // src and dst with, height and format must be identical. no verification |
| 884 | // is done here. |
| 885 | status_t err; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 886 | uint8_t* src_bits = NULL; |
| 887 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), |
| 888 | reinterpret_cast<void**>(&src_bits)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 889 | ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
| 890 | |
| 891 | uint8_t* dst_bits = NULL; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 892 | err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), |
| 893 | reinterpret_cast<void**>(&dst_bits)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 894 | ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
| 895 | |
| 896 | Region::const_iterator head(reg.begin()); |
| 897 | Region::const_iterator tail(reg.end()); |
| 898 | if (head != tail && src_bits && dst_bits) { |
| 899 | const size_t bpp = bytesPerPixel(src->format); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 900 | const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp; |
| 901 | const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 902 | |
| 903 | while (head != tail) { |
| 904 | const Rect& r(*head++); |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 905 | int32_t h = r.height(); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 906 | if (h <= 0) continue; |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 907 | size_t size = static_cast<uint32_t>(r.width()) * bpp; |
| 908 | uint8_t const * s = src_bits + |
| 909 | static_cast<uint32_t>(r.left + src->stride * r.top) * bpp; |
| 910 | uint8_t * d = dst_bits + |
| 911 | static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp; |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 912 | if (dbpr==sbpr && size==sbpr) { |
Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 913 | size *= static_cast<size_t>(h); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 914 | h = 1; |
| 915 | } |
| 916 | do { |
| 917 | memcpy(d, s, size); |
| 918 | d += dbpr; |
| 919 | s += sbpr; |
| 920 | } while (--h > 0); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | if (src_bits) |
| 925 | src->unlock(); |
| 926 | |
| 927 | if (dst_bits) |
| 928 | dst->unlock(); |
| 929 | |
| 930 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | } |
| 932 | |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 933 | // ---------------------------------------------------------------------------- |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 934 | |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 935 | status_t Surface::lock( |
| 936 | ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) |
| 937 | { |
| 938 | if (mLockedBuffer != 0) { |
| 939 | ALOGE("Surface::lock failed, already locked"); |
| 940 | return INVALID_OPERATION; |
| 941 | } |
| 942 | |
| 943 | if (!mConnectedToCpu) { |
| 944 | int err = Surface::connect(NATIVE_WINDOW_API_CPU); |
| 945 | if (err) { |
| 946 | return err; |
| 947 | } |
| 948 | // we're intending to do software rendering from this point |
| 949 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 950 | } |
| 951 | |
| 952 | ANativeWindowBuffer* out; |
| 953 | int fenceFd = -1; |
| 954 | status_t err = dequeueBuffer(&out, &fenceFd); |
| 955 | ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); |
| 956 | if (err == NO_ERROR) { |
| 957 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 958 | const Rect bounds(backBuffer->width, backBuffer->height); |
| 959 | |
| 960 | Region newDirtyRegion; |
| 961 | if (inOutDirtyBounds) { |
| 962 | newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds)); |
| 963 | newDirtyRegion.andSelf(bounds); |
| 964 | } else { |
| 965 | newDirtyRegion.set(bounds); |
| 966 | } |
| 967 | |
| 968 | // figure out if we can copy the frontbuffer back |
| 969 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); |
| 970 | const bool canCopyBack = (frontBuffer != 0 && |
| 971 | backBuffer->width == frontBuffer->width && |
| 972 | backBuffer->height == frontBuffer->height && |
| 973 | backBuffer->format == frontBuffer->format); |
| 974 | |
| 975 | if (canCopyBack) { |
| 976 | // copy the area that is invalid and not repainted this round |
| 977 | const Region copyback(mDirtyRegion.subtract(newDirtyRegion)); |
| 978 | if (!copyback.isEmpty()) |
| 979 | copyBlt(backBuffer, frontBuffer, copyback); |
| 980 | } else { |
| 981 | // if we can't copy-back anything, modify the user's dirty |
| 982 | // region to make sure they redraw the whole buffer |
| 983 | newDirtyRegion.set(bounds); |
| 984 | mDirtyRegion.clear(); |
| 985 | Mutex::Autolock lock(mMutex); |
| 986 | for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) { |
| 987 | mSlots[i].dirtyRegion.clear(); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | |
| 992 | { // scope for the lock |
| 993 | Mutex::Autolock lock(mMutex); |
| 994 | int backBufferSlot(getSlotFromBufferLocked(backBuffer.get())); |
| 995 | if (backBufferSlot >= 0) { |
| 996 | Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion); |
| 997 | mDirtyRegion.subtract(dirtyRegion); |
| 998 | dirtyRegion = newDirtyRegion; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | mDirtyRegion.orSelf(newDirtyRegion); |
| 1003 | if (inOutDirtyBounds) { |
| 1004 | *inOutDirtyBounds = newDirtyRegion.getBounds(); |
| 1005 | } |
| 1006 | |
| 1007 | void* vaddr; |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1008 | status_t res = backBuffer->lockAsync( |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1009 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1010 | newDirtyRegion.bounds(), &vaddr, fenceFd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1011 | |
| 1012 | ALOGW_IF(res, "failed locking buffer (handle = %p)", |
| 1013 | backBuffer->handle); |
| 1014 | |
| 1015 | if (res != 0) { |
| 1016 | err = INVALID_OPERATION; |
| 1017 | } else { |
| 1018 | mLockedBuffer = backBuffer; |
| 1019 | outBuffer->width = backBuffer->width; |
| 1020 | outBuffer->height = backBuffer->height; |
| 1021 | outBuffer->stride = backBuffer->stride; |
| 1022 | outBuffer->format = backBuffer->format; |
| 1023 | outBuffer->bits = vaddr; |
| 1024 | } |
| 1025 | } |
| 1026 | return err; |
| 1027 | } |
| 1028 | |
| 1029 | status_t Surface::unlockAndPost() |
| 1030 | { |
| 1031 | if (mLockedBuffer == 0) { |
| 1032 | ALOGE("Surface::unlockAndPost failed, no locked buffer"); |
| 1033 | return INVALID_OPERATION; |
| 1034 | } |
| 1035 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1036 | int fd = -1; |
| 1037 | status_t err = mLockedBuffer->unlockAsync(&fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1038 | ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); |
| 1039 | |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1040 | err = queueBuffer(mLockedBuffer.get(), fd); |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1041 | ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", |
| 1042 | mLockedBuffer->handle, strerror(-err)); |
| 1043 | |
| 1044 | mPostedBuffer = mLockedBuffer; |
| 1045 | mLockedBuffer = 0; |
| 1046 | return err; |
| 1047 | } |
| 1048 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | }; // namespace android |