Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 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 "SurfaceTextureClient" |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 20 | |
Mathias Agopian | b0e76f4 | 2012-03-23 14:15:44 -0700 | [diff] [blame] | 21 | #include <android/native_window.h> |
| 22 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 23 | #include <utils/Log.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 24 | #include <utils/Trace.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 25 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 26 | #include <gui/ISurfaceComposer.h> |
| 27 | #include <gui/SurfaceComposerClient.h> |
Mathias Agopian | b0e76f4 | 2012-03-23 14:15:44 -0700 | [diff] [blame] | 28 | #include <gui/SurfaceTexture.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 29 | #include <gui/SurfaceTextureClient.h> |
| 30 | |
Mathias Agopian | 41f673c | 2011-11-17 17:48:35 -0800 | [diff] [blame] | 31 | #include <private/gui/ComposerService.h> |
| 32 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | SurfaceTextureClient::SurfaceTextureClient( |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 36 | const sp<ISurfaceTexture>& surfaceTexture) |
| 37 | { |
| 38 | SurfaceTextureClient::init(); |
| 39 | SurfaceTextureClient::setISurfaceTexture(surfaceTexture); |
| 40 | } |
| 41 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 42 | // see SurfaceTextureClient.h |
| 43 | SurfaceTextureClient::SurfaceTextureClient(const |
| 44 | sp<SurfaceTexture>& surfaceTexture) |
| 45 | { |
| 46 | SurfaceTextureClient::init(); |
| 47 | SurfaceTextureClient::setISurfaceTexture(surfaceTexture->getBufferQueue()); |
| 48 | } |
| 49 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 50 | SurfaceTextureClient::SurfaceTextureClient() { |
| 51 | SurfaceTextureClient::init(); |
| 52 | } |
| 53 | |
Mathias Agopian | a36bcd5 | 2011-11-17 18:46:09 -0800 | [diff] [blame] | 54 | SurfaceTextureClient::~SurfaceTextureClient() { |
| 55 | if (mConnectedToCpu) { |
| 56 | SurfaceTextureClient::disconnect(NATIVE_WINDOW_API_CPU); |
| 57 | } |
| 58 | } |
| 59 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 60 | void SurfaceTextureClient::init() { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 61 | // Initialize the ANativeWindow function pointers. |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 62 | ANativeWindow::setSwapInterval = hook_setSwapInterval; |
| 63 | ANativeWindow::dequeueBuffer = hook_dequeueBuffer; |
| 64 | ANativeWindow::cancelBuffer = hook_cancelBuffer; |
| 65 | ANativeWindow::lockBuffer = hook_lockBuffer; |
| 66 | ANativeWindow::queueBuffer = hook_queueBuffer; |
| 67 | ANativeWindow::query = hook_query; |
| 68 | ANativeWindow::perform = hook_perform; |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 69 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 70 | const_cast<int&>(ANativeWindow::minSwapInterval) = 0; |
| 71 | const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; |
| 72 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 73 | mReqWidth = 0; |
| 74 | mReqHeight = 0; |
| 75 | mReqFormat = 0; |
| 76 | mReqUsage = 0; |
| 77 | mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 78 | mCrop.clear(); |
| 79 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 80 | mTransform = 0; |
Mathias Agopian | bb66c9b | 2011-07-21 14:50:29 -0700 | [diff] [blame] | 81 | mDefaultWidth = 0; |
| 82 | mDefaultHeight = 0; |
| 83 | mTransformHint = 0; |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 84 | mConnectedToCpu = false; |
| 85 | } |
| 86 | |
| 87 | void SurfaceTextureClient::setISurfaceTexture( |
| 88 | const sp<ISurfaceTexture>& surfaceTexture) |
| 89 | { |
| 90 | mSurfaceTexture = surfaceTexture; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Jamie Gennis | bae774e | 2011-03-14 15:08:53 -0700 | [diff] [blame] | 93 | sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const { |
| 94 | return mSurfaceTexture; |
| 95 | } |
| 96 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 97 | int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 98 | SurfaceTextureClient* c = getSelf(window); |
| 99 | return c->setSwapInterval(interval); |
| 100 | } |
| 101 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 102 | int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window, |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 103 | ANativeWindowBuffer** buffer) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 104 | SurfaceTextureClient* c = getSelf(window); |
| 105 | return c->dequeueBuffer(buffer); |
| 106 | } |
| 107 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 108 | int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window, |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 109 | ANativeWindowBuffer* buffer) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 110 | SurfaceTextureClient* c = getSelf(window); |
| 111 | return c->cancelBuffer(buffer); |
| 112 | } |
| 113 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 114 | int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window, |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 115 | ANativeWindowBuffer* buffer) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 116 | SurfaceTextureClient* c = getSelf(window); |
| 117 | return c->lockBuffer(buffer); |
| 118 | } |
| 119 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 120 | int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window, |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 121 | ANativeWindowBuffer* buffer) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 122 | SurfaceTextureClient* c = getSelf(window); |
| 123 | return c->queueBuffer(buffer); |
| 124 | } |
| 125 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 126 | int SurfaceTextureClient::hook_query(const ANativeWindow* window, |
Iliyan Malchev | 41abd67 | 2011-04-14 16:54:38 -0700 | [diff] [blame] | 127 | int what, int* value) { |
| 128 | const SurfaceTextureClient* c = getSelf(window); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 129 | return c->query(what, value); |
| 130 | } |
| 131 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 132 | int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 133 | va_list args; |
| 134 | va_start(args, operation); |
| 135 | SurfaceTextureClient* c = getSelf(window); |
| 136 | return c->perform(operation, args); |
| 137 | } |
| 138 | |
| 139 | int SurfaceTextureClient::setSwapInterval(int interval) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 140 | ATRACE_CALL(); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 141 | // EGL specification states: |
| 142 | // interval is silently clamped to minimum and maximum implementation |
| 143 | // dependent values before being stored. |
| 144 | // Although we don't have to, we apply the same logic here. |
| 145 | |
| 146 | if (interval < minSwapInterval) |
| 147 | interval = minSwapInterval; |
| 148 | |
| 149 | if (interval > maxSwapInterval) |
| 150 | interval = maxSwapInterval; |
| 151 | |
| 152 | status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false); |
| 153 | |
| 154 | return res; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 158 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 159 | ALOGV("SurfaceTextureClient::dequeueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 160 | Mutex::Autolock lock(mMutex); |
| 161 | int buf = -1; |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 162 | status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight, |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 163 | mReqFormat, mReqUsage); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 164 | if (result < 0) { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 165 | ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)" |
Jamie Gennis | 8cd5ba4 | 2011-05-19 13:33:00 -0700 | [diff] [blame] | 166 | "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage, |
| 167 | result); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 168 | return result; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 169 | } |
| 170 | sp<GraphicBuffer>& gbuf(mSlots[buf]); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 171 | if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) { |
| 172 | freeAllBuffers(); |
| 173 | } |
| 174 | |
| 175 | if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) { |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 176 | result = mSurfaceTexture->requestBuffer(buf, &gbuf); |
| 177 | if (result != NO_ERROR) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 178 | ALOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d", |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 179 | result); |
| 180 | return result; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | *buffer = gbuf.get(); |
| 184 | return OK; |
| 185 | } |
| 186 | |
| 187 | int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 188 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 189 | ALOGV("SurfaceTextureClient::cancelBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 190 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 1c44140 | 2011-06-20 12:04:09 -0700 | [diff] [blame] | 191 | int i = getSlotFromBufferLocked(buffer); |
| 192 | if (i < 0) { |
| 193 | return i; |
| 194 | } |
| 195 | mSurfaceTexture->cancelBuffer(i); |
| 196 | return OK; |
| 197 | } |
| 198 | |
| 199 | int SurfaceTextureClient::getSlotFromBufferLocked( |
| 200 | android_native_buffer_t* buffer) const { |
| 201 | bool dumpedState = false; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 202 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
Jamie Gennis | 1c44140 | 2011-06-20 12:04:09 -0700 | [diff] [blame] | 203 | if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) { |
| 204 | return i; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 207 | ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 208 | return BAD_VALUE; |
| 209 | } |
| 210 | |
| 211 | int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 212 | ALOGV("SurfaceTextureClient::lockBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 213 | Mutex::Autolock lock(mMutex); |
| 214 | return OK; |
| 215 | } |
| 216 | |
| 217 | int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 218 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 219 | ALOGV("SurfaceTextureClient::queueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 220 | Mutex::Autolock lock(mMutex); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 221 | int64_t timestamp; |
| 222 | if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { |
| 223 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 224 | ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms", |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 225 | timestamp / 1000000.f); |
| 226 | } else { |
| 227 | timestamp = mTimestamp; |
| 228 | } |
Jamie Gennis | 1c44140 | 2011-06-20 12:04:09 -0700 | [diff] [blame] | 229 | int i = getSlotFromBufferLocked(buffer); |
| 230 | if (i < 0) { |
| 231 | return i; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 232 | } |
Pannag Sanketi | 66378c6 | 2011-09-02 17:37:29 -0700 | [diff] [blame] | 233 | status_t err = mSurfaceTexture->queueBuffer(i, timestamp, |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 234 | mCrop, mScalingMode, mTransform, |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 235 | &mDefaultWidth, &mDefaultHeight, &mTransformHint); |
Pannag Sanketi | 66378c6 | 2011-09-02 17:37:29 -0700 | [diff] [blame] | 236 | if (err != OK) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 237 | ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); |
Pannag Sanketi | 66378c6 | 2011-09-02 17:37:29 -0700 | [diff] [blame] | 238 | } |
| 239 | return err; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Iliyan Malchev | 41abd67 | 2011-04-14 16:54:38 -0700 | [diff] [blame] | 242 | int SurfaceTextureClient::query(int what, int* value) const { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 243 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 244 | ALOGV("SurfaceTextureClient::query"); |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 245 | { // scope for the lock |
| 246 | Mutex::Autolock lock(mMutex); |
| 247 | switch (what) { |
| 248 | case NATIVE_WINDOW_FORMAT: |
| 249 | if (mReqFormat) { |
| 250 | *value = mReqFormat; |
| 251 | return NO_ERROR; |
| 252 | } |
| 253 | break; |
| 254 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 255 | { |
| 256 | sp<ISurfaceComposer> composer( |
| 257 | ComposerService::getComposerService()); |
| 258 | if (composer->authenticateSurfaceTexture(mSurfaceTexture)) { |
| 259 | *value = 1; |
| 260 | } else { |
| 261 | *value = 0; |
| 262 | } |
| 263 | } |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 264 | return NO_ERROR; |
| 265 | case NATIVE_WINDOW_CONCRETE_TYPE: |
| 266 | *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT; |
| 267 | return NO_ERROR; |
| 268 | case NATIVE_WINDOW_DEFAULT_WIDTH: |
| 269 | *value = mDefaultWidth; |
| 270 | return NO_ERROR; |
| 271 | case NATIVE_WINDOW_DEFAULT_HEIGHT: |
| 272 | *value = mDefaultHeight; |
| 273 | return NO_ERROR; |
| 274 | case NATIVE_WINDOW_TRANSFORM_HINT: |
| 275 | *value = mTransformHint; |
| 276 | return NO_ERROR; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 277 | } |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 278 | } |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 279 | return mSurfaceTexture->query(what, value); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | int SurfaceTextureClient::perform(int operation, va_list args) |
| 283 | { |
| 284 | int res = NO_ERROR; |
| 285 | switch (operation) { |
| 286 | case NATIVE_WINDOW_CONNECT: |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 287 | // deprecated. must return NO_ERROR. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 288 | break; |
| 289 | case NATIVE_WINDOW_DISCONNECT: |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 290 | // deprecated. must return NO_ERROR. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 291 | break; |
| 292 | case NATIVE_WINDOW_SET_USAGE: |
| 293 | res = dispatchSetUsage(args); |
| 294 | break; |
| 295 | case NATIVE_WINDOW_SET_CROP: |
| 296 | res = dispatchSetCrop(args); |
| 297 | break; |
| 298 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 299 | res = dispatchSetBufferCount(args); |
| 300 | break; |
| 301 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 302 | res = dispatchSetBuffersGeometry(args); |
| 303 | break; |
| 304 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 305 | res = dispatchSetBuffersTransform(args); |
| 306 | break; |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 307 | case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: |
| 308 | res = dispatchSetBuffersTimestamp(args); |
| 309 | break; |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 310 | case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: |
| 311 | res = dispatchSetBuffersDimensions(args); |
| 312 | break; |
| 313 | case NATIVE_WINDOW_SET_BUFFERS_FORMAT: |
| 314 | res = dispatchSetBuffersFormat(args); |
| 315 | break; |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 316 | case NATIVE_WINDOW_LOCK: |
| 317 | res = dispatchLock(args); |
| 318 | break; |
| 319 | case NATIVE_WINDOW_UNLOCK_AND_POST: |
| 320 | res = dispatchUnlockAndPost(args); |
| 321 | break; |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 322 | case NATIVE_WINDOW_SET_SCALING_MODE: |
| 323 | res = dispatchSetScalingMode(args); |
| 324 | break; |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 325 | case NATIVE_WINDOW_API_CONNECT: |
| 326 | res = dispatchConnect(args); |
| 327 | break; |
| 328 | case NATIVE_WINDOW_API_DISCONNECT: |
| 329 | res = dispatchDisconnect(args); |
| 330 | break; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 331 | default: |
| 332 | res = NAME_NOT_FOUND; |
| 333 | break; |
| 334 | } |
| 335 | return res; |
| 336 | } |
| 337 | |
| 338 | int SurfaceTextureClient::dispatchConnect(va_list args) { |
| 339 | int api = va_arg(args, int); |
| 340 | return connect(api); |
| 341 | } |
| 342 | |
| 343 | int SurfaceTextureClient::dispatchDisconnect(va_list args) { |
| 344 | int api = va_arg(args, int); |
| 345 | return disconnect(api); |
| 346 | } |
| 347 | |
| 348 | int SurfaceTextureClient::dispatchSetUsage(va_list args) { |
| 349 | int usage = va_arg(args, int); |
| 350 | return setUsage(usage); |
| 351 | } |
| 352 | |
| 353 | int SurfaceTextureClient::dispatchSetCrop(va_list args) { |
| 354 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 355 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 356 | } |
| 357 | |
| 358 | int SurfaceTextureClient::dispatchSetBufferCount(va_list args) { |
| 359 | size_t bufferCount = va_arg(args, size_t); |
| 360 | return setBufferCount(bufferCount); |
| 361 | } |
| 362 | |
| 363 | int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) { |
| 364 | int w = va_arg(args, int); |
| 365 | int h = va_arg(args, int); |
| 366 | int f = va_arg(args, int); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 367 | int err = setBuffersDimensions(w, h); |
| 368 | if (err != 0) { |
| 369 | return err; |
| 370 | } |
| 371 | return setBuffersFormat(f); |
| 372 | } |
| 373 | |
| 374 | int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) { |
| 375 | int w = va_arg(args, int); |
| 376 | int h = va_arg(args, int); |
| 377 | return setBuffersDimensions(w, h); |
| 378 | } |
| 379 | |
| 380 | int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) { |
| 381 | int f = va_arg(args, int); |
| 382 | return setBuffersFormat(f); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 385 | int SurfaceTextureClient::dispatchSetScalingMode(va_list args) { |
| 386 | int m = va_arg(args, int); |
| 387 | return setScalingMode(m); |
| 388 | } |
| 389 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 390 | int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) { |
| 391 | int transform = va_arg(args, int); |
| 392 | return setBuffersTransform(transform); |
| 393 | } |
| 394 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 395 | int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) { |
| 396 | int64_t timestamp = va_arg(args, int64_t); |
| 397 | return setBuffersTimestamp(timestamp); |
| 398 | } |
| 399 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 400 | int SurfaceTextureClient::dispatchLock(va_list args) { |
| 401 | ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*); |
| 402 | ARect* inOutDirtyBounds = va_arg(args, ARect*); |
| 403 | return lock(outBuffer, inOutDirtyBounds); |
| 404 | } |
| 405 | |
| 406 | int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) { |
| 407 | return unlockAndPost(); |
| 408 | } |
| 409 | |
| 410 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 411 | int SurfaceTextureClient::connect(int api) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 412 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 413 | ALOGV("SurfaceTextureClient::connect"); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 414 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 5bfc245 | 2011-08-08 19:14:03 -0700 | [diff] [blame] | 415 | int err = mSurfaceTexture->connect(api, |
| 416 | &mDefaultWidth, &mDefaultHeight, &mTransformHint); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 417 | if (!err && api == NATIVE_WINDOW_API_CPU) { |
| 418 | mConnectedToCpu = true; |
| 419 | } |
| 420 | return err; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | int SurfaceTextureClient::disconnect(int api) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 424 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 425 | ALOGV("SurfaceTextureClient::disconnect"); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 426 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 13c5ca3 | 2011-10-18 17:14:33 -0700 | [diff] [blame] | 427 | freeAllBuffers(); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 428 | int err = mSurfaceTexture->disconnect(api); |
Mathias Agopian | 70e3f81 | 2011-08-25 17:03:30 -0700 | [diff] [blame] | 429 | if (!err) { |
Mathias Agopian | 70e3f81 | 2011-08-25 17:03:30 -0700 | [diff] [blame] | 430 | mReqFormat = 0; |
| 431 | mReqWidth = 0; |
| 432 | mReqHeight = 0; |
| 433 | mReqUsage = 0; |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 434 | mCrop.clear(); |
| 435 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 436 | mTransform = 0; |
Mathias Agopian | 70e3f81 | 2011-08-25 17:03:30 -0700 | [diff] [blame] | 437 | if (api == NATIVE_WINDOW_API_CPU) { |
| 438 | mConnectedToCpu = false; |
| 439 | } |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 440 | } |
| 441 | return err; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | int SurfaceTextureClient::setUsage(uint32_t reqUsage) |
| 445 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 446 | ALOGV("SurfaceTextureClient::setUsage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 447 | Mutex::Autolock lock(mMutex); |
| 448 | mReqUsage = reqUsage; |
| 449 | return OK; |
| 450 | } |
| 451 | |
| 452 | int SurfaceTextureClient::setCrop(Rect const* rect) |
| 453 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 454 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 455 | ALOGV("SurfaceTextureClient::setCrop"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 456 | |
Jamie Gennis | 68f9127 | 2011-01-28 18:21:54 -0800 | [diff] [blame] | 457 | Rect realRect; |
| 458 | if (rect == NULL || rect->isEmpty()) { |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 459 | realRect.clear(); |
Jamie Gennis | 68f9127 | 2011-01-28 18:21:54 -0800 | [diff] [blame] | 460 | } else { |
| 461 | realRect = *rect; |
| 462 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 463 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 464 | Mutex::Autolock lock(mMutex); |
| 465 | mCrop = *rect; |
| 466 | return NO_ERROR; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | int SurfaceTextureClient::setBufferCount(int bufferCount) |
| 470 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 471 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 472 | ALOGV("SurfaceTextureClient::setBufferCount"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 473 | Mutex::Autolock lock(mMutex); |
| 474 | |
| 475 | status_t err = mSurfaceTexture->setBufferCount(bufferCount); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 476 | ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s", |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 477 | bufferCount, strerror(-err)); |
| 478 | |
| 479 | if (err == NO_ERROR) { |
| 480 | freeAllBuffers(); |
| 481 | } |
| 482 | |
| 483 | return err; |
| 484 | } |
| 485 | |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 486 | int SurfaceTextureClient::setBuffersDimensions(int w, int h) |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 487 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 488 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 489 | ALOGV("SurfaceTextureClient::setBuffersDimensions"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 490 | |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 491 | if (w<0 || h<0) |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 492 | return BAD_VALUE; |
| 493 | |
| 494 | if ((w && !h) || (!w && h)) |
| 495 | return BAD_VALUE; |
| 496 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 497 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 498 | mReqWidth = w; |
| 499 | mReqHeight = h; |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 500 | mCrop.clear(); |
| 501 | return NO_ERROR; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 504 | int SurfaceTextureClient::setBuffersFormat(int format) |
| 505 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 506 | ALOGV("SurfaceTextureClient::setBuffersFormat"); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 507 | |
| 508 | if (format<0) |
| 509 | return BAD_VALUE; |
| 510 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 511 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 512 | mReqFormat = format; |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 513 | return NO_ERROR; |
| 514 | } |
| 515 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 516 | int SurfaceTextureClient::setScalingMode(int mode) |
| 517 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 518 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 519 | ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode); |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 520 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 521 | switch (mode) { |
| 522 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: |
| 523 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: |
| 524 | break; |
| 525 | default: |
| 526 | ALOGE("unknown scaling mode: %d", mode); |
| 527 | return BAD_VALUE; |
| 528 | } |
| 529 | |
| 530 | Mutex::Autolock lock(mMutex); |
| 531 | mScalingMode = mode; |
| 532 | return NO_ERROR; |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 535 | int SurfaceTextureClient::setBuffersTransform(int transform) |
| 536 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 537 | ATRACE_CALL(); |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 538 | ALOGV("SurfaceTextureClient::setBuffersTransform"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 539 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 540 | mTransform = transform; |
| 541 | return NO_ERROR; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 544 | int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp) |
| 545 | { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 546 | ALOGV("SurfaceTextureClient::setBuffersTimestamp"); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 547 | Mutex::Autolock lock(mMutex); |
| 548 | mTimestamp = timestamp; |
| 549 | return NO_ERROR; |
| 550 | } |
| 551 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 552 | void SurfaceTextureClient::freeAllBuffers() { |
| 553 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 554 | mSlots[i] = 0; |
| 555 | } |
| 556 | } |
| 557 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 558 | // ---------------------------------------------------------------------- |
| 559 | // the lock/unlock APIs must be used from the same thread |
| 560 | |
| 561 | static status_t copyBlt( |
| 562 | const sp<GraphicBuffer>& dst, |
| 563 | const sp<GraphicBuffer>& src, |
| 564 | const Region& reg) |
| 565 | { |
| 566 | // src and dst with, height and format must be identical. no verification |
| 567 | // is done here. |
| 568 | status_t err; |
| 569 | uint8_t const * src_bits = NULL; |
| 570 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 571 | ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 572 | |
| 573 | uint8_t* dst_bits = NULL; |
| 574 | err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 575 | ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 576 | |
| 577 | Region::const_iterator head(reg.begin()); |
| 578 | Region::const_iterator tail(reg.end()); |
| 579 | if (head != tail && src_bits && dst_bits) { |
| 580 | const size_t bpp = bytesPerPixel(src->format); |
| 581 | const size_t dbpr = dst->stride * bpp; |
| 582 | const size_t sbpr = src->stride * bpp; |
| 583 | |
| 584 | while (head != tail) { |
| 585 | const Rect& r(*head++); |
| 586 | ssize_t h = r.height(); |
| 587 | if (h <= 0) continue; |
| 588 | size_t size = r.width() * bpp; |
| 589 | uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp; |
| 590 | uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp; |
| 591 | if (dbpr==sbpr && size==sbpr) { |
| 592 | size *= h; |
| 593 | h = 1; |
| 594 | } |
| 595 | do { |
| 596 | memcpy(d, s, size); |
| 597 | d += dbpr; |
| 598 | s += sbpr; |
| 599 | } while (--h > 0); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (src_bits) |
| 604 | src->unlock(); |
| 605 | |
| 606 | if (dst_bits) |
| 607 | dst->unlock(); |
| 608 | |
| 609 | return err; |
| 610 | } |
| 611 | |
| 612 | // ---------------------------------------------------------------------------- |
| 613 | |
| 614 | status_t SurfaceTextureClient::lock( |
| 615 | ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) |
| 616 | { |
| 617 | if (mLockedBuffer != 0) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 618 | ALOGE("Surface::lock failed, already locked"); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 619 | return INVALID_OPERATION; |
| 620 | } |
| 621 | |
| 622 | if (!mConnectedToCpu) { |
| 623 | int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU); |
| 624 | if (err) { |
| 625 | return err; |
| 626 | } |
| 627 | // we're intending to do software rendering from this point |
| 628 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 629 | } |
| 630 | |
| 631 | ANativeWindowBuffer* out; |
| 632 | status_t err = dequeueBuffer(&out); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 633 | ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 634 | if (err == NO_ERROR) { |
| 635 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); |
| 636 | err = lockBuffer(backBuffer.get()); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 637 | ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)", |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 638 | backBuffer->handle, strerror(-err)); |
| 639 | if (err == NO_ERROR) { |
| 640 | const Rect bounds(backBuffer->width, backBuffer->height); |
| 641 | |
| 642 | Region newDirtyRegion; |
| 643 | if (inOutDirtyBounds) { |
| 644 | newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds)); |
| 645 | newDirtyRegion.andSelf(bounds); |
| 646 | } else { |
| 647 | newDirtyRegion.set(bounds); |
| 648 | } |
| 649 | |
| 650 | // figure out if we can copy the frontbuffer back |
| 651 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); |
| 652 | const bool canCopyBack = (frontBuffer != 0 && |
| 653 | backBuffer->width == frontBuffer->width && |
| 654 | backBuffer->height == frontBuffer->height && |
| 655 | backBuffer->format == frontBuffer->format); |
| 656 | |
| 657 | if (canCopyBack) { |
| 658 | // copy the area that is invalid and not repainted this round |
| 659 | const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion)); |
| 660 | if (!copyback.isEmpty()) |
| 661 | copyBlt(backBuffer, frontBuffer, copyback); |
| 662 | } else { |
| 663 | // if we can't copy-back anything, modify the user's dirty |
| 664 | // region to make sure they redraw the whole buffer |
| 665 | newDirtyRegion.set(bounds); |
| 666 | } |
| 667 | |
| 668 | // keep track of the are of the buffer that is "clean" |
| 669 | // (ie: that will be redrawn) |
| 670 | mOldDirtyRegion = newDirtyRegion; |
| 671 | |
| 672 | if (inOutDirtyBounds) { |
| 673 | *inOutDirtyBounds = newDirtyRegion.getBounds(); |
| 674 | } |
| 675 | |
| 676 | void* vaddr; |
| 677 | status_t res = backBuffer->lock( |
| 678 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 679 | newDirtyRegion.bounds(), &vaddr); |
| 680 | |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 681 | ALOGW_IF(res, "failed locking buffer (handle = %p)", |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 682 | backBuffer->handle); |
| 683 | |
| 684 | mLockedBuffer = backBuffer; |
| 685 | outBuffer->width = backBuffer->width; |
| 686 | outBuffer->height = backBuffer->height; |
| 687 | outBuffer->stride = backBuffer->stride; |
| 688 | outBuffer->format = backBuffer->format; |
| 689 | outBuffer->bits = vaddr; |
| 690 | } |
| 691 | } |
| 692 | return err; |
| 693 | } |
| 694 | |
| 695 | status_t SurfaceTextureClient::unlockAndPost() |
| 696 | { |
| 697 | if (mLockedBuffer == 0) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 698 | ALOGE("Surface::unlockAndPost failed, no locked buffer"); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 699 | return INVALID_OPERATION; |
| 700 | } |
| 701 | |
| 702 | status_t err = mLockedBuffer->unlock(); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 703 | ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 704 | |
| 705 | err = queueBuffer(mLockedBuffer.get()); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 706 | ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 707 | mLockedBuffer->handle, strerror(-err)); |
| 708 | |
| 709 | mPostedBuffer = mLockedBuffer; |
| 710 | mLockedBuffer = 0; |
| 711 | return err; |
| 712 | } |
| 713 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 714 | }; // namespace android |