blob: b6f3c11ff15969e189588d75766224e711a41ee1 [file] [log] [blame]
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTextureClient"
Jamie Gennise8d0e8a2011-01-12 20:22:41 -080018//#define LOG_NDEBUG 0
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080019
20#include <gui/SurfaceTextureClient.h>
Jamie Gennis9b8fc652011-08-17 18:19:00 -070021#include <surfaceflinger/ISurfaceComposer.h>
22#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080023
24#include <utils/Log.h>
25
Mathias Agopian3f157002011-11-17 17:48:35 -080026#include <private/gui/ComposerService.h>
27
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080028namespace android {
29
30SurfaceTextureClient::SurfaceTextureClient(
Mathias Agopian949be322011-07-13 17:39:11 -070031 const sp<ISurfaceTexture>& surfaceTexture)
32{
33 SurfaceTextureClient::init();
34 SurfaceTextureClient::setISurfaceTexture(surfaceTexture);
35}
36
37SurfaceTextureClient::SurfaceTextureClient() {
38 SurfaceTextureClient::init();
39}
40
Mathias Agopian90cbbd12011-11-17 18:46:09 -080041SurfaceTextureClient::~SurfaceTextureClient() {
42 if (mConnectedToCpu) {
43 SurfaceTextureClient::disconnect(NATIVE_WINDOW_API_CPU);
44 }
45}
46
Mathias Agopian949be322011-07-13 17:39:11 -070047void SurfaceTextureClient::init() {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080048 // Initialize the ANativeWindow function pointers.
Mathias Agopian949be322011-07-13 17:39:11 -070049 ANativeWindow::setSwapInterval = hook_setSwapInterval;
50 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
51 ANativeWindow::cancelBuffer = hook_cancelBuffer;
52 ANativeWindow::lockBuffer = hook_lockBuffer;
53 ANativeWindow::queueBuffer = hook_queueBuffer;
54 ANativeWindow::query = hook_query;
55 ANativeWindow::perform = hook_perform;
Jamie Gennis83bac212011-02-02 15:31:47 -080056
Mathias Agopian402ff242011-05-02 19:51:12 -070057 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
58 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
59
Mathias Agopian949be322011-07-13 17:39:11 -070060 mReqWidth = 0;
61 mReqHeight = 0;
62 mReqFormat = 0;
63 mReqUsage = 0;
64 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
Mathias Agopian45b63dd2011-07-21 14:50:29 -070065 mDefaultWidth = 0;
66 mDefaultHeight = 0;
67 mTransformHint = 0;
Mathias Agopian949be322011-07-13 17:39:11 -070068 mConnectedToCpu = false;
69}
70
71void SurfaceTextureClient::setISurfaceTexture(
72 const sp<ISurfaceTexture>& surfaceTexture)
73{
74 mSurfaceTexture = surfaceTexture;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080075}
76
Jamie Gennisf95a9f02011-03-14 15:08:53 -070077sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
78 return mSurfaceTexture;
79}
80
Mathias Agopian949be322011-07-13 17:39:11 -070081int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080082 SurfaceTextureClient* c = getSelf(window);
83 return c->setSwapInterval(interval);
84}
85
Mathias Agopian949be322011-07-13 17:39:11 -070086int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070087 ANativeWindowBuffer** buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080088 SurfaceTextureClient* c = getSelf(window);
89 return c->dequeueBuffer(buffer);
90}
91
Mathias Agopian949be322011-07-13 17:39:11 -070092int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070093 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080094 SurfaceTextureClient* c = getSelf(window);
95 return c->cancelBuffer(buffer);
96}
97
Mathias Agopian949be322011-07-13 17:39:11 -070098int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070099 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800100 SurfaceTextureClient* c = getSelf(window);
101 return c->lockBuffer(buffer);
102}
103
Mathias Agopian949be322011-07-13 17:39:11 -0700104int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700105 ANativeWindowBuffer* buffer) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800106 SurfaceTextureClient* c = getSelf(window);
107 return c->queueBuffer(buffer);
108}
109
Mathias Agopian949be322011-07-13 17:39:11 -0700110int SurfaceTextureClient::hook_query(const ANativeWindow* window,
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700111 int what, int* value) {
112 const SurfaceTextureClient* c = getSelf(window);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800113 return c->query(what, value);
114}
115
Mathias Agopian949be322011-07-13 17:39:11 -0700116int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800117 va_list args;
118 va_start(args, operation);
119 SurfaceTextureClient* c = getSelf(window);
120 return c->perform(operation, args);
121}
122
123int SurfaceTextureClient::setSwapInterval(int interval) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700124 // EGL specification states:
125 // interval is silently clamped to minimum and maximum implementation
126 // dependent values before being stored.
127 // Although we don't have to, we apply the same logic here.
128
129 if (interval < minSwapInterval)
130 interval = minSwapInterval;
131
132 if (interval > maxSwapInterval)
133 interval = maxSwapInterval;
134
135 status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
136
137 return res;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800138}
139
140int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
Steve Block71f2cf12011-10-20 11:56:00 +0100141 ALOGV("SurfaceTextureClient::dequeueBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800142 Mutex::Autolock lock(mMutex);
143 int buf = -1;
Mathias Agopian402ff242011-05-02 19:51:12 -0700144 status_t result = mSurfaceTexture->dequeueBuffer(&buf, mReqWidth, mReqHeight,
Mathias Agopian0297dca2011-04-25 20:22:14 -0700145 mReqFormat, mReqUsage);
Mathias Agopian402ff242011-05-02 19:51:12 -0700146 if (result < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100147 ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
Jamie Gennisa2187152011-05-19 13:33:00 -0700148 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
149 result);
Mathias Agopian402ff242011-05-02 19:51:12 -0700150 return result;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800151 }
152 sp<GraphicBuffer>& gbuf(mSlots[buf]);
Mathias Agopian402ff242011-05-02 19:51:12 -0700153 if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
154 freeAllBuffers();
155 }
156
157 if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700158 result = mSurfaceTexture->requestBuffer(buf, &gbuf);
159 if (result != NO_ERROR) {
160 LOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
161 result);
162 return result;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800163 }
164 }
165 *buffer = gbuf.get();
166 return OK;
167}
168
169int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
Steve Block71f2cf12011-10-20 11:56:00 +0100170 ALOGV("SurfaceTextureClient::cancelBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800171 Mutex::Autolock lock(mMutex);
Jamie Gennisaff74052011-06-20 12:04:09 -0700172 int i = getSlotFromBufferLocked(buffer);
173 if (i < 0) {
174 return i;
175 }
176 mSurfaceTexture->cancelBuffer(i);
177 return OK;
178}
179
180int SurfaceTextureClient::getSlotFromBufferLocked(
181 android_native_buffer_t* buffer) const {
182 bool dumpedState = false;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800183 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Jamie Gennisaff74052011-06-20 12:04:09 -0700184 // XXX: Dump the slots whenever we hit a NULL entry while searching for
185 // a buffer.
186 if (mSlots[i] == NULL) {
187 if (!dumpedState) {
Steve Block5baa3a62011-12-20 16:23:08 +0000188 ALOGD("getSlotFromBufferLocked: encountered NULL buffer in slot %d "
Jamie Gennisaff74052011-06-20 12:04:09 -0700189 "looking for buffer %p", i, buffer->handle);
190 for (int j = 0; j < NUM_BUFFER_SLOTS; j++) {
191 if (mSlots[j] == NULL) {
Steve Block5baa3a62011-12-20 16:23:08 +0000192 ALOGD("getSlotFromBufferLocked: %02d: NULL", j);
Jamie Gennisaff74052011-06-20 12:04:09 -0700193 } else {
Steve Block5baa3a62011-12-20 16:23:08 +0000194 ALOGD("getSlotFromBufferLocked: %02d: %p", j, mSlots[j]->handle);
Jamie Gennisaff74052011-06-20 12:04:09 -0700195 }
196 }
197 dumpedState = true;
198 }
199 }
200
201 if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
202 return i;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800203 }
204 }
Jamie Gennisaff74052011-06-20 12:04:09 -0700205 LOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800206 return BAD_VALUE;
207}
208
209int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
Steve Block71f2cf12011-10-20 11:56:00 +0100210 ALOGV("SurfaceTextureClient::lockBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800211 Mutex::Autolock lock(mMutex);
212 return OK;
213}
214
215int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
Steve Block71f2cf12011-10-20 11:56:00 +0100216 ALOGV("SurfaceTextureClient::queueBuffer");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800217 Mutex::Autolock lock(mMutex);
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800218 int64_t timestamp;
219 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
220 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Steve Block71f2cf12011-10-20 11:56:00 +0100221 ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800222 timestamp / 1000000.f);
223 } else {
224 timestamp = mTimestamp;
225 }
Jamie Gennisaff74052011-06-20 12:04:09 -0700226 int i = getSlotFromBufferLocked(buffer);
227 if (i < 0) {
228 return i;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800229 }
Pannag Sanketie0617952011-09-02 17:37:29 -0700230 status_t err = mSurfaceTexture->queueBuffer(i, timestamp,
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700231 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Pannag Sanketie0617952011-09-02 17:37:29 -0700232 if (err != OK) {
233 LOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
234 }
235 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800236}
237
Iliyan Malchev4d7c1ce2011-04-14 16:54:38 -0700238int SurfaceTextureClient::query(int what, int* value) const {
Steve Block71f2cf12011-10-20 11:56:00 +0100239 ALOGV("SurfaceTextureClient::query");
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700240 { // scope for the lock
241 Mutex::Autolock lock(mMutex);
242 switch (what) {
243 case NATIVE_WINDOW_FORMAT:
244 if (mReqFormat) {
245 *value = mReqFormat;
246 return NO_ERROR;
247 }
248 break;
249 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
Jamie Gennis9b8fc652011-08-17 18:19:00 -0700250 {
251 sp<ISurfaceComposer> composer(
252 ComposerService::getComposerService());
253 if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
254 *value = 1;
255 } else {
256 *value = 0;
257 }
258 }
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700259 return NO_ERROR;
260 case NATIVE_WINDOW_CONCRETE_TYPE:
261 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
262 return NO_ERROR;
263 case NATIVE_WINDOW_DEFAULT_WIDTH:
264 *value = mDefaultWidth;
265 return NO_ERROR;
266 case NATIVE_WINDOW_DEFAULT_HEIGHT:
267 *value = mDefaultHeight;
268 return NO_ERROR;
269 case NATIVE_WINDOW_TRANSFORM_HINT:
270 *value = mTransformHint;
271 return NO_ERROR;
Mathias Agopian7bb843c2011-04-20 14:20:59 -0700272 }
Jamie Gennis96dcc972011-02-27 14:10:20 -0800273 }
Mathias Agopianed3894c2011-04-20 14:20:59 -0700274 return mSurfaceTexture->query(what, value);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800275}
276
277int SurfaceTextureClient::perform(int operation, va_list args)
278{
279 int res = NO_ERROR;
280 switch (operation) {
281 case NATIVE_WINDOW_CONNECT:
Mathias Agopian982d2da2011-07-29 17:55:48 -0700282 // deprecated. must return NO_ERROR.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800283 break;
284 case NATIVE_WINDOW_DISCONNECT:
Mathias Agopian982d2da2011-07-29 17:55:48 -0700285 // deprecated. must return NO_ERROR.
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800286 break;
287 case NATIVE_WINDOW_SET_USAGE:
288 res = dispatchSetUsage(args);
289 break;
290 case NATIVE_WINDOW_SET_CROP:
291 res = dispatchSetCrop(args);
292 break;
293 case NATIVE_WINDOW_SET_BUFFER_COUNT:
294 res = dispatchSetBufferCount(args);
295 break;
296 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
297 res = dispatchSetBuffersGeometry(args);
298 break;
299 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
300 res = dispatchSetBuffersTransform(args);
301 break;
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800302 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
303 res = dispatchSetBuffersTimestamp(args);
304 break;
Jamie Gennis97eae022011-07-01 13:12:07 -0700305 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
306 res = dispatchSetBuffersDimensions(args);
307 break;
308 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
309 res = dispatchSetBuffersFormat(args);
310 break;
Mathias Agopian949be322011-07-13 17:39:11 -0700311 case NATIVE_WINDOW_LOCK:
312 res = dispatchLock(args);
313 break;
314 case NATIVE_WINDOW_UNLOCK_AND_POST:
315 res = dispatchUnlockAndPost(args);
316 break;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700317 case NATIVE_WINDOW_SET_SCALING_MODE:
318 res = dispatchSetScalingMode(args);
319 break;
Mathias Agopian982d2da2011-07-29 17:55:48 -0700320 case NATIVE_WINDOW_API_CONNECT:
321 res = dispatchConnect(args);
322 break;
323 case NATIVE_WINDOW_API_DISCONNECT:
324 res = dispatchDisconnect(args);
325 break;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800326 default:
327 res = NAME_NOT_FOUND;
328 break;
329 }
330 return res;
331}
332
333int SurfaceTextureClient::dispatchConnect(va_list args) {
334 int api = va_arg(args, int);
335 return connect(api);
336}
337
338int SurfaceTextureClient::dispatchDisconnect(va_list args) {
339 int api = va_arg(args, int);
340 return disconnect(api);
341}
342
343int SurfaceTextureClient::dispatchSetUsage(va_list args) {
344 int usage = va_arg(args, int);
345 return setUsage(usage);
346}
347
348int SurfaceTextureClient::dispatchSetCrop(va_list args) {
349 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
350 return setCrop(reinterpret_cast<Rect const*>(rect));
351}
352
353int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
354 size_t bufferCount = va_arg(args, size_t);
355 return setBufferCount(bufferCount);
356}
357
358int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
359 int w = va_arg(args, int);
360 int h = va_arg(args, int);
361 int f = va_arg(args, int);
Jamie Gennis97eae022011-07-01 13:12:07 -0700362 int err = setBuffersDimensions(w, h);
363 if (err != 0) {
364 return err;
365 }
366 return setBuffersFormat(f);
367}
368
369int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
370 int w = va_arg(args, int);
371 int h = va_arg(args, int);
372 return setBuffersDimensions(w, h);
373}
374
375int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
376 int f = va_arg(args, int);
377 return setBuffersFormat(f);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800378}
379
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700380int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
381 int m = va_arg(args, int);
382 return setScalingMode(m);
383}
384
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800385int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
386 int transform = va_arg(args, int);
387 return setBuffersTransform(transform);
388}
389
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800390int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
391 int64_t timestamp = va_arg(args, int64_t);
392 return setBuffersTimestamp(timestamp);
393}
394
Mathias Agopian949be322011-07-13 17:39:11 -0700395int SurfaceTextureClient::dispatchLock(va_list args) {
396 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
397 ARect* inOutDirtyBounds = va_arg(args, ARect*);
398 return lock(outBuffer, inOutDirtyBounds);
399}
400
401int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
402 return unlockAndPost();
403}
404
405
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800406int SurfaceTextureClient::connect(int api) {
Steve Block71f2cf12011-10-20 11:56:00 +0100407 ALOGV("SurfaceTextureClient::connect");
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700408 Mutex::Autolock lock(mMutex);
Mathias Agopian053b02d2011-08-08 19:14:03 -0700409 int err = mSurfaceTexture->connect(api,
410 &mDefaultWidth, &mDefaultHeight, &mTransformHint);
Mathias Agopian949be322011-07-13 17:39:11 -0700411 if (!err && api == NATIVE_WINDOW_API_CPU) {
412 mConnectedToCpu = true;
413 }
414 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800415}
416
417int SurfaceTextureClient::disconnect(int api) {
Steve Block71f2cf12011-10-20 11:56:00 +0100418 ALOGV("SurfaceTextureClient::disconnect");
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700419 Mutex::Autolock lock(mMutex);
Jamie Gennis791e6382011-10-18 17:14:33 -0700420 freeAllBuffers();
Mathias Agopian949be322011-07-13 17:39:11 -0700421 int err = mSurfaceTexture->disconnect(api);
Mathias Agopian2370d0a2011-08-25 17:03:30 -0700422 if (!err) {
Mathias Agopian2370d0a2011-08-25 17:03:30 -0700423 mReqFormat = 0;
424 mReqWidth = 0;
425 mReqHeight = 0;
426 mReqUsage = 0;
427 if (api == NATIVE_WINDOW_API_CPU) {
428 mConnectedToCpu = false;
429 }
Mathias Agopian949be322011-07-13 17:39:11 -0700430 }
431 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800432}
433
434int SurfaceTextureClient::setUsage(uint32_t reqUsage)
435{
Steve Block71f2cf12011-10-20 11:56:00 +0100436 ALOGV("SurfaceTextureClient::setUsage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800437 Mutex::Autolock lock(mMutex);
438 mReqUsage = reqUsage;
439 return OK;
440}
441
442int SurfaceTextureClient::setCrop(Rect const* rect)
443{
Steve Block71f2cf12011-10-20 11:56:00 +0100444 ALOGV("SurfaceTextureClient::setCrop");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800445 Mutex::Autolock lock(mMutex);
446
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800447 Rect realRect;
448 if (rect == NULL || rect->isEmpty()) {
449 realRect = Rect(0, 0);
450 } else {
451 realRect = *rect;
452 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800453
454 status_t err = mSurfaceTexture->setCrop(*rect);
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800455 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800456
457 return err;
458}
459
460int SurfaceTextureClient::setBufferCount(int bufferCount)
461{
Steve Block71f2cf12011-10-20 11:56:00 +0100462 ALOGV("SurfaceTextureClient::setBufferCount");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800463 Mutex::Autolock lock(mMutex);
464
465 status_t err = mSurfaceTexture->setBufferCount(bufferCount);
466 LOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
467 bufferCount, strerror(-err));
468
469 if (err == NO_ERROR) {
470 freeAllBuffers();
471 }
472
473 return err;
474}
475
Jamie Gennis97eae022011-07-01 13:12:07 -0700476int SurfaceTextureClient::setBuffersDimensions(int w, int h)
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800477{
Steve Block71f2cf12011-10-20 11:56:00 +0100478 ALOGV("SurfaceTextureClient::setBuffersDimensions");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800479 Mutex::Autolock lock(mMutex);
480
Jamie Gennis97eae022011-07-01 13:12:07 -0700481 if (w<0 || h<0)
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800482 return BAD_VALUE;
483
484 if ((w && !h) || (!w && h))
485 return BAD_VALUE;
486
487 mReqWidth = w;
488 mReqHeight = h;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800489
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800490 status_t err = mSurfaceTexture->setCrop(Rect(0, 0));
491 LOGE_IF(err, "ISurfaceTexture::setCrop(...) returned %s", strerror(-err));
492
493 return err;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800494}
495
Jamie Gennis97eae022011-07-01 13:12:07 -0700496int SurfaceTextureClient::setBuffersFormat(int format)
497{
Steve Block71f2cf12011-10-20 11:56:00 +0100498 ALOGV("SurfaceTextureClient::setBuffersFormat");
Jamie Gennis97eae022011-07-01 13:12:07 -0700499 Mutex::Autolock lock(mMutex);
500
501 if (format<0)
502 return BAD_VALUE;
503
504 mReqFormat = format;
505
506 return NO_ERROR;
507}
508
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700509int SurfaceTextureClient::setScalingMode(int mode)
510{
Steve Block71f2cf12011-10-20 11:56:00 +0100511 ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700512 Mutex::Autolock lock(mMutex);
513 // mode is validated on the server
514 status_t err = mSurfaceTexture->setScalingMode(mode);
515 LOGE_IF(err, "ISurfaceTexture::setScalingMode(%d) returned %s",
516 mode, strerror(-err));
517
518 return err;
519}
520
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800521int SurfaceTextureClient::setBuffersTransform(int transform)
522{
Steve Block71f2cf12011-10-20 11:56:00 +0100523 ALOGV("SurfaceTextureClient::setBuffersTransform");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800524 Mutex::Autolock lock(mMutex);
525 status_t err = mSurfaceTexture->setTransform(transform);
526 return err;
527}
528
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800529int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
530{
Steve Block71f2cf12011-10-20 11:56:00 +0100531 ALOGV("SurfaceTextureClient::setBuffersTimestamp");
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800532 Mutex::Autolock lock(mMutex);
533 mTimestamp = timestamp;
534 return NO_ERROR;
535}
536
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800537void SurfaceTextureClient::freeAllBuffers() {
538 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
539 mSlots[i] = 0;
540 }
541}
542
Mathias Agopian949be322011-07-13 17:39:11 -0700543// ----------------------------------------------------------------------
544// the lock/unlock APIs must be used from the same thread
545
546static status_t copyBlt(
547 const sp<GraphicBuffer>& dst,
548 const sp<GraphicBuffer>& src,
549 const Region& reg)
550{
551 // src and dst with, height and format must be identical. no verification
552 // is done here.
553 status_t err;
554 uint8_t const * src_bits = NULL;
555 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
556 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
557
558 uint8_t* dst_bits = NULL;
559 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
560 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
561
562 Region::const_iterator head(reg.begin());
563 Region::const_iterator tail(reg.end());
564 if (head != tail && src_bits && dst_bits) {
565 const size_t bpp = bytesPerPixel(src->format);
566 const size_t dbpr = dst->stride * bpp;
567 const size_t sbpr = src->stride * bpp;
568
569 while (head != tail) {
570 const Rect& r(*head++);
571 ssize_t h = r.height();
572 if (h <= 0) continue;
573 size_t size = r.width() * bpp;
574 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
575 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
576 if (dbpr==sbpr && size==sbpr) {
577 size *= h;
578 h = 1;
579 }
580 do {
581 memcpy(d, s, size);
582 d += dbpr;
583 s += sbpr;
584 } while (--h > 0);
585 }
586 }
587
588 if (src_bits)
589 src->unlock();
590
591 if (dst_bits)
592 dst->unlock();
593
594 return err;
595}
596
597// ----------------------------------------------------------------------------
598
599status_t SurfaceTextureClient::lock(
600 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
601{
602 if (mLockedBuffer != 0) {
603 LOGE("Surface::lock failed, already locked");
604 return INVALID_OPERATION;
605 }
606
607 if (!mConnectedToCpu) {
608 int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
609 if (err) {
610 return err;
611 }
612 // we're intending to do software rendering from this point
613 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
614 }
615
616 ANativeWindowBuffer* out;
617 status_t err = dequeueBuffer(&out);
618 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
619 if (err == NO_ERROR) {
620 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
621 err = lockBuffer(backBuffer.get());
622 LOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
623 backBuffer->handle, strerror(-err));
624 if (err == NO_ERROR) {
625 const Rect bounds(backBuffer->width, backBuffer->height);
626
627 Region newDirtyRegion;
628 if (inOutDirtyBounds) {
629 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
630 newDirtyRegion.andSelf(bounds);
631 } else {
632 newDirtyRegion.set(bounds);
633 }
634
635 // figure out if we can copy the frontbuffer back
636 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
637 const bool canCopyBack = (frontBuffer != 0 &&
638 backBuffer->width == frontBuffer->width &&
639 backBuffer->height == frontBuffer->height &&
640 backBuffer->format == frontBuffer->format);
641
642 if (canCopyBack) {
643 // copy the area that is invalid and not repainted this round
644 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
645 if (!copyback.isEmpty())
646 copyBlt(backBuffer, frontBuffer, copyback);
647 } else {
648 // if we can't copy-back anything, modify the user's dirty
649 // region to make sure they redraw the whole buffer
650 newDirtyRegion.set(bounds);
651 }
652
653 // keep track of the are of the buffer that is "clean"
654 // (ie: that will be redrawn)
655 mOldDirtyRegion = newDirtyRegion;
656
657 if (inOutDirtyBounds) {
658 *inOutDirtyBounds = newDirtyRegion.getBounds();
659 }
660
661 void* vaddr;
662 status_t res = backBuffer->lock(
663 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
664 newDirtyRegion.bounds(), &vaddr);
665
666 LOGW_IF(res, "failed locking buffer (handle = %p)",
667 backBuffer->handle);
668
669 mLockedBuffer = backBuffer;
670 outBuffer->width = backBuffer->width;
671 outBuffer->height = backBuffer->height;
672 outBuffer->stride = backBuffer->stride;
673 outBuffer->format = backBuffer->format;
674 outBuffer->bits = vaddr;
675 }
676 }
677 return err;
678}
679
680status_t SurfaceTextureClient::unlockAndPost()
681{
682 if (mLockedBuffer == 0) {
683 LOGE("Surface::unlockAndPost failed, no locked buffer");
684 return INVALID_OPERATION;
685 }
686
687 status_t err = mLockedBuffer->unlock();
688 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
689
690 err = queueBuffer(mLockedBuffer.get());
691 LOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
692 mLockedBuffer->handle, strerror(-err));
693
694 mPostedBuffer = mLockedBuffer;
695 mLockedBuffer = 0;
696 return err;
697}
698
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800699}; // namespace android