blob: dac9418190c0e8de229cc2aeab0e30b56f664004 [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
Grace Kloba14a0e582011-06-23 21:21:47 -070097SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070098 mDefaultWidth(1),
99 mDefaultHeight(1),
100 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -0700101 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
102 mClientBufferCount(0),
103 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800104 mCurrentTexture(INVALID_BUFFER_SLOT),
105 mCurrentTransform(0),
106 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800107 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700108 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700109 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -0700110 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700111 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700112 mConnectedApi(NO_CONNECTED_API),
113 mAbandoned(false) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700114 // Choose a name using the PID and a process-unique ID.
115 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
116
117 ST_LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -0800118 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
119 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700120 mNextCrop.makeInvalid();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700121 memcpy(mCurrentTransformMatrix, mtxIdentity,
122 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800123}
124
125SurfaceTexture::~SurfaceTexture() {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700126 ST_LOGV("SurfaceTexture::~SurfaceTexture");
Mathias Agopianef51b992011-08-10 15:28:58 -0700127 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800128}
129
Mathias Agopian80727112011-05-02 19:51:12 -0700130status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
131 if (bufferCount > NUM_BUFFER_SLOTS)
132 return BAD_VALUE;
133
134 // special-case, nothing to do
135 if (bufferCount == mBufferCount)
136 return OK;
137
138 if (!mClientBufferCount &&
139 bufferCount >= mBufferCount) {
140 // easy, we just have more buffers
141 mBufferCount = bufferCount;
142 mServerBufferCount = bufferCount;
143 mDequeueCondition.signal();
144 } else {
145 // we're here because we're either
146 // - reducing the number of available buffers
147 // - or there is a client-buffer-count in effect
148
149 // less than 2 buffers is never allowed
150 if (bufferCount < 2)
151 return BAD_VALUE;
152
153 // when there is non client-buffer-count in effect, the client is not
154 // allowed to dequeue more than one buffer at a time,
155 // so the next time they dequeue a buffer, we know that they don't
156 // own one. the actual resizing will happen during the next
157 // dequeueBuffer.
158
159 mServerBufferCount = bufferCount;
160 }
161 return OK;
162}
163
164status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
165 Mutex::Autolock lock(mMutex);
166 return setBufferCountServerLocked(bufferCount);
167}
168
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800169status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700170 ST_LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700171 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800172
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700173 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700174 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700175 return NO_INIT;
176 }
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700177 if (bufferCount > NUM_BUFFER_SLOTS) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700178 ST_LOGE("setBufferCount: bufferCount larger than slots available");
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700179 return BAD_VALUE;
180 }
181
Mathias Agopian80727112011-05-02 19:51:12 -0700182 // Error out if the user has dequeued buffers
183 for (int i=0 ; i<mBufferCount ; i++) {
184 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700185 ST_LOGE("setBufferCount: client owns some buffers");
Mathias Agopian80727112011-05-02 19:51:12 -0700186 return -EINVAL;
187 }
188 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700189
Jamie Gennis1c121f62011-07-30 16:00:11 -0700190 const int minBufferSlots = mSynchronousMode ?
191 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian80727112011-05-02 19:51:12 -0700192 if (bufferCount == 0) {
Mathias Agopian80727112011-05-02 19:51:12 -0700193 mClientBufferCount = 0;
194 bufferCount = (mServerBufferCount >= minBufferSlots) ?
195 mServerBufferCount : minBufferSlots;
196 return setBufferCountServerLocked(bufferCount);
197 }
198
Jamie Gennis1c121f62011-07-30 16:00:11 -0700199 if (bufferCount < minBufferSlots) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700200 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
Jamie Gennis1c121f62011-07-30 16:00:11 -0700201 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800202 return BAD_VALUE;
203 }
204
Mathias Agopian80727112011-05-02 19:51:12 -0700205 // here we're guaranteed that the client doesn't have dequeued buffers
206 // and will release all of its buffer references.
Mathias Agopianef51b992011-08-10 15:28:58 -0700207 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800208 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700209 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800210 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700211 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700212 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800213 return OK;
214}
215
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700216status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
217{
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700218 if (!w || !h) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700219 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
220 w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700221 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700222 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700223
224 Mutex::Autolock lock(mMutex);
225 mDefaultWidth = w;
226 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700227 return OK;
228}
229
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700230status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700231 ST_LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800232 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700233 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700234 ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700235 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700237 if (slot < 0 || mBufferCount <= slot) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700238 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700239 mBufferCount, slot);
240 return BAD_VALUE;
241 }
242 mSlots[slot].mRequestBufferCalled = true;
243 *buf = mSlots[slot].mGraphicBuffer;
244 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700245}
246
247status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
248 uint32_t format, uint32_t usage) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700249 ST_LOGV("SurfaceTexture::dequeueBuffer");
Mathias Agopianc04f1532011-04-25 20:22:14 -0700250
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700251 if ((w && !h) || (!w && h)) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700252 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700253 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700254 }
255
Mathias Agopianc04f1532011-04-25 20:22:14 -0700256 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700257
258 status_t returnFlags(OK);
259
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700260 int found, foundSync;
261 int dequeuedCount = 0;
262 bool tryAgain = true;
263 while (tryAgain) {
Mathias Agopian2560d142011-08-10 16:33:23 -0700264 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700265 ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
Mathias Agopian2560d142011-08-10 16:33:23 -0700266 return NO_INIT;
267 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700268
Mathias Agopian80727112011-05-02 19:51:12 -0700269 // We need to wait for the FIFO to drain if the number of buffer
270 // needs to change.
271 //
Mathias Agopian2560d142011-08-10 16:33:23 -0700272 // The condition "number of buffers needs to change" is true if
Mathias Agopian80727112011-05-02 19:51:12 -0700273 // - the client doesn't care about how many buffers there are
274 // - AND the actual number of buffer is different from what was
275 // set in the last setBufferCountServer()
276 // - OR -
277 // setBufferCountServer() was set to a value incompatible with
278 // the synchronization mode (for instance because the sync mode
279 // changed since)
280 //
281 // As long as this condition is true AND the FIFO is not empty, we
282 // wait on mDequeueCondition.
283
Mathias Agopian2560d142011-08-10 16:33:23 -0700284 const int minBufferCountNeeded = mSynchronousMode ?
Mathias Agopian80727112011-05-02 19:51:12 -0700285 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
286
Mathias Agopian2560d142011-08-10 16:33:23 -0700287 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
Mathias Agopian80727112011-05-02 19:51:12 -0700288 ((mServerBufferCount != mBufferCount) ||
Mathias Agopian2560d142011-08-10 16:33:23 -0700289 (mServerBufferCount < minBufferCountNeeded));
290
291 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700292 // wait for the FIFO to drain
Mathias Agopian2560d142011-08-10 16:33:23 -0700293 mDequeueCondition.wait(mMutex);
294 // NOTE: we continue here because we need to reevaluate our
295 // whole state (eg: we could be abandoned or disconnected)
296 continue;
Mathias Agopian80727112011-05-02 19:51:12 -0700297 }
298
Mathias Agopian2560d142011-08-10 16:33:23 -0700299 if (numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700300 // here we're guaranteed that mQueue is empty
Mathias Agopianef51b992011-08-10 15:28:58 -0700301 freeAllBuffersLocked();
Mathias Agopian80727112011-05-02 19:51:12 -0700302 mBufferCount = mServerBufferCount;
303 if (mBufferCount < minBufferCountNeeded)
304 mBufferCount = minBufferCountNeeded;
305 mCurrentTexture = INVALID_BUFFER_SLOT;
306 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
307 }
308
309 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700310 found = INVALID_BUFFER_SLOT;
311 foundSync = INVALID_BUFFER_SLOT;
312 dequeuedCount = 0;
313 for (int i = 0; i < mBufferCount; i++) {
314 const int state = mSlots[i].mBufferState;
315 if (state == BufferSlot::DEQUEUED) {
316 dequeuedCount++;
317 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700318
319 // if buffer is FREE it CANNOT be current
320 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
321 "dequeueBuffer: buffer %d is both FREE and current!", i);
322
323 if (ALLOW_DEQUEUE_CURRENT_BUFFER) {
324 if (state == BufferSlot::FREE || i == mCurrentTexture) {
325 foundSync = i;
326 if (i != mCurrentTexture) {
327 found = i;
328 break;
329 }
330 }
331 } else {
332 if (state == BufferSlot::FREE) {
333 foundSync = i;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700334 found = i;
335 break;
336 }
337 }
338 }
Mathias Agopian80727112011-05-02 19:51:12 -0700339
340 // clients are not allowed to dequeue more than one buffer
341 // if they didn't set a buffer count.
342 if (!mClientBufferCount && dequeuedCount) {
343 return -EINVAL;
344 }
345
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700346 // See whether a buffer has been queued since the last setBufferCount so
347 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
348 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
349 if (bufferHasBeenQueued) {
350 // make sure the client is not trying to dequeue more buffers
351 // than allowed.
352 const int avail = mBufferCount - (dequeuedCount+1);
353 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700354 ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
355 "(dequeued=%d)",
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700356 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
357 dequeuedCount);
358 return -EBUSY;
359 }
Mathias Agopian80727112011-05-02 19:51:12 -0700360 }
361
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700362 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian29b57622011-08-17 15:42:04 -0700363 // for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700364 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
365 if (tryAgain) {
366 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700367 }
368 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700369
Mathias Agopian80727112011-05-02 19:51:12 -0700370 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
371 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
372 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700373 }
374
Mathias Agopianc04f1532011-04-25 20:22:14 -0700375 if (found == INVALID_BUFFER_SLOT) {
376 return -EBUSY;
377 }
378
379 const int buf = found;
380 *outBuf = found;
381
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700382 const bool useDefaultSize = !w && !h;
383 if (useDefaultSize) {
384 // use the default size
385 w = mDefaultWidth;
386 h = mDefaultHeight;
387 }
388
389 const bool updateFormat = (format != 0);
390 if (!updateFormat) {
391 // keep the current (or default) format
392 format = mPixelFormat;
393 }
394
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700395 // buffer is now in DEQUEUED (but can also be current at the same time,
396 // if we're in synchronous mode)
397 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
398
399 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700400 if ((buffer == NULL) ||
401 (uint32_t(buffer->width) != w) ||
402 (uint32_t(buffer->height) != h) ||
403 (uint32_t(buffer->format) != format) ||
404 ((uint32_t(buffer->usage) & usage) != usage))
405 {
406 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700407 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700408 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700409 mGraphicBufferAlloc->createGraphicBuffer(
410 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700411 if (graphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700412 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
413 "failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700414 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700415 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700416 if (updateFormat) {
417 mPixelFormat = format;
418 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800419 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700420 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800421 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
422 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
423 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
424 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
425 }
Mathias Agopian80727112011-05-02 19:51:12 -0700426 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700427 }
Mathias Agopian80727112011-05-02 19:51:12 -0700428 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800429}
430
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700431status_t SurfaceTexture::setSynchronousMode(bool enabled) {
432 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700433
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700434 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700435 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700436 return NO_INIT;
437 }
438
Mathias Agopian80727112011-05-02 19:51:12 -0700439 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700440 if (!mAllowSynchronousMode && enabled)
441 return err;
442
Mathias Agopian80727112011-05-02 19:51:12 -0700443 if (!enabled) {
444 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700445 err = drainQueueLocked();
446 if (err != NO_ERROR)
447 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700448 }
449
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700450 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700451 // - if we're going to asynchronous mode, the queue is guaranteed to be
452 // empty here
453 // - if the client set the number of buffers, we're guaranteed that
454 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700455 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700456 mDequeueCondition.signal();
457 }
Mathias Agopian80727112011-05-02 19:51:12 -0700458 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700459}
460
Mathias Agopian97c602c2011-07-19 15:24:46 -0700461status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
462 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700463 ST_LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700464
465 sp<FrameAvailableListener> listener;
466
467 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700468 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700469 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700470 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700471 return NO_INIT;
472 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700473 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700474 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700475 mBufferCount, buf);
476 return -EINVAL;
477 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700478 ST_LOGE("queueBuffer: slot %d is not owned by the client "
479 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700480 return -EINVAL;
481 } else if (buf == mCurrentTexture) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700482 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700483 return -EINVAL;
484 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700485 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700486 "buffer", buf);
487 return -EINVAL;
488 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700489
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700490 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700491 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700492 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700493
494 // Synchronous mode always signals that an additional frame should
495 // be consumed.
496 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700497 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700498 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700499 if (mQueue.empty()) {
500 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700501
502 // Asynchronous mode only signals that a frame should be
503 // consumed if no previous frame was pending. If a frame were
504 // pending then the consumer would have already been notified.
505 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700506 } else {
507 Fifo::iterator front(mQueue.begin());
508 // buffer currently queued is freed
509 mSlots[*front].mBufferState = BufferSlot::FREE;
510 // and we record the new buffer index in the queued list
511 *front = buf;
512 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700513 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700514
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700515 mSlots[buf].mBufferState = BufferSlot::QUEUED;
516 mSlots[buf].mCrop = mNextCrop;
517 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700518 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700519 mSlots[buf].mTimestamp = timestamp;
520 mDequeueCondition.signal();
Mathias Agopian3902be62011-08-17 12:45:40 -0700521
522 *outWidth = mDefaultWidth;
523 *outHeight = mDefaultHeight;
524 *outTransform = 0;
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700525 } // scope for the lock
526
527 // call back without lock held
528 if (listener != 0) {
529 listener->onFrameAvailable();
530 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800531 return OK;
532}
533
534void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700535 ST_LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800536 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700537
538 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700539 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700540 return;
541 }
542
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700543 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700544 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700545 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800546 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700547 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700548 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700549 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800550 return;
551 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700552 mSlots[buf].mBufferState = BufferSlot::FREE;
553 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800554}
555
Jamie Gennisf238e282011-01-09 16:33:17 -0800556status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700557 ST_LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800558 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700559 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700560 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700561 return NO_INIT;
562 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800563 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800564 return OK;
565}
566
567status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700568 ST_LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800569 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700570 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700571 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700572 return NO_INIT;
573 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800574 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800575 return OK;
576}
577
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700578status_t SurfaceTexture::connect(int api,
579 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700580 ST_LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700581 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700582
583 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700584 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700585 return NO_INIT;
586 }
587
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700588 int err = NO_ERROR;
589 switch (api) {
590 case NATIVE_WINDOW_API_EGL:
591 case NATIVE_WINDOW_API_CPU:
592 case NATIVE_WINDOW_API_MEDIA:
593 case NATIVE_WINDOW_API_CAMERA:
594 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700595 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700596 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700597 err = -EINVAL;
598 } else {
599 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700600 *outWidth = mDefaultWidth;
601 *outHeight = mDefaultHeight;
602 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700603 }
604 break;
605 default:
606 err = -EINVAL;
607 break;
608 }
609 return err;
610}
611
612status_t SurfaceTexture::disconnect(int api) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700613 ST_LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700614 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700615
616 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700617 ST_LOGE("disconnect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700618 return NO_INIT;
619 }
620
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700621 int err = NO_ERROR;
622 switch (api) {
623 case NATIVE_WINDOW_API_EGL:
624 case NATIVE_WINDOW_API_CPU:
625 case NATIVE_WINDOW_API_MEDIA:
626 case NATIVE_WINDOW_API_CAMERA:
627 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700628 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700629 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700630 mNextCrop.makeInvalid();
631 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
632 mNextTransform = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700633 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700634 } else {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700635 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700636 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700637 err = -EINVAL;
638 }
639 break;
640 default:
641 err = -EINVAL;
642 break;
643 }
644 return err;
645}
646
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700647status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700648 ST_LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700649
650 switch (mode) {
651 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
652 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
653 break;
654 default:
655 return BAD_VALUE;
656 }
657
658 Mutex::Autolock lock(mMutex);
659 mNextScalingMode = mode;
660 return OK;
661}
662
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800663status_t SurfaceTexture::updateTexImage() {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700664 ST_LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800665 Mutex::Autolock lock(mMutex);
666
Mathias Agopiane47498f2011-08-08 19:35:15 -0700667 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700668 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700669 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700670 }
671
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700672 // In asynchronous mode the list is guaranteed to be one buffer
673 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700674 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700675 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700676 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700677
Jamie Gennisf238e282011-01-09 16:33:17 -0800678 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700679 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800680 if (image == EGL_NO_IMAGE_KHR) {
681 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700682 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700683 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700684 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700685 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700686 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
687 mSlots[buf].mEglImage = image;
688 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700689 if (image == EGL_NO_IMAGE_KHR) {
690 // NOTE: if dpy was invalid, createImage() is guaranteed to
691 // fail. so we'd end up here.
692 return -EINVAL;
693 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800694 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800695
696 GLint error;
697 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700698 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800699 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700700
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700701 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700702 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES,
703 (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700704
Jamie Gennis0eb88512011-01-26 11:52:02 -0800705 bool failed = false;
706 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700707 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700708 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800709 failed = true;
710 }
711 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800712 return -EINVAL;
713 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800714
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700715 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700716 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700717 // state. If it has already been given to the client
718 // (synchronous mode), then it stays in DEQUEUED state.
719 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
720 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
721 }
722
Jamie Gennis9a78c902011-01-12 18:30:40 -0800723 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700724 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700725 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700726 mCurrentCrop = mSlots[buf].mCrop;
727 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700728 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700729 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700730 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700731
732 // Now that we've passed the point at which failures can happen,
733 // it's safe to remove the buffer from the front of the queue.
734 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700735 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700736 } else {
737 // We always bind the texture even if we don't update its contents.
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700738 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800739 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700740
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800741 return OK;
742}
743
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700744bool SurfaceTexture::isExternalFormat(uint32_t format)
745{
746 switch (format) {
747 // supported YUV formats
748 case HAL_PIXEL_FORMAT_YV12:
749 // Legacy/deprecated YUV formats
750 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
751 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
752 case HAL_PIXEL_FORMAT_YCbCr_422_I:
753 return true;
754 }
755
756 // Any OEM format needs to be considered
757 if (format>=0x100 && format<=0x1FF)
758 return true;
759
760 return false;
761}
762
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700763GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700764 return GL_TEXTURE_EXTERNAL_OES;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700765}
766
Jamie Gennisf238e282011-01-09 16:33:17 -0800767void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800768 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700769 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
770}
771
772void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700773 ST_LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800774
Jamie Gennisa214c642011-01-14 13:53:31 -0800775 float xform[16];
776 for (int i = 0; i < 16; i++) {
777 xform[i] = mtxIdentity[i];
778 }
779 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
780 float result[16];
781 mtxMul(result, xform, mtxFlipH);
782 for (int i = 0; i < 16; i++) {
783 xform[i] = result[i];
784 }
785 }
786 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
787 float result[16];
788 mtxMul(result, xform, mtxFlipV);
789 for (int i = 0; i < 16; i++) {
790 xform[i] = result[i];
791 }
792 }
793 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
794 float result[16];
795 mtxMul(result, xform, mtxRot90);
796 for (int i = 0; i < 16; i++) {
797 xform[i] = result[i];
798 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800799 }
800
801 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800802 float tx, ty, sx, sy;
803 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800804 // In order to prevent bilinear sampling at the of the crop rectangle we
805 // may need to shrink it by 2 texels in each direction. Normally this
806 // would just need to take 1/2 a texel off each end, but because the
807 // chroma channels will likely be subsampled we need to chop off a whole
808 // texel. This will cause artifacts if someone does nearest sampling
809 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
810 // accomodate the bilinear and nearest sampling uses.
811 //
812 // If nearest sampling turns out to be a desirable usage of these
813 // textures then we could add the ability to switch a SurfaceTexture to
814 // nearest-mode. Preferably, however, the image producers (video
815 // decoder, camera, etc.) would simply not use a crop rectangle (or at
816 // least not tell the framework about it) so that the GPU can do the
817 // correct edge behavior.
818 int xshrink = 0, yshrink = 0;
819 if (mCurrentCrop.left > 0) {
820 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
821 xshrink++;
822 } else {
823 tx = 0.0f;
824 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700825 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800826 xshrink++;
827 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700828 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800829 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
830 float(buf->getHeight());
831 yshrink++;
832 } else {
833 ty = 0.0f;
834 }
835 if (mCurrentCrop.top > 0) {
836 yshrink++;
837 }
838 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
839 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800840 } else {
841 tx = 0.0f;
842 ty = 0.0f;
843 sx = 1.0f;
844 sy = 1.0f;
845 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800846 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800847 sx, 0, 0, 0,
848 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800849 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800850 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800851 };
852
Jamie Gennisa214c642011-01-14 13:53:31 -0800853 float mtxBeforeFlipV[16];
854 mtxMul(mtxBeforeFlipV, crop, xform);
855
856 // SurfaceFlinger expects the top of its window textures to be at a Y
857 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
858 // want to expose this to applications, however, so we must add an
859 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700860 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800861}
862
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800863nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700864 ST_LOGV("SurfaceTexture::getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800865 Mutex::Autolock lock(mMutex);
866 return mCurrentTimestamp;
867}
868
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800869void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700870 const sp<FrameAvailableListener>& listener) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700871 ST_LOGV("SurfaceTexture::setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800872 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700873 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800874}
875
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700876void SurfaceTexture::freeBufferLocked(int i) {
877 mSlots[i].mGraphicBuffer = 0;
878 mSlots[i].mBufferState = BufferSlot::FREE;
879 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
880 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
881 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
882 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
883 }
884}
885
Mathias Agopianef51b992011-08-10 15:28:58 -0700886void SurfaceTexture::freeAllBuffersLocked() {
887 LOGW_IF(!mQueue.isEmpty(),
888 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian29b57622011-08-17 15:42:04 -0700889 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800890 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700891 freeBufferLocked(i);
892 }
893}
894
895void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
896 LOGW_IF(!mQueue.isEmpty(),
897 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
898 int head = -1;
899 if (!mQueue.empty()) {
900 Fifo::iterator front(mQueue.begin());
901 head = *front;
902 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700903 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700904 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
905 if (i != head) {
906 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800907 }
908 }
909}
910
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700911status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700912 while (mSynchronousMode && !mQueue.isEmpty()) {
913 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700914 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700915 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700916 return NO_INIT;
917 }
918 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700919 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700920 return NO_INIT;
921 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700922 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700923 return NO_ERROR;
924}
925
926status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
927 status_t err = drainQueueLocked();
928 if (err == NO_ERROR) {
929 if (mSynchronousMode) {
930 freeAllBuffersLocked();
931 } else {
932 freeAllBuffersExceptHeadLocked();
933 }
934 }
935 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700936}
937
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800938EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
939 const sp<GraphicBuffer>& graphicBuffer) {
940 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
941 EGLint attrs[] = {
942 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
943 EGL_NONE,
944 };
945 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
946 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700947 if (image == EGL_NO_IMAGE_KHR) {
948 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700949 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800950 }
951 return image;
952}
953
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700954sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
955 Mutex::Autolock lock(mMutex);
956 return mCurrentTextureBuf;
957}
958
959Rect SurfaceTexture::getCurrentCrop() const {
960 Mutex::Autolock lock(mMutex);
961 return mCurrentCrop;
962}
963
964uint32_t SurfaceTexture::getCurrentTransform() const {
965 Mutex::Autolock lock(mMutex);
966 return mCurrentTransform;
967}
968
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700969uint32_t SurfaceTexture::getCurrentScalingMode() const {
970 Mutex::Autolock lock(mMutex);
971 return mCurrentScalingMode;
972}
973
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700974int SurfaceTexture::query(int what, int* outValue)
975{
976 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700977
978 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700979 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700980 return NO_INIT;
981 }
982
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700983 int value;
984 switch (what) {
985 case NATIVE_WINDOW_WIDTH:
986 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700987 break;
988 case NATIVE_WINDOW_HEIGHT:
989 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700990 break;
991 case NATIVE_WINDOW_FORMAT:
992 value = mPixelFormat;
993 break;
994 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
995 value = mSynchronousMode ?
996 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
997 break;
998 default:
999 return BAD_VALUE;
1000 }
1001 outValue[0] = value;
1002 return NO_ERROR;
1003}
Mathias Agopian7a042bf2011-04-11 21:19:55 -07001004
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001005void SurfaceTexture::abandon() {
1006 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001007 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001008 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -07001009 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001010 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001011 mDequeueCondition.signal();
1012}
1013
Jamie Gennisfa28c352011-09-16 17:30:26 -07001014void SurfaceTexture::setName(const String8& name) {
1015 mName = name;
1016}
1017
Mathias Agopian68c77942011-05-09 19:08:33 -07001018void SurfaceTexture::dump(String8& result) const
1019{
1020 char buffer[1024];
1021 dump(result, "", buffer, 1024);
1022}
1023
1024void SurfaceTexture::dump(String8& result, const char* prefix,
1025 char* buffer, size_t SIZE) const
1026{
1027 Mutex::Autolock _l(mMutex);
1028 snprintf(buffer, SIZE,
1029 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1030 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001031 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1032 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -07001033 result.append(buffer);
1034
1035 String8 fifo;
1036 int fifoSize = 0;
1037 Fifo::const_iterator i(mQueue.begin());
1038 while (i != mQueue.end()) {
1039 snprintf(buffer, SIZE, "%02d ", *i++);
1040 fifoSize++;
1041 fifo.append(buffer);
1042 }
1043
1044 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001045 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001046 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1047 ,
1048 prefix, mCurrentCrop.left,
1049 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001050 mCurrentTransform, mCurrentTexture,
Jamie Gennisfa28c352011-09-16 17:30:26 -07001051 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1052 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -07001053 );
1054 result.append(buffer);
1055
1056 struct {
1057 const char * operator()(int state) const {
1058 switch (state) {
1059 case BufferSlot::DEQUEUED: return "DEQUEUED";
1060 case BufferSlot::QUEUED: return "QUEUED";
1061 case BufferSlot::FREE: return "FREE";
1062 default: return "Unknown";
1063 }
1064 }
1065 } stateName;
1066
1067 for (int i=0 ; i<mBufferCount ; i++) {
1068 const BufferSlot& slot(mSlots[i]);
1069 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001070 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001071 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001072 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001073 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001074 stateName(slot.mBufferState),
Jamie Gennisfa28c352011-09-16 17:30:26 -07001075 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1076 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001077 );
1078 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001079
1080 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1081 if (buf != NULL) {
1082 snprintf(buffer, SIZE,
1083 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001084 buf->handle, buf->width, buf->height, buf->stride,
1085 buf->format);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001086 result.append(buffer);
1087 }
1088 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001089 }
1090}
1091
Jamie Gennisf238e282011-01-09 16:33:17 -08001092static void mtxMul(float out[16], const float a[16], const float b[16]) {
1093 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1094 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1095 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1096 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1097
1098 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1099 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1100 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1101 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1102
1103 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1104 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1105 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1106 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1107
1108 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1109 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1110 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1111 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1112}
1113
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001114}; // namespace android