blob: 54d963f292e258dac7286581ab3c15e0cc3f4e58 [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),
97 mConnectedApi(NO_CONNECTED_API) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080098 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -080099 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
100 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700101 mNextCrop.makeInvalid();
Jamie Gennis736aa952011-06-12 17:03:06 -0700102 memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800103}
104
105SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800106 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800107 freeAllBuffers();
108}
109
Mathias Agopian80727112011-05-02 19:51:12 -0700110status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
111 if (bufferCount > NUM_BUFFER_SLOTS)
112 return BAD_VALUE;
113
114 // special-case, nothing to do
115 if (bufferCount == mBufferCount)
116 return OK;
117
118 if (!mClientBufferCount &&
119 bufferCount >= mBufferCount) {
120 // easy, we just have more buffers
121 mBufferCount = bufferCount;
122 mServerBufferCount = bufferCount;
123 mDequeueCondition.signal();
124 } else {
125 // we're here because we're either
126 // - reducing the number of available buffers
127 // - or there is a client-buffer-count in effect
128
129 // less than 2 buffers is never allowed
130 if (bufferCount < 2)
131 return BAD_VALUE;
132
133 // when there is non client-buffer-count in effect, the client is not
134 // allowed to dequeue more than one buffer at a time,
135 // so the next time they dequeue a buffer, we know that they don't
136 // own one. the actual resizing will happen during the next
137 // dequeueBuffer.
138
139 mServerBufferCount = bufferCount;
140 }
141 return OK;
142}
143
144status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
145 Mutex::Autolock lock(mMutex);
146 return setBufferCountServerLocked(bufferCount);
147}
148
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800149status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800150 LOGV("SurfaceTexture::setBufferCount");
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700151 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800152
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700153 if (bufferCount > NUM_BUFFER_SLOTS) {
154 LOGE("setBufferCount: bufferCount larger than slots available");
155 return BAD_VALUE;
156 }
157
Mathias Agopian80727112011-05-02 19:51:12 -0700158 // Error out if the user has dequeued buffers
159 for (int i=0 ; i<mBufferCount ; i++) {
160 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
161 LOGE("setBufferCount: client owns some buffers");
162 return -EINVAL;
163 }
164 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700165
Mathias Agopian80727112011-05-02 19:51:12 -0700166 if (bufferCount == 0) {
167 const int minBufferSlots = mSynchronousMode ?
168 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
169 mClientBufferCount = 0;
170 bufferCount = (mServerBufferCount >= minBufferSlots) ?
171 mServerBufferCount : minBufferSlots;
172 return setBufferCountServerLocked(bufferCount);
173 }
174
175 // We don't allow the client to set a buffer-count less than
176 // MIN_ASYNC_BUFFER_SLOTS (3), there is no reason for it.
177 if (bufferCount < MIN_ASYNC_BUFFER_SLOTS) {
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800178 return BAD_VALUE;
179 }
180
Mathias Agopian80727112011-05-02 19:51:12 -0700181 // here we're guaranteed that the client doesn't have dequeued buffers
182 // and will release all of its buffer references.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800183 freeAllBuffers();
184 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700185 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800186 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700187 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700188 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189 return OK;
190}
191
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700192status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
193{
194 Mutex::Autolock lock(mMutex);
195 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
196 mDefaultWidth = w;
197 mDefaultHeight = h;
198 }
199 return OK;
200}
201
Mathias Agopianc04f1532011-04-25 20:22:14 -0700202sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800203 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800204 Mutex::Autolock lock(mMutex);
205 if (buf < 0 || mBufferCount <= buf) {
206 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
207 mBufferCount, buf);
208 return 0;
209 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700210 mSlots[buf].mRequestBufferCalled = true;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700211 return mSlots[buf].mGraphicBuffer;
212}
213
214status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
215 uint32_t format, uint32_t usage) {
216 LOGV("SurfaceTexture::dequeueBuffer");
217
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700218 if ((w && !h) || (!w && h)) {
Mathias Agopianc04f1532011-04-25 20:22:14 -0700219 LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
220 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700221 }
222
Mathias Agopianc04f1532011-04-25 20:22:14 -0700223 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700224
225 status_t returnFlags(OK);
226
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700227 int found, foundSync;
228 int dequeuedCount = 0;
229 bool tryAgain = true;
230 while (tryAgain) {
Mathias Agopian80727112011-05-02 19:51:12 -0700231 // We need to wait for the FIFO to drain if the number of buffer
232 // needs to change.
233 //
234 // The condition "number of buffer needs to change" is true if
235 // - the client doesn't care about how many buffers there are
236 // - AND the actual number of buffer is different from what was
237 // set in the last setBufferCountServer()
238 // - OR -
239 // setBufferCountServer() was set to a value incompatible with
240 // the synchronization mode (for instance because the sync mode
241 // changed since)
242 //
243 // As long as this condition is true AND the FIFO is not empty, we
244 // wait on mDequeueCondition.
245
246 int minBufferCountNeeded = mSynchronousMode ?
247 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
248
249 if (!mClientBufferCount &&
250 ((mServerBufferCount != mBufferCount) ||
251 (mServerBufferCount < minBufferCountNeeded))) {
252 // wait for the FIFO to drain
253 while (!mQueue.isEmpty()) {
254 mDequeueCondition.wait(mMutex);
255 }
256 minBufferCountNeeded = mSynchronousMode ?
257 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
258 }
259
260
261 if (!mClientBufferCount &&
262 ((mServerBufferCount != mBufferCount) ||
263 (mServerBufferCount < minBufferCountNeeded))) {
264 // here we're guaranteed that mQueue is empty
265 freeAllBuffers();
266 mBufferCount = mServerBufferCount;
267 if (mBufferCount < minBufferCountNeeded)
268 mBufferCount = minBufferCountNeeded;
269 mCurrentTexture = INVALID_BUFFER_SLOT;
270 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
271 }
272
273 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700274 found = INVALID_BUFFER_SLOT;
275 foundSync = INVALID_BUFFER_SLOT;
276 dequeuedCount = 0;
277 for (int i = 0; i < mBufferCount; i++) {
278 const int state = mSlots[i].mBufferState;
279 if (state == BufferSlot::DEQUEUED) {
280 dequeuedCount++;
281 }
Mathias Agopiane1220792011-05-04 18:28:07 -0700282 if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700283 foundSync = i;
284 if (i != mCurrentTexture) {
285 found = i;
286 break;
287 }
288 }
289 }
Mathias Agopian80727112011-05-02 19:51:12 -0700290
291 // clients are not allowed to dequeue more than one buffer
292 // if they didn't set a buffer count.
293 if (!mClientBufferCount && dequeuedCount) {
294 return -EINVAL;
295 }
296
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700297 // See whether a buffer has been queued since the last setBufferCount so
298 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
299 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
300 if (bufferHasBeenQueued) {
301 // make sure the client is not trying to dequeue more buffers
302 // than allowed.
303 const int avail = mBufferCount - (dequeuedCount+1);
304 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
305 LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
306 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
307 dequeuedCount);
308 return -EBUSY;
309 }
Mathias Agopian80727112011-05-02 19:51:12 -0700310 }
311
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700312 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian80727112011-05-02 19:51:12 -0700313 // for for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700314 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
315 if (tryAgain) {
316 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700317 }
318 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700319
Mathias Agopian80727112011-05-02 19:51:12 -0700320 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
321 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
322 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700323 }
324
Mathias Agopianc04f1532011-04-25 20:22:14 -0700325 if (found == INVALID_BUFFER_SLOT) {
326 return -EBUSY;
327 }
328
329 const int buf = found;
330 *outBuf = found;
331
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700332 const bool useDefaultSize = !w && !h;
333 if (useDefaultSize) {
334 // use the default size
335 w = mDefaultWidth;
336 h = mDefaultHeight;
337 }
338
339 const bool updateFormat = (format != 0);
340 if (!updateFormat) {
341 // keep the current (or default) format
342 format = mPixelFormat;
343 }
344
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700345 // buffer is now in DEQUEUED (but can also be current at the same time,
346 // if we're in synchronous mode)
347 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
348
349 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700350 if ((buffer == NULL) ||
351 (uint32_t(buffer->width) != w) ||
352 (uint32_t(buffer->height) != h) ||
353 (uint32_t(buffer->format) != format) ||
354 ((uint32_t(buffer->usage) & usage) != usage))
355 {
356 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700357 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700358 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700359 mGraphicBufferAlloc->createGraphicBuffer(
360 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700361 if (graphicBuffer == 0) {
362 LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700363 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700364 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700365 if (updateFormat) {
366 mPixelFormat = format;
367 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800368 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700369 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800370 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
371 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
372 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
373 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
374 }
Mathias Agopian80727112011-05-02 19:51:12 -0700375 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700376 }
Mathias Agopian80727112011-05-02 19:51:12 -0700377 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800378}
379
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700380status_t SurfaceTexture::setSynchronousMode(bool enabled) {
381 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700382
383 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700384 if (!mAllowSynchronousMode && enabled)
385 return err;
386
Mathias Agopian80727112011-05-02 19:51:12 -0700387 if (!enabled) {
388 // going to asynchronous mode, drain the queue
389 while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
390 mDequeueCondition.wait(mMutex);
391 }
392 }
393
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700394 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700395 // - if we're going to asynchronous mode, the queue is guaranteed to be
396 // empty here
397 // - if the client set the number of buffers, we're guaranteed that
398 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700399 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700400 mDequeueCondition.signal();
401 }
Mathias Agopian80727112011-05-02 19:51:12 -0700402 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700403}
404
Mathias Agopian97c602c2011-07-19 15:24:46 -0700405status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
406 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800407 LOGV("SurfaceTexture::queueBuffer");
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700408
409 sp<FrameAvailableListener> listener;
410
411 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700412 Mutex::Autolock lock(mMutex);
413 if (buf < 0 || buf >= mBufferCount) {
414 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
415 mBufferCount, buf);
416 return -EINVAL;
417 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
418 LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
419 buf, mSlots[buf].mBufferState);
420 return -EINVAL;
421 } else if (buf == mCurrentTexture) {
422 LOGE("queueBuffer: slot %d is current!", buf);
423 return -EINVAL;
424 } else if (!mSlots[buf].mRequestBufferCalled) {
425 LOGE("queueBuffer: slot %d was enqueued without requesting a "
426 "buffer", buf);
427 return -EINVAL;
428 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700429
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700430 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700431 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700432 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700433
434 // Synchronous mode always signals that an additional frame should
435 // be consumed.
436 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700437 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700438 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700439 if (mQueue.empty()) {
440 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700441
442 // Asynchronous mode only signals that a frame should be
443 // consumed if no previous frame was pending. If a frame were
444 // pending then the consumer would have already been notified.
445 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700446 } else {
447 Fifo::iterator front(mQueue.begin());
448 // buffer currently queued is freed
449 mSlots[*front].mBufferState = BufferSlot::FREE;
450 // and we record the new buffer index in the queued list
451 *front = buf;
452 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700453 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700454
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700455 mSlots[buf].mBufferState = BufferSlot::QUEUED;
456 mSlots[buf].mCrop = mNextCrop;
457 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700458 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700459 mSlots[buf].mTimestamp = timestamp;
460 mDequeueCondition.signal();
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700461 } // scope for the lock
462
463 // call back without lock held
464 if (listener != 0) {
465 listener->onFrameAvailable();
466 }
Mathias Agopian97c602c2011-07-19 15:24:46 -0700467
468 *outWidth = mDefaultWidth;
469 *outHeight = mDefaultHeight;
470 *outTransform = 0;
471
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800472 return OK;
473}
474
475void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800476 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800477 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700478 if (buf < 0 || buf >= mBufferCount) {
479 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
480 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800481 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700482 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
483 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
484 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800485 return;
486 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700487 mSlots[buf].mBufferState = BufferSlot::FREE;
488 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800489}
490
Jamie Gennisf238e282011-01-09 16:33:17 -0800491status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800492 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800493 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800494 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800495 return OK;
496}
497
498status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800499 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800500 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800501 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800502 return OK;
503}
504
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700505status_t SurfaceTexture::connect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700506 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700507 Mutex::Autolock lock(mMutex);
508 int err = NO_ERROR;
509 switch (api) {
510 case NATIVE_WINDOW_API_EGL:
511 case NATIVE_WINDOW_API_CPU:
512 case NATIVE_WINDOW_API_MEDIA:
513 case NATIVE_WINDOW_API_CAMERA:
514 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700515 LOGE("connect: already connected (cur=%d, req=%d)",
516 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700517 err = -EINVAL;
518 } else {
519 mConnectedApi = api;
520 }
521 break;
522 default:
523 err = -EINVAL;
524 break;
525 }
526 return err;
527}
528
529status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700530 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700531 Mutex::Autolock lock(mMutex);
532 int err = NO_ERROR;
533 switch (api) {
534 case NATIVE_WINDOW_API_EGL:
535 case NATIVE_WINDOW_API_CPU:
536 case NATIVE_WINDOW_API_MEDIA:
537 case NATIVE_WINDOW_API_CAMERA:
538 if (mConnectedApi == api) {
539 mConnectedApi = NO_CONNECTED_API;
540 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700541 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
542 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700543 err = -EINVAL;
544 }
545 break;
546 default:
547 err = -EINVAL;
548 break;
549 }
550 return err;
551}
552
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700553status_t SurfaceTexture::setScalingMode(int mode) {
Mathias Agopian933389f2011-07-18 16:15:08 -0700554 LOGV("SurfaceTexture::setScalingMode(%d)", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700555
556 switch (mode) {
557 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
558 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
559 break;
560 default:
561 return BAD_VALUE;
562 }
563
564 Mutex::Autolock lock(mMutex);
565 mNextScalingMode = mode;
566 return OK;
567}
568
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800569status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800570 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800571 Mutex::Autolock lock(mMutex);
572
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700573 // In asynchronous mode the list is guaranteed to be one buffer
574 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700575 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700576 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700577 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700578
Jamie Gennisf238e282011-01-09 16:33:17 -0800579 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700580 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800581 if (image == EGL_NO_IMAGE_KHR) {
582 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700583 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
584 mSlots[buf].mEglImage = image;
585 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700586 if (image == EGL_NO_IMAGE_KHR) {
587 // NOTE: if dpy was invalid, createImage() is guaranteed to
588 // fail. so we'd end up here.
589 return -EINVAL;
590 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800591 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800592
593 GLint error;
594 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700595 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800596 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700597
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700598 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700599 if (target != mCurrentTextureTarget) {
600 glDeleteTextures(1, &mTexName);
601 }
602 glBindTexture(target, mTexName);
603 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
604
Jamie Gennis0eb88512011-01-26 11:52:02 -0800605 bool failed = false;
606 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800607 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700608 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800609 failed = true;
610 }
611 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800612 return -EINVAL;
613 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800614
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700615 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700616 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700617 // state. If it has already been given to the client
618 // (synchronous mode), then it stays in DEQUEUED state.
619 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
620 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
621 }
622
Jamie Gennis9a78c902011-01-12 18:30:40 -0800623 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700624 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700625 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700626 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700627 mCurrentCrop = mSlots[buf].mCrop;
628 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700629 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700630 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700631 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700632
633 // Now that we've passed the point at which failures can happen,
634 // it's safe to remove the buffer from the front of the queue.
635 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700636 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700637 } else {
638 // We always bind the texture even if we don't update its contents.
639 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800640 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700641
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800642 return OK;
643}
644
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700645bool SurfaceTexture::isExternalFormat(uint32_t format)
646{
647 switch (format) {
648 // supported YUV formats
649 case HAL_PIXEL_FORMAT_YV12:
650 // Legacy/deprecated YUV formats
651 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
652 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
653 case HAL_PIXEL_FORMAT_YCbCr_422_I:
654 return true;
655 }
656
657 // Any OEM format needs to be considered
658 if (format>=0x100 && format<=0x1FF)
659 return true;
660
661 return false;
662}
663
664GLenum SurfaceTexture::getTextureTarget(uint32_t format)
665{
666 GLenum target = GL_TEXTURE_2D;
667#if defined(GL_OES_EGL_image_external)
668 if (isExternalFormat(format)) {
669 target = GL_TEXTURE_EXTERNAL_OES;
670 }
671#endif
672 return target;
673}
674
675GLenum SurfaceTexture::getCurrentTextureTarget() const {
676 Mutex::Autolock lock(mMutex);
677 return mCurrentTextureTarget;
678}
679
Jamie Gennisf238e282011-01-09 16:33:17 -0800680void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800681 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700682 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
683}
684
685void SurfaceTexture::computeCurrentTransformMatrix() {
686 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800687
Jamie Gennisa214c642011-01-14 13:53:31 -0800688 float xform[16];
689 for (int i = 0; i < 16; i++) {
690 xform[i] = mtxIdentity[i];
691 }
692 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
693 float result[16];
694 mtxMul(result, xform, mtxFlipH);
695 for (int i = 0; i < 16; i++) {
696 xform[i] = result[i];
697 }
698 }
699 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
700 float result[16];
701 mtxMul(result, xform, mtxFlipV);
702 for (int i = 0; i < 16; i++) {
703 xform[i] = result[i];
704 }
705 }
706 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
707 float result[16];
708 mtxMul(result, xform, mtxRot90);
709 for (int i = 0; i < 16; i++) {
710 xform[i] = result[i];
711 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800712 }
713
714 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800715 float tx, ty, sx, sy;
716 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800717 // In order to prevent bilinear sampling at the of the crop rectangle we
718 // may need to shrink it by 2 texels in each direction. Normally this
719 // would just need to take 1/2 a texel off each end, but because the
720 // chroma channels will likely be subsampled we need to chop off a whole
721 // texel. This will cause artifacts if someone does nearest sampling
722 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
723 // accomodate the bilinear and nearest sampling uses.
724 //
725 // If nearest sampling turns out to be a desirable usage of these
726 // textures then we could add the ability to switch a SurfaceTexture to
727 // nearest-mode. Preferably, however, the image producers (video
728 // decoder, camera, etc.) would simply not use a crop rectangle (or at
729 // least not tell the framework about it) so that the GPU can do the
730 // correct edge behavior.
731 int xshrink = 0, yshrink = 0;
732 if (mCurrentCrop.left > 0) {
733 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
734 xshrink++;
735 } else {
736 tx = 0.0f;
737 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700738 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800739 xshrink++;
740 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700741 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800742 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
743 float(buf->getHeight());
744 yshrink++;
745 } else {
746 ty = 0.0f;
747 }
748 if (mCurrentCrop.top > 0) {
749 yshrink++;
750 }
751 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
752 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800753 } else {
754 tx = 0.0f;
755 ty = 0.0f;
756 sx = 1.0f;
757 sy = 1.0f;
758 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800759 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800760 sx, 0, 0, 0,
761 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800762 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800763 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800764 };
765
Jamie Gennisa214c642011-01-14 13:53:31 -0800766 float mtxBeforeFlipV[16];
767 mtxMul(mtxBeforeFlipV, crop, xform);
768
769 // SurfaceFlinger expects the top of its window textures to be at a Y
770 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
771 // want to expose this to applications, however, so we must add an
772 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700773 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800774}
775
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800776nsecs_t SurfaceTexture::getTimestamp() {
777 LOGV("SurfaceTexture::getTimestamp");
778 Mutex::Autolock lock(mMutex);
779 return mCurrentTimestamp;
780}
781
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800782void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700783 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800784 LOGV("SurfaceTexture::setFrameAvailableListener");
785 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700786 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800787}
788
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800789void SurfaceTexture::freeAllBuffers() {
790 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
791 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700792 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800793 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
794 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
795 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
796 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
797 }
798 }
799}
800
801EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
802 const sp<GraphicBuffer>& graphicBuffer) {
803 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
804 EGLint attrs[] = {
805 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
806 EGL_NONE,
807 };
808 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
809 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700810 if (image == EGL_NO_IMAGE_KHR) {
811 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800812 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800813 }
814 return image;
815}
816
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700817sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
818 Mutex::Autolock lock(mMutex);
819 return mCurrentTextureBuf;
820}
821
822Rect SurfaceTexture::getCurrentCrop() const {
823 Mutex::Autolock lock(mMutex);
824 return mCurrentCrop;
825}
826
827uint32_t SurfaceTexture::getCurrentTransform() const {
828 Mutex::Autolock lock(mMutex);
829 return mCurrentTransform;
830}
831
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700832uint32_t SurfaceTexture::getCurrentScalingMode() const {
833 Mutex::Autolock lock(mMutex);
834 return mCurrentScalingMode;
835}
836
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700837int SurfaceTexture::query(int what, int* outValue)
838{
839 Mutex::Autolock lock(mMutex);
840 int value;
841 switch (what) {
842 case NATIVE_WINDOW_WIDTH:
843 value = mDefaultWidth;
844 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
845 value = mCurrentTextureBuf->width;
846 break;
847 case NATIVE_WINDOW_HEIGHT:
848 value = mDefaultHeight;
849 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
850 value = mCurrentTextureBuf->height;
851 break;
852 case NATIVE_WINDOW_FORMAT:
853 value = mPixelFormat;
854 break;
855 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
856 value = mSynchronousMode ?
857 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
858 break;
859 default:
860 return BAD_VALUE;
861 }
862 outValue[0] = value;
863 return NO_ERROR;
864}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700865
Mathias Agopian68c77942011-05-09 19:08:33 -0700866void SurfaceTexture::dump(String8& result) const
867{
868 char buffer[1024];
869 dump(result, "", buffer, 1024);
870}
871
872void SurfaceTexture::dump(String8& result, const char* prefix,
873 char* buffer, size_t SIZE) const
874{
875 Mutex::Autolock _l(mMutex);
876 snprintf(buffer, SIZE,
877 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
878 "mPixelFormat=%d, mTexName=%d\n",
879 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
880 mPixelFormat, mTexName);
881 result.append(buffer);
882
883 String8 fifo;
884 int fifoSize = 0;
885 Fifo::const_iterator i(mQueue.begin());
886 while (i != mQueue.end()) {
887 snprintf(buffer, SIZE, "%02d ", *i++);
888 fifoSize++;
889 fifo.append(buffer);
890 }
891
892 snprintf(buffer, SIZE,
893 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
894 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
895 ,
896 prefix, mCurrentCrop.left,
897 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
898 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
899 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
900 mCurrentTransform, fifoSize, fifo.string()
901 );
902 result.append(buffer);
903
904 struct {
905 const char * operator()(int state) const {
906 switch (state) {
907 case BufferSlot::DEQUEUED: return "DEQUEUED";
908 case BufferSlot::QUEUED: return "QUEUED";
909 case BufferSlot::FREE: return "FREE";
910 default: return "Unknown";
911 }
912 }
913 } stateName;
914
915 for (int i=0 ; i<mBufferCount ; i++) {
916 const BufferSlot& slot(mSlots[i]);
917 snprintf(buffer, SIZE,
918 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700919 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700920 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700921 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
922 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700923 );
924 result.append(buffer);
925 }
926}
927
Jamie Gennisf238e282011-01-09 16:33:17 -0800928static void mtxMul(float out[16], const float a[16], const float b[16]) {
929 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
930 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
931 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
932 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
933
934 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
935 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
936 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
937 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
938
939 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
940 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
941 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
942 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
943
944 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
945 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
946 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
947 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
948}
949
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800950}; // namespace android