blob: 5fb54167f3ca9e5a07ec5cc0e3f03b3a4dc06bb1 [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 }
Pannag Sanketi66378c62011-09-02 17:37:29 -0700233 status_t err = mSurfaceTexture->queueBuffer(i, timestamp,
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700234 mCrop, mScalingMode, mTransform,
Mathias Agopian97c602c2011-07-19 15:24:46 -0700235 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700236 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000237 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700238 }
239 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800240}
241
Iliyan Malchev41abd672011-04-14 16:54:38 -0700242int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800243 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100244 ALOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700245 { // scope for the lock
246 Mutex::Autolock lock(mMutex);
247 switch (what) {
248 case NATIVE_WINDOW_FORMAT:
249 if (mReqFormat) {
250 *value = mReqFormat;
251 return NO_ERROR;
252 }
253 break;
254 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis582270d2011-08-17 18:19:00 -0700255 {
256 sp<ISurfaceComposer> composer(
257 ComposerService::getComposerService());
258 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
259 *value = 1;
260 } else {
261 *value = 0;
262 }
263 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700264 return NO_ERROR;
265 case NATIVE_WINDOW_CONCRETE_TYPE:
266 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
267 return NO_ERROR;
268 case NATIVE_WINDOW_DEFAULT_WIDTH:
269 *value = mDefaultWidth;
270 return NO_ERROR;
271 case NATIVE_WINDOW_DEFAULT_HEIGHT:
272 *value = mDefaultHeight;
273 return NO_ERROR;
274 case NATIVE_WINDOW_TRANSFORM_HINT:
275 *value = mTransformHint;
276 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700277 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800278 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700279 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800280}
281
282int SurfaceTextureClient::perform(int operation, va_list args)
283{
284 int res = NO_ERROR;
285 switch (operation) {
286 case NATIVE_WINDOW_CONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700287 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800288 break;
289 case NATIVE_WINDOW_DISCONNECT:
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_SET_USAGE:
293 res = dispatchSetUsage(args);
294 break;
295 case NATIVE_WINDOW_SET_CROP:
296 res = dispatchSetCrop(args);
297 break;
298 case NATIVE_WINDOW_SET_BUFFER_COUNT:
299 res = dispatchSetBufferCount(args);
300 break;
301 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
302 res = dispatchSetBuffersGeometry(args);
303 break;
304 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
305 res = dispatchSetBuffersTransform(args);
306 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800307 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
308 res = dispatchSetBuffersTimestamp(args);
309 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700310 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
311 res = dispatchSetBuffersDimensions(args);
312 break;
313 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
314 res = dispatchSetBuffersFormat(args);
315 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700316 case NATIVE_WINDOW_LOCK:
317 res = dispatchLock(args);
318 break;
319 case NATIVE_WINDOW_UNLOCK_AND_POST:
320 res = dispatchUnlockAndPost(args);
321 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700322 case NATIVE_WINDOW_SET_SCALING_MODE:
323 res = dispatchSetScalingMode(args);
324 break;
Mathias Agopian81a63352011-07-29 17:55:48 -0700325 case NATIVE_WINDOW_API_CONNECT:
326 res = dispatchConnect(args);
327 break;
328 case NATIVE_WINDOW_API_DISCONNECT:
329 res = dispatchDisconnect(args);
330 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800331 default:
332 res = NAME_NOT_FOUND;
333 break;
334 }
335 return res;
336}
337
338int SurfaceTextureClient::dispatchConnect(va_list args) {
339 int api = va_arg(args, int);
340 return connect(api);
341}
342
343int SurfaceTextureClient::dispatchDisconnect(va_list args) {
344 int api = va_arg(args, int);
345 return disconnect(api);
346}
347
348int SurfaceTextureClient::dispatchSetUsage(va_list args) {
349 int usage = va_arg(args, int);
350 return setUsage(usage);
351}
352
353int SurfaceTextureClient::dispatchSetCrop(va_list args) {
354 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
355 return setCrop(reinterpret_cast<Rect const*>(rect));
356}
357
358int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
359 size_t bufferCount = va_arg(args, size_t);
360 return setBufferCount(bufferCount);
361}
362
363int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
364 int w = va_arg(args, int);
365 int h = va_arg(args, int);
366 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700367 int err = setBuffersDimensions(w, h);
368 if (err != 0) {
369 return err;
370 }
371 return setBuffersFormat(f);
372}
373
374int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
375 int w = va_arg(args, int);
376 int h = va_arg(args, int);
377 return setBuffersDimensions(w, h);
378}
379
380int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
381 int f = va_arg(args, int);
382 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800383}
384
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700385int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
386 int m = va_arg(args, int);
387 return setScalingMode(m);
388}
389
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800390int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
391 int transform = va_arg(args, int);
392 return setBuffersTransform(transform);
393}
394
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800395int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
396 int64_t timestamp = va_arg(args, int64_t);
397 return setBuffersTimestamp(timestamp);
398}
399
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700400int SurfaceTextureClient::dispatchLock(va_list args) {
401 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
402 ARect* inOutDirtyBounds = va_arg(args, ARect*);
403 return lock(outBuffer, inOutDirtyBounds);
404}
405
406int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
407 return unlockAndPost();
408}
409
410
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800411int SurfaceTextureClient::connect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800412 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100413 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700414 Mutex::Autolock lock(mMutex);
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700415 int err = mSurfaceTexture->connect(api,
416 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700417 if (!err && api == NATIVE_WINDOW_API_CPU) {
418 mConnectedToCpu = true;
419 }
420 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800421}
422
423int SurfaceTextureClient::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800424 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100425 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700426 Mutex::Autolock lock(mMutex);
Jamie Gennis13c5ca32011-10-18 17:14:33 -0700427 freeAllBuffers();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700428 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian70e3f812011-08-25 17:03:30 -0700429 if (!err) {
Mathias Agopian70e3f812011-08-25 17:03:30 -0700430 mReqFormat = 0;
431 mReqWidth = 0;
432 mReqHeight = 0;
433 mReqUsage = 0;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700434 mCrop.clear();
435 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
436 mTransform = 0;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700437 if (api == NATIVE_WINDOW_API_CPU) {
438 mConnectedToCpu = false;
439 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700440 }
441 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800442}
443
444int SurfaceTextureClient::setUsage(uint32_t reqUsage)
445{
Steve Block6807e592011-10-20 11:56:00 +0100446 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800447 Mutex::Autolock lock(mMutex);
448 mReqUsage = reqUsage;
449 return OK;
450}
451
452int SurfaceTextureClient::setCrop(Rect const* rect)
453{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800454 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100455 ALOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800456
Jamie Gennis68f91272011-01-28 18:21:54 -0800457 Rect realRect;
458 if (rect == NULL || rect->isEmpty()) {
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700459 realRect.clear();
Jamie Gennis68f91272011-01-28 18:21:54 -0800460 } else {
461 realRect = *rect;
462 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800463
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700464 Mutex::Autolock lock(mMutex);
465 mCrop = *rect;
466 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800467}
468
469int SurfaceTextureClient::setBufferCount(int bufferCount)
470{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800471 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100472 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800473 Mutex::Autolock lock(mMutex);
474
475 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000476 ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800477 bufferCount, strerror(-err));
478
479 if (err == NO_ERROR) {
480 freeAllBuffers();
481 }
482
483 return err;
484}
485
Jamie Gennisbee205f2011-07-01 13:12:07 -0700486int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800487{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800488 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100489 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800490
Jamie Gennisbee205f2011-07-01 13:12:07 -0700491 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800492 return BAD_VALUE;
493
494 if ((w && !h) || (!w && h))
495 return BAD_VALUE;
496
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700497 Mutex::Autolock lock(mMutex);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800498 mReqWidth = w;
499 mReqHeight = h;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700500 mCrop.clear();
501 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800502}
503
Jamie Gennisbee205f2011-07-01 13:12:07 -0700504int SurfaceTextureClient::setBuffersFormat(int format)
505{
Steve Block6807e592011-10-20 11:56:00 +0100506 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennisbee205f2011-07-01 13:12:07 -0700507
508 if (format<0)
509 return BAD_VALUE;
510
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700511 Mutex::Autolock lock(mMutex);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700512 mReqFormat = format;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700513 return NO_ERROR;
514}
515
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700516int SurfaceTextureClient::setScalingMode(int mode)
517{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800518 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100519 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700520
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700521 switch (mode) {
522 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
523 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
524 break;
525 default:
526 ALOGE("unknown scaling mode: %d", mode);
527 return BAD_VALUE;
528 }
529
530 Mutex::Autolock lock(mMutex);
531 mScalingMode = mode;
532 return NO_ERROR;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700533}
534
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800535int SurfaceTextureClient::setBuffersTransform(int transform)
536{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800537 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100538 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800539 Mutex::Autolock lock(mMutex);
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700540 mTransform = transform;
541 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800542}
543
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800544int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
545{
Steve Block6807e592011-10-20 11:56:00 +0100546 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800547 Mutex::Autolock lock(mMutex);
548 mTimestamp = timestamp;
549 return NO_ERROR;
550}
551
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800552void SurfaceTextureClient::freeAllBuffers() {
553 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
554 mSlots[i] = 0;
555 }
556}
557
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700558// ----------------------------------------------------------------------
559// the lock/unlock APIs must be used from the same thread
560
561static status_t copyBlt(
562 const sp<GraphicBuffer>& dst,
563 const sp<GraphicBuffer>& src,
564 const Region& reg)
565{
566 // src and dst with, height and format must be identical. no verification
567 // is done here.
568 status_t err;
569 uint8_t const * src_bits = NULL;
570 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000571 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700572
573 uint8_t* dst_bits = NULL;
574 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000575 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700576
577 Region::const_iterator head(reg.begin());
578 Region::const_iterator tail(reg.end());
579 if (head != tail && src_bits && dst_bits) {
580 const size_t bpp = bytesPerPixel(src->format);
581 const size_t dbpr = dst->stride * bpp;
582 const size_t sbpr = src->stride * bpp;
583
584 while (head != tail) {
585 const Rect& r(*head++);
586 ssize_t h = r.height();
587 if (h <= 0) continue;
588 size_t size = r.width() * bpp;
589 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
590 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
591 if (dbpr==sbpr && size==sbpr) {
592 size *= h;
593 h = 1;
594 }
595 do {
596 memcpy(d, s, size);
597 d += dbpr;
598 s += sbpr;
599 } while (--h > 0);
600 }
601 }
602
603 if (src_bits)
604 src->unlock();
605
606 if (dst_bits)
607 dst->unlock();
608
609 return err;
610}
611
612// ----------------------------------------------------------------------------
613
614status_t SurfaceTextureClient::lock(
615 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
616{
617 if (mLockedBuffer != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000618 ALOGE("Surface::lock failed, already locked");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700619 return INVALID_OPERATION;
620 }
621
622 if (!mConnectedToCpu) {
623 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
624 if (err) {
625 return err;
626 }
627 // we're intending to do software rendering from this point
628 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
629 }
630
631 ANativeWindowBuffer* out;
632 status_t err = dequeueBuffer(&out);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000633 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700634 if (err == NO_ERROR) {
635 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
636 err = lockBuffer(backBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000637 ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700638 backBuffer->handle, strerror(-err));
639 if (err == NO_ERROR) {
640 const Rect bounds(backBuffer->width, backBuffer->height);
641
642 Region newDirtyRegion;
643 if (inOutDirtyBounds) {
644 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
645 newDirtyRegion.andSelf(bounds);
646 } else {
647 newDirtyRegion.set(bounds);
648 }
649
650 // figure out if we can copy the frontbuffer back
651 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
652 const bool canCopyBack = (frontBuffer != 0 &&
653 backBuffer->width == frontBuffer->width &&
654 backBuffer->height == frontBuffer->height &&
655 backBuffer->format == frontBuffer->format);
656
657 if (canCopyBack) {
658 // copy the area that is invalid and not repainted this round
659 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
660 if (!copyback.isEmpty())
661 copyBlt(backBuffer, frontBuffer, copyback);
662 } else {
663 // if we can't copy-back anything, modify the user's dirty
664 // region to make sure they redraw the whole buffer
665 newDirtyRegion.set(bounds);
666 }
667
668 // keep track of the are of the buffer that is "clean"
669 // (ie: that will be redrawn)
670 mOldDirtyRegion = newDirtyRegion;
671
672 if (inOutDirtyBounds) {
673 *inOutDirtyBounds = newDirtyRegion.getBounds();
674 }
675
676 void* vaddr;
677 status_t res = backBuffer->lock(
678 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
679 newDirtyRegion.bounds(), &vaddr);
680
Steve Block32397c12012-01-05 23:22:43 +0000681 ALOGW_IF(res, "failed locking buffer (handle = %p)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700682 backBuffer->handle);
683
684 mLockedBuffer = backBuffer;
685 outBuffer->width = backBuffer->width;
686 outBuffer->height = backBuffer->height;
687 outBuffer->stride = backBuffer->stride;
688 outBuffer->format = backBuffer->format;
689 outBuffer->bits = vaddr;
690 }
691 }
692 return err;
693}
694
695status_t SurfaceTextureClient::unlockAndPost()
696{
697 if (mLockedBuffer == 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000698 ALOGE("Surface::unlockAndPost failed, no locked buffer");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700699 return INVALID_OPERATION;
700 }
701
702 status_t err = mLockedBuffer->unlock();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000703 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700704
705 err = queueBuffer(mLockedBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000706 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700707 mLockedBuffer->handle, strerror(-err));
708
709 mPostedBuffer = mLockedBuffer;
710 mLockedBuffer = 0;
711 return err;
712}
713
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800714}; // namespace android