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