blob: f88dcaf02abe8244152f62485f558d4e005e6cf9 [file] [log] [blame]
Jamie Gennis68e4a7a2010-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 Gennisa85ca372012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise8d0e8a2011-01-12 20:22:41 -080019//#define LOG_NDEBUG 0
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080020
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080021#include <utils/Log.h>
Jamie Gennisa85ca372012-02-23 19:27:23 -080022#include <utils/Trace.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080023
Mathias Agopian8335f1c2012-02-25 18:48:35 -080024#include <gui/ISurfaceComposer.h>
25#include <gui/SurfaceComposerClient.h>
26#include <gui/SurfaceTextureClient.h>
27
Mathias Agopian3f157002011-11-17 17:48:35 -080028#include <private/gui/ComposerService.h>
29
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080030namespace android {
31
32SurfaceTextureClient::SurfaceTextureClient(
Mathias Agopian949be322011-07-13 17:39:11 -070033 const sp<ISurfaceTexture>& surfaceTexture)
34{
35 SurfaceTextureClient::init();
36 SurfaceTextureClient::setISurfaceTexture(surfaceTexture);
37}
38
39SurfaceTextureClient::SurfaceTextureClient() {
40 SurfaceTextureClient::init();
41}
42
Mathias Agopian90cbbd12011-11-17 18:46:09 -080043SurfaceTextureClient::~SurfaceTextureClient() {
44 if (mConnectedToCpu) {
45 SurfaceTextureClient::disconnect(NATIVE_WINDOW_API_CPU);
46 }
47}
48
Mathias Agopian949be322011-07-13 17:39:11 -070049void SurfaceTextureClient::init() {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080050 // Initialize the ANativeWindow function pointers.
Mathias Agopian949be322011-07-13 17:39:11 -070051 ANativeWindow::setSwapInterval = hook_setSwapInterval;
52 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
53 ANativeWindow::cancelBuffer = hook_cancelBuffer;
54 ANativeWindow::lockBuffer = hook_lockBuffer;
55 ANativeWindow::queueBuffer = hook_queueBuffer;
56 ANativeWindow::query = hook_query;
57 ANativeWindow::perform = hook_perform;
Jamie Gennis83bac212011-02-02 15:31:47 -080058
Mathias Agopian402ff242011-05-02 19:51:12 -070059 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
60 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
61
Mathias Agopian949be322011-07-13 17:39:11 -070062 mReqWidth = 0;
63 mReqHeight = 0;
64 mReqFormat = 0;
65 mReqUsage = 0;
66 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Mathias Agopian45b63dd2011-07-21 14:50:29 -070067 mDefaultWidth = 0;
68 mDefaultHeight = 0;
69 mTransformHint = 0;
Mathias Agopian949be322011-07-13 17:39:11 -070070 mConnectedToCpu = false;
71}
72
73void SurfaceTextureClient::setISurfaceTexture(
74 const sp<ISurfaceTexture>& surfaceTexture)
75{
76 mSurfaceTexture = surfaceTexture;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080077}
78
Jamie Gennisf95a9f02011-03-14 15:08:53 -070079sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
80 return mSurfaceTexture;
81}
82
Mathias Agopian949be322011-07-13 17:39:11 -070083int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080084 SurfaceTextureClient* c = getSelf(window);
85 return c->setSwapInterval(interval);
86}
87
Mathias Agopian949be322011-07-13 17:39:11 -070088int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070089 ANativeWindowBuffer** buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080090 SurfaceTextureClient* c = getSelf(window);
91 return c->dequeueBuffer(buffer);
92}
93
Mathias Agopian949be322011-07-13 17:39:11 -070094int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070095 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080096 SurfaceTextureClient* c = getSelf(window);
97 return c->cancelBuffer(buffer);
98}
99
Mathias Agopian949be322011-07-13 17:39:11 -0700100int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700101 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800102 SurfaceTextureClient* c = getSelf(window);
103 return c->lockBuffer(buffer);
104}
105
Mathias Agopian949be322011-07-13 17:39:11 -0700106int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700107 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800108 SurfaceTextureClient* c = getSelf(window);
109 return c->queueBuffer(buffer);
110}
111
Mathias Agopian949be322011-07-13 17:39:11 -0700112int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700113 int what, int* value) {
114 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800115 return c->query(what, value);
116}
117
Mathias Agopian949be322011-07-13 17:39:11 -0700118int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800119 va_list args;
120 va_start(args, operation);
121 SurfaceTextureClient* c = getSelf(window);
122 return c->perform(operation, args);
123}
124
125int SurfaceTextureClient::setSwapInterval(int interval) {
Jamie Gennisa85ca372012-02-23 19:27:23 -0800126 ATRACE_CALL();
Mathias Agopian402ff242011-05-02 19:51:12 -0700127 // EGL specification states:
128 // interval is silently clamped to minimum and maximum implementation
129 // dependent values before being stored.
130 // Although we don't have to, we apply the same logic here.
131
132 if (interval < minSwapInterval)
133 interval = minSwapInterval;
134
135 if (interval > maxSwapInterval)
136 interval = maxSwapInterval;
137
138 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
139
140 return res;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800141}
142
143int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennisa85ca372012-02-23 19:27:23 -0800144 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100145 ALOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800146 Mutex::Autolock lock(mMutex);
147 int buf = -1;
Mathias Agopian402ff242011-05-02 19:51:12 -0700148 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopian0297dca2011-04-25 20:22:14 -0700149 mReqFormat, mReqUsage);
Mathias Agopian402ff242011-05-02 19:51:12 -0700150 if (result < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100151 ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennisa2187152011-05-19 13:33:00 -0700152 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
153 result);
Mathias Agopian402ff242011-05-02 19:51:12 -0700154 return result;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800155 }
156 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian402ff242011-05-02 19:51:12 -0700157 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
158 freeAllBuffers();
159 }
160
161 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700162 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
163 if (result != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +0000164 ALOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700165 result);
166 return result;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800167 }
168 }
169 *buffer = gbuf.get();
170 return OK;
171}
172
173int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennisa85ca372012-02-23 19:27:23 -0800174 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100175 ALOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800176 Mutex::Autolock lock(mMutex);
Jamie Gennisaff74052011-06-20 12:04:09 -0700177 int i = getSlotFromBufferLocked(buffer);
178 if (i < 0) {
179 return i;
180 }
181 mSurfaceTexture->cancelBuffer(i);
182 return OK;
183}
184
185int SurfaceTextureClient::getSlotFromBufferLocked(
186 android_native_buffer_t* buffer) const {
187 bool dumpedState = false;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800188 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennisaff74052011-06-20 12:04:09 -0700189 // XXX: Dump the slots whenever we hit a NULL entry while searching for
190 // a buffer.
191 if (mSlots[i] == NULL) {
192 if (!dumpedState) {
Steve Block5baa3a62011-12-20 16:23:08 +0000193 ALOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
Jamie Gennisaff74052011-06-20 12:04:09 -0700194 "looking for buffer %p", i, buffer->handle);
195 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
196 if (mSlots[j] == NULL) {
Steve Block5baa3a62011-12-20 16:23:08 +0000197 ALOGD("getSlotFromBufferLocked: %02d: NULL", j);
Jamie Gennisaff74052011-06-20 12:04:09 -0700198 } else {
Steve Block5baa3a62011-12-20 16:23:08 +0000199 ALOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
Jamie Gennisaff74052011-06-20 12:04:09 -0700200 }
201 }
202 dumpedState = true;
203 }
204 }
205
206 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
207 return i;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800208 }
209 }
Steve Block3762c312012-01-06 19:20:56 +0000210 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800211 return BAD_VALUE;
212}
213
214int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block71f2cf12011-10-20 11:56:00 +0100215 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800216 Mutex::Autolock lock(mMutex);
217 return OK;
218}
219
220int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennisa85ca372012-02-23 19:27:23 -0800221 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100222 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800223 Mutex::Autolock lock(mMutex);
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800224 int64_t timestamp;
225 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
226 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block71f2cf12011-10-20 11:56:00 +0100227 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800228 timestamp / 1000000.f);
229 } else {
230 timestamp = mTimestamp;
231 }
Jamie Gennisaff74052011-06-20 12:04:09 -0700232 int i = getSlotFromBufferLocked(buffer);
233 if (i < 0) {
234 return i;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800235 }
Pannag Sanketie0617952011-09-02 17:37:29 -0700236 status_t err = mSurfaceTexture->queueBuffer(i, timestamp,
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700237 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketie0617952011-09-02 17:37:29 -0700238 if (err != OK) {
Steve Block3762c312012-01-06 19:20:56 +0000239 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
Pannag Sanketie0617952011-09-02 17:37:29 -0700240 }
241 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800242}
243
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700244int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennisa85ca372012-02-23 19:27:23 -0800245 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100246 ALOGV("SurfaceTextureClient::query");
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700247 { // scope for the lock
248 Mutex::Autolock lock(mMutex);
249 switch (what) {
250 case NATIVE_WINDOW_FORMAT:
251 if (mReqFormat) {
252 *value = mReqFormat;
253 return NO_ERROR;
254 }
255 break;
256 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700257 {
258 sp<ISurfaceComposer> composer(
259 ComposerService::getComposerService());
260 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
261 *value = 1;
262 } else {
263 *value = 0;
264 }
265 }
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700266 return NO_ERROR;
267 case NATIVE_WINDOW_CONCRETE_TYPE:
268 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
269 return NO_ERROR;
270 case NATIVE_WINDOW_DEFAULT_WIDTH:
271 *value = mDefaultWidth;
272 return NO_ERROR;
273 case NATIVE_WINDOW_DEFAULT_HEIGHT:
274 *value = mDefaultHeight;
275 return NO_ERROR;
276 case NATIVE_WINDOW_TRANSFORM_HINT:
277 *value = mTransformHint;
278 return NO_ERROR;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700279 }
Jamie Gennis96dcc972011-02-27 14:10:20 -0800280 }
Mathias Agopianed3894c2011-04-20 14:20:59 -0700281 return mSurfaceTexture->query(what, value);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800282}
283
284int SurfaceTextureClient::perform(int operation, va_list args)
285{
286 int res = NO_ERROR;
287 switch (operation) {
288 case NATIVE_WINDOW_CONNECT:
Mathias Agopian982d2da2011-07-29 17:55:48 -0700289 // deprecated. must return NO_ERROR.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800290 break;
291 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian982d2da2011-07-29 17:55:48 -0700292 // deprecated. must return NO_ERROR.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800293 break;
294 case NATIVE_WINDOW_SET_USAGE:
295 res = dispatchSetUsage(args);
296 break;
297 case NATIVE_WINDOW_SET_CROP:
298 res = dispatchSetCrop(args);
299 break;
300 case NATIVE_WINDOW_SET_BUFFER_COUNT:
301 res = dispatchSetBufferCount(args);
302 break;
303 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
304 res = dispatchSetBuffersGeometry(args);
305 break;
306 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
307 res = dispatchSetBuffersTransform(args);
308 break;
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800309 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
310 res = dispatchSetBuffersTimestamp(args);
311 break;
Jamie Gennis97eae022011-07-01 13:12:07 -0700312 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
313 res = dispatchSetBuffersDimensions(args);
314 break;
315 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
316 res = dispatchSetBuffersFormat(args);
317 break;
Mathias Agopian949be322011-07-13 17:39:11 -0700318 case NATIVE_WINDOW_LOCK:
319 res = dispatchLock(args);
320 break;
321 case NATIVE_WINDOW_UNLOCK_AND_POST:
322 res = dispatchUnlockAndPost(args);
323 break;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700324 case NATIVE_WINDOW_SET_SCALING_MODE:
325 res = dispatchSetScalingMode(args);
326 break;
Mathias Agopian982d2da2011-07-29 17:55:48 -0700327 case NATIVE_WINDOW_API_CONNECT:
328 res = dispatchConnect(args);
329 break;
330 case NATIVE_WINDOW_API_DISCONNECT:
331 res = dispatchDisconnect(args);
332 break;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800333 default:
334 res = NAME_NOT_FOUND;
335 break;
336 }
337 return res;
338}
339
340int SurfaceTextureClient::dispatchConnect(va_list args) {
341 int api = va_arg(args, int);
342 return connect(api);
343}
344
345int SurfaceTextureClient::dispatchDisconnect(va_list args) {
346 int api = va_arg(args, int);
347 return disconnect(api);
348}
349
350int SurfaceTextureClient::dispatchSetUsage(va_list args) {
351 int usage = va_arg(args, int);
352 return setUsage(usage);
353}
354
355int SurfaceTextureClient::dispatchSetCrop(va_list args) {
356 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
357 return setCrop(reinterpret_cast<Rect const*>(rect));
358}
359
360int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
361 size_t bufferCount = va_arg(args, size_t);
362 return setBufferCount(bufferCount);
363}
364
365int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
366 int w = va_arg(args, int);
367 int h = va_arg(args, int);
368 int f = va_arg(args, int);
Jamie Gennis97eae022011-07-01 13:12:07 -0700369 int err = setBuffersDimensions(w, h);
370 if (err != 0) {
371 return err;
372 }
373 return setBuffersFormat(f);
374}
375
376int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
377 int w = va_arg(args, int);
378 int h = va_arg(args, int);
379 return setBuffersDimensions(w, h);
380}
381
382int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
383 int f = va_arg(args, int);
384 return setBuffersFormat(f);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800385}
386
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700387int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
388 int m = va_arg(args, int);
389 return setScalingMode(m);
390}
391
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800392int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
393 int transform = va_arg(args, int);
394 return setBuffersTransform(transform);
395}
396
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800397int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
398 int64_t timestamp = va_arg(args, int64_t);
399 return setBuffersTimestamp(timestamp);
400}
401
Mathias Agopian949be322011-07-13 17:39:11 -0700402int SurfaceTextureClient::dispatchLock(va_list args) {
403 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
404 ARect* inOutDirtyBounds = va_arg(args, ARect*);
405 return lock(outBuffer, inOutDirtyBounds);
406}
407
408int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
409 return unlockAndPost();
410}
411
412
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800413int SurfaceTextureClient::connect(int api) {
Jamie Gennisa85ca372012-02-23 19:27:23 -0800414 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100415 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700416 Mutex::Autolock lock(mMutex);
Mathias Agopian053b02d2011-08-08 19:14:03 -0700417 int err = mSurfaceTexture->connect(api,
418 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Mathias Agopian949be322011-07-13 17:39:11 -0700419 if (!err && api == NATIVE_WINDOW_API_CPU) {
420 mConnectedToCpu = true;
421 }
422 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800423}
424
425int SurfaceTextureClient::disconnect(int api) {
Jamie Gennisa85ca372012-02-23 19:27:23 -0800426 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100427 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700428 Mutex::Autolock lock(mMutex);
Jamie Gennis791e6382011-10-18 17:14:33 -0700429 freeAllBuffers();
Mathias Agopian949be322011-07-13 17:39:11 -0700430 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian2370d0a2011-08-25 17:03:30 -0700431 if (!err) {
Mathias Agopian2370d0a2011-08-25 17:03:30 -0700432 mReqFormat = 0;
433 mReqWidth = 0;
434 mReqHeight = 0;
435 mReqUsage = 0;
436 if (api == NATIVE_WINDOW_API_CPU) {
437 mConnectedToCpu = false;
438 }
Mathias Agopian949be322011-07-13 17:39:11 -0700439 }
440 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800441}
442
443int SurfaceTextureClient::setUsage(uint32_t reqUsage)
444{
Steve Block71f2cf12011-10-20 11:56:00 +0100445 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800446 Mutex::Autolock lock(mMutex);
447 mReqUsage = reqUsage;
448 return OK;
449}
450
451int SurfaceTextureClient::setCrop(Rect const* rect)
452{
Jamie Gennisa85ca372012-02-23 19:27:23 -0800453 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100454 ALOGV("SurfaceTextureClient::setCrop");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800455 Mutex::Autolock lock(mMutex);
456
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800457 Rect realRect;
458 if (rect == NULL || rect->isEmpty()) {
459 realRect = Rect(0, 0);
460 } else {
461 realRect = *rect;
462 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800463
464 status_t err = mSurfaceTexture->setCrop(*rect);
Steve Block3762c312012-01-06 19:20:56 +0000465 ALOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800466
467 return err;
468}
469
470int SurfaceTextureClient::setBufferCount(int bufferCount)
471{
Jamie Gennisa85ca372012-02-23 19:27:23 -0800472 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100473 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800474 Mutex::Autolock lock(mMutex);
475
476 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
Steve Block3762c312012-01-06 19:20:56 +0000477 ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800478 bufferCount, strerror(-err));
479
480 if (err == NO_ERROR) {
481 freeAllBuffers();
482 }
483
484 return err;
485}
486
Jamie Gennis97eae022011-07-01 13:12:07 -0700487int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800488{
Jamie Gennisa85ca372012-02-23 19:27:23 -0800489 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100490 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800491 Mutex::Autolock lock(mMutex);
492
Jamie Gennis97eae022011-07-01 13:12:07 -0700493 if (w<0 || h<0)
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800494 return BAD_VALUE;
495
496 if ((w && !h) || (!w && h))
497 return BAD_VALUE;
498
499 mReqWidth = w;
500 mReqHeight = h;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800501
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800502 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
Steve Block3762c312012-01-06 19:20:56 +0000503 ALOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800504
505 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800506}
507
Jamie Gennis97eae022011-07-01 13:12:07 -0700508int SurfaceTextureClient::setBuffersFormat(int format)
509{
Steve Block71f2cf12011-10-20 11:56:00 +0100510 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennis97eae022011-07-01 13:12:07 -0700511 Mutex::Autolock lock(mMutex);
512
513 if (format<0)
514 return BAD_VALUE;
515
516 mReqFormat = format;
517
518 return NO_ERROR;
519}
520
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700521int SurfaceTextureClient::setScalingMode(int mode)
522{
Jamie Gennisa85ca372012-02-23 19:27:23 -0800523 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100524 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700525 Mutex::Autolock lock(mMutex);
526 // mode is validated on the server
527 status_t err = mSurfaceTexture->setScalingMode(mode);
Steve Block3762c312012-01-06 19:20:56 +0000528 ALOGE_IF(err, "ISurfaceTexture::setScalingMode(%d) returned %s",
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700529 mode, strerror(-err));
530
531 return err;
532}
533
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800534int SurfaceTextureClient::setBuffersTransform(int transform)
535{
Jamie Gennisa85ca372012-02-23 19:27:23 -0800536 ATRACE_CALL();
Steve Block71f2cf12011-10-20 11:56:00 +0100537 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800538 Mutex::Autolock lock(mMutex);
539 status_t err = mSurfaceTexture->setTransform(transform);
540 return err;
541}
542
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800543int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
544{
Steve Block71f2cf12011-10-20 11:56:00 +0100545 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800546 Mutex::Autolock lock(mMutex);
547 mTimestamp = timestamp;
548 return NO_ERROR;
549}
550
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800551void SurfaceTextureClient::freeAllBuffers() {
552 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
553 mSlots[i] = 0;
554 }
555}
556
Mathias Agopian949be322011-07-13 17:39:11 -0700557// ----------------------------------------------------------------------
558// the lock/unlock APIs must be used from the same thread
559
560static status_t copyBlt(
561 const sp<GraphicBuffer>& dst,
562 const sp<GraphicBuffer>& src,
563 const Region& reg)
564{
565 // src and dst with, height and format must be identical. no verification
566 // is done here.
567 status_t err;
568 uint8_t const * src_bits = NULL;
569 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Steve Block3762c312012-01-06 19:20:56 +0000570 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian949be322011-07-13 17:39:11 -0700571
572 uint8_t* dst_bits = NULL;
573 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Steve Block3762c312012-01-06 19:20:56 +0000574 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Mathias Agopian949be322011-07-13 17:39:11 -0700575
576 Region::const_iterator head(reg.begin());
577 Region::const_iterator tail(reg.end());
578 if (head != tail && src_bits && dst_bits) {
579 const size_t bpp = bytesPerPixel(src->format);
580 const size_t dbpr = dst->stride * bpp;
581 const size_t sbpr = src->stride * bpp;
582
583 while (head != tail) {
584 const Rect& r(*head++);
585 ssize_t h = r.height();
586 if (h <= 0) continue;
587 size_t size = r.width() * bpp;
588 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
589 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
590 if (dbpr==sbpr && size==sbpr) {
591 size *= h;
592 h = 1;
593 }
594 do {
595 memcpy(d, s, size);
596 d += dbpr;
597 s += sbpr;
598 } while (--h > 0);
599 }
600 }
601
602 if (src_bits)
603 src->unlock();
604
605 if (dst_bits)
606 dst->unlock();
607
608 return err;
609}
610
611// ----------------------------------------------------------------------------
612
613status_t SurfaceTextureClient::lock(
614 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
615{
616 if (mLockedBuffer != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000617 ALOGE("Surface::lock failed, already locked");
Mathias Agopian949be322011-07-13 17:39:11 -0700618 return INVALID_OPERATION;
619 }
620
621 if (!mConnectedToCpu) {
622 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
623 if (err) {
624 return err;
625 }
626 // we're intending to do software rendering from this point
627 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
628 }
629
630 ANativeWindowBuffer* out;
631 status_t err = dequeueBuffer(&out);
Steve Block3762c312012-01-06 19:20:56 +0000632 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian949be322011-07-13 17:39:11 -0700633 if (err == NO_ERROR) {
634 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
635 err = lockBuffer(backBuffer.get());
Steve Block3762c312012-01-06 19:20:56 +0000636 ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
Mathias Agopian949be322011-07-13 17:39:11 -0700637 backBuffer->handle, strerror(-err));
638 if (err == NO_ERROR) {
639 const Rect bounds(backBuffer->width, backBuffer->height);
640
641 Region newDirtyRegion;
642 if (inOutDirtyBounds) {
643 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
644 newDirtyRegion.andSelf(bounds);
645 } else {
646 newDirtyRegion.set(bounds);
647 }
648
649 // figure out if we can copy the frontbuffer back
650 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
651 const bool canCopyBack = (frontBuffer != 0 &&
652 backBuffer->width == frontBuffer->width &&
653 backBuffer->height == frontBuffer->height &&
654 backBuffer->format == frontBuffer->format);
655
656 if (canCopyBack) {
657 // copy the area that is invalid and not repainted this round
658 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
659 if (!copyback.isEmpty())
660 copyBlt(backBuffer, frontBuffer, copyback);
661 } else {
662 // if we can't copy-back anything, modify the user's dirty
663 // region to make sure they redraw the whole buffer
664 newDirtyRegion.set(bounds);
665 }
666
667 // keep track of the are of the buffer that is "clean"
668 // (ie: that will be redrawn)
669 mOldDirtyRegion = newDirtyRegion;
670
671 if (inOutDirtyBounds) {
672 *inOutDirtyBounds = newDirtyRegion.getBounds();
673 }
674
675 void* vaddr;
676 status_t res = backBuffer->lock(
677 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
678 newDirtyRegion.bounds(), &vaddr);
679
Steve Block8564c8d2012-01-05 23:22:43 +0000680 ALOGW_IF(res, "failed locking buffer (handle = %p)",
Mathias Agopian949be322011-07-13 17:39:11 -0700681 backBuffer->handle);
682
683 mLockedBuffer = backBuffer;
684 outBuffer->width = backBuffer->width;
685 outBuffer->height = backBuffer->height;
686 outBuffer->stride = backBuffer->stride;
687 outBuffer->format = backBuffer->format;
688 outBuffer->bits = vaddr;
689 }
690 }
691 return err;
692}
693
694status_t SurfaceTextureClient::unlockAndPost()
695{
696 if (mLockedBuffer == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000697 ALOGE("Surface::unlockAndPost failed, no locked buffer");
Mathias Agopian949be322011-07-13 17:39:11 -0700698 return INVALID_OPERATION;
699 }
700
701 status_t err = mLockedBuffer->unlock();
Steve Block3762c312012-01-06 19:20:56 +0000702 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian949be322011-07-13 17:39:11 -0700703
704 err = queueBuffer(mLockedBuffer.get());
Steve Block3762c312012-01-06 19:20:56 +0000705 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
Mathias Agopian949be322011-07-13 17:39:11 -0700706 mLockedBuffer->handle, strerror(-err));
707
708 mPostedBuffer = mLockedBuffer;
709 mLockedBuffer = 0;
710 return err;
711}
712
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800713}; // namespace android