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; |
| 159 | } |
| 160 | return BAD_VALUE; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int SurfaceTextureClient::perform(int operation, va_list args) |
| 164 | { |
| 165 | int res = NO_ERROR; |
| 166 | switch (operation) { |
| 167 | case NATIVE_WINDOW_CONNECT: |
| 168 | res = dispatchConnect(args); |
| 169 | break; |
| 170 | case NATIVE_WINDOW_DISCONNECT: |
| 171 | res = dispatchDisconnect(args); |
| 172 | break; |
| 173 | case NATIVE_WINDOW_SET_USAGE: |
| 174 | res = dispatchSetUsage(args); |
| 175 | break; |
| 176 | case NATIVE_WINDOW_SET_CROP: |
| 177 | res = dispatchSetCrop(args); |
| 178 | break; |
| 179 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 180 | res = dispatchSetBufferCount(args); |
| 181 | break; |
| 182 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 183 | res = dispatchSetBuffersGeometry(args); |
| 184 | break; |
| 185 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 186 | res = dispatchSetBuffersTransform(args); |
| 187 | break; |
| 188 | default: |
| 189 | res = NAME_NOT_FOUND; |
| 190 | break; |
| 191 | } |
| 192 | return res; |
| 193 | } |
| 194 | |
| 195 | int SurfaceTextureClient::dispatchConnect(va_list args) { |
| 196 | int api = va_arg(args, int); |
| 197 | return connect(api); |
| 198 | } |
| 199 | |
| 200 | int SurfaceTextureClient::dispatchDisconnect(va_list args) { |
| 201 | int api = va_arg(args, int); |
| 202 | return disconnect(api); |
| 203 | } |
| 204 | |
| 205 | int SurfaceTextureClient::dispatchSetUsage(va_list args) { |
| 206 | int usage = va_arg(args, int); |
| 207 | return setUsage(usage); |
| 208 | } |
| 209 | |
| 210 | int SurfaceTextureClient::dispatchSetCrop(va_list args) { |
| 211 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 212 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 213 | } |
| 214 | |
| 215 | int SurfaceTextureClient::dispatchSetBufferCount(va_list args) { |
| 216 | size_t bufferCount = va_arg(args, size_t); |
| 217 | return setBufferCount(bufferCount); |
| 218 | } |
| 219 | |
| 220 | int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) { |
| 221 | int w = va_arg(args, int); |
| 222 | int h = va_arg(args, int); |
| 223 | int f = va_arg(args, int); |
| 224 | return setBuffersGeometry(w, h, f); |
| 225 | } |
| 226 | |
| 227 | int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) { |
| 228 | int transform = va_arg(args, int); |
| 229 | return setBuffersTransform(transform); |
| 230 | } |
| 231 | |
| 232 | int SurfaceTextureClient::connect(int api) { |
Jamie Gennis | e8d0e8a | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 233 | LOGV("SurfaceTextureClient::connect"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 234 | // XXX: Implement this! |
| 235 | return INVALID_OPERATION; |
| 236 | } |
| 237 | |
| 238 | int SurfaceTextureClient::disconnect(int api) { |
Jamie Gennis | e8d0e8a | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 239 | LOGV("SurfaceTextureClient::disconnect"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 240 | // XXX: Implement this! |
| 241 | return INVALID_OPERATION; |
| 242 | } |
| 243 | |
| 244 | int SurfaceTextureClient::setUsage(uint32_t reqUsage) |
| 245 | { |
Jamie Gennis | e8d0e8a | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 246 | LOGV("SurfaceTextureClient::setUsage"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 247 | Mutex::Autolock lock(mMutex); |
| 248 | mReqUsage = reqUsage; |
| 249 | return OK; |
| 250 | } |
| 251 | |
| 252 | int SurfaceTextureClient::setCrop(Rect const* rect) |
| 253 | { |
Jamie Gennis | e8d0e8a | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 254 | LOGV("SurfaceTextureClient::setCrop"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 255 | Mutex::Autolock lock(mMutex); |
| 256 | |
Jamie Gennis | 2ece4cd | 2011-01-28 18:21:54 -0800 | [diff] [blame] | 257 | Rect realRect; |
| 258 | if (rect == NULL || rect->isEmpty()) { |
| 259 | realRect = Rect(0, 0); |
| 260 | } else { |
| 261 | realRect = *rect; |
| 262 | } |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 263 | |
| 264 | status_t err = mSurfaceTexture->setCrop(*rect); |
Jamie Gennis | 2ece4cd | 2011-01-28 18:21:54 -0800 | [diff] [blame] | 265 | LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err)); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 266 | |
| 267 | return err; |
| 268 | } |
| 269 | |
| 270 | int SurfaceTextureClient::setBufferCount(int bufferCount) |
| 271 | { |
Jamie Gennis | e8d0e8a | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 272 | LOGV("SurfaceTextureClient::setBufferCount"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 273 | Mutex::Autolock lock(mMutex); |
| 274 | |
| 275 | status_t err = mSurfaceTexture->setBufferCount(bufferCount); |
| 276 | LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s", |
| 277 | bufferCount, strerror(-err)); |
| 278 | |
| 279 | if (err == NO_ERROR) { |
| 280 | freeAllBuffers(); |
| 281 | } |
| 282 | |
| 283 | return err; |
| 284 | } |
| 285 | |
| 286 | int SurfaceTextureClient::setBuffersGeometry(int w, int h, int format) |
| 287 | { |
Jamie Gennis | e8d0e8a | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 288 | LOGV("SurfaceTextureClient::setBuffersGeometry"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 289 | Mutex::Autolock lock(mMutex); |
| 290 | |
| 291 | if (w<0 || h<0 || format<0) |
| 292 | return BAD_VALUE; |
| 293 | |
| 294 | if ((w && !h) || (!w && h)) |
| 295 | return BAD_VALUE; |
| 296 | |
| 297 | mReqWidth = w; |
| 298 | mReqHeight = h; |
| 299 | mReqFormat = format; |
| 300 | |
Jamie Gennis | 2ece4cd | 2011-01-28 18:21:54 -0800 | [diff] [blame] | 301 | status_t err = mSurfaceTexture->setCrop(Rect(0, 0)); |
| 302 | LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err)); |
| 303 | |
| 304 | return err; |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | int SurfaceTextureClient::setBuffersTransform(int transform) |
| 308 | { |
Jamie Gennis | e8d0e8a | 2011-01-12 20:22:41 -0800 | [diff] [blame] | 309 | LOGV("SurfaceTextureClient::setBuffersTransform"); |
Jamie Gennis | 68e4a7a | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 310 | Mutex::Autolock lock(mMutex); |
| 311 | status_t err = mSurfaceTexture->setTransform(transform); |
| 312 | return err; |
| 313 | } |
| 314 | |
| 315 | void SurfaceTextureClient::freeAllBuffers() { |
| 316 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 317 | mSlots[i] = 0; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | }; // namespace android |