blob: ee54bf4f37d0d25e14ff13072c127eebf72010a7 [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 Agopian251ce852011-11-14 19:17:37 -080039#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
40#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER true
41#warning "ALLOW_DEQUEUE_CURRENT_BUFFER enabled"
42#else
43#define FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER false
44#endif
Mathias Agopian29b57622011-08-17 15:42:04 -070045
Jamie Gennisfa28c352011-09-16 17:30:26 -070046// Macros for including the SurfaceTexture name in log messages
47#define ST_LOGV(x, ...) LOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
48#define ST_LOGD(x, ...) LOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
49#define ST_LOGI(x, ...) LOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
50#define ST_LOGW(x, ...) LOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
51#define ST_LOGE(x, ...) LOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070052
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080053namespace android {
54
Jamie Gennisf238e282011-01-09 16:33:17 -080055// Transform matrices
56static float mtxIdentity[16] = {
57 1, 0, 0, 0,
58 0, 1, 0, 0,
59 0, 0, 1, 0,
60 0, 0, 0, 1,
61};
62static float mtxFlipH[16] = {
63 -1, 0, 0, 0,
64 0, 1, 0, 0,
65 0, 0, 1, 0,
66 1, 0, 0, 1,
67};
68static float mtxFlipV[16] = {
69 1, 0, 0, 0,
70 0, -1, 0, 0,
71 0, 0, 1, 0,
72 0, 1, 0, 1,
73};
74static float mtxRot90[16] = {
75 0, 1, 0, 0,
76 -1, 0, 0, 0,
77 0, 0, 1, 0,
78 1, 0, 0, 1,
79};
80static float mtxRot180[16] = {
81 -1, 0, 0, 0,
82 0, -1, 0, 0,
83 0, 0, 1, 0,
84 1, 1, 0, 1,
85};
86static float mtxRot270[16] = {
87 0, -1, 0, 0,
88 1, 0, 0, 0,
89 0, 0, 1, 0,
90 0, 1, 0, 1,
91};
92
93static void mtxMul(float out[16], const float a[16], const float b[16]);
94
Jamie Gennisfa28c352011-09-16 17:30:26 -070095// Get an ID that's unique within this process.
96static int32_t createProcessUniqueId() {
97 static volatile int32_t globalCounter = 0;
98 return android_atomic_inc(&globalCounter);
99}
100
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700101SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
102 GLenum texTarget) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700103 mDefaultWidth(1),
104 mDefaultHeight(1),
105 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian80727112011-05-02 19:51:12 -0700106 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
107 mClientBufferCount(0),
108 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800109 mCurrentTexture(INVALID_BUFFER_SLOT),
110 mCurrentTransform(0),
111 mCurrentTimestamp(0),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800112 mNextTransform(0),
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700113 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700114 mTexName(tex),
Grace Kloba14a0e582011-06-23 21:21:47 -0700115 mSynchronousMode(false),
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700116 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700117 mConnectedApi(NO_CONNECTED_API),
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700118 mAbandoned(false),
Sunita Nadampallia9297482011-11-09 18:23:41 -0600119 mTexTarget(texTarget),
120 mFrameCounter(0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700121 // Choose a name using the PID and a process-unique ID.
122 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
123
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700124 ST_LOGV("SurfaceTexture");
Jamie Gennis9a78c902011-01-12 18:30:40 -0800125 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
126 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700127 mNextCrop.makeInvalid();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700128 memcpy(mCurrentTransformMatrix, mtxIdentity,
129 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800130}
131
132SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700133 ST_LOGV("~SurfaceTexture");
Mathias Agopianef51b992011-08-10 15:28:58 -0700134 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135}
136
Mathias Agopian80727112011-05-02 19:51:12 -0700137status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
138 if (bufferCount > NUM_BUFFER_SLOTS)
139 return BAD_VALUE;
140
141 // special-case, nothing to do
142 if (bufferCount == mBufferCount)
143 return OK;
144
145 if (!mClientBufferCount &&
146 bufferCount >= mBufferCount) {
147 // easy, we just have more buffers
148 mBufferCount = bufferCount;
149 mServerBufferCount = bufferCount;
150 mDequeueCondition.signal();
151 } else {
152 // we're here because we're either
153 // - reducing the number of available buffers
154 // - or there is a client-buffer-count in effect
155
156 // less than 2 buffers is never allowed
157 if (bufferCount < 2)
158 return BAD_VALUE;
159
160 // when there is non client-buffer-count in effect, the client is not
161 // allowed to dequeue more than one buffer at a time,
162 // so the next time they dequeue a buffer, we know that they don't
163 // own one. the actual resizing will happen during the next
164 // dequeueBuffer.
165
166 mServerBufferCount = bufferCount;
167 }
168 return OK;
169}
170
171status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
172 Mutex::Autolock lock(mMutex);
173 return setBufferCountServerLocked(bufferCount);
174}
175
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800176status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700177 ST_LOGV("setBufferCount: count=%d", bufferCount);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700178 Mutex::Autolock lock(mMutex);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800179
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700180 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700181 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700182 return NO_INIT;
183 }
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700184 if (bufferCount > NUM_BUFFER_SLOTS) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700185 ST_LOGE("setBufferCount: bufferCount larger than slots available");
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700186 return BAD_VALUE;
187 }
188
Mathias Agopian80727112011-05-02 19:51:12 -0700189 // Error out if the user has dequeued buffers
190 for (int i=0 ; i<mBufferCount ; i++) {
191 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700192 ST_LOGE("setBufferCount: client owns some buffers");
Mathias Agopian80727112011-05-02 19:51:12 -0700193 return -EINVAL;
194 }
195 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700196
Jamie Gennis1c121f62011-07-30 16:00:11 -0700197 const int minBufferSlots = mSynchronousMode ?
198 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian80727112011-05-02 19:51:12 -0700199 if (bufferCount == 0) {
Mathias Agopian80727112011-05-02 19:51:12 -0700200 mClientBufferCount = 0;
201 bufferCount = (mServerBufferCount >= minBufferSlots) ?
202 mServerBufferCount : minBufferSlots;
203 return setBufferCountServerLocked(bufferCount);
204 }
205
Jamie Gennis1c121f62011-07-30 16:00:11 -0700206 if (bufferCount < minBufferSlots) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700207 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
Jamie Gennis1c121f62011-07-30 16:00:11 -0700208 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800209 return BAD_VALUE;
210 }
211
Mathias Agopian80727112011-05-02 19:51:12 -0700212 // here we're guaranteed that the client doesn't have dequeued buffers
213 // and will release all of its buffer references.
Mathias Agopianef51b992011-08-10 15:28:58 -0700214 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800215 mBufferCount = bufferCount;
Mathias Agopian80727112011-05-02 19:51:12 -0700216 mClientBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800217 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700218 mQueue.clear();
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700219 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800220 return OK;
221}
222
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700223status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
224{
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700225 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700226 if (!w || !h) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700227 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
228 w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700229 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700230 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700231
232 Mutex::Autolock lock(mMutex);
233 mDefaultWidth = w;
234 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700235 return OK;
236}
237
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700238status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700239 ST_LOGV("requestBuffer: slot=%d", slot);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800240 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700241 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700242 ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700243 return NO_INIT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800244 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700245 if (slot < 0 || mBufferCount <= slot) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700246 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700247 mBufferCount, slot);
248 return BAD_VALUE;
249 }
250 mSlots[slot].mRequestBufferCalled = true;
251 *buf = mSlots[slot].mGraphicBuffer;
252 return NO_ERROR;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700253}
254
255status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
256 uint32_t format, uint32_t usage) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700257 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700258
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700259 if ((w && !h) || (!w && h)) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700260 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700261 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700262 }
263
Mathias Agopianc04f1532011-04-25 20:22:14 -0700264 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700265
266 status_t returnFlags(OK);
267
Sunita Nadampallia9297482011-11-09 18:23:41 -0600268 int found = -1;
269 int foundSync = -1;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700270 int dequeuedCount = 0;
271 bool tryAgain = true;
272 while (tryAgain) {
Mathias Agopian2560d142011-08-10 16:33:23 -0700273 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700274 ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
Mathias Agopian2560d142011-08-10 16:33:23 -0700275 return NO_INIT;
276 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700277
Mathias Agopian80727112011-05-02 19:51:12 -0700278 // We need to wait for the FIFO to drain if the number of buffer
279 // needs to change.
280 //
Mathias Agopian2560d142011-08-10 16:33:23 -0700281 // The condition "number of buffers needs to change" is true if
Mathias Agopian80727112011-05-02 19:51:12 -0700282 // - the client doesn't care about how many buffers there are
283 // - AND the actual number of buffer is different from what was
284 // set in the last setBufferCountServer()
285 // - OR -
286 // setBufferCountServer() was set to a value incompatible with
287 // the synchronization mode (for instance because the sync mode
288 // changed since)
289 //
290 // As long as this condition is true AND the FIFO is not empty, we
291 // wait on mDequeueCondition.
292
Mathias Agopian2560d142011-08-10 16:33:23 -0700293 const int minBufferCountNeeded = mSynchronousMode ?
Mathias Agopian80727112011-05-02 19:51:12 -0700294 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
295
Mathias Agopian2560d142011-08-10 16:33:23 -0700296 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
Mathias Agopian80727112011-05-02 19:51:12 -0700297 ((mServerBufferCount != mBufferCount) ||
Mathias Agopian2560d142011-08-10 16:33:23 -0700298 (mServerBufferCount < minBufferCountNeeded));
299
300 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700301 // wait for the FIFO to drain
Mathias Agopian2560d142011-08-10 16:33:23 -0700302 mDequeueCondition.wait(mMutex);
303 // NOTE: we continue here because we need to reevaluate our
304 // whole state (eg: we could be abandoned or disconnected)
305 continue;
Mathias Agopian80727112011-05-02 19:51:12 -0700306 }
307
Mathias Agopian2560d142011-08-10 16:33:23 -0700308 if (numberOfBuffersNeedsToChange) {
Mathias Agopian80727112011-05-02 19:51:12 -0700309 // here we're guaranteed that mQueue is empty
Mathias Agopianef51b992011-08-10 15:28:58 -0700310 freeAllBuffersLocked();
Mathias Agopian80727112011-05-02 19:51:12 -0700311 mBufferCount = mServerBufferCount;
312 if (mBufferCount < minBufferCountNeeded)
313 mBufferCount = minBufferCountNeeded;
314 mCurrentTexture = INVALID_BUFFER_SLOT;
315 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
316 }
317
318 // look for a free buffer to give to the client
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700319 found = INVALID_BUFFER_SLOT;
320 foundSync = INVALID_BUFFER_SLOT;
321 dequeuedCount = 0;
322 for (int i = 0; i < mBufferCount; i++) {
323 const int state = mSlots[i].mBufferState;
324 if (state == BufferSlot::DEQUEUED) {
325 dequeuedCount++;
326 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700327
328 // if buffer is FREE it CANNOT be current
329 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
330 "dequeueBuffer: buffer %d is both FREE and current!", i);
331
Mathias Agopian251ce852011-11-14 19:17:37 -0800332 if (FLAG_ALLOW_DEQUEUE_CURRENT_BUFFER) {
Mathias Agopian29b57622011-08-17 15:42:04 -0700333 if (state == BufferSlot::FREE || i == mCurrentTexture) {
334 foundSync = i;
335 if (i != mCurrentTexture) {
336 found = i;
337 break;
338 }
339 }
340 } else {
341 if (state == BufferSlot::FREE) {
Sunita Nadampallia9297482011-11-09 18:23:41 -0600342 /** For Asynchronous mode, we need to return the oldest of free buffers
343 * There is only one instance when the Framecounter overflows, this logic
344 * might return the earlier buffer to client. Which is a negligible impact
345 **/
346 if (found < 0 || mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
347 foundSync = i;
348 found = i;
349 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700350 }
351 }
352 }
Mathias Agopian80727112011-05-02 19:51:12 -0700353
354 // clients are not allowed to dequeue more than one buffer
355 // if they didn't set a buffer count.
356 if (!mClientBufferCount && dequeuedCount) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700357 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
358 "setting the buffer count");
Mathias Agopian80727112011-05-02 19:51:12 -0700359 return -EINVAL;
360 }
361
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700362 // See whether a buffer has been queued since the last setBufferCount so
363 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
364 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
365 if (bufferHasBeenQueued) {
366 // make sure the client is not trying to dequeue more buffers
367 // than allowed.
368 const int avail = mBufferCount - (dequeuedCount+1);
369 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700370 ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
371 "(dequeued=%d)",
Jamie Gennisc2c8dfd2011-05-23 18:44:04 -0700372 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
373 dequeuedCount);
374 return -EBUSY;
375 }
Mathias Agopian80727112011-05-02 19:51:12 -0700376 }
377
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700378 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian29b57622011-08-17 15:42:04 -0700379 // for some buffers to be consumed
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700380 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
381 if (tryAgain) {
382 mDequeueCondition.wait(mMutex);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700383 }
384 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700385
Mathias Agopian80727112011-05-02 19:51:12 -0700386 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
387 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
388 found = foundSync;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700389 }
390
Mathias Agopianc04f1532011-04-25 20:22:14 -0700391 if (found == INVALID_BUFFER_SLOT) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700392 // This should not happen.
393 ST_LOGE("dequeueBuffer: no available buffer slots");
Mathias Agopianc04f1532011-04-25 20:22:14 -0700394 return -EBUSY;
395 }
396
397 const int buf = found;
398 *outBuf = found;
399
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700400 const bool useDefaultSize = !w && !h;
401 if (useDefaultSize) {
402 // use the default size
403 w = mDefaultWidth;
404 h = mDefaultHeight;
405 }
406
407 const bool updateFormat = (format != 0);
408 if (!updateFormat) {
409 // keep the current (or default) format
410 format = mPixelFormat;
411 }
412
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700413 // buffer is now in DEQUEUED (but can also be current at the same time,
414 // if we're in synchronous mode)
415 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
416
417 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopianc04f1532011-04-25 20:22:14 -0700418 if ((buffer == NULL) ||
419 (uint32_t(buffer->width) != w) ||
420 (uint32_t(buffer->height) != h) ||
421 (uint32_t(buffer->format) != format) ||
422 ((uint32_t(buffer->usage) & usage) != usage))
423 {
424 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700425 status_t error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700426 sp<GraphicBuffer> graphicBuffer(
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700427 mGraphicBufferAlloc->createGraphicBuffer(
428 w, h, format, usage, &error));
Mathias Agopianc04f1532011-04-25 20:22:14 -0700429 if (graphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700430 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
431 "failed");
Mathias Agopiand9e8c642011-07-01 14:53:49 -0700432 return error;
Mathias Agopianc04f1532011-04-25 20:22:14 -0700433 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700434 if (updateFormat) {
435 mPixelFormat = format;
436 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800437 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700438 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800439 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
440 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
441 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
442 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
443 }
Mathias Agopian80727112011-05-02 19:51:12 -0700444 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700445 }
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700446 ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", buf,
447 mSlots[buf].mGraphicBuffer->handle, returnFlags);
Mathias Agopian80727112011-05-02 19:51:12 -0700448 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800449}
450
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700451status_t SurfaceTexture::setSynchronousMode(bool enabled) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700452 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700453 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700454
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700455 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700456 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700457 return NO_INIT;
458 }
459
Mathias Agopian80727112011-05-02 19:51:12 -0700460 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700461 if (!mAllowSynchronousMode && enabled)
462 return err;
463
Mathias Agopian80727112011-05-02 19:51:12 -0700464 if (!enabled) {
465 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700466 err = drainQueueLocked();
467 if (err != NO_ERROR)
468 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700469 }
470
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700471 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700472 // - if we're going to asynchronous mode, the queue is guaranteed to be
473 // empty here
474 // - if the client set the number of buffers, we're guaranteed that
475 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700476 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700477 mDequeueCondition.signal();
478 }
Mathias Agopian80727112011-05-02 19:51:12 -0700479 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700480}
481
Mathias Agopian97c602c2011-07-19 15:24:46 -0700482status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
483 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700484 ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700485
486 sp<FrameAvailableListener> listener;
487
488 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700489 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700490 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700491 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700492 return NO_INIT;
493 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700494 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700495 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700496 mBufferCount, buf);
497 return -EINVAL;
498 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700499 ST_LOGE("queueBuffer: slot %d is not owned by the client "
500 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700501 return -EINVAL;
502 } else if (buf == mCurrentTexture) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700503 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700504 return -EINVAL;
505 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700506 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700507 "buffer", buf);
508 return -EINVAL;
509 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700510
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700511 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700512 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700513 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700514
515 // Synchronous mode always signals that an additional frame should
516 // be consumed.
517 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700518 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700519 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700520 if (mQueue.empty()) {
521 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700522
523 // Asynchronous mode only signals that a frame should be
524 // consumed if no previous frame was pending. If a frame were
525 // pending then the consumer would have already been notified.
526 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700527 } else {
528 Fifo::iterator front(mQueue.begin());
529 // buffer currently queued is freed
530 mSlots[*front].mBufferState = BufferSlot::FREE;
531 // and we record the new buffer index in the queued list
532 *front = buf;
533 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700534 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700535
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700536 mSlots[buf].mBufferState = BufferSlot::QUEUED;
537 mSlots[buf].mCrop = mNextCrop;
538 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700539 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700540 mSlots[buf].mTimestamp = timestamp;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600541 mFrameCounter++;
542 mSlots[buf].mFrameNumber = mFrameCounter;
543
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700544 mDequeueCondition.signal();
Mathias Agopian3902be62011-08-17 12:45:40 -0700545
546 *outWidth = mDefaultWidth;
547 *outHeight = mDefaultHeight;
548 *outTransform = 0;
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700549 } // scope for the lock
550
551 // call back without lock held
552 if (listener != 0) {
553 listener->onFrameAvailable();
554 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800555 return OK;
556}
557
558void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700559 ST_LOGV("cancelBuffer: slot=%d", buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800560 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700561
562 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700563 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700564 return;
565 }
566
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700567 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700568 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700569 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800570 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700571 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700572 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700573 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800574 return;
575 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700576 mSlots[buf].mBufferState = BufferSlot::FREE;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600577 mSlots[buf].mFrameNumber = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700578 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800579}
580
Jamie Gennisf238e282011-01-09 16:33:17 -0800581status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700582 ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
583 crop.bottom);
584
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800585 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700586 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700587 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700588 return NO_INIT;
589 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800590 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800591 return OK;
592}
593
594status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700595 ST_LOGV("setTransform: xform=%#x", transform);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800596 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700597 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700598 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700599 return NO_INIT;
600 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800601 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800602 return OK;
603}
604
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700605status_t SurfaceTexture::connect(int api,
606 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700607 ST_LOGV("connect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700608 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700609
610 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700611 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700612 return NO_INIT;
613 }
614
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700615 int err = NO_ERROR;
616 switch (api) {
617 case NATIVE_WINDOW_API_EGL:
618 case NATIVE_WINDOW_API_CPU:
619 case NATIVE_WINDOW_API_MEDIA:
620 case NATIVE_WINDOW_API_CAMERA:
621 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700622 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700623 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700624 err = -EINVAL;
625 } else {
626 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700627 *outWidth = mDefaultWidth;
628 *outHeight = mDefaultHeight;
629 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700630 }
631 break;
632 default:
633 err = -EINVAL;
634 break;
635 }
636 return err;
637}
638
639status_t SurfaceTexture::disconnect(int api) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700640 ST_LOGV("disconnect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700641 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700642
643 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700644 ST_LOGE("disconnect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700645 return NO_INIT;
646 }
647
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700648 int err = NO_ERROR;
649 switch (api) {
650 case NATIVE_WINDOW_API_EGL:
651 case NATIVE_WINDOW_API_CPU:
652 case NATIVE_WINDOW_API_MEDIA:
653 case NATIVE_WINDOW_API_CAMERA:
654 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700655 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700656 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700657 mNextCrop.makeInvalid();
658 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
659 mNextTransform = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700660 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700661 } else {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700662 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700663 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700664 err = -EINVAL;
665 }
666 break;
667 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700668 ST_LOGE("disconnect: unknown API %d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700669 err = -EINVAL;
670 break;
671 }
672 return err;
673}
674
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700675status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700676 ST_LOGV("setScalingMode: mode=%d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700677
678 switch (mode) {
679 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
680 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
681 break;
682 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700683 ST_LOGE("unknown scaling mode: %d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700684 return BAD_VALUE;
685 }
686
687 Mutex::Autolock lock(mMutex);
688 mNextScalingMode = mode;
689 return OK;
690}
691
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800692status_t SurfaceTexture::updateTexImage() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700693 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800694 Mutex::Autolock lock(mMutex);
695
Mathias Agopiane47498f2011-08-08 19:35:15 -0700696 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700697 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700698 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700699 }
700
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700701 // In asynchronous mode the list is guaranteed to be one buffer
702 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700703 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700704 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700705 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700706
Jamie Gennisf238e282011-01-09 16:33:17 -0800707 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700708 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800709 if (image == EGL_NO_IMAGE_KHR) {
710 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700711 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700712 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700713 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700714 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700715 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
716 mSlots[buf].mEglImage = image;
717 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700718 if (image == EGL_NO_IMAGE_KHR) {
719 // NOTE: if dpy was invalid, createImage() is guaranteed to
720 // fail. so we'd end up here.
721 return -EINVAL;
722 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800723 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800724
725 GLint error;
726 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700727 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800728 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700729
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700730 glBindTexture(mTexTarget, mTexName);
731 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700732
Jamie Gennis0eb88512011-01-26 11:52:02 -0800733 bool failed = false;
734 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700735 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700736 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800737 failed = true;
738 }
739 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800740 return -EINVAL;
741 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800742
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700743 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
744 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, buf,
745 mSlots[buf].mGraphicBuffer->handle);
746
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700747 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700748 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700749 // state. If it has already been given to the client
750 // (synchronous mode), then it stays in DEQUEUED state.
751 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
752 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
753 }
754
Jamie Gennis9a78c902011-01-12 18:30:40 -0800755 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700756 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700757 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700758 mCurrentCrop = mSlots[buf].mCrop;
759 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700760 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700761 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700762 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700763
764 // Now that we've passed the point at which failures can happen,
765 // it's safe to remove the buffer from the front of the queue.
766 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700767 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700768 } else {
769 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700770 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800771 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700772
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800773 return OK;
774}
775
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700776bool SurfaceTexture::isExternalFormat(uint32_t format)
777{
778 switch (format) {
779 // supported YUV formats
780 case HAL_PIXEL_FORMAT_YV12:
781 // Legacy/deprecated YUV formats
782 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
783 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
784 case HAL_PIXEL_FORMAT_YCbCr_422_I:
785 return true;
786 }
787
788 // Any OEM format needs to be considered
789 if (format>=0x100 && format<=0x1FF)
790 return true;
791
792 return false;
793}
794
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700795GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700796 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700797}
798
Jamie Gennisf238e282011-01-09 16:33:17 -0800799void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800800 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700801 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
802}
803
804void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700805 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800806
Jamie Gennisa214c642011-01-14 13:53:31 -0800807 float xform[16];
808 for (int i = 0; i < 16; i++) {
809 xform[i] = mtxIdentity[i];
810 }
811 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
812 float result[16];
813 mtxMul(result, xform, mtxFlipH);
814 for (int i = 0; i < 16; i++) {
815 xform[i] = result[i];
816 }
817 }
818 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
819 float result[16];
820 mtxMul(result, xform, mtxFlipV);
821 for (int i = 0; i < 16; i++) {
822 xform[i] = result[i];
823 }
824 }
825 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
826 float result[16];
827 mtxMul(result, xform, mtxRot90);
828 for (int i = 0; i < 16; i++) {
829 xform[i] = result[i];
830 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800831 }
832
833 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800834 float tx, ty, sx, sy;
835 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800836 // In order to prevent bilinear sampling at the of the crop rectangle we
837 // may need to shrink it by 2 texels in each direction. Normally this
838 // would just need to take 1/2 a texel off each end, but because the
839 // chroma channels will likely be subsampled we need to chop off a whole
840 // texel. This will cause artifacts if someone does nearest sampling
841 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
842 // accomodate the bilinear and nearest sampling uses.
843 //
844 // If nearest sampling turns out to be a desirable usage of these
845 // textures then we could add the ability to switch a SurfaceTexture to
846 // nearest-mode. Preferably, however, the image producers (video
847 // decoder, camera, etc.) would simply not use a crop rectangle (or at
848 // least not tell the framework about it) so that the GPU can do the
849 // correct edge behavior.
850 int xshrink = 0, yshrink = 0;
851 if (mCurrentCrop.left > 0) {
852 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
853 xshrink++;
854 } else {
855 tx = 0.0f;
856 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700857 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800858 xshrink++;
859 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700860 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800861 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
862 float(buf->getHeight());
863 yshrink++;
864 } else {
865 ty = 0.0f;
866 }
867 if (mCurrentCrop.top > 0) {
868 yshrink++;
869 }
870 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
871 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800872 } else {
873 tx = 0.0f;
874 ty = 0.0f;
875 sx = 1.0f;
876 sy = 1.0f;
877 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800878 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800879 sx, 0, 0, 0,
880 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800881 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800882 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800883 };
884
Jamie Gennisa214c642011-01-14 13:53:31 -0800885 float mtxBeforeFlipV[16];
886 mtxMul(mtxBeforeFlipV, crop, xform);
887
888 // SurfaceFlinger expects the top of its window textures to be at a Y
889 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
890 // want to expose this to applications, however, so we must add an
891 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700892 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800893}
894
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800895nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700896 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800897 Mutex::Autolock lock(mMutex);
898 return mCurrentTimestamp;
899}
900
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800901void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700902 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700903 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800904 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700905 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800906}
907
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700908void SurfaceTexture::freeBufferLocked(int i) {
909 mSlots[i].mGraphicBuffer = 0;
910 mSlots[i].mBufferState = BufferSlot::FREE;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600911 mSlots[i].mFrameNumber = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700912 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
913 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
914 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
915 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
916 }
917}
918
Mathias Agopianef51b992011-08-10 15:28:58 -0700919void SurfaceTexture::freeAllBuffersLocked() {
920 LOGW_IF(!mQueue.isEmpty(),
921 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian29b57622011-08-17 15:42:04 -0700922 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800923 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700924 freeBufferLocked(i);
925 }
926}
927
928void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
929 LOGW_IF(!mQueue.isEmpty(),
930 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
931 int head = -1;
932 if (!mQueue.empty()) {
933 Fifo::iterator front(mQueue.begin());
934 head = *front;
935 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700936 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700937 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
938 if (i != head) {
939 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800940 }
941 }
942}
943
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700944status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700945 while (mSynchronousMode && !mQueue.isEmpty()) {
946 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700947 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700948 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700949 return NO_INIT;
950 }
951 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700952 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700953 return NO_INIT;
954 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700955 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700956 return NO_ERROR;
957}
958
959status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
960 status_t err = drainQueueLocked();
961 if (err == NO_ERROR) {
962 if (mSynchronousMode) {
963 freeAllBuffersLocked();
964 } else {
965 freeAllBuffersExceptHeadLocked();
966 }
967 }
968 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700969}
970
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800971EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
972 const sp<GraphicBuffer>& graphicBuffer) {
973 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
974 EGLint attrs[] = {
975 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
976 EGL_NONE,
977 };
978 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
979 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700980 if (image == EGL_NO_IMAGE_KHR) {
981 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700982 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800983 }
984 return image;
985}
986
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700987sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
988 Mutex::Autolock lock(mMutex);
989 return mCurrentTextureBuf;
990}
991
992Rect SurfaceTexture::getCurrentCrop() const {
993 Mutex::Autolock lock(mMutex);
994 return mCurrentCrop;
995}
996
997uint32_t SurfaceTexture::getCurrentTransform() const {
998 Mutex::Autolock lock(mMutex);
999 return mCurrentTransform;
1000}
1001
Mathias Agopian7734ebf2011-07-13 15:24:42 -07001002uint32_t SurfaceTexture::getCurrentScalingMode() const {
1003 Mutex::Autolock lock(mMutex);
1004 return mCurrentScalingMode;
1005}
1006
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001007int SurfaceTexture::query(int what, int* outValue)
1008{
1009 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001010
1011 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -07001012 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001013 return NO_INIT;
1014 }
1015
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001016 int value;
1017 switch (what) {
1018 case NATIVE_WINDOW_WIDTH:
1019 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001020 break;
1021 case NATIVE_WINDOW_HEIGHT:
1022 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001023 break;
1024 case NATIVE_WINDOW_FORMAT:
1025 value = mPixelFormat;
1026 break;
1027 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1028 value = mSynchronousMode ?
1029 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
1030 break;
1031 default:
1032 return BAD_VALUE;
1033 }
1034 outValue[0] = value;
1035 return NO_ERROR;
1036}
Mathias Agopian7a042bf2011-04-11 21:19:55 -07001037
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001038void SurfaceTexture::abandon() {
1039 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001040 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001041 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -07001042 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001043 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001044 mDequeueCondition.signal();
1045}
1046
Jamie Gennisfa28c352011-09-16 17:30:26 -07001047void SurfaceTexture::setName(const String8& name) {
1048 mName = name;
1049}
1050
Mathias Agopian68c77942011-05-09 19:08:33 -07001051void SurfaceTexture::dump(String8& result) const
1052{
1053 char buffer[1024];
1054 dump(result, "", buffer, 1024);
1055}
1056
1057void SurfaceTexture::dump(String8& result, const char* prefix,
1058 char* buffer, size_t SIZE) const
1059{
1060 Mutex::Autolock _l(mMutex);
1061 snprintf(buffer, SIZE,
1062 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1063 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001064 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1065 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -07001066 result.append(buffer);
1067
1068 String8 fifo;
1069 int fifoSize = 0;
1070 Fifo::const_iterator i(mQueue.begin());
1071 while (i != mQueue.end()) {
1072 snprintf(buffer, SIZE, "%02d ", *i++);
1073 fifoSize++;
1074 fifo.append(buffer);
1075 }
1076
1077 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001078 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001079 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1080 ,
1081 prefix, mCurrentCrop.left,
1082 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001083 mCurrentTransform, mCurrentTexture,
Jamie Gennisfa28c352011-09-16 17:30:26 -07001084 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1085 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -07001086 );
1087 result.append(buffer);
1088
1089 struct {
1090 const char * operator()(int state) const {
1091 switch (state) {
1092 case BufferSlot::DEQUEUED: return "DEQUEUED";
1093 case BufferSlot::QUEUED: return "QUEUED";
1094 case BufferSlot::FREE: return "FREE";
1095 default: return "Unknown";
1096 }
1097 }
1098 } stateName;
1099
1100 for (int i=0 ; i<mBufferCount ; i++) {
1101 const BufferSlot& slot(mSlots[i]);
1102 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001103 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001104 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001105 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001106 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001107 stateName(slot.mBufferState),
Jamie Gennisfa28c352011-09-16 17:30:26 -07001108 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1109 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001110 );
1111 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001112
1113 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1114 if (buf != NULL) {
1115 snprintf(buffer, SIZE,
1116 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001117 buf->handle, buf->width, buf->height, buf->stride,
1118 buf->format);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001119 result.append(buffer);
1120 }
1121 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001122 }
1123}
1124
Jamie Gennisf238e282011-01-09 16:33:17 -08001125static void mtxMul(float out[16], const float a[16], const float b[16]) {
1126 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1127 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1128 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1129 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1130
1131 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1132 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1133 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1134 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1135
1136 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1137 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1138 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1139 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1140
1141 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1142 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1143 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1144 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1145}
1146
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001147}; // namespace android