blob: ec6da4324d30efcadacbf73ea4e528c91cbbb12a [file] [log] [blame]
Jamie Gennis8ba32fa2010-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 Gennise5366c52011-01-12 20:22:41 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-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 Agopiana5c75c02011-03-31 19:10:24 -070028 mSurfaceTexture(surfaceTexture), mAllocator(0), mReqWidth(0),
Mathias Agopian7a042bf2011-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 Gennis8ba32fa2010-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 Gennis1b20cde2011-02-02 15:31:47 -080041
42 // Get a reference to the allocator.
43 mAllocator = mSurfaceTexture->getAllocator();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080044}
45
Jamie Gennisbae774e2011-03-14 15:08:53 -070046sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
47 return mSurfaceTexture;
48}
49
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080050int SurfaceTextureClient::setSwapInterval(ANativeWindow* window, int interval) {
51 SurfaceTextureClient* c = getSelf(window);
52 return c->setSwapInterval(interval);
53}
54
55int SurfaceTextureClient::dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070056 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057 SurfaceTextureClient* c = getSelf(window);
58 return c->dequeueBuffer(buffer);
59}
60
61int SurfaceTextureClient::cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070062 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080063 SurfaceTextureClient* c = getSelf(window);
64 return c->cancelBuffer(buffer);
65}
66
67int SurfaceTextureClient::lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070068 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080069 SurfaceTextureClient* c = getSelf(window);
70 return c->lockBuffer(buffer);
71}
72
73int SurfaceTextureClient::queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070074 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080075 SurfaceTextureClient* c = getSelf(window);
76 return c->queueBuffer(buffer);
77}
78
Iliyan Malchev41abd672011-04-14 16:54:38 -070079int SurfaceTextureClient::query(const ANativeWindow* window,
80 int what, int* value) {
81 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080082 return c->query(what, value);
83}
84
85int SurfaceTextureClient::perform(ANativeWindow* window, int operation, ...) {
86 va_list args;
87 va_start(args, operation);
88 SurfaceTextureClient* c = getSelf(window);
89 return c->perform(operation, args);
90}
91
92int SurfaceTextureClient::setSwapInterval(int interval) {
93 return INVALID_OPERATION;
94}
95
96int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -080097 LOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080098 Mutex::Autolock lock(mMutex);
99 int buf = -1;
100 status_t err = mSurfaceTexture->dequeueBuffer(&buf);
101 if (err < 0) {
Jamie Gennis74917222011-01-28 12:03:52 -0800102 LOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer failed: %d", err);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800103 return err;
104 }
105 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700106 if (err == ISurfaceTexture::BUFFER_NEEDS_REALLOCATION ||
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700107 gbuf == 0 ||
108 (mReqWidth && gbuf->getWidth() != mReqWidth) ||
109 (mReqHeight && gbuf->getHeight() != mReqHeight) ||
110 (mReqFormat && uint32_t(gbuf->getPixelFormat()) != mReqFormat) ||
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800111 (gbuf->getUsage() & mReqUsage) != mReqUsage) {
112 gbuf = mSurfaceTexture->requestBuffer(buf, mReqWidth, mReqHeight,
113 mReqFormat, mReqUsage);
114 if (gbuf == 0) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800115 LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800116 return NO_MEMORY;
117 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700118 mQueryWidth = gbuf->width;
119 mQueryHeight = gbuf->height;
120 mQueryFormat = gbuf->format;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800121 }
122 *buffer = gbuf.get();
123 return OK;
124}
125
126int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800127 LOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800128 Mutex::Autolock lock(mMutex);
129 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis73e8b9e2011-01-15 13:05:24 -0800130 if (mSlots[i]->handle == buffer->handle) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131 mSurfaceTexture->cancelBuffer(i);
132 return OK;
133 }
134 }
135 return BAD_VALUE;
136}
137
138int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800139 LOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800140 Mutex::Autolock lock(mMutex);
141 return OK;
142}
143
144int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800145 LOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800146 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800147 int64_t timestamp;
148 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
149 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
150 LOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
151 timestamp / 1000000.f);
152 } else {
153 timestamp = mTimestamp;
154 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800155 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis73e8b9e2011-01-15 13:05:24 -0800156 if (mSlots[i]->handle == buffer->handle) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800157 return mSurfaceTexture->queueBuffer(i, timestamp);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800158 }
159 }
160 LOGE("queueBuffer: unknown buffer queued");
161 return BAD_VALUE;
162}
163
Iliyan Malchev41abd672011-04-14 16:54:38 -0700164int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennise5366c52011-01-12 20:22:41 -0800165 LOGV("SurfaceTextureClient::query");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800166 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800167 switch (what) {
168 case NATIVE_WINDOW_WIDTH:
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700169 *value = mQueryWidth ? mQueryWidth : mReqWidth;
170 return NO_ERROR;
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800171 case NATIVE_WINDOW_HEIGHT:
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700172 *value = mQueryHeight ? mQueryHeight : mReqHeight;
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800173 return NO_ERROR;
174 case NATIVE_WINDOW_FORMAT:
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700175 *value = mQueryFormat ? mQueryFormat : mReqFormat;
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800176 return NO_ERROR;
177 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
178 *value = MIN_UNDEQUEUED_BUFFERS;
179 return NO_ERROR;
Jamie Gennis134f0422011-03-08 12:18:54 -0800180 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
181 // SurfaceTextureClient currently never queues frames to SurfaceFlinger.
182 *value = 0;
183 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700184 case NATIVE_WINDOW_CONCRETE_TYPE:
185 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
186 return NO_ERROR;
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800187 }
188 return BAD_VALUE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189}
190
191int SurfaceTextureClient::perform(int operation, va_list args)
192{
193 int res = NO_ERROR;
194 switch (operation) {
195 case NATIVE_WINDOW_CONNECT:
196 res = dispatchConnect(args);
197 break;
198 case NATIVE_WINDOW_DISCONNECT:
199 res = dispatchDisconnect(args);
200 break;
201 case NATIVE_WINDOW_SET_USAGE:
202 res = dispatchSetUsage(args);
203 break;
204 case NATIVE_WINDOW_SET_CROP:
205 res = dispatchSetCrop(args);
206 break;
207 case NATIVE_WINDOW_SET_BUFFER_COUNT:
208 res = dispatchSetBufferCount(args);
209 break;
210 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
211 res = dispatchSetBuffersGeometry(args);
212 break;
213 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
214 res = dispatchSetBuffersTransform(args);
215 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800216 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
217 res = dispatchSetBuffersTimestamp(args);
218 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 default:
220 res = NAME_NOT_FOUND;
221 break;
222 }
223 return res;
224}
225
226int SurfaceTextureClient::dispatchConnect(va_list args) {
227 int api = va_arg(args, int);
228 return connect(api);
229}
230
231int SurfaceTextureClient::dispatchDisconnect(va_list args) {
232 int api = va_arg(args, int);
233 return disconnect(api);
234}
235
236int SurfaceTextureClient::dispatchSetUsage(va_list args) {
237 int usage = va_arg(args, int);
238 return setUsage(usage);
239}
240
241int SurfaceTextureClient::dispatchSetCrop(va_list args) {
242 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
243 return setCrop(reinterpret_cast<Rect const*>(rect));
244}
245
246int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
247 size_t bufferCount = va_arg(args, size_t);
248 return setBufferCount(bufferCount);
249}
250
251int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
252 int w = va_arg(args, int);
253 int h = va_arg(args, int);
254 int f = va_arg(args, int);
255 return setBuffersGeometry(w, h, f);
256}
257
258int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
259 int transform = va_arg(args, int);
260 return setBuffersTransform(transform);
261}
262
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800263int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
264 int64_t timestamp = va_arg(args, int64_t);
265 return setBuffersTimestamp(timestamp);
266}
267
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800268int SurfaceTextureClient::connect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800269 LOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700270 Mutex::Autolock lock(mMutex);
271 int err = NO_ERROR;
272 switch (api) {
273 case NATIVE_WINDOW_API_EGL:
274 if (mConnectedApi) {
275 err = -EINVAL;
276 } else {
277 mConnectedApi = api;
278 }
279 break;
280 default:
281 err = -EINVAL;
282 break;
283 }
284 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800285}
286
287int SurfaceTextureClient::disconnect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800288 LOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700289 Mutex::Autolock lock(mMutex);
290 int err = NO_ERROR;
291 switch (api) {
292 case NATIVE_WINDOW_API_EGL:
293 if (mConnectedApi == api) {
294 mConnectedApi = 0;
295 } else {
296 err = -EINVAL;
297 }
298 break;
299 default:
300 err = -EINVAL;
301 break;
302 }
303 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800304}
305
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700306int SurfaceTextureClient::getConnectedApi() const
307{
308 Mutex::Autolock lock(mMutex);
309 return mConnectedApi;
310}
311
312
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800313int SurfaceTextureClient::setUsage(uint32_t reqUsage)
314{
Jamie Gennise5366c52011-01-12 20:22:41 -0800315 LOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800316 Mutex::Autolock lock(mMutex);
317 mReqUsage = reqUsage;
318 return OK;
319}
320
321int SurfaceTextureClient::setCrop(Rect const* rect)
322{
Jamie Gennise5366c52011-01-12 20:22:41 -0800323 LOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800324 Mutex::Autolock lock(mMutex);
325
Jamie Gennis68f91272011-01-28 18:21:54 -0800326 Rect realRect;
327 if (rect == NULL || rect->isEmpty()) {
328 realRect = Rect(0, 0);
329 } else {
330 realRect = *rect;
331 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800332
333 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis68f91272011-01-28 18:21:54 -0800334 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800335
336 return err;
337}
338
339int SurfaceTextureClient::setBufferCount(int bufferCount)
340{
Jamie Gennise5366c52011-01-12 20:22:41 -0800341 LOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800342 Mutex::Autolock lock(mMutex);
343
344 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
345 LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
346 bufferCount, strerror(-err));
347
348 if (err == NO_ERROR) {
349 freeAllBuffers();
350 }
351
352 return err;
353}
354
355int SurfaceTextureClient::setBuffersGeometry(int w, int h, int format)
356{
Jamie Gennise5366c52011-01-12 20:22:41 -0800357 LOGV("SurfaceTextureClient::setBuffersGeometry");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800358 Mutex::Autolock lock(mMutex);
359
360 if (w<0 || h<0 || format<0)
361 return BAD_VALUE;
362
363 if ((w && !h) || (!w && h))
364 return BAD_VALUE;
365
366 mReqWidth = w;
367 mReqHeight = h;
368 mReqFormat = format;
369
Jamie Gennis68f91272011-01-28 18:21:54 -0800370 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
371 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
372
373 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800374}
375
376int SurfaceTextureClient::setBuffersTransform(int transform)
377{
Jamie Gennise5366c52011-01-12 20:22:41 -0800378 LOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800379 Mutex::Autolock lock(mMutex);
380 status_t err = mSurfaceTexture->setTransform(transform);
381 return err;
382}
383
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800384int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
385{
386 LOGV("SurfaceTextureClient::setBuffersTimestamp");
387 Mutex::Autolock lock(mMutex);
388 mTimestamp = timestamp;
389 return NO_ERROR;
390}
391
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800392void SurfaceTextureClient::freeAllBuffers() {
393 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
394 mSlots[i] = 0;
395 }
396}
397
398}; // namespace android