blob: 8ef885b3ef5aea9e8b2b2d7d11cd8c69831c8a82 [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 Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise70d8b42011-01-09 13:24:09 -080019//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080020
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
28
Mathias Agopian7a042bf2011-04-11 21:19:55 -070029#include <hardware/hardware.h>
30
Mathias Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/IGraphicBufferAlloc.h>
32#include <gui/ISurfaceComposer.h>
33#include <gui/SurfaceComposerClient.h>
34#include <gui/SurfaceTexture.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080035
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#include <private/gui/ComposerService.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080037
38#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070039#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080040#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080041
Jamie Gennis86edf4f2011-11-14 14:51:01 -080042// This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension
43// to synchronize access to the buffers. It will cause dequeueBuffer to stall,
44// waiting for the GL reads for the buffer being dequeued to complete before
45// allowing the buffer to be dequeued.
46#ifdef USE_FENCE_SYNC
47#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
48#error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible"
49#endif
50#endif
51
Jamie Gennisfa28c352011-09-16 17:30:26 -070052// Macros for including the SurfaceTexture name in log messages
Steve Block6807e592011-10-20 11:56:00 +010053#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000054#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000055#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000056#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000057#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070058
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080059namespace android {
60
Jamie Gennisf238e282011-01-09 16:33:17 -080061// Transform matrices
62static float mtxIdentity[16] = {
63 1, 0, 0, 0,
64 0, 1, 0, 0,
65 0, 0, 1, 0,
66 0, 0, 0, 1,
67};
68static float mtxFlipH[16] = {
69 -1, 0, 0, 0,
70 0, 1, 0, 0,
71 0, 0, 1, 0,
72 1, 0, 0, 1,
73};
74static float mtxFlipV[16] = {
75 1, 0, 0, 0,
76 0, -1, 0, 0,
77 0, 0, 1, 0,
78 0, 1, 0, 1,
79};
80static float mtxRot90[16] = {
81 0, 1, 0, 0,
82 -1, 0, 0, 0,
83 0, 0, 1, 0,
84 1, 0, 0, 1,
85};
86static float mtxRot180[16] = {
87 -1, 0, 0, 0,
88 0, -1, 0, 0,
89 0, 0, 1, 0,
90 1, 1, 0, 1,
91};
92static float mtxRot270[16] = {
93 0, -1, 0, 0,
94 1, 0, 0, 0,
95 0, 0, 1, 0,
96 0, 1, 0, 1,
97};
98
99static void mtxMul(float out[16], const float a[16], const float b[16]);
100
Daniel Lameae59d22012-01-22 15:26:27 -0800101// Get an ID that's unique within this process.
102static int32_t createProcessUniqueId() {
103 static volatile int32_t globalCounter = 0;
104 return android_atomic_inc(&globalCounter);
105}
Jamie Gennisfa28c352011-09-16 17:30:26 -0700106
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700107SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Daniel Lamb2675792012-02-23 14:35:13 -0800108 GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) :
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800109 mCurrentTransform(0),
110 mCurrentTimestamp(0),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700111 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700112 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800113#ifdef USE_FENCE_SYNC
114 mUseFenceSync(useFenceSync),
115#else
116 mUseFenceSync(false),
117#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800118 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700119 mEglDisplay(EGL_NO_DISPLAY),
120 mEglContext(EGL_NO_CONTEXT),
Daniel Lameae59d22012-01-22 15:26:27 -0800121 mAbandoned(false),
Jamie Gennis74bed552012-03-28 19:05:54 -0700122 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
123 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800124{
Daniel Lameae59d22012-01-22 15:26:27 -0800125 // Choose a name using the PID and a process-unique ID.
126 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700127 ST_LOGV("SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800128 if (bufferQueue == 0) {
Daniel Lamb2675792012-02-23 14:35:13 -0800129 ST_LOGV("Creating a new BufferQueue");
130 mBufferQueue = new BufferQueue(allowSynchronousMode);
131 }
132 else {
133 mBufferQueue = bufferQueue;
134 }
Daniel Lamb2675792012-02-23 14:35:13 -0800135
Jamie Gennisfa28c352011-09-16 17:30:26 -0700136 memcpy(mCurrentTransformMatrix, mtxIdentity,
137 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700138
139 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
140 // reference once the ctor ends, as that would cause the refcount of 'this'
141 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
142 // that's what we create.
143 wp<BufferQueue::ConsumerListener> listener;
144 sp<BufferQueue::ConsumerListener> proxy;
145 listener = static_cast<BufferQueue::ConsumerListener*>(this);
146 proxy = new BufferQueue::ProxyConsumerListener(listener);
147
148 status_t err = mBufferQueue->consumerConnect(proxy);
149 if (err != NO_ERROR) {
150 ST_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)",
151 strerror(-err), err);
152 } else {
153 mBufferQueue->setConsumerName(mName);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700154 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700155 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800156}
157
158SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700159 ST_LOGV("~SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800160
Daniel Lameae59d22012-01-22 15:26:27 -0800161 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800162}
163
Mathias Agopian80727112011-05-02 19:51:12 -0700164status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
165 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800166 return mBufferQueue->setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700167}
168
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800169
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700170status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
171{
Daniel Lamb2675792012-02-23 14:35:13 -0800172 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700173 mDefaultWidth = w;
174 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800175 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700176}
177
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178status_t SurfaceTexture::updateTexImage() {
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700179 return SurfaceTexture::updateTexImage(NULL);
180}
181
182status_t SurfaceTexture::updateTexImage(BufferRejecter* rejecter) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800183 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700184 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800185 Mutex::Autolock lock(mMutex);
186
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700187 status_t err = NO_ERROR;
188
Mathias Agopiane47498f2011-08-08 19:35:15 -0700189 if (mAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700190 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700191 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700192 }
193
Jamie Gennis74bed552012-03-28 19:05:54 -0700194 if (!mAttached) {
195 ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL "
196 "ES context");
197 return INVALID_OPERATION;
198 }
199
Jamie Gennisce561372012-03-19 18:33:05 -0700200 EGLDisplay dpy = eglGetCurrentDisplay();
201 EGLContext ctx = eglGetCurrentContext();
202
Jamie Gennis74bed552012-03-28 19:05:54 -0700203 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
204 dpy == EGL_NO_DISPLAY) {
Jamie Gennisce561372012-03-19 18:33:05 -0700205 ST_LOGE("updateTexImage: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700206 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700207 }
208
Jamie Gennis74bed552012-03-28 19:05:54 -0700209 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
210 ctx == EGL_NO_CONTEXT) {
Jamie Gennisce561372012-03-19 18:33:05 -0700211 ST_LOGE("updateTexImage: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700212 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700213 }
214
215 mEglDisplay = dpy;
216 mEglContext = ctx;
217
Daniel Lamb2675792012-02-23 14:35:13 -0800218 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800219
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700220 // In asynchronous mode the list is guaranteed to be one buffer
221 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lamfbcda932012-04-09 22:51:52 -0700222 err = mBufferQueue->acquireBuffer(&item);
223 if (err == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800224 int buf = item.mBuf;
225 // This buffer was newly allocated, so we need to clean up on our side
226 if (item.mGraphicBuffer != NULL) {
227 mEGLSlots[buf].mGraphicBuffer = 0;
228 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700229 eglDestroyImageKHR(dpy, mEGLSlots[buf].mEglImage);
Daniel Lameae59d22012-01-22 15:26:27 -0800230 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800231 }
232 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
233 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700234
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700235 // we call the rejecter here, in case the caller has a reason to
236 // not accept this buffer. this is used by SurfaceFlinger to
237 // reject buffers which have the wrong size
238 if (rejecter && rejecter->reject(mEGLSlots[buf].mGraphicBuffer, item)) {
Jesse Hallef194142012-06-14 14:45:17 -0700239 mBufferQueue->releaseBuffer(buf, dpy, mEGLSlots[buf].mFence,
240 mEGLSlots[buf].mReleaseFence);
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700241 mEGLSlots[buf].mFence = EGL_NO_SYNC_KHR;
Jesse Hallef194142012-06-14 14:45:17 -0700242 mEGLSlots[buf].mReleaseFence.clear();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700243 glBindTexture(mTexTarget, mTexName);
244 return NO_ERROR;
245 }
246
Jesse Hall90ed8502012-05-16 23:44:34 -0700247 // Update the GL texture object. We may have to do this even when
248 // item.mGraphicBuffer == NULL, if we destroyed the EGLImage when
249 // detaching from a context but the buffer has not been re-allocated.
Daniel Lameae59d22012-01-22 15:26:27 -0800250 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800251 if (image == EGL_NO_IMAGE_KHR) {
Jesse Hall90ed8502012-05-16 23:44:34 -0700252 if (mEGLSlots[buf].mGraphicBuffer == NULL) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700253 ST_LOGE("updateTexImage: buffer at slot %d is null", buf);
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700254 err = BAD_VALUE;
255 } else {
256 image = createImage(dpy, mEGLSlots[buf].mGraphicBuffer);
257 mEGLSlots[buf].mEglImage = image;
258 if (image == EGL_NO_IMAGE_KHR) {
259 // NOTE: if dpy was invalid, createImage() is guaranteed to
260 // fail. so we'd end up here.
261 err = UNKNOWN_ERROR;
262 }
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700263 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800264 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800265
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700266 if (err == NO_ERROR) {
267 GLint error;
268 while ((error = glGetError()) != GL_NO_ERROR) {
269 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
270 }
271
272 glBindTexture(mTexTarget, mTexName);
273 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
274
275 while ((error = glGetError()) != GL_NO_ERROR) {
276 ST_LOGE("updateTexImage: error binding external texture image %p "
277 "(slot %d): %#04x", image, buf, error);
278 err = UNKNOWN_ERROR;
279 }
280
281 if (err == NO_ERROR) {
282 err = syncForReleaseLocked(dpy);
283 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800284 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700285
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700286 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700287 // Release the buffer we just acquired. It's not safe to
288 // release the old buffer, so instead we just drop the new frame.
Jesse Hallef194142012-06-14 14:45:17 -0700289 mBufferQueue->releaseBuffer(buf, dpy, mEGLSlots[buf].mFence,
290 mEGLSlots[buf].mReleaseFence);
Jamie Gennis74bed552012-03-28 19:05:54 -0700291 mEGLSlots[buf].mFence = EGL_NO_SYNC_KHR;
Jesse Hallef194142012-06-14 14:45:17 -0700292 mEGLSlots[buf].mReleaseFence.clear();
Jamie Gennis74bed552012-03-28 19:05:54 -0700293 return err;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800294 }
295
296 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
297 mCurrentTexture,
298 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800299 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700300
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700301 // release old buffer
Jamie Gennis74bed552012-03-28 19:05:54 -0700302 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700303 status_t status = mBufferQueue->releaseBuffer(mCurrentTexture, dpy,
Jesse Hallef194142012-06-14 14:45:17 -0700304 mEGLSlots[mCurrentTexture].mFence,
305 mEGLSlots[mCurrentTexture].mReleaseFence);
Jamie Gennis74bed552012-03-28 19:05:54 -0700306 mEGLSlots[mCurrentTexture].mFence = EGL_NO_SYNC_KHR;
Jesse Hallef194142012-06-14 14:45:17 -0700307 mEGLSlots[mCurrentTexture].mReleaseFence.clear();
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700308 if (status == BufferQueue::STALE_BUFFER_SLOT) {
309 freeBufferLocked(mCurrentTexture);
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700310 } else if (status != NO_ERROR) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700311 ST_LOGE("updateTexImage: released invalid buffer");
312 err = status;
313 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700314 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700315
Jamie Gennis9a78c902011-01-12 18:30:40 -0800316 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700317 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800318 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
319 mCurrentCrop = item.mCrop;
320 mCurrentTransform = item.mTransform;
321 mCurrentScalingMode = item.mScalingMode;
322 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700323 computeCurrentTransformMatrix();
Daniel Lamfbcda932012-04-09 22:51:52 -0700324 } else {
325 if (err < 0) {
326 ALOGE("updateTexImage failed on acquire %d", err);
327 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700328 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700329 glBindTexture(mTexTarget, mTexName);
Daniel Lamfbcda932012-04-09 22:51:52 -0700330 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800331 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700332
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700333 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800334}
335
Jesse Hallef194142012-06-14 14:45:17 -0700336void SurfaceTexture::setReleaseFence(int fenceFd) {
337 if (fenceFd == -1)
338 return;
339 sp<Fence> fence(new Fence(fenceFd));
340 if (!mEGLSlots[mCurrentTexture].mReleaseFence.get()) {
341 mEGLSlots[mCurrentTexture].mReleaseFence = fence;
342 } else {
343 sp<Fence> mergedFence = Fence::merge(
344 String8("SurfaceTexture merged release"),
345 mEGLSlots[mCurrentTexture].mReleaseFence, fence);
346 if (mergedFence.get()) {
347 ALOGE("failed to merge release fences");
348 // synchronization is broken, the best we can do is hope fences
349 // signal in order so the new fence will act like a union
350 mEGLSlots[mCurrentTexture].mReleaseFence = fence;
351 } else {
352 mEGLSlots[mCurrentTexture].mReleaseFence = mergedFence;
353 }
354 }
355}
356
Jamie Gennis74bed552012-03-28 19:05:54 -0700357status_t SurfaceTexture::detachFromContext() {
358 ATRACE_CALL();
359 ST_LOGV("detachFromContext");
360 Mutex::Autolock lock(mMutex);
361
362 if (mAbandoned) {
363 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
364 return NO_INIT;
365 }
366
367 if (!mAttached) {
368 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
369 "context");
370 return INVALID_OPERATION;
371 }
372
373 EGLDisplay dpy = eglGetCurrentDisplay();
374 EGLContext ctx = eglGetCurrentContext();
375
376 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
377 ST_LOGE("detachFromContext: invalid current EGLDisplay");
378 return INVALID_OPERATION;
379 }
380
381 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
382 ST_LOGE("detachFromContext: invalid current EGLContext");
383 return INVALID_OPERATION;
384 }
385
386 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
387 status_t err = syncForReleaseLocked(dpy);
388 if (err != OK) {
389 return err;
390 }
391
392 glDeleteTextures(1, &mTexName);
393 }
394
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700395 // Because we're giving up the EGLDisplay we need to free all the EGLImages
396 // that are associated with it. They'll be recreated when the
397 // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a
398 // new EGLDisplay).
399 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
400 EGLImageKHR img = mEGLSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700401 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700402 eglDestroyImageKHR(mEglDisplay, img);
403 mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
404 }
405 }
406
Jamie Gennis74bed552012-03-28 19:05:54 -0700407 mEglDisplay = EGL_NO_DISPLAY;
408 mEglContext = EGL_NO_CONTEXT;
409 mAttached = false;
410
411 return OK;
412}
413
414status_t SurfaceTexture::attachToContext(GLuint tex) {
415 ATRACE_CALL();
416 ST_LOGV("attachToContext");
417 Mutex::Autolock lock(mMutex);
418
419 if (mAbandoned) {
420 ST_LOGE("attachToContext: abandoned SurfaceTexture");
421 return NO_INIT;
422 }
423
424 if (mAttached) {
425 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
426 "context");
427 return INVALID_OPERATION;
428 }
429
430 EGLDisplay dpy = eglGetCurrentDisplay();
431 EGLContext ctx = eglGetCurrentContext();
432
433 if (dpy == EGL_NO_DISPLAY) {
434 ST_LOGE("attachToContext: invalid current EGLDisplay");
435 return INVALID_OPERATION;
436 }
437
438 if (ctx == EGL_NO_CONTEXT) {
439 ST_LOGE("attachToContext: invalid current EGLContext");
440 return INVALID_OPERATION;
441 }
442
443 // We need to bind the texture regardless of whether there's a current
444 // buffer.
445 glBindTexture(mTexTarget, tex);
446
447 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700448 // The EGLImageKHR that was associated with the slot was destroyed when
449 // the SurfaceTexture was detached from the old context, so we need to
450 // recreate it here.
451 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
452 if (image == EGL_NO_IMAGE_KHR) {
453 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700454 }
455
456 // Attach the current buffer to the GL texture.
457 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
458
459 GLint error;
460 status_t err = OK;
461 while ((error = glGetError()) != GL_NO_ERROR) {
462 ST_LOGE("attachToContext: error binding external texture image %p "
463 "(slot %d): %#04x", image, mCurrentTexture, error);
464 err = UNKNOWN_ERROR;
465 }
466
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700467 // We destroy the EGLImageKHR here because the current buffer may no
468 // longer be associated with one of the buffer slots, so we have
469 // nowhere to to store it. If the buffer is still associated with a
470 // slot then another EGLImageKHR will be created next time that buffer
471 // gets acquired in updateTexImage.
472 eglDestroyImageKHR(dpy, image);
Jamie Gennis74bed552012-03-28 19:05:54 -0700473
474 if (err != OK) {
475 return err;
476 }
477 }
478
479 mEglDisplay = dpy;
480 mEglContext = ctx;
481 mTexName = tex;
482 mAttached = true;
483
484 return OK;
485}
486
487status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
488 ST_LOGV("syncForReleaseLocked");
489
490 if (mUseFenceSync && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
491 EGLSyncKHR fence = mEGLSlots[mCurrentTexture].mFence;
492 if (fence != EGL_NO_SYNC_KHR) {
493 // There is already a fence for the current slot. We need to wait
494 // on that before replacing it with another fence to ensure that all
495 // outstanding buffer accesses have completed before the producer
496 // accesses it.
497 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
498 if (result == EGL_FALSE) {
499 ST_LOGE("syncForReleaseLocked: error waiting for previous "
500 "fence: %#x", eglGetError());
501 return UNKNOWN_ERROR;
502 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
503 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
504 "fence");
505 return TIMED_OUT;
506 }
507 eglDestroySyncKHR(dpy, fence);
508 }
509
510 // Create a fence for the outstanding accesses in the current OpenGL ES
511 // context.
512 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
513 if (fence == EGL_NO_SYNC_KHR) {
514 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
515 eglGetError());
516 return UNKNOWN_ERROR;
517 }
518 glFlush();
519 mEGLSlots[mCurrentTexture].mFence = fence;
520 }
521
522 return OK;
523}
524
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700525bool SurfaceTexture::isExternalFormat(uint32_t format)
526{
527 switch (format) {
528 // supported YUV formats
529 case HAL_PIXEL_FORMAT_YV12:
530 // Legacy/deprecated YUV formats
531 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
532 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
533 case HAL_PIXEL_FORMAT_YCbCr_422_I:
534 return true;
535 }
536
537 // Any OEM format needs to be considered
538 if (format>=0x100 && format<=0x1FF)
539 return true;
540
541 return false;
542}
543
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700544GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700545 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700546}
547
Jamie Gennisf238e282011-01-09 16:33:17 -0800548void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800549 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700550 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
551}
552
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700553void SurfaceTexture::setFilteringEnabled(bool enabled) {
554 Mutex::Autolock lock(mMutex);
555 bool needsRecompute = mFilteringEnabled != enabled;
556 mFilteringEnabled = enabled;
557 if (needsRecompute) {
558 computeCurrentTransformMatrix();
559 }
560}
561
Jamie Gennis736aa952011-06-12 17:03:06 -0700562void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700563 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800564
Jamie Gennisa214c642011-01-14 13:53:31 -0800565 float xform[16];
566 for (int i = 0; i < 16; i++) {
567 xform[i] = mtxIdentity[i];
568 }
569 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
570 float result[16];
571 mtxMul(result, xform, mtxFlipH);
572 for (int i = 0; i < 16; i++) {
573 xform[i] = result[i];
574 }
575 }
576 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
577 float result[16];
578 mtxMul(result, xform, mtxFlipV);
579 for (int i = 0; i < 16; i++) {
580 xform[i] = result[i];
581 }
582 }
583 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
584 float result[16];
585 mtxMul(result, xform, mtxRot90);
586 for (int i = 0; i < 16; i++) {
587 xform[i] = result[i];
588 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800589 }
590
Daniel Lameae59d22012-01-22 15:26:27 -0800591 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisd72f2332012-05-07 13:50:11 -0700592 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700593 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
594 float bufferWidth = buf->getWidth();
595 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700596 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700597 float shrinkAmount = 0.0f;
598 if (mFilteringEnabled) {
599 // In order to prevent bilinear sampling beyond the edge of the
600 // crop rectangle we may need to shrink it by 2 texels in each
601 // dimension. Normally this would just need to take 1/2 a texel
602 // off each end, but because the chroma channels of YUV420 images
603 // are subsampled we may need to shrink the crop region by a whole
604 // texel on each side.
605 switch (buf->getPixelFormat()) {
606 case PIXEL_FORMAT_RGBA_8888:
607 case PIXEL_FORMAT_RGBX_8888:
608 case PIXEL_FORMAT_RGB_888:
609 case PIXEL_FORMAT_RGB_565:
610 case PIXEL_FORMAT_BGRA_8888:
611 case PIXEL_FORMAT_RGBA_5551:
612 case PIXEL_FORMAT_RGBA_4444:
613 // We know there's no subsampling of any channels, so we
614 // only need to shrink by a half a pixel.
615 shrinkAmount = 0.5;
Jamie Gennis91a68262012-04-16 13:41:05 -0700616
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700617 default:
618 // If we don't recognize the format, we must assume the
619 // worst case (that we care about), which is YUV420.
620 shrinkAmount = 1.0;
621 }
622 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700623
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700624 // Only shrink the dimensions that are not the size of the buffer.
625 if (cropRect.width() < bufferWidth) {
626 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
627 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
628 bufferWidth;
629 }
630 if (cropRect.height() < bufferHeight) {
631 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
632 bufferHeight;
633 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
634 bufferHeight;
635 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800636 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800637 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800638 sx, 0, 0, 0,
639 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800640 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800641 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800642 };
643
Jamie Gennisa214c642011-01-14 13:53:31 -0800644 float mtxBeforeFlipV[16];
645 mtxMul(mtxBeforeFlipV, crop, xform);
646
647 // SurfaceFlinger expects the top of its window textures to be at a Y
648 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
649 // want to expose this to applications, however, so we must add an
650 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700651 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800652}
653
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800654nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700655 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800656 Mutex::Autolock lock(mMutex);
657 return mCurrentTimestamp;
658}
659
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800660void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700661 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700662 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800663 Mutex::Autolock lock(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700664 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800665}
666
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800667EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
668 const sp<GraphicBuffer>& graphicBuffer) {
669 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
670 EGLint attrs[] = {
671 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
672 EGL_NONE,
673 };
674 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
675 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700676 if (image == EGL_NO_IMAGE_KHR) {
677 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700678 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800679 }
680 return image;
681}
682
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700683sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
684 Mutex::Autolock lock(mMutex);
685 return mCurrentTextureBuf;
686}
687
688Rect SurfaceTexture::getCurrentCrop() const {
689 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700690
691 Rect outCrop = mCurrentCrop;
692 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
693 int32_t newWidth = mCurrentCrop.width();
694 int32_t newHeight = mCurrentCrop.height();
695
696 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
697 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
698 ST_LOGV("too wide: newWidth = %d", newWidth);
699 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
700 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
701 ST_LOGV("too tall: newHeight = %d", newHeight);
702 }
703
704 // The crop is too wide
705 if (newWidth < mCurrentCrop.width()) {
706 int32_t dw = (newWidth - mCurrentCrop.width())/2;
707 outCrop.left -=dw;
708 outCrop.right += dw;
709 // The crop is too tall
710 } else if (newHeight < mCurrentCrop.height()) {
711 int32_t dh = (newHeight - mCurrentCrop.height())/2;
712 outCrop.top -= dh;
713 outCrop.bottom += dh;
714 }
715
716 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
717 outCrop.left, outCrop.top,
718 outCrop.right,outCrop.bottom);
719 }
720
Daniel Lam016c8cb2012-04-03 15:54:58 -0700721 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700722}
723
724uint32_t SurfaceTexture::getCurrentTransform() const {
725 Mutex::Autolock lock(mMutex);
726 return mCurrentTransform;
727}
728
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700729uint32_t SurfaceTexture::getCurrentScalingMode() const {
730 Mutex::Autolock lock(mMutex);
731 return mCurrentScalingMode;
732}
733
Jamie Gennis59769462011-11-19 18:04:43 -0800734bool SurfaceTexture::isSynchronousMode() const {
735 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800736 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800737}
738
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700739void SurfaceTexture::freeBufferLocked(int slotIndex) {
740 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
741 mEGLSlots[slotIndex].mGraphicBuffer = 0;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700742 if (slotIndex == mCurrentTexture) {
743 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
744 }
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700745 EGLImageKHR img = mEGLSlots[slotIndex].mEglImage;
746 if (img != EGL_NO_IMAGE_KHR) {
747 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
748 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800749 }
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700750 mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700751}
Daniel Lameae59d22012-01-22 15:26:27 -0800752
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700753void SurfaceTexture::abandon() {
754 ST_LOGV("abandon");
755 Mutex::Autolock lock(mMutex);
756
757 if (!mAbandoned) {
758 mAbandoned = true;
759 mCurrentTextureBuf.clear();
760
761 // destroy all egl buffers
762 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
763 freeBufferLocked(i);
764 }
765
766 // disconnect from the BufferQueue
767 mBufferQueue->consumerDisconnect();
768 mBufferQueue.clear();
769 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700770}
771
Jamie Gennisfa28c352011-09-16 17:30:26 -0700772void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800773 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700774 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800775 mBufferQueue->setConsumerName(name);
776}
777
778status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
779 Mutex::Autolock lock(mMutex);
780 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
781}
782
783status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
784 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700785 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800786 return mBufferQueue->setConsumerUsageBits(usage);
787}
788
789status_t SurfaceTexture::setTransformHint(uint32_t hint) {
790 Mutex::Autolock lock(mMutex);
791 return mBufferQueue->setTransformHint(hint);
792}
793
794// Used for refactoring BufferQueue from SurfaceTexture
795// Should not be in final interface once users of SurfaceTexture are clean up.
796status_t SurfaceTexture::setSynchronousMode(bool enabled) {
797 Mutex::Autolock lock(mMutex);
798 return mBufferQueue->setSynchronousMode(enabled);
799}
800
801// Used for refactoring, should not be in final interface
802sp<BufferQueue> SurfaceTexture::getBufferQueue() const {
803 Mutex::Autolock lock(mMutex);
804 return mBufferQueue;
805}
806
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700807void SurfaceTexture::onFrameAvailable() {
808 ST_LOGV("onFrameAvailable");
809
810 sp<FrameAvailableListener> listener;
811 { // scope for the lock
812 Mutex::Autolock lock(mMutex);
813 listener = mFrameAvailableListener;
814 }
815
816 if (listener != NULL) {
817 ST_LOGV("actually calling onFrameAvailable");
818 listener->onFrameAvailable();
819 }
820}
821
822void SurfaceTexture::onBuffersReleased() {
823 ST_LOGV("onBuffersReleased");
824
825 Mutex::Autolock lock(mMutex);
826
827 if (mAbandoned) {
828 // Nothing to do if we're already abandoned.
829 return;
830 }
831
832 uint32_t mask = 0;
833 mBufferQueue->getReleasedBuffers(&mask);
834 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
835 if (mask & (1 << i)) {
836 freeBufferLocked(i);
837 }
838 }
839}
840
Mathias Agopian68c77942011-05-09 19:08:33 -0700841void SurfaceTexture::dump(String8& result) const
842{
843 char buffer[1024];
844 dump(result, "", buffer, 1024);
845}
846
847void SurfaceTexture::dump(String8& result, const char* prefix,
848 char* buffer, size_t SIZE) const
849{
850 Mutex::Autolock _l(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700851 snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName,
852 int(mAbandoned));
Mathias Agopian68c77942011-05-09 19:08:33 -0700853 result.append(buffer);
854
Mathias Agopian68c77942011-05-09 19:08:33 -0700855 snprintf(buffer, SIZE,
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700856 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n",
857 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700858 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800859 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700860 );
861 result.append(buffer);
862
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700863 if (!mAbandoned) {
864 mBufferQueue->dump(result, prefix, buffer, SIZE);
865 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700866}
867
Jamie Gennisf238e282011-01-09 16:33:17 -0800868static void mtxMul(float out[16], const float a[16], const float b[16]) {
869 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
870 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
871 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
872 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
873
874 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
875 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
876 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
877 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
878
879 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
880 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
881 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
882 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
883
884 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
885 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
886 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
887 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
888}
889
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800890}; // namespace android