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( |
| 27 | const sp<ISurfaceTexture>& surfaceTexture): |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 28 | mSurfaceTexture(surfaceTexture), mAllocator(0), mReqWidth(1), |
| 29 | mReqHeight(1), mReqFormat(DEFAULT_FORMAT), mReqUsage(0), mMutex() { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 30 | // Initialize the ANativeWindow function pointers. |
| 31 | ANativeWindow::setSwapInterval = setSwapInterval; |
| 32 | ANativeWindow::dequeueBuffer = dequeueBuffer; |
| 33 | ANativeWindow::cancelBuffer = cancelBuffer; |
| 34 | ANativeWindow::lockBuffer = lockBuffer; |
| 35 | ANativeWindow::queueBuffer = queueBuffer; |
| 36 | ANativeWindow::query = query; |
| 37 | ANativeWindow::perform = perform; |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 38 | |
| 39 | // Get a reference to the allocator. |
| 40 | mAllocator = mSurfaceTexture->getAllocator(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Jamie Gennis | bae774e | 2011-03-14 15:08:53 -0700 | [diff] [blame] | 43 | sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const { |
| 44 | return mSurfaceTexture; |
| 45 | } |
| 46 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 47 | int SurfaceTextureClient::setSwapInterval(ANativeWindow* window, int interval) { |
| 48 | SurfaceTextureClient* c = getSelf(window); |
| 49 | return c->setSwapInterval(interval); |
| 50 | } |
| 51 | |
| 52 | int SurfaceTextureClient::dequeueBuffer(ANativeWindow* window, |
| 53 | android_native_buffer_t** buffer) { |
| 54 | SurfaceTextureClient* c = getSelf(window); |
| 55 | return c->dequeueBuffer(buffer); |
| 56 | } |
| 57 | |
| 58 | int SurfaceTextureClient::cancelBuffer(ANativeWindow* window, |
| 59 | android_native_buffer_t* buffer) { |
| 60 | SurfaceTextureClient* c = getSelf(window); |
| 61 | return c->cancelBuffer(buffer); |
| 62 | } |
| 63 | |
| 64 | int SurfaceTextureClient::lockBuffer(ANativeWindow* window, |
| 65 | android_native_buffer_t* buffer) { |
| 66 | SurfaceTextureClient* c = getSelf(window); |
| 67 | return c->lockBuffer(buffer); |
| 68 | } |
| 69 | |
| 70 | int SurfaceTextureClient::queueBuffer(ANativeWindow* window, |
| 71 | android_native_buffer_t* buffer) { |
| 72 | SurfaceTextureClient* c = getSelf(window); |
| 73 | return c->queueBuffer(buffer); |
| 74 | } |
| 75 | |
| 76 | int SurfaceTextureClient::query(ANativeWindow* window, int what, int* value) { |
| 77 | SurfaceTextureClient* c = getSelf(window); |
| 78 | return c->query(what, value); |
| 79 | } |
| 80 | |
| 81 | int SurfaceTextureClient::perform(ANativeWindow* window, int operation, ...) { |
| 82 | va_list args; |
| 83 | va_start(args, operation); |
| 84 | SurfaceTextureClient* c = getSelf(window); |
| 85 | return c->perform(operation, args); |
| 86 | } |
| 87 | |
| 88 | int SurfaceTextureClient::setSwapInterval(int interval) { |
| 89 | return INVALID_OPERATION; |
| 90 | } |
| 91 | |
| 92 | int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 93 | LOGV("SurfaceTextureClient::dequeueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 94 | Mutex::Autolock lock(mMutex); |
| 95 | int buf = -1; |
| 96 | status_t err = mSurfaceTexture->dequeueBuffer(&buf); |
| 97 | if (err < 0) { |
Jamie Gennis | 7491722 | 2011-01-28 12:03:52 -0800 | [diff] [blame] | 98 | LOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer failed: %d", err); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 99 | return err; |
| 100 | } |
| 101 | sp<GraphicBuffer>& gbuf(mSlots[buf]); |
| 102 | if (gbuf == 0 || gbuf->getWidth() != mReqWidth || |
| 103 | gbuf->getHeight() != mReqHeight || |
| 104 | uint32_t(gbuf->getPixelFormat()) != mReqFormat || |
| 105 | (gbuf->getUsage() & mReqUsage) != mReqUsage) { |
| 106 | gbuf = mSurfaceTexture->requestBuffer(buf, mReqWidth, mReqHeight, |
| 107 | mReqFormat, mReqUsage); |
| 108 | if (gbuf == 0) { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 109 | LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 110 | return NO_MEMORY; |
| 111 | } |
| 112 | } |
| 113 | *buffer = gbuf.get(); |
| 114 | return OK; |
| 115 | } |
| 116 | |
| 117 | int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 118 | LOGV("SurfaceTextureClient::cancelBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 119 | Mutex::Autolock lock(mMutex); |
| 120 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
Jamie Gennis | 73e8b9e | 2011-01-15 13:05:24 -0800 | [diff] [blame] | 121 | if (mSlots[i]->handle == buffer->handle) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 122 | mSurfaceTexture->cancelBuffer(i); |
| 123 | return OK; |
| 124 | } |
| 125 | } |
| 126 | return BAD_VALUE; |
| 127 | } |
| 128 | |
| 129 | int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 130 | LOGV("SurfaceTextureClient::lockBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 131 | Mutex::Autolock lock(mMutex); |
| 132 | return OK; |
| 133 | } |
| 134 | |
| 135 | int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 136 | LOGV("SurfaceTextureClient::queueBuffer"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 137 | Mutex::Autolock lock(mMutex); |
| 138 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
Jamie Gennis | 73e8b9e | 2011-01-15 13:05:24 -0800 | [diff] [blame] | 139 | if (mSlots[i]->handle == buffer->handle) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 140 | return mSurfaceTexture->queueBuffer(i); |
| 141 | } |
| 142 | } |
| 143 | LOGE("queueBuffer: unknown buffer queued"); |
| 144 | return BAD_VALUE; |
| 145 | } |
| 146 | |
| 147 | int SurfaceTextureClient::query(int what, int* value) { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 148 | LOGV("SurfaceTextureClient::query"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 149 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 150 | switch (what) { |
| 151 | case NATIVE_WINDOW_WIDTH: |
| 152 | case NATIVE_WINDOW_HEIGHT: |
| 153 | // XXX: How should SurfaceTexture behave if setBuffersGeometry didn't |
| 154 | // override the size? |
| 155 | *value = 0; |
| 156 | return NO_ERROR; |
| 157 | case NATIVE_WINDOW_FORMAT: |
| 158 | *value = DEFAULT_FORMAT; |
| 159 | return NO_ERROR; |
| 160 | case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: |
| 161 | *value = MIN_UNDEQUEUED_BUFFERS; |
| 162 | return NO_ERROR; |
Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 163 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: |
| 164 | // SurfaceTextureClient currently never queues frames to SurfaceFlinger. |
| 165 | *value = 0; |
| 166 | return NO_ERROR; |
Jamie Gennis | 391bbe2 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 167 | case NATIVE_WINDOW_CONCRETE_TYPE: |
| 168 | *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT; |
| 169 | return NO_ERROR; |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 170 | } |
| 171 | return BAD_VALUE; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int SurfaceTextureClient::perform(int operation, va_list args) |
| 175 | { |
| 176 | int res = NO_ERROR; |
| 177 | switch (operation) { |
| 178 | case NATIVE_WINDOW_CONNECT: |
| 179 | res = dispatchConnect(args); |
| 180 | break; |
| 181 | case NATIVE_WINDOW_DISCONNECT: |
| 182 | res = dispatchDisconnect(args); |
| 183 | break; |
| 184 | case NATIVE_WINDOW_SET_USAGE: |
| 185 | res = dispatchSetUsage(args); |
| 186 | break; |
| 187 | case NATIVE_WINDOW_SET_CROP: |
| 188 | res = dispatchSetCrop(args); |
| 189 | break; |
| 190 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 191 | res = dispatchSetBufferCount(args); |
| 192 | break; |
| 193 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 194 | res = dispatchSetBuffersGeometry(args); |
| 195 | break; |
| 196 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 197 | res = dispatchSetBuffersTransform(args); |
| 198 | break; |
| 199 | default: |
| 200 | res = NAME_NOT_FOUND; |
| 201 | break; |
| 202 | } |
| 203 | return res; |
| 204 | } |
| 205 | |
| 206 | int SurfaceTextureClient::dispatchConnect(va_list args) { |
| 207 | int api = va_arg(args, int); |
| 208 | return connect(api); |
| 209 | } |
| 210 | |
| 211 | int SurfaceTextureClient::dispatchDisconnect(va_list args) { |
| 212 | int api = va_arg(args, int); |
| 213 | return disconnect(api); |
| 214 | } |
| 215 | |
| 216 | int SurfaceTextureClient::dispatchSetUsage(va_list args) { |
| 217 | int usage = va_arg(args, int); |
| 218 | return setUsage(usage); |
| 219 | } |
| 220 | |
| 221 | int SurfaceTextureClient::dispatchSetCrop(va_list args) { |
| 222 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 223 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 224 | } |
| 225 | |
| 226 | int SurfaceTextureClient::dispatchSetBufferCount(va_list args) { |
| 227 | size_t bufferCount = va_arg(args, size_t); |
| 228 | return setBufferCount(bufferCount); |
| 229 | } |
| 230 | |
| 231 | int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) { |
| 232 | int w = va_arg(args, int); |
| 233 | int h = va_arg(args, int); |
| 234 | int f = va_arg(args, int); |
| 235 | return setBuffersGeometry(w, h, f); |
| 236 | } |
| 237 | |
| 238 | int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) { |
| 239 | int transform = va_arg(args, int); |
| 240 | return setBuffersTransform(transform); |
| 241 | } |
| 242 | |
| 243 | int SurfaceTextureClient::connect(int api) { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 244 | LOGV("SurfaceTextureClient::connect"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 245 | // XXX: Implement this! |
| 246 | return INVALID_OPERATION; |
| 247 | } |
| 248 | |
| 249 | int SurfaceTextureClient::disconnect(int api) { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 250 | LOGV("SurfaceTextureClient::disconnect"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 251 | // XXX: Implement this! |
| 252 | return INVALID_OPERATION; |
| 253 | } |
| 254 | |
| 255 | int SurfaceTextureClient::setUsage(uint32_t reqUsage) |
| 256 | { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 257 | LOGV("SurfaceTextureClient::setUsage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 258 | Mutex::Autolock lock(mMutex); |
| 259 | mReqUsage = reqUsage; |
| 260 | return OK; |
| 261 | } |
| 262 | |
| 263 | int SurfaceTextureClient::setCrop(Rect const* rect) |
| 264 | { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 265 | LOGV("SurfaceTextureClient::setCrop"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 266 | Mutex::Autolock lock(mMutex); |
| 267 | |
Jamie Gennis | 68f9127 | 2011-01-28 18:21:54 -0800 | [diff] [blame] | 268 | Rect realRect; |
| 269 | if (rect == NULL || rect->isEmpty()) { |
| 270 | realRect = Rect(0, 0); |
| 271 | } else { |
| 272 | realRect = *rect; |
| 273 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 274 | |
| 275 | status_t err = mSurfaceTexture->setCrop(*rect); |
Jamie Gennis | 68f9127 | 2011-01-28 18:21:54 -0800 | [diff] [blame] | 276 | LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err)); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 277 | |
| 278 | return err; |
| 279 | } |
| 280 | |
| 281 | int SurfaceTextureClient::setBufferCount(int bufferCount) |
| 282 | { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 283 | LOGV("SurfaceTextureClient::setBufferCount"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 284 | Mutex::Autolock lock(mMutex); |
| 285 | |
| 286 | status_t err = mSurfaceTexture->setBufferCount(bufferCount); |
| 287 | LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s", |
| 288 | bufferCount, strerror(-err)); |
| 289 | |
| 290 | if (err == NO_ERROR) { |
| 291 | freeAllBuffers(); |
| 292 | } |
| 293 | |
| 294 | return err; |
| 295 | } |
| 296 | |
| 297 | int SurfaceTextureClient::setBuffersGeometry(int w, int h, int format) |
| 298 | { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 299 | LOGV("SurfaceTextureClient::setBuffersGeometry"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 300 | Mutex::Autolock lock(mMutex); |
| 301 | |
| 302 | if (w<0 || h<0 || format<0) |
| 303 | return BAD_VALUE; |
| 304 | |
| 305 | if ((w && !h) || (!w && h)) |
| 306 | return BAD_VALUE; |
| 307 | |
| 308 | mReqWidth = w; |
| 309 | mReqHeight = h; |
| 310 | mReqFormat = format; |
| 311 | |
Jamie Gennis | 68f9127 | 2011-01-28 18:21:54 -0800 | [diff] [blame] | 312 | status_t err = mSurfaceTexture->setCrop(Rect(0, 0)); |
| 313 | LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err)); |
| 314 | |
| 315 | return err; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | int SurfaceTextureClient::setBuffersTransform(int transform) |
| 319 | { |
Jamie Gennis | e5366c5 | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 320 | LOGV("SurfaceTextureClient::setBuffersTransform"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 321 | Mutex::Autolock lock(mMutex); |
| 322 | status_t err = mSurfaceTexture->setTransform(transform); |
| 323 | return err; |
| 324 | } |
| 325 | |
| 326 | void SurfaceTextureClient::freeAllBuffers() { |
| 327 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 328 | mSlots[i] = 0; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | }; // namespace android |