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