blob: f66e25f1dfd4966a49a28783ad0fb2bfd6398aa3 [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 Gennise5366c52011-01-12 20:22:41 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#include <gui/SurfaceTextureClient.h>
Jamie Gennis582270d2011-08-17 18:19:00 -070021#include <surfaceflinger/ISurfaceComposer.h>
22#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080023
24#include <utils/Log.h>
25
26namespace android {
27
28SurfaceTextureClient::SurfaceTextureClient(
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070029 const sp<ISurfaceTexture>& surfaceTexture)
30{
31 SurfaceTextureClient::init();
32 SurfaceTextureClient::setISurfaceTexture(surfaceTexture);
33}
34
35SurfaceTextureClient::SurfaceTextureClient() {
36 SurfaceTextureClient::init();
37}
38
39void SurfaceTextureClient::init() {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080040 // Initialize the ANativeWindow function pointers.
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070041 ANativeWindow::setSwapInterval = hook_setSwapInterval;
42 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
43 ANativeWindow::cancelBuffer = hook_cancelBuffer;
44 ANativeWindow::lockBuffer = hook_lockBuffer;
45 ANativeWindow::queueBuffer = hook_queueBuffer;
46 ANativeWindow::query = hook_query;
47 ANativeWindow::perform = hook_perform;
Jamie Gennis1b20cde2011-02-02 15:31:47 -080048
Mathias Agopian80727112011-05-02 19:51:12 -070049 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
50 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
51
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070052 mReqWidth = 0;
53 mReqHeight = 0;
54 mReqFormat = 0;
55 mReqUsage = 0;
56 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Mathias Agopianbb66c9b2011-07-21 14:50:29 -070057 mDefaultWidth = 0;
58 mDefaultHeight = 0;
59 mTransformHint = 0;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070060 mConnectedToCpu = false;
61}
62
63void SurfaceTextureClient::setISurfaceTexture(
64 const sp<ISurfaceTexture>& surfaceTexture)
65{
66 mSurfaceTexture = surfaceTexture;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080067}
68
Jamie Gennisbae774e2011-03-14 15:08:53 -070069sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
70 return mSurfaceTexture;
71}
72
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070073int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080074 SurfaceTextureClient* c = getSelf(window);
75 return c->setSwapInterval(interval);
76}
77
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070078int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070079 ANativeWindowBuffer** buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080080 SurfaceTextureClient* c = getSelf(window);
81 return c->dequeueBuffer(buffer);
82}
83
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070084int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070085 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080086 SurfaceTextureClient* c = getSelf(window);
87 return c->cancelBuffer(buffer);
88}
89
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070090int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070091 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080092 SurfaceTextureClient* c = getSelf(window);
93 return c->lockBuffer(buffer);
94}
95
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070096int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchev697526b2011-05-01 11:33:26 -070097 ANativeWindowBuffer* buffer) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080098 SurfaceTextureClient* c = getSelf(window);
99 return c->queueBuffer(buffer);
100}
101
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700102int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev41abd672011-04-14 16:54:38 -0700103 int what, int* value) {
104 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800105 return c->query(what, value);
106}
107
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700108int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800109 va_list args;
110 va_start(args, operation);
111 SurfaceTextureClient* c = getSelf(window);
112 return c->perform(operation, args);
113}
114
115int SurfaceTextureClient::setSwapInterval(int interval) {
Mathias Agopian80727112011-05-02 19:51:12 -0700116 // EGL specification states:
117 // interval is silently clamped to minimum and maximum implementation
118 // dependent values before being stored.
119 // Although we don't have to, we apply the same logic here.
120
121 if (interval < minSwapInterval)
122 interval = minSwapInterval;
123
124 if (interval > maxSwapInterval)
125 interval = maxSwapInterval;
126
127 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
128
129 return res;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800130}
131
132int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100133 ALOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800134 Mutex::Autolock lock(mMutex);
135 int buf = -1;
Mathias Agopian80727112011-05-02 19:51:12 -0700136 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopianc04f1532011-04-25 20:22:14 -0700137 mReqFormat, mReqUsage);
Mathias Agopian80727112011-05-02 19:51:12 -0700138 if (result < 0) {
Steve Block6807e592011-10-20 11:56:00 +0100139 ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700140 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
141 result);
Mathias Agopian80727112011-05-02 19:51:12 -0700142 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800143 }
144 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian80727112011-05-02 19:51:12 -0700145 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
146 freeAllBuffers();
147 }
148
149 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700150 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
151 if (result != NO_ERROR) {
152 LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
153 result);
154 return result;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800155 }
156 }
157 *buffer = gbuf.get();
158 return OK;
159}
160
161int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100162 ALOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800163 Mutex::Autolock lock(mMutex);
Jamie Gennis1c441402011-06-20 12:04:09 -0700164 int i = getSlotFromBufferLocked(buffer);
165 if (i < 0) {
166 return i;
167 }
168 mSurfaceTexture->cancelBuffer(i);
169 return OK;
170}
171
172int SurfaceTextureClient::getSlotFromBufferLocked(
173 android_native_buffer_t* buffer) const {
174 bool dumpedState = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800175 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennis1c441402011-06-20 12:04:09 -0700176 // XXX: Dump the slots whenever we hit a NULL entry while searching for
177 // a buffer.
178 if (mSlots[i] == NULL) {
179 if (!dumpedState) {
180 LOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
181 "looking for buffer %p", i, buffer->handle);
182 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
183 if (mSlots[j] == NULL) {
184 LOGD("getSlotFromBufferLocked: %02d: NULL", j);
185 } else {
186 LOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
187 }
188 }
189 dumpedState = true;
190 }
191 }
192
193 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
194 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 }
196 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700197 LOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800198 return BAD_VALUE;
199}
200
201int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100202 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800203 Mutex::Autolock lock(mMutex);
204 return OK;
205}
206
207int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Steve Block6807e592011-10-20 11:56:00 +0100208 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800209 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800210 int64_t timestamp;
211 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
212 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block6807e592011-10-20 11:56:00 +0100213 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800214 timestamp / 1000000.f);
215 } else {
216 timestamp = mTimestamp;
217 }
Jamie Gennis1c441402011-06-20 12:04:09 -0700218 int i = getSlotFromBufferLocked(buffer);
219 if (i < 0) {
220 return i;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800221 }
Pannag Sanketi66378c62011-09-02 17:37:29 -0700222 status_t err = mSurfaceTexture->queueBuffer(i, timestamp,
Mathias Agopian97c602c2011-07-19 15:24:46 -0700223 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketi66378c62011-09-02 17:37:29 -0700224 if (err != OK) {
225 LOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
226 }
227 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800228}
229
Iliyan Malchev41abd672011-04-14 16:54:38 -0700230int SurfaceTextureClient::query(int what, int* value) const {
Steve Block6807e592011-10-20 11:56:00 +0100231 ALOGV("SurfaceTextureClient::query");
Mathias Agopian97c602c2011-07-19 15:24:46 -0700232 { // scope for the lock
233 Mutex::Autolock lock(mMutex);
234 switch (what) {
235 case NATIVE_WINDOW_FORMAT:
236 if (mReqFormat) {
237 *value = mReqFormat;
238 return NO_ERROR;
239 }
240 break;
241 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis582270d2011-08-17 18:19:00 -0700242 {
243 sp<ISurfaceComposer> composer(
244 ComposerService::getComposerService());
245 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
246 *value = 1;
247 } else {
248 *value = 0;
249 }
250 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700251 return NO_ERROR;
252 case NATIVE_WINDOW_CONCRETE_TYPE:
253 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
254 return NO_ERROR;
255 case NATIVE_WINDOW_DEFAULT_WIDTH:
256 *value = mDefaultWidth;
257 return NO_ERROR;
258 case NATIVE_WINDOW_DEFAULT_HEIGHT:
259 *value = mDefaultHeight;
260 return NO_ERROR;
261 case NATIVE_WINDOW_TRANSFORM_HINT:
262 *value = mTransformHint;
263 return NO_ERROR;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700264 }
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800265 }
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700266 return mSurfaceTexture->query(what, value);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800267}
268
269int SurfaceTextureClient::perform(int operation, va_list args)
270{
271 int res = NO_ERROR;
272 switch (operation) {
273 case NATIVE_WINDOW_CONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700274 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800275 break;
276 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian81a63352011-07-29 17:55:48 -0700277 // deprecated. must return NO_ERROR.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800278 break;
279 case NATIVE_WINDOW_SET_USAGE:
280 res = dispatchSetUsage(args);
281 break;
282 case NATIVE_WINDOW_SET_CROP:
283 res = dispatchSetCrop(args);
284 break;
285 case NATIVE_WINDOW_SET_BUFFER_COUNT:
286 res = dispatchSetBufferCount(args);
287 break;
288 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
289 res = dispatchSetBuffersGeometry(args);
290 break;
291 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
292 res = dispatchSetBuffersTransform(args);
293 break;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800294 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
295 res = dispatchSetBuffersTimestamp(args);
296 break;
Jamie Gennisbee205f2011-07-01 13:12:07 -0700297 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
298 res = dispatchSetBuffersDimensions(args);
299 break;
300 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
301 res = dispatchSetBuffersFormat(args);
302 break;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700303 case NATIVE_WINDOW_LOCK:
304 res = dispatchLock(args);
305 break;
306 case NATIVE_WINDOW_UNLOCK_AND_POST:
307 res = dispatchUnlockAndPost(args);
308 break;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700309 case NATIVE_WINDOW_SET_SCALING_MODE:
310 res = dispatchSetScalingMode(args);
311 break;
Mathias Agopian81a63352011-07-29 17:55:48 -0700312 case NATIVE_WINDOW_API_CONNECT:
313 res = dispatchConnect(args);
314 break;
315 case NATIVE_WINDOW_API_DISCONNECT:
316 res = dispatchDisconnect(args);
317 break;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800318 default:
319 res = NAME_NOT_FOUND;
320 break;
321 }
322 return res;
323}
324
325int SurfaceTextureClient::dispatchConnect(va_list args) {
326 int api = va_arg(args, int);
327 return connect(api);
328}
329
330int SurfaceTextureClient::dispatchDisconnect(va_list args) {
331 int api = va_arg(args, int);
332 return disconnect(api);
333}
334
335int SurfaceTextureClient::dispatchSetUsage(va_list args) {
336 int usage = va_arg(args, int);
337 return setUsage(usage);
338}
339
340int SurfaceTextureClient::dispatchSetCrop(va_list args) {
341 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
342 return setCrop(reinterpret_cast<Rect const*>(rect));
343}
344
345int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
346 size_t bufferCount = va_arg(args, size_t);
347 return setBufferCount(bufferCount);
348}
349
350int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
351 int w = va_arg(args, int);
352 int h = va_arg(args, int);
353 int f = va_arg(args, int);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700354 int err = setBuffersDimensions(w, h);
355 if (err != 0) {
356 return err;
357 }
358 return setBuffersFormat(f);
359}
360
361int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
362 int w = va_arg(args, int);
363 int h = va_arg(args, int);
364 return setBuffersDimensions(w, h);
365}
366
367int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
368 int f = va_arg(args, int);
369 return setBuffersFormat(f);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800370}
371
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700372int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
373 int m = va_arg(args, int);
374 return setScalingMode(m);
375}
376
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800377int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
378 int transform = va_arg(args, int);
379 return setBuffersTransform(transform);
380}
381
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800382int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
383 int64_t timestamp = va_arg(args, int64_t);
384 return setBuffersTimestamp(timestamp);
385}
386
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700387int SurfaceTextureClient::dispatchLock(va_list args) {
388 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
389 ARect* inOutDirtyBounds = va_arg(args, ARect*);
390 return lock(outBuffer, inOutDirtyBounds);
391}
392
393int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
394 return unlockAndPost();
395}
396
397
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800398int SurfaceTextureClient::connect(int api) {
Steve Block6807e592011-10-20 11:56:00 +0100399 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700400 Mutex::Autolock lock(mMutex);
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700401 int err = mSurfaceTexture->connect(api,
402 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700403 if (!err && api == NATIVE_WINDOW_API_CPU) {
404 mConnectedToCpu = true;
405 }
406 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800407}
408
409int SurfaceTextureClient::disconnect(int api) {
Steve Block6807e592011-10-20 11:56:00 +0100410 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700411 Mutex::Autolock lock(mMutex);
Jamie Gennis13c5ca32011-10-18 17:14:33 -0700412 freeAllBuffers();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700413 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian70e3f812011-08-25 17:03:30 -0700414 if (!err) {
Mathias Agopian70e3f812011-08-25 17:03:30 -0700415 mReqFormat = 0;
416 mReqWidth = 0;
417 mReqHeight = 0;
418 mReqUsage = 0;
419 if (api == NATIVE_WINDOW_API_CPU) {
420 mConnectedToCpu = false;
421 }
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700422 }
423 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800424}
425
426int SurfaceTextureClient::setUsage(uint32_t reqUsage)
427{
Steve Block6807e592011-10-20 11:56:00 +0100428 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800429 Mutex::Autolock lock(mMutex);
430 mReqUsage = reqUsage;
431 return OK;
432}
433
434int SurfaceTextureClient::setCrop(Rect const* rect)
435{
Steve Block6807e592011-10-20 11:56:00 +0100436 ALOGV("SurfaceTextureClient::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800437 Mutex::Autolock lock(mMutex);
438
Jamie Gennis68f91272011-01-28 18:21:54 -0800439 Rect realRect;
440 if (rect == NULL || rect->isEmpty()) {
441 realRect = Rect(0, 0);
442 } else {
443 realRect = *rect;
444 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800445
446 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis68f91272011-01-28 18:21:54 -0800447 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800448
449 return err;
450}
451
452int SurfaceTextureClient::setBufferCount(int bufferCount)
453{
Steve Block6807e592011-10-20 11:56:00 +0100454 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800455 Mutex::Autolock lock(mMutex);
456
457 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
458 LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
459 bufferCount, strerror(-err));
460
461 if (err == NO_ERROR) {
462 freeAllBuffers();
463 }
464
465 return err;
466}
467
Jamie Gennisbee205f2011-07-01 13:12:07 -0700468int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800469{
Steve Block6807e592011-10-20 11:56:00 +0100470 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800471 Mutex::Autolock lock(mMutex);
472
Jamie Gennisbee205f2011-07-01 13:12:07 -0700473 if (w<0 || h<0)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800474 return BAD_VALUE;
475
476 if ((w && !h) || (!w && h))
477 return BAD_VALUE;
478
479 mReqWidth = w;
480 mReqHeight = h;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800481
Jamie Gennis68f91272011-01-28 18:21:54 -0800482 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
483 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
484
485 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800486}
487
Jamie Gennisbee205f2011-07-01 13:12:07 -0700488int SurfaceTextureClient::setBuffersFormat(int format)
489{
Steve Block6807e592011-10-20 11:56:00 +0100490 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennisbee205f2011-07-01 13:12:07 -0700491 Mutex::Autolock lock(mMutex);
492
493 if (format<0)
494 return BAD_VALUE;
495
496 mReqFormat = format;
497
498 return NO_ERROR;
499}
500
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700501int SurfaceTextureClient::setScalingMode(int mode)
502{
Steve Block6807e592011-10-20 11:56:00 +0100503 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700504 Mutex::Autolock lock(mMutex);
505 // mode is validated on the server
506 status_t err = mSurfaceTexture->setScalingMode(mode);
507 LOGE_IF(err, "ISurfaceTexture::setScalingMode(%d) returned %s",
508 mode, strerror(-err));
509
510 return err;
511}
512
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800513int SurfaceTextureClient::setBuffersTransform(int transform)
514{
Steve Block6807e592011-10-20 11:56:00 +0100515 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800516 Mutex::Autolock lock(mMutex);
517 status_t err = mSurfaceTexture->setTransform(transform);
518 return err;
519}
520
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800521int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
522{
Steve Block6807e592011-10-20 11:56:00 +0100523 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800524 Mutex::Autolock lock(mMutex);
525 mTimestamp = timestamp;
526 return NO_ERROR;
527}
528
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800529void SurfaceTextureClient::freeAllBuffers() {
530 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
531 mSlots[i] = 0;
532 }
533}
534
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700535// ----------------------------------------------------------------------
536// the lock/unlock APIs must be used from the same thread
537
538static status_t copyBlt(
539 const sp<GraphicBuffer>& dst,
540 const sp<GraphicBuffer>& src,
541 const Region& reg)
542{
543 // src and dst with, height and format must be identical. no verification
544 // is done here.
545 status_t err;
546 uint8_t const * src_bits = NULL;
547 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
548 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
549
550 uint8_t* dst_bits = NULL;
551 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
552 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
553
554 Region::const_iterator head(reg.begin());
555 Region::const_iterator tail(reg.end());
556 if (head != tail && src_bits && dst_bits) {
557 const size_t bpp = bytesPerPixel(src->format);
558 const size_t dbpr = dst->stride * bpp;
559 const size_t sbpr = src->stride * bpp;
560
561 while (head != tail) {
562 const Rect& r(*head++);
563 ssize_t h = r.height();
564 if (h <= 0) continue;
565 size_t size = r.width() * bpp;
566 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
567 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
568 if (dbpr==sbpr && size==sbpr) {
569 size *= h;
570 h = 1;
571 }
572 do {
573 memcpy(d, s, size);
574 d += dbpr;
575 s += sbpr;
576 } while (--h > 0);
577 }
578 }
579
580 if (src_bits)
581 src->unlock();
582
583 if (dst_bits)
584 dst->unlock();
585
586 return err;
587}
588
589// ----------------------------------------------------------------------------
590
591status_t SurfaceTextureClient::lock(
592 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
593{
594 if (mLockedBuffer != 0) {
595 LOGE("Surface::lock failed, already locked");
596 return INVALID_OPERATION;
597 }
598
599 if (!mConnectedToCpu) {
600 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
601 if (err) {
602 return err;
603 }
604 // we're intending to do software rendering from this point
605 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
606 }
607
608 ANativeWindowBuffer* out;
609 status_t err = dequeueBuffer(&out);
610 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
611 if (err == NO_ERROR) {
612 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
613 err = lockBuffer(backBuffer.get());
614 LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
615 backBuffer->handle, strerror(-err));
616 if (err == NO_ERROR) {
617 const Rect bounds(backBuffer->width, backBuffer->height);
618
619 Region newDirtyRegion;
620 if (inOutDirtyBounds) {
621 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
622 newDirtyRegion.andSelf(bounds);
623 } else {
624 newDirtyRegion.set(bounds);
625 }
626
627 // figure out if we can copy the frontbuffer back
628 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
629 const bool canCopyBack = (frontBuffer != 0 &&
630 backBuffer->width == frontBuffer->width &&
631 backBuffer->height == frontBuffer->height &&
632 backBuffer->format == frontBuffer->format);
633
634 if (canCopyBack) {
635 // copy the area that is invalid and not repainted this round
636 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
637 if (!copyback.isEmpty())
638 copyBlt(backBuffer, frontBuffer, copyback);
639 } else {
640 // if we can't copy-back anything, modify the user's dirty
641 // region to make sure they redraw the whole buffer
642 newDirtyRegion.set(bounds);
643 }
644
645 // keep track of the are of the buffer that is "clean"
646 // (ie: that will be redrawn)
647 mOldDirtyRegion = newDirtyRegion;
648
649 if (inOutDirtyBounds) {
650 *inOutDirtyBounds = newDirtyRegion.getBounds();
651 }
652
653 void* vaddr;
654 status_t res = backBuffer->lock(
655 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
656 newDirtyRegion.bounds(), &vaddr);
657
658 LOGW_IF(res, "failed locking buffer (handle = %p)",
659 backBuffer->handle);
660
661 mLockedBuffer = backBuffer;
662 outBuffer->width = backBuffer->width;
663 outBuffer->height = backBuffer->height;
664 outBuffer->stride = backBuffer->stride;
665 outBuffer->format = backBuffer->format;
666 outBuffer->bits = vaddr;
667 }
668 }
669 return err;
670}
671
672status_t SurfaceTextureClient::unlockAndPost()
673{
674 if (mLockedBuffer == 0) {
675 LOGE("Surface::unlockAndPost failed, no locked buffer");
676 return INVALID_OPERATION;
677 }
678
679 status_t err = mLockedBuffer->unlock();
680 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
681
682 err = queueBuffer(mLockedBuffer.get());
683 LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
684 mLockedBuffer->handle, strerror(-err));
685
686 mPostedBuffer = mLockedBuffer;
687 mLockedBuffer = 0;
688 return err;
689}
690
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800691}; // namespace android