blob: d5b7c89f8f956b54aaccedece8f63193b1482d02 [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;
55 mQueryWidth = 0;
56 mQueryHeight = 0;
57 mQueryFormat = 0;
58 mConnectedToCpu = false;
59}
60
61void SurfaceTextureClient::setISurfaceTexture(
62 const sp<ISurfaceTexture>& surfaceTexture)
63{
64 mSurfaceTexture = surfaceTexture;
65
Jamie Gennis1b20cde2011-02-02 15:31:47 -080066 // Get a reference to the allocator.
67 mAllocator = mSurfaceTexture->getAllocator();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080068}
69
Jamie Gennisbae774e2011-03-14 15:08:53 -070070sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
71 return mSurfaceTexture;
72}
73
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070074int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080075 SurfaceTextureClient* c = getSelf(window);
76 return c->setSwapInterval(interval);
77}
78
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070079int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070080 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080081 SurfaceTextureClient* c = getSelf(window);
82 return c->dequeueBuffer(buffer);
83}
84
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070085int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070086 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080087 SurfaceTextureClient* c = getSelf(window);
88 return c->cancelBuffer(buffer);
89}
90
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070091int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070092 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080093 SurfaceTextureClient* c = getSelf(window);
94 return c->lockBuffer(buffer);
95}
96
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070097int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070098 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080099 SurfaceTextureClient* c = getSelf(window);
100 return c->queueBuffer(buffer);
101}
102
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700103int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev41abd672011-04-14 16:54:38 -0700104 int what, int* value) {
105 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800106 return c->query(what, value);
107}
108
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700109int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800110 va_list args;
111 va_start(args, operation);
112 SurfaceTextureClient* c = getSelf(window);
113 return c->perform(operation, args);
114}
115
116int SurfaceTextureClient::setSwapInterval(int interval) {
Mathias Agopian80727112011-05-02 19:51:12 -0700117 // EGL specification states:
118 // interval is silently clamped to minimum and maximum implementation
119 // dependent values before being stored.
120 // Although we don't have to, we apply the same logic here.
121
122 if (interval < minSwapInterval)
123 interval = minSwapInterval;
124
125 if (interval > maxSwapInterval)
126 interval = maxSwapInterval;
127
128 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
129
130 return res;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131}
132
133int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800134 LOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135 Mutex::Autolock lock(mMutex);
136 int buf = -1;
Mathias Agopian80727112011-05-02 19:51:12 -0700137 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopianc04f1532011-04-25 20:22:14 -0700138 mReqFormat, mReqUsage);
Mathias Agopian80727112011-05-02 19:51:12 -0700139 if (result < 0) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700140 LOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700141 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
142 result);
Mathias Agopian80727112011-05-02 19:51:12 -0700143 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800144 }
145 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian80727112011-05-02 19:51:12 -0700146 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
147 freeAllBuffers();
148 }
149
150 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700151 gbuf = mSurfaceTexture->requestBuffer(buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800152 if (gbuf == 0) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800153 LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800154 return NO_MEMORY;
155 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700156 mQueryWidth = gbuf->width;
157 mQueryHeight = gbuf->height;
158 mQueryFormat = gbuf->format;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800159 }
160 *buffer = gbuf.get();
161 return OK;
162}
163
164int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800165 LOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800166 Mutex::Autolock lock(mMutex);
Jamie Gennis1c441402011-06-20 12:04:09 -0700167 int i = getSlotFromBufferLocked(buffer);
168 if (i < 0) {
169 return i;
170 }
171 mSurfaceTexture->cancelBuffer(i);
172 return OK;
173}
174
175int SurfaceTextureClient::getSlotFromBufferLocked(
176 android_native_buffer_t* buffer) const {
177 bool dumpedState = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis1c441402011-06-20 12:04:09 -0700179 // XXX: Dump the slots whenever we hit a NULL entry while searching for
180 // a buffer.
181 if (mSlots[i] == NULL) {
182 if (!dumpedState) {
183 LOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
184 "looking for buffer %p", i, buffer->handle);
185 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
186 if (mSlots[j] == NULL) {
187 LOGD("getSlotFromBufferLocked: %02d: NULL", j);
188 } else {
189 LOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
190 }
191 }
192 dumpedState = true;
193 }
194 }
195
196 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
197 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800198 }
199 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700200 LOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800201 return BAD_VALUE;
202}
203
204int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800205 LOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800206 Mutex::Autolock lock(mMutex);
207 return OK;
208}
209
210int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800211 LOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800212 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800213 int64_t timestamp;
214 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
215 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
216 LOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
217 timestamp / 1000000.f);
218 } else {
219 timestamp = mTimestamp;
220 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700221 int i = getSlotFromBufferLocked(buffer);
222 if (i < 0) {
223 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800224 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700225 mSurfaceTexture->queueBuffer(i, timestamp);
226 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800227}
228
Iliyan Malchev41abd672011-04-14 16:54:38 -0700229int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennise5366c52011-01-12 20:22:41 -0800230 LOGV("SurfaceTextureClient::query");
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800231 switch (what) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700232 case NATIVE_WINDOW_FORMAT:
233 if (mReqFormat) {
234 *value = mReqFormat;
235 return NO_ERROR;
236 }
237 break;
Jamie Gennis134f0422011-03-08 12:18:54 -0800238 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700239 // TODO: this is not needed anymore
Jamie Gennis134f0422011-03-08 12:18:54 -0800240 *value = 0;
241 return NO_ERROR;
Jamie Gennis391bbe22011-03-14 15:00:06 -0700242 case NATIVE_WINDOW_CONCRETE_TYPE:
243 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
244 return NO_ERROR;
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800245 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700246 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800247}
248
249int SurfaceTextureClient::perform(int operation, va_list args)
250{
251 int res = NO_ERROR;
252 switch (operation) {
253 case NATIVE_WINDOW_CONNECT:
254 res = dispatchConnect(args);
255 break;
256 case NATIVE_WINDOW_DISCONNECT:
257 res = dispatchDisconnect(args);
258 break;
259 case NATIVE_WINDOW_SET_USAGE:
260 res = dispatchSetUsage(args);
261 break;
262 case NATIVE_WINDOW_SET_CROP:
263 res = dispatchSetCrop(args);
264 break;
265 case NATIVE_WINDOW_SET_BUFFER_COUNT:
266 res = dispatchSetBufferCount(args);
267 break;
268 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
269 res = dispatchSetBuffersGeometry(args);
270 break;
271 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
272 res = dispatchSetBuffersTransform(args);
273 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800274 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
275 res = dispatchSetBuffersTimestamp(args);
276 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700277 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
278 res = dispatchSetBuffersDimensions(args);
279 break;
280 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
281 res = dispatchSetBuffersFormat(args);
282 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700283 case NATIVE_WINDOW_LOCK:
284 res = dispatchLock(args);
285 break;
286 case NATIVE_WINDOW_UNLOCK_AND_POST:
287 res = dispatchUnlockAndPost(args);
288 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800289 default:
290 res = NAME_NOT_FOUND;
291 break;
292 }
293 return res;
294}
295
296int SurfaceTextureClient::dispatchConnect(va_list args) {
297 int api = va_arg(args, int);
298 return connect(api);
299}
300
301int SurfaceTextureClient::dispatchDisconnect(va_list args) {
302 int api = va_arg(args, int);
303 return disconnect(api);
304}
305
306int SurfaceTextureClient::dispatchSetUsage(va_list args) {
307 int usage = va_arg(args, int);
308 return setUsage(usage);
309}
310
311int SurfaceTextureClient::dispatchSetCrop(va_list args) {
312 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
313 return setCrop(reinterpret_cast<Rect const*>(rect));
314}
315
316int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
317 size_t bufferCount = va_arg(args, size_t);
318 return setBufferCount(bufferCount);
319}
320
321int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
322 int w = va_arg(args, int);
323 int h = va_arg(args, int);
324 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700325 int err = setBuffersDimensions(w, h);
326 if (err != 0) {
327 return err;
328 }
329 return setBuffersFormat(f);
330}
331
332int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
333 int w = va_arg(args, int);
334 int h = va_arg(args, int);
335 return setBuffersDimensions(w, h);
336}
337
338int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
339 int f = va_arg(args, int);
340 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800341}
342
343int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
344 int transform = va_arg(args, int);
345 return setBuffersTransform(transform);
346}
347
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800348int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
349 int64_t timestamp = va_arg(args, int64_t);
350 return setBuffersTimestamp(timestamp);
351}
352
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700353int SurfaceTextureClient::dispatchLock(va_list args) {
354 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
355 ARect* inOutDirtyBounds = va_arg(args, ARect*);
356 return lock(outBuffer, inOutDirtyBounds);
357}
358
359int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
360 return unlockAndPost();
361}
362
363
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800364int SurfaceTextureClient::connect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800365 LOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700366 Mutex::Autolock lock(mMutex);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700367 int err = mSurfaceTexture->connect(api);
368 if (!err && api == NATIVE_WINDOW_API_CPU) {
369 mConnectedToCpu = true;
370 }
371 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800372}
373
374int SurfaceTextureClient::disconnect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800375 LOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700376 Mutex::Autolock lock(mMutex);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700377 int err = mSurfaceTexture->disconnect(api);
378 if (!err && api == NATIVE_WINDOW_API_CPU) {
379 mConnectedToCpu = false;
380 }
381 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800382}
383
384int SurfaceTextureClient::setUsage(uint32_t reqUsage)
385{
Jamie Gennise5366c52011-01-12 20:22:41 -0800386 LOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800387 Mutex::Autolock lock(mMutex);
388 mReqUsage = reqUsage;
389 return OK;
390}
391
392int SurfaceTextureClient::setCrop(Rect const* rect)
393{
Jamie Gennise5366c52011-01-12 20:22:41 -0800394 LOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800395 Mutex::Autolock lock(mMutex);
396
Jamie Gennis68f91272011-01-28 18:21:54 -0800397 Rect realRect;
398 if (rect == NULL || rect->isEmpty()) {
399 realRect = Rect(0, 0);
400 } else {
401 realRect = *rect;
402 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800403
404 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis68f91272011-01-28 18:21:54 -0800405 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800406
407 return err;
408}
409
410int SurfaceTextureClient::setBufferCount(int bufferCount)
411{
Jamie Gennise5366c52011-01-12 20:22:41 -0800412 LOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800413 Mutex::Autolock lock(mMutex);
414
415 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
416 LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
417 bufferCount, strerror(-err));
418
419 if (err == NO_ERROR) {
420 freeAllBuffers();
421 }
422
423 return err;
424}
425
Jamie Gennisbee205f2011-07-01 13:12:07 -0700426int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800427{
Jamie Gennisbee205f2011-07-01 13:12:07 -0700428 LOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800429 Mutex::Autolock lock(mMutex);
430
Jamie Gennisbee205f2011-07-01 13:12:07 -0700431 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800432 return BAD_VALUE;
433
434 if ((w && !h) || (!w && h))
435 return BAD_VALUE;
436
437 mReqWidth = w;
438 mReqHeight = h;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800439
Jamie Gennis68f91272011-01-28 18:21:54 -0800440 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
441 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
442
443 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800444}
445
Jamie Gennisbee205f2011-07-01 13:12:07 -0700446int SurfaceTextureClient::setBuffersFormat(int format)
447{
448 LOGV("SurfaceTextureClient::setBuffersFormat");
449 Mutex::Autolock lock(mMutex);
450
451 if (format<0)
452 return BAD_VALUE;
453
454 mReqFormat = format;
455
456 return NO_ERROR;
457}
458
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800459int SurfaceTextureClient::setBuffersTransform(int transform)
460{
Jamie Gennise5366c52011-01-12 20:22:41 -0800461 LOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800462 Mutex::Autolock lock(mMutex);
463 status_t err = mSurfaceTexture->setTransform(transform);
464 return err;
465}
466
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800467int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
468{
469 LOGV("SurfaceTextureClient::setBuffersTimestamp");
470 Mutex::Autolock lock(mMutex);
471 mTimestamp = timestamp;
472 return NO_ERROR;
473}
474
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800475void SurfaceTextureClient::freeAllBuffers() {
476 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
477 mSlots[i] = 0;
478 }
479}
480
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700481// ----------------------------------------------------------------------
482// the lock/unlock APIs must be used from the same thread
483
484static status_t copyBlt(
485 const sp<GraphicBuffer>& dst,
486 const sp<GraphicBuffer>& src,
487 const Region& reg)
488{
489 // src and dst with, height and format must be identical. no verification
490 // is done here.
491 status_t err;
492 uint8_t const * src_bits = NULL;
493 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
494 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
495
496 uint8_t* dst_bits = NULL;
497 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
498 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
499
500 Region::const_iterator head(reg.begin());
501 Region::const_iterator tail(reg.end());
502 if (head != tail && src_bits && dst_bits) {
503 const size_t bpp = bytesPerPixel(src->format);
504 const size_t dbpr = dst->stride * bpp;
505 const size_t sbpr = src->stride * bpp;
506
507 while (head != tail) {
508 const Rect& r(*head++);
509 ssize_t h = r.height();
510 if (h <= 0) continue;
511 size_t size = r.width() * bpp;
512 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
513 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
514 if (dbpr==sbpr && size==sbpr) {
515 size *= h;
516 h = 1;
517 }
518 do {
519 memcpy(d, s, size);
520 d += dbpr;
521 s += sbpr;
522 } while (--h > 0);
523 }
524 }
525
526 if (src_bits)
527 src->unlock();
528
529 if (dst_bits)
530 dst->unlock();
531
532 return err;
533}
534
535// ----------------------------------------------------------------------------
536
537status_t SurfaceTextureClient::lock(
538 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
539{
540 if (mLockedBuffer != 0) {
541 LOGE("Surface::lock failed, already locked");
542 return INVALID_OPERATION;
543 }
544
545 if (!mConnectedToCpu) {
546 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
547 if (err) {
548 return err;
549 }
550 // we're intending to do software rendering from this point
551 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
552 }
553
554 ANativeWindowBuffer* out;
555 status_t err = dequeueBuffer(&out);
556 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
557 if (err == NO_ERROR) {
558 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
559 err = lockBuffer(backBuffer.get());
560 LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
561 backBuffer->handle, strerror(-err));
562 if (err == NO_ERROR) {
563 const Rect bounds(backBuffer->width, backBuffer->height);
564
565 Region newDirtyRegion;
566 if (inOutDirtyBounds) {
567 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
568 newDirtyRegion.andSelf(bounds);
569 } else {
570 newDirtyRegion.set(bounds);
571 }
572
573 // figure out if we can copy the frontbuffer back
574 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
575 const bool canCopyBack = (frontBuffer != 0 &&
576 backBuffer->width == frontBuffer->width &&
577 backBuffer->height == frontBuffer->height &&
578 backBuffer->format == frontBuffer->format);
579
580 if (canCopyBack) {
581 // copy the area that is invalid and not repainted this round
582 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
583 if (!copyback.isEmpty())
584 copyBlt(backBuffer, frontBuffer, copyback);
585 } else {
586 // if we can't copy-back anything, modify the user's dirty
587 // region to make sure they redraw the whole buffer
588 newDirtyRegion.set(bounds);
589 }
590
591 // keep track of the are of the buffer that is "clean"
592 // (ie: that will be redrawn)
593 mOldDirtyRegion = newDirtyRegion;
594
595 if (inOutDirtyBounds) {
596 *inOutDirtyBounds = newDirtyRegion.getBounds();
597 }
598
599 void* vaddr;
600 status_t res = backBuffer->lock(
601 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
602 newDirtyRegion.bounds(), &vaddr);
603
604 LOGW_IF(res, "failed locking buffer (handle = %p)",
605 backBuffer->handle);
606
607 mLockedBuffer = backBuffer;
608 outBuffer->width = backBuffer->width;
609 outBuffer->height = backBuffer->height;
610 outBuffer->stride = backBuffer->stride;
611 outBuffer->format = backBuffer->format;
612 outBuffer->bits = vaddr;
613 }
614 }
615 return err;
616}
617
618status_t SurfaceTextureClient::unlockAndPost()
619{
620 if (mLockedBuffer == 0) {
621 LOGE("Surface::unlockAndPost failed, no locked buffer");
622 return INVALID_OPERATION;
623 }
624
625 status_t err = mLockedBuffer->unlock();
626 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
627
628 err = queueBuffer(mLockedBuffer.get());
629 LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
630 mLockedBuffer->handle, strerror(-err));
631
632 mPostedBuffer = mLockedBuffer;
633 mLockedBuffer = 0;
634 return err;
635}
636
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800637}; // namespace android