blob: 14104810f8bc6e59f0a459882deba4b00ae83838 [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) {
498 LOGV("SurfaceTexture::connect");
499 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) {
507 err = -EINVAL;
508 } else {
509 mConnectedApi = api;
510 }
511 break;
512 default:
513 err = -EINVAL;
514 break;
515 }
516 return err;
517}
518
519status_t SurfaceTexture::disconnect(int api) {
520 LOGV("SurfaceTexture::disconnect");
521 Mutex::Autolock lock(mMutex);
522 int err = NO_ERROR;
523 switch (api) {
524 case NATIVE_WINDOW_API_EGL:
525 case NATIVE_WINDOW_API_CPU:
526 case NATIVE_WINDOW_API_MEDIA:
527 case NATIVE_WINDOW_API_CAMERA:
528 if (mConnectedApi == api) {
529 mConnectedApi = NO_CONNECTED_API;
530 } else {
531 err = -EINVAL;
532 }
533 break;
534 default:
535 err = -EINVAL;
536 break;
537 }
538 return err;
539}
540
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800541status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800542 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800543 Mutex::Autolock lock(mMutex);
544
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700545 // In asynchronous mode the list is guaranteed to be one buffer
546 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700547 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700548 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700549 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700550
Jamie Gennisf238e282011-01-09 16:33:17 -0800551 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700552 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800553 if (image == EGL_NO_IMAGE_KHR) {
554 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700555 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
556 mSlots[buf].mEglImage = image;
557 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700558 if (image == EGL_NO_IMAGE_KHR) {
559 // NOTE: if dpy was invalid, createImage() is guaranteed to
560 // fail. so we'd end up here.
561 return -EINVAL;
562 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800563 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800564
565 GLint error;
566 while ((error = glGetError()) != GL_NO_ERROR) {
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700567 LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800568 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700569
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700570 GLenum target = getTextureTarget(mSlots[buf].mGraphicBuffer->format);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700571 if (target != mCurrentTextureTarget) {
572 glDeleteTextures(1, &mTexName);
573 }
574 glBindTexture(target, mTexName);
575 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
576
Jamie Gennis0eb88512011-01-26 11:52:02 -0800577 bool failed = false;
578 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800579 LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700580 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800581 failed = true;
582 }
583 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800584 return -EINVAL;
585 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800586
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700587 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700588 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700589 // state. If it has already been given to the client
590 // (synchronous mode), then it stays in DEQUEUED state.
591 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
592 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
593 }
594
Jamie Gennis9a78c902011-01-12 18:30:40 -0800595 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700596 mCurrentTexture = buf;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700597 mCurrentTextureTarget = target;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700598 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700599 mCurrentCrop = mSlots[buf].mCrop;
600 mCurrentTransform = mSlots[buf].mTransform;
601 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700602 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700603
604 // Now that we've passed the point at which failures can happen,
605 // it's safe to remove the buffer from the front of the queue.
606 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700607 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700608 } else {
609 // We always bind the texture even if we don't update its contents.
610 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800611 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700612
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800613 return OK;
614}
615
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700616bool SurfaceTexture::isExternalFormat(uint32_t format)
617{
618 switch (format) {
619 // supported YUV formats
620 case HAL_PIXEL_FORMAT_YV12:
621 // Legacy/deprecated YUV formats
622 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
623 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
624 case HAL_PIXEL_FORMAT_YCbCr_422_I:
625 return true;
626 }
627
628 // Any OEM format needs to be considered
629 if (format>=0x100 && format<=0x1FF)
630 return true;
631
632 return false;
633}
634
635GLenum SurfaceTexture::getTextureTarget(uint32_t format)
636{
637 GLenum target = GL_TEXTURE_2D;
638#if defined(GL_OES_EGL_image_external)
639 if (isExternalFormat(format)) {
640 target = GL_TEXTURE_EXTERNAL_OES;
641 }
642#endif
643 return target;
644}
645
646GLenum SurfaceTexture::getCurrentTextureTarget() const {
647 Mutex::Autolock lock(mMutex);
648 return mCurrentTextureTarget;
649}
650
Jamie Gennisf238e282011-01-09 16:33:17 -0800651void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800652 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700653 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
654}
655
656void SurfaceTexture::computeCurrentTransformMatrix() {
657 LOGV("SurfaceTexture::computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800658
Jamie Gennisa214c642011-01-14 13:53:31 -0800659 float xform[16];
660 for (int i = 0; i < 16; i++) {
661 xform[i] = mtxIdentity[i];
662 }
663 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
664 float result[16];
665 mtxMul(result, xform, mtxFlipH);
666 for (int i = 0; i < 16; i++) {
667 xform[i] = result[i];
668 }
669 }
670 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
671 float result[16];
672 mtxMul(result, xform, mtxFlipV);
673 for (int i = 0; i < 16; i++) {
674 xform[i] = result[i];
675 }
676 }
677 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
678 float result[16];
679 mtxMul(result, xform, mtxRot90);
680 for (int i = 0; i < 16; i++) {
681 xform[i] = result[i];
682 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800683 }
684
685 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800686 float tx, ty, sx, sy;
687 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800688 // In order to prevent bilinear sampling at the of the crop rectangle we
689 // may need to shrink it by 2 texels in each direction. Normally this
690 // would just need to take 1/2 a texel off each end, but because the
691 // chroma channels will likely be subsampled we need to chop off a whole
692 // texel. This will cause artifacts if someone does nearest sampling
693 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
694 // accomodate the bilinear and nearest sampling uses.
695 //
696 // If nearest sampling turns out to be a desirable usage of these
697 // textures then we could add the ability to switch a SurfaceTexture to
698 // nearest-mode. Preferably, however, the image producers (video
699 // decoder, camera, etc.) would simply not use a crop rectangle (or at
700 // least not tell the framework about it) so that the GPU can do the
701 // correct edge behavior.
702 int xshrink = 0, yshrink = 0;
703 if (mCurrentCrop.left > 0) {
704 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
705 xshrink++;
706 } else {
707 tx = 0.0f;
708 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700709 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800710 xshrink++;
711 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700712 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800713 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
714 float(buf->getHeight());
715 yshrink++;
716 } else {
717 ty = 0.0f;
718 }
719 if (mCurrentCrop.top > 0) {
720 yshrink++;
721 }
722 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
723 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800724 } else {
725 tx = 0.0f;
726 ty = 0.0f;
727 sx = 1.0f;
728 sy = 1.0f;
729 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800730 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800731 sx, 0, 0, 0,
732 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800733 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800734 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800735 };
736
Jamie Gennisa214c642011-01-14 13:53:31 -0800737 float mtxBeforeFlipV[16];
738 mtxMul(mtxBeforeFlipV, crop, xform);
739
740 // SurfaceFlinger expects the top of its window textures to be at a Y
741 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
742 // want to expose this to applications, however, so we must add an
743 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700744 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800745}
746
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800747nsecs_t SurfaceTexture::getTimestamp() {
748 LOGV("SurfaceTexture::getTimestamp");
749 Mutex::Autolock lock(mMutex);
750 return mCurrentTimestamp;
751}
752
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800753void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700754 const sp<FrameAvailableListener>& listener) {
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800755 LOGV("SurfaceTexture::setFrameAvailableListener");
756 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700757 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800758}
759
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800760sp<IBinder> SurfaceTexture::getAllocator() {
761 LOGV("SurfaceTexture::getAllocator");
762 return mGraphicBufferAlloc->asBinder();
763}
764
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800765void SurfaceTexture::freeAllBuffers() {
766 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
767 mSlots[i].mGraphicBuffer = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700768 mSlots[i].mBufferState = BufferSlot::FREE;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800769 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
770 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
771 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
772 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
773 }
774 }
775}
776
777EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
778 const sp<GraphicBuffer>& graphicBuffer) {
779 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
780 EGLint attrs[] = {
781 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
782 EGL_NONE,
783 };
784 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
785 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700786 if (image == EGL_NO_IMAGE_KHR) {
787 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800788 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800789 }
790 return image;
791}
792
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700793sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
794 Mutex::Autolock lock(mMutex);
795 return mCurrentTextureBuf;
796}
797
798Rect SurfaceTexture::getCurrentCrop() const {
799 Mutex::Autolock lock(mMutex);
800 return mCurrentCrop;
801}
802
803uint32_t SurfaceTexture::getCurrentTransform() const {
804 Mutex::Autolock lock(mMutex);
805 return mCurrentTransform;
806}
807
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700808int SurfaceTexture::query(int what, int* outValue)
809{
810 Mutex::Autolock lock(mMutex);
811 int value;
812 switch (what) {
813 case NATIVE_WINDOW_WIDTH:
814 value = mDefaultWidth;
815 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
816 value = mCurrentTextureBuf->width;
817 break;
818 case NATIVE_WINDOW_HEIGHT:
819 value = mDefaultHeight;
820 if (!mDefaultWidth && !mDefaultHeight && mCurrentTextureBuf!=0)
821 value = mCurrentTextureBuf->height;
822 break;
823 case NATIVE_WINDOW_FORMAT:
824 value = mPixelFormat;
825 break;
826 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
827 value = mSynchronousMode ?
828 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
829 break;
830 default:
831 return BAD_VALUE;
832 }
833 outValue[0] = value;
834 return NO_ERROR;
835}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700836
Mathias Agopian68c77942011-05-09 19:08:33 -0700837void SurfaceTexture::dump(String8& result) const
838{
839 char buffer[1024];
840 dump(result, "", buffer, 1024);
841}
842
843void SurfaceTexture::dump(String8& result, const char* prefix,
844 char* buffer, size_t SIZE) const
845{
846 Mutex::Autolock _l(mMutex);
847 snprintf(buffer, SIZE,
848 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
849 "mPixelFormat=%d, mTexName=%d\n",
850 prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
851 mPixelFormat, mTexName);
852 result.append(buffer);
853
854 String8 fifo;
855 int fifoSize = 0;
856 Fifo::const_iterator i(mQueue.begin());
857 while (i != mQueue.end()) {
858 snprintf(buffer, SIZE, "%02d ", *i++);
859 fifoSize++;
860 fifo.append(buffer);
861 }
862
863 snprintf(buffer, SIZE,
864 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d, target=0x%04x}\n"
865 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
866 ,
867 prefix, mCurrentCrop.left,
868 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
869 mCurrentTransform, mCurrentTexture, mCurrentTextureTarget,
870 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
871 mCurrentTransform, fifoSize, fifo.string()
872 );
873 result.append(buffer);
874
875 struct {
876 const char * operator()(int state) const {
877 switch (state) {
878 case BufferSlot::DEQUEUED: return "DEQUEUED";
879 case BufferSlot::QUEUED: return "QUEUED";
880 case BufferSlot::FREE: return "FREE";
881 default: return "Unknown";
882 }
883 }
884 } stateName;
885
886 for (int i=0 ; i<mBufferCount ; i++) {
887 const BufferSlot& slot(mSlots[i]);
888 snprintf(buffer, SIZE,
889 "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700890 "timestamp=%lld\n",
Mathias Agopian68c77942011-05-09 19:08:33 -0700891 prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700892 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
893 slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700894 );
895 result.append(buffer);
896 }
897}
898
Jamie Gennisf238e282011-01-09 16:33:17 -0800899static void mtxMul(float out[16], const float a[16], const float b[16]) {
900 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
901 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
902 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
903 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
904
905 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
906 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
907 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
908 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
909
910 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
911 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
912 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
913 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
914
915 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
916 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
917 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
918 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
919}
920
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800921}; // namespace android