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