blob: 1dc6cd2cf751951ad13d4177317eed49c642d982 [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 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700225 mSurfaceTexture->queueBuffer(i, timestamp,
226 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Jamie Gennis1c441402011-06-20 12:04:09 -0700227 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800228}
229
Iliyan Malchev41abd672011-04-14 16:54:38 -0700230int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennise5366c52011-01-12 20:22:41 -0800231 LOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700232 { // scope for the lock
233 Mutex::Autolock lock(mMutex);
234 switch (what) {
235 case NATIVE_WINDOW_FORMAT:
236 if (mReqFormat) {
237 *value = mReqFormat;
238 return NO_ERROR;
239 }
240 break;
241 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
242 *value = 0;
243 return NO_ERROR;
244 case NATIVE_WINDOW_CONCRETE_TYPE:
245 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
246 return NO_ERROR;
247 case NATIVE_WINDOW_DEFAULT_WIDTH:
248 *value = mDefaultWidth;
249 return NO_ERROR;
250 case NATIVE_WINDOW_DEFAULT_HEIGHT:
251 *value = mDefaultHeight;
252 return NO_ERROR;
253 case NATIVE_WINDOW_TRANSFORM_HINT:
254 *value = mTransformHint;
255 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700256 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800257 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700258 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800259}
260
261int SurfaceTextureClient::perform(int operation, va_list args)
262{
263 int res = NO_ERROR;
264 switch (operation) {
265 case NATIVE_WINDOW_CONNECT:
266 res = dispatchConnect(args);
267 break;
268 case NATIVE_WINDOW_DISCONNECT:
269 res = dispatchDisconnect(args);
270 break;
271 case NATIVE_WINDOW_SET_USAGE:
272 res = dispatchSetUsage(args);
273 break;
274 case NATIVE_WINDOW_SET_CROP:
275 res = dispatchSetCrop(args);
276 break;
277 case NATIVE_WINDOW_SET_BUFFER_COUNT:
278 res = dispatchSetBufferCount(args);
279 break;
280 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
281 res = dispatchSetBuffersGeometry(args);
282 break;
283 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
284 res = dispatchSetBuffersTransform(args);
285 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800286 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
287 res = dispatchSetBuffersTimestamp(args);
288 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700289 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
290 res = dispatchSetBuffersDimensions(args);
291 break;
292 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
293 res = dispatchSetBuffersFormat(args);
294 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700295 case NATIVE_WINDOW_LOCK:
296 res = dispatchLock(args);
297 break;
298 case NATIVE_WINDOW_UNLOCK_AND_POST:
299 res = dispatchUnlockAndPost(args);
300 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700301 case NATIVE_WINDOW_SET_SCALING_MODE:
302 res = dispatchSetScalingMode(args);
303 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800304 default:
305 res = NAME_NOT_FOUND;
306 break;
307 }
308 return res;
309}
310
311int SurfaceTextureClient::dispatchConnect(va_list args) {
312 int api = va_arg(args, int);
313 return connect(api);
314}
315
316int SurfaceTextureClient::dispatchDisconnect(va_list args) {
317 int api = va_arg(args, int);
318 return disconnect(api);
319}
320
321int SurfaceTextureClient::dispatchSetUsage(va_list args) {
322 int usage = va_arg(args, int);
323 return setUsage(usage);
324}
325
326int SurfaceTextureClient::dispatchSetCrop(va_list args) {
327 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
328 return setCrop(reinterpret_cast<Rect const*>(rect));
329}
330
331int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
332 size_t bufferCount = va_arg(args, size_t);
333 return setBufferCount(bufferCount);
334}
335
336int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
337 int w = va_arg(args, int);
338 int h = va_arg(args, int);
339 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700340 int err = setBuffersDimensions(w, h);
341 if (err != 0) {
342 return err;
343 }
344 return setBuffersFormat(f);
345}
346
347int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
348 int w = va_arg(args, int);
349 int h = va_arg(args, int);
350 return setBuffersDimensions(w, h);
351}
352
353int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
354 int f = va_arg(args, int);
355 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800356}
357
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700358int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
359 int m = va_arg(args, int);
360 return setScalingMode(m);
361}
362
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800363int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
364 int transform = va_arg(args, int);
365 return setBuffersTransform(transform);
366}
367
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800368int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
369 int64_t timestamp = va_arg(args, int64_t);
370 return setBuffersTimestamp(timestamp);
371}
372
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700373int SurfaceTextureClient::dispatchLock(va_list args) {
374 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
375 ARect* inOutDirtyBounds = va_arg(args, ARect*);
376 return lock(outBuffer, inOutDirtyBounds);
377}
378
379int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
380 return unlockAndPost();
381}
382
383
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800384int SurfaceTextureClient::connect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800385 LOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700386 Mutex::Autolock lock(mMutex);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700387 int err = mSurfaceTexture->connect(api);
388 if (!err && api == NATIVE_WINDOW_API_CPU) {
389 mConnectedToCpu = true;
390 }
391 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800392}
393
394int SurfaceTextureClient::disconnect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800395 LOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700396 Mutex::Autolock lock(mMutex);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700397 int err = mSurfaceTexture->disconnect(api);
398 if (!err && api == NATIVE_WINDOW_API_CPU) {
399 mConnectedToCpu = false;
400 }
401 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800402}
403
404int SurfaceTextureClient::setUsage(uint32_t reqUsage)
405{
Jamie Gennise5366c52011-01-12 20:22:41 -0800406 LOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800407 Mutex::Autolock lock(mMutex);
408 mReqUsage = reqUsage;
409 return OK;
410}
411
412int SurfaceTextureClient::setCrop(Rect const* rect)
413{
Jamie Gennise5366c52011-01-12 20:22:41 -0800414 LOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800415 Mutex::Autolock lock(mMutex);
416
Jamie Gennis68f91272011-01-28 18:21:54 -0800417 Rect realRect;
418 if (rect == NULL || rect->isEmpty()) {
419 realRect = Rect(0, 0);
420 } else {
421 realRect = *rect;
422 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800423
424 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis68f91272011-01-28 18:21:54 -0800425 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800426
427 return err;
428}
429
430int SurfaceTextureClient::setBufferCount(int bufferCount)
431{
Jamie Gennise5366c52011-01-12 20:22:41 -0800432 LOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800433 Mutex::Autolock lock(mMutex);
434
435 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
436 LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
437 bufferCount, strerror(-err));
438
439 if (err == NO_ERROR) {
440 freeAllBuffers();
441 }
442
443 return err;
444}
445
Jamie Gennisbee205f2011-07-01 13:12:07 -0700446int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800447{
Jamie Gennisbee205f2011-07-01 13:12:07 -0700448 LOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800449 Mutex::Autolock lock(mMutex);
450
Jamie Gennisbee205f2011-07-01 13:12:07 -0700451 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800452 return BAD_VALUE;
453
454 if ((w && !h) || (!w && h))
455 return BAD_VALUE;
456
457 mReqWidth = w;
458 mReqHeight = h;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800459
Jamie Gennis68f91272011-01-28 18:21:54 -0800460 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
461 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
462
463 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800464}
465
Jamie Gennisbee205f2011-07-01 13:12:07 -0700466int SurfaceTextureClient::setBuffersFormat(int format)
467{
468 LOGV("SurfaceTextureClient::setBuffersFormat");
469 Mutex::Autolock lock(mMutex);
470
471 if (format<0)
472 return BAD_VALUE;
473
474 mReqFormat = format;
475
476 return NO_ERROR;
477}
478
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700479int SurfaceTextureClient::setScalingMode(int mode)
480{
Mathias Agopian933389f2011-07-18 16:15:08 -0700481 LOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700482 Mutex::Autolock lock(mMutex);
483 // mode is validated on the server
484 status_t err = mSurfaceTexture->setScalingMode(mode);
485 LOGE_IF(err, "ISurfaceTexture::setScalingMode(%d) returned %s",
486 mode, strerror(-err));
487
488 return err;
489}
490
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800491int SurfaceTextureClient::setBuffersTransform(int transform)
492{
Jamie Gennise5366c52011-01-12 20:22:41 -0800493 LOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800494 Mutex::Autolock lock(mMutex);
495 status_t err = mSurfaceTexture->setTransform(transform);
496 return err;
497}
498
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800499int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
500{
501 LOGV("SurfaceTextureClient::setBuffersTimestamp");
502 Mutex::Autolock lock(mMutex);
503 mTimestamp = timestamp;
504 return NO_ERROR;
505}
506
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800507void SurfaceTextureClient::freeAllBuffers() {
508 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
509 mSlots[i] = 0;
510 }
511}
512
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700513// ----------------------------------------------------------------------
514// the lock/unlock APIs must be used from the same thread
515
516static status_t copyBlt(
517 const sp<GraphicBuffer>& dst,
518 const sp<GraphicBuffer>& src,
519 const Region& reg)
520{
521 // src and dst with, height and format must be identical. no verification
522 // is done here.
523 status_t err;
524 uint8_t const * src_bits = NULL;
525 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
526 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
527
528 uint8_t* dst_bits = NULL;
529 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
530 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
531
532 Region::const_iterator head(reg.begin());
533 Region::const_iterator tail(reg.end());
534 if (head != tail && src_bits && dst_bits) {
535 const size_t bpp = bytesPerPixel(src->format);
536 const size_t dbpr = dst->stride * bpp;
537 const size_t sbpr = src->stride * bpp;
538
539 while (head != tail) {
540 const Rect& r(*head++);
541 ssize_t h = r.height();
542 if (h <= 0) continue;
543 size_t size = r.width() * bpp;
544 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
545 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
546 if (dbpr==sbpr && size==sbpr) {
547 size *= h;
548 h = 1;
549 }
550 do {
551 memcpy(d, s, size);
552 d += dbpr;
553 s += sbpr;
554 } while (--h > 0);
555 }
556 }
557
558 if (src_bits)
559 src->unlock();
560
561 if (dst_bits)
562 dst->unlock();
563
564 return err;
565}
566
567// ----------------------------------------------------------------------------
568
569status_t SurfaceTextureClient::lock(
570 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
571{
572 if (mLockedBuffer != 0) {
573 LOGE("Surface::lock failed, already locked");
574 return INVALID_OPERATION;
575 }
576
577 if (!mConnectedToCpu) {
578 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
579 if (err) {
580 return err;
581 }
582 // we're intending to do software rendering from this point
583 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
584 }
585
586 ANativeWindowBuffer* out;
587 status_t err = dequeueBuffer(&out);
588 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
589 if (err == NO_ERROR) {
590 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
591 err = lockBuffer(backBuffer.get());
592 LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
593 backBuffer->handle, strerror(-err));
594 if (err == NO_ERROR) {
595 const Rect bounds(backBuffer->width, backBuffer->height);
596
597 Region newDirtyRegion;
598 if (inOutDirtyBounds) {
599 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
600 newDirtyRegion.andSelf(bounds);
601 } else {
602 newDirtyRegion.set(bounds);
603 }
604
605 // figure out if we can copy the frontbuffer back
606 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
607 const bool canCopyBack = (frontBuffer != 0 &&
608 backBuffer->width == frontBuffer->width &&
609 backBuffer->height == frontBuffer->height &&
610 backBuffer->format == frontBuffer->format);
611
612 if (canCopyBack) {
613 // copy the area that is invalid and not repainted this round
614 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
615 if (!copyback.isEmpty())
616 copyBlt(backBuffer, frontBuffer, copyback);
617 } else {
618 // if we can't copy-back anything, modify the user's dirty
619 // region to make sure they redraw the whole buffer
620 newDirtyRegion.set(bounds);
621 }
622
623 // keep track of the are of the buffer that is "clean"
624 // (ie: that will be redrawn)
625 mOldDirtyRegion = newDirtyRegion;
626
627 if (inOutDirtyBounds) {
628 *inOutDirtyBounds = newDirtyRegion.getBounds();
629 }
630
631 void* vaddr;
632 status_t res = backBuffer->lock(
633 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
634 newDirtyRegion.bounds(), &vaddr);
635
636 LOGW_IF(res, "failed locking buffer (handle = %p)",
637 backBuffer->handle);
638
639 mLockedBuffer = backBuffer;
640 outBuffer->width = backBuffer->width;
641 outBuffer->height = backBuffer->height;
642 outBuffer->stride = backBuffer->stride;
643 outBuffer->format = backBuffer->format;
644 outBuffer->bits = vaddr;
645 }
646 }
647 return err;
648}
649
650status_t SurfaceTextureClient::unlockAndPost()
651{
652 if (mLockedBuffer == 0) {
653 LOGE("Surface::unlockAndPost failed, no locked buffer");
654 return INVALID_OPERATION;
655 }
656
657 status_t err = mLockedBuffer->unlock();
658 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
659
660 err = queueBuffer(mLockedBuffer.get());
661 LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
662 mLockedBuffer->handle, strerror(-err));
663
664 mPostedBuffer = mLockedBuffer;
665 mLockedBuffer = 0;
666 return err;
667}
668
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800669}; // namespace android