blob: b9e870da2477302d442f518d9bbb890584c315df [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 Agopianbb66c9b2011-07-21 14:50:29 -070078 mDefaultWidth = 0;
79 mDefaultHeight = 0;
80 mTransformHint = 0;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070081 mConnectedToCpu = false;
82}
83
84void SurfaceTextureClient::setISurfaceTexture(
85 const sp<ISurfaceTexture>& surfaceTexture)
86{
87 mSurfaceTexture = surfaceTexture;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080088}
89
Jamie Gennisbae774e2011-03-14 15:08:53 -070090sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
91 return mSurfaceTexture;
92}
93
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070094int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080095 SurfaceTextureClient* c = getSelf(window);
96 return c->setSwapInterval(interval);
97}
98
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070099int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700100 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800101 SurfaceTextureClient* c = getSelf(window);
102 return c->dequeueBuffer(buffer);
103}
104
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700105int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700106 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800107 SurfaceTextureClient* c = getSelf(window);
108 return c->cancelBuffer(buffer);
109}
110
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700111int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700112 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800113 SurfaceTextureClient* c = getSelf(window);
114 return c->lockBuffer(buffer);
115}
116
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700117int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700118 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800119 SurfaceTextureClient* c = getSelf(window);
120 return c->queueBuffer(buffer);
121}
122
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700123int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev41abd672011-04-14 16:54:38 -0700124 int what, int* value) {
125 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800126 return c->query(what, value);
127}
128
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700129int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800130 va_list args;
131 va_start(args, operation);
132 SurfaceTextureClient* c = getSelf(window);
133 return c->perform(operation, args);
134}
135
136int SurfaceTextureClient::setSwapInterval(int interval) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800137 ATRACE_CALL();
Mathias Agopian80727112011-05-02 19:51:12 -0700138 // EGL specification states:
139 // interval is silently clamped to minimum and maximum implementation
140 // dependent values before being stored.
141 // Although we don't have to, we apply the same logic here.
142
143 if (interval < minSwapInterval)
144 interval = minSwapInterval;
145
146 if (interval > maxSwapInterval)
147 interval = maxSwapInterval;
148
149 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
150
151 return res;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800152}
153
154int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800155 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100156 ALOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800157 Mutex::Autolock lock(mMutex);
158 int buf = -1;
Mathias Agopian80727112011-05-02 19:51:12 -0700159 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopianc04f1532011-04-25 20:22:14 -0700160 mReqFormat, mReqUsage);
Mathias Agopian80727112011-05-02 19:51:12 -0700161 if (result < 0) {
Steve Block6807e592011-10-20 11:56:00 +0100162 ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700163 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
164 result);
Mathias Agopian80727112011-05-02 19:51:12 -0700165 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800166 }
167 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian80727112011-05-02 19:51:12 -0700168 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
169 freeAllBuffers();
170 }
171
172 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700173 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
174 if (result != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000175 ALOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700176 result);
177 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178 }
179 }
180 *buffer = gbuf.get();
181 return OK;
182}
183
184int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800185 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100186 ALOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800187 Mutex::Autolock lock(mMutex);
Jamie Gennis1c441402011-06-20 12:04:09 -0700188 int i = getSlotFromBufferLocked(buffer);
189 if (i < 0) {
190 return i;
191 }
192 mSurfaceTexture->cancelBuffer(i);
193 return OK;
194}
195
196int SurfaceTextureClient::getSlotFromBufferLocked(
197 android_native_buffer_t* buffer) const {
198 bool dumpedState = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800199 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis1c441402011-06-20 12:04:09 -0700200 // XXX: Dump the slots whenever we hit a NULL entry while searching for
201 // a buffer.
202 if (mSlots[i] == NULL) {
203 if (!dumpedState) {
Steve Block9d453682011-12-20 16:23:08 +0000204 ALOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
Jamie Gennis1c441402011-06-20 12:04:09 -0700205 "looking for buffer %p", i, buffer->handle);
206 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
207 if (mSlots[j] == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000208 ALOGD("getSlotFromBufferLocked: %02d: NULL", j);
Jamie Gennis1c441402011-06-20 12:04:09 -0700209 } else {
Steve Block9d453682011-12-20 16:23:08 +0000210 ALOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
Jamie Gennis1c441402011-06-20 12:04:09 -0700211 }
212 }
213 dumpedState = true;
214 }
215 }
216
217 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
218 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 }
220 }
Steve Blocke6f43dd2012-01-06 19:20:56 +0000221 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800222 return BAD_VALUE;
223}
224
225int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100226 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800227 Mutex::Autolock lock(mMutex);
228 return OK;
229}
230
231int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800232 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100233 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800234 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800235 int64_t timestamp;
236 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
237 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block6807e592011-10-20 11:56:00 +0100238 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800239 timestamp / 1000000.f);
240 } else {
241 timestamp = mTimestamp;
242 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700243 int i = getSlotFromBufferLocked(buffer);
244 if (i < 0) {
245 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800246 }
Pannag Sanketi66378c62011-09-02 17:37:29 -0700247 status_t err = mSurfaceTexture->queueBuffer(i, timestamp,
Mathias Agopian97c602c2011-07-19 15:24:46 -0700248 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700249 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000250 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700251 }
252 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800253}
254
Iliyan Malchev41abd672011-04-14 16:54:38 -0700255int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800256 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100257 ALOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700258 { // scope for the lock
259 Mutex::Autolock lock(mMutex);
260 switch (what) {
261 case NATIVE_WINDOW_FORMAT:
262 if (mReqFormat) {
263 *value = mReqFormat;
264 return NO_ERROR;
265 }
266 break;
267 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis582270d2011-08-17 18:19:00 -0700268 {
269 sp<ISurfaceComposer> composer(
270 ComposerService::getComposerService());
271 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
272 *value = 1;
273 } else {
274 *value = 0;
275 }
276 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700277 return NO_ERROR;
278 case NATIVE_WINDOW_CONCRETE_TYPE:
279 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
280 return NO_ERROR;
281 case NATIVE_WINDOW_DEFAULT_WIDTH:
282 *value = mDefaultWidth;
283 return NO_ERROR;
284 case NATIVE_WINDOW_DEFAULT_HEIGHT:
285 *value = mDefaultHeight;
286 return NO_ERROR;
287 case NATIVE_WINDOW_TRANSFORM_HINT:
288 *value = mTransformHint;
289 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700290 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800291 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700292 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800293}
294
295int SurfaceTextureClient::perform(int operation, va_list args)
296{
297 int res = NO_ERROR;
298 switch (operation) {
299 case NATIVE_WINDOW_CONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700300 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800301 break;
302 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700303 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800304 break;
305 case NATIVE_WINDOW_SET_USAGE:
306 res = dispatchSetUsage(args);
307 break;
308 case NATIVE_WINDOW_SET_CROP:
309 res = dispatchSetCrop(args);
310 break;
311 case NATIVE_WINDOW_SET_BUFFER_COUNT:
312 res = dispatchSetBufferCount(args);
313 break;
314 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
315 res = dispatchSetBuffersGeometry(args);
316 break;
317 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
318 res = dispatchSetBuffersTransform(args);
319 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800320 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
321 res = dispatchSetBuffersTimestamp(args);
322 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700323 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
324 res = dispatchSetBuffersDimensions(args);
325 break;
326 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
327 res = dispatchSetBuffersFormat(args);
328 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700329 case NATIVE_WINDOW_LOCK:
330 res = dispatchLock(args);
331 break;
332 case NATIVE_WINDOW_UNLOCK_AND_POST:
333 res = dispatchUnlockAndPost(args);
334 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700335 case NATIVE_WINDOW_SET_SCALING_MODE:
336 res = dispatchSetScalingMode(args);
337 break;
Mathias Agopian81a63352011-07-29 17:55:48 -0700338 case NATIVE_WINDOW_API_CONNECT:
339 res = dispatchConnect(args);
340 break;
341 case NATIVE_WINDOW_API_DISCONNECT:
342 res = dispatchDisconnect(args);
343 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800344 default:
345 res = NAME_NOT_FOUND;
346 break;
347 }
348 return res;
349}
350
351int SurfaceTextureClient::dispatchConnect(va_list args) {
352 int api = va_arg(args, int);
353 return connect(api);
354}
355
356int SurfaceTextureClient::dispatchDisconnect(va_list args) {
357 int api = va_arg(args, int);
358 return disconnect(api);
359}
360
361int SurfaceTextureClient::dispatchSetUsage(va_list args) {
362 int usage = va_arg(args, int);
363 return setUsage(usage);
364}
365
366int SurfaceTextureClient::dispatchSetCrop(va_list args) {
367 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
368 return setCrop(reinterpret_cast<Rect const*>(rect));
369}
370
371int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
372 size_t bufferCount = va_arg(args, size_t);
373 return setBufferCount(bufferCount);
374}
375
376int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
377 int w = va_arg(args, int);
378 int h = va_arg(args, int);
379 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700380 int err = setBuffersDimensions(w, h);
381 if (err != 0) {
382 return err;
383 }
384 return setBuffersFormat(f);
385}
386
387int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
388 int w = va_arg(args, int);
389 int h = va_arg(args, int);
390 return setBuffersDimensions(w, h);
391}
392
393int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
394 int f = va_arg(args, int);
395 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800396}
397
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700398int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
399 int m = va_arg(args, int);
400 return setScalingMode(m);
401}
402
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800403int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
404 int transform = va_arg(args, int);
405 return setBuffersTransform(transform);
406}
407
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800408int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
409 int64_t timestamp = va_arg(args, int64_t);
410 return setBuffersTimestamp(timestamp);
411}
412
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700413int SurfaceTextureClient::dispatchLock(va_list args) {
414 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
415 ARect* inOutDirtyBounds = va_arg(args, ARect*);
416 return lock(outBuffer, inOutDirtyBounds);
417}
418
419int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
420 return unlockAndPost();
421}
422
423
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800424int SurfaceTextureClient::connect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800425 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100426 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700427 Mutex::Autolock lock(mMutex);
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700428 int err = mSurfaceTexture->connect(api,
429 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700430 if (!err && api == NATIVE_WINDOW_API_CPU) {
431 mConnectedToCpu = true;
432 }
433 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800434}
435
436int SurfaceTextureClient::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800437 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100438 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700439 Mutex::Autolock lock(mMutex);
Jamie Gennis13c5ca32011-10-18 17:14:33 -0700440 freeAllBuffers();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700441 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian70e3f812011-08-25 17:03:30 -0700442 if (!err) {
Mathias Agopian70e3f812011-08-25 17:03:30 -0700443 mReqFormat = 0;
444 mReqWidth = 0;
445 mReqHeight = 0;
446 mReqUsage = 0;
447 if (api == NATIVE_WINDOW_API_CPU) {
448 mConnectedToCpu = false;
449 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700450 }
451 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800452}
453
454int SurfaceTextureClient::setUsage(uint32_t reqUsage)
455{
Steve Block6807e592011-10-20 11:56:00 +0100456 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800457 Mutex::Autolock lock(mMutex);
458 mReqUsage = reqUsage;
459 return OK;
460}
461
462int SurfaceTextureClient::setCrop(Rect const* rect)
463{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800464 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100465 ALOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800466 Mutex::Autolock lock(mMutex);
467
Jamie Gennis68f91272011-01-28 18:21:54 -0800468 Rect realRect;
469 if (rect == NULL || rect->isEmpty()) {
470 realRect = Rect(0, 0);
471 } else {
472 realRect = *rect;
473 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800474
475 status_t err = mSurfaceTexture->setCrop(*rect);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000476 ALOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800477
478 return err;
479}
480
481int SurfaceTextureClient::setBufferCount(int bufferCount)
482{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800483 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100484 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800485 Mutex::Autolock lock(mMutex);
486
487 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000488 ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800489 bufferCount, strerror(-err));
490
491 if (err == NO_ERROR) {
492 freeAllBuffers();
493 }
494
495 return err;
496}
497
Jamie Gennisbee205f2011-07-01 13:12:07 -0700498int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800499{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800500 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100501 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800502 Mutex::Autolock lock(mMutex);
503
Jamie Gennisbee205f2011-07-01 13:12:07 -0700504 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800505 return BAD_VALUE;
506
507 if ((w && !h) || (!w && h))
508 return BAD_VALUE;
509
510 mReqWidth = w;
511 mReqHeight = h;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800512
Jamie Gennis68f91272011-01-28 18:21:54 -0800513 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
Steve Blocke6f43dd2012-01-06 19:20:56 +0000514 ALOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis68f91272011-01-28 18:21:54 -0800515
516 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800517}
518
Jamie Gennisbee205f2011-07-01 13:12:07 -0700519int SurfaceTextureClient::setBuffersFormat(int format)
520{
Steve Block6807e592011-10-20 11:56:00 +0100521 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennisbee205f2011-07-01 13:12:07 -0700522 Mutex::Autolock lock(mMutex);
523
524 if (format<0)
525 return BAD_VALUE;
526
527 mReqFormat = format;
528
529 return NO_ERROR;
530}
531
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700532int SurfaceTextureClient::setScalingMode(int mode)
533{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800534 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100535 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700536 Mutex::Autolock lock(mMutex);
537 // mode is validated on the server
538 status_t err = mSurfaceTexture->setScalingMode(mode);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000539 ALOGE_IF(err, "ISurfaceTexture::setScalingMode(%d) returned %s",
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700540 mode, strerror(-err));
541
542 return err;
543}
544
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800545int SurfaceTextureClient::setBuffersTransform(int transform)
546{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800547 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100548 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800549 Mutex::Autolock lock(mMutex);
550 status_t err = mSurfaceTexture->setTransform(transform);
551 return err;
552}
553
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800554int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
555{
Steve Block6807e592011-10-20 11:56:00 +0100556 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800557 Mutex::Autolock lock(mMutex);
558 mTimestamp = timestamp;
559 return NO_ERROR;
560}
561
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800562void SurfaceTextureClient::freeAllBuffers() {
563 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
564 mSlots[i] = 0;
565 }
566}
567
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700568// ----------------------------------------------------------------------
569// the lock/unlock APIs must be used from the same thread
570
571static status_t copyBlt(
572 const sp<GraphicBuffer>& dst,
573 const sp<GraphicBuffer>& src,
574 const Region& reg)
575{
576 // src and dst with, height and format must be identical. no verification
577 // is done here.
578 status_t err;
579 uint8_t const * src_bits = NULL;
580 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000581 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700582
583 uint8_t* dst_bits = NULL;
584 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000585 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700586
587 Region::const_iterator head(reg.begin());
588 Region::const_iterator tail(reg.end());
589 if (head != tail && src_bits && dst_bits) {
590 const size_t bpp = bytesPerPixel(src->format);
591 const size_t dbpr = dst->stride * bpp;
592 const size_t sbpr = src->stride * bpp;
593
594 while (head != tail) {
595 const Rect& r(*head++);
596 ssize_t h = r.height();
597 if (h <= 0) continue;
598 size_t size = r.width() * bpp;
599 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
600 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
601 if (dbpr==sbpr && size==sbpr) {
602 size *= h;
603 h = 1;
604 }
605 do {
606 memcpy(d, s, size);
607 d += dbpr;
608 s += sbpr;
609 } while (--h > 0);
610 }
611 }
612
613 if (src_bits)
614 src->unlock();
615
616 if (dst_bits)
617 dst->unlock();
618
619 return err;
620}
621
622// ----------------------------------------------------------------------------
623
624status_t SurfaceTextureClient::lock(
625 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
626{
627 if (mLockedBuffer != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000628 ALOGE("Surface::lock failed, already locked");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700629 return INVALID_OPERATION;
630 }
631
632 if (!mConnectedToCpu) {
633 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
634 if (err) {
635 return err;
636 }
637 // we're intending to do software rendering from this point
638 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
639 }
640
641 ANativeWindowBuffer* out;
642 status_t err = dequeueBuffer(&out);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000643 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700644 if (err == NO_ERROR) {
645 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
646 err = lockBuffer(backBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000647 ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700648 backBuffer->handle, strerror(-err));
649 if (err == NO_ERROR) {
650 const Rect bounds(backBuffer->width, backBuffer->height);
651
652 Region newDirtyRegion;
653 if (inOutDirtyBounds) {
654 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
655 newDirtyRegion.andSelf(bounds);
656 } else {
657 newDirtyRegion.set(bounds);
658 }
659
660 // figure out if we can copy the frontbuffer back
661 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
662 const bool canCopyBack = (frontBuffer != 0 &&
663 backBuffer->width == frontBuffer->width &&
664 backBuffer->height == frontBuffer->height &&
665 backBuffer->format == frontBuffer->format);
666
667 if (canCopyBack) {
668 // copy the area that is invalid and not repainted this round
669 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
670 if (!copyback.isEmpty())
671 copyBlt(backBuffer, frontBuffer, copyback);
672 } else {
673 // if we can't copy-back anything, modify the user's dirty
674 // region to make sure they redraw the whole buffer
675 newDirtyRegion.set(bounds);
676 }
677
678 // keep track of the are of the buffer that is "clean"
679 // (ie: that will be redrawn)
680 mOldDirtyRegion = newDirtyRegion;
681
682 if (inOutDirtyBounds) {
683 *inOutDirtyBounds = newDirtyRegion.getBounds();
684 }
685
686 void* vaddr;
687 status_t res = backBuffer->lock(
688 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
689 newDirtyRegion.bounds(), &vaddr);
690
Steve Block32397c12012-01-05 23:22:43 +0000691 ALOGW_IF(res, "failed locking buffer (handle = %p)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700692 backBuffer->handle);
693
694 mLockedBuffer = backBuffer;
695 outBuffer->width = backBuffer->width;
696 outBuffer->height = backBuffer->height;
697 outBuffer->stride = backBuffer->stride;
698 outBuffer->format = backBuffer->format;
699 outBuffer->bits = vaddr;
700 }
701 }
702 return err;
703}
704
705status_t SurfaceTextureClient::unlockAndPost()
706{
707 if (mLockedBuffer == 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000708 ALOGE("Surface::unlockAndPost failed, no locked buffer");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700709 return INVALID_OPERATION;
710 }
711
712 status_t err = mLockedBuffer->unlock();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000713 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700714
715 err = queueBuffer(mLockedBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000716 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700717 mLockedBuffer->handle, strerror(-err));
718
719 mPostedBuffer = mLockedBuffer;
720 mLockedBuffer = 0;
721 return err;
722}
723
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800724}; // namespace android