blob: cc9f461f8993258b248c406f93e967ea23094c6e [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
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800405status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
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 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800466 return OK;
467}
468
469void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800470 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800471 Mutex::Autolock lock(mMutex);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700472 if (buf < 0 || buf >= mBufferCount) {
473 LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
474 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800475 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700476 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
477 LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
478 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800479 return;
480 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700481 mSlots[buf].mBufferState = BufferSlot::FREE;
482 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800483}
484
Jamie Gennisf238e282011-01-09 16:33:17 -0800485status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800486 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800487 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800488 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800489 return OK;
490}
491
492status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800493 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800494 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800495 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800496 return OK;
497}
498
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700499status_t SurfaceTexture::connect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700500 LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700501 Mutex::Autolock lock(mMutex);
502 int err = NO_ERROR;
503 switch (api) {
504 case NATIVE_WINDOW_API_EGL:
505 case NATIVE_WINDOW_API_CPU:
506 case NATIVE_WINDOW_API_MEDIA:
507 case NATIVE_WINDOW_API_CAMERA:
508 if (mConnectedApi != NO_CONNECTED_API) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700509 LOGE("connect: already connected (cur=%d, req=%d)",
510 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700511 err = -EINVAL;
512 } else {
513 mConnectedApi = api;
514 }
515 break;
516 default:
517 err = -EINVAL;
518 break;
519 }
520 return err;
521}
522
523status_t SurfaceTexture::disconnect(int api) {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700524 LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700525 Mutex::Autolock lock(mMutex);
526 int err = NO_ERROR;
527 switch (api) {
528 case NATIVE_WINDOW_API_EGL:
529 case NATIVE_WINDOW_API_CPU:
530 case NATIVE_WINDOW_API_MEDIA:
531 case NATIVE_WINDOW_API_CAMERA:
532 if (mConnectedApi == api) {
533 mConnectedApi = NO_CONNECTED_API;
534 } else {
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700535 LOGE("disconnect: connected to another api (cur=%d, req=%d)",
536 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700537 err = -EINVAL;
538 }
539 break;
540 default:
541 err = -EINVAL;
542 break;
543 }
544 return err;
545}
546
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700547status_t SurfaceTexture::setScalingMode(int mode) {
548 LOGV("SurfaceTexture::setScalingMode");
549
550 switch (mode) {
551 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
552 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
553 break;
554 default:
555 return BAD_VALUE;
556 }
557
558 Mutex::Autolock lock(mMutex);
559 mNextScalingMode = mode;
560 return OK;
561}
562
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800563status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800564 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800565 Mutex::Autolock lock(mMutex);
566
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700567 // In asynchronous mode the list is guaranteed to be one buffer
568 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700569 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700570 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700571 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700572
Jamie Gennisf238e282011-01-09 16:33:17 -0800573 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700574 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800575 if (image == EGL_NO_IMAGE_KHR) {
576 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700577 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
578 mSlots[buf].mEglImage = image;
579 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700580 if (image == EGL_NO_IMAGE_KHR) {
581 // NOTE: if dpy was invalid, createImage() is guaranteed to
582 // fail. so we'd end up here.
583 return -EINVAL;
584 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800585 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800586
587 GLint error;
588 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700589 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800590 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700591
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700592 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700593 if (target != mCurrentTextureTarget) {
594 glDeleteTextures(1, &mTexName);
595 }
596 glBindTexture(target, mTexName);
597 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
598
Jamie Gennis0eb88512011-01-26 11:52:02 -0800599 bool failed = false;
600 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800601 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700602 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800603 failed = true;
604 }
605 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800606 return -EINVAL;
607 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800608
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700609 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700610 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700611 // state. If it has already been given to the client
612 // (synchronous mode), then it stays in DEQUEUED state.
613 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
614 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
615 }
616
Jamie Gennis9a78c902011-01-12 18:30:40 -0800617 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700618 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700619 mCurrentTextureTarget = target;
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.
633 glBindTexture(mCurrentTextureTarget, 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
658GLenum SurfaceTexture::getTextureTarget(uint32_t format)
659{
660 GLenum target = GL_TEXTURE_2D;
661#if defined(GL_OES_EGL_image_external)
662 if (isExternalFormat(format)) {
663 target = GL_TEXTURE_EXTERNAL_OES;
664 }
665#endif
666 return target;
667}
668
669GLenum SurfaceTexture::getCurrentTextureTarget() const {
670 Mutex::Autolock lock(mMutex);
671 return mCurrentTextureTarget;
672}
673
Jamie Gennisf238e282011-01-09 16:33:17 -0800674void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800675 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700676 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
677}
678
679void SurfaceTexture::computeCurrentTransformMatrix() {
680 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800681
Jamie Gennisa214c642011-01-14 13:53:31 -0800682 float xform[16];
683 for (int i = 0; i < 16; i++) {
684 xform[i] = mtxIdentity[i];
685 }
686 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
687 float result[16];
688 mtxMul(result, xform, mtxFlipH);
689 for (int i = 0; i < 16; i++) {
690 xform[i] = result[i];
691 }
692 }
693 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
694 float result[16];
695 mtxMul(result, xform, mtxFlipV);
696 for (int i = 0; i < 16; i++) {
697 xform[i] = result[i];
698 }
699 }
700 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
701 float result[16];
702 mtxMul(result, xform, mtxRot90);
703 for (int i = 0; i < 16; i++) {
704 xform[i] = result[i];
705 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800706 }
707
708 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800709 float tx, ty, sx, sy;
710 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800711 // In order to prevent bilinear sampling at the of the crop rectangle we
712 // may need to shrink it by 2 texels in each direction. Normally this
713 // would just need to take 1/2 a texel off each end, but because the
714 // chroma channels will likely be subsampled we need to chop off a whole
715 // texel. This will cause artifacts if someone does nearest sampling
716 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
717 // accomodate the bilinear and nearest sampling uses.
718 //
719 // If nearest sampling turns out to be a desirable usage of these
720 // textures then we could add the ability to switch a SurfaceTexture to
721 // nearest-mode. Preferably, however, the image producers (video
722 // decoder, camera, etc.) would simply not use a crop rectangle (or at
723 // least not tell the framework about it) so that the GPU can do the
724 // correct edge behavior.
725 int xshrink = 0, yshrink = 0;
726 if (mCurrentCrop.left > 0) {
727 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
728 xshrink++;
729 } else {
730 tx = 0.0f;
731 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700732 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800733 xshrink++;
734 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700735 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800736 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
737 float(buf->getHeight());
738 yshrink++;
739 } else {
740 ty = 0.0f;
741 }
742 if (mCurrentCrop.top > 0) {
743 yshrink++;
744 }
745 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
746 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800747 } else {
748 tx = 0.0f;
749 ty = 0.0f;
750 sx = 1.0f;
751 sy = 1.0f;
752 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800753 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800754 sx, 0, 0, 0,
755 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800756 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800757 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800758 };
759
Jamie Gennisa214c642011-01-14 13:53:31 -0800760 float mtxBeforeFlipV[16];
761 mtxMul(mtxBeforeFlipV, crop, xform);
762
763 // SurfaceFlinger expects the top of its window textures to be at a Y
764 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
765 // want to expose this to applications, however, so we must add an
766 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700767 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800768}
769
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800770nsecs_t SurfaceTexture::getTimestamp() {
771 LOGV("SurfaceTexture::getTimestamp");
772 Mutex::Autolock lock(mMutex);
773 return mCurrentTimestamp;
774}
775
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800776void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700777 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800778 LOGV("SurfaceTexture::setFrameAvailableListener");
779 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700780 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800781}
782
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800783sp<IBinder> SurfaceTexture::getAllocator() {
784 LOGV("SurfaceTexture::getAllocator");
785 return mGraphicBufferAlloc->asBinder();
786}
787
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800788void SurfaceTexture::freeAllBuffers() {
789 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
790 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700791 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800792 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
793 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
794 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
795 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
796 }
797 }
798}
799
800EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
801 const sp<GraphicBuffer>& graphicBuffer) {
802 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
803 EGLint attrs[] = {
804 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
805 EGL_NONE,
806 };
807 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
808 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700809 if (image == EGL_NO_IMAGE_KHR) {
810 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800811 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800812 }
813 return image;
814}
815
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700816sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
817 Mutex::Autolock lock(mMutex);
818 return mCurrentTextureBuf;
819}
820
821Rect SurfaceTexture::getCurrentCrop() const {
822 Mutex::Autolock lock(mMutex);
823 return mCurrentCrop;
824}
825
826uint32_t SurfaceTexture::getCurrentTransform() const {
827 Mutex::Autolock lock(mMutex);
828 return mCurrentTransform;
829}
830
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700831uint32_t SurfaceTexture::getCurrentScalingMode() const {
832 Mutex::Autolock lock(mMutex);
833 return mCurrentScalingMode;
834}
835
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700836int SurfaceTexture::query(int what, int* outValue)
837{
838 Mutex::Autolock lock(mMutex);
839 int value;
840 switch (what) {
841 case NATIVE_WINDOW_WIDTH:
842 value = mDefaultWidth;
843 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
844 value = mCurrentTextureBuf->width;
845 break;
846 case NATIVE_WINDOW_HEIGHT:
847 value = mDefaultHeight;
848 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
849 value = mCurrentTextureBuf->height;
850 break;
851 case NATIVE_WINDOW_FORMAT:
852 value = mPixelFormat;
853 break;
854 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
855 value = mSynchronousMode ?
856 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
857 break;
858 default:
859 return BAD_VALUE;
860 }
861 outValue[0] = value;
862 return NO_ERROR;
863}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700864
Mathias Agopian68c77942011-05-09 19:08:33 -0700865void SurfaceTexture::dump(String8& result) const
866{
867 char buffer[1024];
868 dump(result, "", buffer, 1024);
869}
870
871void SurfaceTexture::dump(String8& result, const char* prefix,
872 char* buffer, size_t SIZE) const
873{
874 Mutex::Autolock _l(mMutex);
875 snprintf(buffer, SIZE,
876 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
877 "mPixelFormat=%d, mTexName=%d\n",
878 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
879 mPixelFormat, mTexName);
880 result.append(buffer);
881
882 String8 fifo;
883 int fifoSize = 0;
884 Fifo::const_iterator i(mQueue.begin());
885 while (i != mQueue.end()) {
886 snprintf(buffer, SIZE, "%02d ", *i++);
887 fifoSize++;
888 fifo.append(buffer);
889 }
890
891 snprintf(buffer, SIZE,
892 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
893 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
894 ,
895 prefix, mCurrentCrop.left,
896 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
897 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
898 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
899 mCurrentTransform, fifoSize, fifo.string()
900 );
901 result.append(buffer);
902
903 struct {
904 const char * operator()(int state) const {
905 switch (state) {
906 case BufferSlot::DEQUEUED: return "DEQUEUED";
907 case BufferSlot::QUEUED: return "QUEUED";
908 case BufferSlot::FREE: return "FREE";
909 default: return "Unknown";
910 }
911 }
912 } stateName;
913
914 for (int i=0 ; i<mBufferCount ; i++) {
915 const BufferSlot& slot(mSlots[i]);
916 snprintf(buffer, SIZE,
917 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700918 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700919 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700920 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
921 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700922 );
923 result.append(buffer);
924 }
925}
926
Jamie Gennisf238e282011-01-09 16:33:17 -0800927static void mtxMul(float out[16], const float a[16], const float b[16]) {
928 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
929 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
930 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
931 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
932
933 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
934 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
935 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
936 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
937
938 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
939 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
940 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
941 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
942
943 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
944 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
945 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
946 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
947}
948
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800949}; // namespace android