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