blob: d1037defc416879fcfbaae684d15bc554c72aa3d [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:
Mathias Agopian81a63352011-07-29 17:55:48 -0700261 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800262 break;
263 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700264 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800265 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;
Mathias Agopian81a63352011-07-29 17:55:48 -0700299 case NATIVE_WINDOW_API_CONNECT:
300 res = dispatchConnect(args);
301 break;
302 case NATIVE_WINDOW_API_DISCONNECT:
303 res = dispatchDisconnect(args);
304 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800305 default:
306 res = NAME_NOT_FOUND;
307 break;
308 }
309 return res;
310}
311
312int SurfaceTextureClient::dispatchConnect(va_list args) {
313 int api = va_arg(args, int);
314 return connect(api);
315}
316
317int SurfaceTextureClient::dispatchDisconnect(va_list args) {
318 int api = va_arg(args, int);
319 return disconnect(api);
320}
321
322int SurfaceTextureClient::dispatchSetUsage(va_list args) {
323 int usage = va_arg(args, int);
324 return setUsage(usage);
325}
326
327int SurfaceTextureClient::dispatchSetCrop(va_list args) {
328 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
329 return setCrop(reinterpret_cast<Rect const*>(rect));
330}
331
332int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
333 size_t bufferCount = va_arg(args, size_t);
334 return setBufferCount(bufferCount);
335}
336
337int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
338 int w = va_arg(args, int);
339 int h = va_arg(args, int);
340 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700341 int err = setBuffersDimensions(w, h);
342 if (err != 0) {
343 return err;
344 }
345 return setBuffersFormat(f);
346}
347
348int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
349 int w = va_arg(args, int);
350 int h = va_arg(args, int);
351 return setBuffersDimensions(w, h);
352}
353
354int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
355 int f = va_arg(args, int);
356 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800357}
358
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700359int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
360 int m = va_arg(args, int);
361 return setScalingMode(m);
362}
363
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800364int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
365 int transform = va_arg(args, int);
366 return setBuffersTransform(transform);
367}
368
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800369int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
370 int64_t timestamp = va_arg(args, int64_t);
371 return setBuffersTimestamp(timestamp);
372}
373
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700374int SurfaceTextureClient::dispatchLock(va_list args) {
375 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
376 ARect* inOutDirtyBounds = va_arg(args, ARect*);
377 return lock(outBuffer, inOutDirtyBounds);
378}
379
380int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
381 return unlockAndPost();
382}
383
384
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800385int SurfaceTextureClient::connect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800386 LOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700387 Mutex::Autolock lock(mMutex);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700388 int err = mSurfaceTexture->connect(api);
389 if (!err && api == NATIVE_WINDOW_API_CPU) {
390 mConnectedToCpu = true;
391 }
392 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800393}
394
395int SurfaceTextureClient::disconnect(int api) {
Jamie Gennise5366c52011-01-12 20:22:41 -0800396 LOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700397 Mutex::Autolock lock(mMutex);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700398 int err = mSurfaceTexture->disconnect(api);
399 if (!err && api == NATIVE_WINDOW_API_CPU) {
400 mConnectedToCpu = false;
401 }
402 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800403}
404
405int SurfaceTextureClient::setUsage(uint32_t reqUsage)
406{
Jamie Gennise5366c52011-01-12 20:22:41 -0800407 LOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800408 Mutex::Autolock lock(mMutex);
409 mReqUsage = reqUsage;
410 return OK;
411}
412
413int SurfaceTextureClient::setCrop(Rect const* rect)
414{
Jamie Gennise5366c52011-01-12 20:22:41 -0800415 LOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800416 Mutex::Autolock lock(mMutex);
417
Jamie Gennis68f91272011-01-28 18:21:54 -0800418 Rect realRect;
419 if (rect == NULL || rect->isEmpty()) {
420 realRect = Rect(0, 0);
421 } else {
422 realRect = *rect;
423 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800424
425 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis68f91272011-01-28 18:21:54 -0800426 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800427
428 return err;
429}
430
431int SurfaceTextureClient::setBufferCount(int bufferCount)
432{
Jamie Gennise5366c52011-01-12 20:22:41 -0800433 LOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800434 Mutex::Autolock lock(mMutex);
435
436 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
437 LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
438 bufferCount, strerror(-err));
439
440 if (err == NO_ERROR) {
441 freeAllBuffers();
442 }
443
444 return err;
445}
446
Jamie Gennisbee205f2011-07-01 13:12:07 -0700447int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800448{
Jamie Gennisbee205f2011-07-01 13:12:07 -0700449 LOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800450 Mutex::Autolock lock(mMutex);
451
Jamie Gennisbee205f2011-07-01 13:12:07 -0700452 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800453 return BAD_VALUE;
454
455 if ((w && !h) || (!w && h))
456 return BAD_VALUE;
457
458 mReqWidth = w;
459 mReqHeight = h;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800460
Jamie Gennis68f91272011-01-28 18:21:54 -0800461 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
462 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
463
464 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800465}
466
Jamie Gennisbee205f2011-07-01 13:12:07 -0700467int SurfaceTextureClient::setBuffersFormat(int format)
468{
469 LOGV("SurfaceTextureClient::setBuffersFormat");
470 Mutex::Autolock lock(mMutex);
471
472 if (format<0)
473 return BAD_VALUE;
474
475 mReqFormat = format;
476
477 return NO_ERROR;
478}
479
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700480int SurfaceTextureClient::setScalingMode(int mode)
481{
Mathias Agopian933389f2011-07-18 16:15:08 -0700482 LOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700483 Mutex::Autolock lock(mMutex);
484 // mode is validated on the server
485 status_t err = mSurfaceTexture->setScalingMode(mode);
486 LOGE_IF(err, "ISurfaceTexture::setScalingMode(%d) returned %s",
487 mode, strerror(-err));
488
489 return err;
490}
491
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800492int SurfaceTextureClient::setBuffersTransform(int transform)
493{
Jamie Gennise5366c52011-01-12 20:22:41 -0800494 LOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800495 Mutex::Autolock lock(mMutex);
496 status_t err = mSurfaceTexture->setTransform(transform);
497 return err;
498}
499
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800500int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
501{
502 LOGV("SurfaceTextureClient::setBuffersTimestamp");
503 Mutex::Autolock lock(mMutex);
504 mTimestamp = timestamp;
505 return NO_ERROR;
506}
507
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800508void SurfaceTextureClient::freeAllBuffers() {
509 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
510 mSlots[i] = 0;
511 }
512}
513
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700514// ----------------------------------------------------------------------
515// the lock/unlock APIs must be used from the same thread
516
517static status_t copyBlt(
518 const sp<GraphicBuffer>& dst,
519 const sp<GraphicBuffer>& src,
520 const Region& reg)
521{
522 // src and dst with, height and format must be identical. no verification
523 // is done here.
524 status_t err;
525 uint8_t const * src_bits = NULL;
526 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
527 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
528
529 uint8_t* dst_bits = NULL;
530 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
531 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
532
533 Region::const_iterator head(reg.begin());
534 Region::const_iterator tail(reg.end());
535 if (head != tail && src_bits && dst_bits) {
536 const size_t bpp = bytesPerPixel(src->format);
537 const size_t dbpr = dst->stride * bpp;
538 const size_t sbpr = src->stride * bpp;
539
540 while (head != tail) {
541 const Rect& r(*head++);
542 ssize_t h = r.height();
543 if (h <= 0) continue;
544 size_t size = r.width() * bpp;
545 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
546 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
547 if (dbpr==sbpr && size==sbpr) {
548 size *= h;
549 h = 1;
550 }
551 do {
552 memcpy(d, s, size);
553 d += dbpr;
554 s += sbpr;
555 } while (--h > 0);
556 }
557 }
558
559 if (src_bits)
560 src->unlock();
561
562 if (dst_bits)
563 dst->unlock();
564
565 return err;
566}
567
568// ----------------------------------------------------------------------------
569
570status_t SurfaceTextureClient::lock(
571 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
572{
573 if (mLockedBuffer != 0) {
574 LOGE("Surface::lock failed, already locked");
575 return INVALID_OPERATION;
576 }
577
578 if (!mConnectedToCpu) {
579 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
580 if (err) {
581 return err;
582 }
583 // we're intending to do software rendering from this point
584 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
585 }
586
587 ANativeWindowBuffer* out;
588 status_t err = dequeueBuffer(&out);
589 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
590 if (err == NO_ERROR) {
591 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
592 err = lockBuffer(backBuffer.get());
593 LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
594 backBuffer->handle, strerror(-err));
595 if (err == NO_ERROR) {
596 const Rect bounds(backBuffer->width, backBuffer->height);
597
598 Region newDirtyRegion;
599 if (inOutDirtyBounds) {
600 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
601 newDirtyRegion.andSelf(bounds);
602 } else {
603 newDirtyRegion.set(bounds);
604 }
605
606 // figure out if we can copy the frontbuffer back
607 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
608 const bool canCopyBack = (frontBuffer != 0 &&
609 backBuffer->width == frontBuffer->width &&
610 backBuffer->height == frontBuffer->height &&
611 backBuffer->format == frontBuffer->format);
612
613 if (canCopyBack) {
614 // copy the area that is invalid and not repainted this round
615 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
616 if (!copyback.isEmpty())
617 copyBlt(backBuffer, frontBuffer, copyback);
618 } else {
619 // if we can't copy-back anything, modify the user's dirty
620 // region to make sure they redraw the whole buffer
621 newDirtyRegion.set(bounds);
622 }
623
624 // keep track of the are of the buffer that is "clean"
625 // (ie: that will be redrawn)
626 mOldDirtyRegion = newDirtyRegion;
627
628 if (inOutDirtyBounds) {
629 *inOutDirtyBounds = newDirtyRegion.getBounds();
630 }
631
632 void* vaddr;
633 status_t res = backBuffer->lock(
634 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
635 newDirtyRegion.bounds(), &vaddr);
636
637 LOGW_IF(res, "failed locking buffer (handle = %p)",
638 backBuffer->handle);
639
640 mLockedBuffer = backBuffer;
641 outBuffer->width = backBuffer->width;
642 outBuffer->height = backBuffer->height;
643 outBuffer->stride = backBuffer->stride;
644 outBuffer->format = backBuffer->format;
645 outBuffer->bits = vaddr;
646 }
647 }
648 return err;
649}
650
651status_t SurfaceTextureClient::unlockAndPost()
652{
653 if (mLockedBuffer == 0) {
654 LOGE("Surface::unlockAndPost failed, no locked buffer");
655 return INVALID_OPERATION;
656 }
657
658 status_t err = mLockedBuffer->unlock();
659 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
660
661 err = queueBuffer(mLockedBuffer.get());
662 LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
663 mLockedBuffer->handle, strerror(-err));
664
665 mPostedBuffer = mLockedBuffer;
666 mLockedBuffer = 0;
667 return err;
668}
669
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800670}; // namespace android