blob: e6837ea3607e6c2f31c2ce39867423df5b6c91c7 [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(
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070027 const sp<ISurfaceTexture>& surfaceTexture)
28{
29 SurfaceTextureClient::init();
30 SurfaceTextureClient::setISurfaceTexture(surfaceTexture);
31}
32
33SurfaceTextureClient::SurfaceTextureClient() {
34 SurfaceTextureClient::init();
35}
36
37void SurfaceTextureClient::init() {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038 // Initialize the ANativeWindow function pointers.
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070039 ANativeWindow::setSwapInterval = hook_setSwapInterval;
40 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
41 ANativeWindow::cancelBuffer = hook_cancelBuffer;
42 ANativeWindow::lockBuffer = hook_lockBuffer;
43 ANativeWindow::queueBuffer = hook_queueBuffer;
44 ANativeWindow::query = hook_query;
45 ANativeWindow::perform = hook_perform;
Jamie Gennis1b20cde2011-02-02 15:31:47 -080046
Mathias Agopian80727112011-05-02 19:51:12 -070047 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
48 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
49
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070050 mReqWidth = 0;
51 mReqHeight = 0;
52 mReqFormat = 0;
53 mReqUsage = 0;
54 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -070055 mDefaultWidth = 0;
56 mDefaultHeight = 0;
57 mTransformHint = 0;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070058 mConnectedToCpu = false;
59}
60
61void SurfaceTextureClient::setISurfaceTexture(
62 const sp<ISurfaceTexture>& surfaceTexture)
63{
64 mSurfaceTexture = surfaceTexture;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080065}
66
Jamie Gennisbae774e2011-03-14 15:08:53 -070067sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
68 return mSurfaceTexture;
69}
70
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070071int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080072 SurfaceTextureClient* c = getSelf(window);
73 return c->setSwapInterval(interval);
74}
75
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070076int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070077 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080078 SurfaceTextureClient* c = getSelf(window);
79 return c->dequeueBuffer(buffer);
80}
81
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070082int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070083 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080084 SurfaceTextureClient* c = getSelf(window);
85 return c->cancelBuffer(buffer);
86}
87
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070088int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070089 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080090 SurfaceTextureClient* c = getSelf(window);
91 return c->lockBuffer(buffer);
92}
93
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070094int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070095 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080096 SurfaceTextureClient* c = getSelf(window);
97 return c->queueBuffer(buffer);
98}
99
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700100int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev41abd672011-04-14 16:54:38 -0700101 int what, int* value) {
102 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800103 return c->query(what, value);
104}
105
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700106int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800107 va_list args;
108 va_start(args, operation);
109 SurfaceTextureClient* c = getSelf(window);
110 return c->perform(operation, args);
111}
112
113int SurfaceTextureClient::setSwapInterval(int interval) {
Mathias Agopian80727112011-05-02 19:51:12 -0700114 // EGL specification states:
115 // interval is silently clamped to minimum and maximum implementation
116 // dependent values before being stored.
117 // Although we don't have to, we apply the same logic here.
118
119 if (interval < minSwapInterval)
120 interval = minSwapInterval;
121
122 if (interval > maxSwapInterval)
123 interval = maxSwapInterval;
124
125 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
126
127 return res;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800128}
129
130int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800131 LOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800132 Mutex::Autolock lock(mMutex);
133 int buf = -1;
Mathias Agopian80727112011-05-02 19:51:12 -0700134 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopianc04f1532011-04-25 20:22:14 -0700135 mReqFormat, mReqUsage);
Mathias Agopian80727112011-05-02 19:51:12 -0700136 if (result < 0) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700137 LOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700138 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
139 result);
Mathias Agopian80727112011-05-02 19:51:12 -0700140 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800141 }
142 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian80727112011-05-02 19:51:12 -0700143 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
144 freeAllBuffers();
145 }
146
147 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700148 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
149 if (result != NO_ERROR) {
150 LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
151 result);
152 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800153 }
154 }
155 *buffer = gbuf.get();
156 return OK;
157}
158
159int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800160 LOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800161 Mutex::Autolock lock(mMutex);
Jamie Gennis1c441402011-06-20 12:04:09 -0700162 int i = getSlotFromBufferLocked(buffer);
163 if (i < 0) {
164 return i;
165 }
166 mSurfaceTexture->cancelBuffer(i);
167 return OK;
168}
169
170int SurfaceTextureClient::getSlotFromBufferLocked(
171 android_native_buffer_t* buffer) const {
172 bool dumpedState = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800173 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis1c441402011-06-20 12:04:09 -0700174 // XXX: Dump the slots whenever we hit a NULL entry while searching for
175 // a buffer.
176 if (mSlots[i] == NULL) {
177 if (!dumpedState) {
178 LOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
179 "looking for buffer %p", i, buffer->handle);
180 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
181 if (mSlots[j] == NULL) {
182 LOGD("getSlotFromBufferLocked: %02d: NULL", j);
183 } else {
184 LOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
185 }
186 }
187 dumpedState = true;
188 }
189 }
190
191 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
192 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800193 }
194 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700195 LOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800196 return BAD_VALUE;
197}
198
199int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800200 LOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800201 Mutex::Autolock lock(mMutex);
202 return OK;
203}
204
205int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800206 LOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800207 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800208 int64_t timestamp;
209 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
210 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
211 LOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
212 timestamp / 1000000.f);
213 } else {
214 timestamp = mTimestamp;
215 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700216 int i = getSlotFromBufferLocked(buffer);
217 if (i < 0) {
218 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700220 mSurfaceTexture->queueBuffer(i, timestamp,
221 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Jamie Gennis1c441402011-06-20 12:04:09 -0700222 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800223}
224
Iliyan Malchev41abd672011-04-14 16:54:38 -0700225int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennise5366c52011-01-12 20:22:41 -0800226 LOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700227 { // scope for the lock
228 Mutex::Autolock lock(mMutex);
229 switch (what) {
230 case NATIVE_WINDOW_FORMAT:
231 if (mReqFormat) {
232 *value = mReqFormat;
233 return NO_ERROR;
234 }
235 break;
236 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
237 *value = 0;
238 return NO_ERROR;
239 case NATIVE_WINDOW_CONCRETE_TYPE:
240 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
241 return NO_ERROR;
242 case NATIVE_WINDOW_DEFAULT_WIDTH:
243 *value = mDefaultWidth;
244 return NO_ERROR;
245 case NATIVE_WINDOW_DEFAULT_HEIGHT:
246 *value = mDefaultHeight;
247 return NO_ERROR;
248 case NATIVE_WINDOW_TRANSFORM_HINT:
249 *value = mTransformHint;
250 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700251 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800252 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700253 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800254}
255
256int SurfaceTextureClient::perform(int operation, va_list args)
257{
258 int res = NO_ERROR;
259 switch (operation) {
260 case NATIVE_WINDOW_CONNECT:
261 res = dispatchConnect(args);
262 break;
263 case NATIVE_WINDOW_DISCONNECT:
264 res = dispatchDisconnect(args);
265 break;
266 case NATIVE_WINDOW_SET_USAGE:
267 res = dispatchSetUsage(args);
268 break;
269 case NATIVE_WINDOW_SET_CROP:
270 res = dispatchSetCrop(args);
271 break;
272 case NATIVE_WINDOW_SET_BUFFER_COUNT:
273 res = dispatchSetBufferCount(args);
274 break;
275 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
276 res = dispatchSetBuffersGeometry(args);
277 break;
278 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
279 res = dispatchSetBuffersTransform(args);
280 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800281 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
282 res = dispatchSetBuffersTimestamp(args);
283 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700284 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
285 res = dispatchSetBuffersDimensions(args);
286 break;
287 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
288 res = dispatchSetBuffersFormat(args);
289 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700290 case NATIVE_WINDOW_LOCK:
291 res = dispatchLock(args);
292 break;
293 case NATIVE_WINDOW_UNLOCK_AND_POST:
294 res = dispatchUnlockAndPost(args);
295 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700296 case NATIVE_WINDOW_SET_SCALING_MODE:
297 res = dispatchSetScalingMode(args);
298 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800299 default:
300 res = NAME_NOT_FOUND;
301 break;
302 }
303 return res;
304}
305
306int SurfaceTextureClient::dispatchConnect(va_list args) {
307 int api = va_arg(args, int);
308 return connect(api);
309}
310
311int SurfaceTextureClient::dispatchDisconnect(va_list args) {
312 int api = va_arg(args, int);
313 return disconnect(api);
314}
315
316int SurfaceTextureClient::dispatchSetUsage(va_list args) {
317 int usage = va_arg(args, int);
318 return setUsage(usage);
319}
320
321int SurfaceTextureClient::dispatchSetCrop(va_list args) {
322 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
323 return setCrop(reinterpret_cast<Rect const*>(rect));
324}
325
326int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
327 size_t bufferCount = va_arg(args, size_t);
328 return setBufferCount(bufferCount);
329}
330
331int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
332 int w = va_arg(args, int);
333 int h = va_arg(args, int);
334 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700335 int err = setBuffersDimensions(w, h);
336 if (err != 0) {
337 return err;
338 }
339 return setBuffersFormat(f);
340}
341
342int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
343 int w = va_arg(args, int);
344 int h = va_arg(args, int);
345 return setBuffersDimensions(w, h);
346}
347
348int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
349 int f = va_arg(args, int);
350 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800351}
352
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700353int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
354 int m = va_arg(args, int);
355 return setScalingMode(m);
356}
357
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800358int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
359 int transform = va_arg(args, int);
360 return setBuffersTransform(transform);
361}
362
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800363int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
364 int64_t timestamp = va_arg(args, int64_t);
365 return setBuffersTimestamp(timestamp);
366}
367
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700368int SurfaceTextureClient::dispatchLock(va_list args) {
369 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
370 ARect* inOutDirtyBounds = va_arg(args, ARect*);
371 return lock(outBuffer, inOutDirtyBounds);
372}
373
374int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
375 return unlockAndPost();
376}
377
378
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800379int SurfaceTextureClient::connect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800380 LOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700381 Mutex::Autolock lock(mMutex);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700382 int err = mSurfaceTexture->connect(api);
383 if (!err && api == NATIVE_WINDOW_API_CPU) {
384 mConnectedToCpu = true;
385 }
386 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800387}
388
389int SurfaceTextureClient::disconnect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800390 LOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700391 Mutex::Autolock lock(mMutex);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700392 int err = mSurfaceTexture->disconnect(api);
393 if (!err && api == NATIVE_WINDOW_API_CPU) {
394 mConnectedToCpu = false;
395 }
396 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800397}
398
399int SurfaceTextureClient::setUsage(uint32_t reqUsage)
400{
Jamie Gennise5366c52011-01-12 20:22:41 -0800401 LOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800402 Mutex::Autolock lock(mMutex);
403 mReqUsage = reqUsage;
404 return OK;
405}
406
407int SurfaceTextureClient::setCrop(Rect const* rect)
408{
Jamie Gennise5366c52011-01-12 20:22:41 -0800409 LOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800410 Mutex::Autolock lock(mMutex);
411
Jamie Gennis68f91272011-01-28 18:21:54 -0800412 Rect realRect;
413 if (rect == NULL || rect->isEmpty()) {
414 realRect = Rect(0, 0);
415 } else {
416 realRect = *rect;
417 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800418
419 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis68f91272011-01-28 18:21:54 -0800420 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800421
422 return err;
423}
424
425int SurfaceTextureClient::setBufferCount(int bufferCount)
426{
Jamie Gennise5366c52011-01-12 20:22:41 -0800427 LOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800428 Mutex::Autolock lock(mMutex);
429
430 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
431 LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
432 bufferCount, strerror(-err));
433
434 if (err == NO_ERROR) {
435 freeAllBuffers();
436 }
437
438 return err;
439}
440
Jamie Gennisbee205f2011-07-01 13:12:07 -0700441int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800442{
Jamie Gennisbee205f2011-07-01 13:12:07 -0700443 LOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800444 Mutex::Autolock lock(mMutex);
445
Jamie Gennisbee205f2011-07-01 13:12:07 -0700446 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800447 return BAD_VALUE;
448
449 if ((w && !h) || (!w && h))
450 return BAD_VALUE;
451
452 mReqWidth = w;
453 mReqHeight = h;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800454
Jamie Gennis68f91272011-01-28 18:21:54 -0800455 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
456 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
457
458 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800459}
460
Jamie Gennisbee205f2011-07-01 13:12:07 -0700461int SurfaceTextureClient::setBuffersFormat(int format)
462{
463 LOGV("SurfaceTextureClient::setBuffersFormat");
464 Mutex::Autolock lock(mMutex);
465
466 if (format<0)
467 return BAD_VALUE;
468
469 mReqFormat = format;
470
471 return NO_ERROR;
472}
473
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700474int SurfaceTextureClient::setScalingMode(int mode)
475{
Mathias Agopian933389f2011-07-18 16:15:08 -0700476 LOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700477 Mutex::Autolock lock(mMutex);
478 // mode is validated on the server
479 status_t err = mSurfaceTexture->setScalingMode(mode);
480 LOGE_IF(err, "ISurfaceTexture::setScalingMode(%d) returned %s",
481 mode, strerror(-err));
482
483 return err;
484}
485
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800486int SurfaceTextureClient::setBuffersTransform(int transform)
487{
Jamie Gennise5366c52011-01-12 20:22:41 -0800488 LOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800489 Mutex::Autolock lock(mMutex);
490 status_t err = mSurfaceTexture->setTransform(transform);
491 return err;
492}
493
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800494int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
495{
496 LOGV("SurfaceTextureClient::setBuffersTimestamp");
497 Mutex::Autolock lock(mMutex);
498 mTimestamp = timestamp;
499 return NO_ERROR;
500}
501
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800502void SurfaceTextureClient::freeAllBuffers() {
503 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
504 mSlots[i] = 0;
505 }
506}
507
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700508// ----------------------------------------------------------------------
509// the lock/unlock APIs must be used from the same thread
510
511static status_t copyBlt(
512 const sp<GraphicBuffer>& dst,
513 const sp<GraphicBuffer>& src,
514 const Region& reg)
515{
516 // src and dst with, height and format must be identical. no verification
517 // is done here.
518 status_t err;
519 uint8_t const * src_bits = NULL;
520 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
521 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
522
523 uint8_t* dst_bits = NULL;
524 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
525 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
526
527 Region::const_iterator head(reg.begin());
528 Region::const_iterator tail(reg.end());
529 if (head != tail && src_bits && dst_bits) {
530 const size_t bpp = bytesPerPixel(src->format);
531 const size_t dbpr = dst->stride * bpp;
532 const size_t sbpr = src->stride * bpp;
533
534 while (head != tail) {
535 const Rect& r(*head++);
536 ssize_t h = r.height();
537 if (h <= 0) continue;
538 size_t size = r.width() * bpp;
539 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
540 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
541 if (dbpr==sbpr && size==sbpr) {
542 size *= h;
543 h = 1;
544 }
545 do {
546 memcpy(d, s, size);
547 d += dbpr;
548 s += sbpr;
549 } while (--h > 0);
550 }
551 }
552
553 if (src_bits)
554 src->unlock();
555
556 if (dst_bits)
557 dst->unlock();
558
559 return err;
560}
561
562// ----------------------------------------------------------------------------
563
564status_t SurfaceTextureClient::lock(
565 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
566{
567 if (mLockedBuffer != 0) {
568 LOGE("Surface::lock failed, already locked");
569 return INVALID_OPERATION;
570 }
571
572 if (!mConnectedToCpu) {
573 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
574 if (err) {
575 return err;
576 }
577 // we're intending to do software rendering from this point
578 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
579 }
580
581 ANativeWindowBuffer* out;
582 status_t err = dequeueBuffer(&out);
583 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
584 if (err == NO_ERROR) {
585 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
586 err = lockBuffer(backBuffer.get());
587 LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
588 backBuffer->handle, strerror(-err));
589 if (err == NO_ERROR) {
590 const Rect bounds(backBuffer->width, backBuffer->height);
591
592 Region newDirtyRegion;
593 if (inOutDirtyBounds) {
594 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
595 newDirtyRegion.andSelf(bounds);
596 } else {
597 newDirtyRegion.set(bounds);
598 }
599
600 // figure out if we can copy the frontbuffer back
601 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
602 const bool canCopyBack = (frontBuffer != 0 &&
603 backBuffer->width == frontBuffer->width &&
604 backBuffer->height == frontBuffer->height &&
605 backBuffer->format == frontBuffer->format);
606
607 if (canCopyBack) {
608 // copy the area that is invalid and not repainted this round
609 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
610 if (!copyback.isEmpty())
611 copyBlt(backBuffer, frontBuffer, copyback);
612 } else {
613 // if we can't copy-back anything, modify the user's dirty
614 // region to make sure they redraw the whole buffer
615 newDirtyRegion.set(bounds);
616 }
617
618 // keep track of the are of the buffer that is "clean"
619 // (ie: that will be redrawn)
620 mOldDirtyRegion = newDirtyRegion;
621
622 if (inOutDirtyBounds) {
623 *inOutDirtyBounds = newDirtyRegion.getBounds();
624 }
625
626 void* vaddr;
627 status_t res = backBuffer->lock(
628 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
629 newDirtyRegion.bounds(), &vaddr);
630
631 LOGW_IF(res, "failed locking buffer (handle = %p)",
632 backBuffer->handle);
633
634 mLockedBuffer = backBuffer;
635 outBuffer->width = backBuffer->width;
636 outBuffer->height = backBuffer->height;
637 outBuffer->stride = backBuffer->stride;
638 outBuffer->format = backBuffer->format;
639 outBuffer->bits = vaddr;
640 }
641 }
642 return err;
643}
644
645status_t SurfaceTextureClient::unlockAndPost()
646{
647 if (mLockedBuffer == 0) {
648 LOGE("Surface::unlockAndPost failed, no locked buffer");
649 return INVALID_OPERATION;
650 }
651
652 status_t err = mLockedBuffer->unlock();
653 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
654
655 err = queueBuffer(mLockedBuffer.get());
656 LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
657 mLockedBuffer->handle, strerror(-err));
658
659 mPostedBuffer = mLockedBuffer;
660 mLockedBuffer = 0;
661 return err;
662}
663
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800664}; // namespace android