blob: 111141f4a584c0fa9bd3d432c5017f848883e9c0 [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;
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800159 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
160 // SurfaceTextureClient currently never queues frames to SurfaceFlinger.
161 *value = 0;
162 return NO_ERROR;
Jamie Gennisc4ca7c52011-03-14 15:00:06 -0700163 case NATIVE_WINDOW_CONCRETE_TYPE:
164 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
165 return NO_ERROR;
Jamie Gennis96dcc972011-02-27 14:10:20 -0800166 }
167 return BAD_VALUE;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800168}
169
170int 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
202int SurfaceTextureClient::dispatchConnect(va_list args) {
203 int api = va_arg(args, int);
204 return connect(api);
205}
206
207int SurfaceTextureClient::dispatchDisconnect(va_list args) {
208 int api = va_arg(args, int);
209 return disconnect(api);
210}
211
212int SurfaceTextureClient::dispatchSetUsage(va_list args) {
213 int usage = va_arg(args, int);
214 return setUsage(usage);
215}
216
217int 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
222int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
223 size_t bufferCount = va_arg(args, size_t);
224 return setBufferCount(bufferCount);
225}
226
227int 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
234int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
235 int transform = va_arg(args, int);
236 return setBuffersTransform(transform);
237}
238
239int SurfaceTextureClient::connect(int api) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800240 LOGV("SurfaceTextureClient::connect");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800241 // XXX: Implement this!
242 return INVALID_OPERATION;
243}
244
245int SurfaceTextureClient::disconnect(int api) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800246 LOGV("SurfaceTextureClient::disconnect");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800247 // XXX: Implement this!
248 return INVALID_OPERATION;
249}
250
251int SurfaceTextureClient::setUsage(uint32_t reqUsage)
252{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800253 LOGV("SurfaceTextureClient::setUsage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800254 Mutex::Autolock lock(mMutex);
255 mReqUsage = reqUsage;
256 return OK;
257}
258
259int SurfaceTextureClient::setCrop(Rect const* rect)
260{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800261 LOGV("SurfaceTextureClient::setCrop");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800262 Mutex::Autolock lock(mMutex);
263
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800264 Rect realRect;
265 if (rect == NULL || rect->isEmpty()) {
266 realRect = Rect(0, 0);
267 } else {
268 realRect = *rect;
269 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800270
271 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800272 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800273
274 return err;
275}
276
277int SurfaceTextureClient::setBufferCount(int bufferCount)
278{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800279 LOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800280 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
293int SurfaceTextureClient::setBuffersGeometry(int w, int h, int format)
294{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800295 LOGV("SurfaceTextureClient::setBuffersGeometry");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800296 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 Gennis2ece4cd2011-01-28 18:21:54 -0800308 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
309 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
310
311 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800312}
313
314int SurfaceTextureClient::setBuffersTransform(int transform)
315{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800316 LOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800317 Mutex::Autolock lock(mMutex);
318 status_t err = mSurfaceTexture->setTransform(transform);
319 return err;
320}
321
322void SurfaceTextureClient::freeAllBuffers() {
323 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
324 mSlots[i] = 0;
325 }
326}
327
328}; // namespace android