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