blob: 1bd0a1ce872be2ab84222ab67e9e32c0f0b5be2c [file] [log] [blame]
Jamie Gennis68e4a7a2010-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 Gennis7dc00d52011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis68e4a7a2010-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 Agopian27cd07c2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennisf7acf162011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
Mathias Agopian7f3289c2011-05-09 19:08:33 -070037#include <utils/String8.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080038
Mathias Agopiandb5494c2011-11-16 15:59:13 -080039
40#define ALLOW_DEQUEUE_CURRENT_BUFFER false
Mathias Agopian8618ebc2011-08-17 15:42:04 -070041
Jamie Gennis44a05222011-09-16 17:30:26 -070042// Macros for including the SurfaceTexture name in log messages
43#define ST_LOGV(x, ...) LOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
44#define ST_LOGD(x, ...) LOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
45#define ST_LOGI(x, ...) LOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
46#define ST_LOGW(x, ...) LOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
47#define ST_LOGE(x, ...) LOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian8618ebc2011-08-17 15:42:04 -070048
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080049namespace android {
50
Jamie Gennisb598fb92011-01-09 16:33:17 -080051// Transform matrices
52static float mtxIdentity[16] = {
53 1, 0, 0, 0,
54 0, 1, 0, 0,
55 0, 0, 1, 0,
56 0, 0, 0, 1,
57};
58static float mtxFlipH[16] = {
59 -1, 0, 0, 0,
60 0, 1, 0, 0,
61 0, 0, 1, 0,
62 1, 0, 0, 1,
63};
64static float mtxFlipV[16] = {
65 1, 0, 0, 0,
66 0, -1, 0, 0,
67 0, 0, 1, 0,
68 0, 1, 0, 1,
69};
70static float mtxRot90[16] = {
71 0, 1, 0, 0,
72 -1, 0, 0, 0,
73 0, 0, 1, 0,
74 1, 0, 0, 1,
75};
76static float mtxRot180[16] = {
77 -1, 0, 0, 0,
78 0, -1, 0, 0,
79 0, 0, 1, 0,
80 1, 1, 0, 1,
81};
82static float mtxRot270[16] = {
83 0, -1, 0, 0,
84 1, 0, 0, 0,
85 0, 0, 1, 0,
86 0, 1, 0, 1,
87};
88
89static void mtxMul(float out[16], const float a[16], const float b[16]);
90
Jamie Gennis44a05222011-09-16 17:30:26 -070091// Get an ID that's unique within this process.
92static int32_t createProcessUniqueId() {
93 static volatile int32_t globalCounter = 0;
94 return android_atomic_inc(&globalCounter);
95}
96
Jamie Gennis8606fef2011-09-28 12:13:31 -070097SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
98 GLenum texTarget) :
Mathias Agopiane5a1bff2011-03-31 19:10:24 -070099 mDefaultWidth(1),
100 mDefaultHeight(1),
101 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
Mathias Agopian402ff242011-05-02 19:51:12 -0700102 mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
103 mClientBufferCount(0),
104 mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800105 mCurrentTexture(INVALID_BUFFER_SLOT),
106 mCurrentTransform(0),
107 mCurrentTimestamp(0),
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800108 mNextTransform(0),
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700109 mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700110 mTexName(tex),
Grace Kloba0904d0a2011-06-23 21:21:47 -0700111 mSynchronousMode(false),
Jamie Gennis97096872011-07-13 19:12:20 -0700112 mAllowSynchronousMode(allowSynchronousMode),
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700113 mConnectedApi(NO_CONNECTED_API),
Jamie Gennis8606fef2011-09-28 12:13:31 -0700114 mAbandoned(false),
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600115 mTexTarget(texTarget),
116 mFrameCounter(0) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700117 // Choose a name using the PID and a process-unique ID.
118 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
119
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700120 ST_LOGV("SurfaceTexture");
Jamie Gennisf7acf162011-01-12 18:30:40 -0800121 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
122 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Mathias Agopian926340c2011-04-27 18:57:33 -0700123 mNextCrop.makeInvalid();
Jamie Gennis44a05222011-09-16 17:30:26 -0700124 memcpy(mCurrentTransformMatrix, mtxIdentity,
125 sizeof(mCurrentTransformMatrix));
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800126}
127
128SurfaceTexture::~SurfaceTexture() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700129 ST_LOGV("~SurfaceTexture");
Mathias Agopiana04cda92011-08-10 15:28:58 -0700130 freeAllBuffersLocked();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800131}
132
Mathias Agopian402ff242011-05-02 19:51:12 -0700133status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
134 if (bufferCount > NUM_BUFFER_SLOTS)
135 return BAD_VALUE;
136
137 // special-case, nothing to do
138 if (bufferCount == mBufferCount)
139 return OK;
140
141 if (!mClientBufferCount &&
142 bufferCount >= mBufferCount) {
143 // easy, we just have more buffers
144 mBufferCount = bufferCount;
145 mServerBufferCount = bufferCount;
146 mDequeueCondition.signal();
147 } else {
148 // we're here because we're either
149 // - reducing the number of available buffers
150 // - or there is a client-buffer-count in effect
151
152 // less than 2 buffers is never allowed
153 if (bufferCount < 2)
154 return BAD_VALUE;
155
156 // when there is non client-buffer-count in effect, the client is not
157 // allowed to dequeue more than one buffer at a time,
158 // so the next time they dequeue a buffer, we know that they don't
159 // own one. the actual resizing will happen during the next
160 // dequeueBuffer.
161
162 mServerBufferCount = bufferCount;
163 }
164 return OK;
165}
166
167status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
168 Mutex::Autolock lock(mMutex);
169 return setBufferCountServerLocked(bufferCount);
170}
171
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800172status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700173 ST_LOGV("setBufferCount: count=%d", bufferCount);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700174 Mutex::Autolock lock(mMutex);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800175
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700176 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700177 ST_LOGE("setBufferCount: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700178 return NO_INIT;
179 }
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700180 if (bufferCount > NUM_BUFFER_SLOTS) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700181 ST_LOGE("setBufferCount: bufferCount larger than slots available");
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700182 return BAD_VALUE;
183 }
184
Mathias Agopian402ff242011-05-02 19:51:12 -0700185 // Error out if the user has dequeued buffers
186 for (int i=0 ; i<mBufferCount ; i++) {
187 if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700188 ST_LOGE("setBufferCount: client owns some buffers");
Mathias Agopian402ff242011-05-02 19:51:12 -0700189 return -EINVAL;
190 }
191 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700192
Jamie Gennisd995b082011-07-30 16:00:11 -0700193 const int minBufferSlots = mSynchronousMode ?
194 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
Mathias Agopian402ff242011-05-02 19:51:12 -0700195 if (bufferCount == 0) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700196 mClientBufferCount = 0;
197 bufferCount = (mServerBufferCount >= minBufferSlots) ?
198 mServerBufferCount : minBufferSlots;
199 return setBufferCountServerLocked(bufferCount);
200 }
201
Jamie Gennisd995b082011-07-30 16:00:11 -0700202 if (bufferCount < minBufferSlots) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700203 ST_LOGE("setBufferCount: requested buffer count (%d) is less than "
Jamie Gennisd995b082011-07-30 16:00:11 -0700204 "minimum (%d)", bufferCount, minBufferSlots);
Jamie Gennis96dcc972011-02-27 14:10:20 -0800205 return BAD_VALUE;
206 }
207
Mathias Agopian402ff242011-05-02 19:51:12 -0700208 // here we're guaranteed that the client doesn't have dequeued buffers
209 // and will release all of its buffer references.
Mathias Agopiana04cda92011-08-10 15:28:58 -0700210 freeAllBuffersLocked();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800211 mBufferCount = bufferCount;
Mathias Agopian402ff242011-05-02 19:51:12 -0700212 mClientBufferCount = bufferCount;
Jamie Gennisd369dc42011-01-09 13:25:39 -0800213 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700214 mQueue.clear();
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700215 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800216 return OK;
217}
218
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700219status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
220{
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700221 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
Mathias Agopianf3503c22011-07-25 19:56:08 -0700222 if (!w || !h) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700223 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
224 w, h);
Mathias Agopianf3503c22011-07-25 19:56:08 -0700225 return BAD_VALUE;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700226 }
Mathias Agopianf3503c22011-07-25 19:56:08 -0700227
228 Mutex::Autolock lock(mMutex);
229 mDefaultWidth = w;
230 mDefaultHeight = h;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700231 return OK;
232}
233
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700234status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700235 ST_LOGV("requestBuffer: slot=%d", slot);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800236 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700237 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700238 ST_LOGE("requestBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700239 return NO_INIT;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800240 }
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700241 if (slot < 0 || mBufferCount <= slot) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700242 ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d",
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700243 mBufferCount, slot);
244 return BAD_VALUE;
245 }
246 mSlots[slot].mRequestBufferCalled = true;
247 *buf = mSlots[slot].mGraphicBuffer;
248 return NO_ERROR;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700249}
250
251status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
252 uint32_t format, uint32_t usage) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700253 ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700254
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700255 if ((w && !h) || (!w && h)) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700256 ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700257 return BAD_VALUE;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700258 }
259
Mathias Agopian0297dca2011-04-25 20:22:14 -0700260 Mutex::Autolock lock(mMutex);
Mathias Agopian402ff242011-05-02 19:51:12 -0700261
262 status_t returnFlags(OK);
263
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600264 int found = -1;
265 int foundSync = -1;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700266 int dequeuedCount = 0;
267 bool tryAgain = true;
268 while (tryAgain) {
Mathias Agopian71fd1212011-08-10 16:33:23 -0700269 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700270 ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
Mathias Agopian71fd1212011-08-10 16:33:23 -0700271 return NO_INIT;
272 }
Mathias Agopian71fd1212011-08-10 16:33:23 -0700273
Mathias Agopian402ff242011-05-02 19:51:12 -0700274 // We need to wait for the FIFO to drain if the number of buffer
275 // needs to change.
276 //
Mathias Agopian71fd1212011-08-10 16:33:23 -0700277 // The condition "number of buffers needs to change" is true if
Mathias Agopian402ff242011-05-02 19:51:12 -0700278 // - the client doesn't care about how many buffers there are
279 // - AND the actual number of buffer is different from what was
280 // set in the last setBufferCountServer()
281 // - OR -
282 // setBufferCountServer() was set to a value incompatible with
283 // the synchronization mode (for instance because the sync mode
284 // changed since)
285 //
286 // As long as this condition is true AND the FIFO is not empty, we
287 // wait on mDequeueCondition.
288
Mathias Agopian71fd1212011-08-10 16:33:23 -0700289 const int minBufferCountNeeded = mSynchronousMode ?
Mathias Agopian402ff242011-05-02 19:51:12 -0700290 MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
291
Mathias Agopian71fd1212011-08-10 16:33:23 -0700292 const bool numberOfBuffersNeedsToChange = !mClientBufferCount &&
Mathias Agopian402ff242011-05-02 19:51:12 -0700293 ((mServerBufferCount != mBufferCount) ||
Mathias Agopian71fd1212011-08-10 16:33:23 -0700294 (mServerBufferCount < minBufferCountNeeded));
295
296 if (!mQueue.isEmpty() && numberOfBuffersNeedsToChange) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700297 // wait for the FIFO to drain
Mathias Agopian71fd1212011-08-10 16:33:23 -0700298 mDequeueCondition.wait(mMutex);
299 // NOTE: we continue here because we need to reevaluate our
300 // whole state (eg: we could be abandoned or disconnected)
301 continue;
Mathias Agopian402ff242011-05-02 19:51:12 -0700302 }
303
Mathias Agopian71fd1212011-08-10 16:33:23 -0700304 if (numberOfBuffersNeedsToChange) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700305 // here we're guaranteed that mQueue is empty
Mathias Agopiana04cda92011-08-10 15:28:58 -0700306 freeAllBuffersLocked();
Mathias Agopian402ff242011-05-02 19:51:12 -0700307 mBufferCount = mServerBufferCount;
308 if (mBufferCount < minBufferCountNeeded)
309 mBufferCount = minBufferCountNeeded;
310 mCurrentTexture = INVALID_BUFFER_SLOT;
311 returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
312 }
313
314 // look for a free buffer to give to the client
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700315 found = INVALID_BUFFER_SLOT;
316 foundSync = INVALID_BUFFER_SLOT;
317 dequeuedCount = 0;
318 for (int i = 0; i < mBufferCount; i++) {
319 const int state = mSlots[i].mBufferState;
320 if (state == BufferSlot::DEQUEUED) {
321 dequeuedCount++;
322 }
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700323
324 // if buffer is FREE it CANNOT be current
325 LOGW_IF((state == BufferSlot::FREE) && (mCurrentTexture==i),
326 "dequeueBuffer: buffer %d is both FREE and current!", i);
327
Mathias Agopiandb5494c2011-11-16 15:59:13 -0800328 if (ALLOW_DEQUEUE_CURRENT_BUFFER) {
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700329 if (state == BufferSlot::FREE || i == mCurrentTexture) {
330 foundSync = i;
331 if (i != mCurrentTexture) {
332 found = i;
333 break;
334 }
335 }
336 } else {
337 if (state == BufferSlot::FREE) {
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600338 /** For Asynchronous mode, we need to return the oldest of free buffers
339 * There is only one instance when the Framecounter overflows, this logic
340 * might return the earlier buffer to client. Which is a negligible impact
341 **/
342 if (found < 0 || mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) {
343 foundSync = i;
344 found = i;
345 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700346 }
347 }
348 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700349
350 // clients are not allowed to dequeue more than one buffer
351 // if they didn't set a buffer count.
352 if (!mClientBufferCount && dequeuedCount) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700353 ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without "
354 "setting the buffer count");
Mathias Agopian402ff242011-05-02 19:51:12 -0700355 return -EINVAL;
356 }
357
Jamie Gennis44e9e0d2011-05-23 18:44:04 -0700358 // See whether a buffer has been queued since the last setBufferCount so
359 // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
360 bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
361 if (bufferHasBeenQueued) {
362 // make sure the client is not trying to dequeue more buffers
363 // than allowed.
364 const int avail = mBufferCount - (dequeuedCount+1);
365 if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700366 ST_LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded "
367 "(dequeued=%d)",
Jamie Gennis44e9e0d2011-05-23 18:44:04 -0700368 MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
369 dequeuedCount);
370 return -EBUSY;
371 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700372 }
373
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700374 // we're in synchronous mode and didn't find a buffer, we need to wait
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700375 // for some buffers to be consumed
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700376 tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
377 if (tryAgain) {
378 mDequeueCondition.wait(mMutex);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700379 }
380 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700381
Mathias Agopian402ff242011-05-02 19:51:12 -0700382 if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
383 // foundSync guaranteed to be != INVALID_BUFFER_SLOT
384 found = foundSync;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700385 }
386
Mathias Agopian0297dca2011-04-25 20:22:14 -0700387 if (found == INVALID_BUFFER_SLOT) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700388 // This should not happen.
389 ST_LOGE("dequeueBuffer: no available buffer slots");
Mathias Agopian0297dca2011-04-25 20:22:14 -0700390 return -EBUSY;
391 }
392
393 const int buf = found;
394 *outBuf = found;
395
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700396 const bool useDefaultSize = !w && !h;
397 if (useDefaultSize) {
398 // use the default size
399 w = mDefaultWidth;
400 h = mDefaultHeight;
401 }
402
403 const bool updateFormat = (format != 0);
404 if (!updateFormat) {
405 // keep the current (or default) format
406 format = mPixelFormat;
407 }
408
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700409 // buffer is now in DEQUEUED (but can also be current at the same time,
410 // if we're in synchronous mode)
411 mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
412
413 const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
Mathias Agopian0297dca2011-04-25 20:22:14 -0700414 if ((buffer == NULL) ||
415 (uint32_t(buffer->width) != w) ||
416 (uint32_t(buffer->height) != h) ||
417 (uint32_t(buffer->format) != format) ||
418 ((uint32_t(buffer->usage) & usage) != usage))
419 {
420 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Mathias Agopianeec0f7e2011-07-01 14:53:49 -0700421 status_t error;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700422 sp<GraphicBuffer> graphicBuffer(
Mathias Agopianeec0f7e2011-07-01 14:53:49 -0700423 mGraphicBufferAlloc->createGraphicBuffer(
424 w, h, format, usage, &error));
Mathias Agopian0297dca2011-04-25 20:22:14 -0700425 if (graphicBuffer == 0) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700426 ST_LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer "
427 "failed");
Mathias Agopianeec0f7e2011-07-01 14:53:49 -0700428 return error;
Mathias Agopian0297dca2011-04-25 20:22:14 -0700429 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700430 if (updateFormat) {
431 mPixelFormat = format;
432 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800433 mSlots[buf].mGraphicBuffer = graphicBuffer;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700434 mSlots[buf].mRequestBufferCalled = false;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800435 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
436 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
437 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
438 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
439 }
Jamie Gennise36d05482011-11-17 16:00:44 -0800440 if (mCurrentTexture == buf) {
441 // The current texture no longer references the buffer in this slot
442 // since we just allocated a new buffer.
443 mCurrentTexture = INVALID_BUFFER_SLOT;
444 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700445 returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700446 }
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700447 ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", buf,
448 mSlots[buf].mGraphicBuffer->handle, returnFlags);
Mathias Agopian402ff242011-05-02 19:51:12 -0700449 return returnFlags;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800450}
451
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700452status_t SurfaceTexture::setSynchronousMode(bool enabled) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700453 ST_LOGV("setSynchronousMode: enabled=%d", enabled);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700454 Mutex::Autolock lock(mMutex);
Mathias Agopian402ff242011-05-02 19:51:12 -0700455
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700456 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700457 ST_LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700458 return NO_INIT;
459 }
460
Mathias Agopian402ff242011-05-02 19:51:12 -0700461 status_t err = OK;
Grace Kloba0904d0a2011-06-23 21:21:47 -0700462 if (!mAllowSynchronousMode && enabled)
463 return err;
464
Mathias Agopian402ff242011-05-02 19:51:12 -0700465 if (!enabled) {
466 // going to asynchronous mode, drain the queue
Mathias Agopian5c715752011-08-10 17:35:09 -0700467 err = drainQueueLocked();
468 if (err != NO_ERROR)
469 return err;
Mathias Agopian402ff242011-05-02 19:51:12 -0700470 }
471
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700472 if (mSynchronousMode != enabled) {
Mathias Agopian402ff242011-05-02 19:51:12 -0700473 // - if we're going to asynchronous mode, the queue is guaranteed to be
474 // empty here
475 // - if the client set the number of buffers, we're guaranteed that
476 // we have at least 3 (because we don't allow less)
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700477 mSynchronousMode = enabled;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700478 mDequeueCondition.signal();
479 }
Mathias Agopian402ff242011-05-02 19:51:12 -0700480 return err;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700481}
482
Mathias Agopianf07b8a32011-07-19 15:24:46 -0700483status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
484 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700485 ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp);
Mathias Agopiane845c352011-05-11 15:05:29 -0700486
487 sp<FrameAvailableListener> listener;
488
489 { // scope for the lock
Jamie Gennisa2187152011-05-19 13:33:00 -0700490 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700491 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700492 ST_LOGE("queueBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700493 return NO_INIT;
494 }
Jamie Gennisa2187152011-05-19 13:33:00 -0700495 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700496 ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d",
Jamie Gennisa2187152011-05-19 13:33:00 -0700497 mBufferCount, buf);
498 return -EINVAL;
499 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700500 ST_LOGE("queueBuffer: slot %d is not owned by the client "
501 "(state=%d)", buf, mSlots[buf].mBufferState);
Jamie Gennisa2187152011-05-19 13:33:00 -0700502 return -EINVAL;
503 } else if (buf == mCurrentTexture) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700504 ST_LOGE("queueBuffer: slot %d is current!", buf);
Jamie Gennisa2187152011-05-19 13:33:00 -0700505 return -EINVAL;
506 } else if (!mSlots[buf].mRequestBufferCalled) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700507 ST_LOGE("queueBuffer: slot %d was enqueued without requesting a "
Jamie Gennisa2187152011-05-19 13:33:00 -0700508 "buffer", buf);
509 return -EINVAL;
510 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700511
Jamie Gennisa2187152011-05-19 13:33:00 -0700512 if (mSynchronousMode) {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700513 // In synchronous mode we queue all buffers in a FIFO.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700514 mQueue.push_back(buf);
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700515
516 // Synchronous mode always signals that an additional frame should
517 // be consumed.
518 listener = mFrameAvailableListener;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700519 } else {
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700520 // In asynchronous mode we only keep the most recent buffer.
Jamie Gennisa2187152011-05-19 13:33:00 -0700521 if (mQueue.empty()) {
522 mQueue.push_back(buf);
Jamie Gennisbd5404d2011-06-26 18:27:47 -0700523
524 // Asynchronous mode only signals that a frame should be
525 // consumed if no previous frame was pending. If a frame were
526 // pending then the consumer would have already been notified.
527 listener = mFrameAvailableListener;
Jamie Gennisa2187152011-05-19 13:33:00 -0700528 } else {
529 Fifo::iterator front(mQueue.begin());
530 // buffer currently queued is freed
531 mSlots[*front].mBufferState = BufferSlot::FREE;
532 // and we record the new buffer index in the queued list
533 *front = buf;
534 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700535 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700536
Jamie Gennisa2187152011-05-19 13:33:00 -0700537 mSlots[buf].mBufferState = BufferSlot::QUEUED;
538 mSlots[buf].mCrop = mNextCrop;
539 mSlots[buf].mTransform = mNextTransform;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700540 mSlots[buf].mScalingMode = mNextScalingMode;
Jamie Gennisa2187152011-05-19 13:33:00 -0700541 mSlots[buf].mTimestamp = timestamp;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600542 mFrameCounter++;
543 mSlots[buf].mFrameNumber = mFrameCounter;
544
Jamie Gennisa2187152011-05-19 13:33:00 -0700545 mDequeueCondition.signal();
Mathias Agopian1a227432011-08-17 12:45:40 -0700546
547 *outWidth = mDefaultWidth;
548 *outHeight = mDefaultHeight;
549 *outTransform = 0;
Mathias Agopiane845c352011-05-11 15:05:29 -0700550 } // scope for the lock
551
552 // call back without lock held
553 if (listener != 0) {
554 listener->onFrameAvailable();
555 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800556 return OK;
557}
558
559void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700560 ST_LOGV("cancelBuffer: slot=%d", buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800561 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700562
563 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700564 ST_LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700565 return;
566 }
567
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700568 if (buf < 0 || buf >= mBufferCount) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700569 ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700570 mBufferCount, buf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800571 return;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700572 } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700573 ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700574 buf, mSlots[buf].mBufferState);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800575 return;
576 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700577 mSlots[buf].mBufferState = BufferSlot::FREE;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600578 mSlots[buf].mFrameNumber = 0;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700579 mDequeueCondition.signal();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800580}
581
Jamie Gennisb598fb92011-01-09 16:33:17 -0800582status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700583 ST_LOGV("setCrop: crop=[%d,%d,%d,%d]", crop.left, crop.top, crop.right,
584 crop.bottom);
585
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800586 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700587 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700588 ST_LOGE("setCrop: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700589 return NO_INIT;
590 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800591 mNextCrop = crop;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800592 return OK;
593}
594
595status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700596 ST_LOGV("setTransform: xform=%#x", transform);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800597 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700598 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700599 ST_LOGE("setTransform: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700600 return NO_INIT;
601 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800602 mNextTransform = transform;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800603 return OK;
604}
605
Mathias Agopian053b02d2011-08-08 19:14:03 -0700606status_t SurfaceTexture::connect(int api,
607 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700608 ST_LOGV("connect: api=%d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700609 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700610
611 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700612 ST_LOGE("connect: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700613 return NO_INIT;
614 }
615
Jamie Gennis97096872011-07-13 19:12:20 -0700616 int err = NO_ERROR;
617 switch (api) {
618 case NATIVE_WINDOW_API_EGL:
619 case NATIVE_WINDOW_API_CPU:
620 case NATIVE_WINDOW_API_MEDIA:
621 case NATIVE_WINDOW_API_CAMERA:
622 if (mConnectedApi != NO_CONNECTED_API) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700623 ST_LOGE("connect: already connected (cur=%d, req=%d)",
Mathias Agopian949be322011-07-13 17:39:11 -0700624 mConnectedApi, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700625 err = -EINVAL;
626 } else {
627 mConnectedApi = api;
Mathias Agopian053b02d2011-08-08 19:14:03 -0700628 *outWidth = mDefaultWidth;
629 *outHeight = mDefaultHeight;
630 *outTransform = 0;
Jamie Gennis97096872011-07-13 19:12:20 -0700631 }
632 break;
633 default:
634 err = -EINVAL;
635 break;
636 }
637 return err;
638}
639
640status_t SurfaceTexture::disconnect(int api) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700641 ST_LOGV("disconnect: api=%d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700642 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700643
644 if (mAbandoned) {
Mathias Agopianb05bb172011-11-18 14:30:20 -0800645 // it is not really an error to disconnect after the surface
646 // has been abandoned, it should just be a no-op.
647 return NO_ERROR;
Jamie Gennis5ef59bc2011-07-19 12:08:33 -0700648 }
649
Jamie Gennis97096872011-07-13 19:12:20 -0700650 int err = NO_ERROR;
651 switch (api) {
652 case NATIVE_WINDOW_API_EGL:
653 case NATIVE_WINDOW_API_CPU:
654 case NATIVE_WINDOW_API_MEDIA:
655 case NATIVE_WINDOW_API_CAMERA:
656 if (mConnectedApi == api) {
Mathias Agopian5c715752011-08-10 17:35:09 -0700657 drainQueueAndFreeBuffersLocked();
Jamie Gennis97096872011-07-13 19:12:20 -0700658 mConnectedApi = NO_CONNECTED_API;
Mathias Agopian2370d0a2011-08-25 17:03:30 -0700659 mNextCrop.makeInvalid();
660 mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
661 mNextTransform = 0;
Mathias Agopian5c715752011-08-10 17:35:09 -0700662 mDequeueCondition.signal();
Jamie Gennis97096872011-07-13 19:12:20 -0700663 } else {
Jamie Gennis44a05222011-09-16 17:30:26 -0700664 ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)",
Mathias Agopian949be322011-07-13 17:39:11 -0700665 mConnectedApi, api);
Jamie Gennis97096872011-07-13 19:12:20 -0700666 err = -EINVAL;
667 }
668 break;
669 default:
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700670 ST_LOGE("disconnect: unknown API %d", api);
Jamie Gennis97096872011-07-13 19:12:20 -0700671 err = -EINVAL;
672 break;
673 }
674 return err;
675}
676
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700677status_t SurfaceTexture::setScalingMode(int mode) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700678 ST_LOGV("setScalingMode: mode=%d", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700679
680 switch (mode) {
681 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
682 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
683 break;
684 default:
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700685 ST_LOGE("unknown scaling mode: %d", mode);
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700686 return BAD_VALUE;
687 }
688
689 Mutex::Autolock lock(mMutex);
690 mNextScalingMode = mode;
691 return OK;
692}
693
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800694status_t SurfaceTexture::updateTexImage() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700695 ST_LOGV("updateTexImage");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800696 Mutex::Autolock lock(mMutex);
697
Mathias Agopian95dfd052011-08-08 19:35:15 -0700698 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700699 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian5c715752011-08-10 17:35:09 -0700700 return NO_INIT;
Mathias Agopian95dfd052011-08-08 19:35:15 -0700701 }
702
Jamie Gennis9fb59762011-06-27 15:41:52 -0700703 // In asynchronous mode the list is guaranteed to be one buffer
704 // deep, while in synchronous mode we use the oldest buffer.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700705 if (!mQueue.empty()) {
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700706 Fifo::iterator front(mQueue.begin());
Jamie Gennis9fb59762011-06-27 15:41:52 -0700707 int buf = *front;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700708
Jamie Gennisb598fb92011-01-09 16:33:17 -0800709 // Update the GL texture object.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700710 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800711 if (image == EGL_NO_IMAGE_KHR) {
712 EGLDisplay dpy = eglGetCurrentDisplay();
Mathias Agopian95dfd052011-08-08 19:35:15 -0700713 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700714 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian5c715752011-08-10 17:35:09 -0700715 return BAD_VALUE;
Mathias Agopian95dfd052011-08-08 19:35:15 -0700716 }
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700717 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
718 mSlots[buf].mEglImage = image;
719 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian6fad64c2011-04-26 14:57:40 -0700720 if (image == EGL_NO_IMAGE_KHR) {
721 // NOTE: if dpy was invalid, createImage() is guaranteed to
722 // fail. so we'd end up here.
723 return -EINVAL;
724 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800725 }
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800726
727 GLint error;
728 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700729 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800730 }
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700731
Jamie Gennis8606fef2011-09-28 12:13:31 -0700732 glBindTexture(mTexTarget, mTexName);
733 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700734
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800735 bool failed = false;
736 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700737 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700738 image, buf, error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800739 failed = true;
740 }
741 if (failed) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800742 return -EINVAL;
743 }
Jamie Gennisf7acf162011-01-12 18:30:40 -0800744
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700745 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
746 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, buf,
747 mSlots[buf].mGraphicBuffer->handle);
748
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700749 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis9fb59762011-06-27 15:41:52 -0700750 // The current buffer becomes FREE if it was still in the queued
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700751 // state. If it has already been given to the client
752 // (synchronous mode), then it stays in DEQUEUED state.
753 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
754 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
755 }
756
Jamie Gennisf7acf162011-01-12 18:30:40 -0800757 // Update the SurfaceTexture state.
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700758 mCurrentTexture = buf;
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700759 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennisa2187152011-05-19 13:33:00 -0700760 mCurrentCrop = mSlots[buf].mCrop;
761 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian09d7ed72011-07-13 15:24:42 -0700762 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennisa2187152011-05-19 13:33:00 -0700763 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Genniseadfb672011-06-12 17:03:06 -0700764 computeCurrentTransformMatrix();
Jamie Gennis9fb59762011-06-27 15:41:52 -0700765
766 // Now that we've passed the point at which failures can happen,
767 // it's safe to remove the buffer from the front of the queue.
768 mQueue.erase(front);
Mathias Agopian5bbb1cf2011-04-21 18:52:51 -0700769 mDequeueCondition.signal();
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700770 } else {
771 // We always bind the texture even if we don't update its contents.
Jamie Gennis8606fef2011-09-28 12:13:31 -0700772 glBindTexture(mTexTarget, mTexName);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800773 }
Jamie Gennis9fb59762011-06-27 15:41:52 -0700774
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800775 return OK;
776}
777
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700778bool SurfaceTexture::isExternalFormat(uint32_t format)
779{
780 switch (format) {
781 // supported YUV formats
782 case HAL_PIXEL_FORMAT_YV12:
783 // Legacy/deprecated YUV formats
784 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
785 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
786 case HAL_PIXEL_FORMAT_YCbCr_422_I:
787 return true;
788 }
789
790 // Any OEM format needs to be considered
791 if (format>=0x100 && format<=0x1FF)
792 return true;
793
794 return false;
795}
796
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700797GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennis8606fef2011-09-28 12:13:31 -0700798 return mTexTarget;
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700799}
800
Jamie Gennisb598fb92011-01-09 16:33:17 -0800801void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisb598fb92011-01-09 16:33:17 -0800802 Mutex::Autolock lock(mMutex);
Jamie Genniseadfb672011-06-12 17:03:06 -0700803 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
804}
805
806void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700807 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisb598fb92011-01-09 16:33:17 -0800808
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800809 float xform[16];
810 for (int i = 0; i < 16; i++) {
811 xform[i] = mtxIdentity[i];
812 }
813 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
814 float result[16];
815 mtxMul(result, xform, mtxFlipH);
816 for (int i = 0; i < 16; i++) {
817 xform[i] = result[i];
818 }
819 }
820 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
821 float result[16];
822 mtxMul(result, xform, mtxFlipV);
823 for (int i = 0; i < 16; i++) {
824 xform[i] = result[i];
825 }
826 }
827 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
828 float result[16];
829 mtxMul(result, xform, mtxRot90);
830 for (int i = 0; i < 16; i++) {
831 xform[i] = result[i];
832 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800833 }
834
835 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800836 float tx, ty, sx, sy;
837 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800838 // In order to prevent bilinear sampling at the of the crop rectangle we
839 // may need to shrink it by 2 texels in each direction. Normally this
840 // would just need to take 1/2 a texel off each end, but because the
841 // chroma channels will likely be subsampled we need to chop off a whole
842 // texel. This will cause artifacts if someone does nearest sampling
843 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
844 // accomodate the bilinear and nearest sampling uses.
845 //
846 // If nearest sampling turns out to be a desirable usage of these
847 // textures then we could add the ability to switch a SurfaceTexture to
848 // nearest-mode. Preferably, however, the image producers (video
849 // decoder, camera, etc.) would simply not use a crop rectangle (or at
850 // least not tell the framework about it) so that the GPU can do the
851 // correct edge behavior.
852 int xshrink = 0, yshrink = 0;
853 if (mCurrentCrop.left > 0) {
854 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
855 xshrink++;
856 } else {
857 tx = 0.0f;
858 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700859 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800860 xshrink++;
861 }
Mathias Agopiane5a1bff2011-03-31 19:10:24 -0700862 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800863 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
864 float(buf->getHeight());
865 yshrink++;
866 } else {
867 ty = 0.0f;
868 }
869 if (mCurrentCrop.top > 0) {
870 yshrink++;
871 }
872 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
873 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800874 } else {
875 tx = 0.0f;
876 ty = 0.0f;
877 sx = 1.0f;
878 sy = 1.0f;
879 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800880 float crop[16] = {
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800881 sx, 0, 0, 0,
882 0, sy, 0, 0,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800883 0, 0, 1, 0,
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800884 tx, ty, 0, 1,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800885 };
886
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800887 float mtxBeforeFlipV[16];
888 mtxMul(mtxBeforeFlipV, crop, xform);
889
890 // SurfaceFlinger expects the top of its window textures to be at a Y
891 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
892 // want to expose this to applications, however, so we must add an
893 // additional vertical flip to the transform after all the other transforms.
Jamie Genniseadfb672011-06-12 17:03:06 -0700894 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800895}
896
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800897nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700898 ST_LOGV("getTimestamp");
Eino-Ville Talvalac5f94d82011-02-18 11:02:42 -0800899 Mutex::Autolock lock(mMutex);
900 return mCurrentTimestamp;
901}
902
Jamie Gennis376590d2011-01-13 14:43:36 -0800903void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700904 const sp<FrameAvailableListener>& listener) {
Jamie Gennise4bd0f32011-10-19 13:36:31 -0700905 ST_LOGV("setFrameAvailableListener");
Jamie Gennis376590d2011-01-13 14:43:36 -0800906 Mutex::Autolock lock(mMutex);
Pannag Sanketic9ec69e2011-06-24 09:56:27 -0700907 mFrameAvailableListener = listener;
Jamie Gennis376590d2011-01-13 14:43:36 -0800908}
909
Mathias Agopian5c715752011-08-10 17:35:09 -0700910void SurfaceTexture::freeBufferLocked(int i) {
911 mSlots[i].mGraphicBuffer = 0;
912 mSlots[i].mBufferState = BufferSlot::FREE;
Sunita Nadampallif1e868f2011-11-09 18:23:41 -0600913 mSlots[i].mFrameNumber = 0;
Mathias Agopian5c715752011-08-10 17:35:09 -0700914 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
915 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
916 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
917 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
918 }
919}
920
Mathias Agopiana04cda92011-08-10 15:28:58 -0700921void SurfaceTexture::freeAllBuffersLocked() {
922 LOGW_IF(!mQueue.isEmpty(),
923 "freeAllBuffersLocked called but mQueue is not empty");
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700924 mCurrentTexture = INVALID_BUFFER_SLOT;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800925 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
Mathias Agopian5c715752011-08-10 17:35:09 -0700926 freeBufferLocked(i);
927 }
928}
929
930void SurfaceTexture::freeAllBuffersExceptHeadLocked() {
931 LOGW_IF(!mQueue.isEmpty(),
932 "freeAllBuffersExceptCurrentLocked called but mQueue is not empty");
933 int head = -1;
934 if (!mQueue.empty()) {
935 Fifo::iterator front(mQueue.begin());
936 head = *front;
937 }
Mathias Agopian8618ebc2011-08-17 15:42:04 -0700938 mCurrentTexture = INVALID_BUFFER_SLOT;
Mathias Agopian5c715752011-08-10 17:35:09 -0700939 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
940 if (i != head) {
941 freeBufferLocked(i);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800942 }
943 }
944}
945
Mathias Agopian5c715752011-08-10 17:35:09 -0700946status_t SurfaceTexture::drainQueueLocked() {
Mathias Agopian71fd1212011-08-10 16:33:23 -0700947 while (mSynchronousMode && !mQueue.isEmpty()) {
948 mDequeueCondition.wait(mMutex);
Mathias Agopian5c715752011-08-10 17:35:09 -0700949 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700950 ST_LOGE("drainQueueLocked: SurfaceTexture has been abandoned!");
Mathias Agopian5c715752011-08-10 17:35:09 -0700951 return NO_INIT;
952 }
953 if (mConnectedApi == NO_CONNECTED_API) {
Jamie Gennis44a05222011-09-16 17:30:26 -0700954 ST_LOGE("drainQueueLocked: SurfaceTexture is not connected!");
Mathias Agopian5c715752011-08-10 17:35:09 -0700955 return NO_INIT;
956 }
Mathias Agopian71fd1212011-08-10 16:33:23 -0700957 }
Mathias Agopian5c715752011-08-10 17:35:09 -0700958 return NO_ERROR;
959}
960
961status_t SurfaceTexture::drainQueueAndFreeBuffersLocked() {
962 status_t err = drainQueueLocked();
963 if (err == NO_ERROR) {
964 if (mSynchronousMode) {
965 freeAllBuffersLocked();
966 } else {
967 freeAllBuffersExceptHeadLocked();
968 }
969 }
970 return err;
Mathias Agopian71fd1212011-08-10 16:33:23 -0700971}
972
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800973EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
974 const sp<GraphicBuffer>& graphicBuffer) {
975 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
976 EGLint attrs[] = {
977 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
978 EGL_NONE,
979 };
980 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
981 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian6fad64c2011-04-26 14:57:40 -0700982 if (image == EGL_NO_IMAGE_KHR) {
983 EGLint error = eglGetError();
Jamie Gennis44a05222011-09-16 17:30:26 -0700984 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800985 }
986 return image;
987}
988
Mathias Agopian27cd07c2011-04-11 21:19:55 -0700989sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
990 Mutex::Autolock lock(mMutex);
991 return mCurrentTextureBuf;
992}
993
994Rect SurfaceTexture::getCurrentCrop() const {
995 Mutex::Autolock lock(mMutex);
996 return mCurrentCrop;
997}
998
999uint32_t SurfaceTexture::getCurrentTransform() const {
1000 Mutex::Autolock lock(mMutex);
1001 return mCurrentTransform;
1002}
1003
Mathias Agopian09d7ed72011-07-13 15:24:42 -07001004uint32_t SurfaceTexture::getCurrentScalingMode() const {
1005 Mutex::Autolock lock(mMutex);
1006 return mCurrentScalingMode;
1007}
1008
Mathias Agopianed3894c2011-04-20 14:20:59 -07001009int SurfaceTexture::query(int what, int* outValue)
1010{
1011 Mutex::Autolock lock(mMutex);
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001012
1013 if (mAbandoned) {
Jamie Gennis44a05222011-09-16 17:30:26 -07001014 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001015 return NO_INIT;
1016 }
1017
Mathias Agopianed3894c2011-04-20 14:20:59 -07001018 int value;
1019 switch (what) {
1020 case NATIVE_WINDOW_WIDTH:
1021 value = mDefaultWidth;
Mathias Agopianed3894c2011-04-20 14:20:59 -07001022 break;
1023 case NATIVE_WINDOW_HEIGHT:
1024 value = mDefaultHeight;
Mathias Agopianed3894c2011-04-20 14:20:59 -07001025 break;
1026 case NATIVE_WINDOW_FORMAT:
1027 value = mPixelFormat;
1028 break;
1029 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
1030 value = mSynchronousMode ?
1031 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
1032 break;
1033 default:
1034 return BAD_VALUE;
1035 }
1036 outValue[0] = value;
1037 return NO_ERROR;
1038}
Mathias Agopian27cd07c2011-04-11 21:19:55 -07001039
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001040void SurfaceTexture::abandon() {
1041 Mutex::Autolock lock(mMutex);
Mathias Agopian5c715752011-08-10 17:35:09 -07001042 mQueue.clear();
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001043 mAbandoned = true;
Mathias Agopianec46b4e2011-08-03 15:18:36 -07001044 mCurrentTextureBuf.clear();
Mathias Agopian5c715752011-08-10 17:35:09 -07001045 freeAllBuffersLocked();
Jamie Gennis5ef59bc2011-07-19 12:08:33 -07001046 mDequeueCondition.signal();
1047}
1048
Jamie Gennis44a05222011-09-16 17:30:26 -07001049void SurfaceTexture::setName(const String8& name) {
1050 mName = name;
1051}
1052
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001053void SurfaceTexture::dump(String8& result) const
1054{
1055 char buffer[1024];
1056 dump(result, "", buffer, 1024);
1057}
1058
1059void SurfaceTexture::dump(String8& result, const char* prefix,
1060 char* buffer, size_t SIZE) const
1061{
1062 Mutex::Autolock _l(mMutex);
1063 snprintf(buffer, SIZE,
1064 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
1065 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennis44a05222011-09-16 17:30:26 -07001066 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
1067 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001068 result.append(buffer);
1069
1070 String8 fifo;
1071 int fifoSize = 0;
1072 Fifo::const_iterator i(mQueue.begin());
1073 while (i != mQueue.end()) {
1074 snprintf(buffer, SIZE, "%02d ", *i++);
1075 fifoSize++;
1076 fifo.append(buffer);
1077 }
1078
1079 snprintf(buffer, SIZE,
Jamie Gennisecfa1d32011-07-19 17:58:43 -07001080 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001081 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
1082 ,
1083 prefix, mCurrentCrop.left,
1084 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennisecfa1d32011-07-19 17:58:43 -07001085 mCurrentTransform, mCurrentTexture,
Jamie Gennis44a05222011-09-16 17:30:26 -07001086 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
1087 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001088 );
1089 result.append(buffer);
1090
1091 struct {
1092 const char * operator()(int state) const {
1093 switch (state) {
1094 case BufferSlot::DEQUEUED: return "DEQUEUED";
1095 case BufferSlot::QUEUED: return "QUEUED";
1096 case BufferSlot::FREE: return "FREE";
1097 default: return "Unknown";
1098 }
1099 }
1100 } stateName;
1101
1102 for (int i=0 ; i<mBufferCount ; i++) {
1103 const BufferSlot& slot(mSlots[i]);
1104 snprintf(buffer, SIZE,
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001105 "%s%s[%02d] "
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001106 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian49541932011-08-09 15:48:43 -07001107 "transform=0x%02x, timestamp=%lld",
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001108 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopian0c3367f2011-08-08 16:02:13 -07001109 stateName(slot.mBufferState),
Jamie Gennis44a05222011-09-16 17:30:26 -07001110 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
1111 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001112 );
1113 result.append(buffer);
Mathias Agopian49541932011-08-09 15:48:43 -07001114
1115 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
1116 if (buf != NULL) {
1117 snprintf(buffer, SIZE,
1118 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennis44a05222011-09-16 17:30:26 -07001119 buf->handle, buf->width, buf->height, buf->stride,
1120 buf->format);
Mathias Agopian49541932011-08-09 15:48:43 -07001121 result.append(buffer);
1122 }
1123 result.append("\n");
Mathias Agopian7f3289c2011-05-09 19:08:33 -07001124 }
1125}
1126
Jamie Gennisb598fb92011-01-09 16:33:17 -08001127static void mtxMul(float out[16], const float a[16], const float b[16]) {
1128 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1129 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1130 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1131 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1132
1133 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1134 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1135 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1136 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1137
1138 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1139 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1140 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1141 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1142
1143 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1144 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1145 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1146 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1147}
1148
Jamie Gennis68e4a7a2010-12-20 11:27:26 -08001149}; // namespace android