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