blob: c190195b53d0a0391f20679eb1264aff3580fed9 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture"
Jamie Gennise70d8b42011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#define GL_GLEXT_PROTOTYPES
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <gui/SurfaceTexture.h>
29
Mathias Agopian7a042bf2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070037#include <utils/String8.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038
39namespace android {
40
Jamie Gennisf238e282011-01-09 16:33:17 -080041// Transform matrices
42static float mtxIdentity[16] = {
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1,
47};
48static float mtxFlipH[16] = {
49 -1, 0, 0, 0,
50 0, 1, 0, 0,
51 0, 0, 1, 0,
52 1, 0, 0, 1,
53};
54static float mtxFlipV[16] = {
55 1, 0, 0, 0,
56 0, -1, 0, 0,
57 0, 0, 1, 0,
58 0, 1, 0, 1,
59};
60static float mtxRot90[16] = {
61 0, 1, 0, 0,
62 -1, 0, 0, 0,
63 0, 0, 1, 0,
64 1, 0, 0, 1,
65};
66static float mtxRot180[16] = {
67 -1, 0, 0, 0,
68 0, -1, 0, 0,
69 0, 0, 1, 0,
70 1, 1, 0, 1,
71};
72static float mtxRot270[16] = {
73 0, -1, 0, 0,
74 1, 0, 0, 0,
75 0, 0, 1, 0,
76 0, 1, 0, 1,
77};
78
79static void mtxMul(float out[16], const float a[16], const float b[16]);
80
Grace Kloba14a0e582011-06-23 21:21:47 -070081SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070082 mDefaultWidth(1),
83 mDefaultHeight(1),
84 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -070085 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
86 mClientBufferCount(0),
87 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080088 mCurrentTexture(INVALID_BUFFER_SLOT),
Mathias Agopian7a042bf2011-04-11 21:19:55 -070089 mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080090 mCurrentTransform(0),
91 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080092 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -070093 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070094 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -070095 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070096 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -070097 mConnectedApi(NO_CONNECTED_API),
98 mAbandoned(false) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080099 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -0800100 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
101 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700102 mNextCrop.makeInvalid();
Jamie Gennis736aa952011-06-12 17:03:06 -0700103 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800104}
105
106SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800107 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800108 freeAllBuffers();
109}
110
Mathias Agopian80727112011-05-02 19:51:12 -0700111status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
112 if (bufferCount > NUM_BUFFER_SLOTS)
113 return BAD_VALUE;
114
115 // special-case, nothing to do
116 if (bufferCount == mBufferCount)
117 return OK;
118
119 if (!mClientBufferCount &&
120 bufferCount >= mBufferCount) {
121 // easy, we just have more buffers
122 mBufferCount = bufferCount;
123 mServerBufferCount = bufferCount;
124 mDequeueCondition.signal();
125 } else {
126 // we're here because we're either
127 // - reducing the number of available buffers
128 // - or there is a client-buffer-count in effect
129
130 // less than 2 buffers is never allowed
131 if (bufferCount < 2)
132 return BAD_VALUE;
133
134 // when there is non client-buffer-count in effect, the client is not
135 // allowed to dequeue more than one buffer at a time,
136 // so the next time they dequeue a buffer, we know that they don't
137 // own one. the actual resizing will happen during the next
138 // dequeueBuffer.
139
140 mServerBufferCount = bufferCount;
141 }
142 return OK;
143}
144
145status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
146 Mutex::Autolock lock(mMutex);
147 return setBufferCountServerLocked(bufferCount);
148}
149
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800150status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800151 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700152 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800153
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700154 if (mAbandoned) {
155 LOGE("setBufferCount: SurfaceTexture has been abandoned!");
156 return NO_INIT;
157 }
158
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700159 if (bufferCount > NUM_BUFFER_SLOTS) {
160 LOGE("setBufferCount: bufferCount larger than slots available");
161 return BAD_VALUE;
162 }
163
Mathias Agopian80727112011-05-02 19:51:12 -0700164 // Error out if the user has dequeued buffers
165 for (int i=0 ; i<mBufferCount ; i++) {
166 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
167 LOGE("setBufferCount: client owns some buffers");
168 return -EINVAL;
169 }
170 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700171
Mathias Agopian80727112011-05-02 19:51:12 -0700172 if (bufferCount == 0) {
173 const int minBufferSlots = mSynchronousMode ?
174 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
175 mClientBufferCount = 0;
176 bufferCount = (mServerBufferCount >= minBufferSlots) ?
177 mServerBufferCount : minBufferSlots;
178 return setBufferCountServerLocked(bufferCount);
179 }
180
181 // We don't allow the client to set a buffer-count less than
182 // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it.
183 if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) {
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800184 return BAD_VALUE;
185 }
186
Mathias Agopian80727112011-05-02 19:51:12 -0700187 // here we're guaranteed that the client doesn't have dequeued buffers
188 // and will release all of its buffer references.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189 freeAllBuffers();
190 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700191 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800192 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700193 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700194 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 return OK;
196}
197
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700198status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
199{
200 Mutex::Autolock lock(mMutex);
201 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
202 mDefaultWidth = w;
203 mDefaultHeight = h;
204 }
205 return OK;
206}
207
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700208status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800209 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800210 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700211 if (mAbandoned) {
212 LOGE("requestBuffer: SurfaceTexture has been abandoned!");
213 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800214 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700215 if (slot < 0 || mBufferCount <= slot) {
216 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
217 mBufferCount, slot);
218 return BAD_VALUE;
219 }
220 mSlots[slot].mRequestBufferCalled = true;
221 *buf = mSlots[slot].mGraphicBuffer;
222 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700223}
224
225status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
226 uint32_t format, uint32_t usage) {
227 LOGV("SurfaceTexture::dequeueBuffer");
228
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700229 if (mAbandoned) {
230 LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
231 return NO_INIT;
232 }
233
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700234 if ((w && !h) || (!w && h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700235 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
236 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700237 }
238
Mathias Agopianc04f1532011-04-25 20:22:14 -0700239 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700240
241 status_t returnFlags(OK);
242
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700243 int found, foundSync;
244 int dequeuedCount = 0;
245 bool tryAgain = true;
246 while (tryAgain) {
Mathias Agopian80727112011-05-02 19:51:12 -0700247 // We need to wait for the FIFO to drain if the number of buffer
248 // needs to change.
249 //
250 // The condition "number of buffer needs to change" is true if
251 // - the client doesn't care about how many buffers there are
252 // - AND the actual number of buffer is different from what was
253 // set in the last setBufferCountServer()
254 // - OR -
255 // setBufferCountServer() was set to a value incompatible with
256 // the synchronization mode (for instance because the sync mode
257 // changed since)
258 //
259 // As long as this condition is true AND the FIFO is not empty, we
260 // wait on mDequeueCondition.
261
262 int minBufferCountNeeded = mSynchronousMode ?
263 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
264
265 if (!mClientBufferCount &&
266 ((mServerBufferCount != mBufferCount) ||
267 (mServerBufferCount < minBufferCountNeeded))) {
268 // wait for the FIFO to drain
269 while (!mQueue.isEmpty()) {
270 mDequeueCondition.wait(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700271 if (mAbandoned) {
272 LOGE("dequeueBuffer: SurfaceTexture was abandoned while "
273 "blocked!");
274 return NO_INIT;
275 }
Mathias Agopian80727112011-05-02 19:51:12 -0700276 }
277 minBufferCountNeeded = mSynchronousMode ?
278 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
279 }
280
281
282 if (!mClientBufferCount &&
283 ((mServerBufferCount != mBufferCount) ||
284 (mServerBufferCount < minBufferCountNeeded))) {
285 // here we're guaranteed that mQueue is empty
286 freeAllBuffers();
287 mBufferCount = mServerBufferCount;
288 if (mBufferCount < minBufferCountNeeded)
289 mBufferCount = minBufferCountNeeded;
290 mCurrentTexture = INVALID_BUFFER_SLOT;
291 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
292 }
293
294 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700295 found = INVALID_BUFFER_SLOT;
296 foundSync = INVALID_BUFFER_SLOT;
297 dequeuedCount = 0;
298 for (int i = 0; i < mBufferCount; i++) {
299 const int state = mSlots[i].mBufferState;
300 if (state == BufferSlot::DEQUEUED) {
301 dequeuedCount++;
302 }
Mathias Agopiane1220792011-05-04 18:28:07 -0700303 if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700304 foundSync = i;
305 if (i != mCurrentTexture) {
306 found = i;
307 break;
308 }
309 }
310 }
Mathias Agopian80727112011-05-02 19:51:12 -0700311
312 // clients are not allowed to dequeue more than one buffer
313 // if they didn't set a buffer count.
314 if (!mClientBufferCount && dequeuedCount) {
315 return -EINVAL;
316 }
317
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700318 // See whether a buffer has been queued since the last setBufferCount so
319 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
320 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
321 if (bufferHasBeenQueued) {
322 // make sure the client is not trying to dequeue more buffers
323 // than allowed.
324 const int avail = mBufferCount - (dequeuedCount+1);
325 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
326 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
327 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
328 dequeuedCount);
329 return -EBUSY;
330 }
Mathias Agopian80727112011-05-02 19:51:12 -0700331 }
332
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700333 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700334 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700335 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
336 if (tryAgain) {
337 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700338 }
339 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700340
Mathias Agopian80727112011-05-02 19:51:12 -0700341 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
342 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
343 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700344 }
345
Mathias Agopianc04f1532011-04-25 20:22:14 -0700346 if (found == INVALID_BUFFER_SLOT) {
347 return -EBUSY;
348 }
349
350 const int buf = found;
351 *outBuf = found;
352
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700353 const bool useDefaultSize = !w && !h;
354 if (useDefaultSize) {
355 // use the default size
356 w = mDefaultWidth;
357 h = mDefaultHeight;
358 }
359
360 const bool updateFormat = (format != 0);
361 if (!updateFormat) {
362 // keep the current (or default) format
363 format = mPixelFormat;
364 }
365
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700366 // buffer is now in DEQUEUED (but can also be current at the same time,
367 // if we're in synchronous mode)
368 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
369
370 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700371 if ((buffer == NULL) ||
372 (uint32_t(buffer->width) != w) ||
373 (uint32_t(buffer->height) != h) ||
374 (uint32_t(buffer->format) != format) ||
375 ((uint32_t(buffer->usage) & usage) != usage))
376 {
377 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700378 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700379 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700380 mGraphicBufferAlloc->createGraphicBuffer(
381 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700382 if (graphicBuffer == 0) {
383 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700384 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700385 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700386 if (updateFormat) {
387 mPixelFormat = format;
388 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800389 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700390 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800391 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
392 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
393 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
394 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
395 }
Mathias Agopian80727112011-05-02 19:51:12 -0700396 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700397 }
Mathias Agopian80727112011-05-02 19:51:12 -0700398 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800399}
400
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700401status_t SurfaceTexture::setSynchronousMode(bool enabled) {
402 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700403
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700404 if (mAbandoned) {
405 LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
406 return NO_INIT;
407 }
408
Mathias Agopian80727112011-05-02 19:51:12 -0700409 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700410 if (!mAllowSynchronousMode && enabled)
411 return err;
412
Mathias Agopian80727112011-05-02 19:51:12 -0700413 if (!enabled) {
414 // going to asynchronous mode, drain the queue
415 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
416 mDequeueCondition.wait(mMutex);
417 }
418 }
419
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700420 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700421 // - if we're going to asynchronous mode, the queue is guaranteed to be
422 // empty here
423 // - if the client set the number of buffers, we're guaranteed that
424 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700425 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700426 mDequeueCondition.signal();
427 }
Mathias Agopian80727112011-05-02 19:51:12 -0700428 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700429}
430
Mathias Agopian97c602c2011-07-19 15:24:46 -0700431status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
432 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800433 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700434
435 sp<FrameAvailableListener> listener;
436
437 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700438 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700439 if (mAbandoned) {
440 LOGE("queueBuffer: SurfaceTexture has been abandoned!");
441 return NO_INIT;
442 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700443 if (buf < 0 || buf >= mBufferCount) {
444 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
445 mBufferCount, buf);
446 return -EINVAL;
447 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
448 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
449 buf, mSlots[buf].mBufferState);
450 return -EINVAL;
451 } else if (buf == mCurrentTexture) {
452 LOGE("queueBuffer: slot %d is current!", buf);
453 return -EINVAL;
454 } else if (!mSlots[buf].mRequestBufferCalled) {
455 LOGE("queueBuffer: slot %d was enqueued without requesting a "
456 "buffer", buf);
457 return -EINVAL;
458 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700459
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700460 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700461 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700462 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700463
464 // Synchronous mode always signals that an additional frame should
465 // be consumed.
466 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700467 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700468 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700469 if (mQueue.empty()) {
470 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700471
472 // Asynchronous mode only signals that a frame should be
473 // consumed if no previous frame was pending. If a frame were
474 // pending then the consumer would have already been notified.
475 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700476 } else {
477 Fifo::iterator front(mQueue.begin());
478 // buffer currently queued is freed
479 mSlots[*front].mBufferState = BufferSlot::FREE;
480 // and we record the new buffer index in the queued list
481 *front = buf;
482 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700483 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700484
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700485 mSlots[buf].mBufferState = BufferSlot::QUEUED;
486 mSlots[buf].mCrop = mNextCrop;
487 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700488 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700489 mSlots[buf].mTimestamp = timestamp;
490 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700491 } // scope for the lock
492
493 // call back without lock held
494 if (listener != 0) {
495 listener->onFrameAvailable();
496 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700497
498 *outWidth = mDefaultWidth;
499 *outHeight = mDefaultHeight;
500 *outTransform = 0;
501
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800502 return OK;
503}
504
505void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800506 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800507 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700508
509 if (mAbandoned) {
510 LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
511 return;
512 }
513
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700514 if (buf < 0 || buf >= mBufferCount) {
515 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
516 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800517 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700518 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
519 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
520 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800521 return;
522 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700523 mSlots[buf].mBufferState = BufferSlot::FREE;
524 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800525}
526
Jamie Gennisf238e282011-01-09 16:33:17 -0800527status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800528 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800529 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700530 if (mAbandoned) {
531 LOGE("setCrop: SurfaceTexture has been abandoned!");
532 return NO_INIT;
533 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800534 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800535 return OK;
536}
537
538status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800539 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800540 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700541 if (mAbandoned) {
542 LOGE("setTransform: SurfaceTexture has been abandoned!");
543 return NO_INIT;
544 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800545 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800546 return OK;
547}
548
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700549status_t SurfaceTexture::connect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700550 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700551 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700552
553 if (mAbandoned) {
554 LOGE("connect: SurfaceTexture has been abandoned!");
555 return NO_INIT;
556 }
557
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700558 int err = NO_ERROR;
559 switch (api) {
560 case NATIVE_WINDOW_API_EGL:
561 case NATIVE_WINDOW_API_CPU:
562 case NATIVE_WINDOW_API_MEDIA:
563 case NATIVE_WINDOW_API_CAMERA:
564 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700565 LOGE("connect: already connected (cur=%d, req=%d)",
566 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700567 err = -EINVAL;
568 } else {
569 mConnectedApi = api;
570 }
571 break;
572 default:
573 err = -EINVAL;
574 break;
575 }
576 return err;
577}
578
579status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700580 LOGV("SurfaceTexture::disconnect(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) {
584 LOGE("connect: SurfaceTexture has been abandoned!");
585 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 == api) {
595 mConnectedApi = NO_CONNECTED_API;
596 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700597 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
598 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700599 err = -EINVAL;
600 }
601 break;
602 default:
603 err = -EINVAL;
604 break;
605 }
606 return err;
607}
608
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700609status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700610 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700611
612 switch (mode) {
613 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
614 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
615 break;
616 default:
617 return BAD_VALUE;
618 }
619
620 Mutex::Autolock lock(mMutex);
621 mNextScalingMode = mode;
622 return OK;
623}
624
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800625status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800626 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800627 Mutex::Autolock lock(mMutex);
628
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700629 // In asynchronous mode the list is guaranteed to be one buffer
630 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700631 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700632 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700633 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700634
Jamie Gennisf238e282011-01-09 16:33:17 -0800635 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700636 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800637 if (image == EGL_NO_IMAGE_KHR) {
638 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700639 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
640 mSlots[buf].mEglImage = image;
641 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700642 if (image == EGL_NO_IMAGE_KHR) {
643 // NOTE: if dpy was invalid, createImage() is guaranteed to
644 // fail. so we'd end up here.
645 return -EINVAL;
646 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800647 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800648
649 GLint error;
650 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700651 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800652 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700653
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700654 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700655 if (target != mCurrentTextureTarget) {
656 glDeleteTextures(1, &mTexName);
657 }
658 glBindTexture(target, mTexName);
659 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
660
Jamie Gennis0eb88512011-01-26 11:52:02 -0800661 bool failed = false;
662 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800663 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700664 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800665 failed = true;
666 }
667 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800668 return -EINVAL;
669 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800670
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700671 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700672 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700673 // state. If it has already been given to the client
674 // (synchronous mode), then it stays in DEQUEUED state.
675 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
676 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
677 }
678
Jamie Gennis9a78c902011-01-12 18:30:40 -0800679 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700680 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700681 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700682 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700683 mCurrentCrop = mSlots[buf].mCrop;
684 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700685 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700686 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700687 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700688
689 // Now that we've passed the point at which failures can happen,
690 // it's safe to remove the buffer from the front of the queue.
691 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700692 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700693 } else {
694 // We always bind the texture even if we don't update its contents.
695 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800696 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700697
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800698 return OK;
699}
700
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700701bool SurfaceTexture::isExternalFormat(uint32_t format)
702{
703 switch (format) {
704 // supported YUV formats
705 case HAL_PIXEL_FORMAT_YV12:
706 // Legacy/deprecated YUV formats
707 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
708 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
709 case HAL_PIXEL_FORMAT_YCbCr_422_I:
710 return true;
711 }
712
713 // Any OEM format needs to be considered
714 if (format>=0x100 && format<=0x1FF)
715 return true;
716
717 return false;
718}
719
720GLenum SurfaceTexture::getTextureTarget(uint32_t format)
721{
722 GLenum target = GL_TEXTURE_2D;
723#if defined(GL_OES_EGL_image_external)
724 if (isExternalFormat(format)) {
725 target = GL_TEXTURE_EXTERNAL_OES;
726 }
727#endif
728 return target;
729}
730
731GLenum SurfaceTexture::getCurrentTextureTarget() const {
732 Mutex::Autolock lock(mMutex);
733 return mCurrentTextureTarget;
734}
735
Jamie Gennisf238e282011-01-09 16:33:17 -0800736void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800737 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700738 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
739}
740
741void SurfaceTexture::computeCurrentTransformMatrix() {
742 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800743
Jamie Gennisa214c642011-01-14 13:53:31 -0800744 float xform[16];
745 for (int i = 0; i < 16; i++) {
746 xform[i] = mtxIdentity[i];
747 }
748 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
749 float result[16];
750 mtxMul(result, xform, mtxFlipH);
751 for (int i = 0; i < 16; i++) {
752 xform[i] = result[i];
753 }
754 }
755 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
756 float result[16];
757 mtxMul(result, xform, mtxFlipV);
758 for (int i = 0; i < 16; i++) {
759 xform[i] = result[i];
760 }
761 }
762 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
763 float result[16];
764 mtxMul(result, xform, mtxRot90);
765 for (int i = 0; i < 16; i++) {
766 xform[i] = result[i];
767 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800768 }
769
770 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800771 float tx, ty, sx, sy;
772 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800773 // In order to prevent bilinear sampling at the of the crop rectangle we
774 // may need to shrink it by 2 texels in each direction. Normally this
775 // would just need to take 1/2 a texel off each end, but because the
776 // chroma channels will likely be subsampled we need to chop off a whole
777 // texel. This will cause artifacts if someone does nearest sampling
778 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
779 // accomodate the bilinear and nearest sampling uses.
780 //
781 // If nearest sampling turns out to be a desirable usage of these
782 // textures then we could add the ability to switch a SurfaceTexture to
783 // nearest-mode. Preferably, however, the image producers (video
784 // decoder, camera, etc.) would simply not use a crop rectangle (or at
785 // least not tell the framework about it) so that the GPU can do the
786 // correct edge behavior.
787 int xshrink = 0, yshrink = 0;
788 if (mCurrentCrop.left > 0) {
789 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
790 xshrink++;
791 } else {
792 tx = 0.0f;
793 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700794 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800795 xshrink++;
796 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700797 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800798 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
799 float(buf->getHeight());
800 yshrink++;
801 } else {
802 ty = 0.0f;
803 }
804 if (mCurrentCrop.top > 0) {
805 yshrink++;
806 }
807 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
808 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800809 } else {
810 tx = 0.0f;
811 ty = 0.0f;
812 sx = 1.0f;
813 sy = 1.0f;
814 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800815 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800816 sx, 0, 0, 0,
817 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800818 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800819 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800820 };
821
Jamie Gennisa214c642011-01-14 13:53:31 -0800822 float mtxBeforeFlipV[16];
823 mtxMul(mtxBeforeFlipV, crop, xform);
824
825 // SurfaceFlinger expects the top of its window textures to be at a Y
826 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
827 // want to expose this to applications, however, so we must add an
828 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700829 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800830}
831
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800832nsecs_t SurfaceTexture::getTimestamp() {
833 LOGV("SurfaceTexture::getTimestamp");
834 Mutex::Autolock lock(mMutex);
835 return mCurrentTimestamp;
836}
837
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800838void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700839 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800840 LOGV("SurfaceTexture::setFrameAvailableListener");
841 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700842 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800843}
844
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800845void SurfaceTexture::freeAllBuffers() {
846 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
847 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700848 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800849 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
850 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
851 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
852 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
853 }
854 }
855}
856
857EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
858 const sp<GraphicBuffer>& graphicBuffer) {
859 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
860 EGLint attrs[] = {
861 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
862 EGL_NONE,
863 };
864 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
865 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700866 if (image == EGL_NO_IMAGE_KHR) {
867 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800868 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800869 }
870 return image;
871}
872
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700873sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
874 Mutex::Autolock lock(mMutex);
875 return mCurrentTextureBuf;
876}
877
878Rect SurfaceTexture::getCurrentCrop() const {
879 Mutex::Autolock lock(mMutex);
880 return mCurrentCrop;
881}
882
883uint32_t SurfaceTexture::getCurrentTransform() const {
884 Mutex::Autolock lock(mMutex);
885 return mCurrentTransform;
886}
887
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700888uint32_t SurfaceTexture::getCurrentScalingMode() const {
889 Mutex::Autolock lock(mMutex);
890 return mCurrentScalingMode;
891}
892
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700893int SurfaceTexture::query(int what, int* outValue)
894{
895 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700896
897 if (mAbandoned) {
898 LOGE("query: SurfaceTexture has been abandoned!");
899 return NO_INIT;
900 }
901
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700902 int value;
903 switch (what) {
904 case NATIVE_WINDOW_WIDTH:
905 value = mDefaultWidth;
906 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
907 value = mCurrentTextureBuf->width;
908 break;
909 case NATIVE_WINDOW_HEIGHT:
910 value = mDefaultHeight;
911 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
912 value = mCurrentTextureBuf->height;
913 break;
914 case NATIVE_WINDOW_FORMAT:
915 value = mPixelFormat;
916 break;
917 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
918 value = mSynchronousMode ?
919 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
920 break;
921 default:
922 return BAD_VALUE;
923 }
924 outValue[0] = value;
925 return NO_ERROR;
926}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700927
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700928void SurfaceTexture::abandon() {
929 Mutex::Autolock lock(mMutex);
930 freeAllBuffers();
931 mAbandoned = true;
932 mDequeueCondition.signal();
933}
934
Mathias Agopian68c77942011-05-09 19:08:33 -0700935void SurfaceTexture::dump(String8& result) const
936{
937 char buffer[1024];
938 dump(result, "", buffer, 1024);
939}
940
941void SurfaceTexture::dump(String8& result, const char* prefix,
942 char* buffer, size_t SIZE) const
943{
944 Mutex::Autolock _l(mMutex);
945 snprintf(buffer, SIZE,
946 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
947 "mPixelFormat=%d, mTexName=%d\n",
948 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
949 mPixelFormat, mTexName);
950 result.append(buffer);
951
952 String8 fifo;
953 int fifoSize = 0;
954 Fifo::const_iterator i(mQueue.begin());
955 while (i != mQueue.end()) {
956 snprintf(buffer, SIZE, "%02d ", *i++);
957 fifoSize++;
958 fifo.append(buffer);
959 }
960
961 snprintf(buffer, SIZE,
962 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
963 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
964 ,
965 prefix, mCurrentCrop.left,
966 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
967 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
968 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
969 mCurrentTransform, fifoSize, fifo.string()
970 );
971 result.append(buffer);
972
973 struct {
974 const char * operator()(int state) const {
975 switch (state) {
976 case BufferSlot::DEQUEUED: return "DEQUEUED";
977 case BufferSlot::QUEUED: return "QUEUED";
978 case BufferSlot::FREE: return "FREE";
979 default: return "Unknown";
980 }
981 }
982 } stateName;
983
984 for (int i=0 ; i<mBufferCount ; i++) {
985 const BufferSlot& slot(mSlots[i]);
986 snprintf(buffer, SIZE,
987 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700988 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700989 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700990 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
991 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700992 );
993 result.append(buffer);
994 }
995}
996
Jamie Gennisf238e282011-01-09 16:33:17 -0800997static void mtxMul(float out[16], const float a[16], const float b[16]) {
998 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
999 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1000 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1001 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1002
1003 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1004 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1005 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1006 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1007
1008 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1009 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1010 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1011 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1012
1013 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1014 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1015 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1016 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1017}
1018
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001019}; // namespace android