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" |
| 18 | |
| 19 | #include <gui/SurfaceTextureClient.h> |
| 20 | |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | SurfaceTextureClient::SurfaceTextureClient( |
| 26 | const sp<ISurfaceTexture>& surfaceTexture): |
| 27 | mSurfaceTexture(surfaceTexture), mReqWidth(1), mReqHeight(1), |
| 28 | mReqFormat(DEFAULT_FORMAT), mReqUsage(0), mMutex() { |
| 29 | // Initialize the ANativeWindow function pointers. |
| 30 | ANativeWindow::setSwapInterval = setSwapInterval; |
| 31 | ANativeWindow::dequeueBuffer = dequeueBuffer; |
| 32 | ANativeWindow::cancelBuffer = cancelBuffer; |
| 33 | ANativeWindow::lockBuffer = lockBuffer; |
| 34 | ANativeWindow::queueBuffer = queueBuffer; |
| 35 | ANativeWindow::query = query; |
| 36 | ANativeWindow::perform = perform; |
| 37 | } |
| 38 | |
| 39 | int SurfaceTextureClient::setSwapInterval(ANativeWindow* window, int interval) { |
| 40 | SurfaceTextureClient* c = getSelf(window); |
| 41 | return c->setSwapInterval(interval); |
| 42 | } |
| 43 | |
| 44 | int SurfaceTextureClient::dequeueBuffer(ANativeWindow* window, |
| 45 | android_native_buffer_t** buffer) { |
| 46 | SurfaceTextureClient* c = getSelf(window); |
| 47 | return c->dequeueBuffer(buffer); |
| 48 | } |
| 49 | |
| 50 | int SurfaceTextureClient::cancelBuffer(ANativeWindow* window, |
| 51 | android_native_buffer_t* buffer) { |
| 52 | SurfaceTextureClient* c = getSelf(window); |
| 53 | return c->cancelBuffer(buffer); |
| 54 | } |
| 55 | |
| 56 | int SurfaceTextureClient::lockBuffer(ANativeWindow* window, |
| 57 | android_native_buffer_t* buffer) { |
| 58 | SurfaceTextureClient* c = getSelf(window); |
| 59 | return c->lockBuffer(buffer); |
| 60 | } |
| 61 | |
| 62 | int SurfaceTextureClient::queueBuffer(ANativeWindow* window, |
| 63 | android_native_buffer_t* buffer) { |
| 64 | SurfaceTextureClient* c = getSelf(window); |
| 65 | return c->queueBuffer(buffer); |
| 66 | } |
| 67 | |
| 68 | int SurfaceTextureClient::query(ANativeWindow* window, int what, int* value) { |
| 69 | SurfaceTextureClient* c = getSelf(window); |
| 70 | return c->query(what, value); |
| 71 | } |
| 72 | |
| 73 | int SurfaceTextureClient::perform(ANativeWindow* window, int operation, ...) { |
| 74 | va_list args; |
| 75 | va_start(args, operation); |
| 76 | SurfaceTextureClient* c = getSelf(window); |
| 77 | return c->perform(operation, args); |
| 78 | } |
| 79 | |
| 80 | int SurfaceTextureClient::setSwapInterval(int interval) { |
| 81 | return INVALID_OPERATION; |
| 82 | } |
| 83 | |
| 84 | int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) { |
| 85 | Mutex::Autolock lock(mMutex); |
| 86 | int buf = -1; |
| 87 | status_t err = mSurfaceTexture->dequeueBuffer(&buf); |
| 88 | if (err < 0) { |
| 89 | return err; |
| 90 | } |
| 91 | sp<GraphicBuffer>& gbuf(mSlots[buf]); |
| 92 | if (gbuf == 0 || gbuf->getWidth() != mReqWidth || |
| 93 | gbuf->getHeight() != mReqHeight || |
| 94 | uint32_t(gbuf->getPixelFormat()) != mReqFormat || |
| 95 | (gbuf->getUsage() & mReqUsage) != mReqUsage) { |
| 96 | gbuf = mSurfaceTexture->requestBuffer(buf, mReqWidth, mReqHeight, |
| 97 | mReqFormat, mReqUsage); |
| 98 | if (gbuf == 0) { |
| 99 | return NO_MEMORY; |
| 100 | } |
| 101 | } |
| 102 | *buffer = gbuf.get(); |
| 103 | return OK; |
| 104 | } |
| 105 | |
| 106 | int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) { |
| 107 | Mutex::Autolock lock(mMutex); |
| 108 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 109 | if (mSlots[i].get() == buffer) { |
| 110 | mSurfaceTexture->cancelBuffer(i); |
| 111 | return OK; |
| 112 | } |
| 113 | } |
| 114 | return BAD_VALUE; |
| 115 | } |
| 116 | |
| 117 | int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) { |
| 118 | Mutex::Autolock lock(mMutex); |
| 119 | return OK; |
| 120 | } |
| 121 | |
| 122 | int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) { |
| 123 | Mutex::Autolock lock(mMutex); |
| 124 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 125 | if (mSlots[i].get() == GraphicBuffer::getSelf(buffer)) { |
| 126 | return mSurfaceTexture->queueBuffer(i); |
| 127 | } |
| 128 | } |
| 129 | LOGE("queueBuffer: unknown buffer queued"); |
| 130 | return BAD_VALUE; |
| 131 | } |
| 132 | |
| 133 | int SurfaceTextureClient::query(int what, int* value) { |
| 134 | Mutex::Autolock lock(mMutex); |
| 135 | // XXX: Implement this! |
| 136 | return INVALID_OPERATION; |
| 137 | } |
| 138 | |
| 139 | int SurfaceTextureClient::perform(int operation, va_list args) |
| 140 | { |
| 141 | int res = NO_ERROR; |
| 142 | switch (operation) { |
| 143 | case NATIVE_WINDOW_CONNECT: |
| 144 | res = dispatchConnect(args); |
| 145 | break; |
| 146 | case NATIVE_WINDOW_DISCONNECT: |
| 147 | res = dispatchDisconnect(args); |
| 148 | break; |
| 149 | case NATIVE_WINDOW_SET_USAGE: |
| 150 | res = dispatchSetUsage(args); |
| 151 | break; |
| 152 | case NATIVE_WINDOW_SET_CROP: |
| 153 | res = dispatchSetCrop(args); |
| 154 | break; |
| 155 | case NATIVE_WINDOW_SET_BUFFER_COUNT: |
| 156 | res = dispatchSetBufferCount(args); |
| 157 | break; |
| 158 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: |
| 159 | res = dispatchSetBuffersGeometry(args); |
| 160 | break; |
| 161 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: |
| 162 | res = dispatchSetBuffersTransform(args); |
| 163 | break; |
| 164 | default: |
| 165 | res = NAME_NOT_FOUND; |
| 166 | break; |
| 167 | } |
| 168 | return res; |
| 169 | } |
| 170 | |
| 171 | int SurfaceTextureClient::dispatchConnect(va_list args) { |
| 172 | int api = va_arg(args, int); |
| 173 | return connect(api); |
| 174 | } |
| 175 | |
| 176 | int SurfaceTextureClient::dispatchDisconnect(va_list args) { |
| 177 | int api = va_arg(args, int); |
| 178 | return disconnect(api); |
| 179 | } |
| 180 | |
| 181 | int SurfaceTextureClient::dispatchSetUsage(va_list args) { |
| 182 | int usage = va_arg(args, int); |
| 183 | return setUsage(usage); |
| 184 | } |
| 185 | |
| 186 | int SurfaceTextureClient::dispatchSetCrop(va_list args) { |
| 187 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); |
| 188 | return setCrop(reinterpret_cast<Rect const*>(rect)); |
| 189 | } |
| 190 | |
| 191 | int SurfaceTextureClient::dispatchSetBufferCount(va_list args) { |
| 192 | size_t bufferCount = va_arg(args, size_t); |
| 193 | return setBufferCount(bufferCount); |
| 194 | } |
| 195 | |
| 196 | int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) { |
| 197 | int w = va_arg(args, int); |
| 198 | int h = va_arg(args, int); |
| 199 | int f = va_arg(args, int); |
| 200 | return setBuffersGeometry(w, h, f); |
| 201 | } |
| 202 | |
| 203 | int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) { |
| 204 | int transform = va_arg(args, int); |
| 205 | return setBuffersTransform(transform); |
| 206 | } |
| 207 | |
| 208 | int SurfaceTextureClient::connect(int api) { |
| 209 | // XXX: Implement this! |
| 210 | return INVALID_OPERATION; |
| 211 | } |
| 212 | |
| 213 | int SurfaceTextureClient::disconnect(int api) { |
| 214 | // XXX: Implement this! |
| 215 | return INVALID_OPERATION; |
| 216 | } |
| 217 | |
| 218 | int SurfaceTextureClient::setUsage(uint32_t reqUsage) |
| 219 | { |
| 220 | Mutex::Autolock lock(mMutex); |
| 221 | mReqUsage = reqUsage; |
| 222 | return OK; |
| 223 | } |
| 224 | |
| 225 | int SurfaceTextureClient::setCrop(Rect const* rect) |
| 226 | { |
| 227 | Mutex::Autolock lock(mMutex); |
| 228 | |
| 229 | // empty/invalid rects are not allowed |
| 230 | if (rect->isEmpty()) |
| 231 | return BAD_VALUE; |
| 232 | |
| 233 | status_t err = mSurfaceTexture->setCrop(*rect); |
| 234 | LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", |
| 235 | strerror(-err)); |
| 236 | |
| 237 | return err; |
| 238 | } |
| 239 | |
| 240 | int SurfaceTextureClient::setBufferCount(int bufferCount) |
| 241 | { |
| 242 | Mutex::Autolock lock(mMutex); |
| 243 | |
| 244 | status_t err = mSurfaceTexture->setBufferCount(bufferCount); |
| 245 | LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s", |
| 246 | bufferCount, strerror(-err)); |
| 247 | |
| 248 | if (err == NO_ERROR) { |
| 249 | freeAllBuffers(); |
| 250 | } |
| 251 | |
| 252 | return err; |
| 253 | } |
| 254 | |
| 255 | int SurfaceTextureClient::setBuffersGeometry(int w, int h, int format) |
| 256 | { |
| 257 | Mutex::Autolock lock(mMutex); |
| 258 | |
| 259 | if (w<0 || h<0 || format<0) |
| 260 | return BAD_VALUE; |
| 261 | |
| 262 | if ((w && !h) || (!w && h)) |
| 263 | return BAD_VALUE; |
| 264 | |
| 265 | mReqWidth = w; |
| 266 | mReqHeight = h; |
| 267 | mReqFormat = format; |
| 268 | |
| 269 | return NO_ERROR; |
| 270 | } |
| 271 | |
| 272 | int SurfaceTextureClient::setBuffersTransform(int transform) |
| 273 | { |
| 274 | Mutex::Autolock lock(mMutex); |
| 275 | status_t err = mSurfaceTexture->setTransform(transform); |
| 276 | return err; |
| 277 | } |
| 278 | |
| 279 | void SurfaceTextureClient::freeAllBuffers() { |
| 280 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { |
| 281 | mSlots[i] = 0; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | }; // namespace android |