blob: d18a3d48e56bf1649d5eab5cb5d877686eb3f20f [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
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080021#include <utils/Log.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080022#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080023
Mathias Agopian90ac7992012-02-25 18:48:35 -080024#include <gui/ISurfaceComposer.h>
25#include <gui/SurfaceComposerClient.h>
26#include <gui/SurfaceTextureClient.h>
27
Mathias Agopian41f673c2011-11-17 17:48:35 -080028#include <private/gui/ComposerService.h>
29
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080030namespace android {
31
32SurfaceTextureClient::SurfaceTextureClient(
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070033 const sp<ISurfaceTexture>& surfaceTexture)
34{
35 SurfaceTextureClient::init();
36 SurfaceTextureClient::setISurfaceTexture(surfaceTexture);
37}
38
Daniel Lamb2675792012-02-23 14:35:13 -080039// see SurfaceTextureClient.h
40SurfaceTextureClient::SurfaceTextureClient(const
41 sp<SurfaceTexture>& surfaceTexture)
42{
43 SurfaceTextureClient::init();
44 SurfaceTextureClient::setISurfaceTexture(surfaceTexture->getBufferQueue());
45}
46
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070047SurfaceTextureClient::SurfaceTextureClient() {
48 SurfaceTextureClient::init();
49}
50
Mathias Agopiana36bcd52011-11-17 18:46:09 -080051SurfaceTextureClient::~SurfaceTextureClient() {
52 if (mConnectedToCpu) {
53 SurfaceTextureClient::disconnect(NATIVE_WINDOW_API_CPU);
54 }
55}
56
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070057void SurfaceTextureClient::init() {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080058 // Initialize the ANativeWindow function pointers.
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070059 ANativeWindow::setSwapInterval = hook_setSwapInterval;
60 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
61 ANativeWindow::cancelBuffer = hook_cancelBuffer;
62 ANativeWindow::lockBuffer = hook_lockBuffer;
63 ANativeWindow::queueBuffer = hook_queueBuffer;
64 ANativeWindow::query = hook_query;
65 ANativeWindow::perform = hook_perform;
Jamie Gennis1b20cde2011-02-02 15:31:47 -080066
Mathias Agopian80727112011-05-02 19:51:12 -070067 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
68 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
69
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070070 mReqWidth = 0;
71 mReqHeight = 0;
72 mReqFormat = 0;
73 mReqUsage = 0;
74 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -070075 mDefaultWidth = 0;
76 mDefaultHeight = 0;
77 mTransformHint = 0;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070078 mConnectedToCpu = false;
79}
80
81void SurfaceTextureClient::setISurfaceTexture(
82 const sp<ISurfaceTexture>& surfaceTexture)
83{
84 mSurfaceTexture = surfaceTexture;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080085}
86
Jamie Gennisbae774e2011-03-14 15:08:53 -070087sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
88 return mSurfaceTexture;
89}
90
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070091int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080092 SurfaceTextureClient* c = getSelf(window);
93 return c->setSwapInterval(interval);
94}
95
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070096int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070097 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080098 SurfaceTextureClient* c = getSelf(window);
99 return c->dequeueBuffer(buffer);
100}
101
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700102int SurfaceTextureClient::hook_cancelBuffer(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->cancelBuffer(buffer);
106}
107
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700108int SurfaceTextureClient::hook_lockBuffer(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->lockBuffer(buffer);
112}
113
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700114int SurfaceTextureClient::hook_queueBuffer(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->queueBuffer(buffer);
118}
119
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700120int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev41abd672011-04-14 16:54:38 -0700121 int what, int* value) {
122 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800123 return c->query(what, value);
124}
125
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700126int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800127 va_list args;
128 va_start(args, operation);
129 SurfaceTextureClient* c = getSelf(window);
130 return c->perform(operation, args);
131}
132
133int SurfaceTextureClient::setSwapInterval(int interval) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800134 ATRACE_CALL();
Mathias Agopian80727112011-05-02 19:51:12 -0700135 // EGL specification states:
136 // interval is silently clamped to minimum and maximum implementation
137 // dependent values before being stored.
138 // Although we don't have to, we apply the same logic here.
139
140 if (interval < minSwapInterval)
141 interval = minSwapInterval;
142
143 if (interval > maxSwapInterval)
144 interval = maxSwapInterval;
145
146 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
147
148 return res;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800149}
150
151int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800152 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100153 ALOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800154 Mutex::Autolock lock(mMutex);
155 int buf = -1;
Mathias Agopian80727112011-05-02 19:51:12 -0700156 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopianc04f1532011-04-25 20:22:14 -0700157 mReqFormat, mReqUsage);
Mathias Agopian80727112011-05-02 19:51:12 -0700158 if (result < 0) {
Steve Block6807e592011-10-20 11:56:00 +0100159 ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700160 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
161 result);
Mathias Agopian80727112011-05-02 19:51:12 -0700162 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800163 }
164 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian80727112011-05-02 19:51:12 -0700165 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
166 freeAllBuffers();
167 }
168
169 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700170 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
171 if (result != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000172 ALOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700173 result);
174 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800175 }
176 }
177 *buffer = gbuf.get();
178 return OK;
179}
180
181int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800182 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100183 ALOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800184 Mutex::Autolock lock(mMutex);
Jamie Gennis1c441402011-06-20 12:04:09 -0700185 int i = getSlotFromBufferLocked(buffer);
186 if (i < 0) {
187 return i;
188 }
189 mSurfaceTexture->cancelBuffer(i);
190 return OK;
191}
192
193int SurfaceTextureClient::getSlotFromBufferLocked(
194 android_native_buffer_t* buffer) const {
195 bool dumpedState = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800196 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis1c441402011-06-20 12:04:09 -0700197 // XXX: Dump the slots whenever we hit a NULL entry while searching for
198 // a buffer.
199 if (mSlots[i] == NULL) {
200 if (!dumpedState) {
Steve Block9d453682011-12-20 16:23:08 +0000201 ALOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
Jamie Gennis1c441402011-06-20 12:04:09 -0700202 "looking for buffer %p", i, buffer->handle);
203 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
204 if (mSlots[j] == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000205 ALOGD("getSlotFromBufferLocked: %02d: NULL", j);
Jamie Gennis1c441402011-06-20 12:04:09 -0700206 } else {
Steve Block9d453682011-12-20 16:23:08 +0000207 ALOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
Jamie Gennis1c441402011-06-20 12:04:09 -0700208 }
209 }
210 dumpedState = true;
211 }
212 }
213
214 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
215 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800216 }
217 }
Steve Blocke6f43dd2012-01-06 19:20:56 +0000218 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 return BAD_VALUE;
220}
221
222int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100223 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800224 Mutex::Autolock lock(mMutex);
225 return OK;
226}
227
228int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800229 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100230 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800231 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800232 int64_t timestamp;
233 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
234 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block6807e592011-10-20 11:56:00 +0100235 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800236 timestamp / 1000000.f);
237 } else {
238 timestamp = mTimestamp;
239 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700240 int i = getSlotFromBufferLocked(buffer);
241 if (i < 0) {
242 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800243 }
Pannag Sanketi66378c62011-09-02 17:37:29 -0700244 status_t err = mSurfaceTexture->queueBuffer(i, timestamp,
Mathias Agopian97c602c2011-07-19 15:24:46 -0700245 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700246 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000247 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700248 }
249 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800250}
251
Iliyan Malchev41abd672011-04-14 16:54:38 -0700252int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800253 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100254 ALOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700255 { // scope for the lock
256 Mutex::Autolock lock(mMutex);
257 switch (what) {
258 case NATIVE_WINDOW_FORMAT:
259 if (mReqFormat) {
260 *value = mReqFormat;
261 return NO_ERROR;
262 }
263 break;
264 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis582270d2011-08-17 18:19:00 -0700265 {
266 sp<ISurfaceComposer> composer(
267 ComposerService::getComposerService());
268 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
269 *value = 1;
270 } else {
271 *value = 0;
272 }
273 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700274 return NO_ERROR;
275 case NATIVE_WINDOW_CONCRETE_TYPE:
276 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
277 return NO_ERROR;
278 case NATIVE_WINDOW_DEFAULT_WIDTH:
279 *value = mDefaultWidth;
280 return NO_ERROR;
281 case NATIVE_WINDOW_DEFAULT_HEIGHT:
282 *value = mDefaultHeight;
283 return NO_ERROR;
284 case NATIVE_WINDOW_TRANSFORM_HINT:
285 *value = mTransformHint;
286 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700287 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800288 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700289 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800290}
291
292int SurfaceTextureClient::perform(int operation, va_list args)
293{
294 int res = NO_ERROR;
295 switch (operation) {
296 case NATIVE_WINDOW_CONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700297 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800298 break;
299 case NATIVE_WINDOW_DISCONNECT:
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_SET_USAGE:
303 res = dispatchSetUsage(args);
304 break;
305 case NATIVE_WINDOW_SET_CROP:
306 res = dispatchSetCrop(args);
307 break;
308 case NATIVE_WINDOW_SET_BUFFER_COUNT:
309 res = dispatchSetBufferCount(args);
310 break;
311 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
312 res = dispatchSetBuffersGeometry(args);
313 break;
314 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
315 res = dispatchSetBuffersTransform(args);
316 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800317 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
318 res = dispatchSetBuffersTimestamp(args);
319 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700320 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
321 res = dispatchSetBuffersDimensions(args);
322 break;
323 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
324 res = dispatchSetBuffersFormat(args);
325 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700326 case NATIVE_WINDOW_LOCK:
327 res = dispatchLock(args);
328 break;
329 case NATIVE_WINDOW_UNLOCK_AND_POST:
330 res = dispatchUnlockAndPost(args);
331 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700332 case NATIVE_WINDOW_SET_SCALING_MODE:
333 res = dispatchSetScalingMode(args);
334 break;
Mathias Agopian81a63352011-07-29 17:55:48 -0700335 case NATIVE_WINDOW_API_CONNECT:
336 res = dispatchConnect(args);
337 break;
338 case NATIVE_WINDOW_API_DISCONNECT:
339 res = dispatchDisconnect(args);
340 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800341 default:
342 res = NAME_NOT_FOUND;
343 break;
344 }
345 return res;
346}
347
348int SurfaceTextureClient::dispatchConnect(va_list args) {
349 int api = va_arg(args, int);
350 return connect(api);
351}
352
353int SurfaceTextureClient::dispatchDisconnect(va_list args) {
354 int api = va_arg(args, int);
355 return disconnect(api);
356}
357
358int SurfaceTextureClient::dispatchSetUsage(va_list args) {
359 int usage = va_arg(args, int);
360 return setUsage(usage);
361}
362
363int SurfaceTextureClient::dispatchSetCrop(va_list args) {
364 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
365 return setCrop(reinterpret_cast<Rect const*>(rect));
366}
367
368int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
369 size_t bufferCount = va_arg(args, size_t);
370 return setBufferCount(bufferCount);
371}
372
373int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
374 int w = va_arg(args, int);
375 int h = va_arg(args, int);
376 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700377 int err = setBuffersDimensions(w, h);
378 if (err != 0) {
379 return err;
380 }
381 return setBuffersFormat(f);
382}
383
384int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
385 int w = va_arg(args, int);
386 int h = va_arg(args, int);
387 return setBuffersDimensions(w, h);
388}
389
390int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
391 int f = va_arg(args, int);
392 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800393}
394
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700395int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
396 int m = va_arg(args, int);
397 return setScalingMode(m);
398}
399
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800400int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
401 int transform = va_arg(args, int);
402 return setBuffersTransform(transform);
403}
404
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800405int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
406 int64_t timestamp = va_arg(args, int64_t);
407 return setBuffersTimestamp(timestamp);
408}
409
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700410int SurfaceTextureClient::dispatchLock(va_list args) {
411 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
412 ARect* inOutDirtyBounds = va_arg(args, ARect*);
413 return lock(outBuffer, inOutDirtyBounds);
414}
415
416int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
417 return unlockAndPost();
418}
419
420
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800421int SurfaceTextureClient::connect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800422 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100423 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700424 Mutex::Autolock lock(mMutex);
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700425 int err = mSurfaceTexture->connect(api,
426 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700427 if (!err && api == NATIVE_WINDOW_API_CPU) {
428 mConnectedToCpu = true;
429 }
430 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800431}
432
433int SurfaceTextureClient::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800434 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100435 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700436 Mutex::Autolock lock(mMutex);
Jamie Gennis13c5ca32011-10-18 17:14:33 -0700437 freeAllBuffers();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700438 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian70e3f812011-08-25 17:03:30 -0700439 if (!err) {
Mathias Agopian70e3f812011-08-25 17:03:30 -0700440 mReqFormat = 0;
441 mReqWidth = 0;
442 mReqHeight = 0;
443 mReqUsage = 0;
444 if (api == NATIVE_WINDOW_API_CPU) {
445 mConnectedToCpu = false;
446 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700447 }
448 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800449}
450
451int SurfaceTextureClient::setUsage(uint32_t reqUsage)
452{
Steve Block6807e592011-10-20 11:56:00 +0100453 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800454 Mutex::Autolock lock(mMutex);
455 mReqUsage = reqUsage;
456 return OK;
457}
458
459int SurfaceTextureClient::setCrop(Rect const* rect)
460{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800461 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100462 ALOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800463 Mutex::Autolock lock(mMutex);
464
Jamie Gennis68f91272011-01-28 18:21:54 -0800465 Rect realRect;
466 if (rect == NULL || rect->isEmpty()) {
467 realRect = Rect(0, 0);
468 } else {
469 realRect = *rect;
470 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800471
472 status_t err = mSurfaceTexture->setCrop(*rect);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000473 ALOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800474
475 return err;
476}
477
478int SurfaceTextureClient::setBufferCount(int bufferCount)
479{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800480 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100481 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800482 Mutex::Autolock lock(mMutex);
483
484 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000485 ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800486 bufferCount, strerror(-err));
487
488 if (err == NO_ERROR) {
489 freeAllBuffers();
490 }
491
492 return err;
493}
494
Jamie Gennisbee205f2011-07-01 13:12:07 -0700495int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800496{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800497 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100498 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800499 Mutex::Autolock lock(mMutex);
500
Jamie Gennisbee205f2011-07-01 13:12:07 -0700501 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800502 return BAD_VALUE;
503
504 if ((w && !h) || (!w && h))
505 return BAD_VALUE;
506
507 mReqWidth = w;
508 mReqHeight = h;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800509
Jamie Gennis68f91272011-01-28 18:21:54 -0800510 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
Steve Blocke6f43dd2012-01-06 19:20:56 +0000511 ALOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis68f91272011-01-28 18:21:54 -0800512
513 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800514}
515
Jamie Gennisbee205f2011-07-01 13:12:07 -0700516int SurfaceTextureClient::setBuffersFormat(int format)
517{
Steve Block6807e592011-10-20 11:56:00 +0100518 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennisbee205f2011-07-01 13:12:07 -0700519 Mutex::Autolock lock(mMutex);
520
521 if (format<0)
522 return BAD_VALUE;
523
524 mReqFormat = format;
525
526 return NO_ERROR;
527}
528
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700529int SurfaceTextureClient::setScalingMode(int mode)
530{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800531 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100532 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700533 Mutex::Autolock lock(mMutex);
534 // mode is validated on the server
535 status_t err = mSurfaceTexture->setScalingMode(mode);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000536 ALOGE_IF(err, "ISurfaceTexture::setScalingMode(%d) returned %s",
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700537 mode, strerror(-err));
538
539 return err;
540}
541
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800542int SurfaceTextureClient::setBuffersTransform(int transform)
543{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800544 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100545 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800546 Mutex::Autolock lock(mMutex);
547 status_t err = mSurfaceTexture->setTransform(transform);
548 return err;
549}
550
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800551int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
552{
Steve Block6807e592011-10-20 11:56:00 +0100553 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800554 Mutex::Autolock lock(mMutex);
555 mTimestamp = timestamp;
556 return NO_ERROR;
557}
558
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800559void SurfaceTextureClient::freeAllBuffers() {
560 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
561 mSlots[i] = 0;
562 }
563}
564
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700565// ----------------------------------------------------------------------
566// the lock/unlock APIs must be used from the same thread
567
568static status_t copyBlt(
569 const sp<GraphicBuffer>& dst,
570 const sp<GraphicBuffer>& src,
571 const Region& reg)
572{
573 // src and dst with, height and format must be identical. no verification
574 // is done here.
575 status_t err;
576 uint8_t const * src_bits = NULL;
577 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000578 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700579
580 uint8_t* dst_bits = NULL;
581 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000582 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700583
584 Region::const_iterator head(reg.begin());
585 Region::const_iterator tail(reg.end());
586 if (head != tail && src_bits && dst_bits) {
587 const size_t bpp = bytesPerPixel(src->format);
588 const size_t dbpr = dst->stride * bpp;
589 const size_t sbpr = src->stride * bpp;
590
591 while (head != tail) {
592 const Rect& r(*head++);
593 ssize_t h = r.height();
594 if (h <= 0) continue;
595 size_t size = r.width() * bpp;
596 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
597 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
598 if (dbpr==sbpr && size==sbpr) {
599 size *= h;
600 h = 1;
601 }
602 do {
603 memcpy(d, s, size);
604 d += dbpr;
605 s += sbpr;
606 } while (--h > 0);
607 }
608 }
609
610 if (src_bits)
611 src->unlock();
612
613 if (dst_bits)
614 dst->unlock();
615
616 return err;
617}
618
619// ----------------------------------------------------------------------------
620
621status_t SurfaceTextureClient::lock(
622 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
623{
624 if (mLockedBuffer != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000625 ALOGE("Surface::lock failed, already locked");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700626 return INVALID_OPERATION;
627 }
628
629 if (!mConnectedToCpu) {
630 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
631 if (err) {
632 return err;
633 }
634 // we're intending to do software rendering from this point
635 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
636 }
637
638 ANativeWindowBuffer* out;
639 status_t err = dequeueBuffer(&out);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000640 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700641 if (err == NO_ERROR) {
642 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
643 err = lockBuffer(backBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000644 ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700645 backBuffer->handle, strerror(-err));
646 if (err == NO_ERROR) {
647 const Rect bounds(backBuffer->width, backBuffer->height);
648
649 Region newDirtyRegion;
650 if (inOutDirtyBounds) {
651 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
652 newDirtyRegion.andSelf(bounds);
653 } else {
654 newDirtyRegion.set(bounds);
655 }
656
657 // figure out if we can copy the frontbuffer back
658 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
659 const bool canCopyBack = (frontBuffer != 0 &&
660 backBuffer->width == frontBuffer->width &&
661 backBuffer->height == frontBuffer->height &&
662 backBuffer->format == frontBuffer->format);
663
664 if (canCopyBack) {
665 // copy the area that is invalid and not repainted this round
666 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
667 if (!copyback.isEmpty())
668 copyBlt(backBuffer, frontBuffer, copyback);
669 } else {
670 // if we can't copy-back anything, modify the user's dirty
671 // region to make sure they redraw the whole buffer
672 newDirtyRegion.set(bounds);
673 }
674
675 // keep track of the are of the buffer that is "clean"
676 // (ie: that will be redrawn)
677 mOldDirtyRegion = newDirtyRegion;
678
679 if (inOutDirtyBounds) {
680 *inOutDirtyBounds = newDirtyRegion.getBounds();
681 }
682
683 void* vaddr;
684 status_t res = backBuffer->lock(
685 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
686 newDirtyRegion.bounds(), &vaddr);
687
Steve Block32397c12012-01-05 23:22:43 +0000688 ALOGW_IF(res, "failed locking buffer (handle = %p)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700689 backBuffer->handle);
690
691 mLockedBuffer = backBuffer;
692 outBuffer->width = backBuffer->width;
693 outBuffer->height = backBuffer->height;
694 outBuffer->stride = backBuffer->stride;
695 outBuffer->format = backBuffer->format;
696 outBuffer->bits = vaddr;
697 }
698 }
699 return err;
700}
701
702status_t SurfaceTextureClient::unlockAndPost()
703{
704 if (mLockedBuffer == 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000705 ALOGE("Surface::unlockAndPost failed, no locked buffer");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700706 return INVALID_OPERATION;
707 }
708
709 status_t err = mLockedBuffer->unlock();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000710 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700711
712 err = queueBuffer(mLockedBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000713 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700714 mLockedBuffer->handle, strerror(-err));
715
716 mPostedBuffer = mLockedBuffer;
717 mLockedBuffer = 0;
718 return err;
719}
720
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800721}; // namespace android