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