blob: b4d01a961f71a31e3e13fa6b617b3835f2476f12 [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 Agopian7c6eba62011-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
Steve Block6807e592011-10-20 11:56:00 +010047#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Jamie Gennisfa28c352011-09-16 17:30:26 -070048#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 Agopian7c6eba62011-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 }
Jamie Gennisaaa3ecf2011-11-17 16:00:44 -0800444 if (mCurrentTexture == buf) {
445 // The current texture no longer references the buffer in this slot
446 // since we just allocated a new buffer.
447 mCurrentTexture = INVALID_BUFFER_SLOT;
448 }
Mathias Agopian80727112011-05-02 19:51:12 -0700449 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700450 }
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700451 ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", buf,
452 mSlots[buf].mGraphicBuffer->handle, returnFlags);
Mathias Agopian80727112011-05-02 19:51:12 -0700453 return returnFlags;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800454}
455
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700456status_t SurfaceTexture::setSynchronousMode(bool enabled) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700457 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700458 Mutex::Autolock lock(mMutex);
Mathias Agopian80727112011-05-02 19:51:12 -0700459
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700460 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700461 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700462 return NO_INIT;
463 }
464
Mathias Agopian80727112011-05-02 19:51:12 -0700465 status_t err = OK;
Grace Kloba14a0e582011-06-23 21:21:47 -0700466 if (!mAllowSynchronousMode && enabled)
467 return err;
468
Mathias Agopian80727112011-05-02 19:51:12 -0700469 if (!enabled) {
470 // going to asynchronous mode, drain the queue
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700471 err = drainQueueLocked();
472 if (err != NO_ERROR)
473 return err;
Mathias Agopian80727112011-05-02 19:51:12 -0700474 }
475
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700476 if (mSynchronousMode != enabled) {
Mathias Agopian80727112011-05-02 19:51:12 -0700477 // - if we're going to asynchronous mode, the queue is guaranteed to be
478 // empty here
479 // - if the client set the number of buffers, we're guaranteed that
480 // we have at least 3 (because we don't allow less)
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700481 mSynchronousMode = enabled;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700482 mDequeueCondition.signal();
483 }
Mathias Agopian80727112011-05-02 19:51:12 -0700484 return err;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700485}
486
Mathias Agopian97c602c2011-07-19 15:24:46 -0700487status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
488 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700489 ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700490
491 sp<FrameAvailableListener> listener;
492
493 { // scope for the lock
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700494 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700495 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700496 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700497 return NO_INIT;
498 }
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700499 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700500 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700501 mBufferCount, buf);
502 return -EINVAL;
503 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700504 ST_LOGE("queueBuffer: slot %d is not owned by the client "
505 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700506 return -EINVAL;
507 } else if (buf == mCurrentTexture) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700508 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700509 return -EINVAL;
510 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700511 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700512 "buffer", buf);
513 return -EINVAL;
514 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700515
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700516 if (mSynchronousMode) {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700517 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700518 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700519
520 // Synchronous mode always signals that an additional frame should
521 // be consumed.
522 listener = mFrameAvailableListener;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700523 } else {
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700524 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700525 if (mQueue.empty()) {
526 mQueue.push_back(buf);
Jamie Gennis3d8063b2011-06-26 18:27:47 -0700527
528 // Asynchronous mode only signals that a frame should be
529 // consumed if no previous frame was pending. If a frame were
530 // pending then the consumer would have already been notified.
531 listener = mFrameAvailableListener;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700532 } else {
533 Fifo::iterator front(mQueue.begin());
534 // buffer currently queued is freed
535 mSlots[*front].mBufferState = BufferSlot::FREE;
536 // and we record the new buffer index in the queued list
537 *front = buf;
538 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700539 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700540
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700541 mSlots[buf].mBufferState = BufferSlot::QUEUED;
542 mSlots[buf].mCrop = mNextCrop;
543 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700544 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700545 mSlots[buf].mTimestamp = timestamp;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600546 mFrameCounter++;
547 mSlots[buf].mFrameNumber = mFrameCounter;
548
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700549 mDequeueCondition.signal();
Mathias Agopian3902be62011-08-17 12:45:40 -0700550
551 *outWidth = mDefaultWidth;
552 *outHeight = mDefaultHeight;
553 *outTransform = 0;
Mathias Agopiancf46eb92011-05-11 15:05:29 -0700554 } // scope for the lock
555
556 // call back without lock held
557 if (listener != 0) {
558 listener->onFrameAvailable();
559 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800560 return OK;
561}
562
563void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700564 ST_LOGV("cancelBuffer: slot=%d", buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800565 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700566
567 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700568 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700569 return;
570 }
571
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700572 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700573 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700574 mBufferCount, buf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800575 return;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700576 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700577 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700578 buf, mSlots[buf].mBufferState);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800579 return;
580 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700581 mSlots[buf].mBufferState = BufferSlot::FREE;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600582 mSlots[buf].mFrameNumber = 0;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700583 mDequeueCondition.signal();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800584}
585
Jamie Gennisf238e282011-01-09 16:33:17 -0800586status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700587 ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
588 crop.bottom);
589
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800590 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700591 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700592 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700593 return NO_INIT;
594 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800595 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800596 return OK;
597}
598
599status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700600 ST_LOGV("setTransform: xform=%#x", transform);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800601 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700602 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700603 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700604 return NO_INIT;
605 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800606 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800607 return OK;
608}
609
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700610status_t SurfaceTexture::connect(int api,
611 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700612 ST_LOGV("connect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700613 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700614
615 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700616 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700617 return NO_INIT;
618 }
619
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700620 int err = NO_ERROR;
621 switch (api) {
622 case NATIVE_WINDOW_API_EGL:
623 case NATIVE_WINDOW_API_CPU:
624 case NATIVE_WINDOW_API_MEDIA:
625 case NATIVE_WINDOW_API_CAMERA:
626 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700627 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700628 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700629 err = -EINVAL;
630 } else {
631 mConnectedApi = api;
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700632 *outWidth = mDefaultWidth;
633 *outHeight = mDefaultHeight;
634 *outTransform = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700635 }
636 break;
637 default:
638 err = -EINVAL;
639 break;
640 }
641 return err;
642}
643
644status_t SurfaceTexture::disconnect(int api) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700645 ST_LOGV("disconnect: api=%d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700646 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700647
648 if (mAbandoned) {
Mathias Agopian8b8a0042011-11-18 14:30:20 -0800649 // it is not really an error to disconnect after the surface
650 // has been abandoned, it should just be a no-op.
651 return NO_ERROR;
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700652 }
653
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700654 int err = NO_ERROR;
655 switch (api) {
656 case NATIVE_WINDOW_API_EGL:
657 case NATIVE_WINDOW_API_CPU:
658 case NATIVE_WINDOW_API_MEDIA:
659 case NATIVE_WINDOW_API_CAMERA:
660 if (mConnectedApi == api) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700661 drainQueueAndFreeBuffersLocked();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700662 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian70e3f812011-08-25 17:03:30 -0700663 mNextCrop.makeInvalid();
664 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
665 mNextTransform = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700666 mDequeueCondition.signal();
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700667 } else {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700668 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700669 mConnectedApi, api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700670 err = -EINVAL;
671 }
672 break;
673 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700674 ST_LOGE("disconnect: unknown API %d", api);
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700675 err = -EINVAL;
676 break;
677 }
678 return err;
679}
680
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700681status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700682 ST_LOGV("setScalingMode: mode=%d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700683
684 switch (mode) {
685 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
686 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
687 break;
688 default:
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700689 ST_LOGE("unknown scaling mode: %d", mode);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700690 return BAD_VALUE;
691 }
692
693 Mutex::Autolock lock(mMutex);
694 mNextScalingMode = mode;
695 return OK;
696}
697
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800698status_t SurfaceTexture::updateTexImage() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700699 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800700 Mutex::Autolock lock(mMutex);
701
Mathias Agopiane47498f2011-08-08 19:35:15 -0700702 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700703 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700704 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700705 }
706
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700707 // In asynchronous mode the list is guaranteed to be one buffer
708 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700709 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700710 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700711 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700712
Jamie Gennisf238e282011-01-09 16:33:17 -0800713 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700714 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800715 if (image == EGL_NO_IMAGE_KHR) {
716 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopiane47498f2011-08-08 19:35:15 -0700717 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700718 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700719 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700720 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700721 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
722 mSlots[buf].mEglImage = image;
723 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700724 if (image == EGL_NO_IMAGE_KHR) {
725 // NOTE: if dpy was invalid, createImage() is guaranteed to
726 // fail. so we'd end up here.
727 return -EINVAL;
728 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800729 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800730
731 GLint error;
732 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700733 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800734 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700735
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700736 glBindTexture(mTexTarget, mTexName);
737 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700738
Jamie Gennis0eb88512011-01-26 11:52:02 -0800739 bool failed = false;
740 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700741 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700742 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800743 failed = true;
744 }
745 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800746 return -EINVAL;
747 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800748
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700749 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
750 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, buf,
751 mSlots[buf].mGraphicBuffer->handle);
752
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700753 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700754 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700755 // state. If it has already been given to the client
756 // (synchronous mode), then it stays in DEQUEUED state.
757 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
758 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
759 }
760
Jamie Gennis9a78c902011-01-12 18:30:40 -0800761 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700762 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700763 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700764 mCurrentCrop = mSlots[buf].mCrop;
765 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700766 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700767 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700768 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700769
770 // Now that we've passed the point at which failures can happen,
771 // it's safe to remove the buffer from the front of the queue.
772 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700773 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700774 } else {
775 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700776 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800777 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700778
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800779 return OK;
780}
781
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700782bool SurfaceTexture::isExternalFormat(uint32_t format)
783{
784 switch (format) {
785 // supported YUV formats
786 case HAL_PIXEL_FORMAT_YV12:
787 // Legacy/deprecated YUV formats
788 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
789 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
790 case HAL_PIXEL_FORMAT_YCbCr_422_I:
791 return true;
792 }
793
794 // Any OEM format needs to be considered
795 if (format>=0x100 && format<=0x1FF)
796 return true;
797
798 return false;
799}
800
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700801GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700802 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700803}
804
Jamie Gennisf238e282011-01-09 16:33:17 -0800805void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800806 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700807 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
808}
809
810void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700811 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800812
Jamie Gennisa214c642011-01-14 13:53:31 -0800813 float xform[16];
814 for (int i = 0; i < 16; i++) {
815 xform[i] = mtxIdentity[i];
816 }
817 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
818 float result[16];
819 mtxMul(result, xform, mtxFlipH);
820 for (int i = 0; i < 16; i++) {
821 xform[i] = result[i];
822 }
823 }
824 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
825 float result[16];
826 mtxMul(result, xform, mtxFlipV);
827 for (int i = 0; i < 16; i++) {
828 xform[i] = result[i];
829 }
830 }
831 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
832 float result[16];
833 mtxMul(result, xform, mtxRot90);
834 for (int i = 0; i < 16; i++) {
835 xform[i] = result[i];
836 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800837 }
838
839 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800840 float tx, ty, sx, sy;
841 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800842 // In order to prevent bilinear sampling at the of the crop rectangle we
843 // may need to shrink it by 2 texels in each direction. Normally this
844 // would just need to take 1/2 a texel off each end, but because the
845 // chroma channels will likely be subsampled we need to chop off a whole
846 // texel. This will cause artifacts if someone does nearest sampling
847 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
848 // accomodate the bilinear and nearest sampling uses.
849 //
850 // If nearest sampling turns out to be a desirable usage of these
851 // textures then we could add the ability to switch a SurfaceTexture to
852 // nearest-mode. Preferably, however, the image producers (video
853 // decoder, camera, etc.) would simply not use a crop rectangle (or at
854 // least not tell the framework about it) so that the GPU can do the
855 // correct edge behavior.
856 int xshrink = 0, yshrink = 0;
857 if (mCurrentCrop.left > 0) {
858 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
859 xshrink++;
860 } else {
861 tx = 0.0f;
862 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700863 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800864 xshrink++;
865 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700866 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800867 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
868 float(buf->getHeight());
869 yshrink++;
870 } else {
871 ty = 0.0f;
872 }
873 if (mCurrentCrop.top > 0) {
874 yshrink++;
875 }
876 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
877 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800878 } else {
879 tx = 0.0f;
880 ty = 0.0f;
881 sx = 1.0f;
882 sy = 1.0f;
883 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800884 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800885 sx, 0, 0, 0,
886 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800887 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800888 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800889 };
890
Jamie Gennisa214c642011-01-14 13:53:31 -0800891 float mtxBeforeFlipV[16];
892 mtxMul(mtxBeforeFlipV, crop, xform);
893
894 // SurfaceFlinger expects the top of its window textures to be at a Y
895 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
896 // want to expose this to applications, however, so we must add an
897 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700898 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800899}
900
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800901nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700902 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800903 Mutex::Autolock lock(mMutex);
904 return mCurrentTimestamp;
905}
906
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800907void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700908 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700909 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800910 Mutex::Autolock lock(mMutex);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700911 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800912}
913
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700914void SurfaceTexture::freeBufferLocked(int i) {
915 mSlots[i].mGraphicBuffer = 0;
916 mSlots[i].mBufferState = BufferSlot::FREE;
Sunita Nadampallia9297482011-11-09 18:23:41 -0600917 mSlots[i].mFrameNumber = 0;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700918 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
919 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
920 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
921 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
922 }
923}
924
Mathias Agopianef51b992011-08-10 15:28:58 -0700925void SurfaceTexture::freeAllBuffersLocked() {
926 LOGW_IF(!mQueue.isEmpty(),
927 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian29b57622011-08-17 15:42:04 -0700928 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800929 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700930 freeBufferLocked(i);
931 }
932}
933
934void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
935 LOGW_IF(!mQueue.isEmpty(),
936 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
937 int head = -1;
938 if (!mQueue.empty()) {
939 Fifo::iterator front(mQueue.begin());
940 head = *front;
941 }
Mathias Agopian29b57622011-08-17 15:42:04 -0700942 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700943 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
944 if (i != head) {
945 freeBufferLocked(i);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800946 }
947 }
948}
949
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700950status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian2560d142011-08-10 16:33:23 -0700951 while (mSynchronousMode && !mQueue.isEmpty()) {
952 mDequeueCondition.wait(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700953 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700954 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700955 return NO_INIT;
956 }
957 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700958 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700959 return NO_INIT;
960 }
Mathias Agopian2560d142011-08-10 16:33:23 -0700961 }
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700962 return NO_ERROR;
963}
964
965status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
966 status_t err = drainQueueLocked();
967 if (err == NO_ERROR) {
968 if (mSynchronousMode) {
969 freeAllBuffersLocked();
970 } else {
971 freeAllBuffersExceptHeadLocked();
972 }
973 }
974 return err;
Mathias Agopian2560d142011-08-10 16:33:23 -0700975}
976
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800977EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
978 const sp<GraphicBuffer>& graphicBuffer) {
979 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
980 EGLint attrs[] = {
981 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
982 EGL_NONE,
983 };
984 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
985 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700986 if (image == EGL_NO_IMAGE_KHR) {
987 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700988 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800989 }
990 return image;
991}
992
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700993sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
994 Mutex::Autolock lock(mMutex);
995 return mCurrentTextureBuf;
996}
997
998Rect SurfaceTexture::getCurrentCrop() const {
999 Mutex::Autolock lock(mMutex);
1000 return mCurrentCrop;
1001}
1002
1003uint32_t SurfaceTexture::getCurrentTransform() const {
1004 Mutex::Autolock lock(mMutex);
1005 return mCurrentTransform;
1006}
1007
Mathias Agopian7734ebf2011-07-13 15:24:42 -07001008uint32_t SurfaceTexture::getCurrentScalingMode() const {
1009 Mutex::Autolock lock(mMutex);
1010 return mCurrentScalingMode;
1011}
1012
Jamie Gennis59769462011-11-19 18:04:43 -08001013bool SurfaceTexture::isSynchronousMode() const {
1014 Mutex::Autolock lock(mMutex);
1015 return mSynchronousMode;
1016}
1017
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001018int SurfaceTexture::query(int what, int* outValue)
1019{
1020 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001021
1022 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -07001023 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001024 return NO_INIT;
1025 }
1026
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001027 int value;
1028 switch (what) {
1029 case NATIVE_WINDOW_WIDTH:
1030 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001031 break;
1032 case NATIVE_WINDOW_HEIGHT:
1033 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -07001034 break;
1035 case NATIVE_WINDOW_FORMAT:
1036 value = mPixelFormat;
1037 break;
1038 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1039 value = mSynchronousMode ?
1040 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
1041 break;
1042 default:
1043 return BAD_VALUE;
1044 }
1045 outValue[0] = value;
1046 return NO_ERROR;
1047}
Mathias Agopian7a042bf2011-04-11 21:19:55 -07001048
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001049void SurfaceTexture::abandon() {
1050 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001051 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001052 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -07001053 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -07001054 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001055 mDequeueCondition.signal();
1056}
1057
Jamie Gennisfa28c352011-09-16 17:30:26 -07001058void SurfaceTexture::setName(const String8& name) {
1059 mName = name;
1060}
1061
Mathias Agopian68c77942011-05-09 19:08:33 -07001062void SurfaceTexture::dump(String8& result) const
1063{
1064 char buffer[1024];
1065 dump(result, "", buffer, 1024);
1066}
1067
1068void SurfaceTexture::dump(String8& result, const char* prefix,
1069 char* buffer, size_t SIZE) const
1070{
1071 Mutex::Autolock _l(mMutex);
1072 snprintf(buffer, SIZE,
1073 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1074 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001075 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1076 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -07001077 result.append(buffer);
1078
1079 String8 fifo;
1080 int fifoSize = 0;
1081 Fifo::const_iterator i(mQueue.begin());
1082 while (i != mQueue.end()) {
1083 snprintf(buffer, SIZE, "%02d ", *i++);
1084 fifoSize++;
1085 fifo.append(buffer);
1086 }
1087
1088 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001089 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -07001090 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1091 ,
1092 prefix, mCurrentCrop.left,
1093 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001094 mCurrentTransform, mCurrentTexture,
Jamie Gennisfa28c352011-09-16 17:30:26 -07001095 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1096 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -07001097 );
1098 result.append(buffer);
1099
1100 struct {
1101 const char * operator()(int state) const {
1102 switch (state) {
1103 case BufferSlot::DEQUEUED: return "DEQUEUED";
1104 case BufferSlot::QUEUED: return "QUEUED";
1105 case BufferSlot::FREE: return "FREE";
1106 default: return "Unknown";
1107 }
1108 }
1109 } stateName;
1110
1111 for (int i=0 ; i<mBufferCount ; i++) {
1112 const BufferSlot& slot(mSlots[i]);
1113 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001114 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -07001115 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001116 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -07001117 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -07001118 stateName(slot.mBufferState),
Jamie Gennisfa28c352011-09-16 17:30:26 -07001119 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1120 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -07001121 );
1122 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001123
1124 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1125 if (buf != NULL) {
1126 snprintf(buffer, SIZE,
1127 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennisfa28c352011-09-16 17:30:26 -07001128 buf->handle, buf->width, buf->height, buf->stride,
1129 buf->format);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -07001130 result.append(buffer);
1131 }
1132 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -07001133 }
1134}
1135
Jamie Gennisf238e282011-01-09 16:33:17 -08001136static void mtxMul(float out[16], const float a[16], const float b[16]) {
1137 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1138 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1139 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1140 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1141
1142 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1143 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1144 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1145 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1146
1147 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1148 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1149 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1150 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1151
1152 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1153 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1154 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1155 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1156}
1157
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001158}; // namespace android