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