blob: 5d3bd1a1ce41b191e735451ffe130ffeba780824 [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();
Jamie Gennisd72f2332012-05-07 13:50:11 -070079 mCropNeedsTransform = false;
Mathias Agopian851ef8f2012-03-29 17:10:08 -070080 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
81 mTransform = 0;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -070082 mDefaultWidth = 0;
83 mDefaultHeight = 0;
Michael I. Gold55a70142012-04-09 19:46:29 -070084 mUserWidth = 0;
85 mUserHeight = 0;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -070086 mTransformHint = 0;
Mathias Agopian2488b202012-04-20 17:19:28 -070087 mConsumerRunningBehind = false;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070088 mConnectedToCpu = false;
89}
90
91void SurfaceTextureClient::setISurfaceTexture(
92 const sp<ISurfaceTexture>& surfaceTexture)
93{
94 mSurfaceTexture = surfaceTexture;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080095}
96
Jamie Gennisbae774e2011-03-14 15:08:53 -070097sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
98 return mSurfaceTexture;
99}
100
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700101int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800102 SurfaceTextureClient* c = getSelf(window);
103 return c->setSwapInterval(interval);
104}
105
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700106int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700107 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800108 SurfaceTextureClient* c = getSelf(window);
109 return c->dequeueBuffer(buffer);
110}
111
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700112int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700113 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800114 SurfaceTextureClient* c = getSelf(window);
115 return c->cancelBuffer(buffer);
116}
117
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700118int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700119 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800120 SurfaceTextureClient* c = getSelf(window);
121 return c->lockBuffer(buffer);
122}
123
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700124int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -0700125 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800126 SurfaceTextureClient* c = getSelf(window);
127 return c->queueBuffer(buffer);
128}
129
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700130int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev41abd672011-04-14 16:54:38 -0700131 int what, int* value) {
132 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800133 return c->query(what, value);
134}
135
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700136int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800137 va_list args;
138 va_start(args, operation);
139 SurfaceTextureClient* c = getSelf(window);
140 return c->perform(operation, args);
141}
142
143int SurfaceTextureClient::setSwapInterval(int interval) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800144 ATRACE_CALL();
Mathias Agopian80727112011-05-02 19:51:12 -0700145 // EGL specification states:
146 // interval is silently clamped to minimum and maximum implementation
147 // dependent values before being stored.
148 // Although we don't have to, we apply the same logic here.
149
150 if (interval < minSwapInterval)
151 interval = minSwapInterval;
152
153 if (interval > maxSwapInterval)
154 interval = maxSwapInterval;
155
156 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
157
158 return res;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800159}
160
161int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800162 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100163 ALOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800164 Mutex::Autolock lock(mMutex);
165 int buf = -1;
Michael I. Gold55a70142012-04-09 19:46:29 -0700166 int reqW = mReqWidth ? mReqWidth : mUserWidth;
167 int reqH = mReqHeight ? mReqHeight : mUserHeight;
168 status_t result = mSurfaceTexture->dequeueBuffer(&buf, reqW, reqH,
Mathias Agopianc04f1532011-04-25 20:22:14 -0700169 mReqFormat, mReqUsage);
Mathias Agopian80727112011-05-02 19:51:12 -0700170 if (result < 0) {
Steve Block6807e592011-10-20 11:56:00 +0100171 ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700172 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
173 result);
Mathias Agopian80727112011-05-02 19:51:12 -0700174 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800175 }
Mathias Agopianac6035a2012-04-12 16:32:37 -0700176 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopian80727112011-05-02 19:51:12 -0700177 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
178 freeAllBuffers();
179 }
180
181 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700182 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
183 if (result != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000184 ALOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700185 result);
186 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800187 }
188 }
189 *buffer = gbuf.get();
190 return OK;
191}
192
193int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800194 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100195 ALOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800196 Mutex::Autolock lock(mMutex);
Jamie Gennis1c441402011-06-20 12:04:09 -0700197 int i = getSlotFromBufferLocked(buffer);
198 if (i < 0) {
199 return i;
200 }
201 mSurfaceTexture->cancelBuffer(i);
202 return OK;
203}
204
205int SurfaceTextureClient::getSlotFromBufferLocked(
206 android_native_buffer_t* buffer) const {
207 bool dumpedState = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800208 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopianac6035a2012-04-12 16:32:37 -0700209 if (mSlots[i].buffer != NULL &&
210 mSlots[i].buffer->handle == buffer->handle) {
Jamie Gennis1c441402011-06-20 12:04:09 -0700211 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800212 }
213 }
Steve Blocke6f43dd2012-01-06 19:20:56 +0000214 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800215 return BAD_VALUE;
216}
217
218int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100219 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800220 Mutex::Autolock lock(mMutex);
221 return OK;
222}
223
224int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800225 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100226 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800227 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800228 int64_t timestamp;
229 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
230 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block6807e592011-10-20 11:56:00 +0100231 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800232 timestamp / 1000000.f);
233 } else {
234 timestamp = mTimestamp;
235 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700236 int i = getSlotFromBufferLocked(buffer);
237 if (i < 0) {
238 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800239 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700240
Jamie Gennisd72f2332012-05-07 13:50:11 -0700241 Rect crop(mCrop);
242 if (mCropNeedsTransform) {
243 // The crop rect was specified in the post-transform coordinate space,
244 // so we need to transform that rect by the inverse of mTransform to
245 // put it into the buffer pixel space before queuing it.
246 uint32_t invTransform = mTransform;
247 int32_t width = buffer->width;
248 int32_t height = buffer->height;
249 if (mTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
250 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V |
251 NATIVE_WINDOW_TRANSFORM_FLIP_H;
252 width = buffer->height;
253 height = buffer->width;
254 }
255 crop = mCrop.transform(invTransform, width, height);
256 }
257
258 // Make sure the crop rectangle is entirely inside the buffer.
259 crop.intersect(Rect(buffer->width, buffer->height), &crop);
260
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700261 ISurfaceTexture::QueueBufferOutput output;
Jamie Gennisd72f2332012-05-07 13:50:11 -0700262 ISurfaceTexture::QueueBufferInput input(timestamp, crop, mScalingMode,
263 mTransform);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700264 status_t err = mSurfaceTexture->queueBuffer(i, input, &output);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700265 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000266 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700267 }
Mathias Agopian2488b202012-04-20 17:19:28 -0700268 uint32_t numPendingBuffers = 0;
269 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
270 &numPendingBuffers);
271
272 mConsumerRunningBehind = (numPendingBuffers >= 2);
273
Pannag Sanketi66378c62011-09-02 17:37:29 -0700274 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800275}
276
Iliyan Malchev41abd672011-04-14 16:54:38 -0700277int SurfaceTextureClient::query(int what, int* value) const {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800278 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100279 ALOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700280 { // scope for the lock
281 Mutex::Autolock lock(mMutex);
282 switch (what) {
283 case NATIVE_WINDOW_FORMAT:
284 if (mReqFormat) {
285 *value = mReqFormat;
286 return NO_ERROR;
287 }
288 break;
Mathias Agopian2488b202012-04-20 17:19:28 -0700289 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
290 sp<ISurfaceComposer> composer(
291 ComposerService::getComposerService());
292 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
293 *value = 1;
294 } else {
295 *value = 0;
Jamie Gennis582270d2011-08-17 18:19:00 -0700296 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700297 return NO_ERROR;
Mathias Agopian2488b202012-04-20 17:19:28 -0700298 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700299 case NATIVE_WINDOW_CONCRETE_TYPE:
300 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
301 return NO_ERROR;
302 case NATIVE_WINDOW_DEFAULT_WIDTH:
Michael I. Gold55a70142012-04-09 19:46:29 -0700303 *value = mUserWidth ? mUserWidth : mDefaultWidth;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700304 return NO_ERROR;
305 case NATIVE_WINDOW_DEFAULT_HEIGHT:
Michael I. Gold55a70142012-04-09 19:46:29 -0700306 *value = mUserHeight ? mUserHeight : mDefaultHeight;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700307 return NO_ERROR;
308 case NATIVE_WINDOW_TRANSFORM_HINT:
309 *value = mTransformHint;
310 return NO_ERROR;
Mathias Agopian2488b202012-04-20 17:19:28 -0700311 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
312 status_t err = NO_ERROR;
313 if (!mConsumerRunningBehind) {
314 *value = 0;
315 } else {
316 err = mSurfaceTexture->query(what, value);
317 if (err == NO_ERROR) {
318 mConsumerRunningBehind = *value;
319 }
320 }
321 return err;
322 }
Mathias Agopiana67932f2011-04-20 14:20:59 -0700323 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800324 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700325 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800326}
327
328int SurfaceTextureClient::perform(int operation, va_list args)
329{
330 int res = NO_ERROR;
331 switch (operation) {
332 case NATIVE_WINDOW_CONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700333 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800334 break;
335 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700336 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800337 break;
338 case NATIVE_WINDOW_SET_USAGE:
339 res = dispatchSetUsage(args);
340 break;
341 case NATIVE_WINDOW_SET_CROP:
342 res = dispatchSetCrop(args);
343 break;
Jamie Gennisd72f2332012-05-07 13:50:11 -0700344 case NATIVE_WINDOW_SET_POST_TRANSFORM_CROP:
345 res = dispatchSetPostTransformCrop(args);
346 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800347 case NATIVE_WINDOW_SET_BUFFER_COUNT:
348 res = dispatchSetBufferCount(args);
349 break;
350 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
351 res = dispatchSetBuffersGeometry(args);
352 break;
353 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
354 res = dispatchSetBuffersTransform(args);
355 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800356 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
357 res = dispatchSetBuffersTimestamp(args);
358 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700359 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
360 res = dispatchSetBuffersDimensions(args);
361 break;
Michael I. Gold55a70142012-04-09 19:46:29 -0700362 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
363 res = dispatchSetBuffersUserDimensions(args);
364 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700365 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
366 res = dispatchSetBuffersFormat(args);
367 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700368 case NATIVE_WINDOW_LOCK:
369 res = dispatchLock(args);
370 break;
371 case NATIVE_WINDOW_UNLOCK_AND_POST:
372 res = dispatchUnlockAndPost(args);
373 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700374 case NATIVE_WINDOW_SET_SCALING_MODE:
375 res = dispatchSetScalingMode(args);
376 break;
Mathias Agopian81a63352011-07-29 17:55:48 -0700377 case NATIVE_WINDOW_API_CONNECT:
378 res = dispatchConnect(args);
379 break;
380 case NATIVE_WINDOW_API_DISCONNECT:
381 res = dispatchDisconnect(args);
382 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800383 default:
384 res = NAME_NOT_FOUND;
385 break;
386 }
387 return res;
388}
389
390int SurfaceTextureClient::dispatchConnect(va_list args) {
391 int api = va_arg(args, int);
392 return connect(api);
393}
394
395int SurfaceTextureClient::dispatchDisconnect(va_list args) {
396 int api = va_arg(args, int);
397 return disconnect(api);
398}
399
400int SurfaceTextureClient::dispatchSetUsage(va_list args) {
401 int usage = va_arg(args, int);
402 return setUsage(usage);
403}
404
405int SurfaceTextureClient::dispatchSetCrop(va_list args) {
406 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
407 return setCrop(reinterpret_cast<Rect const*>(rect));
408}
409
Jamie Gennisd72f2332012-05-07 13:50:11 -0700410int SurfaceTextureClient::dispatchSetPostTransformCrop(va_list args) {
411 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
412 return setPostTransformCrop(reinterpret_cast<Rect const*>(rect));
413}
414
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800415int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
416 size_t bufferCount = va_arg(args, size_t);
417 return setBufferCount(bufferCount);
418}
419
420int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
421 int w = va_arg(args, int);
422 int h = va_arg(args, int);
423 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700424 int err = setBuffersDimensions(w, h);
425 if (err != 0) {
426 return err;
427 }
428 return setBuffersFormat(f);
429}
430
431int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
432 int w = va_arg(args, int);
433 int h = va_arg(args, int);
434 return setBuffersDimensions(w, h);
435}
436
Michael I. Gold55a70142012-04-09 19:46:29 -0700437int SurfaceTextureClient::dispatchSetBuffersUserDimensions(va_list args) {
438 int w = va_arg(args, int);
439 int h = va_arg(args, int);
440 return setBuffersUserDimensions(w, h);
441}
442
Jamie Gennisbee205f2011-07-01 13:12:07 -0700443int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
444 int f = va_arg(args, int);
445 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800446}
447
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700448int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
449 int m = va_arg(args, int);
450 return setScalingMode(m);
451}
452
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800453int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
454 int transform = va_arg(args, int);
455 return setBuffersTransform(transform);
456}
457
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800458int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
459 int64_t timestamp = va_arg(args, int64_t);
460 return setBuffersTimestamp(timestamp);
461}
462
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700463int SurfaceTextureClient::dispatchLock(va_list args) {
464 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
465 ARect* inOutDirtyBounds = va_arg(args, ARect*);
466 return lock(outBuffer, inOutDirtyBounds);
467}
468
469int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
470 return unlockAndPost();
471}
472
473
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800474int SurfaceTextureClient::connect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800475 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100476 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700477 Mutex::Autolock lock(mMutex);
Mathias Agopian24202f52012-04-23 14:28:58 -0700478 ISurfaceTexture::QueueBufferOutput output;
479 int err = mSurfaceTexture->connect(api, &output);
Mathias Agopian2488b202012-04-20 17:19:28 -0700480 if (err == NO_ERROR) {
481 uint32_t numPendingBuffers = 0;
482 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
483 &numPendingBuffers);
484 mConsumerRunningBehind = (numPendingBuffers >= 2);
485 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700486 if (!err && api == NATIVE_WINDOW_API_CPU) {
487 mConnectedToCpu = true;
488 }
489 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800490}
491
492int SurfaceTextureClient::disconnect(int api) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800493 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100494 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700495 Mutex::Autolock lock(mMutex);
Jamie Gennis13c5ca32011-10-18 17:14:33 -0700496 freeAllBuffers();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700497 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian70e3f812011-08-25 17:03:30 -0700498 if (!err) {
Mathias Agopian70e3f812011-08-25 17:03:30 -0700499 mReqFormat = 0;
500 mReqWidth = 0;
501 mReqHeight = 0;
502 mReqUsage = 0;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700503 mCrop.clear();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700504 mCropNeedsTransform = false;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700505 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
506 mTransform = 0;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700507 if (api == NATIVE_WINDOW_API_CPU) {
508 mConnectedToCpu = false;
509 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700510 }
511 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800512}
513
514int SurfaceTextureClient::setUsage(uint32_t reqUsage)
515{
Steve Block6807e592011-10-20 11:56:00 +0100516 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800517 Mutex::Autolock lock(mMutex);
518 mReqUsage = reqUsage;
519 return OK;
520}
521
522int SurfaceTextureClient::setCrop(Rect const* rect)
523{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800524 ATRACE_CALL();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800525
Jamie Gennis68f91272011-01-28 18:21:54 -0800526 Rect realRect;
527 if (rect == NULL || rect->isEmpty()) {
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700528 realRect.clear();
Jamie Gennis68f91272011-01-28 18:21:54 -0800529 } else {
530 realRect = *rect;
531 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800532
Jamie Genniscd1806e2012-05-10 02:22:33 -0700533 ALOGV("SurfaceTextureClient::setCrop rect=[%d %d %d %d]",
534 realRect.left, realRect.top, realRect.right, realRect.bottom);
535
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700536 Mutex::Autolock lock(mMutex);
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700537 mCrop = realRect;
Jamie Gennisd72f2332012-05-07 13:50:11 -0700538 mCropNeedsTransform = false;
539 return NO_ERROR;
540}
541
542int SurfaceTextureClient::setPostTransformCrop(Rect const* rect)
543{
544 ATRACE_CALL();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700545
546 Rect realRect;
547 if (rect == NULL || rect->isEmpty()) {
548 realRect.clear();
549 } else {
550 realRect = *rect;
551 }
552
Jamie Genniscd1806e2012-05-10 02:22:33 -0700553 ALOGV("SurfaceTextureClient::setPostTransformCrop rect=[%d %d %d %d]",
554 realRect.left, realRect.top, realRect.right, realRect.bottom);
555
Jamie Gennisd72f2332012-05-07 13:50:11 -0700556 Mutex::Autolock lock(mMutex);
557 mCrop = realRect;
558 mCropNeedsTransform = true;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700559 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800560}
561
562int SurfaceTextureClient::setBufferCount(int bufferCount)
563{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800564 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100565 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800566 Mutex::Autolock lock(mMutex);
567
568 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000569 ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800570 bufferCount, strerror(-err));
571
572 if (err == NO_ERROR) {
573 freeAllBuffers();
574 }
575
576 return err;
577}
578
Jamie Gennisbee205f2011-07-01 13:12:07 -0700579int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800580{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800581 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100582 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800583
Jamie Gennisbee205f2011-07-01 13:12:07 -0700584 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800585 return BAD_VALUE;
586
587 if ((w && !h) || (!w && h))
588 return BAD_VALUE;
589
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700590 Mutex::Autolock lock(mMutex);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800591 mReqWidth = w;
592 mReqHeight = h;
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700593 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800594}
595
Michael I. Gold55a70142012-04-09 19:46:29 -0700596int SurfaceTextureClient::setBuffersUserDimensions(int w, int h)
597{
598 ATRACE_CALL();
599 ALOGV("SurfaceTextureClient::setBuffersUserDimensions");
600
601 if (w<0 || h<0)
602 return BAD_VALUE;
603
604 if ((w && !h) || (!w && h))
605 return BAD_VALUE;
606
607 Mutex::Autolock lock(mMutex);
608 mUserWidth = w;
609 mUserHeight = h;
Michael I. Gold55a70142012-04-09 19:46:29 -0700610 return NO_ERROR;
611}
612
Jamie Gennisbee205f2011-07-01 13:12:07 -0700613int SurfaceTextureClient::setBuffersFormat(int format)
614{
Steve Block6807e592011-10-20 11:56:00 +0100615 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennisbee205f2011-07-01 13:12:07 -0700616
617 if (format<0)
618 return BAD_VALUE;
619
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700620 Mutex::Autolock lock(mMutex);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700621 mReqFormat = format;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700622 return NO_ERROR;
623}
624
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700625int SurfaceTextureClient::setScalingMode(int mode)
626{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800627 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100628 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700629
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700630 switch (mode) {
631 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
632 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
Daniel Lam016c8cb2012-04-03 15:54:58 -0700633 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700634 break;
635 default:
636 ALOGE("unknown scaling mode: %d", mode);
637 return BAD_VALUE;
638 }
639
640 Mutex::Autolock lock(mMutex);
641 mScalingMode = mode;
642 return NO_ERROR;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700643}
644
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800645int SurfaceTextureClient::setBuffersTransform(int transform)
646{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800647 ATRACE_CALL();
Steve Block6807e592011-10-20 11:56:00 +0100648 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800649 Mutex::Autolock lock(mMutex);
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700650 mTransform = transform;
651 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800652}
653
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800654int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
655{
Steve Block6807e592011-10-20 11:56:00 +0100656 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800657 Mutex::Autolock lock(mMutex);
658 mTimestamp = timestamp;
659 return NO_ERROR;
660}
661
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800662void SurfaceTextureClient::freeAllBuffers() {
663 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopianac6035a2012-04-12 16:32:37 -0700664 mSlots[i].buffer = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800665 }
666}
667
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700668// ----------------------------------------------------------------------
669// the lock/unlock APIs must be used from the same thread
670
671static status_t copyBlt(
672 const sp<GraphicBuffer>& dst,
673 const sp<GraphicBuffer>& src,
674 const Region& reg)
675{
676 // src and dst with, height and format must be identical. no verification
677 // is done here.
678 status_t err;
679 uint8_t const * src_bits = NULL;
680 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000681 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700682
683 uint8_t* dst_bits = NULL;
684 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000685 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700686
687 Region::const_iterator head(reg.begin());
688 Region::const_iterator tail(reg.end());
689 if (head != tail && src_bits && dst_bits) {
690 const size_t bpp = bytesPerPixel(src->format);
691 const size_t dbpr = dst->stride * bpp;
692 const size_t sbpr = src->stride * bpp;
693
694 while (head != tail) {
695 const Rect& r(*head++);
696 ssize_t h = r.height();
697 if (h <= 0) continue;
698 size_t size = r.width() * bpp;
699 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
700 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
701 if (dbpr==sbpr && size==sbpr) {
702 size *= h;
703 h = 1;
704 }
705 do {
706 memcpy(d, s, size);
707 d += dbpr;
708 s += sbpr;
709 } while (--h > 0);
710 }
711 }
712
713 if (src_bits)
714 src->unlock();
715
716 if (dst_bits)
717 dst->unlock();
718
719 return err;
720}
721
722// ----------------------------------------------------------------------------
723
724status_t SurfaceTextureClient::lock(
725 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
726{
727 if (mLockedBuffer != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000728 ALOGE("Surface::lock failed, already locked");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700729 return INVALID_OPERATION;
730 }
731
732 if (!mConnectedToCpu) {
733 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
734 if (err) {
735 return err;
736 }
737 // we're intending to do software rendering from this point
738 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
739 }
740
741 ANativeWindowBuffer* out;
742 status_t err = dequeueBuffer(&out);
Steve Blocke6f43dd2012-01-06 19:20:56 +0000743 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700744 if (err == NO_ERROR) {
745 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
746 err = lockBuffer(backBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000747 ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700748 backBuffer->handle, strerror(-err));
749 if (err == NO_ERROR) {
750 const Rect bounds(backBuffer->width, backBuffer->height);
751
752 Region newDirtyRegion;
753 if (inOutDirtyBounds) {
754 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
755 newDirtyRegion.andSelf(bounds);
756 } else {
757 newDirtyRegion.set(bounds);
758 }
759
760 // figure out if we can copy the frontbuffer back
761 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
762 const bool canCopyBack = (frontBuffer != 0 &&
763 backBuffer->width == frontBuffer->width &&
764 backBuffer->height == frontBuffer->height &&
765 backBuffer->format == frontBuffer->format);
766
767 if (canCopyBack) {
768 // copy the area that is invalid and not repainted this round
Mathias Agopianac6035a2012-04-12 16:32:37 -0700769 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700770 if (!copyback.isEmpty())
771 copyBlt(backBuffer, frontBuffer, copyback);
772 } else {
773 // if we can't copy-back anything, modify the user's dirty
774 // region to make sure they redraw the whole buffer
775 newDirtyRegion.set(bounds);
Mathias Agopianac6035a2012-04-12 16:32:37 -0700776 mDirtyRegion.clear();
777 Mutex::Autolock lock(mMutex);
778 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
779 mSlots[i].dirtyRegion.clear();
780 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700781 }
782
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700783
Mathias Agopianac6035a2012-04-12 16:32:37 -0700784 { // scope for the lock
785 Mutex::Autolock lock(mMutex);
786 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
787 if (backBufferSlot >= 0) {
788 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
789 mDirtyRegion.subtract(dirtyRegion);
790 dirtyRegion = newDirtyRegion;
791 }
792 }
793
794 mDirtyRegion.orSelf(newDirtyRegion);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700795 if (inOutDirtyBounds) {
796 *inOutDirtyBounds = newDirtyRegion.getBounds();
797 }
798
799 void* vaddr;
800 status_t res = backBuffer->lock(
801 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
802 newDirtyRegion.bounds(), &vaddr);
803
Steve Block32397c12012-01-05 23:22:43 +0000804 ALOGW_IF(res, "failed locking buffer (handle = %p)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700805 backBuffer->handle);
806
807 mLockedBuffer = backBuffer;
808 outBuffer->width = backBuffer->width;
809 outBuffer->height = backBuffer->height;
810 outBuffer->stride = backBuffer->stride;
811 outBuffer->format = backBuffer->format;
812 outBuffer->bits = vaddr;
813 }
814 }
815 return err;
816}
817
818status_t SurfaceTextureClient::unlockAndPost()
819{
820 if (mLockedBuffer == 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000821 ALOGE("Surface::unlockAndPost failed, no locked buffer");
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700822 return INVALID_OPERATION;
823 }
824
825 status_t err = mLockedBuffer->unlock();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000826 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700827
828 err = queueBuffer(mLockedBuffer.get());
Steve Blocke6f43dd2012-01-06 19:20:56 +0000829 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700830 mLockedBuffer->handle, strerror(-err));
831
832 mPostedBuffer = mLockedBuffer;
833 mLockedBuffer = 0;
834 return err;
835}
836
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800837}; // namespace android