blob: ab154edff1ffac19e65e7a76425df82a453c915b [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 }
Jamie Gennisaaa3ecf2011-11-17 16:00:44 -0800437 if (mCurrentTexture == buf) {
438 // The current texture no longer references the buffer in this slot
439 // since we just allocated a new buffer.
440 mCurrentTexture = INVALID_BUFFER_SLOT;
441 }
Mathias Agopian80727112011-05-02 19:51:12 -0700442 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700443 }
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700444 ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", buf,
445 mSlots[buf].mGraphicBuffer->handle, returnFlags);
Mathias Agopian80727112011-05-02 19:51:12 -0700446 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800447}
448
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700449status_t SurfaceTexture::setSynchronousMode(bool enabled) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700450 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700451 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700452
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700453 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700454 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700455 return NO_INIT;
456 }
457
Mathias Agopian80727112011-05-02 19:51:12 -0700458 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700459 if (!mAllowSynchronousMode && enabled)
460 return err;
461
Mathias Agopian80727112011-05-02 19:51:12 -0700462 if (!enabled) {
463 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700464 err = drainQueueLocked();
465 if (err != NO_ERROR)
466 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700467 }
468
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700469 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700470 // - if we're going to asynchronous mode, the queue is guaranteed to be
471 // empty here
472 // - if the client set the number of buffers, we're guaranteed that
473 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700474 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700475 mDequeueCondition.signal();
476 }
Mathias Agopian80727112011-05-02 19:51:12 -0700477 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700478}
479
Mathias Agopian97c602c2011-07-19 15:24:46 -0700480status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
481 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700482 ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700483
484 sp<FrameAvailableListener> listener;
485
486 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700487 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700488 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700489 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700490 return NO_INIT;
491 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700492 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700493 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700494 mBufferCount, buf);
495 return -EINVAL;
496 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700497 ST_LOGE("queueBuffer: slot %d is not owned by the client "
498 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700499 return -EINVAL;
500 } else if (buf == mCurrentTexture) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700501 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700502 return -EINVAL;
503 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700504 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700505 "buffer", buf);
506 return -EINVAL;
507 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700508
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700509 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700510 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700511 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700512
513 // Synchronous mode always signals that an additional frame should
514 // be consumed.
515 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700516 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700517 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700518 if (mQueue.empty()) {
519 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700520
521 // Asynchronous mode only signals that a frame should be
522 // consumed if no previous frame was pending. If a frame were
523 // pending then the consumer would have already been notified.
524 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700525 } else {
526 Fifo::iterator front(mQueue.begin());
527 // buffer currently queued is freed
528 mSlots[*front].mBufferState = BufferSlot::FREE;
529 // and we record the new buffer index in the queued list
530 *front = buf;
531 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700532 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700533
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700534 mSlots[buf].mBufferState = BufferSlot::QUEUED;
535 mSlots[buf].mCrop = mNextCrop;
536 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700537 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700538 mSlots[buf].mTimestamp = timestamp;
539 mDequeueCondition.signal();
Mathias Agopian3902be62011-08-17 12:45:40 -0700540
541 *outWidth = mDefaultWidth;
542 *outHeight = mDefaultHeight;
543 *outTransform = 0;
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700544 } // scope for the lock
545
546 // call back without lock held
547 if (listener != 0) {
548 listener->onFrameAvailable();
549 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800550 return OK;
551}
552
553void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700554 ST_LOGV("cancelBuffer: slot=%d", buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800555 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700556
557 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700558 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700559 return;
560 }
561
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700562 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700563 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700564 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800565 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700566 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700567 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700568 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800569 return;
570 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700571 mSlots[buf].mBufferState = BufferSlot::FREE;
572 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800573}
574
Jamie Gennisf238e282011-01-09 16:33:17 -0800575status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700576 ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
577 crop.bottom);
578
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800579 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700580 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700581 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700582 return NO_INIT;
583 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800584 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800585 return OK;
586}
587
588status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700589 ST_LOGV("setTransform: xform=%#x", transform);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800590 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700591 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700592 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700593 return NO_INIT;
594 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800595 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800596 return OK;
597}
598
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700599status_t SurfaceTexture::connect(int api,
600 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700601 ST_LOGV("connect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700602 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700603
604 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700605 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700606 return NO_INIT;
607 }
608
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700609 int err = NO_ERROR;
610 switch (api) {
611 case NATIVE_WINDOW_API_EGL:
612 case NATIVE_WINDOW_API_CPU:
613 case NATIVE_WINDOW_API_MEDIA:
614 case NATIVE_WINDOW_API_CAMERA:
615 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700616 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700617 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700618 err = -EINVAL;
619 } else {
620 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700621 *outWidth = mDefaultWidth;
622 *outHeight = mDefaultHeight;
623 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700624 }
625 break;
626 default:
627 err = -EINVAL;
628 break;
629 }
630 return err;
631}
632
633status_t SurfaceTexture::disconnect(int api) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700634 ST_LOGV("disconnect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700635 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700636
637 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700638 ST_LOGE("disconnect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700639 return NO_INIT;
640 }
641
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700642 int err = NO_ERROR;
643 switch (api) {
644 case NATIVE_WINDOW_API_EGL:
645 case NATIVE_WINDOW_API_CPU:
646 case NATIVE_WINDOW_API_MEDIA:
647 case NATIVE_WINDOW_API_CAMERA:
648 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700649 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700650 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700651 mNextCrop.makeInvalid();
652 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
653 mNextTransform = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700654 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700655 } else {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700656 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700657 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700658 err = -EINVAL;
659 }
660 break;
661 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700662 ST_LOGE("disconnect: unknown API %d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700663 err = -EINVAL;
664 break;
665 }
666 return err;
667}
668
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700669status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700670 ST_LOGV("setScalingMode: mode=%d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700671
672 switch (mode) {
673 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
674 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
675 break;
676 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700677 ST_LOGE("unknown scaling mode: %d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700678 return BAD_VALUE;
679 }
680
681 Mutex::Autolock lock(mMutex);
682 mNextScalingMode = mode;
683 return OK;
684}
685
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800686status_t SurfaceTexture::updateTexImage() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700687 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800688 Mutex::Autolock lock(mMutex);
689
Mathias Agopiane47498f2011-08-08 19:35:15 -0700690 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700691 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700692 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700693 }
694
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700695 // In asynchronous mode the list is guaranteed to be one buffer
696 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700697 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700698 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700699 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700700
Jamie Gennisf238e282011-01-09 16:33:17 -0800701 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700702 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800703 if (image == EGL_NO_IMAGE_KHR) {
704 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700705 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700706 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700707 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700708 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700709 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
710 mSlots[buf].mEglImage = image;
711 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700712 if (image == EGL_NO_IMAGE_KHR) {
713 // NOTE: if dpy was invalid, createImage() is guaranteed to
714 // fail. so we'd end up here.
715 return -EINVAL;
716 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800717 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800718
719 GLint error;
720 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700721 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800722 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700723
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700724 glBindTexture(mTexTarget, mTexName);
725 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700726
Jamie Gennis0eb88512011-01-26 11:52:02 -0800727 bool failed = false;
728 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700729 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700730 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800731 failed = true;
732 }
733 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800734 return -EINVAL;
735 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800736
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700737 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
738 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, buf,
739 mSlots[buf].mGraphicBuffer->handle);
740
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700741 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700742 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700743 // state. If it has already been given to the client
744 // (synchronous mode), then it stays in DEQUEUED state.
745 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
746 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
747 }
748
Jamie Gennis9a78c902011-01-12 18:30:40 -0800749 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700750 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700751 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700752 mCurrentCrop = mSlots[buf].mCrop;
753 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700754 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700755 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700756 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700757
758 // Now that we've passed the point at which failures can happen,
759 // it's safe to remove the buffer from the front of the queue.
760 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700761 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700762 } else {
763 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700764 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800765 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700766
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800767 return OK;
768}
769
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700770bool SurfaceTexture::isExternalFormat(uint32_t format)
771{
772 switch (format) {
773 // supported YUV formats
774 case HAL_PIXEL_FORMAT_YV12:
775 // Legacy/deprecated YUV formats
776 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
777 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
778 case HAL_PIXEL_FORMAT_YCbCr_422_I:
779 return true;
780 }
781
782 // Any OEM format needs to be considered
783 if (format>=0x100 && format<=0x1FF)
784 return true;
785
786 return false;
787}
788
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700789GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700790 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700791}
792
Jamie Gennisf238e282011-01-09 16:33:17 -0800793void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800794 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700795 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
796}
797
798void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700799 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800800
Jamie Gennisa214c642011-01-14 13:53:31 -0800801 float xform[16];
802 for (int i = 0; i < 16; i++) {
803 xform[i] = mtxIdentity[i];
804 }
805 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
806 float result[16];
807 mtxMul(result, xform, mtxFlipH);
808 for (int i = 0; i < 16; i++) {
809 xform[i] = result[i];
810 }
811 }
812 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
813 float result[16];
814 mtxMul(result, xform, mtxFlipV);
815 for (int i = 0; i < 16; i++) {
816 xform[i] = result[i];
817 }
818 }
819 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
820 float result[16];
821 mtxMul(result, xform, mtxRot90);
822 for (int i = 0; i < 16; i++) {
823 xform[i] = result[i];
824 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800825 }
826
827 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800828 float tx, ty, sx, sy;
829 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800830 // In order to prevent bilinear sampling at the of the crop rectangle we
831 // may need to shrink it by 2 texels in each direction. Normally this
832 // would just need to take 1/2 a texel off each end, but because the
833 // chroma channels will likely be subsampled we need to chop off a whole
834 // texel. This will cause artifacts if someone does nearest sampling
835 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
836 // accomodate the bilinear and nearest sampling uses.
837 //
838 // If nearest sampling turns out to be a desirable usage of these
839 // textures then we could add the ability to switch a SurfaceTexture to
840 // nearest-mode. Preferably, however, the image producers (video
841 // decoder, camera, etc.) would simply not use a crop rectangle (or at
842 // least not tell the framework about it) so that the GPU can do the
843 // correct edge behavior.
844 int xshrink = 0, yshrink = 0;
845 if (mCurrentCrop.left > 0) {
846 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
847 xshrink++;
848 } else {
849 tx = 0.0f;
850 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700851 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800852 xshrink++;
853 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700854 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800855 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
856 float(buf->getHeight());
857 yshrink++;
858 } else {
859 ty = 0.0f;
860 }
861 if (mCurrentCrop.top > 0) {
862 yshrink++;
863 }
864 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
865 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800866 } else {
867 tx = 0.0f;
868 ty = 0.0f;
869 sx = 1.0f;
870 sy = 1.0f;
871 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800872 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800873 sx, 0, 0, 0,
874 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800875 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800876 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800877 };
878
Jamie Gennisa214c642011-01-14 13:53:31 -0800879 float mtxBeforeFlipV[16];
880 mtxMul(mtxBeforeFlipV, crop, xform);
881
882 // SurfaceFlinger expects the top of its window textures to be at a Y
883 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
884 // want to expose this to applications, however, so we must add an
885 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700886 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800887}
888
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800889nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700890 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800891 Mutex::Autolock lock(mMutex);
892 return mCurrentTimestamp;
893}
894
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800895void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700896 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700897 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800898 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700899 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800900}
901
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700902void SurfaceTexture::freeBufferLocked(int i) {
903 mSlots[i].mGraphicBuffer = 0;
904 mSlots[i].mBufferState = BufferSlot::FREE;
905 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
906 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
907 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
908 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
909 }
910}
911
Mathias Agopianef51b992011-08-10 15:28:58 -0700912void SurfaceTexture::freeAllBuffersLocked() {
913 LOGW_IF(!mQueue.isEmpty(),
914 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian29b57622011-08-17 15:42:04 -0700915 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800916 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700917 freeBufferLocked(i);
918 }
919}
920
921void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
922 LOGW_IF(!mQueue.isEmpty(),
923 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
924 int head = -1;
925 if (!mQueue.empty()) {
926 Fifo::iterator front(mQueue.begin());
927 head = *front;
928 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700929 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700930 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
931 if (i != head) {
932 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800933 }
934 }
935}
936
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700937status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700938 while (mSynchronousMode && !mQueue.isEmpty()) {
939 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700940 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700941 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700942 return NO_INIT;
943 }
944 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700945 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700946 return NO_INIT;
947 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700948 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700949 return NO_ERROR;
950}
951
952status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
953 status_t err = drainQueueLocked();
954 if (err == NO_ERROR) {
955 if (mSynchronousMode) {
956 freeAllBuffersLocked();
957 } else {
958 freeAllBuffersExceptHeadLocked();
959 }
960 }
961 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700962}
963
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800964EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
965 const sp<GraphicBuffer>& graphicBuffer) {
966 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
967 EGLint attrs[] = {
968 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
969 EGL_NONE,
970 };
971 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
972 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700973 if (image == EGL_NO_IMAGE_KHR) {
974 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700975 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800976 }
977 return image;
978}
979
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700980sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
981 Mutex::Autolock lock(mMutex);
982 return mCurrentTextureBuf;
983}
984
985Rect SurfaceTexture::getCurrentCrop() const {
986 Mutex::Autolock lock(mMutex);
987 return mCurrentCrop;
988}
989
990uint32_t SurfaceTexture::getCurrentTransform() const {
991 Mutex::Autolock lock(mMutex);
992 return mCurrentTransform;
993}
994
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700995uint32_t SurfaceTexture::getCurrentScalingMode() const {
996 Mutex::Autolock lock(mMutex);
997 return mCurrentScalingMode;
998}
999
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001000int SurfaceTexture::query(int what, int* outValue)
1001{
1002 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001003
1004 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -07001005 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001006 return NO_INIT;
1007 }
1008
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001009 int value;
1010 switch (what) {
1011 case NATIVE_WINDOW_WIDTH:
1012 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001013 break;
1014 case NATIVE_WINDOW_HEIGHT:
1015 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001016 break;
1017 case NATIVE_WINDOW_FORMAT:
1018 value = mPixelFormat;
1019 break;
1020 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1021 value = mSynchronousMode ?
1022 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
1023 break;
1024 default:
1025 return BAD_VALUE;
1026 }
1027 outValue[0] = value;
1028 return NO_ERROR;
1029}
Mathias Agopian7a042bf2011-04-11 21:19:55 -07001030
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001031void SurfaceTexture::abandon() {
1032 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001033 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001034 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -07001035 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001036 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001037 mDequeueCondition.signal();
1038}
1039
Jamie Gennisfa28c352011-09-16 17:30:26 -07001040void SurfaceTexture::setName(const String8& name) {
1041 mName = name;
1042}
1043
Mathias Agopian68c77942011-05-09 19:08:33 -07001044void SurfaceTexture::dump(String8& result) const
1045{
1046 char buffer[1024];
1047 dump(result, "", buffer, 1024);
1048}
1049
1050void SurfaceTexture::dump(String8& result, const char* prefix,
1051 char* buffer, size_t SIZE) const
1052{
1053 Mutex::Autolock _l(mMutex);
1054 snprintf(buffer, SIZE,
1055 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1056 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001057 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1058 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -07001059 result.append(buffer);
1060
1061 String8 fifo;
1062 int fifoSize = 0;
1063 Fifo::const_iterator i(mQueue.begin());
1064 while (i != mQueue.end()) {
1065 snprintf(buffer, SIZE, "%02d ", *i++);
1066 fifoSize++;
1067 fifo.append(buffer);
1068 }
1069
1070 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001071 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001072 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1073 ,
1074 prefix, mCurrentCrop.left,
1075 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001076 mCurrentTransform, mCurrentTexture,
Jamie Gennisfa28c352011-09-16 17:30:26 -07001077 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1078 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -07001079 );
1080 result.append(buffer);
1081
1082 struct {
1083 const char * operator()(int state) const {
1084 switch (state) {
1085 case BufferSlot::DEQUEUED: return "DEQUEUED";
1086 case BufferSlot::QUEUED: return "QUEUED";
1087 case BufferSlot::FREE: return "FREE";
1088 default: return "Unknown";
1089 }
1090 }
1091 } stateName;
1092
1093 for (int i=0 ; i<mBufferCount ; i++) {
1094 const BufferSlot& slot(mSlots[i]);
1095 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001096 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001097 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001098 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001099 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001100 stateName(slot.mBufferState),
Jamie Gennisfa28c352011-09-16 17:30:26 -07001101 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1102 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001103 );
1104 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001105
1106 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1107 if (buf != NULL) {
1108 snprintf(buffer, SIZE,
1109 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001110 buf->handle, buf->width, buf->height, buf->stride,
1111 buf->format);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001112 result.append(buffer);
1113 }
1114 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001115 }
1116}
1117
Jamie Gennisf238e282011-01-09 16:33:17 -08001118static void mtxMul(float out[16], const float a[16], const float b[16]) {
1119 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1120 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1121 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1122 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1123
1124 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1125 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1126 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1127 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1128
1129 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1130 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1131 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1132 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1133
1134 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1135 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1136 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1137 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1138}
1139
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001140}; // namespace android