blob: 374f3c5240194d63030f9eaf7dd9b41eaf1872c5 [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 Agopian251ce852011-11-14 19:17:37 -080039#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
40#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER true
41#warning "ALLOW_DEQUEUE_CURRENT_BUFFER enabled"
42#else
43#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER false
44#endif
Mathias Agopian29b57622011-08-17 15:42:04 -070045
Jamie Gennisfa28c352011-09-16 17:30:26 -070046// Macros for including the SurfaceTexture name in log messages
47#define ST_LOGV(x, ...) LOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
48#define ST_LOGD(x, ...) LOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
49#define ST_LOGI(x, ...) LOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
50#define ST_LOGW(x, ...) LOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
51#define ST_LOGE(x, ...) LOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070052
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080053namespace android {
54
Jamie Gennisf238e282011-01-09 16:33:17 -080055// Transform matrices
56static float mtxIdentity[16] = {
57 1, 0, 0, 0,
58 0, 1, 0, 0,
59 0, 0, 1, 0,
60 0, 0, 0, 1,
61};
62static float mtxFlipH[16] = {
63 -1, 0, 0, 0,
64 0, 1, 0, 0,
65 0, 0, 1, 0,
66 1, 0, 0, 1,
67};
68static float mtxFlipV[16] = {
69 1, 0, 0, 0,
70 0, -1, 0, 0,
71 0, 0, 1, 0,
72 0, 1, 0, 1,
73};
74static float mtxRot90[16] = {
75 0, 1, 0, 0,
76 -1, 0, 0, 0,
77 0, 0, 1, 0,
78 1, 0, 0, 1,
79};
80static float mtxRot180[16] = {
81 -1, 0, 0, 0,
82 0, -1, 0, 0,
83 0, 0, 1, 0,
84 1, 1, 0, 1,
85};
86static float mtxRot270[16] = {
87 0, -1, 0, 0,
88 1, 0, 0, 0,
89 0, 0, 1, 0,
90 0, 1, 0, 1,
91};
92
93static void mtxMul(float out[16], const float a[16], const float b[16]);
94
Jamie Gennisfa28c352011-09-16 17:30:26 -070095// Get an ID that's unique within this process.
96static int32_t createProcessUniqueId() {
97 static volatile int32_t globalCounter = 0;
98 return android_atomic_inc(&globalCounter);
99}
100
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700101SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
102 GLenum texTarget) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700103 mDefaultWidth(1),
104 mDefaultHeight(1),
105 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -0700106 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
107 mClientBufferCount(0),
108 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800109 mCurrentTexture(INVALID_BUFFER_SLOT),
110 mCurrentTransform(0),
111 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800112 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700113 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700114 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -0700115 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700116 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700117 mConnectedApi(NO_CONNECTED_API),
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700118 mAbandoned(false),
119 mTexTarget(texTarget) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700120 // Choose a name using the PID and a process-unique ID.
121 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
122
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700123 ST_LOGV("SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -0800124 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
125 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700126 mNextCrop.makeInvalid();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700127 memcpy(mCurrentTransformMatrix, mtxIdentity,
128 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129}
130
131SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700132 ST_LOGV("~SurfaceTexture");
Mathias Agopianef51b992011-08-10 15:28:58 -0700133 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800134}
135
Mathias Agopian80727112011-05-02 19:51:12 -0700136status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
137 if (bufferCount > NUM_BUFFER_SLOTS)
138 return BAD_VALUE;
139
140 // special-case, nothing to do
141 if (bufferCount == mBufferCount)
142 return OK;
143
144 if (!mClientBufferCount &&
145 bufferCount >= mBufferCount) {
146 // easy, we just have more buffers
147 mBufferCount = bufferCount;
148 mServerBufferCount = bufferCount;
149 mDequeueCondition.signal();
150 } else {
151 // we're here because we're either
152 // - reducing the number of available buffers
153 // - or there is a client-buffer-count in effect
154
155 // less than 2 buffers is never allowed
156 if (bufferCount < 2)
157 return BAD_VALUE;
158
159 // when there is non client-buffer-count in effect, the client is not
160 // allowed to dequeue more than one buffer at a time,
161 // so the next time they dequeue a buffer, we know that they don't
162 // own one. the actual resizing will happen during the next
163 // dequeueBuffer.
164
165 mServerBufferCount = bufferCount;
166 }
167 return OK;
168}
169
170status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
171 Mutex::Autolock lock(mMutex);
172 return setBufferCountServerLocked(bufferCount);
173}
174
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800175status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700176 ST_LOGV("setBufferCount: count=%d", bufferCount);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700177 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800178
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700179 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700180 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700181 return NO_INIT;
182 }
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700183 if (bufferCount > NUM_BUFFER_SLOTS) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700184 ST_LOGE("setBufferCount: bufferCount larger than slots available");
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700185 return BAD_VALUE;
186 }
187
Mathias Agopian80727112011-05-02 19:51:12 -0700188 // Error out if the user has dequeued buffers
189 for (int i=0 ; i<mBufferCount ; i++) {
190 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700191 ST_LOGE("setBufferCount: client owns some buffers");
Mathias Agopian80727112011-05-02 19:51:12 -0700192 return -EINVAL;
193 }
194 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700195
Jamie Gennis1c121f62011-07-30 16:00:11 -0700196 const int minBufferSlots = mSynchronousMode ?
197 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian80727112011-05-02 19:51:12 -0700198 if (bufferCount == 0) {
Mathias Agopian80727112011-05-02 19:51:12 -0700199 mClientBufferCount = 0;
200 bufferCount = (mServerBufferCount >= minBufferSlots) ?
201 mServerBufferCount : minBufferSlots;
202 return setBufferCountServerLocked(bufferCount);
203 }
204
Jamie Gennis1c121f62011-07-30 16:00:11 -0700205 if (bufferCount < minBufferSlots) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700206 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
Jamie Gennis1c121f62011-07-30 16:00:11 -0700207 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800208 return BAD_VALUE;
209 }
210
Mathias Agopian80727112011-05-02 19:51:12 -0700211 // here we're guaranteed that the client doesn't have dequeued buffers
212 // and will release all of its buffer references.
Mathias Agopianef51b992011-08-10 15:28:58 -0700213 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800214 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700215 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800216 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700217 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700218 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 return OK;
220}
221
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700222status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
223{
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700224 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700225 if (!w || !h) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700226 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
227 w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700228 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700229 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700230
231 Mutex::Autolock lock(mMutex);
232 mDefaultWidth = w;
233 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700234 return OK;
235}
236
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700237status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700238 ST_LOGV("requestBuffer: slot=%d", slot);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800239 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700240 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700241 ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700242 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800243 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700244 if (slot < 0 || mBufferCount <= slot) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700245 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700246 mBufferCount, slot);
247 return BAD_VALUE;
248 }
249 mSlots[slot].mRequestBufferCalled = true;
250 *buf = mSlots[slot].mGraphicBuffer;
251 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700252}
253
254status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
255 uint32_t format, uint32_t usage) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700256 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700257
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700258 if ((w && !h) || (!w && h)) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700259 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700260 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700261 }
262
Mathias Agopianc04f1532011-04-25 20:22:14 -0700263 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700264
265 status_t returnFlags(OK);
266
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700267 int found, foundSync;
268 int dequeuedCount = 0;
269 bool tryAgain = true;
270 while (tryAgain) {
Mathias Agopian2560d142011-08-10 16:33:23 -0700271 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700272 ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
Mathias Agopian2560d142011-08-10 16:33:23 -0700273 return NO_INIT;
274 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700275
Mathias Agopian80727112011-05-02 19:51:12 -0700276 // We need to wait for the FIFO to drain if the number of buffer
277 // needs to change.
278 //
Mathias Agopian2560d142011-08-10 16:33:23 -0700279 // The condition "number of buffers needs to change" is true if
Mathias Agopian80727112011-05-02 19:51:12 -0700280 // - the client doesn't care about how many buffers there are
281 // - AND the actual number of buffer is different from what was
282 // set in the last setBufferCountServer()
283 // - OR -
284 // setBufferCountServer() was set to a value incompatible with
285 // the synchronization mode (for instance because the sync mode
286 // changed since)
287 //
288 // As long as this condition is true AND the FIFO is not empty, we
289 // wait on mDequeueCondition.
290
Mathias Agopian2560d142011-08-10 16:33:23 -0700291 const int minBufferCountNeeded = mSynchronousMode ?
Mathias Agopian80727112011-05-02 19:51:12 -0700292 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
293
Mathias Agopian2560d142011-08-10 16:33:23 -0700294 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
Mathias Agopian80727112011-05-02 19:51:12 -0700295 ((mServerBufferCount != mBufferCount) ||
Mathias Agopian2560d142011-08-10 16:33:23 -0700296 (mServerBufferCount < minBufferCountNeeded));
297
298 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700299 // wait for the FIFO to drain
Mathias Agopian2560d142011-08-10 16:33:23 -0700300 mDequeueCondition.wait(mMutex);
301 // NOTE: we continue here because we need to reevaluate our
302 // whole state (eg: we could be abandoned or disconnected)
303 continue;
Mathias Agopian80727112011-05-02 19:51:12 -0700304 }
305
Mathias Agopian2560d142011-08-10 16:33:23 -0700306 if (numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700307 // here we're guaranteed that mQueue is empty
Mathias Agopianef51b992011-08-10 15:28:58 -0700308 freeAllBuffersLocked();
Mathias Agopian80727112011-05-02 19:51:12 -0700309 mBufferCount = mServerBufferCount;
310 if (mBufferCount < minBufferCountNeeded)
311 mBufferCount = minBufferCountNeeded;
312 mCurrentTexture = INVALID_BUFFER_SLOT;
313 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
314 }
315
316 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700317 found = INVALID_BUFFER_SLOT;
318 foundSync = INVALID_BUFFER_SLOT;
319 dequeuedCount = 0;
320 for (int i = 0; i < mBufferCount; i++) {
321 const int state = mSlots[i].mBufferState;
322 if (state == BufferSlot::DEQUEUED) {
323 dequeuedCount++;
324 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700325
326 // if buffer is FREE it CANNOT be current
327 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
328 "dequeueBuffer: buffer %d is both FREE and current!", i);
329
Mathias Agopian251ce852011-11-14 19:17:37 -0800330 if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER) {
Mathias Agopian29b57622011-08-17 15:42:04 -0700331 if (state == BufferSlot::FREE || i == mCurrentTexture) {
332 foundSync = i;
333 if (i != mCurrentTexture) {
334 found = i;
335 break;
336 }
337 }
338 } else {
339 if (state == BufferSlot::FREE) {
340 foundSync = i;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700341 found = i;
342 break;
343 }
344 }
345 }
Mathias Agopian80727112011-05-02 19:51:12 -0700346
347 // clients are not allowed to dequeue more than one buffer
348 // if they didn't set a buffer count.
349 if (!mClientBufferCount && dequeuedCount) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700350 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
351 "setting the buffer count");
Mathias Agopian80727112011-05-02 19:51:12 -0700352 return -EINVAL;
353 }
354
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700355 // See whether a buffer has been queued since the last setBufferCount so
356 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
357 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
358 if (bufferHasBeenQueued) {
359 // make sure the client is not trying to dequeue more buffers
360 // than allowed.
361 const int avail = mBufferCount - (dequeuedCount+1);
362 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700363 ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
364 "(dequeued=%d)",
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700365 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
366 dequeuedCount);
367 return -EBUSY;
368 }
Mathias Agopian80727112011-05-02 19:51:12 -0700369 }
370
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700371 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian29b57622011-08-17 15:42:04 -0700372 // for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700373 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
374 if (tryAgain) {
375 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700376 }
377 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700378
Mathias Agopian80727112011-05-02 19:51:12 -0700379 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
380 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
381 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700382 }
383
Mathias Agopianc04f1532011-04-25 20:22:14 -0700384 if (found == INVALID_BUFFER_SLOT) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700385 // This should not happen.
386 ST_LOGE("dequeueBuffer: no available buffer slots");
Mathias Agopianc04f1532011-04-25 20:22:14 -0700387 return -EBUSY;
388 }
389
390 const int buf = found;
391 *outBuf = found;
392
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700393 const bool useDefaultSize = !w && !h;
394 if (useDefaultSize) {
395 // use the default size
396 w = mDefaultWidth;
397 h = mDefaultHeight;
398 }
399
400 const bool updateFormat = (format != 0);
401 if (!updateFormat) {
402 // keep the current (or default) format
403 format = mPixelFormat;
404 }
405
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700406 // buffer is now in DEQUEUED (but can also be current at the same time,
407 // if we're in synchronous mode)
408 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
409
410 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700411 if ((buffer == NULL) ||
412 (uint32_t(buffer->width) != w) ||
413 (uint32_t(buffer->height) != h) ||
414 (uint32_t(buffer->format) != format) ||
415 ((uint32_t(buffer->usage) & usage) != usage))
416 {
417 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700418 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700419 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700420 mGraphicBufferAlloc->createGraphicBuffer(
421 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700422 if (graphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700423 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
424 "failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700425 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700426 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700427 if (updateFormat) {
428 mPixelFormat = format;
429 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800430 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700431 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800432 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
433 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
434 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
435 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
436 }
Mathias Agopian80727112011-05-02 19:51:12 -0700437 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700438 }
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700439 ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", buf,
440 mSlots[buf].mGraphicBuffer->handle, returnFlags);
Mathias Agopian80727112011-05-02 19:51:12 -0700441 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800442}
443
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700444status_t SurfaceTexture::setSynchronousMode(bool enabled) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700445 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700446 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700447
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700448 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700449 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700450 return NO_INIT;
451 }
452
Mathias Agopian80727112011-05-02 19:51:12 -0700453 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700454 if (!mAllowSynchronousMode && enabled)
455 return err;
456
Mathias Agopian80727112011-05-02 19:51:12 -0700457 if (!enabled) {
458 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700459 err = drainQueueLocked();
460 if (err != NO_ERROR)
461 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700462 }
463
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700464 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700465 // - if we're going to asynchronous mode, the queue is guaranteed to be
466 // empty here
467 // - if the client set the number of buffers, we're guaranteed that
468 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700469 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700470 mDequeueCondition.signal();
471 }
Mathias Agopian80727112011-05-02 19:51:12 -0700472 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700473}
474
Mathias Agopian97c602c2011-07-19 15:24:46 -0700475status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
476 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700477 ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700478
479 sp<FrameAvailableListener> listener;
480
481 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700482 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700483 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700484 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700485 return NO_INIT;
486 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700487 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700488 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700489 mBufferCount, buf);
490 return -EINVAL;
491 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700492 ST_LOGE("queueBuffer: slot %d is not owned by the client "
493 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700494 return -EINVAL;
495 } else if (buf == mCurrentTexture) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700496 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700497 return -EINVAL;
498 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700499 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700500 "buffer", buf);
501 return -EINVAL;
502 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700503
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700504 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700505 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700506 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700507
508 // Synchronous mode always signals that an additional frame should
509 // be consumed.
510 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700511 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700512 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700513 if (mQueue.empty()) {
514 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700515
516 // Asynchronous mode only signals that a frame should be
517 // consumed if no previous frame was pending. If a frame were
518 // pending then the consumer would have already been notified.
519 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700520 } else {
521 Fifo::iterator front(mQueue.begin());
522 // buffer currently queued is freed
523 mSlots[*front].mBufferState = BufferSlot::FREE;
524 // and we record the new buffer index in the queued list
525 *front = buf;
526 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700527 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700528
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700529 mSlots[buf].mBufferState = BufferSlot::QUEUED;
530 mSlots[buf].mCrop = mNextCrop;
531 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700532 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700533 mSlots[buf].mTimestamp = timestamp;
534 mDequeueCondition.signal();
Mathias Agopian3902be62011-08-17 12:45:40 -0700535
536 *outWidth = mDefaultWidth;
537 *outHeight = mDefaultHeight;
538 *outTransform = 0;
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700539 } // scope for the lock
540
541 // call back without lock held
542 if (listener != 0) {
543 listener->onFrameAvailable();
544 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800545 return OK;
546}
547
548void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700549 ST_LOGV("cancelBuffer: slot=%d", buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800550 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700551
552 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700553 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700554 return;
555 }
556
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700557 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700558 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700559 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800560 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700561 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700562 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700563 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800564 return;
565 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700566 mSlots[buf].mBufferState = BufferSlot::FREE;
567 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800568}
569
Jamie Gennisf238e282011-01-09 16:33:17 -0800570status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700571 ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
572 crop.bottom);
573
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800574 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700575 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700576 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700577 return NO_INIT;
578 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800579 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800580 return OK;
581}
582
583status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700584 ST_LOGV("setTransform: xform=%#x", transform);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800585 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700586 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700587 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700588 return NO_INIT;
589 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800590 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800591 return OK;
592}
593
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700594status_t SurfaceTexture::connect(int api,
595 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700596 ST_LOGV("connect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700597 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700598
599 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700600 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700601 return NO_INIT;
602 }
603
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700604 int err = NO_ERROR;
605 switch (api) {
606 case NATIVE_WINDOW_API_EGL:
607 case NATIVE_WINDOW_API_CPU:
608 case NATIVE_WINDOW_API_MEDIA:
609 case NATIVE_WINDOW_API_CAMERA:
610 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700611 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700612 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700613 err = -EINVAL;
614 } else {
615 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700616 *outWidth = mDefaultWidth;
617 *outHeight = mDefaultHeight;
618 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700619 }
620 break;
621 default:
622 err = -EINVAL;
623 break;
624 }
625 return err;
626}
627
628status_t SurfaceTexture::disconnect(int api) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700629 ST_LOGV("disconnect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700630 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700631
632 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700633 ST_LOGE("disconnect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700634 return NO_INIT;
635 }
636
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700637 int err = NO_ERROR;
638 switch (api) {
639 case NATIVE_WINDOW_API_EGL:
640 case NATIVE_WINDOW_API_CPU:
641 case NATIVE_WINDOW_API_MEDIA:
642 case NATIVE_WINDOW_API_CAMERA:
643 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700644 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700645 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700646 mNextCrop.makeInvalid();
647 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
648 mNextTransform = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700649 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700650 } else {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700651 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700652 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700653 err = -EINVAL;
654 }
655 break;
656 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700657 ST_LOGE("disconnect: unknown API %d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700658 err = -EINVAL;
659 break;
660 }
661 return err;
662}
663
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700664status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700665 ST_LOGV("setScalingMode: mode=%d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700666
667 switch (mode) {
668 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
669 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
670 break;
671 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700672 ST_LOGE("unknown scaling mode: %d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700673 return BAD_VALUE;
674 }
675
676 Mutex::Autolock lock(mMutex);
677 mNextScalingMode = mode;
678 return OK;
679}
680
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800681status_t SurfaceTexture::updateTexImage() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700682 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800683 Mutex::Autolock lock(mMutex);
684
Mathias Agopiane47498f2011-08-08 19:35:15 -0700685 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700686 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700687 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700688 }
689
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700690 // In asynchronous mode the list is guaranteed to be one buffer
691 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700692 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700693 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700694 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700695
Jamie Gennisf238e282011-01-09 16:33:17 -0800696 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700697 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800698 if (image == EGL_NO_IMAGE_KHR) {
699 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700700 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700701 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700702 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700703 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700704 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
705 mSlots[buf].mEglImage = image;
706 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700707 if (image == EGL_NO_IMAGE_KHR) {
708 // NOTE: if dpy was invalid, createImage() is guaranteed to
709 // fail. so we'd end up here.
710 return -EINVAL;
711 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800712 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800713
714 GLint error;
715 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700716 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800717 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700718
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700719 glBindTexture(mTexTarget, mTexName);
720 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700721
Jamie Gennis0eb88512011-01-26 11:52:02 -0800722 bool failed = false;
723 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700724 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700725 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800726 failed = true;
727 }
728 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800729 return -EINVAL;
730 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800731
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700732 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
733 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, buf,
734 mSlots[buf].mGraphicBuffer->handle);
735
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700736 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700737 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700738 // state. If it has already been given to the client
739 // (synchronous mode), then it stays in DEQUEUED state.
740 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
741 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
742 }
743
Jamie Gennis9a78c902011-01-12 18:30:40 -0800744 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700745 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700746 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700747 mCurrentCrop = mSlots[buf].mCrop;
748 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700749 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700750 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700751 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700752
753 // Now that we've passed the point at which failures can happen,
754 // it's safe to remove the buffer from the front of the queue.
755 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700756 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700757 } else {
758 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700759 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800760 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700761
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800762 return OK;
763}
764
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700765bool SurfaceTexture::isExternalFormat(uint32_t format)
766{
767 switch (format) {
768 // supported YUV formats
769 case HAL_PIXEL_FORMAT_YV12:
770 // Legacy/deprecated YUV formats
771 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
772 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
773 case HAL_PIXEL_FORMAT_YCbCr_422_I:
774 return true;
775 }
776
777 // Any OEM format needs to be considered
778 if (format>=0x100 && format<=0x1FF)
779 return true;
780
781 return false;
782}
783
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700784GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700785 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700786}
787
Jamie Gennisf238e282011-01-09 16:33:17 -0800788void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800789 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700790 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
791}
792
793void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700794 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800795
Jamie Gennisa214c642011-01-14 13:53:31 -0800796 float xform[16];
797 for (int i = 0; i < 16; i++) {
798 xform[i] = mtxIdentity[i];
799 }
800 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
801 float result[16];
802 mtxMul(result, xform, mtxFlipH);
803 for (int i = 0; i < 16; i++) {
804 xform[i] = result[i];
805 }
806 }
807 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
808 float result[16];
809 mtxMul(result, xform, mtxFlipV);
810 for (int i = 0; i < 16; i++) {
811 xform[i] = result[i];
812 }
813 }
814 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
815 float result[16];
816 mtxMul(result, xform, mtxRot90);
817 for (int i = 0; i < 16; i++) {
818 xform[i] = result[i];
819 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800820 }
821
822 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800823 float tx, ty, sx, sy;
824 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800825 // In order to prevent bilinear sampling at the of the crop rectangle we
826 // may need to shrink it by 2 texels in each direction. Normally this
827 // would just need to take 1/2 a texel off each end, but because the
828 // chroma channels will likely be subsampled we need to chop off a whole
829 // texel. This will cause artifacts if someone does nearest sampling
830 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
831 // accomodate the bilinear and nearest sampling uses.
832 //
833 // If nearest sampling turns out to be a desirable usage of these
834 // textures then we could add the ability to switch a SurfaceTexture to
835 // nearest-mode. Preferably, however, the image producers (video
836 // decoder, camera, etc.) would simply not use a crop rectangle (or at
837 // least not tell the framework about it) so that the GPU can do the
838 // correct edge behavior.
839 int xshrink = 0, yshrink = 0;
840 if (mCurrentCrop.left > 0) {
841 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
842 xshrink++;
843 } else {
844 tx = 0.0f;
845 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700846 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800847 xshrink++;
848 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700849 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800850 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
851 float(buf->getHeight());
852 yshrink++;
853 } else {
854 ty = 0.0f;
855 }
856 if (mCurrentCrop.top > 0) {
857 yshrink++;
858 }
859 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
860 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800861 } else {
862 tx = 0.0f;
863 ty = 0.0f;
864 sx = 1.0f;
865 sy = 1.0f;
866 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800867 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800868 sx, 0, 0, 0,
869 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800870 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800871 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800872 };
873
Jamie Gennisa214c642011-01-14 13:53:31 -0800874 float mtxBeforeFlipV[16];
875 mtxMul(mtxBeforeFlipV, crop, xform);
876
877 // SurfaceFlinger expects the top of its window textures to be at a Y
878 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
879 // want to expose this to applications, however, so we must add an
880 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700881 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800882}
883
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800884nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700885 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800886 Mutex::Autolock lock(mMutex);
887 return mCurrentTimestamp;
888}
889
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800890void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700891 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700892 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800893 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700894 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800895}
896
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700897void SurfaceTexture::freeBufferLocked(int i) {
898 mSlots[i].mGraphicBuffer = 0;
899 mSlots[i].mBufferState = BufferSlot::FREE;
900 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
901 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
902 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
903 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
904 }
905}
906
Mathias Agopianef51b992011-08-10 15:28:58 -0700907void SurfaceTexture::freeAllBuffersLocked() {
908 LOGW_IF(!mQueue.isEmpty(),
909 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian29b57622011-08-17 15:42:04 -0700910 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800911 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700912 freeBufferLocked(i);
913 }
914}
915
916void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
917 LOGW_IF(!mQueue.isEmpty(),
918 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
919 int head = -1;
920 if (!mQueue.empty()) {
921 Fifo::iterator front(mQueue.begin());
922 head = *front;
923 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700924 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700925 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
926 if (i != head) {
927 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800928 }
929 }
930}
931
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700932status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700933 while (mSynchronousMode && !mQueue.isEmpty()) {
934 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700935 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700936 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700937 return NO_INIT;
938 }
939 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700940 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700941 return NO_INIT;
942 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700943 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700944 return NO_ERROR;
945}
946
947status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
948 status_t err = drainQueueLocked();
949 if (err == NO_ERROR) {
950 if (mSynchronousMode) {
951 freeAllBuffersLocked();
952 } else {
953 freeAllBuffersExceptHeadLocked();
954 }
955 }
956 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700957}
958
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800959EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
960 const sp<GraphicBuffer>& graphicBuffer) {
961 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
962 EGLint attrs[] = {
963 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
964 EGL_NONE,
965 };
966 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
967 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700968 if (image == EGL_NO_IMAGE_KHR) {
969 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700970 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800971 }
972 return image;
973}
974
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700975sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
976 Mutex::Autolock lock(mMutex);
977 return mCurrentTextureBuf;
978}
979
980Rect SurfaceTexture::getCurrentCrop() const {
981 Mutex::Autolock lock(mMutex);
982 return mCurrentCrop;
983}
984
985uint32_t SurfaceTexture::getCurrentTransform() const {
986 Mutex::Autolock lock(mMutex);
987 return mCurrentTransform;
988}
989
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700990uint32_t SurfaceTexture::getCurrentScalingMode() const {
991 Mutex::Autolock lock(mMutex);
992 return mCurrentScalingMode;
993}
994
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700995int SurfaceTexture::query(int what, int* outValue)
996{
997 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700998
999 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -07001000 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001001 return NO_INIT;
1002 }
1003
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001004 int value;
1005 switch (what) {
1006 case NATIVE_WINDOW_WIDTH:
1007 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001008 break;
1009 case NATIVE_WINDOW_HEIGHT:
1010 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001011 break;
1012 case NATIVE_WINDOW_FORMAT:
1013 value = mPixelFormat;
1014 break;
1015 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1016 value = mSynchronousMode ?
1017 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
1018 break;
1019 default:
1020 return BAD_VALUE;
1021 }
1022 outValue[0] = value;
1023 return NO_ERROR;
1024}
Mathias Agopian7a042bf2011-04-11 21:19:55 -07001025
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001026void SurfaceTexture::abandon() {
1027 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001028 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001029 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -07001030 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001031 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001032 mDequeueCondition.signal();
1033}
1034
Jamie Gennisfa28c352011-09-16 17:30:26 -07001035void SurfaceTexture::setName(const String8& name) {
1036 mName = name;
1037}
1038
Mathias Agopian68c77942011-05-09 19:08:33 -07001039void SurfaceTexture::dump(String8& result) const
1040{
1041 char buffer[1024];
1042 dump(result, "", buffer, 1024);
1043}
1044
1045void SurfaceTexture::dump(String8& result, const char* prefix,
1046 char* buffer, size_t SIZE) const
1047{
1048 Mutex::Autolock _l(mMutex);
1049 snprintf(buffer, SIZE,
1050 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1051 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001052 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1053 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -07001054 result.append(buffer);
1055
1056 String8 fifo;
1057 int fifoSize = 0;
1058 Fifo::const_iterator i(mQueue.begin());
1059 while (i != mQueue.end()) {
1060 snprintf(buffer, SIZE, "%02d ", *i++);
1061 fifoSize++;
1062 fifo.append(buffer);
1063 }
1064
1065 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001066 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001067 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1068 ,
1069 prefix, mCurrentCrop.left,
1070 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001071 mCurrentTransform, mCurrentTexture,
Jamie Gennisfa28c352011-09-16 17:30:26 -07001072 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1073 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -07001074 );
1075 result.append(buffer);
1076
1077 struct {
1078 const char * operator()(int state) const {
1079 switch (state) {
1080 case BufferSlot::DEQUEUED: return "DEQUEUED";
1081 case BufferSlot::QUEUED: return "QUEUED";
1082 case BufferSlot::FREE: return "FREE";
1083 default: return "Unknown";
1084 }
1085 }
1086 } stateName;
1087
1088 for (int i=0 ; i<mBufferCount ; i++) {
1089 const BufferSlot& slot(mSlots[i]);
1090 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001091 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001092 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001093 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001094 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001095 stateName(slot.mBufferState),
Jamie Gennisfa28c352011-09-16 17:30:26 -07001096 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1097 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001098 );
1099 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001100
1101 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1102 if (buf != NULL) {
1103 snprintf(buffer, SIZE,
1104 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001105 buf->handle, buf->width, buf->height, buf->stride,
1106 buf->format);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001107 result.append(buffer);
1108 }
1109 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001110 }
1111}
1112
Jamie Gennisf238e282011-01-09 16:33:17 -08001113static void mtxMul(float out[16], const float a[16], const float b[16]) {
1114 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1115 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1116 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1117 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1118
1119 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1120 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1121 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1122 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1123
1124 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1125 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1126 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1127 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1128
1129 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1130 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1131 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1132 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1133}
1134
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001135}; // namespace android