blob: 6aa81a96c0a016a2545b1373c531df94a42999ee [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 // XXX: Dump the slots whenever we hit a NULL entry while searching for
204 // a buffer.
205 if (mSlots[i] == NULL) {
206 if (!dumpedState) {
Steve Block9d453682011-12-20 16:23:08 +0000207 ALOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
Jamie Gennis1c441402011-06-20 12:04:09 -0700208 "looking for buffer %p", i, buffer->handle);
209 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
210 if (mSlots[j] == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000211 ALOGD("getSlotFromBufferLocked: %02d: NULL", j);
Jamie Gennis1c441402011-06-20 12:04:09 -0700212 } else {
Steve Block9d453682011-12-20 16:23:08 +0000213 ALOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
Jamie Gennis1c441402011-06-20 12:04:09 -0700214 }
215 }
216 dumpedState = true;
217 }
218 }
219
220 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
221 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800222 }
223 }
Steve Blocke6f43dd2012-01-06 19:20:56 +0000224 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800225 return BAD_VALUE;
226}
227
228int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100229 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800230 Mutex::Autolock lock(mMutex);
231 return OK;
232}
233
234int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800235 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100236 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800237 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800238 int64_t timestamp;
239 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
240 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block6807e592011-10-20 11:56:00 +0100241 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800242 timestamp / 1000000.f);
243 } else {
244 timestamp = mTimestamp;
245 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700246 int i = getSlotFromBufferLocked(buffer);
247 if (i < 0) {
248 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800249 }
Pannag Sanketi66378c62011-09-02 17:37:29 -0700250 status_t err = mSurfaceTexture->queueBuffer(i, timestamp,
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700251 mCrop, mScalingMode, mTransform,
Mathias Agopian97c602c2011-07-19 15:24:46 -0700252 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700253 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000254 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700255 }
256 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800257}
258
Iliyan Malchev41abd672011-04-14 16:54:38 -0700259int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800260 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100261 ALOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700262 { // scope for the lock
263 Mutex::Autolock lock(mMutex);
264 switch (what) {
265 case NATIVE_WINDOW_FORMAT:
266 if (mReqFormat) {
267 *value = mReqFormat;
268 return NO_ERROR;
269 }
270 break;
271 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis582270d2011-08-17 18:19:00 -0700272 {
273 sp<ISurfaceComposer> composer(
274 ComposerService::getComposerService());
275 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
276 *value = 1;
277 } else {
278 *value = 0;
279 }
280 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700281 return NO_ERROR;
282 case NATIVE_WINDOW_CONCRETE_TYPE:
283 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
284 return NO_ERROR;
285 case NATIVE_WINDOW_DEFAULT_WIDTH:
286 *value = mDefaultWidth;
287 return NO_ERROR;
288 case NATIVE_WINDOW_DEFAULT_HEIGHT:
289 *value = mDefaultHeight;
290 return NO_ERROR;
291 case NATIVE_WINDOW_TRANSFORM_HINT:
292 *value = mTransformHint;
293 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700294 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800295 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700296 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800297}
298
299int SurfaceTextureClient::perform(int operation, va_list args)
300{
301 int res = NO_ERROR;
302 switch (operation) {
303 case NATIVE_WINDOW_CONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700304 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800305 break;
306 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700307 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800308 break;
309 case NATIVE_WINDOW_SET_USAGE:
310 res = dispatchSetUsage(args);
311 break;
312 case NATIVE_WINDOW_SET_CROP:
313 res = dispatchSetCrop(args);
314 break;
315 case NATIVE_WINDOW_SET_BUFFER_COUNT:
316 res = dispatchSetBufferCount(args);
317 break;
318 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
319 res = dispatchSetBuffersGeometry(args);
320 break;
321 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
322 res = dispatchSetBuffersTransform(args);
323 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800324 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
325 res = dispatchSetBuffersTimestamp(args);
326 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700327 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
328 res = dispatchSetBuffersDimensions(args);
329 break;
330 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
331 res = dispatchSetBuffersFormat(args);
332 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700333 case NATIVE_WINDOW_LOCK:
334 res = dispatchLock(args);
335 break;
336 case NATIVE_WINDOW_UNLOCK_AND_POST:
337 res = dispatchUnlockAndPost(args);
338 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700339 case NATIVE_WINDOW_SET_SCALING_MODE:
340 res = dispatchSetScalingMode(args);
341 break;
Mathias Agopian81a63352011-07-29 17:55:48 -0700342 case NATIVE_WINDOW_API_CONNECT:
343 res = dispatchConnect(args);
344 break;
345 case NATIVE_WINDOW_API_DISCONNECT:
346 res = dispatchDisconnect(args);
347 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800348 default:
349 res = NAME_NOT_FOUND;
350 break;
351 }
352 return res;
353}
354
355int SurfaceTextureClient::dispatchConnect(va_list args) {
356 int api = va_arg(args, int);
357 return connect(api);
358}
359
360int SurfaceTextureClient::dispatchDisconnect(va_list args) {
361 int api = va_arg(args, int);
362 return disconnect(api);
363}
364
365int SurfaceTextureClient::dispatchSetUsage(va_list args) {
366 int usage = va_arg(args, int);
367 return setUsage(usage);
368}
369
370int SurfaceTextureClient::dispatchSetCrop(va_list args) {
371 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
372 return setCrop(reinterpret_cast<Rect const*>(rect));
373}
374
375int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
376 size_t bufferCount = va_arg(args, size_t);
377 return setBufferCount(bufferCount);
378}
379
380int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
381 int w = va_arg(args, int);
382 int h = va_arg(args, int);
383 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700384 int err = setBuffersDimensions(w, h);
385 if (err != 0) {
386 return err;
387 }
388 return setBuffersFormat(f);
389}
390
391int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
392 int w = va_arg(args, int);
393 int h = va_arg(args, int);
394 return setBuffersDimensions(w, h);
395}
396
397int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
398 int f = va_arg(args, int);
399 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800400}
401
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700402int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
403 int m = va_arg(args, int);
404 return setScalingMode(m);
405}
406
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800407int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
408 int transform = va_arg(args, int);
409 return setBuffersTransform(transform);
410}
411
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800412int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
413 int64_t timestamp = va_arg(args, int64_t);
414 return setBuffersTimestamp(timestamp);
415}
416
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700417int SurfaceTextureClient::dispatchLock(va_list args) {
418 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
419 ARect* inOutDirtyBounds = va_arg(args, ARect*);
420 return lock(outBuffer, inOutDirtyBounds);
421}
422
423int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
424 return unlockAndPost();
425}
426
427
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800428int SurfaceTextureClient::connect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800429 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100430 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700431 Mutex::Autolock lock(mMutex);
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700432 int err = mSurfaceTexture->connect(api,
433 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700434 if (!err && api == NATIVE_WINDOW_API_CPU) {
435 mConnectedToCpu = true;
436 }
437 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800438}
439
440int SurfaceTextureClient::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800441 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100442 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700443 Mutex::Autolock lock(mMutex);
Jamie Gennis13c5ca32011-10-18 17:14:33 -0700444 freeAllBuffers();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700445 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian70e3f812011-08-25 17:03:30 -0700446 if (!err) {
Mathias Agopian70e3f812011-08-25 17:03:30 -0700447 mReqFormat = 0;
448 mReqWidth = 0;
449 mReqHeight = 0;
450 mReqUsage = 0;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700451 mCrop.clear();
452 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
453 mTransform = 0;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700454 if (api == NATIVE_WINDOW_API_CPU) {
455 mConnectedToCpu = false;
456 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700457 }
458 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800459}
460
461int SurfaceTextureClient::setUsage(uint32_t reqUsage)
462{
Steve Block6807e592011-10-20 11:56:00 +0100463 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800464 Mutex::Autolock lock(mMutex);
465 mReqUsage = reqUsage;
466 return OK;
467}
468
469int SurfaceTextureClient::setCrop(Rect const* rect)
470{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800471 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100472 ALOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800473
Jamie Gennis68f91272011-01-28 18:21:54 -0800474 Rect realRect;
475 if (rect == NULL || rect->isEmpty()) {
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700476 realRect.clear();
Jamie Gennis68f91272011-01-28 18:21:54 -0800477 } else {
478 realRect = *rect;
479 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800480
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700481 Mutex::Autolock lock(mMutex);
482 mCrop = *rect;
483 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800484}
485
486int SurfaceTextureClient::setBufferCount(int bufferCount)
487{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800488 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100489 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800490 Mutex::Autolock lock(mMutex);
491
492 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000493 ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800494 bufferCount, strerror(-err));
495
496 if (err == NO_ERROR) {
497 freeAllBuffers();
498 }
499
500 return err;
501}
502
Jamie Gennisbee205f2011-07-01 13:12:07 -0700503int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800504{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800505 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100506 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800507
Jamie Gennisbee205f2011-07-01 13:12:07 -0700508 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800509 return BAD_VALUE;
510
511 if ((w && !h) || (!w && h))
512 return BAD_VALUE;
513
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700514 Mutex::Autolock lock(mMutex);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800515 mReqWidth = w;
516 mReqHeight = h;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700517 mCrop.clear();
518 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800519}
520
Jamie Gennisbee205f2011-07-01 13:12:07 -0700521int SurfaceTextureClient::setBuffersFormat(int format)
522{
Steve Block6807e592011-10-20 11:56:00 +0100523 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennisbee205f2011-07-01 13:12:07 -0700524
525 if (format<0)
526 return BAD_VALUE;
527
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700528 Mutex::Autolock lock(mMutex);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700529 mReqFormat = format;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700530 return NO_ERROR;
531}
532
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700533int SurfaceTextureClient::setScalingMode(int mode)
534{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800535 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100536 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700537
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700538 switch (mode) {
539 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
540 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
541 break;
542 default:
543 ALOGE("unknown scaling mode: %d", mode);
544 return BAD_VALUE;
545 }
546
547 Mutex::Autolock lock(mMutex);
548 mScalingMode = mode;
549 return NO_ERROR;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700550}
551
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800552int SurfaceTextureClient::setBuffersTransform(int transform)
553{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800554 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100555 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800556 Mutex::Autolock lock(mMutex);
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700557 mTransform = transform;
558 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800559}
560
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800561int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
562{
Steve Block6807e592011-10-20 11:56:00 +0100563 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800564 Mutex::Autolock lock(mMutex);
565 mTimestamp = timestamp;
566 return NO_ERROR;
567}
568
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800569void SurfaceTextureClient::freeAllBuffers() {
570 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
571 mSlots[i] = 0;
572 }
573}
574
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700575// ----------------------------------------------------------------------
576// the lock/unlock APIs must be used from the same thread
577
578static status_t copyBlt(
579 const sp<GraphicBuffer>& dst,
580 const sp<GraphicBuffer>& src,
581 const Region& reg)
582{
583 // src and dst with, height and format must be identical. no verification
584 // is done here.
585 status_t err;
586 uint8_t const * src_bits = NULL;
587 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000588 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700589
590 uint8_t* dst_bits = NULL;
591 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000592 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700593
594 Region::const_iterator head(reg.begin());
595 Region::const_iterator tail(reg.end());
596 if (head != tail && src_bits && dst_bits) {
597 const size_t bpp = bytesPerPixel(src->format);
598 const size_t dbpr = dst->stride * bpp;
599 const size_t sbpr = src->stride * bpp;
600
601 while (head != tail) {
602 const Rect& r(*head++);
603 ssize_t h = r.height();
604 if (h <= 0) continue;
605 size_t size = r.width() * bpp;
606 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
607 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
608 if (dbpr==sbpr && size==sbpr) {
609 size *= h;
610 h = 1;
611 }
612 do {
613 memcpy(d, s, size);
614 d += dbpr;
615 s += sbpr;
616 } while (--h > 0);
617 }
618 }
619
620 if (src_bits)
621 src->unlock();
622
623 if (dst_bits)
624 dst->unlock();
625
626 return err;
627}
628
629// ----------------------------------------------------------------------------
630
631status_t SurfaceTextureClient::lock(
632 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
633{
634 if (mLockedBuffer != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000635 ALOGE("Surface::lock failed, already locked");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700636 return INVALID_OPERATION;
637 }
638
639 if (!mConnectedToCpu) {
640 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
641 if (err) {
642 return err;
643 }
644 // we're intending to do software rendering from this point
645 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
646 }
647
648 ANativeWindowBuffer* out;
649 status_t err = dequeueBuffer(&out);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000650 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700651 if (err == NO_ERROR) {
652 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
653 err = lockBuffer(backBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000654 ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700655 backBuffer->handle, strerror(-err));
656 if (err == NO_ERROR) {
657 const Rect bounds(backBuffer->width, backBuffer->height);
658
659 Region newDirtyRegion;
660 if (inOutDirtyBounds) {
661 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
662 newDirtyRegion.andSelf(bounds);
663 } else {
664 newDirtyRegion.set(bounds);
665 }
666
667 // figure out if we can copy the frontbuffer back
668 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
669 const bool canCopyBack = (frontBuffer != 0 &&
670 backBuffer->width == frontBuffer->width &&
671 backBuffer->height == frontBuffer->height &&
672 backBuffer->format == frontBuffer->format);
673
674 if (canCopyBack) {
675 // copy the area that is invalid and not repainted this round
676 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
677 if (!copyback.isEmpty())
678 copyBlt(backBuffer, frontBuffer, copyback);
679 } else {
680 // if we can't copy-back anything, modify the user's dirty
681 // region to make sure they redraw the whole buffer
682 newDirtyRegion.set(bounds);
683 }
684
685 // keep track of the are of the buffer that is "clean"
686 // (ie: that will be redrawn)
687 mOldDirtyRegion = newDirtyRegion;
688
689 if (inOutDirtyBounds) {
690 *inOutDirtyBounds = newDirtyRegion.getBounds();
691 }
692
693 void* vaddr;
694 status_t res = backBuffer->lock(
695 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
696 newDirtyRegion.bounds(), &vaddr);
697
Steve Block32397c12012-01-05 23:22:43 +0000698 ALOGW_IF(res, "failed locking buffer (handle = %p)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700699 backBuffer->handle);
700
701 mLockedBuffer = backBuffer;
702 outBuffer->width = backBuffer->width;
703 outBuffer->height = backBuffer->height;
704 outBuffer->stride = backBuffer->stride;
705 outBuffer->format = backBuffer->format;
706 outBuffer->bits = vaddr;
707 }
708 }
709 return err;
710}
711
712status_t SurfaceTextureClient::unlockAndPost()
713{
714 if (mLockedBuffer == 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000715 ALOGE("Surface::unlockAndPost failed, no locked buffer");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700716 return INVALID_OPERATION;
717 }
718
719 status_t err = mLockedBuffer->unlock();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000720 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700721
722 err = queueBuffer(mLockedBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000723 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700724 mLockedBuffer->handle, strerror(-err));
725
726 mPostedBuffer = mLockedBuffer;
727 mLockedBuffer = 0;
728 return err;
729}
730
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800731}; // namespace android