blob: 43b330c79e7723373f4fcbe37bc863b64628980f [file] [log] [blame]
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001/*
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 Gennise8d0e8a2011-01-12 20:22:41 -080018//#define LOG_NDEBUG 0
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080019
20#include <gui/SurfaceTextureClient.h>
21
22#include <utils/Log.h>
23
24namespace android {
25
26SurfaceTextureClient::SurfaceTextureClient(
27 const sp<ISurfaceTexture>& surfaceTexture):
Jamie Gennis83bac212011-02-02 15:31:47 -080028 mSurfaceTexture(surfaceTexture), mAllocator(0), mReqWidth(1),
29 mReqHeight(1), mReqFormat(DEFAULT_FORMAT), mReqUsage(0), mMutex() {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080030 // 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 Gennis83bac212011-02-02 15:31:47 -080038
39 // Get a reference to the allocator.
40 mAllocator = mSurfaceTexture->getAllocator();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080041}
42
43int SurfaceTextureClient::setSwapInterval(ANativeWindow* window, int interval) {
44 SurfaceTextureClient* c = getSelf(window);
45 return c->setSwapInterval(interval);
46}
47
48int SurfaceTextureClient::dequeueBuffer(ANativeWindow* window,
49 android_native_buffer_t** buffer) {
50 SurfaceTextureClient* c = getSelf(window);
51 return c->dequeueBuffer(buffer);
52}
53
54int SurfaceTextureClient::cancelBuffer(ANativeWindow* window,
55 android_native_buffer_t* buffer) {
56 SurfaceTextureClient* c = getSelf(window);
57 return c->cancelBuffer(buffer);
58}
59
60int SurfaceTextureClient::lockBuffer(ANativeWindow* window,
61 android_native_buffer_t* buffer) {
62 SurfaceTextureClient* c = getSelf(window);
63 return c->lockBuffer(buffer);
64}
65
66int SurfaceTextureClient::queueBuffer(ANativeWindow* window,
67 android_native_buffer_t* buffer) {
68 SurfaceTextureClient* c = getSelf(window);
69 return c->queueBuffer(buffer);
70}
71
72int SurfaceTextureClient::query(ANativeWindow* window, int what, int* value) {
73 SurfaceTextureClient* c = getSelf(window);
74 return c->query(what, value);
75}
76
77int 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
84int SurfaceTextureClient::setSwapInterval(int interval) {
85 return INVALID_OPERATION;
86}
87
88int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -080089 LOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080090 Mutex::Autolock lock(mMutex);
91 int buf = -1;
92 status_t err = mSurfaceTexture->dequeueBuffer(&buf);
93 if (err < 0) {
Jamie Gennis69130622011-01-28 12:03:52 -080094 LOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer failed: %d", err);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080095 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 Gennise8d0e8a2011-01-12 20:22:41 -0800105 LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800106 return NO_MEMORY;
107 }
108 }
109 *buffer = gbuf.get();
110 return OK;
111}
112
113int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800114 LOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800115 Mutex::Autolock lock(mMutex);
116 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis4559ea42011-01-15 13:05:24 -0800117 if (mSlots[i]->handle == buffer->handle) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800118 mSurfaceTexture->cancelBuffer(i);
119 return OK;
120 }
121 }
122 return BAD_VALUE;
123}
124
125int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800126 LOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800127 Mutex::Autolock lock(mMutex);
128 return OK;
129}
130
131int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800132 LOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800133 Mutex::Autolock lock(mMutex);
134 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis4559ea42011-01-15 13:05:24 -0800135 if (mSlots[i]->handle == buffer->handle) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800136 return mSurfaceTexture->queueBuffer(i);
137 }
138 }
139 LOGE("queueBuffer: unknown buffer queued");
140 return BAD_VALUE;
141}
142
143int SurfaceTextureClient::query(int what, int* value) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800144 LOGV("SurfaceTextureClient::query");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800145 Mutex::Autolock lock(mMutex);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800146 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 Gennis68e4a7a2010-12-20 11:27:26 -0800161}
162
163int 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
195int SurfaceTextureClient::dispatchConnect(va_list args) {
196 int api = va_arg(args, int);
197 return connect(api);
198}
199
200int SurfaceTextureClient::dispatchDisconnect(va_list args) {
201 int api = va_arg(args, int);
202 return disconnect(api);
203}
204
205int SurfaceTextureClient::dispatchSetUsage(va_list args) {
206 int usage = va_arg(args, int);
207 return setUsage(usage);
208}
209
210int 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
215int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
216 size_t bufferCount = va_arg(args, size_t);
217 return setBufferCount(bufferCount);
218}
219
220int 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
227int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
228 int transform = va_arg(args, int);
229 return setBuffersTransform(transform);
230}
231
232int SurfaceTextureClient::connect(int api) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800233 LOGV("SurfaceTextureClient::connect");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800234 // XXX: Implement this!
235 return INVALID_OPERATION;
236}
237
238int SurfaceTextureClient::disconnect(int api) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800239 LOGV("SurfaceTextureClient::disconnect");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800240 // XXX: Implement this!
241 return INVALID_OPERATION;
242}
243
244int SurfaceTextureClient::setUsage(uint32_t reqUsage)
245{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800246 LOGV("SurfaceTextureClient::setUsage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800247 Mutex::Autolock lock(mMutex);
248 mReqUsage = reqUsage;
249 return OK;
250}
251
252int SurfaceTextureClient::setCrop(Rect const* rect)
253{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800254 LOGV("SurfaceTextureClient::setCrop");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800255 Mutex::Autolock lock(mMutex);
256
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800257 Rect realRect;
258 if (rect == NULL || rect->isEmpty()) {
259 realRect = Rect(0, 0);
260 } else {
261 realRect = *rect;
262 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800263
264 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800265 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800266
267 return err;
268}
269
270int SurfaceTextureClient::setBufferCount(int bufferCount)
271{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800272 LOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800273 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
286int SurfaceTextureClient::setBuffersGeometry(int w, int h, int format)
287{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800288 LOGV("SurfaceTextureClient::setBuffersGeometry");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800289 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 Gennis2ece4cd2011-01-28 18:21:54 -0800301 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
302 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
303
304 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800305}
306
307int SurfaceTextureClient::setBuffersTransform(int transform)
308{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800309 LOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800310 Mutex::Autolock lock(mMutex);
311 status_t err = mSurfaceTexture->setTransform(transform);
312 return err;
313}
314
315void SurfaceTextureClient::freeAllBuffers() {
316 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
317 mSlots[i] = 0;
318 }
319}
320
321}; // namespace android