blob: c72a45b895767b60c72eecd3ca6ed7873d03b8b1 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture"
Jamie Gennise70d8b42011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#define GL_GLEXT_PROTOTYPES
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <gui/SurfaceTexture.h>
29
Mathias Agopian7a042bf2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070037#include <utils/String8.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038
Mathias Agopian29b57622011-08-17 15:42:04 -070039
40#define ALLOW_DEQUEUE_CURRENT_BUFFER false
41
Jamie Gennisfa28c352011-09-16 17:30:26 -070042// Macros for including the SurfaceTexture name in log messages
43#define ST_LOGV(x, ...) LOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
44#define ST_LOGD(x, ...) LOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
45#define ST_LOGI(x, ...) LOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
46#define ST_LOGW(x, ...) LOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
47#define ST_LOGE(x, ...) LOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070048
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080049namespace android {
50
Jamie Gennisf238e282011-01-09 16:33:17 -080051// Transform matrices
52static float mtxIdentity[16] = {
53 1, 0, 0, 0,
54 0, 1, 0, 0,
55 0, 0, 1, 0,
56 0, 0, 0, 1,
57};
58static float mtxFlipH[16] = {
59 -1, 0, 0, 0,
60 0, 1, 0, 0,
61 0, 0, 1, 0,
62 1, 0, 0, 1,
63};
64static float mtxFlipV[16] = {
65 1, 0, 0, 0,
66 0, -1, 0, 0,
67 0, 0, 1, 0,
68 0, 1, 0, 1,
69};
70static float mtxRot90[16] = {
71 0, 1, 0, 0,
72 -1, 0, 0, 0,
73 0, 0, 1, 0,
74 1, 0, 0, 1,
75};
76static float mtxRot180[16] = {
77 -1, 0, 0, 0,
78 0, -1, 0, 0,
79 0, 0, 1, 0,
80 1, 1, 0, 1,
81};
82static float mtxRot270[16] = {
83 0, -1, 0, 0,
84 1, 0, 0, 0,
85 0, 0, 1, 0,
86 0, 1, 0, 1,
87};
88
89static void mtxMul(float out[16], const float a[16], const float b[16]);
90
Jamie Gennisfa28c352011-09-16 17:30:26 -070091// Get an ID that's unique within this process.
92static int32_t createProcessUniqueId() {
93 static volatile int32_t globalCounter = 0;
94 return android_atomic_inc(&globalCounter);
95}
96
Jamie Gennisfb1b5a22011-09-28 12:13:31 -070097SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
98 GLenum texTarget) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070099 mDefaultWidth(1),
100 mDefaultHeight(1),
101 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -0700102 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
103 mClientBufferCount(0),
104 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800105 mCurrentTexture(INVALID_BUFFER_SLOT),
106 mCurrentTransform(0),
107 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800108 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700109 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700110 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -0700111 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700112 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700113 mConnectedApi(NO_CONNECTED_API),
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700114 mAbandoned(false),
115 mTexTarget(texTarget) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700116 // Choose a name using the PID and a process-unique ID.
117 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
118
119 ST_LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -0800120 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
121 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700122 mNextCrop.makeInvalid();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700123 memcpy(mCurrentTransformMatrix, mtxIdentity,
124 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800125}
126
127SurfaceTexture::~SurfaceTexture() {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700128 ST_LOGV("SurfaceTexture::~SurfaceTexture");
Mathias Agopianef51b992011-08-10 15:28:58 -0700129 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800130}
131
Mathias Agopian80727112011-05-02 19:51:12 -0700132status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
133 if (bufferCount > NUM_BUFFER_SLOTS)
134 return BAD_VALUE;
135
136 // special-case, nothing to do
137 if (bufferCount == mBufferCount)
138 return OK;
139
140 if (!mClientBufferCount &&
141 bufferCount >= mBufferCount) {
142 // easy, we just have more buffers
143 mBufferCount = bufferCount;
144 mServerBufferCount = bufferCount;
145 mDequeueCondition.signal();
146 } else {
147 // we're here because we're either
148 // - reducing the number of available buffers
149 // - or there is a client-buffer-count in effect
150
151 // less than 2 buffers is never allowed
152 if (bufferCount < 2)
153 return BAD_VALUE;
154
155 // when there is non client-buffer-count in effect, the client is not
156 // allowed to dequeue more than one buffer at a time,
157 // so the next time they dequeue a buffer, we know that they don't
158 // own one. the actual resizing will happen during the next
159 // dequeueBuffer.
160
161 mServerBufferCount = bufferCount;
162 }
163 return OK;
164}
165
166status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
167 Mutex::Autolock lock(mMutex);
168 return setBufferCountServerLocked(bufferCount);
169}
170
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800171status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700172 ST_LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700173 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800174
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700175 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700176 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700177 return NO_INIT;
178 }
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700179 if (bufferCount > NUM_BUFFER_SLOTS) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700180 ST_LOGE("setBufferCount: bufferCount larger than slots available");
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700181 return BAD_VALUE;
182 }
183
Mathias Agopian80727112011-05-02 19:51:12 -0700184 // Error out if the user has dequeued buffers
185 for (int i=0 ; i<mBufferCount ; i++) {
186 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700187 ST_LOGE("setBufferCount: client owns some buffers");
Mathias Agopian80727112011-05-02 19:51:12 -0700188 return -EINVAL;
189 }
190 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700191
Jamie Gennis1c121f62011-07-30 16:00:11 -0700192 const int minBufferSlots = mSynchronousMode ?
193 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian80727112011-05-02 19:51:12 -0700194 if (bufferCount == 0) {
Mathias Agopian80727112011-05-02 19:51:12 -0700195 mClientBufferCount = 0;
196 bufferCount = (mServerBufferCount >= minBufferSlots) ?
197 mServerBufferCount : minBufferSlots;
198 return setBufferCountServerLocked(bufferCount);
199 }
200
Jamie Gennis1c121f62011-07-30 16:00:11 -0700201 if (bufferCount < minBufferSlots) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700202 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
Jamie Gennis1c121f62011-07-30 16:00:11 -0700203 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800204 return BAD_VALUE;
205 }
206
Mathias Agopian80727112011-05-02 19:51:12 -0700207 // here we're guaranteed that the client doesn't have dequeued buffers
208 // and will release all of its buffer references.
Mathias Agopianef51b992011-08-10 15:28:58 -0700209 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800210 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700211 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800212 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700213 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700214 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800215 return OK;
216}
217
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700218status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
219{
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700220 if (!w || !h) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700221 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
222 w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700223 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700224 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700225
226 Mutex::Autolock lock(mMutex);
227 mDefaultWidth = w;
228 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700229 return OK;
230}
231
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700232status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700233 ST_LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800234 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700235 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700236 ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700237 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800238 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700239 if (slot < 0 || mBufferCount <= slot) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700240 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700241 mBufferCount, slot);
242 return BAD_VALUE;
243 }
244 mSlots[slot].mRequestBufferCalled = true;
245 *buf = mSlots[slot].mGraphicBuffer;
246 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700247}
248
249status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
250 uint32_t format, uint32_t usage) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700251 ST_LOGV("SurfaceTexture::dequeueBuffer");
Mathias Agopianc04f1532011-04-25 20:22:14 -0700252
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700253 if ((w && !h) || (!w && h)) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700254 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700255 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700256 }
257
Mathias Agopianc04f1532011-04-25 20:22:14 -0700258 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700259
260 status_t returnFlags(OK);
261
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700262 int found, foundSync;
263 int dequeuedCount = 0;
264 bool tryAgain = true;
265 while (tryAgain) {
Mathias Agopian2560d142011-08-10 16:33:23 -0700266 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700267 ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
Mathias Agopian2560d142011-08-10 16:33:23 -0700268 return NO_INIT;
269 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700270
Mathias Agopian80727112011-05-02 19:51:12 -0700271 // We need to wait for the FIFO to drain if the number of buffer
272 // needs to change.
273 //
Mathias Agopian2560d142011-08-10 16:33:23 -0700274 // The condition "number of buffers needs to change" is true if
Mathias Agopian80727112011-05-02 19:51:12 -0700275 // - the client doesn't care about how many buffers there are
276 // - AND the actual number of buffer is different from what was
277 // set in the last setBufferCountServer()
278 // - OR -
279 // setBufferCountServer() was set to a value incompatible with
280 // the synchronization mode (for instance because the sync mode
281 // changed since)
282 //
283 // As long as this condition is true AND the FIFO is not empty, we
284 // wait on mDequeueCondition.
285
Mathias Agopian2560d142011-08-10 16:33:23 -0700286 const int minBufferCountNeeded = mSynchronousMode ?
Mathias Agopian80727112011-05-02 19:51:12 -0700287 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
288
Mathias Agopian2560d142011-08-10 16:33:23 -0700289 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
Mathias Agopian80727112011-05-02 19:51:12 -0700290 ((mServerBufferCount != mBufferCount) ||
Mathias Agopian2560d142011-08-10 16:33:23 -0700291 (mServerBufferCount < minBufferCountNeeded));
292
293 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700294 // wait for the FIFO to drain
Mathias Agopian2560d142011-08-10 16:33:23 -0700295 mDequeueCondition.wait(mMutex);
296 // NOTE: we continue here because we need to reevaluate our
297 // whole state (eg: we could be abandoned or disconnected)
298 continue;
Mathias Agopian80727112011-05-02 19:51:12 -0700299 }
300
Mathias Agopian2560d142011-08-10 16:33:23 -0700301 if (numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700302 // here we're guaranteed that mQueue is empty
Mathias Agopianef51b992011-08-10 15:28:58 -0700303 freeAllBuffersLocked();
Mathias Agopian80727112011-05-02 19:51:12 -0700304 mBufferCount = mServerBufferCount;
305 if (mBufferCount < minBufferCountNeeded)
306 mBufferCount = minBufferCountNeeded;
307 mCurrentTexture = INVALID_BUFFER_SLOT;
308 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
309 }
310
311 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700312 found = INVALID_BUFFER_SLOT;
313 foundSync = INVALID_BUFFER_SLOT;
314 dequeuedCount = 0;
315 for (int i = 0; i < mBufferCount; i++) {
316 const int state = mSlots[i].mBufferState;
317 if (state == BufferSlot::DEQUEUED) {
318 dequeuedCount++;
319 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700320
321 // if buffer is FREE it CANNOT be current
322 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
323 "dequeueBuffer: buffer %d is both FREE and current!", i);
324
325 if (ALLOW_DEQUEUE_CURRENT_BUFFER) {
326 if (state == BufferSlot::FREE || i == mCurrentTexture) {
327 foundSync = i;
328 if (i != mCurrentTexture) {
329 found = i;
330 break;
331 }
332 }
333 } else {
334 if (state == BufferSlot::FREE) {
335 foundSync = i;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700336 found = i;
337 break;
338 }
339 }
340 }
Mathias Agopian80727112011-05-02 19:51:12 -0700341
342 // clients are not allowed to dequeue more than one buffer
343 // if they didn't set a buffer count.
344 if (!mClientBufferCount && dequeuedCount) {
345 return -EINVAL;
346 }
347
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700348 // See whether a buffer has been queued since the last setBufferCount so
349 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
350 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
351 if (bufferHasBeenQueued) {
352 // make sure the client is not trying to dequeue more buffers
353 // than allowed.
354 const int avail = mBufferCount - (dequeuedCount+1);
355 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700356 ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
357 "(dequeued=%d)",
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700358 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
359 dequeuedCount);
360 return -EBUSY;
361 }
Mathias Agopian80727112011-05-02 19:51:12 -0700362 }
363
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700364 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian29b57622011-08-17 15:42:04 -0700365 // for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700366 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
367 if (tryAgain) {
368 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700369 }
370 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700371
Mathias Agopian80727112011-05-02 19:51:12 -0700372 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
373 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
374 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700375 }
376
Mathias Agopianc04f1532011-04-25 20:22:14 -0700377 if (found == INVALID_BUFFER_SLOT) {
378 return -EBUSY;
379 }
380
381 const int buf = found;
382 *outBuf = found;
383
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700384 const bool useDefaultSize = !w && !h;
385 if (useDefaultSize) {
386 // use the default size
387 w = mDefaultWidth;
388 h = mDefaultHeight;
389 }
390
391 const bool updateFormat = (format != 0);
392 if (!updateFormat) {
393 // keep the current (or default) format
394 format = mPixelFormat;
395 }
396
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700397 // buffer is now in DEQUEUED (but can also be current at the same time,
398 // if we're in synchronous mode)
399 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
400
401 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700402 if ((buffer == NULL) ||
403 (uint32_t(buffer->width) != w) ||
404 (uint32_t(buffer->height) != h) ||
405 (uint32_t(buffer->format) != format) ||
406 ((uint32_t(buffer->usage) & usage) != usage))
407 {
408 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700409 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700410 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700411 mGraphicBufferAlloc->createGraphicBuffer(
412 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700413 if (graphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700414 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
415 "failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700416 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700417 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700418 if (updateFormat) {
419 mPixelFormat = format;
420 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800421 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700422 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800423 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
424 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
425 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
426 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
427 }
Mathias Agopian80727112011-05-02 19:51:12 -0700428 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700429 }
Mathias Agopian80727112011-05-02 19:51:12 -0700430 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800431}
432
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700433status_t SurfaceTexture::setSynchronousMode(bool enabled) {
434 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700435
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700436 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700437 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700438 return NO_INIT;
439 }
440
Mathias Agopian80727112011-05-02 19:51:12 -0700441 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700442 if (!mAllowSynchronousMode && enabled)
443 return err;
444
Mathias Agopian80727112011-05-02 19:51:12 -0700445 if (!enabled) {
446 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700447 err = drainQueueLocked();
448 if (err != NO_ERROR)
449 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700450 }
451
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700452 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700453 // - if we're going to asynchronous mode, the queue is guaranteed to be
454 // empty here
455 // - if the client set the number of buffers, we're guaranteed that
456 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700457 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700458 mDequeueCondition.signal();
459 }
Mathias Agopian80727112011-05-02 19:51:12 -0700460 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700461}
462
Mathias Agopian97c602c2011-07-19 15:24:46 -0700463status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
464 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700465 ST_LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700466
467 sp<FrameAvailableListener> listener;
468
469 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700470 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700471 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700472 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700473 return NO_INIT;
474 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700475 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700476 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700477 mBufferCount, buf);
478 return -EINVAL;
479 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700480 ST_LOGE("queueBuffer: slot %d is not owned by the client "
481 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700482 return -EINVAL;
483 } else if (buf == mCurrentTexture) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700484 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700485 return -EINVAL;
486 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700487 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700488 "buffer", buf);
489 return -EINVAL;
490 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700491
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700492 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700493 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700494 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700495
496 // Synchronous mode always signals that an additional frame should
497 // be consumed.
498 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700499 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700500 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700501 if (mQueue.empty()) {
502 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700503
504 // Asynchronous mode only signals that a frame should be
505 // consumed if no previous frame was pending. If a frame were
506 // pending then the consumer would have already been notified.
507 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700508 } else {
509 Fifo::iterator front(mQueue.begin());
510 // buffer currently queued is freed
511 mSlots[*front].mBufferState = BufferSlot::FREE;
512 // and we record the new buffer index in the queued list
513 *front = buf;
514 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700515 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700516
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700517 mSlots[buf].mBufferState = BufferSlot::QUEUED;
518 mSlots[buf].mCrop = mNextCrop;
519 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700520 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700521 mSlots[buf].mTimestamp = timestamp;
522 mDequeueCondition.signal();
Mathias Agopian3902be62011-08-17 12:45:40 -0700523
524 *outWidth = mDefaultWidth;
525 *outHeight = mDefaultHeight;
526 *outTransform = 0;
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700527 } // scope for the lock
528
529 // call back without lock held
530 if (listener != 0) {
531 listener->onFrameAvailable();
532 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800533 return OK;
534}
535
536void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700537 ST_LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800538 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700539
540 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700541 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700542 return;
543 }
544
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700545 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700546 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700547 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800548 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700549 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700550 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700551 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800552 return;
553 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700554 mSlots[buf].mBufferState = BufferSlot::FREE;
555 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800556}
557
Jamie Gennisf238e282011-01-09 16:33:17 -0800558status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700559 ST_LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800560 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700561 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700562 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700563 return NO_INIT;
564 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800565 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800566 return OK;
567}
568
569status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700570 ST_LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800571 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700572 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700573 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700574 return NO_INIT;
575 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800576 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800577 return OK;
578}
579
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700580status_t SurfaceTexture::connect(int api,
581 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700582 ST_LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700583 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700584
585 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700586 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700587 return NO_INIT;
588 }
589
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700590 int err = NO_ERROR;
591 switch (api) {
592 case NATIVE_WINDOW_API_EGL:
593 case NATIVE_WINDOW_API_CPU:
594 case NATIVE_WINDOW_API_MEDIA:
595 case NATIVE_WINDOW_API_CAMERA:
596 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700597 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700598 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700599 err = -EINVAL;
600 } else {
601 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700602 *outWidth = mDefaultWidth;
603 *outHeight = mDefaultHeight;
604 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700605 }
606 break;
607 default:
608 err = -EINVAL;
609 break;
610 }
611 return err;
612}
613
614status_t SurfaceTexture::disconnect(int api) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700615 ST_LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700616 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700617
618 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700619 ST_LOGE("disconnect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700620 return NO_INIT;
621 }
622
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700623 int err = NO_ERROR;
624 switch (api) {
625 case NATIVE_WINDOW_API_EGL:
626 case NATIVE_WINDOW_API_CPU:
627 case NATIVE_WINDOW_API_MEDIA:
628 case NATIVE_WINDOW_API_CAMERA:
629 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700630 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700631 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700632 mNextCrop.makeInvalid();
633 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
634 mNextTransform = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700635 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700636 } else {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700637 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700638 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700639 err = -EINVAL;
640 }
641 break;
642 default:
643 err = -EINVAL;
644 break;
645 }
646 return err;
647}
648
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700649status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700650 ST_LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700651
652 switch (mode) {
653 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
654 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
655 break;
656 default:
657 return BAD_VALUE;
658 }
659
660 Mutex::Autolock lock(mMutex);
661 mNextScalingMode = mode;
662 return OK;
663}
664
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800665status_t SurfaceTexture::updateTexImage() {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700666 ST_LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800667 Mutex::Autolock lock(mMutex);
668
Mathias Agopiane47498f2011-08-08 19:35:15 -0700669 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700670 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700671 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700672 }
673
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700674 // In asynchronous mode the list is guaranteed to be one buffer
675 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700676 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700677 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700678 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700679
Jamie Gennisf238e282011-01-09 16:33:17 -0800680 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700681 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800682 if (image == EGL_NO_IMAGE_KHR) {
683 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700684 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700685 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700686 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700687 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700688 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
689 mSlots[buf].mEglImage = image;
690 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700691 if (image == EGL_NO_IMAGE_KHR) {
692 // NOTE: if dpy was invalid, createImage() is guaranteed to
693 // fail. so we'd end up here.
694 return -EINVAL;
695 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800696 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800697
698 GLint error;
699 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700700 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800701 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700702
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700703 glBindTexture(mTexTarget, mTexName);
704 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700705
Jamie Gennis0eb88512011-01-26 11:52:02 -0800706 bool failed = false;
707 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700708 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700709 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800710 failed = true;
711 }
712 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800713 return -EINVAL;
714 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800715
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700716 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700717 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700718 // state. If it has already been given to the client
719 // (synchronous mode), then it stays in DEQUEUED state.
720 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
721 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
722 }
723
Jamie Gennis9a78c902011-01-12 18:30:40 -0800724 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700725 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700726 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700727 mCurrentCrop = mSlots[buf].mCrop;
728 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700729 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700730 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700731 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700732
733 // Now that we've passed the point at which failures can happen,
734 // it's safe to remove the buffer from the front of the queue.
735 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700736 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700737 } else {
738 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700739 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800740 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700741
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800742 return OK;
743}
744
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700745bool SurfaceTexture::isExternalFormat(uint32_t format)
746{
747 switch (format) {
748 // supported YUV formats
749 case HAL_PIXEL_FORMAT_YV12:
750 // Legacy/deprecated YUV formats
751 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
752 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
753 case HAL_PIXEL_FORMAT_YCbCr_422_I:
754 return true;
755 }
756
757 // Any OEM format needs to be considered
758 if (format>=0x100 && format<=0x1FF)
759 return true;
760
761 return false;
762}
763
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700764GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700765 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700766}
767
Jamie Gennisf238e282011-01-09 16:33:17 -0800768void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800769 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700770 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
771}
772
773void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700774 ST_LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800775
Jamie Gennisa214c642011-01-14 13:53:31 -0800776 float xform[16];
777 for (int i = 0; i < 16; i++) {
778 xform[i] = mtxIdentity[i];
779 }
780 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
781 float result[16];
782 mtxMul(result, xform, mtxFlipH);
783 for (int i = 0; i < 16; i++) {
784 xform[i] = result[i];
785 }
786 }
787 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
788 float result[16];
789 mtxMul(result, xform, mtxFlipV);
790 for (int i = 0; i < 16; i++) {
791 xform[i] = result[i];
792 }
793 }
794 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
795 float result[16];
796 mtxMul(result, xform, mtxRot90);
797 for (int i = 0; i < 16; i++) {
798 xform[i] = result[i];
799 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800800 }
801
802 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800803 float tx, ty, sx, sy;
804 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800805 // In order to prevent bilinear sampling at the of the crop rectangle we
806 // may need to shrink it by 2 texels in each direction. Normally this
807 // would just need to take 1/2 a texel off each end, but because the
808 // chroma channels will likely be subsampled we need to chop off a whole
809 // texel. This will cause artifacts if someone does nearest sampling
810 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
811 // accomodate the bilinear and nearest sampling uses.
812 //
813 // If nearest sampling turns out to be a desirable usage of these
814 // textures then we could add the ability to switch a SurfaceTexture to
815 // nearest-mode. Preferably, however, the image producers (video
816 // decoder, camera, etc.) would simply not use a crop rectangle (or at
817 // least not tell the framework about it) so that the GPU can do the
818 // correct edge behavior.
819 int xshrink = 0, yshrink = 0;
820 if (mCurrentCrop.left > 0) {
821 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
822 xshrink++;
823 } else {
824 tx = 0.0f;
825 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700826 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800827 xshrink++;
828 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700829 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800830 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
831 float(buf->getHeight());
832 yshrink++;
833 } else {
834 ty = 0.0f;
835 }
836 if (mCurrentCrop.top > 0) {
837 yshrink++;
838 }
839 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
840 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800841 } else {
842 tx = 0.0f;
843 ty = 0.0f;
844 sx = 1.0f;
845 sy = 1.0f;
846 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800847 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800848 sx, 0, 0, 0,
849 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800850 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800851 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800852 };
853
Jamie Gennisa214c642011-01-14 13:53:31 -0800854 float mtxBeforeFlipV[16];
855 mtxMul(mtxBeforeFlipV, crop, xform);
856
857 // SurfaceFlinger expects the top of its window textures to be at a Y
858 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
859 // want to expose this to applications, however, so we must add an
860 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700861 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800862}
863
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800864nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700865 ST_LOGV("SurfaceTexture::getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800866 Mutex::Autolock lock(mMutex);
867 return mCurrentTimestamp;
868}
869
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800870void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700871 const sp<FrameAvailableListener>& listener) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700872 ST_LOGV("SurfaceTexture::setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800873 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700874 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800875}
876
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700877void SurfaceTexture::freeBufferLocked(int i) {
878 mSlots[i].mGraphicBuffer = 0;
879 mSlots[i].mBufferState = BufferSlot::FREE;
880 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
881 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
882 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
883 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
884 }
885}
886
Mathias Agopianef51b992011-08-10 15:28:58 -0700887void SurfaceTexture::freeAllBuffersLocked() {
888 LOGW_IF(!mQueue.isEmpty(),
889 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian29b57622011-08-17 15:42:04 -0700890 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800891 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700892 freeBufferLocked(i);
893 }
894}
895
896void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
897 LOGW_IF(!mQueue.isEmpty(),
898 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
899 int head = -1;
900 if (!mQueue.empty()) {
901 Fifo::iterator front(mQueue.begin());
902 head = *front;
903 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700904 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700905 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
906 if (i != head) {
907 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800908 }
909 }
910}
911
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700912status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700913 while (mSynchronousMode && !mQueue.isEmpty()) {
914 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700915 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700916 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700917 return NO_INIT;
918 }
919 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700920 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700921 return NO_INIT;
922 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700923 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700924 return NO_ERROR;
925}
926
927status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
928 status_t err = drainQueueLocked();
929 if (err == NO_ERROR) {
930 if (mSynchronousMode) {
931 freeAllBuffersLocked();
932 } else {
933 freeAllBuffersExceptHeadLocked();
934 }
935 }
936 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700937}
938
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800939EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
940 const sp<GraphicBuffer>& graphicBuffer) {
941 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
942 EGLint attrs[] = {
943 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
944 EGL_NONE,
945 };
946 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
947 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700948 if (image == EGL_NO_IMAGE_KHR) {
949 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700950 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800951 }
952 return image;
953}
954
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700955sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
956 Mutex::Autolock lock(mMutex);
957 return mCurrentTextureBuf;
958}
959
960Rect SurfaceTexture::getCurrentCrop() const {
961 Mutex::Autolock lock(mMutex);
962 return mCurrentCrop;
963}
964
965uint32_t SurfaceTexture::getCurrentTransform() const {
966 Mutex::Autolock lock(mMutex);
967 return mCurrentTransform;
968}
969
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700970uint32_t SurfaceTexture::getCurrentScalingMode() const {
971 Mutex::Autolock lock(mMutex);
972 return mCurrentScalingMode;
973}
974
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700975int SurfaceTexture::query(int what, int* outValue)
976{
977 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700978
979 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700980 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700981 return NO_INIT;
982 }
983
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700984 int value;
985 switch (what) {
986 case NATIVE_WINDOW_WIDTH:
987 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700988 break;
989 case NATIVE_WINDOW_HEIGHT:
990 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700991 break;
992 case NATIVE_WINDOW_FORMAT:
993 value = mPixelFormat;
994 break;
995 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
996 value = mSynchronousMode ?
997 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
998 break;
999 default:
1000 return BAD_VALUE;
1001 }
1002 outValue[0] = value;
1003 return NO_ERROR;
1004}
Mathias Agopian7a042bf2011-04-11 21:19:55 -07001005
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001006void SurfaceTexture::abandon() {
1007 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001008 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001009 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -07001010 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001011 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001012 mDequeueCondition.signal();
1013}
1014
Jamie Gennisfa28c352011-09-16 17:30:26 -07001015void SurfaceTexture::setName(const String8& name) {
1016 mName = name;
1017}
1018
Mathias Agopian68c77942011-05-09 19:08:33 -07001019void SurfaceTexture::dump(String8& result) const
1020{
1021 char buffer[1024];
1022 dump(result, "", buffer, 1024);
1023}
1024
1025void SurfaceTexture::dump(String8& result, const char* prefix,
1026 char* buffer, size_t SIZE) const
1027{
1028 Mutex::Autolock _l(mMutex);
1029 snprintf(buffer, SIZE,
1030 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1031 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001032 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1033 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -07001034 result.append(buffer);
1035
1036 String8 fifo;
1037 int fifoSize = 0;
1038 Fifo::const_iterator i(mQueue.begin());
1039 while (i != mQueue.end()) {
1040 snprintf(buffer, SIZE, "%02d ", *i++);
1041 fifoSize++;
1042 fifo.append(buffer);
1043 }
1044
1045 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001046 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001047 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1048 ,
1049 prefix, mCurrentCrop.left,
1050 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001051 mCurrentTransform, mCurrentTexture,
Jamie Gennisfa28c352011-09-16 17:30:26 -07001052 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1053 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -07001054 );
1055 result.append(buffer);
1056
1057 struct {
1058 const char * operator()(int state) const {
1059 switch (state) {
1060 case BufferSlot::DEQUEUED: return "DEQUEUED";
1061 case BufferSlot::QUEUED: return "QUEUED";
1062 case BufferSlot::FREE: return "FREE";
1063 default: return "Unknown";
1064 }
1065 }
1066 } stateName;
1067
1068 for (int i=0 ; i<mBufferCount ; i++) {
1069 const BufferSlot& slot(mSlots[i]);
1070 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001071 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001072 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001073 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001074 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001075 stateName(slot.mBufferState),
Jamie Gennisfa28c352011-09-16 17:30:26 -07001076 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1077 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001078 );
1079 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001080
1081 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1082 if (buf != NULL) {
1083 snprintf(buffer, SIZE,
1084 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001085 buf->handle, buf->width, buf->height, buf->stride,
1086 buf->format);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001087 result.append(buffer);
1088 }
1089 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001090 }
1091}
1092
Jamie Gennisf238e282011-01-09 16:33:17 -08001093static void mtxMul(float out[16], const float a[16], const float b[16]) {
1094 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1095 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1096 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1097 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1098
1099 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1100 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1101 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1102 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1103
1104 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1105 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1106 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1107 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1108
1109 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1110 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1111 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1112 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1113}
1114
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001115}; // namespace android