blob: 36a2af7dcff0b31d5f5aecd50bde2f122d46d9a4 [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
Jamie Gennis9fea3422012-08-07 18:03:04 -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 Gennis01dbf552012-09-06 14:54:19 -070042// This compile option makes SurfaceTexture use the
43// EGL_ANDROID_native_fence_sync extension to create Android native fences to
44// signal when all GLES reads for a given buffer have completed. It is not
45// compatible with using the EGL_KHR_fence_sync extension for the same
46// purpose.
47#ifdef USE_NATIVE_FENCE_SYNC
48#ifdef USE_FENCE_SYNC
49#error "USE_NATIVE_FENCE_SYNC and USE_FENCE_SYNC are incompatible"
50#endif
Jamie Gennis61e04b92012-09-09 17:48:42 -070051static const bool useNativeFenceSync = true;
Jamie Gennis01dbf552012-09-06 14:54:19 -070052#else
Jamie Gennis61e04b92012-09-09 17:48:42 -070053static const bool useNativeFenceSync = false;
54#endif
55
56// This compile option makes SurfaceTexture use the EGL_ANDROID_sync_wait
57// extension to insert server-side waits into the GLES command stream. This
58// feature requires the EGL_ANDROID_native_fence_sync and
59// EGL_ANDROID_wait_sync extensions.
60#ifdef USE_WAIT_SYNC
61static const bool useWaitSync = true;
62#else
63static const bool useWaitSync = false;
Jamie Gennis01dbf552012-09-06 14:54:19 -070064#endif
65
Jamie Gennis86edf4f2011-11-14 14:51:01 -080066// This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension
67// to synchronize access to the buffers. It will cause dequeueBuffer to stall,
68// waiting for the GL reads for the buffer being dequeued to complete before
69// allowing the buffer to be dequeued.
70#ifdef USE_FENCE_SYNC
71#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
72#error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible"
73#endif
74#endif
75
Jamie Gennisfa28c352011-09-16 17:30:26 -070076// Macros for including the SurfaceTexture name in log messages
Steve Block6807e592011-10-20 11:56:00 +010077#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000078#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000079#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000080#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000081#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070082
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080083namespace android {
84
Jamie Gennisf238e282011-01-09 16:33:17 -080085// Transform matrices
86static float mtxIdentity[16] = {
87 1, 0, 0, 0,
88 0, 1, 0, 0,
89 0, 0, 1, 0,
90 0, 0, 0, 1,
91};
92static float mtxFlipH[16] = {
93 -1, 0, 0, 0,
94 0, 1, 0, 0,
95 0, 0, 1, 0,
96 1, 0, 0, 1,
97};
98static float mtxFlipV[16] = {
99 1, 0, 0, 0,
100 0, -1, 0, 0,
101 0, 0, 1, 0,
102 0, 1, 0, 1,
103};
104static float mtxRot90[16] = {
105 0, 1, 0, 0,
106 -1, 0, 0, 0,
107 0, 0, 1, 0,
108 1, 0, 0, 1,
109};
110static float mtxRot180[16] = {
111 -1, 0, 0, 0,
112 0, -1, 0, 0,
113 0, 0, 1, 0,
114 1, 1, 0, 1,
115};
116static float mtxRot270[16] = {
117 0, -1, 0, 0,
118 1, 0, 0, 0,
119 0, 0, 1, 0,
120 0, 1, 0, 1,
121};
122
123static void mtxMul(float out[16], const float a[16], const float b[16]);
124
Jamie Gennisfa28c352011-09-16 17:30:26 -0700125
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700126SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Daniel Lamb2675792012-02-23 14:35:13 -0800127 GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) :
Jamie Gennis9fea3422012-08-07 18:03:04 -0700128 ConsumerBase(bufferQueue == 0 ? new BufferQueue(allowSynchronousMode) : bufferQueue),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800129 mCurrentTransform(0),
130 mCurrentTimestamp(0),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700131 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700132 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800133#ifdef USE_FENCE_SYNC
134 mUseFenceSync(useFenceSync),
135#else
136 mUseFenceSync(false),
137#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800138 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700139 mEglDisplay(EGL_NO_DISPLAY),
140 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700141 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
142 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800143{
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700144 ST_LOGV("SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800145
Jamie Gennisfa28c352011-09-16 17:30:26 -0700146 memcpy(mCurrentTransformMatrix, mtxIdentity,
147 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700148
Jamie Gennis9fea3422012-08-07 18:03:04 -0700149 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800150}
151
Jamie Gennis31a353d2012-08-24 17:25:13 -0700152status_t SurfaceTexture::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700153 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -0700154 return mBufferQueue->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700155}
156
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800157
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700158status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
159{
Daniel Lamb2675792012-02-23 14:35:13 -0800160 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700161 mDefaultWidth = w;
162 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800163 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700164}
165
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800166status_t SurfaceTexture::updateTexImage() {
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700167 return SurfaceTexture::updateTexImage(NULL);
168}
169
Jamie Gennis9fea3422012-08-07 18:03:04 -0700170status_t SurfaceTexture::acquireBufferLocked(BufferQueue::BufferItem *item) {
171 status_t err = ConsumerBase::acquireBufferLocked(item);
172 if (err != NO_ERROR) {
173 return err;
174 }
175
176 int slot = item->mBuf;
177 if (item->mGraphicBuffer != NULL) {
178 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
179 eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage);
180 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
181 }
182 }
183
184 // Update the GL texture object. We may have to do this even when
185 // item.mGraphicBuffer == NULL, if we destroyed the EGLImage when
186 // detaching from a context but the buffer has not been re-allocated.
187 if (mEglSlots[slot].mEglImage == EGL_NO_IMAGE_KHR) {
188 EGLImageKHR image = createImage(mEglDisplay, mSlots[slot].mGraphicBuffer);
189 if (image == EGL_NO_IMAGE_KHR) {
190 return UNKNOWN_ERROR;
191 }
192 mEglSlots[slot].mEglImage = image;
193 }
194
195 return NO_ERROR;
196}
197
198status_t SurfaceTexture::releaseBufferLocked(int buf, EGLDisplay display,
Jamie Gennisb2725412012-09-05 20:09:05 -0700199 EGLSyncKHR eglFence) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700200 status_t err = ConsumerBase::releaseBufferLocked(buf, mEglDisplay,
Jamie Gennisb2725412012-09-05 20:09:05 -0700201 eglFence);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700202
203 mEglSlots[mCurrentTexture].mEglFence = EGL_NO_SYNC_KHR;
204
205 return err;
206}
207
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700208status_t SurfaceTexture::updateTexImage(BufferRejecter* rejecter) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800209 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700210 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800211 Mutex::Autolock lock(mMutex);
212
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700213 status_t err = NO_ERROR;
214
Mathias Agopiane47498f2011-08-08 19:35:15 -0700215 if (mAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700216 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700217 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700218 }
219
Jamie Gennis74bed552012-03-28 19:05:54 -0700220 if (!mAttached) {
221 ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL "
222 "ES context");
223 return INVALID_OPERATION;
224 }
225
Jamie Gennisce561372012-03-19 18:33:05 -0700226 EGLDisplay dpy = eglGetCurrentDisplay();
227 EGLContext ctx = eglGetCurrentContext();
228
Jamie Gennis74bed552012-03-28 19:05:54 -0700229 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
230 dpy == EGL_NO_DISPLAY) {
Jamie Gennisce561372012-03-19 18:33:05 -0700231 ST_LOGE("updateTexImage: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700232 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700233 }
234
Jamie Gennis74bed552012-03-28 19:05:54 -0700235 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
236 ctx == EGL_NO_CONTEXT) {
Jamie Gennisce561372012-03-19 18:33:05 -0700237 ST_LOGE("updateTexImage: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700238 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700239 }
240
241 mEglDisplay = dpy;
242 mEglContext = ctx;
243
Daniel Lamb2675792012-02-23 14:35:13 -0800244 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800245
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700246 // In asynchronous mode the list is guaranteed to be one buffer
247 // deep, while in synchronous mode we use the oldest buffer.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700248 err = acquireBufferLocked(&item);
Daniel Lamfbcda932012-04-09 22:51:52 -0700249 if (err == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800250 int buf = item.mBuf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700251
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700252 // we call the rejecter here, in case the caller has a reason to
253 // not accept this buffer. this is used by SurfaceFlinger to
254 // reject buffers which have the wrong size
Jamie Gennis9fea3422012-08-07 18:03:04 -0700255 if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
Jamie Gennisb2725412012-09-05 20:09:05 -0700256 releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR);
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700257 glBindTexture(mTexTarget, mTexName);
258 return NO_ERROR;
259 }
260
Jamie Gennis9fea3422012-08-07 18:03:04 -0700261 GLint error;
262 while ((error = glGetError()) != GL_NO_ERROR) {
263 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
264 }
265
266 EGLImageKHR image = mEglSlots[buf].mEglImage;
267 glBindTexture(mTexTarget, mTexName);
268 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
269
270 while ((error = glGetError()) != GL_NO_ERROR) {
271 ST_LOGE("updateTexImage: error binding external texture image %p "
272 "(slot %d): %#04x", image, buf, error);
273 err = UNKNOWN_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800274 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800275
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700276 if (err == NO_ERROR) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700277 err = syncForReleaseLocked(dpy);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800278 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700279
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700280 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700281 // Release the buffer we just acquired. It's not safe to
282 // release the old buffer, so instead we just drop the new frame.
Jamie Gennisb2725412012-09-05 20:09:05 -0700283 releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700284 return err;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800285 }
286
287 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
288 mCurrentTexture,
289 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Jamie Gennis9fea3422012-08-07 18:03:04 -0700290 buf, mSlots[buf].mGraphicBuffer->handle);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700291
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700292 // release old buffer
Jamie Gennis74bed552012-03-28 19:05:54 -0700293 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700294 status_t status = releaseBufferLocked(mCurrentTexture, dpy,
Jamie Gennisb2725412012-09-05 20:09:05 -0700295 mEglSlots[mCurrentTexture].mEglFence);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700296 if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) {
297 ST_LOGE("updateTexImage: failed to release buffer: %s (%d)",
298 strerror(-status), status);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700299 err = status;
300 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700301 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700302
Jamie Gennis9a78c902011-01-12 18:30:40 -0800303 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700304 mCurrentTexture = buf;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700305 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Daniel Lameae59d22012-01-22 15:26:27 -0800306 mCurrentCrop = item.mCrop;
307 mCurrentTransform = item.mTransform;
308 mCurrentScalingMode = item.mScalingMode;
309 mCurrentTimestamp = item.mTimestamp;
Jesse Halldc5b4852012-06-29 15:21:18 -0700310 mCurrentFence = item.mFence;
Jamie Gennis736aa952011-06-12 17:03:06 -0700311 computeCurrentTransformMatrix();
Daniel Lamfbcda932012-04-09 22:51:52 -0700312 } else {
313 if (err < 0) {
Jamie Gennisd69097f2012-08-30 13:28:23 -0700314 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
315 strerror(-err), err);
316 return err;
Daniel Lamfbcda932012-04-09 22:51:52 -0700317 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700318 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700319 glBindTexture(mTexTarget, mTexName);
Daniel Lamfbcda932012-04-09 22:51:52 -0700320 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800321 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700322
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700323 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800324}
325
Jesse Hallef194142012-06-14 14:45:17 -0700326void SurfaceTexture::setReleaseFence(int fenceFd) {
Jamie Gennis3d1d09c2012-08-08 15:39:55 -0700327 sp<Fence> fence(new Fence(fenceFd));
Jamie Gennis45cb2ba2012-08-06 17:10:57 -0700328 if (fenceFd == -1 || mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT)
Jesse Hallef194142012-06-14 14:45:17 -0700329 return;
Jamie Gennisb2725412012-09-05 20:09:05 -0700330 status_t err = addReleaseFence(mCurrentTexture, fence);
331 if (err != OK) {
332 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
333 strerror(-err), err);
Jesse Hallef194142012-06-14 14:45:17 -0700334 }
335}
336
Jamie Gennis74bed552012-03-28 19:05:54 -0700337status_t SurfaceTexture::detachFromContext() {
338 ATRACE_CALL();
339 ST_LOGV("detachFromContext");
340 Mutex::Autolock lock(mMutex);
341
342 if (mAbandoned) {
343 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
344 return NO_INIT;
345 }
346
347 if (!mAttached) {
348 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
349 "context");
350 return INVALID_OPERATION;
351 }
352
353 EGLDisplay dpy = eglGetCurrentDisplay();
354 EGLContext ctx = eglGetCurrentContext();
355
356 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
357 ST_LOGE("detachFromContext: invalid current EGLDisplay");
358 return INVALID_OPERATION;
359 }
360
361 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
362 ST_LOGE("detachFromContext: invalid current EGLContext");
363 return INVALID_OPERATION;
364 }
365
366 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
367 status_t err = syncForReleaseLocked(dpy);
368 if (err != OK) {
369 return err;
370 }
371
372 glDeleteTextures(1, &mTexName);
373 }
374
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700375 // Because we're giving up the EGLDisplay we need to free all the EGLImages
376 // that are associated with it. They'll be recreated when the
377 // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a
378 // new EGLDisplay).
379 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700380 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700381 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700382 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700383 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700384 }
385 }
386
Jamie Gennis74bed552012-03-28 19:05:54 -0700387 mEglDisplay = EGL_NO_DISPLAY;
388 mEglContext = EGL_NO_CONTEXT;
389 mAttached = false;
390
391 return OK;
392}
393
394status_t SurfaceTexture::attachToContext(GLuint tex) {
395 ATRACE_CALL();
396 ST_LOGV("attachToContext");
397 Mutex::Autolock lock(mMutex);
398
399 if (mAbandoned) {
400 ST_LOGE("attachToContext: abandoned SurfaceTexture");
401 return NO_INIT;
402 }
403
404 if (mAttached) {
405 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
406 "context");
407 return INVALID_OPERATION;
408 }
409
410 EGLDisplay dpy = eglGetCurrentDisplay();
411 EGLContext ctx = eglGetCurrentContext();
412
413 if (dpy == EGL_NO_DISPLAY) {
414 ST_LOGE("attachToContext: invalid current EGLDisplay");
415 return INVALID_OPERATION;
416 }
417
418 if (ctx == EGL_NO_CONTEXT) {
419 ST_LOGE("attachToContext: invalid current EGLContext");
420 return INVALID_OPERATION;
421 }
422
423 // We need to bind the texture regardless of whether there's a current
424 // buffer.
425 glBindTexture(mTexTarget, tex);
426
427 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700428 // The EGLImageKHR that was associated with the slot was destroyed when
429 // the SurfaceTexture was detached from the old context, so we need to
430 // recreate it here.
431 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
432 if (image == EGL_NO_IMAGE_KHR) {
433 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700434 }
435
436 // Attach the current buffer to the GL texture.
437 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
438
439 GLint error;
440 status_t err = OK;
441 while ((error = glGetError()) != GL_NO_ERROR) {
442 ST_LOGE("attachToContext: error binding external texture image %p "
443 "(slot %d): %#04x", image, mCurrentTexture, error);
444 err = UNKNOWN_ERROR;
445 }
446
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700447 // We destroy the EGLImageKHR here because the current buffer may no
448 // longer be associated with one of the buffer slots, so we have
449 // nowhere to to store it. If the buffer is still associated with a
450 // slot then another EGLImageKHR will be created next time that buffer
451 // gets acquired in updateTexImage.
452 eglDestroyImageKHR(dpy, image);
Jamie Gennis74bed552012-03-28 19:05:54 -0700453
454 if (err != OK) {
455 return err;
456 }
457 }
458
459 mEglDisplay = dpy;
460 mEglContext = ctx;
461 mTexName = tex;
462 mAttached = true;
463
464 return OK;
465}
466
467status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
468 ST_LOGV("syncForReleaseLocked");
469
Jamie Gennis01dbf552012-09-06 14:54:19 -0700470 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
471 if (useNativeFenceSync) {
472 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
473 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
474 if (sync == EGL_NO_SYNC_KHR) {
475 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
476 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700477 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700478 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700479 glFlush();
480 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700481 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700482 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
483 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
484 "fd: %#x", eglGetError());
485 return UNKNOWN_ERROR;
486 }
487 sp<Fence> fence(new Fence(fenceFd));
488 status_t err = addReleaseFence(mCurrentTexture, fence);
489 if (err != OK) {
490 ST_LOGE("syncForReleaseLocked: error adding release fence: "
491 "%s (%d)", strerror(-err), err);
492 return err;
493 }
494 } else if (mUseFenceSync) {
495 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
496 if (fence != EGL_NO_SYNC_KHR) {
497 // There is already a fence for the current slot. We need to
498 // wait on that before replacing it with another fence to
499 // ensure that all outstanding buffer accesses have completed
500 // before the producer accesses it.
501 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
502 if (result == EGL_FALSE) {
503 ST_LOGE("syncForReleaseLocked: error waiting for previous "
504 "fence: %#x", eglGetError());
505 return UNKNOWN_ERROR;
506 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
507 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
508 "fence");
509 return TIMED_OUT;
510 }
511 eglDestroySyncKHR(dpy, fence);
512 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700513
Jamie Gennis01dbf552012-09-06 14:54:19 -0700514 // Create a fence for the outstanding accesses in the current
515 // OpenGL ES context.
516 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
517 if (fence == EGL_NO_SYNC_KHR) {
518 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
519 eglGetError());
520 return UNKNOWN_ERROR;
521 }
522 glFlush();
523 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700524 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700525 }
526
527 return OK;
528}
529
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700530bool SurfaceTexture::isExternalFormat(uint32_t format)
531{
532 switch (format) {
533 // supported YUV formats
534 case HAL_PIXEL_FORMAT_YV12:
535 // Legacy/deprecated YUV formats
536 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
537 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
538 case HAL_PIXEL_FORMAT_YCbCr_422_I:
539 return true;
540 }
541
542 // Any OEM format needs to be considered
543 if (format>=0x100 && format<=0x1FF)
544 return true;
545
546 return false;
547}
548
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700549GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700550 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700551}
552
Jamie Gennisf238e282011-01-09 16:33:17 -0800553void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800554 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700555 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
556}
557
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700558void SurfaceTexture::setFilteringEnabled(bool enabled) {
559 Mutex::Autolock lock(mMutex);
560 bool needsRecompute = mFilteringEnabled != enabled;
561 mFilteringEnabled = enabled;
562 if (needsRecompute) {
563 computeCurrentTransformMatrix();
564 }
565}
566
Jamie Gennis736aa952011-06-12 17:03:06 -0700567void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700568 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800569
Jamie Gennisa214c642011-01-14 13:53:31 -0800570 float xform[16];
571 for (int i = 0; i < 16; i++) {
572 xform[i] = mtxIdentity[i];
573 }
574 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
575 float result[16];
576 mtxMul(result, xform, mtxFlipH);
577 for (int i = 0; i < 16; i++) {
578 xform[i] = result[i];
579 }
580 }
581 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
582 float result[16];
583 mtxMul(result, xform, mtxFlipV);
584 for (int i = 0; i < 16; i++) {
585 xform[i] = result[i];
586 }
587 }
588 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
589 float result[16];
590 mtxMul(result, xform, mtxRot90);
591 for (int i = 0; i < 16; i++) {
592 xform[i] = result[i];
593 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800594 }
595
Daniel Lameae59d22012-01-22 15:26:27 -0800596 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisd72f2332012-05-07 13:50:11 -0700597 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700598 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
599 float bufferWidth = buf->getWidth();
600 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700601 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700602 float shrinkAmount = 0.0f;
603 if (mFilteringEnabled) {
604 // In order to prevent bilinear sampling beyond the edge of the
605 // crop rectangle we may need to shrink it by 2 texels in each
606 // dimension. Normally this would just need to take 1/2 a texel
607 // off each end, but because the chroma channels of YUV420 images
608 // are subsampled we may need to shrink the crop region by a whole
609 // texel on each side.
610 switch (buf->getPixelFormat()) {
611 case PIXEL_FORMAT_RGBA_8888:
612 case PIXEL_FORMAT_RGBX_8888:
613 case PIXEL_FORMAT_RGB_888:
614 case PIXEL_FORMAT_RGB_565:
615 case PIXEL_FORMAT_BGRA_8888:
616 case PIXEL_FORMAT_RGBA_5551:
617 case PIXEL_FORMAT_RGBA_4444:
618 // We know there's no subsampling of any channels, so we
619 // only need to shrink by a half a pixel.
620 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700621 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700622
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700623 default:
624 // If we don't recognize the format, we must assume the
625 // worst case (that we care about), which is YUV420.
626 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700627 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700628 }
629 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700630
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700631 // Only shrink the dimensions that are not the size of the buffer.
632 if (cropRect.width() < bufferWidth) {
633 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
634 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
635 bufferWidth;
636 }
637 if (cropRect.height() < bufferHeight) {
638 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
639 bufferHeight;
640 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
641 bufferHeight;
642 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800643 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800644 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800645 sx, 0, 0, 0,
646 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800647 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800648 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800649 };
650
Jamie Gennisa214c642011-01-14 13:53:31 -0800651 float mtxBeforeFlipV[16];
652 mtxMul(mtxBeforeFlipV, crop, xform);
653
654 // SurfaceFlinger expects the top of its window textures to be at a Y
655 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
656 // want to expose this to applications, however, so we must add an
657 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700658 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800659}
660
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800661nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700662 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800663 Mutex::Autolock lock(mMutex);
664 return mCurrentTimestamp;
665}
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
Jesse Halldc5b4852012-06-29 15:21:18 -0700734sp<Fence> SurfaceTexture::getCurrentFence() const {
735 Mutex::Autolock lock(mMutex);
736 return mCurrentFence;
737}
738
Jamie Gennis61e04b92012-09-09 17:48:42 -0700739status_t SurfaceTexture::doGLFenceWait() const {
740 Mutex::Autolock lock(mMutex);
741
742 EGLDisplay dpy = eglGetCurrentDisplay();
743 EGLContext ctx = eglGetCurrentContext();
744
745 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
746 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
747 return INVALID_OPERATION;
748 }
749
750 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
751 ST_LOGE("doGLFenceWait: invalid current EGLContext");
752 return INVALID_OPERATION;
753 }
754
755 if (mCurrentFence != NULL) {
756 if (useWaitSync) {
757 // Create an EGLSyncKHR from the current fence.
758 int fenceFd = mCurrentFence->dup();
759 if (fenceFd == -1) {
760 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
761 return -errno;
762 }
763 EGLint attribs[] = {
764 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
765 EGL_NONE
766 };
767 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
768 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
769 if (sync == EGL_NO_SYNC_KHR) {
770 close(fenceFd);
771 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
772 eglGetError());
773 return UNKNOWN_ERROR;
774 }
775
776 // XXX: The spec draft is inconsistent as to whether this should
777 // return an EGLint or void. Ignore the return value for now, as
778 // it's not strictly needed.
779 eglWaitSyncANDROID(dpy, sync, 0);
780 EGLint eglErr = eglGetError();
781 eglDestroySyncKHR(dpy, sync);
782 if (eglErr != EGL_SUCCESS) {
783 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
784 eglErr);
785 return UNKNOWN_ERROR;
786 }
787 } else {
788 status_t err = mCurrentFence->wait(Fence::TIMEOUT_NEVER);
789 if (err != NO_ERROR) {
790 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
791 return err;
792 }
793 }
794 }
795
796 return NO_ERROR;
797}
798
Jamie Gennis59769462011-11-19 18:04:43 -0800799bool SurfaceTexture::isSynchronousMode() const {
800 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800801 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800802}
803
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700804void SurfaceTexture::freeBufferLocked(int slotIndex) {
805 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700806 if (slotIndex == mCurrentTexture) {
807 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
808 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700809 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700810 if (img != EGL_NO_IMAGE_KHR) {
811 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
812 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800813 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700814 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
815 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700816}
Daniel Lameae59d22012-01-22 15:26:27 -0800817
Jamie Gennis9fea3422012-08-07 18:03:04 -0700818void SurfaceTexture::abandonLocked() {
819 ST_LOGV("abandonLocked");
820 mCurrentTextureBuf.clear();
821 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700822}
823
Jamie Gennisfa28c352011-09-16 17:30:26 -0700824void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800825 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700826 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800827 mBufferQueue->setConsumerName(name);
828}
829
830status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
831 Mutex::Autolock lock(mMutex);
832 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
833}
834
835status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
836 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700837 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800838 return mBufferQueue->setConsumerUsageBits(usage);
839}
840
841status_t SurfaceTexture::setTransformHint(uint32_t hint) {
842 Mutex::Autolock lock(mMutex);
843 return mBufferQueue->setTransformHint(hint);
844}
845
846// Used for refactoring BufferQueue from SurfaceTexture
847// Should not be in final interface once users of SurfaceTexture are clean up.
848status_t SurfaceTexture::setSynchronousMode(bool enabled) {
849 Mutex::Autolock lock(mMutex);
850 return mBufferQueue->setSynchronousMode(enabled);
851}
852
Jamie Gennis9fea3422012-08-07 18:03:04 -0700853void SurfaceTexture::dumpLocked(String8& result, const char* prefix,
854 char* buffer, size_t size) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700855{
Jamie Gennis9fea3422012-08-07 18:03:04 -0700856 snprintf(buffer, size,
857 "%smTexName=%d mCurrentTexture=%d\n"
858 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
859 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
860 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
861 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700862 result.append(buffer);
863
Jamie Gennis9fea3422012-08-07 18:03:04 -0700864 ConsumerBase::dumpLocked(result, prefix, buffer, size);
Mathias Agopian68c77942011-05-09 19:08:33 -0700865}
866
Jamie Gennisf238e282011-01-09 16:33:17 -0800867static void mtxMul(float out[16], const float a[16], const float b[16]) {
868 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
869 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
870 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
871 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
872
873 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
874 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
875 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
876 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
877
878 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
879 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
880 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
881 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
882
883 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
884 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
885 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
886 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
887}
888
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800889}; // namespace android