blob: b37d821f9b2ba1627cc82e203e51729e3d89f6e8 [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;
Michael I. Gold55a70142012-04-09 19:46:29 -070083 mUserWidth = 0;
84 mUserHeight = 0;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -070085 mTransformHint = 0;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070086 mConnectedToCpu = false;
87}
88
89void SurfaceTextureClient::setISurfaceTexture(
90 const sp<ISurfaceTexture>& surfaceTexture)
91{
92 mSurfaceTexture = surfaceTexture;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080093}
94
Jamie Gennisbae774e2011-03-14 15:08:53 -070095sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
96 return mSurfaceTexture;
97}
98
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070099int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800100 SurfaceTextureClient* c = getSelf(window);
101 return c->setSwapInterval(interval);
102}
103
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700104int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700105 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800106 SurfaceTextureClient* c = getSelf(window);
107 return c->dequeueBuffer(buffer);
108}
109
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700110int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700111 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800112 SurfaceTextureClient* c = getSelf(window);
113 return c->cancelBuffer(buffer);
114}
115
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700116int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700117 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800118 SurfaceTextureClient* c = getSelf(window);
119 return c->lockBuffer(buffer);
120}
121
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700122int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700123 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800124 SurfaceTextureClient* c = getSelf(window);
125 return c->queueBuffer(buffer);
126}
127
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700128int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev41abd672011-04-14 16:54:38 -0700129 int what, int* value) {
130 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131 return c->query(what, value);
132}
133
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700134int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135 va_list args;
136 va_start(args, operation);
137 SurfaceTextureClient* c = getSelf(window);
138 return c->perform(operation, args);
139}
140
141int SurfaceTextureClient::setSwapInterval(int interval) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800142 ATRACE_CALL();
Mathias Agopian80727112011-05-02 19:51:12 -0700143 // EGL specification states:
144 // interval is silently clamped to minimum and maximum implementation
145 // dependent values before being stored.
146 // Although we don't have to, we apply the same logic here.
147
148 if (interval < minSwapInterval)
149 interval = minSwapInterval;
150
151 if (interval > maxSwapInterval)
152 interval = maxSwapInterval;
153
154 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
155
156 return res;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800157}
158
159int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800160 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100161 ALOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800162 Mutex::Autolock lock(mMutex);
163 int buf = -1;
Michael I. Gold55a70142012-04-09 19:46:29 -0700164 int reqW = mReqWidth ? mReqWidth : mUserWidth;
165 int reqH = mReqHeight ? mReqHeight : mUserHeight;
166 status_t result = mSurfaceTexture->dequeueBuffer(&buf, reqW, reqH,
Mathias Agopianc04f1532011-04-25 20:22:14 -0700167 mReqFormat, mReqUsage);
Mathias Agopian80727112011-05-02 19:51:12 -0700168 if (result < 0) {
Steve Block6807e592011-10-20 11:56:00 +0100169 ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700170 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
171 result);
Mathias Agopian80727112011-05-02 19:51:12 -0700172 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800173 }
Mathias Agopianac6035a2012-04-12 16:32:37 -0700174 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopian80727112011-05-02 19:51:12 -0700175 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
176 freeAllBuffers();
177 }
178
179 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700180 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
181 if (result != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000182 ALOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700183 result);
184 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800185 }
186 }
187 *buffer = gbuf.get();
188 return OK;
189}
190
191int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800192 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100193 ALOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800194 Mutex::Autolock lock(mMutex);
Jamie Gennis1c441402011-06-20 12:04:09 -0700195 int i = getSlotFromBufferLocked(buffer);
196 if (i < 0) {
197 return i;
198 }
199 mSurfaceTexture->cancelBuffer(i);
200 return OK;
201}
202
203int SurfaceTextureClient::getSlotFromBufferLocked(
204 android_native_buffer_t* buffer) const {
205 bool dumpedState = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800206 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopianac6035a2012-04-12 16:32:37 -0700207 if (mSlots[i].buffer != NULL &&
208 mSlots[i].buffer->handle == buffer->handle) {
Jamie Gennis1c441402011-06-20 12:04:09 -0700209 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800210 }
211 }
Steve Blocke6f43dd2012-01-06 19:20:56 +0000212 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800213 return BAD_VALUE;
214}
215
216int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100217 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800218 Mutex::Autolock lock(mMutex);
219 return OK;
220}
221
222int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800223 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100224 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800225 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800226 int64_t timestamp;
227 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
228 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block6807e592011-10-20 11:56:00 +0100229 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800230 timestamp / 1000000.f);
231 } else {
232 timestamp = mTimestamp;
233 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700234 int i = getSlotFromBufferLocked(buffer);
235 if (i < 0) {
236 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800237 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700238
239 ISurfaceTexture::QueueBufferOutput output;
240 ISurfaceTexture::QueueBufferInput input(timestamp,
241 mCrop, mScalingMode, mTransform);
242 status_t err = mSurfaceTexture->queueBuffer(i, input, &output);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700243 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000244 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700245 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700246 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700247 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800248}
249
Iliyan Malchev41abd672011-04-14 16:54:38 -0700250int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800251 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100252 ALOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700253 { // scope for the lock
254 Mutex::Autolock lock(mMutex);
255 switch (what) {
256 case NATIVE_WINDOW_FORMAT:
257 if (mReqFormat) {
258 *value = mReqFormat;
259 return NO_ERROR;
260 }
261 break;
262 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis582270d2011-08-17 18:19:00 -0700263 {
264 sp<ISurfaceComposer> composer(
265 ComposerService::getComposerService());
266 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
267 *value = 1;
268 } else {
269 *value = 0;
270 }
271 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700272 return NO_ERROR;
273 case NATIVE_WINDOW_CONCRETE_TYPE:
274 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
275 return NO_ERROR;
276 case NATIVE_WINDOW_DEFAULT_WIDTH:
Michael I. Gold55a70142012-04-09 19:46:29 -0700277 *value = mUserWidth ? mUserWidth : mDefaultWidth;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700278 return NO_ERROR;
279 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Michael I. Gold55a70142012-04-09 19:46:29 -0700280 *value = mUserHeight ? mUserHeight : mDefaultHeight;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700281 return NO_ERROR;
282 case NATIVE_WINDOW_TRANSFORM_HINT:
283 *value = mTransformHint;
284 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700285 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800286 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700287 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800288}
289
290int SurfaceTextureClient::perform(int operation, va_list args)
291{
292 int res = NO_ERROR;
293 switch (operation) {
294 case NATIVE_WINDOW_CONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700295 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800296 break;
297 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700298 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800299 break;
300 case NATIVE_WINDOW_SET_USAGE:
301 res = dispatchSetUsage(args);
302 break;
303 case NATIVE_WINDOW_SET_CROP:
304 res = dispatchSetCrop(args);
305 break;
306 case NATIVE_WINDOW_SET_BUFFER_COUNT:
307 res = dispatchSetBufferCount(args);
308 break;
309 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
310 res = dispatchSetBuffersGeometry(args);
311 break;
312 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
313 res = dispatchSetBuffersTransform(args);
314 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800315 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
316 res = dispatchSetBuffersTimestamp(args);
317 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700318 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
319 res = dispatchSetBuffersDimensions(args);
320 break;
Michael I. Gold55a70142012-04-09 19:46:29 -0700321 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
322 res = dispatchSetBuffersUserDimensions(args);
323 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700324 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
325 res = dispatchSetBuffersFormat(args);
326 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700327 case NATIVE_WINDOW_LOCK:
328 res = dispatchLock(args);
329 break;
330 case NATIVE_WINDOW_UNLOCK_AND_POST:
331 res = dispatchUnlockAndPost(args);
332 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700333 case NATIVE_WINDOW_SET_SCALING_MODE:
334 res = dispatchSetScalingMode(args);
335 break;
Mathias Agopian81a63352011-07-29 17:55:48 -0700336 case NATIVE_WINDOW_API_CONNECT:
337 res = dispatchConnect(args);
338 break;
339 case NATIVE_WINDOW_API_DISCONNECT:
340 res = dispatchDisconnect(args);
341 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800342 default:
343 res = NAME_NOT_FOUND;
344 break;
345 }
346 return res;
347}
348
349int SurfaceTextureClient::dispatchConnect(va_list args) {
350 int api = va_arg(args, int);
351 return connect(api);
352}
353
354int SurfaceTextureClient::dispatchDisconnect(va_list args) {
355 int api = va_arg(args, int);
356 return disconnect(api);
357}
358
359int SurfaceTextureClient::dispatchSetUsage(va_list args) {
360 int usage = va_arg(args, int);
361 return setUsage(usage);
362}
363
364int SurfaceTextureClient::dispatchSetCrop(va_list args) {
365 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
366 return setCrop(reinterpret_cast<Rect const*>(rect));
367}
368
369int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
370 size_t bufferCount = va_arg(args, size_t);
371 return setBufferCount(bufferCount);
372}
373
374int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
375 int w = va_arg(args, int);
376 int h = va_arg(args, int);
377 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700378 int err = setBuffersDimensions(w, h);
379 if (err != 0) {
380 return err;
381 }
382 return setBuffersFormat(f);
383}
384
385int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
386 int w = va_arg(args, int);
387 int h = va_arg(args, int);
388 return setBuffersDimensions(w, h);
389}
390
Michael I. Gold55a70142012-04-09 19:46:29 -0700391int SurfaceTextureClient::dispatchSetBuffersUserDimensions(va_list args) {
392 int w = va_arg(args, int);
393 int h = va_arg(args, int);
394 return setBuffersUserDimensions(w, h);
395}
396
Jamie Gennisbee205f2011-07-01 13:12:07 -0700397int 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
Michael I. Gold55a70142012-04-09 19:46:29 -0700521int SurfaceTextureClient::setBuffersUserDimensions(int w, int h)
522{
523 ATRACE_CALL();
524 ALOGV("SurfaceTextureClient::setBuffersUserDimensions");
525
526 if (w<0 || h<0)
527 return BAD_VALUE;
528
529 if ((w && !h) || (!w && h))
530 return BAD_VALUE;
531
532 Mutex::Autolock lock(mMutex);
533 mUserWidth = w;
534 mUserHeight = h;
535 mCrop.clear();
536 return NO_ERROR;
537}
538
Jamie Gennisbee205f2011-07-01 13:12:07 -0700539int SurfaceTextureClient::setBuffersFormat(int format)
540{
Steve Block6807e592011-10-20 11:56:00 +0100541 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennisbee205f2011-07-01 13:12:07 -0700542
543 if (format<0)
544 return BAD_VALUE;
545
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700546 Mutex::Autolock lock(mMutex);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700547 mReqFormat = format;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700548 return NO_ERROR;
549}
550
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700551int SurfaceTextureClient::setScalingMode(int mode)
552{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800553 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100554 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700555
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700556 switch (mode) {
557 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
558 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
Daniel Lam016c8cb2012-04-03 15:54:58 -0700559 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700560 break;
561 default:
562 ALOGE("unknown scaling mode: %d", mode);
563 return BAD_VALUE;
564 }
565
566 Mutex::Autolock lock(mMutex);
567 mScalingMode = mode;
568 return NO_ERROR;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700569}
570
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800571int SurfaceTextureClient::setBuffersTransform(int transform)
572{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800573 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100574 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800575 Mutex::Autolock lock(mMutex);
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700576 mTransform = transform;
577 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800578}
579
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800580int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
581{
Steve Block6807e592011-10-20 11:56:00 +0100582 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800583 Mutex::Autolock lock(mMutex);
584 mTimestamp = timestamp;
585 return NO_ERROR;
586}
587
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800588void SurfaceTextureClient::freeAllBuffers() {
589 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopianac6035a2012-04-12 16:32:37 -0700590 mSlots[i].buffer = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800591 }
592}
593
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700594// ----------------------------------------------------------------------
595// the lock/unlock APIs must be used from the same thread
596
597static status_t copyBlt(
598 const sp<GraphicBuffer>& dst,
599 const sp<GraphicBuffer>& src,
600 const Region& reg)
601{
602 // src and dst with, height and format must be identical. no verification
603 // is done here.
604 status_t err;
605 uint8_t const * src_bits = NULL;
606 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000607 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700608
609 uint8_t* dst_bits = NULL;
610 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000611 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700612
613 Region::const_iterator head(reg.begin());
614 Region::const_iterator tail(reg.end());
615 if (head != tail && src_bits && dst_bits) {
616 const size_t bpp = bytesPerPixel(src->format);
617 const size_t dbpr = dst->stride * bpp;
618 const size_t sbpr = src->stride * bpp;
619
620 while (head != tail) {
621 const Rect& r(*head++);
622 ssize_t h = r.height();
623 if (h <= 0) continue;
624 size_t size = r.width() * bpp;
625 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
626 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
627 if (dbpr==sbpr && size==sbpr) {
628 size *= h;
629 h = 1;
630 }
631 do {
632 memcpy(d, s, size);
633 d += dbpr;
634 s += sbpr;
635 } while (--h > 0);
636 }
637 }
638
639 if (src_bits)
640 src->unlock();
641
642 if (dst_bits)
643 dst->unlock();
644
645 return err;
646}
647
648// ----------------------------------------------------------------------------
649
650status_t SurfaceTextureClient::lock(
651 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
652{
653 if (mLockedBuffer != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000654 ALOGE("Surface::lock failed, already locked");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700655 return INVALID_OPERATION;
656 }
657
658 if (!mConnectedToCpu) {
659 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
660 if (err) {
661 return err;
662 }
663 // we're intending to do software rendering from this point
664 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
665 }
666
667 ANativeWindowBuffer* out;
668 status_t err = dequeueBuffer(&out);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000669 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700670 if (err == NO_ERROR) {
671 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
672 err = lockBuffer(backBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000673 ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700674 backBuffer->handle, strerror(-err));
675 if (err == NO_ERROR) {
676 const Rect bounds(backBuffer->width, backBuffer->height);
677
678 Region newDirtyRegion;
679 if (inOutDirtyBounds) {
680 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
681 newDirtyRegion.andSelf(bounds);
682 } else {
683 newDirtyRegion.set(bounds);
684 }
685
686 // figure out if we can copy the frontbuffer back
687 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
688 const bool canCopyBack = (frontBuffer != 0 &&
689 backBuffer->width == frontBuffer->width &&
690 backBuffer->height == frontBuffer->height &&
691 backBuffer->format == frontBuffer->format);
692
693 if (canCopyBack) {
694 // copy the area that is invalid and not repainted this round
Mathias Agopianac6035a2012-04-12 16:32:37 -0700695 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700696 if (!copyback.isEmpty())
697 copyBlt(backBuffer, frontBuffer, copyback);
698 } else {
699 // if we can't copy-back anything, modify the user's dirty
700 // region to make sure they redraw the whole buffer
701 newDirtyRegion.set(bounds);
Mathias Agopianac6035a2012-04-12 16:32:37 -0700702 mDirtyRegion.clear();
703 Mutex::Autolock lock(mMutex);
704 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
705 mSlots[i].dirtyRegion.clear();
706 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700707 }
708
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700709
Mathias Agopianac6035a2012-04-12 16:32:37 -0700710 { // scope for the lock
711 Mutex::Autolock lock(mMutex);
712 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
713 if (backBufferSlot >= 0) {
714 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
715 mDirtyRegion.subtract(dirtyRegion);
716 dirtyRegion = newDirtyRegion;
717 }
718 }
719
720 mDirtyRegion.orSelf(newDirtyRegion);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700721 if (inOutDirtyBounds) {
722 *inOutDirtyBounds = newDirtyRegion.getBounds();
723 }
724
725 void* vaddr;
726 status_t res = backBuffer->lock(
727 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
728 newDirtyRegion.bounds(), &vaddr);
729
Steve Block32397c12012-01-05 23:22:43 +0000730 ALOGW_IF(res, "failed locking buffer (handle = %p)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700731 backBuffer->handle);
732
733 mLockedBuffer = backBuffer;
734 outBuffer->width = backBuffer->width;
735 outBuffer->height = backBuffer->height;
736 outBuffer->stride = backBuffer->stride;
737 outBuffer->format = backBuffer->format;
738 outBuffer->bits = vaddr;
739 }
740 }
741 return err;
742}
743
744status_t SurfaceTextureClient::unlockAndPost()
745{
746 if (mLockedBuffer == 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000747 ALOGE("Surface::unlockAndPost failed, no locked buffer");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700748 return INVALID_OPERATION;
749 }
750
751 status_t err = mLockedBuffer->unlock();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000752 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700753
754 err = queueBuffer(mLockedBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000755 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700756 mLockedBuffer->handle, strerror(-err));
757
758 mPostedBuffer = mLockedBuffer;
759 mLockedBuffer = 0;
760 return err;
761}
762
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800763}; // namespace android