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