blob: 592658bd3857308787c0116cbe184f2b13bf0305 [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 Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise5366c52011-01-12 20:22:41 -080019//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080020
Mathias Agopianb0e76f42012-03-23 14:15:44 -070021#include <android/native_window.h>
22
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080023#include <utils/Log.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080024#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080025
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/ISurfaceComposer.h>
27#include <gui/SurfaceComposerClient.h>
Mathias Agopianb0e76f42012-03-23 14:15:44 -070028#include <gui/SurfaceTexture.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080029#include <gui/SurfaceTextureClient.h>
30
Mathias Agopian41f673c2011-11-17 17:48:35 -080031#include <private/gui/ComposerService.h>
32
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080033namespace android {
34
35SurfaceTextureClient::SurfaceTextureClient(
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070036 const sp<ISurfaceTexture>& surfaceTexture)
37{
38 SurfaceTextureClient::init();
39 SurfaceTextureClient::setISurfaceTexture(surfaceTexture);
40}
41
Daniel Lamb2675792012-02-23 14:35:13 -080042// see SurfaceTextureClient.h
43SurfaceTextureClient::SurfaceTextureClient(const
44 sp<SurfaceTexture>& surfaceTexture)
45{
46 SurfaceTextureClient::init();
47 SurfaceTextureClient::setISurfaceTexture(surfaceTexture->getBufferQueue());
48}
49
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070050SurfaceTextureClient::SurfaceTextureClient() {
51 SurfaceTextureClient::init();
52}
53
Mathias Agopiana36bcd52011-11-17 18:46:09 -080054SurfaceTextureClient::~SurfaceTextureClient() {
55 if (mConnectedToCpu) {
56 SurfaceTextureClient::disconnect(NATIVE_WINDOW_API_CPU);
57 }
58}
59
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070060void SurfaceTextureClient::init() {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080061 // Initialize the ANativeWindow function pointers.
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070062 ANativeWindow::setSwapInterval = hook_setSwapInterval;
63 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
64 ANativeWindow::cancelBuffer = hook_cancelBuffer;
65 ANativeWindow::lockBuffer = hook_lockBuffer;
66 ANativeWindow::queueBuffer = hook_queueBuffer;
67 ANativeWindow::query = hook_query;
68 ANativeWindow::perform = hook_perform;
Jamie Gennis1b20cde2011-02-02 15:31:47 -080069
Mathias Agopian80727112011-05-02 19:51:12 -070070 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
71 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
72
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070073 mReqWidth = 0;
74 mReqHeight = 0;
75 mReqFormat = 0;
76 mReqUsage = 0;
77 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Mathias Agopian851ef8f2012-03-29 17:10:08 -070078 mCrop.clear();
79 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
80 mTransform = 0;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -070081 mDefaultWidth = 0;
82 mDefaultHeight = 0;
83 mTransformHint = 0;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070084 mConnectedToCpu = false;
85}
86
87void SurfaceTextureClient::setISurfaceTexture(
88 const sp<ISurfaceTexture>& surfaceTexture)
89{
90 mSurfaceTexture = surfaceTexture;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080091}
92
Jamie Gennisbae774e2011-03-14 15:08:53 -070093sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
94 return mSurfaceTexture;
95}
96
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070097int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080098 SurfaceTextureClient* c = getSelf(window);
99 return c->setSwapInterval(interval);
100}
101
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700102int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700103 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800104 SurfaceTextureClient* c = getSelf(window);
105 return c->dequeueBuffer(buffer);
106}
107
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700108int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700109 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800110 SurfaceTextureClient* c = getSelf(window);
111 return c->cancelBuffer(buffer);
112}
113
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700114int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700115 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800116 SurfaceTextureClient* c = getSelf(window);
117 return c->lockBuffer(buffer);
118}
119
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700120int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700121 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800122 SurfaceTextureClient* c = getSelf(window);
123 return c->queueBuffer(buffer);
124}
125
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700126int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev41abd672011-04-14 16:54:38 -0700127 int what, int* value) {
128 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129 return c->query(what, value);
130}
131
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700132int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800133 va_list args;
134 va_start(args, operation);
135 SurfaceTextureClient* c = getSelf(window);
136 return c->perform(operation, args);
137}
138
139int SurfaceTextureClient::setSwapInterval(int interval) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800140 ATRACE_CALL();
Mathias Agopian80727112011-05-02 19:51:12 -0700141 // EGL specification states:
142 // interval is silently clamped to minimum and maximum implementation
143 // dependent values before being stored.
144 // Although we don't have to, we apply the same logic here.
145
146 if (interval < minSwapInterval)
147 interval = minSwapInterval;
148
149 if (interval > maxSwapInterval)
150 interval = maxSwapInterval;
151
152 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
153
154 return res;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800155}
156
157int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800158 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100159 ALOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800160 Mutex::Autolock lock(mMutex);
161 int buf = -1;
Mathias Agopian80727112011-05-02 19:51:12 -0700162 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopianc04f1532011-04-25 20:22:14 -0700163 mReqFormat, mReqUsage);
Mathias Agopian80727112011-05-02 19:51:12 -0700164 if (result < 0) {
Steve Block6807e592011-10-20 11:56:00 +0100165 ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700166 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
167 result);
Mathias Agopian80727112011-05-02 19:51:12 -0700168 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800169 }
170 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian80727112011-05-02 19:51:12 -0700171 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
172 freeAllBuffers();
173 }
174
175 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700176 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
177 if (result != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000178 ALOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700179 result);
180 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800181 }
182 }
183 *buffer = gbuf.get();
184 return OK;
185}
186
187int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800188 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100189 ALOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800190 Mutex::Autolock lock(mMutex);
Jamie Gennis1c441402011-06-20 12:04:09 -0700191 int i = getSlotFromBufferLocked(buffer);
192 if (i < 0) {
193 return i;
194 }
195 mSurfaceTexture->cancelBuffer(i);
196 return OK;
197}
198
199int SurfaceTextureClient::getSlotFromBufferLocked(
200 android_native_buffer_t* buffer) const {
201 bool dumpedState = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800202 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis1c441402011-06-20 12:04:09 -0700203 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
204 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800205 }
206 }
Steve Blocke6f43dd2012-01-06 19:20:56 +0000207 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800208 return BAD_VALUE;
209}
210
211int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100212 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800213 Mutex::Autolock lock(mMutex);
214 return OK;
215}
216
217int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800218 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100219 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800220 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800221 int64_t timestamp;
222 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
223 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block6807e592011-10-20 11:56:00 +0100224 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800225 timestamp / 1000000.f);
226 } else {
227 timestamp = mTimestamp;
228 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700229 int i = getSlotFromBufferLocked(buffer);
230 if (i < 0) {
231 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800232 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700233
234 ISurfaceTexture::QueueBufferOutput output;
235 ISurfaceTexture::QueueBufferInput input(timestamp,
236 mCrop, mScalingMode, mTransform);
237 status_t err = mSurfaceTexture->queueBuffer(i, input, &output);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700238 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000239 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700240 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700241 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700242 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800243}
244
Iliyan Malchev41abd672011-04-14 16:54:38 -0700245int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800246 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100247 ALOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700248 { // scope for the lock
249 Mutex::Autolock lock(mMutex);
250 switch (what) {
251 case NATIVE_WINDOW_FORMAT:
252 if (mReqFormat) {
253 *value = mReqFormat;
254 return NO_ERROR;
255 }
256 break;
257 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis582270d2011-08-17 18:19:00 -0700258 {
259 sp<ISurfaceComposer> composer(
260 ComposerService::getComposerService());
261 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
262 *value = 1;
263 } else {
264 *value = 0;
265 }
266 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700267 return NO_ERROR;
268 case NATIVE_WINDOW_CONCRETE_TYPE:
269 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
270 return NO_ERROR;
271 case NATIVE_WINDOW_DEFAULT_WIDTH:
272 *value = mDefaultWidth;
273 return NO_ERROR;
274 case NATIVE_WINDOW_DEFAULT_HEIGHT:
275 *value = mDefaultHeight;
276 return NO_ERROR;
277 case NATIVE_WINDOW_TRANSFORM_HINT:
278 *value = mTransformHint;
279 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700280 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800281 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700282 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800283}
284
285int SurfaceTextureClient::perform(int operation, va_list args)
286{
287 int res = NO_ERROR;
288 switch (operation) {
289 case NATIVE_WINDOW_CONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700290 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800291 break;
292 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700293 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800294 break;
295 case NATIVE_WINDOW_SET_USAGE:
296 res = dispatchSetUsage(args);
297 break;
298 case NATIVE_WINDOW_SET_CROP:
299 res = dispatchSetCrop(args);
300 break;
301 case NATIVE_WINDOW_SET_BUFFER_COUNT:
302 res = dispatchSetBufferCount(args);
303 break;
304 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
305 res = dispatchSetBuffersGeometry(args);
306 break;
307 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
308 res = dispatchSetBuffersTransform(args);
309 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800310 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
311 res = dispatchSetBuffersTimestamp(args);
312 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700313 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
314 res = dispatchSetBuffersDimensions(args);
315 break;
316 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
317 res = dispatchSetBuffersFormat(args);
318 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700319 case NATIVE_WINDOW_LOCK:
320 res = dispatchLock(args);
321 break;
322 case NATIVE_WINDOW_UNLOCK_AND_POST:
323 res = dispatchUnlockAndPost(args);
324 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700325 case NATIVE_WINDOW_SET_SCALING_MODE:
326 res = dispatchSetScalingMode(args);
327 break;
Mathias Agopian81a63352011-07-29 17:55:48 -0700328 case NATIVE_WINDOW_API_CONNECT:
329 res = dispatchConnect(args);
330 break;
331 case NATIVE_WINDOW_API_DISCONNECT:
332 res = dispatchDisconnect(args);
333 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800334 default:
335 res = NAME_NOT_FOUND;
336 break;
337 }
338 return res;
339}
340
341int SurfaceTextureClient::dispatchConnect(va_list args) {
342 int api = va_arg(args, int);
343 return connect(api);
344}
345
346int SurfaceTextureClient::dispatchDisconnect(va_list args) {
347 int api = va_arg(args, int);
348 return disconnect(api);
349}
350
351int SurfaceTextureClient::dispatchSetUsage(va_list args) {
352 int usage = va_arg(args, int);
353 return setUsage(usage);
354}
355
356int SurfaceTextureClient::dispatchSetCrop(va_list args) {
357 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
358 return setCrop(reinterpret_cast<Rect const*>(rect));
359}
360
361int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
362 size_t bufferCount = va_arg(args, size_t);
363 return setBufferCount(bufferCount);
364}
365
366int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
367 int w = va_arg(args, int);
368 int h = va_arg(args, int);
369 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700370 int err = setBuffersDimensions(w, h);
371 if (err != 0) {
372 return err;
373 }
374 return setBuffersFormat(f);
375}
376
377int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
378 int w = va_arg(args, int);
379 int h = va_arg(args, int);
380 return setBuffersDimensions(w, h);
381}
382
383int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
384 int f = va_arg(args, int);
385 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800386}
387
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700388int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
389 int m = va_arg(args, int);
390 return setScalingMode(m);
391}
392
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800393int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
394 int transform = va_arg(args, int);
395 return setBuffersTransform(transform);
396}
397
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800398int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
399 int64_t timestamp = va_arg(args, int64_t);
400 return setBuffersTimestamp(timestamp);
401}
402
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700403int SurfaceTextureClient::dispatchLock(va_list args) {
404 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
405 ARect* inOutDirtyBounds = va_arg(args, ARect*);
406 return lock(outBuffer, inOutDirtyBounds);
407}
408
409int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
410 return unlockAndPost();
411}
412
413
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800414int SurfaceTextureClient::connect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800415 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100416 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700417 Mutex::Autolock lock(mMutex);
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700418 int err = mSurfaceTexture->connect(api,
419 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700420 if (!err && api == NATIVE_WINDOW_API_CPU) {
421 mConnectedToCpu = true;
422 }
423 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800424}
425
426int SurfaceTextureClient::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800427 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100428 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700429 Mutex::Autolock lock(mMutex);
Jamie Gennis13c5ca32011-10-18 17:14:33 -0700430 freeAllBuffers();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700431 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian70e3f812011-08-25 17:03:30 -0700432 if (!err) {
Mathias Agopian70e3f812011-08-25 17:03:30 -0700433 mReqFormat = 0;
434 mReqWidth = 0;
435 mReqHeight = 0;
436 mReqUsage = 0;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700437 mCrop.clear();
438 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
439 mTransform = 0;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700440 if (api == NATIVE_WINDOW_API_CPU) {
441 mConnectedToCpu = false;
442 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700443 }
444 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800445}
446
447int SurfaceTextureClient::setUsage(uint32_t reqUsage)
448{
Steve Block6807e592011-10-20 11:56:00 +0100449 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800450 Mutex::Autolock lock(mMutex);
451 mReqUsage = reqUsage;
452 return OK;
453}
454
455int SurfaceTextureClient::setCrop(Rect const* rect)
456{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800457 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100458 ALOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800459
Jamie Gennis68f91272011-01-28 18:21:54 -0800460 Rect realRect;
461 if (rect == NULL || rect->isEmpty()) {
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700462 realRect.clear();
Jamie Gennis68f91272011-01-28 18:21:54 -0800463 } else {
464 realRect = *rect;
465 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800466
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700467 Mutex::Autolock lock(mMutex);
468 mCrop = *rect;
469 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800470}
471
472int SurfaceTextureClient::setBufferCount(int bufferCount)
473{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800474 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100475 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800476 Mutex::Autolock lock(mMutex);
477
478 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000479 ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800480 bufferCount, strerror(-err));
481
482 if (err == NO_ERROR) {
483 freeAllBuffers();
484 }
485
486 return err;
487}
488
Jamie Gennisbee205f2011-07-01 13:12:07 -0700489int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800490{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800491 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100492 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800493
Jamie Gennisbee205f2011-07-01 13:12:07 -0700494 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800495 return BAD_VALUE;
496
497 if ((w && !h) || (!w && h))
498 return BAD_VALUE;
499
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700500 Mutex::Autolock lock(mMutex);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800501 mReqWidth = w;
502 mReqHeight = h;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700503 mCrop.clear();
504 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800505}
506
Jamie Gennisbee205f2011-07-01 13:12:07 -0700507int SurfaceTextureClient::setBuffersFormat(int format)
508{
Steve Block6807e592011-10-20 11:56:00 +0100509 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennisbee205f2011-07-01 13:12:07 -0700510
511 if (format<0)
512 return BAD_VALUE;
513
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700514 Mutex::Autolock lock(mMutex);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700515 mReqFormat = format;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700516 return NO_ERROR;
517}
518
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700519int SurfaceTextureClient::setScalingMode(int mode)
520{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800521 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100522 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700523
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700524 switch (mode) {
525 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
526 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
527 break;
528 default:
529 ALOGE("unknown scaling mode: %d", mode);
530 return BAD_VALUE;
531 }
532
533 Mutex::Autolock lock(mMutex);
534 mScalingMode = mode;
535 return NO_ERROR;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700536}
537
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800538int SurfaceTextureClient::setBuffersTransform(int transform)
539{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800540 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100541 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800542 Mutex::Autolock lock(mMutex);
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700543 mTransform = transform;
544 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800545}
546
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800547int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
548{
Steve Block6807e592011-10-20 11:56:00 +0100549 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800550 Mutex::Autolock lock(mMutex);
551 mTimestamp = timestamp;
552 return NO_ERROR;
553}
554
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800555void SurfaceTextureClient::freeAllBuffers() {
556 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
557 mSlots[i] = 0;
558 }
559}
560
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700561// ----------------------------------------------------------------------
562// the lock/unlock APIs must be used from the same thread
563
564static status_t copyBlt(
565 const sp<GraphicBuffer>& dst,
566 const sp<GraphicBuffer>& src,
567 const Region& reg)
568{
569 // src and dst with, height and format must be identical. no verification
570 // is done here.
571 status_t err;
572 uint8_t const * src_bits = NULL;
573 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000574 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700575
576 uint8_t* dst_bits = NULL;
577 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000578 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700579
580 Region::const_iterator head(reg.begin());
581 Region::const_iterator tail(reg.end());
582 if (head != tail && src_bits && dst_bits) {
583 const size_t bpp = bytesPerPixel(src->format);
584 const size_t dbpr = dst->stride * bpp;
585 const size_t sbpr = src->stride * bpp;
586
587 while (head != tail) {
588 const Rect& r(*head++);
589 ssize_t h = r.height();
590 if (h <= 0) continue;
591 size_t size = r.width() * bpp;
592 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
593 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
594 if (dbpr==sbpr && size==sbpr) {
595 size *= h;
596 h = 1;
597 }
598 do {
599 memcpy(d, s, size);
600 d += dbpr;
601 s += sbpr;
602 } while (--h > 0);
603 }
604 }
605
606 if (src_bits)
607 src->unlock();
608
609 if (dst_bits)
610 dst->unlock();
611
612 return err;
613}
614
615// ----------------------------------------------------------------------------
616
617status_t SurfaceTextureClient::lock(
618 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
619{
620 if (mLockedBuffer != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000621 ALOGE("Surface::lock failed, already locked");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700622 return INVALID_OPERATION;
623 }
624
625 if (!mConnectedToCpu) {
626 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
627 if (err) {
628 return err;
629 }
630 // we're intending to do software rendering from this point
631 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
632 }
633
634 ANativeWindowBuffer* out;
635 status_t err = dequeueBuffer(&out);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000636 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700637 if (err == NO_ERROR) {
638 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
639 err = lockBuffer(backBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000640 ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700641 backBuffer->handle, strerror(-err));
642 if (err == NO_ERROR) {
643 const Rect bounds(backBuffer->width, backBuffer->height);
644
645 Region newDirtyRegion;
646 if (inOutDirtyBounds) {
647 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
648 newDirtyRegion.andSelf(bounds);
649 } else {
650 newDirtyRegion.set(bounds);
651 }
652
653 // figure out if we can copy the frontbuffer back
654 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
655 const bool canCopyBack = (frontBuffer != 0 &&
656 backBuffer->width == frontBuffer->width &&
657 backBuffer->height == frontBuffer->height &&
658 backBuffer->format == frontBuffer->format);
659
660 if (canCopyBack) {
661 // copy the area that is invalid and not repainted this round
662 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
663 if (!copyback.isEmpty())
664 copyBlt(backBuffer, frontBuffer, copyback);
665 } else {
666 // if we can't copy-back anything, modify the user's dirty
667 // region to make sure they redraw the whole buffer
668 newDirtyRegion.set(bounds);
669 }
670
671 // keep track of the are of the buffer that is "clean"
672 // (ie: that will be redrawn)
673 mOldDirtyRegion = newDirtyRegion;
674
675 if (inOutDirtyBounds) {
676 *inOutDirtyBounds = newDirtyRegion.getBounds();
677 }
678
679 void* vaddr;
680 status_t res = backBuffer->lock(
681 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
682 newDirtyRegion.bounds(), &vaddr);
683
Steve Block32397c12012-01-05 23:22:43 +0000684 ALOGW_IF(res, "failed locking buffer (handle = %p)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700685 backBuffer->handle);
686
687 mLockedBuffer = backBuffer;
688 outBuffer->width = backBuffer->width;
689 outBuffer->height = backBuffer->height;
690 outBuffer->stride = backBuffer->stride;
691 outBuffer->format = backBuffer->format;
692 outBuffer->bits = vaddr;
693 }
694 }
695 return err;
696}
697
698status_t SurfaceTextureClient::unlockAndPost()
699{
700 if (mLockedBuffer == 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000701 ALOGE("Surface::unlockAndPost failed, no locked buffer");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700702 return INVALID_OPERATION;
703 }
704
705 status_t err = mLockedBuffer->unlock();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000706 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700707
708 err = queueBuffer(mLockedBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000709 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700710 mLockedBuffer->handle, strerror(-err));
711
712 mPostedBuffer = mLockedBuffer;
713 mLockedBuffer = 0;
714 return err;
715}
716
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800717}; // namespace android