blob: 4afca68849b8dfb18fc7d627c0974bd9115480f6 [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
39namespace android {
40
Jamie Gennisf238e282011-01-09 16:33:17 -080041// Transform matrices
42static float mtxIdentity[16] = {
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1,
47};
48static float mtxFlipH[16] = {
49 -1, 0, 0, 0,
50 0, 1, 0, 0,
51 0, 0, 1, 0,
52 1, 0, 0, 1,
53};
54static float mtxFlipV[16] = {
55 1, 0, 0, 0,
56 0, -1, 0, 0,
57 0, 0, 1, 0,
58 0, 1, 0, 1,
59};
60static float mtxRot90[16] = {
61 0, 1, 0, 0,
62 -1, 0, 0, 0,
63 0, 0, 1, 0,
64 1, 0, 0, 1,
65};
66static float mtxRot180[16] = {
67 -1, 0, 0, 0,
68 0, -1, 0, 0,
69 0, 0, 1, 0,
70 1, 1, 0, 1,
71};
72static float mtxRot270[16] = {
73 0, -1, 0, 0,
74 1, 0, 0, 0,
75 0, 0, 1, 0,
76 0, 1, 0, 1,
77};
78
79static void mtxMul(float out[16], const float a[16], const float b[16]);
80
Grace Kloba14a0e582011-06-23 21:21:47 -070081SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070082 mDefaultWidth(1),
83 mDefaultHeight(1),
84 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -070085 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
86 mClientBufferCount(0),
87 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080088 mCurrentTexture(INVALID_BUFFER_SLOT),
89 mCurrentTransform(0),
90 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080091 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -070092 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070093 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -070094 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070095 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -070096 mConnectedApi(NO_CONNECTED_API),
97 mAbandoned(false) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080098 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -080099 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
100 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700101 mNextCrop.makeInvalid();
Jamie Gennis736aa952011-06-12 17:03:06 -0700102 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800103}
104
105SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800106 LOGV("SurfaceTexture::~SurfaceTexture");
Mathias Agopianef51b992011-08-10 15:28:58 -0700107 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800108}
109
Mathias Agopian80727112011-05-02 19:51:12 -0700110status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
111 if (bufferCount > NUM_BUFFER_SLOTS)
112 return BAD_VALUE;
113
114 // special-case, nothing to do
115 if (bufferCount == mBufferCount)
116 return OK;
117
118 if (!mClientBufferCount &&
119 bufferCount >= mBufferCount) {
120 // easy, we just have more buffers
121 mBufferCount = bufferCount;
122 mServerBufferCount = bufferCount;
123 mDequeueCondition.signal();
124 } else {
125 // we're here because we're either
126 // - reducing the number of available buffers
127 // - or there is a client-buffer-count in effect
128
129 // less than 2 buffers is never allowed
130 if (bufferCount < 2)
131 return BAD_VALUE;
132
133 // when there is non client-buffer-count in effect, the client is not
134 // allowed to dequeue more than one buffer at a time,
135 // so the next time they dequeue a buffer, we know that they don't
136 // own one. the actual resizing will happen during the next
137 // dequeueBuffer.
138
139 mServerBufferCount = bufferCount;
140 }
141 return OK;
142}
143
144status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
145 Mutex::Autolock lock(mMutex);
146 return setBufferCountServerLocked(bufferCount);
147}
148
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800149status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800150 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700151 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800152
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700153 if (mAbandoned) {
154 LOGE("setBufferCount: SurfaceTexture has been abandoned!");
155 return NO_INIT;
156 }
Mathias Agopianef51b992011-08-10 15:28:58 -0700157 if (mConnectedApi == NO_CONNECTED_API) {
158 LOGE("setBufferCount: SurfaceTexture is not connected!");
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 }
Mathias Agopianef51b992011-08-10 15:28:58 -0700220 if (mConnectedApi == NO_CONNECTED_API) {
221 LOGE("requestBuffer: SurfaceTexture is not connected!");
222 return NO_INIT;
223 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700224 if (slot < 0 || mBufferCount <= slot) {
225 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
226 mBufferCount, slot);
227 return BAD_VALUE;
228 }
229 mSlots[slot].mRequestBufferCalled = true;
230 *buf = mSlots[slot].mGraphicBuffer;
231 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700232}
233
234status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
235 uint32_t format, uint32_t usage) {
236 LOGV("SurfaceTexture::dequeueBuffer");
237
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700238 if ((w && !h) || (!w && h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700239 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
240 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700241 }
242
Mathias Agopianc04f1532011-04-25 20:22:14 -0700243 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700244
245 status_t returnFlags(OK);
246
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700247 int found, foundSync;
248 int dequeuedCount = 0;
249 bool tryAgain = true;
250 while (tryAgain) {
Mathias Agopian2560d142011-08-10 16:33:23 -0700251 if (mAbandoned) {
252 LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
253 return NO_INIT;
254 }
255 if (mConnectedApi == NO_CONNECTED_API) {
256 LOGE("dequeueBuffer: SurfaceTexture is not connected!");
257 return NO_INIT;
258 }
259
Mathias Agopian80727112011-05-02 19:51:12 -0700260 // We need to wait for the FIFO to drain if the number of buffer
261 // needs to change.
262 //
Mathias Agopian2560d142011-08-10 16:33:23 -0700263 // The condition "number of buffers needs to change" is true if
Mathias Agopian80727112011-05-02 19:51:12 -0700264 // - the client doesn't care about how many buffers there are
265 // - AND the actual number of buffer is different from what was
266 // set in the last setBufferCountServer()
267 // - OR -
268 // setBufferCountServer() was set to a value incompatible with
269 // the synchronization mode (for instance because the sync mode
270 // changed since)
271 //
272 // As long as this condition is true AND the FIFO is not empty, we
273 // wait on mDequeueCondition.
274
Mathias Agopian2560d142011-08-10 16:33:23 -0700275 const int minBufferCountNeeded = mSynchronousMode ?
Mathias Agopian80727112011-05-02 19:51:12 -0700276 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
277
Mathias Agopian2560d142011-08-10 16:33:23 -0700278 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
Mathias Agopian80727112011-05-02 19:51:12 -0700279 ((mServerBufferCount != mBufferCount) ||
Mathias Agopian2560d142011-08-10 16:33:23 -0700280 (mServerBufferCount < minBufferCountNeeded));
281
282 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700283 // wait for the FIFO to drain
Mathias Agopian2560d142011-08-10 16:33:23 -0700284 mDequeueCondition.wait(mMutex);
285 // NOTE: we continue here because we need to reevaluate our
286 // whole state (eg: we could be abandoned or disconnected)
287 continue;
Mathias Agopian80727112011-05-02 19:51:12 -0700288 }
289
Mathias Agopian2560d142011-08-10 16:33:23 -0700290 if (numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700291 // here we're guaranteed that mQueue is empty
Mathias Agopianef51b992011-08-10 15:28:58 -0700292 freeAllBuffersLocked();
Mathias Agopian80727112011-05-02 19:51:12 -0700293 mBufferCount = mServerBufferCount;
294 if (mBufferCount < minBufferCountNeeded)
295 mBufferCount = minBufferCountNeeded;
296 mCurrentTexture = INVALID_BUFFER_SLOT;
297 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
298 }
299
300 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700301 found = INVALID_BUFFER_SLOT;
302 foundSync = INVALID_BUFFER_SLOT;
303 dequeuedCount = 0;
304 for (int i = 0; i < mBufferCount; i++) {
305 const int state = mSlots[i].mBufferState;
306 if (state == BufferSlot::DEQUEUED) {
307 dequeuedCount++;
308 }
Mathias Agopiane1220792011-05-04 18:28:07 -0700309 if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700310 foundSync = i;
311 if (i != mCurrentTexture) {
312 found = i;
313 break;
314 }
315 }
316 }
Mathias Agopian80727112011-05-02 19:51:12 -0700317
318 // clients are not allowed to dequeue more than one buffer
319 // if they didn't set a buffer count.
320 if (!mClientBufferCount && dequeuedCount) {
321 return -EINVAL;
322 }
323
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700324 // See whether a buffer has been queued since the last setBufferCount so
325 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
326 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
327 if (bufferHasBeenQueued) {
328 // make sure the client is not trying to dequeue more buffers
329 // than allowed.
330 const int avail = mBufferCount - (dequeuedCount+1);
331 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
332 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
333 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
334 dequeuedCount);
335 return -EBUSY;
336 }
Mathias Agopian80727112011-05-02 19:51:12 -0700337 }
338
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700339 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700340 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700341 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
342 if (tryAgain) {
343 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700344 }
345 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700346
Mathias Agopian80727112011-05-02 19:51:12 -0700347 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
348 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
349 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700350 }
351
Mathias Agopianc04f1532011-04-25 20:22:14 -0700352 if (found == INVALID_BUFFER_SLOT) {
353 return -EBUSY;
354 }
355
356 const int buf = found;
357 *outBuf = found;
358
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700359 const bool useDefaultSize = !w && !h;
360 if (useDefaultSize) {
361 // use the default size
362 w = mDefaultWidth;
363 h = mDefaultHeight;
364 }
365
366 const bool updateFormat = (format != 0);
367 if (!updateFormat) {
368 // keep the current (or default) format
369 format = mPixelFormat;
370 }
371
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700372 // buffer is now in DEQUEUED (but can also be current at the same time,
373 // if we're in synchronous mode)
374 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
375
376 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700377 if ((buffer == NULL) ||
378 (uint32_t(buffer->width) != w) ||
379 (uint32_t(buffer->height) != h) ||
380 (uint32_t(buffer->format) != format) ||
381 ((uint32_t(buffer->usage) & usage) != usage))
382 {
383 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700384 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700385 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700386 mGraphicBufferAlloc->createGraphicBuffer(
387 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700388 if (graphicBuffer == 0) {
389 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700390 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700391 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700392 if (updateFormat) {
393 mPixelFormat = format;
394 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800395 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700396 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800397 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
398 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
399 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
400 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
401 }
Mathias Agopian80727112011-05-02 19:51:12 -0700402 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700403 }
Mathias Agopian80727112011-05-02 19:51:12 -0700404 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800405}
406
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700407status_t SurfaceTexture::setSynchronousMode(bool enabled) {
408 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700409
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700410 if (mAbandoned) {
411 LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
412 return NO_INIT;
413 }
414
Mathias Agopian80727112011-05-02 19:51:12 -0700415 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700416 if (!mAllowSynchronousMode && enabled)
417 return err;
418
Mathias Agopian80727112011-05-02 19:51:12 -0700419 if (!enabled) {
420 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700421 err = drainQueueLocked();
422 if (err != NO_ERROR)
423 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700424 }
425
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700426 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700427 // - if we're going to asynchronous mode, the queue is guaranteed to be
428 // empty here
429 // - if the client set the number of buffers, we're guaranteed that
430 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700431 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700432 mDequeueCondition.signal();
433 }
Mathias Agopian80727112011-05-02 19:51:12 -0700434 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700435}
436
Mathias Agopian97c602c2011-07-19 15:24:46 -0700437status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
438 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800439 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700440
441 sp<FrameAvailableListener> listener;
442
443 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700444 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700445 if (mAbandoned) {
446 LOGE("queueBuffer: SurfaceTexture has been abandoned!");
447 return NO_INIT;
448 }
Mathias Agopianef51b992011-08-10 15:28:58 -0700449 if (mConnectedApi == NO_CONNECTED_API) {
450 LOGE("queueBuffer: SurfaceTexture is not connected!");
451 return NO_INIT;
452 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700453 if (buf < 0 || buf >= mBufferCount) {
454 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
455 mBufferCount, buf);
456 return -EINVAL;
457 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
458 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
459 buf, mSlots[buf].mBufferState);
460 return -EINVAL;
461 } else if (buf == mCurrentTexture) {
462 LOGE("queueBuffer: slot %d is current!", buf);
463 return -EINVAL;
464 } else if (!mSlots[buf].mRequestBufferCalled) {
465 LOGE("queueBuffer: slot %d was enqueued without requesting a "
466 "buffer", buf);
467 return -EINVAL;
468 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700469
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700470 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700471 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700472 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700473
474 // Synchronous mode always signals that an additional frame should
475 // be consumed.
476 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700477 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700478 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700479 if (mQueue.empty()) {
480 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700481
482 // Asynchronous mode only signals that a frame should be
483 // consumed if no previous frame was pending. If a frame were
484 // pending then the consumer would have already been notified.
485 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700486 } else {
487 Fifo::iterator front(mQueue.begin());
488 // buffer currently queued is freed
489 mSlots[*front].mBufferState = BufferSlot::FREE;
490 // and we record the new buffer index in the queued list
491 *front = buf;
492 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700493 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700494
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700495 mSlots[buf].mBufferState = BufferSlot::QUEUED;
496 mSlots[buf].mCrop = mNextCrop;
497 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700498 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700499 mSlots[buf].mTimestamp = timestamp;
500 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700501 } // scope for the lock
502
503 // call back without lock held
504 if (listener != 0) {
505 listener->onFrameAvailable();
506 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700507
508 *outWidth = mDefaultWidth;
509 *outHeight = mDefaultHeight;
510 *outTransform = 0;
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 }
Mathias Agopianef51b992011-08-10 15:28:58 -0700523 if (mConnectedApi == NO_CONNECTED_API) {
524 LOGE("cancelBuffer: SurfaceTexture is not connected!");
525 return;
526 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700527
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700528 if (buf < 0 || buf >= mBufferCount) {
529 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
530 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800531 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700532 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
533 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
534 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800535 return;
536 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700537 mSlots[buf].mBufferState = BufferSlot::FREE;
538 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800539}
540
Jamie Gennisf238e282011-01-09 16:33:17 -0800541status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800542 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800543 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700544 if (mAbandoned) {
545 LOGE("setCrop: SurfaceTexture has been abandoned!");
546 return NO_INIT;
547 }
Mathias Agopianef51b992011-08-10 15:28:58 -0700548 if (mConnectedApi == NO_CONNECTED_API) {
549 LOGE("setCrop: SurfaceTexture is not connected!");
550 return NO_INIT;
551 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800552 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800553 return OK;
554}
555
556status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800557 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800558 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700559 if (mAbandoned) {
560 LOGE("setTransform: SurfaceTexture has been abandoned!");
561 return NO_INIT;
562 }
Mathias Agopianef51b992011-08-10 15:28:58 -0700563 if (mConnectedApi == NO_CONNECTED_API) {
564 LOGE("setTransform: SurfaceTexture is not connected!");
565 return NO_INIT;
566 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800567 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800568 return OK;
569}
570
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700571status_t SurfaceTexture::connect(int api,
572 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700573 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700574 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700575
576 if (mAbandoned) {
577 LOGE("connect: SurfaceTexture has been abandoned!");
578 return NO_INIT;
579 }
580
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700581 int err = NO_ERROR;
582 switch (api) {
583 case NATIVE_WINDOW_API_EGL:
584 case NATIVE_WINDOW_API_CPU:
585 case NATIVE_WINDOW_API_MEDIA:
586 case NATIVE_WINDOW_API_CAMERA:
587 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700588 LOGE("connect: already connected (cur=%d, req=%d)",
589 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700590 err = -EINVAL;
591 } else {
592 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700593 *outWidth = mDefaultWidth;
594 *outHeight = mDefaultHeight;
595 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700596 }
597 break;
598 default:
599 err = -EINVAL;
600 break;
601 }
602 return err;
603}
604
605status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700606 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700607 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700608
609 if (mAbandoned) {
610 LOGE("connect: SurfaceTexture has been abandoned!");
611 return NO_INIT;
612 }
613
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700614 int err = NO_ERROR;
615 switch (api) {
616 case NATIVE_WINDOW_API_EGL:
617 case NATIVE_WINDOW_API_CPU:
618 case NATIVE_WINDOW_API_MEDIA:
619 case NATIVE_WINDOW_API_CAMERA:
620 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700621 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700622 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700623 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700624 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700625 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
626 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700627 err = -EINVAL;
628 }
629 break;
630 default:
631 err = -EINVAL;
632 break;
633 }
634 return err;
635}
636
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700637status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700638 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700639
640 switch (mode) {
641 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
642 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
643 break;
644 default:
645 return BAD_VALUE;
646 }
647
648 Mutex::Autolock lock(mMutex);
649 mNextScalingMode = mode;
650 return OK;
651}
652
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800653status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800654 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800655 Mutex::Autolock lock(mMutex);
656
Mathias Agopiane47498f2011-08-08 19:35:15 -0700657 if (mAbandoned) {
658 LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700659 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700660 }
661
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700662 // In asynchronous mode the list is guaranteed to be one buffer
663 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700664 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700665 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700666 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700667
Jamie Gennisf238e282011-01-09 16:33:17 -0800668 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700669 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800670 if (image == EGL_NO_IMAGE_KHR) {
671 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700672 if (mSlots[buf].mGraphicBuffer == 0) {
673 LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700674 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700675 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700676 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
677 mSlots[buf].mEglImage = image;
678 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700679 if (image == EGL_NO_IMAGE_KHR) {
680 // NOTE: if dpy was invalid, createImage() is guaranteed to
681 // fail. so we'd end up here.
682 return -EINVAL;
683 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800684 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800685
686 GLint error;
687 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700688 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800689 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700690
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700691 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
692 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700693
Jamie Gennis0eb88512011-01-26 11:52:02 -0800694 bool failed = false;
695 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800696 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700697 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800698 failed = true;
699 }
700 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800701 return -EINVAL;
702 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800703
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700704 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700705 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700706 // state. If it has already been given to the client
707 // (synchronous mode), then it stays in DEQUEUED state.
708 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
709 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
710 }
711
Jamie Gennis9a78c902011-01-12 18:30:40 -0800712 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700713 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700714 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700715 mCurrentCrop = mSlots[buf].mCrop;
716 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700717 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700718 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700719 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700720
721 // Now that we've passed the point at which failures can happen,
722 // it's safe to remove the buffer from the front of the queue.
723 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700724 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700725 } else {
726 // We always bind the texture even if we don't update its contents.
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700727 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800728 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700729
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800730 return OK;
731}
732
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700733bool SurfaceTexture::isExternalFormat(uint32_t format)
734{
735 switch (format) {
736 // supported YUV formats
737 case HAL_PIXEL_FORMAT_YV12:
738 // Legacy/deprecated YUV formats
739 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
740 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
741 case HAL_PIXEL_FORMAT_YCbCr_422_I:
742 return true;
743 }
744
745 // Any OEM format needs to be considered
746 if (format>=0x100 && format<=0x1FF)
747 return true;
748
749 return false;
750}
751
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700752GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700753 return GL_TEXTURE_EXTERNAL_OES;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700754}
755
Jamie Gennisf238e282011-01-09 16:33:17 -0800756void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800757 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700758 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
759}
760
761void SurfaceTexture::computeCurrentTransformMatrix() {
762 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800763
Jamie Gennisa214c642011-01-14 13:53:31 -0800764 float xform[16];
765 for (int i = 0; i < 16; i++) {
766 xform[i] = mtxIdentity[i];
767 }
768 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
769 float result[16];
770 mtxMul(result, xform, mtxFlipH);
771 for (int i = 0; i < 16; i++) {
772 xform[i] = result[i];
773 }
774 }
775 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
776 float result[16];
777 mtxMul(result, xform, mtxFlipV);
778 for (int i = 0; i < 16; i++) {
779 xform[i] = result[i];
780 }
781 }
782 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
783 float result[16];
784 mtxMul(result, xform, mtxRot90);
785 for (int i = 0; i < 16; i++) {
786 xform[i] = result[i];
787 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800788 }
789
790 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800791 float tx, ty, sx, sy;
792 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800793 // In order to prevent bilinear sampling at the of the crop rectangle we
794 // may need to shrink it by 2 texels in each direction. Normally this
795 // would just need to take 1/2 a texel off each end, but because the
796 // chroma channels will likely be subsampled we need to chop off a whole
797 // texel. This will cause artifacts if someone does nearest sampling
798 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
799 // accomodate the bilinear and nearest sampling uses.
800 //
801 // If nearest sampling turns out to be a desirable usage of these
802 // textures then we could add the ability to switch a SurfaceTexture to
803 // nearest-mode. Preferably, however, the image producers (video
804 // decoder, camera, etc.) would simply not use a crop rectangle (or at
805 // least not tell the framework about it) so that the GPU can do the
806 // correct edge behavior.
807 int xshrink = 0, yshrink = 0;
808 if (mCurrentCrop.left > 0) {
809 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
810 xshrink++;
811 } else {
812 tx = 0.0f;
813 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700814 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800815 xshrink++;
816 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700817 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800818 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
819 float(buf->getHeight());
820 yshrink++;
821 } else {
822 ty = 0.0f;
823 }
824 if (mCurrentCrop.top > 0) {
825 yshrink++;
826 }
827 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
828 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800829 } else {
830 tx = 0.0f;
831 ty = 0.0f;
832 sx = 1.0f;
833 sy = 1.0f;
834 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800835 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800836 sx, 0, 0, 0,
837 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800838 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800839 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800840 };
841
Jamie Gennisa214c642011-01-14 13:53:31 -0800842 float mtxBeforeFlipV[16];
843 mtxMul(mtxBeforeFlipV, crop, xform);
844
845 // SurfaceFlinger expects the top of its window textures to be at a Y
846 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
847 // want to expose this to applications, however, so we must add an
848 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700849 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800850}
851
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800852nsecs_t SurfaceTexture::getTimestamp() {
853 LOGV("SurfaceTexture::getTimestamp");
854 Mutex::Autolock lock(mMutex);
855 return mCurrentTimestamp;
856}
857
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800858void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700859 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800860 LOGV("SurfaceTexture::setFrameAvailableListener");
861 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700862 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800863}
864
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700865void SurfaceTexture::freeBufferLocked(int i) {
866 mSlots[i].mGraphicBuffer = 0;
867 mSlots[i].mBufferState = BufferSlot::FREE;
868 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
869 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
870 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
871 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
872 }
873}
874
Mathias Agopianef51b992011-08-10 15:28:58 -0700875void SurfaceTexture::freeAllBuffersLocked() {
876 LOGW_IF(!mQueue.isEmpty(),
877 "freeAllBuffersLocked called but mQueue is not empty");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800878 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700879 freeBufferLocked(i);
880 }
881}
882
883void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
884 LOGW_IF(!mQueue.isEmpty(),
885 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
886 int head = -1;
887 if (!mQueue.empty()) {
888 Fifo::iterator front(mQueue.begin());
889 head = *front;
890 }
891 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
892 if (i != head) {
893 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800894 }
895 }
896}
897
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700898status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700899 while (mSynchronousMode && !mQueue.isEmpty()) {
900 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700901 if (mAbandoned) {
902 LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
903 return NO_INIT;
904 }
905 if (mConnectedApi == NO_CONNECTED_API) {
906 LOGE("drainQueueLocked: SurfaceTexture is not connected!");
907 return NO_INIT;
908 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700909 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700910 return NO_ERROR;
911}
912
913status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
914 status_t err = drainQueueLocked();
915 if (err == NO_ERROR) {
916 if (mSynchronousMode) {
917 freeAllBuffersLocked();
918 } else {
919 freeAllBuffersExceptHeadLocked();
920 }
921 }
922 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700923}
924
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800925EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
926 const sp<GraphicBuffer>& graphicBuffer) {
927 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
928 EGLint attrs[] = {
929 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
930 EGL_NONE,
931 };
932 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
933 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700934 if (image == EGL_NO_IMAGE_KHR) {
935 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800936 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800937 }
938 return image;
939}
940
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700941sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
942 Mutex::Autolock lock(mMutex);
943 return mCurrentTextureBuf;
944}
945
946Rect SurfaceTexture::getCurrentCrop() const {
947 Mutex::Autolock lock(mMutex);
948 return mCurrentCrop;
949}
950
951uint32_t SurfaceTexture::getCurrentTransform() const {
952 Mutex::Autolock lock(mMutex);
953 return mCurrentTransform;
954}
955
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700956uint32_t SurfaceTexture::getCurrentScalingMode() const {
957 Mutex::Autolock lock(mMutex);
958 return mCurrentScalingMode;
959}
960
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700961int SurfaceTexture::query(int what, int* outValue)
962{
963 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700964
965 if (mAbandoned) {
966 LOGE("query: SurfaceTexture has been abandoned!");
967 return NO_INIT;
968 }
969
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700970 int value;
971 switch (what) {
972 case NATIVE_WINDOW_WIDTH:
973 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700974 break;
975 case NATIVE_WINDOW_HEIGHT:
976 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700977 break;
978 case NATIVE_WINDOW_FORMAT:
979 value = mPixelFormat;
980 break;
981 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
982 value = mSynchronousMode ?
983 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
984 break;
985 default:
986 return BAD_VALUE;
987 }
988 outValue[0] = value;
989 return NO_ERROR;
990}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700991
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700992void SurfaceTexture::abandon() {
993 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700994 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700995 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700996 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700997 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700998 mDequeueCondition.signal();
999}
1000
Mathias Agopian68c77942011-05-09 19:08:33 -07001001void SurfaceTexture::dump(String8& result) const
1002{
1003 char buffer[1024];
1004 dump(result, "", buffer, 1024);
1005}
1006
1007void SurfaceTexture::dump(String8& result, const char* prefix,
1008 char* buffer, size_t SIZE) const
1009{
1010 Mutex::Autolock _l(mMutex);
1011 snprintf(buffer, SIZE,
1012 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1013 "mPixelFormat=%d, mTexName=%d\n",
1014 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
1015 mPixelFormat, mTexName);
1016 result.append(buffer);
1017
1018 String8 fifo;
1019 int fifoSize = 0;
1020 Fifo::const_iterator i(mQueue.begin());
1021 while (i != mQueue.end()) {
1022 snprintf(buffer, SIZE, "%02d ", *i++);
1023 fifoSize++;
1024 fifo.append(buffer);
1025 }
1026
1027 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001028 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001029 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1030 ,
1031 prefix, mCurrentCrop.left,
1032 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001033 mCurrentTransform, mCurrentTexture,
Mathias Agopian68c77942011-05-09 19:08:33 -07001034 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
1035 mCurrentTransform, fifoSize, fifo.string()
1036 );
1037 result.append(buffer);
1038
1039 struct {
1040 const char * operator()(int state) const {
1041 switch (state) {
1042 case BufferSlot::DEQUEUED: return "DEQUEUED";
1043 case BufferSlot::QUEUED: return "QUEUED";
1044 case BufferSlot::FREE: return "FREE";
1045 default: return "Unknown";
1046 }
1047 }
1048 } stateName;
1049
1050 for (int i=0 ; i<mBufferCount ; i++) {
1051 const BufferSlot& slot(mSlots[i]);
1052 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001053 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001054 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001055 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001056 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001057 stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -07001058 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
1059 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001060 );
1061 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001062
1063 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1064 if (buf != NULL) {
1065 snprintf(buffer, SIZE,
1066 ", %p [%4ux%4u:%4u,%3X]",
1067 buf->handle, buf->width, buf->height, buf->stride, buf->format);
1068 result.append(buffer);
1069 }
1070 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001071 }
1072}
1073
Jamie Gennisf238e282011-01-09 16:33:17 -08001074static void mtxMul(float out[16], const float a[16], const float b[16]) {
1075 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1076 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1077 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1078 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1079
1080 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1081 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1082 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1083 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1084
1085 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1086 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1087 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1088 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1089
1090 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1091 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1092 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1093 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1094}
1095
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001096}; // namespace android