blob: f04f5c29655f03fcf507a07173ed0c965e451006 [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),
89 mCurrentTransform(0),
90 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080091 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -070092 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -070093 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -070094 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -070095 mAllowSynchronousMode(allowSynchronousMode),
96 mConnectedApi(NO_CONNECTED_API) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080097 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -080098 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
99 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700100 mNextCrop.makeInvalid();
Jamie Gennis736aa952011-06-12 17:03:06 -0700101 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800102}
103
104SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800105 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800106 freeAllBuffers();
107}
108
Mathias Agopian80727112011-05-02 19:51:12 -0700109status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
110 if (bufferCount > NUM_BUFFER_SLOTS)
111 return BAD_VALUE;
112
113 // special-case, nothing to do
114 if (bufferCount == mBufferCount)
115 return OK;
116
117 if (!mClientBufferCount &&
118 bufferCount >= mBufferCount) {
119 // easy, we just have more buffers
120 mBufferCount = bufferCount;
121 mServerBufferCount = bufferCount;
122 mDequeueCondition.signal();
123 } else {
124 // we're here because we're either
125 // - reducing the number of available buffers
126 // - or there is a client-buffer-count in effect
127
128 // less than 2 buffers is never allowed
129 if (bufferCount < 2)
130 return BAD_VALUE;
131
132 // when there is non client-buffer-count in effect, the client is not
133 // allowed to dequeue more than one buffer at a time,
134 // so the next time they dequeue a buffer, we know that they don't
135 // own one. the actual resizing will happen during the next
136 // dequeueBuffer.
137
138 mServerBufferCount = bufferCount;
139 }
140 return OK;
141}
142
143status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
144 Mutex::Autolock lock(mMutex);
145 return setBufferCountServerLocked(bufferCount);
146}
147
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800148status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800149 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700150 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800151
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700152 if (bufferCount > NUM_BUFFER_SLOTS) {
153 LOGE("setBufferCount: bufferCount larger than slots available");
154 return BAD_VALUE;
155 }
156
Mathias Agopian80727112011-05-02 19:51:12 -0700157 // Error out if the user has dequeued buffers
158 for (int i=0 ; i<mBufferCount ; i++) {
159 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
160 LOGE("setBufferCount: client owns some buffers");
161 return -EINVAL;
162 }
163 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700164
Mathias Agopian80727112011-05-02 19:51:12 -0700165 if (bufferCount == 0) {
166 const int minBufferSlots = mSynchronousMode ?
167 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
168 mClientBufferCount = 0;
169 bufferCount = (mServerBufferCount >= minBufferSlots) ?
170 mServerBufferCount : minBufferSlots;
171 return setBufferCountServerLocked(bufferCount);
172 }
173
174 // We don't allow the client to set a buffer-count less than
175 // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it.
176 if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) {
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800177 return BAD_VALUE;
178 }
179
Mathias Agopian80727112011-05-02 19:51:12 -0700180 // here we're guaranteed that the client doesn't have dequeued buffers
181 // and will release all of its buffer references.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800182 freeAllBuffers();
183 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700184 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800185 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700186 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700187 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800188 return OK;
189}
190
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700191status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
192{
193 Mutex::Autolock lock(mMutex);
194 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
195 mDefaultWidth = w;
196 mDefaultHeight = h;
197 }
198 return OK;
199}
200
Mathias Agopianc04f1532011-04-25 20:22:14 -0700201sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800202 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800203 Mutex::Autolock lock(mMutex);
204 if (buf < 0 || mBufferCount <= buf) {
205 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
206 mBufferCount, buf);
207 return 0;
208 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700209 mSlots[buf].mRequestBufferCalled = true;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700210 return mSlots[buf].mGraphicBuffer;
211}
212
213status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
214 uint32_t format, uint32_t usage) {
215 LOGV("SurfaceTexture::dequeueBuffer");
216
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700217 if ((w && !h) || (!w && h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700218 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
219 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700220 }
221
Mathias Agopianc04f1532011-04-25 20:22:14 -0700222 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700223
224 status_t returnFlags(OK);
225
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700226 int found, foundSync;
227 int dequeuedCount = 0;
228 bool tryAgain = true;
229 while (tryAgain) {
Mathias Agopian80727112011-05-02 19:51:12 -0700230 // We need to wait for the FIFO to drain if the number of buffer
231 // needs to change.
232 //
233 // The condition "number of buffer needs to change" is true if
234 // - the client doesn't care about how many buffers there are
235 // - AND the actual number of buffer is different from what was
236 // set in the last setBufferCountServer()
237 // - OR -
238 // setBufferCountServer() was set to a value incompatible with
239 // the synchronization mode (for instance because the sync mode
240 // changed since)
241 //
242 // As long as this condition is true AND the FIFO is not empty, we
243 // wait on mDequeueCondition.
244
245 int minBufferCountNeeded = mSynchronousMode ?
246 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
247
248 if (!mClientBufferCount &&
249 ((mServerBufferCount != mBufferCount) ||
250 (mServerBufferCount < minBufferCountNeeded))) {
251 // wait for the FIFO to drain
252 while (!mQueue.isEmpty()) {
253 mDequeueCondition.wait(mMutex);
254 }
255 minBufferCountNeeded = mSynchronousMode ?
256 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
257 }
258
259
260 if (!mClientBufferCount &&
261 ((mServerBufferCount != mBufferCount) ||
262 (mServerBufferCount < minBufferCountNeeded))) {
263 // here we're guaranteed that mQueue is empty
264 freeAllBuffers();
265 mBufferCount = mServerBufferCount;
266 if (mBufferCount < minBufferCountNeeded)
267 mBufferCount = minBufferCountNeeded;
268 mCurrentTexture = INVALID_BUFFER_SLOT;
269 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
270 }
271
272 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700273 found = INVALID_BUFFER_SLOT;
274 foundSync = INVALID_BUFFER_SLOT;
275 dequeuedCount = 0;
276 for (int i = 0; i < mBufferCount; i++) {
277 const int state = mSlots[i].mBufferState;
278 if (state == BufferSlot::DEQUEUED) {
279 dequeuedCount++;
280 }
Mathias Agopiane1220792011-05-04 18:28:07 -0700281 if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700282 foundSync = i;
283 if (i != mCurrentTexture) {
284 found = i;
285 break;
286 }
287 }
288 }
Mathias Agopian80727112011-05-02 19:51:12 -0700289
290 // clients are not allowed to dequeue more than one buffer
291 // if they didn't set a buffer count.
292 if (!mClientBufferCount && dequeuedCount) {
293 return -EINVAL;
294 }
295
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700296 // See whether a buffer has been queued since the last setBufferCount so
297 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
298 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
299 if (bufferHasBeenQueued) {
300 // make sure the client is not trying to dequeue more buffers
301 // than allowed.
302 const int avail = mBufferCount - (dequeuedCount+1);
303 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
304 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
305 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
306 dequeuedCount);
307 return -EBUSY;
308 }
Mathias Agopian80727112011-05-02 19:51:12 -0700309 }
310
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700311 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700312 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700313 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
314 if (tryAgain) {
315 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700316 }
317 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700318
Mathias Agopian80727112011-05-02 19:51:12 -0700319 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
320 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
321 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700322 }
323
Mathias Agopianc04f1532011-04-25 20:22:14 -0700324 if (found == INVALID_BUFFER_SLOT) {
325 return -EBUSY;
326 }
327
328 const int buf = found;
329 *outBuf = found;
330
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700331 const bool useDefaultSize = !w && !h;
332 if (useDefaultSize) {
333 // use the default size
334 w = mDefaultWidth;
335 h = mDefaultHeight;
336 }
337
338 const bool updateFormat = (format != 0);
339 if (!updateFormat) {
340 // keep the current (or default) format
341 format = mPixelFormat;
342 }
343
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700344 // buffer is now in DEQUEUED (but can also be current at the same time,
345 // if we're in synchronous mode)
346 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
347
348 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700349 if ((buffer == NULL) ||
350 (uint32_t(buffer->width) != w) ||
351 (uint32_t(buffer->height) != h) ||
352 (uint32_t(buffer->format) != format) ||
353 ((uint32_t(buffer->usage) & usage) != usage))
354 {
355 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700356 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700357 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700358 mGraphicBufferAlloc->createGraphicBuffer(
359 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700360 if (graphicBuffer == 0) {
361 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700362 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700363 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700364 if (updateFormat) {
365 mPixelFormat = format;
366 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800367 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700368 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800369 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
370 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
371 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
372 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
373 }
Mathias Agopian80727112011-05-02 19:51:12 -0700374 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700375 }
Mathias Agopian80727112011-05-02 19:51:12 -0700376 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800377}
378
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700379status_t SurfaceTexture::setSynchronousMode(bool enabled) {
380 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700381
382 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700383 if (!mAllowSynchronousMode && enabled)
384 return err;
385
Mathias Agopian80727112011-05-02 19:51:12 -0700386 if (!enabled) {
387 // going to asynchronous mode, drain the queue
388 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
389 mDequeueCondition.wait(mMutex);
390 }
391 }
392
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700393 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700394 // - if we're going to asynchronous mode, the queue is guaranteed to be
395 // empty here
396 // - if the client set the number of buffers, we're guaranteed that
397 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700398 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700399 mDequeueCondition.signal();
400 }
Mathias Agopian80727112011-05-02 19:51:12 -0700401 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700402}
403
Mathias Agopian97c602c2011-07-19 15:24:46 -0700404status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
405 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800406 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700407
408 sp<FrameAvailableListener> listener;
409
410 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700411 Mutex::Autolock lock(mMutex);
412 if (buf < 0 || buf >= mBufferCount) {
413 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
414 mBufferCount, buf);
415 return -EINVAL;
416 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
417 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
418 buf, mSlots[buf].mBufferState);
419 return -EINVAL;
420 } else if (buf == mCurrentTexture) {
421 LOGE("queueBuffer: slot %d is current!", buf);
422 return -EINVAL;
423 } else if (!mSlots[buf].mRequestBufferCalled) {
424 LOGE("queueBuffer: slot %d was enqueued without requesting a "
425 "buffer", buf);
426 return -EINVAL;
427 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700428
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700429 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700430 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700431 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700432
433 // Synchronous mode always signals that an additional frame should
434 // be consumed.
435 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700436 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700437 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700438 if (mQueue.empty()) {
439 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700440
441 // Asynchronous mode only signals that a frame should be
442 // consumed if no previous frame was pending. If a frame were
443 // pending then the consumer would have already been notified.
444 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700445 } else {
446 Fifo::iterator front(mQueue.begin());
447 // buffer currently queued is freed
448 mSlots[*front].mBufferState = BufferSlot::FREE;
449 // and we record the new buffer index in the queued list
450 *front = buf;
451 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700452 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700453
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700454 mSlots[buf].mBufferState = BufferSlot::QUEUED;
455 mSlots[buf].mCrop = mNextCrop;
456 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700457 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700458 mSlots[buf].mTimestamp = timestamp;
459 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700460 } // scope for the lock
461
462 // call back without lock held
463 if (listener != 0) {
464 listener->onFrameAvailable();
465 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700466
467 *outWidth = mDefaultWidth;
468 *outHeight = mDefaultHeight;
469 *outTransform = 0;
470
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800471 return OK;
472}
473
474void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800475 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800476 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700477 if (buf < 0 || buf >= mBufferCount) {
478 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
479 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800480 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700481 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
482 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
483 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800484 return;
485 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700486 mSlots[buf].mBufferState = BufferSlot::FREE;
487 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800488}
489
Jamie Gennisf238e282011-01-09 16:33:17 -0800490status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800491 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800492 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800493 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800494 return OK;
495}
496
497status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800498 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800499 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800500 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800501 return OK;
502}
503
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700504status_t SurfaceTexture::connect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700505 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700506 Mutex::Autolock lock(mMutex);
507 int err = NO_ERROR;
508 switch (api) {
509 case NATIVE_WINDOW_API_EGL:
510 case NATIVE_WINDOW_API_CPU:
511 case NATIVE_WINDOW_API_MEDIA:
512 case NATIVE_WINDOW_API_CAMERA:
513 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700514 LOGE("connect: already connected (cur=%d, req=%d)",
515 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700516 err = -EINVAL;
517 } else {
518 mConnectedApi = api;
519 }
520 break;
521 default:
522 err = -EINVAL;
523 break;
524 }
525 return err;
526}
527
528status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700529 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700530 Mutex::Autolock lock(mMutex);
531 int err = NO_ERROR;
532 switch (api) {
533 case NATIVE_WINDOW_API_EGL:
534 case NATIVE_WINDOW_API_CPU:
535 case NATIVE_WINDOW_API_MEDIA:
536 case NATIVE_WINDOW_API_CAMERA:
537 if (mConnectedApi == api) {
538 mConnectedApi = NO_CONNECTED_API;
539 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700540 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
541 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700542 err = -EINVAL;
543 }
544 break;
545 default:
546 err = -EINVAL;
547 break;
548 }
549 return err;
550}
551
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700552status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700553 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700554
555 switch (mode) {
556 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
557 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
558 break;
559 default:
560 return BAD_VALUE;
561 }
562
563 Mutex::Autolock lock(mMutex);
564 mNextScalingMode = mode;
565 return OK;
566}
567
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800568status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800569 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800570 Mutex::Autolock lock(mMutex);
571
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700572 // In asynchronous mode the list is guaranteed to be one buffer
573 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700574 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700575 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700576 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700577
Jamie Gennisf238e282011-01-09 16:33:17 -0800578 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700579 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800580 if (image == EGL_NO_IMAGE_KHR) {
581 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700582 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
583 mSlots[buf].mEglImage = image;
584 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700585 if (image == EGL_NO_IMAGE_KHR) {
586 // NOTE: if dpy was invalid, createImage() is guaranteed to
587 // fail. so we'd end up here.
588 return -EINVAL;
589 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800590 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800591
592 GLint error;
593 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700594 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800595 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700596
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700597 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
598 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700599
Jamie Gennis0eb88512011-01-26 11:52:02 -0800600 bool failed = false;
601 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800602 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700603 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800604 failed = true;
605 }
606 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800607 return -EINVAL;
608 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800609
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700610 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700611 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700612 // state. If it has already been given to the client
613 // (synchronous mode), then it stays in DEQUEUED state.
614 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
615 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
616 }
617
Jamie Gennis9a78c902011-01-12 18:30:40 -0800618 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700619 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700620 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700621 mCurrentCrop = mSlots[buf].mCrop;
622 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700623 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700624 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700625 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700626
627 // Now that we've passed the point at which failures can happen,
628 // it's safe to remove the buffer from the front of the queue.
629 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700630 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700631 } else {
632 // We always bind the texture even if we don't update its contents.
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700633 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800634 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700635
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800636 return OK;
637}
638
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700639bool SurfaceTexture::isExternalFormat(uint32_t format)
640{
641 switch (format) {
642 // supported YUV formats
643 case HAL_PIXEL_FORMAT_YV12:
644 // Legacy/deprecated YUV formats
645 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
646 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
647 case HAL_PIXEL_FORMAT_YCbCr_422_I:
648 return true;
649 }
650
651 // Any OEM format needs to be considered
652 if (format>=0x100 && format<=0x1FF)
653 return true;
654
655 return false;
656}
657
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700658GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700659 return GL_TEXTURE_EXTERNAL_OES;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700660}
661
Jamie Gennisf238e282011-01-09 16:33:17 -0800662void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800663 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700664 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
665}
666
667void SurfaceTexture::computeCurrentTransformMatrix() {
668 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800669
Jamie Gennisa214c642011-01-14 13:53:31 -0800670 float xform[16];
671 for (int i = 0; i < 16; i++) {
672 xform[i] = mtxIdentity[i];
673 }
674 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
675 float result[16];
676 mtxMul(result, xform, mtxFlipH);
677 for (int i = 0; i < 16; i++) {
678 xform[i] = result[i];
679 }
680 }
681 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
682 float result[16];
683 mtxMul(result, xform, mtxFlipV);
684 for (int i = 0; i < 16; i++) {
685 xform[i] = result[i];
686 }
687 }
688 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
689 float result[16];
690 mtxMul(result, xform, mtxRot90);
691 for (int i = 0; i < 16; i++) {
692 xform[i] = result[i];
693 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800694 }
695
696 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800697 float tx, ty, sx, sy;
698 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800699 // In order to prevent bilinear sampling at the of the crop rectangle we
700 // may need to shrink it by 2 texels in each direction. Normally this
701 // would just need to take 1/2 a texel off each end, but because the
702 // chroma channels will likely be subsampled we need to chop off a whole
703 // texel. This will cause artifacts if someone does nearest sampling
704 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
705 // accomodate the bilinear and nearest sampling uses.
706 //
707 // If nearest sampling turns out to be a desirable usage of these
708 // textures then we could add the ability to switch a SurfaceTexture to
709 // nearest-mode. Preferably, however, the image producers (video
710 // decoder, camera, etc.) would simply not use a crop rectangle (or at
711 // least not tell the framework about it) so that the GPU can do the
712 // correct edge behavior.
713 int xshrink = 0, yshrink = 0;
714 if (mCurrentCrop.left > 0) {
715 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
716 xshrink++;
717 } else {
718 tx = 0.0f;
719 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700720 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800721 xshrink++;
722 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700723 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800724 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
725 float(buf->getHeight());
726 yshrink++;
727 } else {
728 ty = 0.0f;
729 }
730 if (mCurrentCrop.top > 0) {
731 yshrink++;
732 }
733 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
734 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800735 } else {
736 tx = 0.0f;
737 ty = 0.0f;
738 sx = 1.0f;
739 sy = 1.0f;
740 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800741 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800742 sx, 0, 0, 0,
743 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800744 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800745 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800746 };
747
Jamie Gennisa214c642011-01-14 13:53:31 -0800748 float mtxBeforeFlipV[16];
749 mtxMul(mtxBeforeFlipV, crop, xform);
750
751 // SurfaceFlinger expects the top of its window textures to be at a Y
752 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
753 // want to expose this to applications, however, so we must add an
754 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700755 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800756}
757
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800758nsecs_t SurfaceTexture::getTimestamp() {
759 LOGV("SurfaceTexture::getTimestamp");
760 Mutex::Autolock lock(mMutex);
761 return mCurrentTimestamp;
762}
763
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800764void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700765 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800766 LOGV("SurfaceTexture::setFrameAvailableListener");
767 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700768 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800769}
770
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800771void SurfaceTexture::freeAllBuffers() {
772 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
773 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700774 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800775 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
776 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
777 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
778 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
779 }
780 }
781}
782
783EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
784 const sp<GraphicBuffer>& graphicBuffer) {
785 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
786 EGLint attrs[] = {
787 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
788 EGL_NONE,
789 };
790 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
791 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700792 if (image == EGL_NO_IMAGE_KHR) {
793 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800794 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800795 }
796 return image;
797}
798
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700799sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
800 Mutex::Autolock lock(mMutex);
801 return mCurrentTextureBuf;
802}
803
804Rect SurfaceTexture::getCurrentCrop() const {
805 Mutex::Autolock lock(mMutex);
806 return mCurrentCrop;
807}
808
809uint32_t SurfaceTexture::getCurrentTransform() const {
810 Mutex::Autolock lock(mMutex);
811 return mCurrentTransform;
812}
813
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700814uint32_t SurfaceTexture::getCurrentScalingMode() const {
815 Mutex::Autolock lock(mMutex);
816 return mCurrentScalingMode;
817}
818
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700819int SurfaceTexture::query(int what, int* outValue)
820{
821 Mutex::Autolock lock(mMutex);
822 int value;
823 switch (what) {
824 case NATIVE_WINDOW_WIDTH:
825 value = mDefaultWidth;
826 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
827 value = mCurrentTextureBuf->width;
828 break;
829 case NATIVE_WINDOW_HEIGHT:
830 value = mDefaultHeight;
831 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
832 value = mCurrentTextureBuf->height;
833 break;
834 case NATIVE_WINDOW_FORMAT:
835 value = mPixelFormat;
836 break;
837 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
838 value = mSynchronousMode ?
839 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
840 break;
841 default:
842 return BAD_VALUE;
843 }
844 outValue[0] = value;
845 return NO_ERROR;
846}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700847
Mathias Agopian68c77942011-05-09 19:08:33 -0700848void SurfaceTexture::dump(String8& result) const
849{
850 char buffer[1024];
851 dump(result, "", buffer, 1024);
852}
853
854void SurfaceTexture::dump(String8& result, const char* prefix,
855 char* buffer, size_t SIZE) const
856{
857 Mutex::Autolock _l(mMutex);
858 snprintf(buffer, SIZE,
859 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
860 "mPixelFormat=%d, mTexName=%d\n",
861 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
862 mPixelFormat, mTexName);
863 result.append(buffer);
864
865 String8 fifo;
866 int fifoSize = 0;
867 Fifo::const_iterator i(mQueue.begin());
868 while (i != mQueue.end()) {
869 snprintf(buffer, SIZE, "%02d ", *i++);
870 fifoSize++;
871 fifo.append(buffer);
872 }
873
874 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700875 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -0700876 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
877 ,
878 prefix, mCurrentCrop.left,
879 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700880 mCurrentTransform, mCurrentTexture,
Mathias Agopian68c77942011-05-09 19:08:33 -0700881 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
882 mCurrentTransform, fifoSize, fifo.string()
883 );
884 result.append(buffer);
885
886 struct {
887 const char * operator()(int state) const {
888 switch (state) {
889 case BufferSlot::DEQUEUED: return "DEQUEUED";
890 case BufferSlot::QUEUED: return "QUEUED";
891 case BufferSlot::FREE: return "FREE";
892 default: return "Unknown";
893 }
894 }
895 } stateName;
896
897 for (int i=0 ; i<mBufferCount ; i++) {
898 const BufferSlot& slot(mSlots[i]);
899 snprintf(buffer, SIZE,
900 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700901 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700902 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700903 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
904 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700905 );
906 result.append(buffer);
907 }
908}
909
Jamie Gennisf238e282011-01-09 16:33:17 -0800910static void mtxMul(float out[16], const float a[16], const float b[16]) {
911 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
912 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
913 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
914 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
915
916 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
917 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
918 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
919 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
920
921 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
922 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
923 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
924 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
925
926 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
927 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
928 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
929 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
930}
931
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800932}; // namespace android