blob: 056190916414bb595073b71cbb0ad6dfc6ba1512 [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
Mathias Agopianc21180e2011-11-16 15:59:13 -080039
40#define ALLOW_DEQUEUE_CURRENT_BUFFER false
Mathias Agopian29b57622011-08-17 15:42:04 -070041
Jamie Gennisfa28c352011-09-16 17:30:26 -070042// Macros for including the SurfaceTexture name in log messages
43#define ST_LOGV(x, ...) LOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
44#define ST_LOGD(x, ...) LOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
45#define ST_LOGI(x, ...) LOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
46#define ST_LOGW(x, ...) LOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
47#define ST_LOGE(x, ...) LOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070048
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080049namespace android {
50
Jamie Gennisf238e282011-01-09 16:33:17 -080051// Transform matrices
52static float mtxIdentity[16] = {
53 1, 0, 0, 0,
54 0, 1, 0, 0,
55 0, 0, 1, 0,
56 0, 0, 0, 1,
57};
58static float mtxFlipH[16] = {
59 -1, 0, 0, 0,
60 0, 1, 0, 0,
61 0, 0, 1, 0,
62 1, 0, 0, 1,
63};
64static float mtxFlipV[16] = {
65 1, 0, 0, 0,
66 0, -1, 0, 0,
67 0, 0, 1, 0,
68 0, 1, 0, 1,
69};
70static float mtxRot90[16] = {
71 0, 1, 0, 0,
72 -1, 0, 0, 0,
73 0, 0, 1, 0,
74 1, 0, 0, 1,
75};
76static float mtxRot180[16] = {
77 -1, 0, 0, 0,
78 0, -1, 0, 0,
79 0, 0, 1, 0,
80 1, 1, 0, 1,
81};
82static float mtxRot270[16] = {
83 0, -1, 0, 0,
84 1, 0, 0, 0,
85 0, 0, 1, 0,
86 0, 1, 0, 1,
87};
88
89static void mtxMul(float out[16], const float a[16], const float b[16]);
90
Jamie Gennisfa28c352011-09-16 17:30:26 -070091// Get an ID that's unique within this process.
92static int32_t createProcessUniqueId() {
93 static volatile int32_t globalCounter = 0;
94 return android_atomic_inc(&globalCounter);
95}
96
Jamie Gennisfb1b5a22011-09-28 12:13:31 -070097SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
98 GLenum texTarget) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070099 mDefaultWidth(1),
100 mDefaultHeight(1),
101 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -0700102 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
103 mClientBufferCount(0),
104 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800105 mCurrentTexture(INVALID_BUFFER_SLOT),
106 mCurrentTransform(0),
107 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800108 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700109 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700110 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -0700111 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700112 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700113 mConnectedApi(NO_CONNECTED_API),
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700114 mAbandoned(false),
Sunita Nadampallia9297482011-11-09 18:23:41 -0600115 mTexTarget(texTarget),
116 mFrameCounter(0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700117 // Choose a name using the PID and a process-unique ID.
118 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
119
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700120 ST_LOGV("SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -0800121 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
122 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700123 mNextCrop.makeInvalid();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700124 memcpy(mCurrentTransformMatrix, mtxIdentity,
125 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800126}
127
128SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700129 ST_LOGV("~SurfaceTexture");
Mathias Agopianef51b992011-08-10 15:28:58 -0700130 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131}
132
Mathias Agopian80727112011-05-02 19:51:12 -0700133status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
134 if (bufferCount > NUM_BUFFER_SLOTS)
135 return BAD_VALUE;
136
137 // special-case, nothing to do
138 if (bufferCount == mBufferCount)
139 return OK;
140
141 if (!mClientBufferCount &&
142 bufferCount >= mBufferCount) {
143 // easy, we just have more buffers
144 mBufferCount = bufferCount;
145 mServerBufferCount = bufferCount;
146 mDequeueCondition.signal();
147 } else {
148 // we're here because we're either
149 // - reducing the number of available buffers
150 // - or there is a client-buffer-count in effect
151
152 // less than 2 buffers is never allowed
153 if (bufferCount < 2)
154 return BAD_VALUE;
155
156 // when there is non client-buffer-count in effect, the client is not
157 // allowed to dequeue more than one buffer at a time,
158 // so the next time they dequeue a buffer, we know that they don't
159 // own one. the actual resizing will happen during the next
160 // dequeueBuffer.
161
162 mServerBufferCount = bufferCount;
163 }
164 return OK;
165}
166
167status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
168 Mutex::Autolock lock(mMutex);
169 return setBufferCountServerLocked(bufferCount);
170}
171
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800172status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700173 ST_LOGV("setBufferCount: count=%d", bufferCount);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700174 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800175
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700176 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700177 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700178 return NO_INIT;
179 }
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700180 if (bufferCount > NUM_BUFFER_SLOTS) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700181 ST_LOGE("setBufferCount: bufferCount larger than slots available");
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700182 return BAD_VALUE;
183 }
184
Mathias Agopian80727112011-05-02 19:51:12 -0700185 // Error out if the user has dequeued buffers
186 for (int i=0 ; i<mBufferCount ; i++) {
187 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700188 ST_LOGE("setBufferCount: client owns some buffers");
Mathias Agopian80727112011-05-02 19:51:12 -0700189 return -EINVAL;
190 }
191 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700192
Jamie Gennis1c121f62011-07-30 16:00:11 -0700193 const int minBufferSlots = mSynchronousMode ?
194 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian80727112011-05-02 19:51:12 -0700195 if (bufferCount == 0) {
Mathias Agopian80727112011-05-02 19:51:12 -0700196 mClientBufferCount = 0;
197 bufferCount = (mServerBufferCount >= minBufferSlots) ?
198 mServerBufferCount : minBufferSlots;
199 return setBufferCountServerLocked(bufferCount);
200 }
201
Jamie Gennis1c121f62011-07-30 16:00:11 -0700202 if (bufferCount < minBufferSlots) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700203 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
Jamie Gennis1c121f62011-07-30 16:00:11 -0700204 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800205 return BAD_VALUE;
206 }
207
Mathias Agopian80727112011-05-02 19:51:12 -0700208 // here we're guaranteed that the client doesn't have dequeued buffers
209 // and will release all of its buffer references.
Mathias Agopianef51b992011-08-10 15:28:58 -0700210 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800211 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700212 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800213 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700214 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700215 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800216 return OK;
217}
218
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700219status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
220{
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700221 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700222 if (!w || !h) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700223 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
224 w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700225 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700226 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700227
228 Mutex::Autolock lock(mMutex);
229 mDefaultWidth = w;
230 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700231 return OK;
232}
233
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700234status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700235 ST_LOGV("requestBuffer: slot=%d", slot);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700237 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700238 ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700239 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800240 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700241 if (slot < 0 || mBufferCount <= slot) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700242 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700243 mBufferCount, slot);
244 return BAD_VALUE;
245 }
246 mSlots[slot].mRequestBufferCalled = true;
247 *buf = mSlots[slot].mGraphicBuffer;
248 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700249}
250
251status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
252 uint32_t format, uint32_t usage) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700253 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700254
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700255 if ((w && !h) || (!w && h)) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700256 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700257 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700258 }
259
Mathias Agopianc04f1532011-04-25 20:22:14 -0700260 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700261
262 status_t returnFlags(OK);
263
Sunita Nadampallia9297482011-11-09 18:23:41 -0600264 int found = -1;
265 int foundSync = -1;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700266 int dequeuedCount = 0;
267 bool tryAgain = true;
268 while (tryAgain) {
Mathias Agopian2560d142011-08-10 16:33:23 -0700269 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700270 ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
Mathias Agopian2560d142011-08-10 16:33:23 -0700271 return NO_INIT;
272 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700273
Mathias Agopian80727112011-05-02 19:51:12 -0700274 // We need to wait for the FIFO to drain if the number of buffer
275 // needs to change.
276 //
Mathias Agopian2560d142011-08-10 16:33:23 -0700277 // The condition "number of buffers needs to change" is true if
Mathias Agopian80727112011-05-02 19:51:12 -0700278 // - the client doesn't care about how many buffers there are
279 // - AND the actual number of buffer is different from what was
280 // set in the last setBufferCountServer()
281 // - OR -
282 // setBufferCountServer() was set to a value incompatible with
283 // the synchronization mode (for instance because the sync mode
284 // changed since)
285 //
286 // As long as this condition is true AND the FIFO is not empty, we
287 // wait on mDequeueCondition.
288
Mathias Agopian2560d142011-08-10 16:33:23 -0700289 const int minBufferCountNeeded = mSynchronousMode ?
Mathias Agopian80727112011-05-02 19:51:12 -0700290 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
291
Mathias Agopian2560d142011-08-10 16:33:23 -0700292 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
Mathias Agopian80727112011-05-02 19:51:12 -0700293 ((mServerBufferCount != mBufferCount) ||
Mathias Agopian2560d142011-08-10 16:33:23 -0700294 (mServerBufferCount < minBufferCountNeeded));
295
296 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700297 // wait for the FIFO to drain
Mathias Agopian2560d142011-08-10 16:33:23 -0700298 mDequeueCondition.wait(mMutex);
299 // NOTE: we continue here because we need to reevaluate our
300 // whole state (eg: we could be abandoned or disconnected)
301 continue;
Mathias Agopian80727112011-05-02 19:51:12 -0700302 }
303
Mathias Agopian2560d142011-08-10 16:33:23 -0700304 if (numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700305 // here we're guaranteed that mQueue is empty
Mathias Agopianef51b992011-08-10 15:28:58 -0700306 freeAllBuffersLocked();
Mathias Agopian80727112011-05-02 19:51:12 -0700307 mBufferCount = mServerBufferCount;
308 if (mBufferCount < minBufferCountNeeded)
309 mBufferCount = minBufferCountNeeded;
310 mCurrentTexture = INVALID_BUFFER_SLOT;
311 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
312 }
313
314 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700315 found = INVALID_BUFFER_SLOT;
316 foundSync = INVALID_BUFFER_SLOT;
317 dequeuedCount = 0;
318 for (int i = 0; i < mBufferCount; i++) {
319 const int state = mSlots[i].mBufferState;
320 if (state == BufferSlot::DEQUEUED) {
321 dequeuedCount++;
322 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700323
324 // if buffer is FREE it CANNOT be current
325 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
326 "dequeueBuffer: buffer %d is both FREE and current!", i);
327
Mathias Agopianc21180e2011-11-16 15:59:13 -0800328 if (ALLOW_DEQUEUE_CURRENT_BUFFER) {
Mathias Agopian29b57622011-08-17 15:42:04 -0700329 if (state == BufferSlot::FREE || i == mCurrentTexture) {
330 foundSync = i;
331 if (i != mCurrentTexture) {
332 found = i;
333 break;
334 }
335 }
336 } else {
337 if (state == BufferSlot::FREE) {
Sunita Nadampallia9297482011-11-09 18:23:41 -0600338 /** For Asynchronous mode, we need to return the oldest of free buffers
339 * There is only one instance when the Framecounter overflows, this logic
340 * might return the earlier buffer to client. Which is a negligible impact
341 **/
342 if (found < 0 || mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
343 foundSync = i;
344 found = i;
345 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700346 }
347 }
348 }
Mathias Agopian80727112011-05-02 19:51:12 -0700349
350 // clients are not allowed to dequeue more than one buffer
351 // if they didn't set a buffer count.
352 if (!mClientBufferCount && dequeuedCount) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700353 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
354 "setting the buffer count");
Mathias Agopian80727112011-05-02 19:51:12 -0700355 return -EINVAL;
356 }
357
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700358 // See whether a buffer has been queued since the last setBufferCount so
359 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
360 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
361 if (bufferHasBeenQueued) {
362 // make sure the client is not trying to dequeue more buffers
363 // than allowed.
364 const int avail = mBufferCount - (dequeuedCount+1);
365 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700366 ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
367 "(dequeued=%d)",
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700368 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
369 dequeuedCount);
370 return -EBUSY;
371 }
Mathias Agopian80727112011-05-02 19:51:12 -0700372 }
373
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700374 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian29b57622011-08-17 15:42:04 -0700375 // for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700376 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
377 if (tryAgain) {
378 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700379 }
380 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700381
Mathias Agopian80727112011-05-02 19:51:12 -0700382 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
383 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
384 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700385 }
386
Mathias Agopianc04f1532011-04-25 20:22:14 -0700387 if (found == INVALID_BUFFER_SLOT) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700388 // This should not happen.
389 ST_LOGE("dequeueBuffer: no available buffer slots");
Mathias Agopianc04f1532011-04-25 20:22:14 -0700390 return -EBUSY;
391 }
392
393 const int buf = found;
394 *outBuf = found;
395
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700396 const bool useDefaultSize = !w && !h;
397 if (useDefaultSize) {
398 // use the default size
399 w = mDefaultWidth;
400 h = mDefaultHeight;
401 }
402
403 const bool updateFormat = (format != 0);
404 if (!updateFormat) {
405 // keep the current (or default) format
406 format = mPixelFormat;
407 }
408
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700409 // buffer is now in DEQUEUED (but can also be current at the same time,
410 // if we're in synchronous mode)
411 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
412
413 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700414 if ((buffer == NULL) ||
415 (uint32_t(buffer->width) != w) ||
416 (uint32_t(buffer->height) != h) ||
417 (uint32_t(buffer->format) != format) ||
418 ((uint32_t(buffer->usage) & usage) != usage))
419 {
420 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700421 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700422 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700423 mGraphicBufferAlloc->createGraphicBuffer(
424 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700425 if (graphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700426 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
427 "failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700428 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700429 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700430 if (updateFormat) {
431 mPixelFormat = format;
432 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800433 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700434 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800435 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
436 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
437 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
438 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
439 }
Jamie Gennisaaa3ecf2011-11-17 16:00:44 -0800440 if (mCurrentTexture == buf) {
441 // The current texture no longer references the buffer in this slot
442 // since we just allocated a new buffer.
443 mCurrentTexture = INVALID_BUFFER_SLOT;
444 }
Mathias Agopian80727112011-05-02 19:51:12 -0700445 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700446 }
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700447 ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", buf,
448 mSlots[buf].mGraphicBuffer->handle, returnFlags);
Mathias Agopian80727112011-05-02 19:51:12 -0700449 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800450}
451
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700452status_t SurfaceTexture::setSynchronousMode(bool enabled) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700453 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700454 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700455
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700456 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700457 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700458 return NO_INIT;
459 }
460
Mathias Agopian80727112011-05-02 19:51:12 -0700461 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700462 if (!mAllowSynchronousMode && enabled)
463 return err;
464
Mathias Agopian80727112011-05-02 19:51:12 -0700465 if (!enabled) {
466 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700467 err = drainQueueLocked();
468 if (err != NO_ERROR)
469 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700470 }
471
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700472 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700473 // - if we're going to asynchronous mode, the queue is guaranteed to be
474 // empty here
475 // - if the client set the number of buffers, we're guaranteed that
476 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700477 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700478 mDequeueCondition.signal();
479 }
Mathias Agopian80727112011-05-02 19:51:12 -0700480 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700481}
482
Mathias Agopian97c602c2011-07-19 15:24:46 -0700483status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
484 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700485 ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700486
487 sp<FrameAvailableListener> listener;
488
489 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700490 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700491 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700492 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700493 return NO_INIT;
494 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700495 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700496 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700497 mBufferCount, buf);
498 return -EINVAL;
499 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700500 ST_LOGE("queueBuffer: slot %d is not owned by the client "
501 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700502 return -EINVAL;
503 } else if (buf == mCurrentTexture) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700504 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700505 return -EINVAL;
506 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700507 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700508 "buffer", buf);
509 return -EINVAL;
510 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700511
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700512 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700513 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700514 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700515
516 // Synchronous mode always signals that an additional frame should
517 // be consumed.
518 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700519 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700520 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700521 if (mQueue.empty()) {
522 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700523
524 // Asynchronous mode only signals that a frame should be
525 // consumed if no previous frame was pending. If a frame were
526 // pending then the consumer would have already been notified.
527 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700528 } else {
529 Fifo::iterator front(mQueue.begin());
530 // buffer currently queued is freed
531 mSlots[*front].mBufferState = BufferSlot::FREE;
532 // and we record the new buffer index in the queued list
533 *front = buf;
534 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700535 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700536
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700537 mSlots[buf].mBufferState = BufferSlot::QUEUED;
538 mSlots[buf].mCrop = mNextCrop;
539 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700540 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700541 mSlots[buf].mTimestamp = timestamp;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600542 mFrameCounter++;
543 mSlots[buf].mFrameNumber = mFrameCounter;
544
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700545 mDequeueCondition.signal();
Mathias Agopian3902be62011-08-17 12:45:40 -0700546
547 *outWidth = mDefaultWidth;
548 *outHeight = mDefaultHeight;
549 *outTransform = 0;
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700550 } // scope for the lock
551
552 // call back without lock held
553 if (listener != 0) {
554 listener->onFrameAvailable();
555 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800556 return OK;
557}
558
559void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700560 ST_LOGV("cancelBuffer: slot=%d", buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800561 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700562
563 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700564 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700565 return;
566 }
567
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700568 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700569 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700570 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800571 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700572 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700573 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700574 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800575 return;
576 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700577 mSlots[buf].mBufferState = BufferSlot::FREE;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600578 mSlots[buf].mFrameNumber = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700579 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800580}
581
Jamie Gennisf238e282011-01-09 16:33:17 -0800582status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700583 ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
584 crop.bottom);
585
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800586 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700587 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700588 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700589 return NO_INIT;
590 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800591 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800592 return OK;
593}
594
595status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700596 ST_LOGV("setTransform: xform=%#x", transform);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800597 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700598 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700599 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700600 return NO_INIT;
601 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800602 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800603 return OK;
604}
605
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700606status_t SurfaceTexture::connect(int api,
607 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700608 ST_LOGV("connect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700609 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700610
611 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700612 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700613 return NO_INIT;
614 }
615
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700616 int err = NO_ERROR;
617 switch (api) {
618 case NATIVE_WINDOW_API_EGL:
619 case NATIVE_WINDOW_API_CPU:
620 case NATIVE_WINDOW_API_MEDIA:
621 case NATIVE_WINDOW_API_CAMERA:
622 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700623 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700624 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700625 err = -EINVAL;
626 } else {
627 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700628 *outWidth = mDefaultWidth;
629 *outHeight = mDefaultHeight;
630 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700631 }
632 break;
633 default:
634 err = -EINVAL;
635 break;
636 }
637 return err;
638}
639
640status_t SurfaceTexture::disconnect(int api) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700641 ST_LOGV("disconnect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700642 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700643
644 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700645 ST_LOGE("disconnect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700646 return NO_INIT;
647 }
648
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700649 int err = NO_ERROR;
650 switch (api) {
651 case NATIVE_WINDOW_API_EGL:
652 case NATIVE_WINDOW_API_CPU:
653 case NATIVE_WINDOW_API_MEDIA:
654 case NATIVE_WINDOW_API_CAMERA:
655 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700656 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700657 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700658 mNextCrop.makeInvalid();
659 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
660 mNextTransform = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700661 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700662 } else {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700663 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700664 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700665 err = -EINVAL;
666 }
667 break;
668 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700669 ST_LOGE("disconnect: unknown API %d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700670 err = -EINVAL;
671 break;
672 }
673 return err;
674}
675
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700676status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700677 ST_LOGV("setScalingMode: mode=%d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700678
679 switch (mode) {
680 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
681 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
682 break;
683 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700684 ST_LOGE("unknown scaling mode: %d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700685 return BAD_VALUE;
686 }
687
688 Mutex::Autolock lock(mMutex);
689 mNextScalingMode = mode;
690 return OK;
691}
692
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800693status_t SurfaceTexture::updateTexImage() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700694 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800695 Mutex::Autolock lock(mMutex);
696
Mathias Agopiane47498f2011-08-08 19:35:15 -0700697 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700698 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700699 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700700 }
701
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700702 // In asynchronous mode the list is guaranteed to be one buffer
703 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700704 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700705 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700706 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700707
Jamie Gennisf238e282011-01-09 16:33:17 -0800708 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700709 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800710 if (image == EGL_NO_IMAGE_KHR) {
711 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700712 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700713 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700714 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700715 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700716 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
717 mSlots[buf].mEglImage = image;
718 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700719 if (image == EGL_NO_IMAGE_KHR) {
720 // NOTE: if dpy was invalid, createImage() is guaranteed to
721 // fail. so we'd end up here.
722 return -EINVAL;
723 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800724 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800725
726 GLint error;
727 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700728 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800729 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700730
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700731 glBindTexture(mTexTarget, mTexName);
732 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700733
Jamie Gennis0eb88512011-01-26 11:52:02 -0800734 bool failed = false;
735 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700736 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700737 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800738 failed = true;
739 }
740 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800741 return -EINVAL;
742 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800743
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700744 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
745 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, buf,
746 mSlots[buf].mGraphicBuffer->handle);
747
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700748 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700749 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700750 // state. If it has already been given to the client
751 // (synchronous mode), then it stays in DEQUEUED state.
752 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
753 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
754 }
755
Jamie Gennis9a78c902011-01-12 18:30:40 -0800756 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700757 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700758 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700759 mCurrentCrop = mSlots[buf].mCrop;
760 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700761 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700762 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700763 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700764
765 // Now that we've passed the point at which failures can happen,
766 // it's safe to remove the buffer from the front of the queue.
767 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700768 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700769 } else {
770 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700771 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800772 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700773
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800774 return OK;
775}
776
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700777bool SurfaceTexture::isExternalFormat(uint32_t format)
778{
779 switch (format) {
780 // supported YUV formats
781 case HAL_PIXEL_FORMAT_YV12:
782 // Legacy/deprecated YUV formats
783 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
784 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
785 case HAL_PIXEL_FORMAT_YCbCr_422_I:
786 return true;
787 }
788
789 // Any OEM format needs to be considered
790 if (format>=0x100 && format<=0x1FF)
791 return true;
792
793 return false;
794}
795
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700796GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700797 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700798}
799
Jamie Gennisf238e282011-01-09 16:33:17 -0800800void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800801 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700802 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
803}
804
805void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700806 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800807
Jamie Gennisa214c642011-01-14 13:53:31 -0800808 float xform[16];
809 for (int i = 0; i < 16; i++) {
810 xform[i] = mtxIdentity[i];
811 }
812 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
813 float result[16];
814 mtxMul(result, xform, mtxFlipH);
815 for (int i = 0; i < 16; i++) {
816 xform[i] = result[i];
817 }
818 }
819 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
820 float result[16];
821 mtxMul(result, xform, mtxFlipV);
822 for (int i = 0; i < 16; i++) {
823 xform[i] = result[i];
824 }
825 }
826 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
827 float result[16];
828 mtxMul(result, xform, mtxRot90);
829 for (int i = 0; i < 16; i++) {
830 xform[i] = result[i];
831 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800832 }
833
834 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800835 float tx, ty, sx, sy;
836 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800837 // In order to prevent bilinear sampling at the of the crop rectangle we
838 // may need to shrink it by 2 texels in each direction. Normally this
839 // would just need to take 1/2 a texel off each end, but because the
840 // chroma channels will likely be subsampled we need to chop off a whole
841 // texel. This will cause artifacts if someone does nearest sampling
842 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
843 // accomodate the bilinear and nearest sampling uses.
844 //
845 // If nearest sampling turns out to be a desirable usage of these
846 // textures then we could add the ability to switch a SurfaceTexture to
847 // nearest-mode. Preferably, however, the image producers (video
848 // decoder, camera, etc.) would simply not use a crop rectangle (or at
849 // least not tell the framework about it) so that the GPU can do the
850 // correct edge behavior.
851 int xshrink = 0, yshrink = 0;
852 if (mCurrentCrop.left > 0) {
853 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
854 xshrink++;
855 } else {
856 tx = 0.0f;
857 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700858 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800859 xshrink++;
860 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700861 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800862 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
863 float(buf->getHeight());
864 yshrink++;
865 } else {
866 ty = 0.0f;
867 }
868 if (mCurrentCrop.top > 0) {
869 yshrink++;
870 }
871 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
872 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800873 } else {
874 tx = 0.0f;
875 ty = 0.0f;
876 sx = 1.0f;
877 sy = 1.0f;
878 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800879 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800880 sx, 0, 0, 0,
881 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800882 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800883 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800884 };
885
Jamie Gennisa214c642011-01-14 13:53:31 -0800886 float mtxBeforeFlipV[16];
887 mtxMul(mtxBeforeFlipV, crop, xform);
888
889 // SurfaceFlinger expects the top of its window textures to be at a Y
890 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
891 // want to expose this to applications, however, so we must add an
892 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700893 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800894}
895
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800896nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700897 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800898 Mutex::Autolock lock(mMutex);
899 return mCurrentTimestamp;
900}
901
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800902void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700903 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700904 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800905 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700906 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800907}
908
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700909void SurfaceTexture::freeBufferLocked(int i) {
910 mSlots[i].mGraphicBuffer = 0;
911 mSlots[i].mBufferState = BufferSlot::FREE;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600912 mSlots[i].mFrameNumber = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700913 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
914 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
915 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
916 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
917 }
918}
919
Mathias Agopianef51b992011-08-10 15:28:58 -0700920void SurfaceTexture::freeAllBuffersLocked() {
921 LOGW_IF(!mQueue.isEmpty(),
922 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian29b57622011-08-17 15:42:04 -0700923 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800924 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700925 freeBufferLocked(i);
926 }
927}
928
929void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
930 LOGW_IF(!mQueue.isEmpty(),
931 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
932 int head = -1;
933 if (!mQueue.empty()) {
934 Fifo::iterator front(mQueue.begin());
935 head = *front;
936 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700937 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700938 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
939 if (i != head) {
940 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800941 }
942 }
943}
944
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700945status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700946 while (mSynchronousMode && !mQueue.isEmpty()) {
947 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700948 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700949 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700950 return NO_INIT;
951 }
952 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700953 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700954 return NO_INIT;
955 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700956 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700957 return NO_ERROR;
958}
959
960status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
961 status_t err = drainQueueLocked();
962 if (err == NO_ERROR) {
963 if (mSynchronousMode) {
964 freeAllBuffersLocked();
965 } else {
966 freeAllBuffersExceptHeadLocked();
967 }
968 }
969 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700970}
971
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800972EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
973 const sp<GraphicBuffer>& graphicBuffer) {
974 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
975 EGLint attrs[] = {
976 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
977 EGL_NONE,
978 };
979 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
980 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700981 if (image == EGL_NO_IMAGE_KHR) {
982 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700983 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800984 }
985 return image;
986}
987
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700988sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
989 Mutex::Autolock lock(mMutex);
990 return mCurrentTextureBuf;
991}
992
993Rect SurfaceTexture::getCurrentCrop() const {
994 Mutex::Autolock lock(mMutex);
995 return mCurrentCrop;
996}
997
998uint32_t SurfaceTexture::getCurrentTransform() const {
999 Mutex::Autolock lock(mMutex);
1000 return mCurrentTransform;
1001}
1002
Mathias Agopian7734ebf2011-07-13 15:24:42 -07001003uint32_t SurfaceTexture::getCurrentScalingMode() const {
1004 Mutex::Autolock lock(mMutex);
1005 return mCurrentScalingMode;
1006}
1007
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001008int SurfaceTexture::query(int what, int* outValue)
1009{
1010 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001011
1012 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -07001013 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001014 return NO_INIT;
1015 }
1016
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001017 int value;
1018 switch (what) {
1019 case NATIVE_WINDOW_WIDTH:
1020 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001021 break;
1022 case NATIVE_WINDOW_HEIGHT:
1023 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001024 break;
1025 case NATIVE_WINDOW_FORMAT:
1026 value = mPixelFormat;
1027 break;
1028 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1029 value = mSynchronousMode ?
1030 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
1031 break;
1032 default:
1033 return BAD_VALUE;
1034 }
1035 outValue[0] = value;
1036 return NO_ERROR;
1037}
Mathias Agopian7a042bf2011-04-11 21:19:55 -07001038
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001039void SurfaceTexture::abandon() {
1040 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001041 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001042 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -07001043 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001044 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001045 mDequeueCondition.signal();
1046}
1047
Jamie Gennisfa28c352011-09-16 17:30:26 -07001048void SurfaceTexture::setName(const String8& name) {
1049 mName = name;
1050}
1051
Mathias Agopian68c77942011-05-09 19:08:33 -07001052void SurfaceTexture::dump(String8& result) const
1053{
1054 char buffer[1024];
1055 dump(result, "", buffer, 1024);
1056}
1057
1058void SurfaceTexture::dump(String8& result, const char* prefix,
1059 char* buffer, size_t SIZE) const
1060{
1061 Mutex::Autolock _l(mMutex);
1062 snprintf(buffer, SIZE,
1063 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1064 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001065 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1066 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -07001067 result.append(buffer);
1068
1069 String8 fifo;
1070 int fifoSize = 0;
1071 Fifo::const_iterator i(mQueue.begin());
1072 while (i != mQueue.end()) {
1073 snprintf(buffer, SIZE, "%02d ", *i++);
1074 fifoSize++;
1075 fifo.append(buffer);
1076 }
1077
1078 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001079 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001080 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1081 ,
1082 prefix, mCurrentCrop.left,
1083 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001084 mCurrentTransform, mCurrentTexture,
Jamie Gennisfa28c352011-09-16 17:30:26 -07001085 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1086 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -07001087 );
1088 result.append(buffer);
1089
1090 struct {
1091 const char * operator()(int state) const {
1092 switch (state) {
1093 case BufferSlot::DEQUEUED: return "DEQUEUED";
1094 case BufferSlot::QUEUED: return "QUEUED";
1095 case BufferSlot::FREE: return "FREE";
1096 default: return "Unknown";
1097 }
1098 }
1099 } stateName;
1100
1101 for (int i=0 ; i<mBufferCount ; i++) {
1102 const BufferSlot& slot(mSlots[i]);
1103 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001104 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001105 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001106 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001107 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001108 stateName(slot.mBufferState),
Jamie Gennisfa28c352011-09-16 17:30:26 -07001109 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1110 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001111 );
1112 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001113
1114 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1115 if (buf != NULL) {
1116 snprintf(buffer, SIZE,
1117 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001118 buf->handle, buf->width, buf->height, buf->stride,
1119 buf->format);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001120 result.append(buffer);
1121 }
1122 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001123 }
1124}
1125
Jamie Gennisf238e282011-01-09 16:33:17 -08001126static void mtxMul(float out[16], const float a[16], const float b[16]) {
1127 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1128 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1129 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1130 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1131
1132 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1133 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1134 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1135 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1136
1137 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1138 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1139 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1140 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1141
1142 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1143 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1144 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1145 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1146}
1147
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001148}; // namespace android