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