blob: b9b2310c7f55ab6ad0ffb1b5dd354874b160d37b [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):
Mathias Agopiane5a1bff2011-03-31 19:10:24 -070028 mSurfaceTexture(surfaceTexture), mAllocator(0), mReqWidth(0),
Mathias Agopian27cd07c2011-04-11 21:19:55 -070029 mReqHeight(0), mReqFormat(0), mReqUsage(0),
30 mTimestamp(NATIVE_WINDOW_TIMESTAMP_AUTO), mConnectedApi(0),
31 mQueryWidth(0), mQueryHeight(0), mQueryFormat(0),
32 mMutex() {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080033 // Initialize the ANativeWindow function pointers.
34 ANativeWindow::setSwapInterval = setSwapInterval;
35 ANativeWindow::dequeueBuffer = dequeueBuffer;
36 ANativeWindow::cancelBuffer = cancelBuffer;
37 ANativeWindow::lockBuffer = lockBuffer;
38 ANativeWindow::queueBuffer = queueBuffer;
39 ANativeWindow::query = query;
40 ANativeWindow::perform = perform;
Jamie Gennis83bac212011-02-02 15:31:47 -080041
Mathias Agopian402ff242011-05-02 19:51:12 -070042 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
43 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
44
Jamie Gennis83bac212011-02-02 15:31:47 -080045 // Get a reference to the allocator.
46 mAllocator = mSurfaceTexture->getAllocator();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080047}
48
Jamie Gennisf95a9f02011-03-14 15:08:53 -070049sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
50 return mSurfaceTexture;
51}
52
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080053int SurfaceTextureClient::setSwapInterval(ANativeWindow* window, int interval) {
54 SurfaceTextureClient* c = getSelf(window);
55 return c->setSwapInterval(interval);
56}
57
58int SurfaceTextureClient::dequeueBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070059 ANativeWindowBuffer** buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080060 SurfaceTextureClient* c = getSelf(window);
61 return c->dequeueBuffer(buffer);
62}
63
64int SurfaceTextureClient::cancelBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070065 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080066 SurfaceTextureClient* c = getSelf(window);
67 return c->cancelBuffer(buffer);
68}
69
70int SurfaceTextureClient::lockBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070071 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080072 SurfaceTextureClient* c = getSelf(window);
73 return c->lockBuffer(buffer);
74}
75
76int SurfaceTextureClient::queueBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070077 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080078 SurfaceTextureClient* c = getSelf(window);
79 return c->queueBuffer(buffer);
80}
81
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -070082int SurfaceTextureClient::query(const ANativeWindow* window,
83 int what, int* value) {
84 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080085 return c->query(what, value);
86}
87
88int SurfaceTextureClient::perform(ANativeWindow* window, int operation, ...) {
89 va_list args;
90 va_start(args, operation);
91 SurfaceTextureClient* c = getSelf(window);
92 return c->perform(operation, args);
93}
94
95int SurfaceTextureClient::setSwapInterval(int interval) {
Mathias Agopian402ff242011-05-02 19:51:12 -070096 // EGL specification states:
97 // interval is silently clamped to minimum and maximum implementation
98 // dependent values before being stored.
99 // Although we don't have to, we apply the same logic here.
100
101 if (interval < minSwapInterval)
102 interval = minSwapInterval;
103
104 if (interval > maxSwapInterval)
105 interval = maxSwapInterval;
106
107 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
108
109 return res;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800110}
111
112int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800113 LOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800114 Mutex::Autolock lock(mMutex);
115 int buf = -1;
Mathias Agopian402ff242011-05-02 19:51:12 -0700116 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopian0297dca2011-04-25 20:22:14 -0700117 mReqFormat, mReqUsage);
Mathias Agopian402ff242011-05-02 19:51:12 -0700118 if (result < 0) {
Mathias Agopian0297dca2011-04-25 20:22:14 -0700119 LOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennisa2187152011-05-19 13:33:00 -0700120 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
121 result);
Mathias Agopian402ff242011-05-02 19:51:12 -0700122 return result;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800123 }
124 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian402ff242011-05-02 19:51:12 -0700125 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
126 freeAllBuffers();
127 }
128
129 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Mathias Agopian0297dca2011-04-25 20:22:14 -0700130 gbuf = mSurfaceTexture->requestBuffer(buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800131 if (gbuf == 0) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800132 LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800133 return NO_MEMORY;
134 }
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700135 mQueryWidth = gbuf->width;
136 mQueryHeight = gbuf->height;
137 mQueryFormat = gbuf->format;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800138 }
139 *buffer = gbuf.get();
140 return OK;
141}
142
143int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800144 LOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800145 Mutex::Autolock lock(mMutex);
Jamie Gennisaff74052011-06-20 12:04:09 -0700146 int i = getSlotFromBufferLocked(buffer);
147 if (i < 0) {
148 return i;
149 }
150 mSurfaceTexture->cancelBuffer(i);
151 return OK;
152}
153
154int SurfaceTextureClient::getSlotFromBufferLocked(
155 android_native_buffer_t* buffer) const {
156 bool dumpedState = false;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800157 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennisaff74052011-06-20 12:04:09 -0700158 // XXX: Dump the slots whenever we hit a NULL entry while searching for
159 // a buffer.
160 if (mSlots[i] == NULL) {
161 if (!dumpedState) {
162 LOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
163 "looking for buffer %p", i, buffer->handle);
164 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
165 if (mSlots[j] == NULL) {
166 LOGD("getSlotFromBufferLocked: %02d: NULL", j);
167 } else {
168 LOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
169 }
170 }
171 dumpedState = true;
172 }
173 }
174
175 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
176 return i;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800177 }
178 }
Jamie Gennisaff74052011-06-20 12:04:09 -0700179 LOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800180 return BAD_VALUE;
181}
182
183int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800184 LOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800185 Mutex::Autolock lock(mMutex);
186 return OK;
187}
188
189int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800190 LOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800191 Mutex::Autolock lock(mMutex);
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800192 int64_t timestamp;
193 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
194 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
195 LOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
196 timestamp / 1000000.f);
197 } else {
198 timestamp = mTimestamp;
199 }
Jamie Gennisaff74052011-06-20 12:04:09 -0700200 int i = getSlotFromBufferLocked(buffer);
201 if (i < 0) {
202 return i;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800203 }
Jamie Gennisaff74052011-06-20 12:04:09 -0700204 mSurfaceTexture->queueBuffer(i, timestamp);
205 return OK;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800206}
207
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700208int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800209 LOGV("SurfaceTextureClient::query");
Jamie Gennis96dcc972011-02-27 14:10:20 -0800210 switch (what) {
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700211 case NATIVE_WINDOW_FORMAT:
212 if (mReqFormat) {
213 *value = mReqFormat;
214 return NO_ERROR;
215 }
216 break;
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800217 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Mathias Agopianed3894c2011-04-20 14:20:59 -0700218 // TODO: this is not needed anymore
Jamie Gennisd2acedf2011-03-08 12:18:54 -0800219 *value = 0;
220 return NO_ERROR;
Jamie Gennisc4ca7c52011-03-14 15:00:06 -0700221 case NATIVE_WINDOW_CONCRETE_TYPE:
Mathias Agopianed3894c2011-04-20 14:20:59 -0700222 // TODO: this is not needed anymore
Jamie Gennisc4ca7c52011-03-14 15:00:06 -0700223 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
224 return NO_ERROR;
Jamie Gennis96dcc972011-02-27 14:10:20 -0800225 }
Mathias Agopianed3894c2011-04-20 14:20:59 -0700226 return mSurfaceTexture->query(what, value);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800227}
228
229int SurfaceTextureClient::perform(int operation, va_list args)
230{
231 int res = NO_ERROR;
232 switch (operation) {
233 case NATIVE_WINDOW_CONNECT:
234 res = dispatchConnect(args);
235 break;
236 case NATIVE_WINDOW_DISCONNECT:
237 res = dispatchDisconnect(args);
238 break;
239 case NATIVE_WINDOW_SET_USAGE:
240 res = dispatchSetUsage(args);
241 break;
242 case NATIVE_WINDOW_SET_CROP:
243 res = dispatchSetCrop(args);
244 break;
245 case NATIVE_WINDOW_SET_BUFFER_COUNT:
246 res = dispatchSetBufferCount(args);
247 break;
248 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
249 res = dispatchSetBuffersGeometry(args);
250 break;
251 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
252 res = dispatchSetBuffersTransform(args);
253 break;
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800254 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
255 res = dispatchSetBuffersTimestamp(args);
256 break;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800257 default:
258 res = NAME_NOT_FOUND;
259 break;
260 }
261 return res;
262}
263
264int SurfaceTextureClient::dispatchConnect(va_list args) {
265 int api = va_arg(args, int);
266 return connect(api);
267}
268
269int SurfaceTextureClient::dispatchDisconnect(va_list args) {
270 int api = va_arg(args, int);
271 return disconnect(api);
272}
273
274int SurfaceTextureClient::dispatchSetUsage(va_list args) {
275 int usage = va_arg(args, int);
276 return setUsage(usage);
277}
278
279int SurfaceTextureClient::dispatchSetCrop(va_list args) {
280 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
281 return setCrop(reinterpret_cast<Rect const*>(rect));
282}
283
284int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
285 size_t bufferCount = va_arg(args, size_t);
286 return setBufferCount(bufferCount);
287}
288
289int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
290 int w = va_arg(args, int);
291 int h = va_arg(args, int);
292 int f = va_arg(args, int);
293 return setBuffersGeometry(w, h, f);
294}
295
296int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
297 int transform = va_arg(args, int);
298 return setBuffersTransform(transform);
299}
300
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800301int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
302 int64_t timestamp = va_arg(args, int64_t);
303 return setBuffersTimestamp(timestamp);
304}
305
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800306int SurfaceTextureClient::connect(int api) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800307 LOGV("SurfaceTextureClient::connect");
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700308 Mutex::Autolock lock(mMutex);
309 int err = NO_ERROR;
310 switch (api) {
311 case NATIVE_WINDOW_API_EGL:
312 if (mConnectedApi) {
313 err = -EINVAL;
314 } else {
315 mConnectedApi = api;
316 }
317 break;
318 default:
319 err = -EINVAL;
320 break;
321 }
322 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800323}
324
325int SurfaceTextureClient::disconnect(int api) {
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800326 LOGV("SurfaceTextureClient::disconnect");
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700327 Mutex::Autolock lock(mMutex);
328 int err = NO_ERROR;
329 switch (api) {
330 case NATIVE_WINDOW_API_EGL:
331 if (mConnectedApi == api) {
332 mConnectedApi = 0;
333 } else {
334 err = -EINVAL;
335 }
336 break;
337 default:
338 err = -EINVAL;
339 break;
340 }
341 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800342}
343
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700344int SurfaceTextureClient::getConnectedApi() const
345{
346 Mutex::Autolock lock(mMutex);
347 return mConnectedApi;
348}
349
350
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800351int SurfaceTextureClient::setUsage(uint32_t reqUsage)
352{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800353 LOGV("SurfaceTextureClient::setUsage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800354 Mutex::Autolock lock(mMutex);
355 mReqUsage = reqUsage;
356 return OK;
357}
358
359int SurfaceTextureClient::setCrop(Rect const* rect)
360{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800361 LOGV("SurfaceTextureClient::setCrop");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800362 Mutex::Autolock lock(mMutex);
363
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800364 Rect realRect;
365 if (rect == NULL || rect->isEmpty()) {
366 realRect = Rect(0, 0);
367 } else {
368 realRect = *rect;
369 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800370
371 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800372 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800373
374 return err;
375}
376
377int SurfaceTextureClient::setBufferCount(int bufferCount)
378{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800379 LOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800380 Mutex::Autolock lock(mMutex);
381
382 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
383 LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
384 bufferCount, strerror(-err));
385
386 if (err == NO_ERROR) {
387 freeAllBuffers();
388 }
389
390 return err;
391}
392
393int SurfaceTextureClient::setBuffersGeometry(int w, int h, int format)
394{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800395 LOGV("SurfaceTextureClient::setBuffersGeometry");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800396 Mutex::Autolock lock(mMutex);
397
398 if (w<0 || h<0 || format<0)
399 return BAD_VALUE;
400
401 if ((w && !h) || (!w && h))
402 return BAD_VALUE;
403
404 mReqWidth = w;
405 mReqHeight = h;
406 mReqFormat = format;
407
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800408 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
409 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
410
411 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800412}
413
414int SurfaceTextureClient::setBuffersTransform(int transform)
415{
Jamie Gennise8d0e8a2011-01-12 20:22:41 -0800416 LOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800417 Mutex::Autolock lock(mMutex);
418 status_t err = mSurfaceTexture->setTransform(transform);
419 return err;
420}
421
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800422int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
423{
424 LOGV("SurfaceTextureClient::setBuffersTimestamp");
425 Mutex::Autolock lock(mMutex);
426 mTimestamp = timestamp;
427 return NO_ERROR;
428}
429
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800430void SurfaceTextureClient::freeAllBuffers() {
431 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
432 mSlots[i] = 0;
433 }
434}
435
436}; // namespace android