blob: ac9b33b61cd8e4ae0580a7796b6237ae90f2c2a8 [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 "SurfaceTexture"
Jamie Gennise70d8b42011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#define GL_GLEXT_PROTOTYPES
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <gui/SurfaceTexture.h>
29
Mathias Agopian7a042bf2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070037#include <utils/String8.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038
Mathias Agopian29b57622011-08-17 15:42:04 -070039
40#define ALLOW_DEQUEUE_CURRENT_BUFFER false
41
42
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080043namespace android {
44
Jamie Gennisf238e282011-01-09 16:33:17 -080045// Transform matrices
46static float mtxIdentity[16] = {
47 1, 0, 0, 0,
48 0, 1, 0, 0,
49 0, 0, 1, 0,
50 0, 0, 0, 1,
51};
52static float mtxFlipH[16] = {
53 -1, 0, 0, 0,
54 0, 1, 0, 0,
55 0, 0, 1, 0,
56 1, 0, 0, 1,
57};
58static float mtxFlipV[16] = {
59 1, 0, 0, 0,
60 0, -1, 0, 0,
61 0, 0, 1, 0,
62 0, 1, 0, 1,
63};
64static float mtxRot90[16] = {
65 0, 1, 0, 0,
66 -1, 0, 0, 0,
67 0, 0, 1, 0,
68 1, 0, 0, 1,
69};
70static float mtxRot180[16] = {
71 -1, 0, 0, 0,
72 0, -1, 0, 0,
73 0, 0, 1, 0,
74 1, 1, 0, 1,
75};
76static float mtxRot270[16] = {
77 0, -1, 0, 0,
78 1, 0, 0, 0,
79 0, 0, 1, 0,
80 0, 1, 0, 1,
81};
82
83static void mtxMul(float out[16], const float a[16], const float b[16]);
84
Grace Kloba14a0e582011-06-23 21:21:47 -070085SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070086 mDefaultWidth(1),
87 mDefaultHeight(1),
88 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -070089 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
90 mClientBufferCount(0),
91 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080092 mCurrentTexture(INVALID_BUFFER_SLOT),
93 mCurrentTransform(0),
94 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080095 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -070096 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070097 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -070098 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070099 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700100 mConnectedApi(NO_CONNECTED_API),
101 mAbandoned(false) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800102 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -0800103 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
104 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700105 mNextCrop.makeInvalid();
Jamie Gennis736aa952011-06-12 17:03:06 -0700106 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800107}
108
109SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800110 LOGV("SurfaceTexture::~SurfaceTexture");
Mathias Agopianef51b992011-08-10 15:28:58 -0700111 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800112}
113
Mathias Agopian80727112011-05-02 19:51:12 -0700114status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
115 if (bufferCount > NUM_BUFFER_SLOTS)
116 return BAD_VALUE;
117
118 // special-case, nothing to do
119 if (bufferCount == mBufferCount)
120 return OK;
121
122 if (!mClientBufferCount &&
123 bufferCount >= mBufferCount) {
124 // easy, we just have more buffers
125 mBufferCount = bufferCount;
126 mServerBufferCount = bufferCount;
127 mDequeueCondition.signal();
128 } else {
129 // we're here because we're either
130 // - reducing the number of available buffers
131 // - or there is a client-buffer-count in effect
132
133 // less than 2 buffers is never allowed
134 if (bufferCount < 2)
135 return BAD_VALUE;
136
137 // when there is non client-buffer-count in effect, the client is not
138 // allowed to dequeue more than one buffer at a time,
139 // so the next time they dequeue a buffer, we know that they don't
140 // own one. the actual resizing will happen during the next
141 // dequeueBuffer.
142
143 mServerBufferCount = bufferCount;
144 }
145 return OK;
146}
147
148status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
149 Mutex::Autolock lock(mMutex);
150 return setBufferCountServerLocked(bufferCount);
151}
152
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800153status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800154 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700155 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800156
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700157 if (mAbandoned) {
158 LOGE("setBufferCount: SurfaceTexture has been abandoned!");
159 return NO_INIT;
160 }
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700161 if (bufferCount > NUM_BUFFER_SLOTS) {
162 LOGE("setBufferCount: bufferCount larger than slots available");
163 return BAD_VALUE;
164 }
165
Mathias Agopian80727112011-05-02 19:51:12 -0700166 // Error out if the user has dequeued buffers
167 for (int i=0 ; i<mBufferCount ; i++) {
168 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
169 LOGE("setBufferCount: client owns some buffers");
170 return -EINVAL;
171 }
172 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700173
Jamie Gennis1c121f62011-07-30 16:00:11 -0700174 const int minBufferSlots = mSynchronousMode ?
175 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian80727112011-05-02 19:51:12 -0700176 if (bufferCount == 0) {
Mathias Agopian80727112011-05-02 19:51:12 -0700177 mClientBufferCount = 0;
178 bufferCount = (mServerBufferCount >= minBufferSlots) ?
179 mServerBufferCount : minBufferSlots;
180 return setBufferCountServerLocked(bufferCount);
181 }
182
Jamie Gennis1c121f62011-07-30 16:00:11 -0700183 if (bufferCount < minBufferSlots) {
184 LOGE("setBufferCount: requested buffer count (%d) is less than "
185 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800186 return BAD_VALUE;
187 }
188
Mathias Agopian80727112011-05-02 19:51:12 -0700189 // here we're guaranteed that the client doesn't have dequeued buffers
190 // and will release all of its buffer references.
Mathias Agopianef51b992011-08-10 15:28:58 -0700191 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800192 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700193 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800194 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700195 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700196 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800197 return OK;
198}
199
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700200status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
201{
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700202 if (!w || !h) {
203 LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)", w, h);
204 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700205 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700206
207 Mutex::Autolock lock(mMutex);
208 mDefaultWidth = w;
209 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700210 return OK;
211}
212
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700213status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800214 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800215 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700216 if (mAbandoned) {
217 LOGE("requestBuffer: SurfaceTexture has been abandoned!");
218 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700220 if (slot < 0 || mBufferCount <= slot) {
221 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
222 mBufferCount, slot);
223 return BAD_VALUE;
224 }
225 mSlots[slot].mRequestBufferCalled = true;
226 *buf = mSlots[slot].mGraphicBuffer;
227 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700228}
229
230status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
231 uint32_t format, uint32_t usage) {
232 LOGV("SurfaceTexture::dequeueBuffer");
233
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700234 if ((w && !h) || (!w && h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700235 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
236 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700237 }
238
Mathias Agopianc04f1532011-04-25 20:22:14 -0700239 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700240
241 status_t returnFlags(OK);
242
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700243 int found, foundSync;
244 int dequeuedCount = 0;
245 bool tryAgain = true;
246 while (tryAgain) {
Mathias Agopian2560d142011-08-10 16:33:23 -0700247 if (mAbandoned) {
248 LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
249 return NO_INIT;
250 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700251
Mathias Agopian80727112011-05-02 19:51:12 -0700252 // We need to wait for the FIFO to drain if the number of buffer
253 // needs to change.
254 //
Mathias Agopian2560d142011-08-10 16:33:23 -0700255 // The condition "number of buffers needs to change" is true if
Mathias Agopian80727112011-05-02 19:51:12 -0700256 // - the client doesn't care about how many buffers there are
257 // - AND the actual number of buffer is different from what was
258 // set in the last setBufferCountServer()
259 // - OR -
260 // setBufferCountServer() was set to a value incompatible with
261 // the synchronization mode (for instance because the sync mode
262 // changed since)
263 //
264 // As long as this condition is true AND the FIFO is not empty, we
265 // wait on mDequeueCondition.
266
Mathias Agopian2560d142011-08-10 16:33:23 -0700267 const int minBufferCountNeeded = mSynchronousMode ?
Mathias Agopian80727112011-05-02 19:51:12 -0700268 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
269
Mathias Agopian2560d142011-08-10 16:33:23 -0700270 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
Mathias Agopian80727112011-05-02 19:51:12 -0700271 ((mServerBufferCount != mBufferCount) ||
Mathias Agopian2560d142011-08-10 16:33:23 -0700272 (mServerBufferCount < minBufferCountNeeded));
273
274 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700275 // wait for the FIFO to drain
Mathias Agopian2560d142011-08-10 16:33:23 -0700276 mDequeueCondition.wait(mMutex);
277 // NOTE: we continue here because we need to reevaluate our
278 // whole state (eg: we could be abandoned or disconnected)
279 continue;
Mathias Agopian80727112011-05-02 19:51:12 -0700280 }
281
Mathias Agopian2560d142011-08-10 16:33:23 -0700282 if (numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700283 // here we're guaranteed that mQueue is empty
Mathias Agopianef51b992011-08-10 15:28:58 -0700284 freeAllBuffersLocked();
Mathias Agopian80727112011-05-02 19:51:12 -0700285 mBufferCount = mServerBufferCount;
286 if (mBufferCount < minBufferCountNeeded)
287 mBufferCount = minBufferCountNeeded;
288 mCurrentTexture = INVALID_BUFFER_SLOT;
289 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
290 }
291
292 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700293 found = INVALID_BUFFER_SLOT;
294 foundSync = INVALID_BUFFER_SLOT;
295 dequeuedCount = 0;
296 for (int i = 0; i < mBufferCount; i++) {
297 const int state = mSlots[i].mBufferState;
298 if (state == BufferSlot::DEQUEUED) {
299 dequeuedCount++;
300 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700301
302 // if buffer is FREE it CANNOT be current
303 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
304 "dequeueBuffer: buffer %d is both FREE and current!", i);
305
306 if (ALLOW_DEQUEUE_CURRENT_BUFFER) {
307 if (state == BufferSlot::FREE || i == mCurrentTexture) {
308 foundSync = i;
309 if (i != mCurrentTexture) {
310 found = i;
311 break;
312 }
313 }
314 } else {
315 if (state == BufferSlot::FREE) {
316 foundSync = i;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700317 found = i;
318 break;
319 }
320 }
321 }
Mathias Agopian80727112011-05-02 19:51:12 -0700322
323 // clients are not allowed to dequeue more than one buffer
324 // if they didn't set a buffer count.
325 if (!mClientBufferCount && dequeuedCount) {
326 return -EINVAL;
327 }
328
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700329 // See whether a buffer has been queued since the last setBufferCount so
330 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
331 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
332 if (bufferHasBeenQueued) {
333 // make sure the client is not trying to dequeue more buffers
334 // than allowed.
335 const int avail = mBufferCount - (dequeuedCount+1);
336 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
337 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
338 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
339 dequeuedCount);
340 return -EBUSY;
341 }
Mathias Agopian80727112011-05-02 19:51:12 -0700342 }
343
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700344 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian29b57622011-08-17 15:42:04 -0700345 // for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700346 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
347 if (tryAgain) {
348 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700349 }
350 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700351
Mathias Agopian80727112011-05-02 19:51:12 -0700352 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
353 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
354 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700355 }
356
Mathias Agopianc04f1532011-04-25 20:22:14 -0700357 if (found == INVALID_BUFFER_SLOT) {
358 return -EBUSY;
359 }
360
361 const int buf = found;
362 *outBuf = found;
363
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700364 const bool useDefaultSize = !w && !h;
365 if (useDefaultSize) {
366 // use the default size
367 w = mDefaultWidth;
368 h = mDefaultHeight;
369 }
370
371 const bool updateFormat = (format != 0);
372 if (!updateFormat) {
373 // keep the current (or default) format
374 format = mPixelFormat;
375 }
376
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700377 // buffer is now in DEQUEUED (but can also be current at the same time,
378 // if we're in synchronous mode)
379 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
380
381 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700382 if ((buffer == NULL) ||
383 (uint32_t(buffer->width) != w) ||
384 (uint32_t(buffer->height) != h) ||
385 (uint32_t(buffer->format) != format) ||
386 ((uint32_t(buffer->usage) & usage) != usage))
387 {
388 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700389 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700390 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700391 mGraphicBufferAlloc->createGraphicBuffer(
392 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700393 if (graphicBuffer == 0) {
394 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700395 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700396 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700397 if (updateFormat) {
398 mPixelFormat = format;
399 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800400 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700401 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800402 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
403 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
404 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
405 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
406 }
Mathias Agopian80727112011-05-02 19:51:12 -0700407 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700408 }
Mathias Agopian80727112011-05-02 19:51:12 -0700409 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800410}
411
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700412status_t SurfaceTexture::setSynchronousMode(bool enabled) {
413 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700414
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700415 if (mAbandoned) {
416 LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
417 return NO_INIT;
418 }
419
Mathias Agopian80727112011-05-02 19:51:12 -0700420 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700421 if (!mAllowSynchronousMode && enabled)
422 return err;
423
Mathias Agopian80727112011-05-02 19:51:12 -0700424 if (!enabled) {
425 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700426 err = drainQueueLocked();
427 if (err != NO_ERROR)
428 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700429 }
430
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700431 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700432 // - if we're going to asynchronous mode, the queue is guaranteed to be
433 // empty here
434 // - if the client set the number of buffers, we're guaranteed that
435 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700436 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700437 mDequeueCondition.signal();
438 }
Mathias Agopian80727112011-05-02 19:51:12 -0700439 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700440}
441
Mathias Agopian97c602c2011-07-19 15:24:46 -0700442status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
443 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800444 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700445
446 sp<FrameAvailableListener> listener;
447
448 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700449 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700450 if (mAbandoned) {
451 LOGE("queueBuffer: SurfaceTexture has been abandoned!");
452 return NO_INIT;
453 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700454 if (buf < 0 || buf >= mBufferCount) {
455 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
456 mBufferCount, buf);
457 return -EINVAL;
458 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
459 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
460 buf, mSlots[buf].mBufferState);
461 return -EINVAL;
462 } else if (buf == mCurrentTexture) {
463 LOGE("queueBuffer: slot %d is current!", buf);
464 return -EINVAL;
465 } else if (!mSlots[buf].mRequestBufferCalled) {
466 LOGE("queueBuffer: slot %d was enqueued without requesting a "
467 "buffer", buf);
468 return -EINVAL;
469 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700470
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700471 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700472 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700473 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700474
475 // Synchronous mode always signals that an additional frame should
476 // be consumed.
477 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700478 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700479 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700480 if (mQueue.empty()) {
481 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700482
483 // Asynchronous mode only signals that a frame should be
484 // consumed if no previous frame was pending. If a frame were
485 // pending then the consumer would have already been notified.
486 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700487 } else {
488 Fifo::iterator front(mQueue.begin());
489 // buffer currently queued is freed
490 mSlots[*front].mBufferState = BufferSlot::FREE;
491 // and we record the new buffer index in the queued list
492 *front = buf;
493 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700494 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700495
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700496 mSlots[buf].mBufferState = BufferSlot::QUEUED;
497 mSlots[buf].mCrop = mNextCrop;
498 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700499 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700500 mSlots[buf].mTimestamp = timestamp;
501 mDequeueCondition.signal();
Mathias Agopian3902be62011-08-17 12:45:40 -0700502
503 *outWidth = mDefaultWidth;
504 *outHeight = mDefaultHeight;
505 *outTransform = 0;
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700506 } // scope for the lock
507
508 // call back without lock held
509 if (listener != 0) {
510 listener->onFrameAvailable();
511 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800512 return OK;
513}
514
515void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800516 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800517 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700518
519 if (mAbandoned) {
520 LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
521 return;
522 }
523
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700524 if (buf < 0 || buf >= mBufferCount) {
525 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
526 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800527 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700528 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
529 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
530 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800531 return;
532 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700533 mSlots[buf].mBufferState = BufferSlot::FREE;
534 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800535}
536
Jamie Gennisf238e282011-01-09 16:33:17 -0800537status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800538 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800539 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700540 if (mAbandoned) {
541 LOGE("setCrop: SurfaceTexture has been abandoned!");
542 return NO_INIT;
543 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800544 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800545 return OK;
546}
547
548status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800549 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800550 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700551 if (mAbandoned) {
552 LOGE("setTransform: SurfaceTexture has been abandoned!");
553 return NO_INIT;
554 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800555 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800556 return OK;
557}
558
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700559status_t SurfaceTexture::connect(int api,
560 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700561 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700562 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700563
564 if (mAbandoned) {
565 LOGE("connect: SurfaceTexture has been abandoned!");
566 return NO_INIT;
567 }
568
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700569 int err = NO_ERROR;
570 switch (api) {
571 case NATIVE_WINDOW_API_EGL:
572 case NATIVE_WINDOW_API_CPU:
573 case NATIVE_WINDOW_API_MEDIA:
574 case NATIVE_WINDOW_API_CAMERA:
575 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700576 LOGE("connect: already connected (cur=%d, req=%d)",
577 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700578 err = -EINVAL;
579 } else {
580 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700581 *outWidth = mDefaultWidth;
582 *outHeight = mDefaultHeight;
583 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700584 }
585 break;
586 default:
587 err = -EINVAL;
588 break;
589 }
590 return err;
591}
592
593status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700594 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700595 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700596
597 if (mAbandoned) {
598 LOGE("connect: SurfaceTexture has been abandoned!");
599 return NO_INIT;
600 }
601
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700602 int err = NO_ERROR;
603 switch (api) {
604 case NATIVE_WINDOW_API_EGL:
605 case NATIVE_WINDOW_API_CPU:
606 case NATIVE_WINDOW_API_MEDIA:
607 case NATIVE_WINDOW_API_CAMERA:
608 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700609 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700610 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700611 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700612 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700613 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
614 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700615 err = -EINVAL;
616 }
617 break;
618 default:
619 err = -EINVAL;
620 break;
621 }
622 return err;
623}
624
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700625status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700626 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700627
628 switch (mode) {
629 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
630 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
631 break;
632 default:
633 return BAD_VALUE;
634 }
635
636 Mutex::Autolock lock(mMutex);
637 mNextScalingMode = mode;
638 return OK;
639}
640
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800641status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800642 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800643 Mutex::Autolock lock(mMutex);
644
Mathias Agopiane47498f2011-08-08 19:35:15 -0700645 if (mAbandoned) {
646 LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700647 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700648 }
649
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700650 // In asynchronous mode the list is guaranteed to be one buffer
651 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700652 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700653 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700654 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700655
Jamie Gennisf238e282011-01-09 16:33:17 -0800656 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700657 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800658 if (image == EGL_NO_IMAGE_KHR) {
659 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700660 if (mSlots[buf].mGraphicBuffer == 0) {
661 LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700662 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700663 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700664 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
665 mSlots[buf].mEglImage = image;
666 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700667 if (image == EGL_NO_IMAGE_KHR) {
668 // NOTE: if dpy was invalid, createImage() is guaranteed to
669 // fail. so we'd end up here.
670 return -EINVAL;
671 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800672 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800673
674 GLint error;
675 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700676 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800677 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700678
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700679 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
680 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700681
Jamie Gennis0eb88512011-01-26 11:52:02 -0800682 bool failed = false;
683 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800684 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700685 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800686 failed = true;
687 }
688 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800689 return -EINVAL;
690 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800691
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700692 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700693 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700694 // state. If it has already been given to the client
695 // (synchronous mode), then it stays in DEQUEUED state.
696 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
697 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
698 }
699
Jamie Gennis9a78c902011-01-12 18:30:40 -0800700 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700701 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700702 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700703 mCurrentCrop = mSlots[buf].mCrop;
704 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700705 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700706 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700707 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700708
709 // Now that we've passed the point at which failures can happen,
710 // it's safe to remove the buffer from the front of the queue.
711 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700712 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700713 } else {
714 // We always bind the texture even if we don't update its contents.
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700715 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800716 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700717
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800718 return OK;
719}
720
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700721bool SurfaceTexture::isExternalFormat(uint32_t format)
722{
723 switch (format) {
724 // supported YUV formats
725 case HAL_PIXEL_FORMAT_YV12:
726 // Legacy/deprecated YUV formats
727 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
728 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
729 case HAL_PIXEL_FORMAT_YCbCr_422_I:
730 return true;
731 }
732
733 // Any OEM format needs to be considered
734 if (format>=0x100 && format<=0x1FF)
735 return true;
736
737 return false;
738}
739
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700740GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700741 return GL_TEXTURE_EXTERNAL_OES;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700742}
743
Jamie Gennisf238e282011-01-09 16:33:17 -0800744void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800745 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700746 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
747}
748
749void SurfaceTexture::computeCurrentTransformMatrix() {
750 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800751
Jamie Gennisa214c642011-01-14 13:53:31 -0800752 float xform[16];
753 for (int i = 0; i < 16; i++) {
754 xform[i] = mtxIdentity[i];
755 }
756 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
757 float result[16];
758 mtxMul(result, xform, mtxFlipH);
759 for (int i = 0; i < 16; i++) {
760 xform[i] = result[i];
761 }
762 }
763 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
764 float result[16];
765 mtxMul(result, xform, mtxFlipV);
766 for (int i = 0; i < 16; i++) {
767 xform[i] = result[i];
768 }
769 }
770 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
771 float result[16];
772 mtxMul(result, xform, mtxRot90);
773 for (int i = 0; i < 16; i++) {
774 xform[i] = result[i];
775 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800776 }
777
778 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800779 float tx, ty, sx, sy;
780 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800781 // In order to prevent bilinear sampling at the of the crop rectangle we
782 // may need to shrink it by 2 texels in each direction. Normally this
783 // would just need to take 1/2 a texel off each end, but because the
784 // chroma channels will likely be subsampled we need to chop off a whole
785 // texel. This will cause artifacts if someone does nearest sampling
786 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
787 // accomodate the bilinear and nearest sampling uses.
788 //
789 // If nearest sampling turns out to be a desirable usage of these
790 // textures then we could add the ability to switch a SurfaceTexture to
791 // nearest-mode. Preferably, however, the image producers (video
792 // decoder, camera, etc.) would simply not use a crop rectangle (or at
793 // least not tell the framework about it) so that the GPU can do the
794 // correct edge behavior.
795 int xshrink = 0, yshrink = 0;
796 if (mCurrentCrop.left > 0) {
797 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
798 xshrink++;
799 } else {
800 tx = 0.0f;
801 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700802 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800803 xshrink++;
804 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700805 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800806 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
807 float(buf->getHeight());
808 yshrink++;
809 } else {
810 ty = 0.0f;
811 }
812 if (mCurrentCrop.top > 0) {
813 yshrink++;
814 }
815 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
816 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800817 } else {
818 tx = 0.0f;
819 ty = 0.0f;
820 sx = 1.0f;
821 sy = 1.0f;
822 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800823 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800824 sx, 0, 0, 0,
825 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800826 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800827 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800828 };
829
Jamie Gennisa214c642011-01-14 13:53:31 -0800830 float mtxBeforeFlipV[16];
831 mtxMul(mtxBeforeFlipV, crop, xform);
832
833 // SurfaceFlinger expects the top of its window textures to be at a Y
834 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
835 // want to expose this to applications, however, so we must add an
836 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700837 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800838}
839
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800840nsecs_t SurfaceTexture::getTimestamp() {
841 LOGV("SurfaceTexture::getTimestamp");
842 Mutex::Autolock lock(mMutex);
843 return mCurrentTimestamp;
844}
845
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800846void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700847 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800848 LOGV("SurfaceTexture::setFrameAvailableListener");
849 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700850 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800851}
852
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700853void SurfaceTexture::freeBufferLocked(int i) {
854 mSlots[i].mGraphicBuffer = 0;
855 mSlots[i].mBufferState = BufferSlot::FREE;
856 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
857 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
858 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
859 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
860 }
861}
862
Mathias Agopianef51b992011-08-10 15:28:58 -0700863void SurfaceTexture::freeAllBuffersLocked() {
864 LOGW_IF(!mQueue.isEmpty(),
865 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian29b57622011-08-17 15:42:04 -0700866 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800867 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700868 freeBufferLocked(i);
869 }
870}
871
872void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
873 LOGW_IF(!mQueue.isEmpty(),
874 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
875 int head = -1;
876 if (!mQueue.empty()) {
877 Fifo::iterator front(mQueue.begin());
878 head = *front;
879 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700880 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700881 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
882 if (i != head) {
883 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800884 }
885 }
886}
887
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700888status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700889 while (mSynchronousMode && !mQueue.isEmpty()) {
890 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700891 if (mAbandoned) {
892 LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
893 return NO_INIT;
894 }
895 if (mConnectedApi == NO_CONNECTED_API) {
896 LOGE("drainQueueLocked: SurfaceTexture is not connected!");
897 return NO_INIT;
898 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700899 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700900 return NO_ERROR;
901}
902
903status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
904 status_t err = drainQueueLocked();
905 if (err == NO_ERROR) {
906 if (mSynchronousMode) {
907 freeAllBuffersLocked();
908 } else {
909 freeAllBuffersExceptHeadLocked();
910 }
911 }
912 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700913}
914
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800915EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
916 const sp<GraphicBuffer>& graphicBuffer) {
917 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
918 EGLint attrs[] = {
919 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
920 EGL_NONE,
921 };
922 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
923 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700924 if (image == EGL_NO_IMAGE_KHR) {
925 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800926 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800927 }
928 return image;
929}
930
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700931sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
932 Mutex::Autolock lock(mMutex);
933 return mCurrentTextureBuf;
934}
935
936Rect SurfaceTexture::getCurrentCrop() const {
937 Mutex::Autolock lock(mMutex);
938 return mCurrentCrop;
939}
940
941uint32_t SurfaceTexture::getCurrentTransform() const {
942 Mutex::Autolock lock(mMutex);
943 return mCurrentTransform;
944}
945
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700946uint32_t SurfaceTexture::getCurrentScalingMode() const {
947 Mutex::Autolock lock(mMutex);
948 return mCurrentScalingMode;
949}
950
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700951int SurfaceTexture::query(int what, int* outValue)
952{
953 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700954
955 if (mAbandoned) {
956 LOGE("query: SurfaceTexture has been abandoned!");
957 return NO_INIT;
958 }
959
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700960 int value;
961 switch (what) {
962 case NATIVE_WINDOW_WIDTH:
963 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700964 break;
965 case NATIVE_WINDOW_HEIGHT:
966 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700967 break;
968 case NATIVE_WINDOW_FORMAT:
969 value = mPixelFormat;
970 break;
971 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
972 value = mSynchronousMode ?
973 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
974 break;
975 default:
976 return BAD_VALUE;
977 }
978 outValue[0] = value;
979 return NO_ERROR;
980}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700981
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700982void SurfaceTexture::abandon() {
983 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700984 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700985 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700986 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700987 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700988 mDequeueCondition.signal();
989}
990
Mathias Agopian68c77942011-05-09 19:08:33 -0700991void SurfaceTexture::dump(String8& result) const
992{
993 char buffer[1024];
994 dump(result, "", buffer, 1024);
995}
996
997void SurfaceTexture::dump(String8& result, const char* prefix,
998 char* buffer, size_t SIZE) const
999{
1000 Mutex::Autolock _l(mMutex);
1001 snprintf(buffer, SIZE,
1002 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1003 "mPixelFormat=%d, mTexName=%d\n",
1004 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
1005 mPixelFormat, mTexName);
1006 result.append(buffer);
1007
1008 String8 fifo;
1009 int fifoSize = 0;
1010 Fifo::const_iterator i(mQueue.begin());
1011 while (i != mQueue.end()) {
1012 snprintf(buffer, SIZE, "%02d ", *i++);
1013 fifoSize++;
1014 fifo.append(buffer);
1015 }
1016
1017 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001018 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001019 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1020 ,
1021 prefix, mCurrentCrop.left,
1022 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001023 mCurrentTransform, mCurrentTexture,
Mathias Agopian68c77942011-05-09 19:08:33 -07001024 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
1025 mCurrentTransform, fifoSize, fifo.string()
1026 );
1027 result.append(buffer);
1028
1029 struct {
1030 const char * operator()(int state) const {
1031 switch (state) {
1032 case BufferSlot::DEQUEUED: return "DEQUEUED";
1033 case BufferSlot::QUEUED: return "QUEUED";
1034 case BufferSlot::FREE: return "FREE";
1035 default: return "Unknown";
1036 }
1037 }
1038 } stateName;
1039
1040 for (int i=0 ; i<mBufferCount ; i++) {
1041 const BufferSlot& slot(mSlots[i]);
1042 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001043 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001044 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001045 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001046 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001047 stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -07001048 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
1049 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001050 );
1051 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001052
1053 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1054 if (buf != NULL) {
1055 snprintf(buffer, SIZE,
1056 ", %p [%4ux%4u:%4u,%3X]",
1057 buf->handle, buf->width, buf->height, buf->stride, buf->format);
1058 result.append(buffer);
1059 }
1060 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001061 }
1062}
1063
Jamie Gennisf238e282011-01-09 16:33:17 -08001064static void mtxMul(float out[16], const float a[16], const float b[16]) {
1065 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1066 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1067 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1068 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1069
1070 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1071 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1072 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1073 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1074
1075 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1076 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1077 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1078 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1079
1080 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1081 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1082 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1083 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1084}
1085
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001086}; // namespace android