blob: a12d40adcabbbfc111f853167c2cca07a860a2b3 [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 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
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800404status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800405 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700406
407 sp<FrameAvailableListener> listener;
408
409 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700410 Mutex::Autolock lock(mMutex);
411 if (buf < 0 || buf >= mBufferCount) {
412 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
413 mBufferCount, buf);
414 return -EINVAL;
415 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
416 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
417 buf, mSlots[buf].mBufferState);
418 return -EINVAL;
419 } else if (buf == mCurrentTexture) {
420 LOGE("queueBuffer: slot %d is current!", buf);
421 return -EINVAL;
422 } else if (!mSlots[buf].mRequestBufferCalled) {
423 LOGE("queueBuffer: slot %d was enqueued without requesting a "
424 "buffer", buf);
425 return -EINVAL;
426 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700427
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700428 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700429 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700430 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700431
432 // Synchronous mode always signals that an additional frame should
433 // be consumed.
434 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700435 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700436 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700437 if (mQueue.empty()) {
438 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700439
440 // Asynchronous mode only signals that a frame should be
441 // consumed if no previous frame was pending. If a frame were
442 // pending then the consumer would have already been notified.
443 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700444 } else {
445 Fifo::iterator front(mQueue.begin());
446 // buffer currently queued is freed
447 mSlots[*front].mBufferState = BufferSlot::FREE;
448 // and we record the new buffer index in the queued list
449 *front = buf;
450 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700451 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700452
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700453 mSlots[buf].mBufferState = BufferSlot::QUEUED;
454 mSlots[buf].mCrop = mNextCrop;
455 mSlots[buf].mTransform = mNextTransform;
456 mSlots[buf].mTimestamp = timestamp;
457 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700458 } // scope for the lock
459
460 // call back without lock held
461 if (listener != 0) {
462 listener->onFrameAvailable();
463 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800464 return OK;
465}
466
467void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800468 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800469 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700470 if (buf < 0 || buf >= mBufferCount) {
471 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
472 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800473 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700474 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
475 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
476 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800477 return;
478 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700479 mSlots[buf].mBufferState = BufferSlot::FREE;
480 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800481}
482
Jamie Gennisf238e282011-01-09 16:33:17 -0800483status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800484 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800485 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800486 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800487 return OK;
488}
489
490status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800491 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800492 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800493 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800494 return OK;
495}
496
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700497status_t SurfaceTexture::connect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700498 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700499 Mutex::Autolock lock(mMutex);
500 int err = NO_ERROR;
501 switch (api) {
502 case NATIVE_WINDOW_API_EGL:
503 case NATIVE_WINDOW_API_CPU:
504 case NATIVE_WINDOW_API_MEDIA:
505 case NATIVE_WINDOW_API_CAMERA:
506 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700507 LOGE("connect: already connected (cur=%d, req=%d)",
508 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700509 err = -EINVAL;
510 } else {
511 mConnectedApi = api;
512 }
513 break;
514 default:
515 err = -EINVAL;
516 break;
517 }
518 return err;
519}
520
521status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700522 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700523 Mutex::Autolock lock(mMutex);
524 int err = NO_ERROR;
525 switch (api) {
526 case NATIVE_WINDOW_API_EGL:
527 case NATIVE_WINDOW_API_CPU:
528 case NATIVE_WINDOW_API_MEDIA:
529 case NATIVE_WINDOW_API_CAMERA:
530 if (mConnectedApi == api) {
531 mConnectedApi = NO_CONNECTED_API;
532 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700533 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
534 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700535 err = -EINVAL;
536 }
537 break;
538 default:
539 err = -EINVAL;
540 break;
541 }
542 return err;
543}
544
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800545status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800546 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800547 Mutex::Autolock lock(mMutex);
548
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700549 // In asynchronous mode the list is guaranteed to be one buffer
550 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700551 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700552 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700553 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700554
Jamie Gennisf238e282011-01-09 16:33:17 -0800555 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700556 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800557 if (image == EGL_NO_IMAGE_KHR) {
558 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700559 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
560 mSlots[buf].mEglImage = image;
561 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700562 if (image == EGL_NO_IMAGE_KHR) {
563 // NOTE: if dpy was invalid, createImage() is guaranteed to
564 // fail. so we'd end up here.
565 return -EINVAL;
566 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800567 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800568
569 GLint error;
570 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700571 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800572 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700573
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700574 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700575 if (target != mCurrentTextureTarget) {
576 glDeleteTextures(1, &mTexName);
577 }
578 glBindTexture(target, mTexName);
579 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
580
Jamie Gennis0eb88512011-01-26 11:52:02 -0800581 bool failed = false;
582 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800583 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700584 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800585 failed = true;
586 }
587 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800588 return -EINVAL;
589 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800590
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700591 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700592 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700593 // state. If it has already been given to the client
594 // (synchronous mode), then it stays in DEQUEUED state.
595 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
596 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
597 }
598
Jamie Gennis9a78c902011-01-12 18:30:40 -0800599 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700600 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700601 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700602 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700603 mCurrentCrop = mSlots[buf].mCrop;
604 mCurrentTransform = mSlots[buf].mTransform;
605 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700606 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700607
608 // Now that we've passed the point at which failures can happen,
609 // it's safe to remove the buffer from the front of the queue.
610 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700611 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700612 } else {
613 // We always bind the texture even if we don't update its contents.
614 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800615 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700616
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800617 return OK;
618}
619
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700620bool SurfaceTexture::isExternalFormat(uint32_t format)
621{
622 switch (format) {
623 // supported YUV formats
624 case HAL_PIXEL_FORMAT_YV12:
625 // Legacy/deprecated YUV formats
626 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
627 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
628 case HAL_PIXEL_FORMAT_YCbCr_422_I:
629 return true;
630 }
631
632 // Any OEM format needs to be considered
633 if (format>=0x100 && format<=0x1FF)
634 return true;
635
636 return false;
637}
638
639GLenum SurfaceTexture::getTextureTarget(uint32_t format)
640{
641 GLenum target = GL_TEXTURE_2D;
642#if defined(GL_OES_EGL_image_external)
643 if (isExternalFormat(format)) {
644 target = GL_TEXTURE_EXTERNAL_OES;
645 }
646#endif
647 return target;
648}
649
650GLenum SurfaceTexture::getCurrentTextureTarget() const {
651 Mutex::Autolock lock(mMutex);
652 return mCurrentTextureTarget;
653}
654
Jamie Gennisf238e282011-01-09 16:33:17 -0800655void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800656 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700657 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
658}
659
660void SurfaceTexture::computeCurrentTransformMatrix() {
661 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800662
Jamie Gennisa214c642011-01-14 13:53:31 -0800663 float xform[16];
664 for (int i = 0; i < 16; i++) {
665 xform[i] = mtxIdentity[i];
666 }
667 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
668 float result[16];
669 mtxMul(result, xform, mtxFlipH);
670 for (int i = 0; i < 16; i++) {
671 xform[i] = result[i];
672 }
673 }
674 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
675 float result[16];
676 mtxMul(result, xform, mtxFlipV);
677 for (int i = 0; i < 16; i++) {
678 xform[i] = result[i];
679 }
680 }
681 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
682 float result[16];
683 mtxMul(result, xform, mtxRot90);
684 for (int i = 0; i < 16; i++) {
685 xform[i] = result[i];
686 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800687 }
688
689 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800690 float tx, ty, sx, sy;
691 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800692 // In order to prevent bilinear sampling at the of the crop rectangle we
693 // may need to shrink it by 2 texels in each direction. Normally this
694 // would just need to take 1/2 a texel off each end, but because the
695 // chroma channels will likely be subsampled we need to chop off a whole
696 // texel. This will cause artifacts if someone does nearest sampling
697 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
698 // accomodate the bilinear and nearest sampling uses.
699 //
700 // If nearest sampling turns out to be a desirable usage of these
701 // textures then we could add the ability to switch a SurfaceTexture to
702 // nearest-mode. Preferably, however, the image producers (video
703 // decoder, camera, etc.) would simply not use a crop rectangle (or at
704 // least not tell the framework about it) so that the GPU can do the
705 // correct edge behavior.
706 int xshrink = 0, yshrink = 0;
707 if (mCurrentCrop.left > 0) {
708 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
709 xshrink++;
710 } else {
711 tx = 0.0f;
712 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700713 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800714 xshrink++;
715 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700716 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800717 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
718 float(buf->getHeight());
719 yshrink++;
720 } else {
721 ty = 0.0f;
722 }
723 if (mCurrentCrop.top > 0) {
724 yshrink++;
725 }
726 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
727 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800728 } else {
729 tx = 0.0f;
730 ty = 0.0f;
731 sx = 1.0f;
732 sy = 1.0f;
733 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800734 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800735 sx, 0, 0, 0,
736 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800737 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800738 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800739 };
740
Jamie Gennisa214c642011-01-14 13:53:31 -0800741 float mtxBeforeFlipV[16];
742 mtxMul(mtxBeforeFlipV, crop, xform);
743
744 // SurfaceFlinger expects the top of its window textures to be at a Y
745 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
746 // want to expose this to applications, however, so we must add an
747 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700748 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800749}
750
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800751nsecs_t SurfaceTexture::getTimestamp() {
752 LOGV("SurfaceTexture::getTimestamp");
753 Mutex::Autolock lock(mMutex);
754 return mCurrentTimestamp;
755}
756
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800757void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700758 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800759 LOGV("SurfaceTexture::setFrameAvailableListener");
760 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700761 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800762}
763
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800764sp<IBinder> SurfaceTexture::getAllocator() {
765 LOGV("SurfaceTexture::getAllocator");
766 return mGraphicBufferAlloc->asBinder();
767}
768
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800769void SurfaceTexture::freeAllBuffers() {
770 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
771 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700772 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800773 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
774 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
775 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
776 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
777 }
778 }
779}
780
781EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
782 const sp<GraphicBuffer>& graphicBuffer) {
783 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
784 EGLint attrs[] = {
785 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
786 EGL_NONE,
787 };
788 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
789 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700790 if (image == EGL_NO_IMAGE_KHR) {
791 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800792 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800793 }
794 return image;
795}
796
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700797sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
798 Mutex::Autolock lock(mMutex);
799 return mCurrentTextureBuf;
800}
801
802Rect SurfaceTexture::getCurrentCrop() const {
803 Mutex::Autolock lock(mMutex);
804 return mCurrentCrop;
805}
806
807uint32_t SurfaceTexture::getCurrentTransform() const {
808 Mutex::Autolock lock(mMutex);
809 return mCurrentTransform;
810}
811
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700812int SurfaceTexture::query(int what, int* outValue)
813{
814 Mutex::Autolock lock(mMutex);
815 int value;
816 switch (what) {
817 case NATIVE_WINDOW_WIDTH:
818 value = mDefaultWidth;
819 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
820 value = mCurrentTextureBuf->width;
821 break;
822 case NATIVE_WINDOW_HEIGHT:
823 value = mDefaultHeight;
824 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
825 value = mCurrentTextureBuf->height;
826 break;
827 case NATIVE_WINDOW_FORMAT:
828 value = mPixelFormat;
829 break;
830 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
831 value = mSynchronousMode ?
832 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
833 break;
834 default:
835 return BAD_VALUE;
836 }
837 outValue[0] = value;
838 return NO_ERROR;
839}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700840
Mathias Agopian68c77942011-05-09 19:08:33 -0700841void SurfaceTexture::dump(String8& result) const
842{
843 char buffer[1024];
844 dump(result, "", buffer, 1024);
845}
846
847void SurfaceTexture::dump(String8& result, const char* prefix,
848 char* buffer, size_t SIZE) const
849{
850 Mutex::Autolock _l(mMutex);
851 snprintf(buffer, SIZE,
852 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
853 "mPixelFormat=%d, mTexName=%d\n",
854 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
855 mPixelFormat, mTexName);
856 result.append(buffer);
857
858 String8 fifo;
859 int fifoSize = 0;
860 Fifo::const_iterator i(mQueue.begin());
861 while (i != mQueue.end()) {
862 snprintf(buffer, SIZE, "%02d ", *i++);
863 fifoSize++;
864 fifo.append(buffer);
865 }
866
867 snprintf(buffer, SIZE,
868 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
869 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
870 ,
871 prefix, mCurrentCrop.left,
872 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
873 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
874 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
875 mCurrentTransform, fifoSize, fifo.string()
876 );
877 result.append(buffer);
878
879 struct {
880 const char * operator()(int state) const {
881 switch (state) {
882 case BufferSlot::DEQUEUED: return "DEQUEUED";
883 case BufferSlot::QUEUED: return "QUEUED";
884 case BufferSlot::FREE: return "FREE";
885 default: return "Unknown";
886 }
887 }
888 } stateName;
889
890 for (int i=0 ; i<mBufferCount ; i++) {
891 const BufferSlot& slot(mSlots[i]);
892 snprintf(buffer, SIZE,
893 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700894 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700895 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700896 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
897 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700898 );
899 result.append(buffer);
900 }
901}
902
Jamie Gennisf238e282011-01-09 16:33:17 -0800903static void mtxMul(float out[16], const float a[16], const float b[16]) {
904 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
905 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
906 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
907 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
908
909 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
910 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
911 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
912 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
913
914 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
915 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
916 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
917 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
918
919 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
920 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
921 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
922 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
923}
924
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800925}; // namespace android