blob: 037f5fb6a68076a89f71572a16a388bb5f3df134 [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 Gennisfa28c352011-09-16 17:30:26 -070066// Macros for including the SurfaceTexture name in log messages
Steve Block6807e592011-10-20 11:56:00 +010067#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000068#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000069#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000070#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000071#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070072
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080073namespace android {
74
Jamie Gennisf238e282011-01-09 16:33:17 -080075// Transform matrices
76static float mtxIdentity[16] = {
77 1, 0, 0, 0,
78 0, 1, 0, 0,
79 0, 0, 1, 0,
80 0, 0, 0, 1,
81};
82static float mtxFlipH[16] = {
83 -1, 0, 0, 0,
84 0, 1, 0, 0,
85 0, 0, 1, 0,
86 1, 0, 0, 1,
87};
88static float mtxFlipV[16] = {
89 1, 0, 0, 0,
90 0, -1, 0, 0,
91 0, 0, 1, 0,
92 0, 1, 0, 1,
93};
94static float mtxRot90[16] = {
95 0, 1, 0, 0,
96 -1, 0, 0, 0,
97 0, 0, 1, 0,
98 1, 0, 0, 1,
99};
100static float mtxRot180[16] = {
101 -1, 0, 0, 0,
102 0, -1, 0, 0,
103 0, 0, 1, 0,
104 1, 1, 0, 1,
105};
106static float mtxRot270[16] = {
107 0, -1, 0, 0,
108 1, 0, 0, 0,
109 0, 0, 1, 0,
110 0, 1, 0, 1,
111};
112
113static void mtxMul(float out[16], const float a[16], const float b[16]);
114
Jamie Gennisfa28c352011-09-16 17:30:26 -0700115
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700116SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Daniel Lamb2675792012-02-23 14:35:13 -0800117 GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) :
Jamie Gennis9fea3422012-08-07 18:03:04 -0700118 ConsumerBase(bufferQueue == 0 ? new BufferQueue(allowSynchronousMode) : bufferQueue),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800119 mCurrentTransform(0),
120 mCurrentTimestamp(0),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700121 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700122 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800123#ifdef USE_FENCE_SYNC
124 mUseFenceSync(useFenceSync),
125#else
126 mUseFenceSync(false),
127#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800128 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700129 mEglDisplay(EGL_NO_DISPLAY),
130 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700131 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
132 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800133{
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700134 ST_LOGV("SurfaceTexture");
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
Jamie Gennis9fea3422012-08-07 18:03:04 -0700139 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800140}
141
Jamie Gennis31a353d2012-08-24 17:25:13 -0700142status_t SurfaceTexture::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700143 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -0700144 return mBufferQueue->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700145}
146
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800147
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700148status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
149{
Daniel Lamb2675792012-02-23 14:35:13 -0800150 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700151 mDefaultWidth = w;
152 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800153 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700154}
155
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800156status_t SurfaceTexture::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800157 ATRACE_CALL();
158 ST_LOGV("updateTexImage");
159 Mutex::Autolock lock(mMutex);
160
161 if (mAbandoned) {
162 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
163 return NO_INIT;
164 }
165
166 // Make sure the EGL state is the same as in previous calls.
167 status_t err = checkAndUpdateEglStateLocked();
168 if (err != NO_ERROR) {
169 return err;
170 }
171
172 BufferQueue::BufferItem item;
173
174 // Acquire the next buffer.
175 // In asynchronous mode the list is guaranteed to be one buffer
176 // deep, while in synchronous mode we use the oldest buffer.
177 err = acquireBufferLocked(&item);
178 if (err != NO_ERROR) {
179 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
180 // We always bind the texture even if we don't update its contents.
181 ST_LOGV("updateTexImage: no buffers were available");
182 glBindTexture(mTexTarget, mTexName);
183 err = NO_ERROR;
184 } else {
185 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
186 strerror(-err), err);
187 }
188 return err;
189 }
190
191 // Release the previous buffer.
192 err = releaseAndUpdateLocked(item);
193 if (err != NO_ERROR) {
194 // We always bind the texture.
195 glBindTexture(mTexTarget, mTexName);
196 return err;
197 }
198
199 // Bind the new buffer to the GL texture.
200 err = bindTextureImage();
201 if (err != NO_ERROR) {
202 return err;
203 }
204
205 // Wait for the new buffer to be ready.
206 return doGLFenceWaitLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700207}
208
Jamie Gennis9fea3422012-08-07 18:03:04 -0700209status_t SurfaceTexture::acquireBufferLocked(BufferQueue::BufferItem *item) {
210 status_t err = ConsumerBase::acquireBufferLocked(item);
211 if (err != NO_ERROR) {
212 return err;
213 }
214
215 int slot = item->mBuf;
216 if (item->mGraphicBuffer != NULL) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800217 // This buffer has not been acquired before, so we must assume
218 // that any EGLImage in mEglSlots is stale.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700219 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800220 if (!eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage)) {
221 ST_LOGW("acquireBufferLocked: eglDestroyImageKHR failed for slot=%d",
222 slot);
223 // keep going
224 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700225 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
226 }
227 }
228
Jamie Gennis9fea3422012-08-07 18:03:04 -0700229 return NO_ERROR;
230}
231
232status_t SurfaceTexture::releaseBufferLocked(int buf, EGLDisplay display,
Jamie Gennisb2725412012-09-05 20:09:05 -0700233 EGLSyncKHR eglFence) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800234 status_t err = ConsumerBase::releaseBufferLocked(buf, display, eglFence);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700235
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700236 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700237
238 return err;
239}
240
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800241status_t SurfaceTexture::releaseAndUpdateLocked(const BufferQueue::BufferItem& item)
242{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700243 status_t err = NO_ERROR;
244
Jamie Gennis74bed552012-03-28 19:05:54 -0700245 if (!mAttached) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800246 ST_LOGE("releaseAndUpdate: SurfaceTexture is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700247 "ES context");
248 return INVALID_OPERATION;
249 }
250
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800251 // Confirm state.
252 err = checkAndUpdateEglStateLocked();
253 if (err != NO_ERROR) {
254 return err;
255 }
256
257 int buf = item.mBuf;
258
259 // If the mEglSlot entry is empty, create an EGLImage for the gralloc
260 // buffer currently in the slot in ConsumerBase.
261 //
262 // We may have to do this even when item.mGraphicBuffer == NULL (which
263 // means the buffer was previously acquired), if we destroyed the
264 // EGLImage when detaching from a context but the buffer has not been
265 // re-allocated.
266 if (mEglSlots[buf].mEglImage == EGL_NO_IMAGE_KHR) {
267 EGLImageKHR image = createImage(mEglDisplay, mSlots[buf].mGraphicBuffer);
268 if (image == EGL_NO_IMAGE_KHR) {
269 ST_LOGW("releaseAndUpdate: unable to createImage on display=%p slot=%d",
270 mEglDisplay, buf);
271 return UNKNOWN_ERROR;
272 }
273 mEglSlots[buf].mEglImage = image;
274 }
275
276 // Do whatever sync ops we need to do before releasing the old slot.
277 err = syncForReleaseLocked(mEglDisplay);
278 if (err != NO_ERROR) {
279 // Release the buffer we just acquired. It's not safe to
280 // release the old buffer, so instead we just drop the new frame.
281 releaseBufferLocked(buf, mEglDisplay, EGL_NO_SYNC_KHR);
282 return err;
283 }
284
285 ST_LOGV("releaseAndUpdate: (slot=%d buf=%p) -> (slot=%d buf=%p)",
286 mCurrentTexture,
287 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
288 buf, mSlots[buf].mGraphicBuffer->handle);
289
290 // release old buffer
291 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
292 status_t status = releaseBufferLocked(mCurrentTexture, mEglDisplay,
293 mEglSlots[mCurrentTexture].mEglFence);
294 if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) {
295 ST_LOGE("releaseAndUpdate: failed to release buffer: %s (%d)",
296 strerror(-status), status);
297 err = status;
298 // keep going, with error raised [?]
299 }
300 }
301
302 // Update the SurfaceTexture state.
303 mCurrentTexture = buf;
304 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
305 mCurrentCrop = item.mCrop;
306 mCurrentTransform = item.mTransform;
307 mCurrentScalingMode = item.mScalingMode;
308 mCurrentTimestamp = item.mTimestamp;
309 mCurrentFence = item.mFence;
310
311 computeCurrentTransformMatrixLocked();
312
313 return err;
314}
315
316status_t SurfaceTexture::bindTextureImage() {
317 if (mEglDisplay == EGL_NO_DISPLAY) {
318 ALOGE("bindTextureImage: invalid display");
319 return INVALID_OPERATION;
320 }
321
322 GLint error;
323 while ((error = glGetError()) != GL_NO_ERROR) {
324 ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
325 }
326
327 glBindTexture(mTexTarget, mTexName);
328 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
329 if (mCurrentTextureBuf == NULL) {
330 ST_LOGE("bindTextureImage: no currently-bound texture");
331 return NO_INIT;
332 }
333 return bindUnslottedBufferLocked(mEglDisplay);
334 } else {
335 EGLImageKHR image = mEglSlots[mCurrentTexture].mEglImage;
336
337 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
338
339 while ((error = glGetError()) != GL_NO_ERROR) {
340 ST_LOGE("bindTextureImage: error binding external texture image %p"
341 ": %#04x", image, error);
342 return UNKNOWN_ERROR;
343 }
344 return NO_ERROR;
345 }
346}
347
348status_t SurfaceTexture::checkAndUpdateEglStateLocked() {
Jamie Gennisce561372012-03-19 18:33:05 -0700349 EGLDisplay dpy = eglGetCurrentDisplay();
350 EGLContext ctx = eglGetCurrentContext();
351
Jamie Gennis74bed552012-03-28 19:05:54 -0700352 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
353 dpy == EGL_NO_DISPLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800354 ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700355 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700356 }
357
Jamie Gennis74bed552012-03-28 19:05:54 -0700358 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
359 ctx == EGL_NO_CONTEXT) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800360 ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700361 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700362 }
363
364 mEglDisplay = dpy;
365 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800366 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800367}
368
Jesse Hallef194142012-06-14 14:45:17 -0700369void SurfaceTexture::setReleaseFence(int fenceFd) {
Jamie Gennis3d1d09c2012-08-08 15:39:55 -0700370 sp<Fence> fence(new Fence(fenceFd));
Jamie Gennis45cb2ba2012-08-06 17:10:57 -0700371 if (fenceFd == -1 || mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT)
Jesse Hallef194142012-06-14 14:45:17 -0700372 return;
Jamie Gennisb2725412012-09-05 20:09:05 -0700373 status_t err = addReleaseFence(mCurrentTexture, fence);
374 if (err != OK) {
375 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
376 strerror(-err), err);
Jesse Hallef194142012-06-14 14:45:17 -0700377 }
378}
379
Jamie Gennis74bed552012-03-28 19:05:54 -0700380status_t SurfaceTexture::detachFromContext() {
381 ATRACE_CALL();
382 ST_LOGV("detachFromContext");
383 Mutex::Autolock lock(mMutex);
384
385 if (mAbandoned) {
386 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
387 return NO_INIT;
388 }
389
390 if (!mAttached) {
391 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
392 "context");
393 return INVALID_OPERATION;
394 }
395
396 EGLDisplay dpy = eglGetCurrentDisplay();
397 EGLContext ctx = eglGetCurrentContext();
398
399 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
400 ST_LOGE("detachFromContext: invalid current EGLDisplay");
401 return INVALID_OPERATION;
402 }
403
404 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
405 ST_LOGE("detachFromContext: invalid current EGLContext");
406 return INVALID_OPERATION;
407 }
408
409 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
410 status_t err = syncForReleaseLocked(dpy);
411 if (err != OK) {
412 return err;
413 }
414
415 glDeleteTextures(1, &mTexName);
416 }
417
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700418 // Because we're giving up the EGLDisplay we need to free all the EGLImages
419 // that are associated with it. They'll be recreated when the
420 // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a
421 // new EGLDisplay).
422 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700423 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700424 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700425 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700426 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700427 }
428 }
429
Jamie Gennis74bed552012-03-28 19:05:54 -0700430 mEglDisplay = EGL_NO_DISPLAY;
431 mEglContext = EGL_NO_CONTEXT;
432 mAttached = false;
433
434 return OK;
435}
436
437status_t SurfaceTexture::attachToContext(GLuint tex) {
438 ATRACE_CALL();
439 ST_LOGV("attachToContext");
440 Mutex::Autolock lock(mMutex);
441
442 if (mAbandoned) {
443 ST_LOGE("attachToContext: abandoned SurfaceTexture");
444 return NO_INIT;
445 }
446
447 if (mAttached) {
448 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
449 "context");
450 return INVALID_OPERATION;
451 }
452
453 EGLDisplay dpy = eglGetCurrentDisplay();
454 EGLContext ctx = eglGetCurrentContext();
455
456 if (dpy == EGL_NO_DISPLAY) {
457 ST_LOGE("attachToContext: invalid current EGLDisplay");
458 return INVALID_OPERATION;
459 }
460
461 if (ctx == EGL_NO_CONTEXT) {
462 ST_LOGE("attachToContext: invalid current EGLContext");
463 return INVALID_OPERATION;
464 }
465
466 // We need to bind the texture regardless of whether there's a current
467 // buffer.
468 glBindTexture(mTexTarget, tex);
469
470 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700471 // The EGLImageKHR that was associated with the slot was destroyed when
472 // the SurfaceTexture was detached from the old context, so we need to
473 // recreate it here.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800474 status_t err = bindUnslottedBufferLocked(dpy);
475 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700476 return err;
477 }
478 }
479
480 mEglDisplay = dpy;
481 mEglContext = ctx;
482 mTexName = tex;
483 mAttached = true;
484
485 return OK;
486}
487
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800488status_t SurfaceTexture::bindUnslottedBufferLocked(EGLDisplay dpy) {
489 ST_LOGV("bindUnslottedBuffer ct=%d ctb=%p",
490 mCurrentTexture, mCurrentTextureBuf.get());
491
492 // Create a temporary EGLImageKHR.
493 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
494 if (image == EGL_NO_IMAGE_KHR) {
495 return UNKNOWN_ERROR;
496 }
497
498 // Attach the current buffer to the GL texture.
499 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
500
501 GLint error;
502 status_t err = OK;
503 while ((error = glGetError()) != GL_NO_ERROR) {
504 ST_LOGE("bindUnslottedBuffer: error binding external texture image %p "
505 "(slot %d): %#04x", image, mCurrentTexture, error);
506 err = UNKNOWN_ERROR;
507 }
508
509 // We destroy the EGLImageKHR here because the current buffer may no
510 // longer be associated with one of the buffer slots, so we have
511 // nowhere to to store it. If the buffer is still associated with a
512 // slot then another EGLImageKHR will be created next time that buffer
513 // gets acquired in updateTexImage.
514 eglDestroyImageKHR(dpy, image);
515
516 return err;
517}
518
519
Jamie Gennis74bed552012-03-28 19:05:54 -0700520status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
521 ST_LOGV("syncForReleaseLocked");
522
Jamie Gennis01dbf552012-09-06 14:54:19 -0700523 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
524 if (useNativeFenceSync) {
525 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
526 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
527 if (sync == EGL_NO_SYNC_KHR) {
528 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
529 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700530 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700531 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700532 glFlush();
533 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700534 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700535 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
536 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
537 "fd: %#x", eglGetError());
538 return UNKNOWN_ERROR;
539 }
540 sp<Fence> fence(new Fence(fenceFd));
Jesse Hall9504eb92012-10-05 14:34:21 -0700541 status_t err = addReleaseFenceLocked(mCurrentTexture, fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700542 if (err != OK) {
543 ST_LOGE("syncForReleaseLocked: error adding release fence: "
544 "%s (%d)", strerror(-err), err);
545 return err;
546 }
547 } else if (mUseFenceSync) {
548 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
549 if (fence != EGL_NO_SYNC_KHR) {
550 // There is already a fence for the current slot. We need to
551 // wait on that before replacing it with another fence to
552 // ensure that all outstanding buffer accesses have completed
553 // before the producer accesses it.
554 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
555 if (result == EGL_FALSE) {
556 ST_LOGE("syncForReleaseLocked: error waiting for previous "
557 "fence: %#x", eglGetError());
558 return UNKNOWN_ERROR;
559 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
560 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
561 "fence");
562 return TIMED_OUT;
563 }
564 eglDestroySyncKHR(dpy, fence);
565 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700566
Jamie Gennis01dbf552012-09-06 14:54:19 -0700567 // Create a fence for the outstanding accesses in the current
568 // OpenGL ES context.
569 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
570 if (fence == EGL_NO_SYNC_KHR) {
571 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
572 eglGetError());
573 return UNKNOWN_ERROR;
574 }
575 glFlush();
576 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700577 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700578 }
579
580 return OK;
581}
582
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700583bool SurfaceTexture::isExternalFormat(uint32_t format)
584{
585 switch (format) {
586 // supported YUV formats
587 case HAL_PIXEL_FORMAT_YV12:
588 // Legacy/deprecated YUV formats
589 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
590 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
591 case HAL_PIXEL_FORMAT_YCbCr_422_I:
592 return true;
593 }
594
595 // Any OEM format needs to be considered
596 if (format>=0x100 && format<=0x1FF)
597 return true;
598
599 return false;
600}
601
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700602GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700603 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700604}
605
Jamie Gennisf238e282011-01-09 16:33:17 -0800606void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800607 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700608 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
609}
610
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700611void SurfaceTexture::setFilteringEnabled(bool enabled) {
612 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700613 if (mAbandoned) {
614 ST_LOGE("setFilteringEnabled: SurfaceTexture is abandoned!");
615 return;
616 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700617 bool needsRecompute = mFilteringEnabled != enabled;
618 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700619
620 if (needsRecompute && mCurrentTextureBuf==NULL) {
621 ST_LOGD("setFilteringEnabled called with mCurrentTextureBuf == NULL");
622 }
623
624 if (needsRecompute && mCurrentTextureBuf != NULL) {
625 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700626 }
627}
628
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700629void SurfaceTexture::computeCurrentTransformMatrixLocked() {
630 ST_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800631
Jamie Gennisa214c642011-01-14 13:53:31 -0800632 float xform[16];
633 for (int i = 0; i < 16; i++) {
634 xform[i] = mtxIdentity[i];
635 }
636 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
637 float result[16];
638 mtxMul(result, xform, mtxFlipH);
639 for (int i = 0; i < 16; i++) {
640 xform[i] = result[i];
641 }
642 }
643 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
644 float result[16];
645 mtxMul(result, xform, mtxFlipV);
646 for (int i = 0; i < 16; i++) {
647 xform[i] = result[i];
648 }
649 }
650 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
651 float result[16];
652 mtxMul(result, xform, mtxRot90);
653 for (int i = 0; i < 16; i++) {
654 xform[i] = result[i];
655 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800656 }
657
Daniel Lameae59d22012-01-22 15:26:27 -0800658 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700659
660 if (buf == NULL) {
661 ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureBuf is NULL");
662 }
663
Jamie Gennisd72f2332012-05-07 13:50:11 -0700664 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700665 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
666 float bufferWidth = buf->getWidth();
667 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700668 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700669 float shrinkAmount = 0.0f;
670 if (mFilteringEnabled) {
671 // In order to prevent bilinear sampling beyond the edge of the
672 // crop rectangle we may need to shrink it by 2 texels in each
673 // dimension. Normally this would just need to take 1/2 a texel
674 // off each end, but because the chroma channels of YUV420 images
675 // are subsampled we may need to shrink the crop region by a whole
676 // texel on each side.
677 switch (buf->getPixelFormat()) {
678 case PIXEL_FORMAT_RGBA_8888:
679 case PIXEL_FORMAT_RGBX_8888:
680 case PIXEL_FORMAT_RGB_888:
681 case PIXEL_FORMAT_RGB_565:
682 case PIXEL_FORMAT_BGRA_8888:
683 case PIXEL_FORMAT_RGBA_5551:
684 case PIXEL_FORMAT_RGBA_4444:
685 // We know there's no subsampling of any channels, so we
686 // only need to shrink by a half a pixel.
687 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700688 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700689
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700690 default:
691 // If we don't recognize the format, we must assume the
692 // worst case (that we care about), which is YUV420.
693 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700694 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700695 }
696 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700697
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700698 // Only shrink the dimensions that are not the size of the buffer.
699 if (cropRect.width() < bufferWidth) {
700 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
701 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
702 bufferWidth;
703 }
704 if (cropRect.height() < bufferHeight) {
705 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
706 bufferHeight;
707 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
708 bufferHeight;
709 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800710 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800711 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800712 sx, 0, 0, 0,
713 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800714 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800715 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800716 };
717
Jamie Gennisa214c642011-01-14 13:53:31 -0800718 float mtxBeforeFlipV[16];
719 mtxMul(mtxBeforeFlipV, crop, xform);
720
721 // SurfaceFlinger expects the top of its window textures to be at a Y
722 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
723 // want to expose this to applications, however, so we must add an
724 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700725 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800726}
727
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800728nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700729 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800730 Mutex::Autolock lock(mMutex);
731 return mCurrentTimestamp;
732}
733
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800734EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
735 const sp<GraphicBuffer>& graphicBuffer) {
736 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
737 EGLint attrs[] = {
738 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
739 EGL_NONE,
740 };
741 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
742 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700743 if (image == EGL_NO_IMAGE_KHR) {
744 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700745 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800746 }
747 return image;
748}
749
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700750sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
751 Mutex::Autolock lock(mMutex);
752 return mCurrentTextureBuf;
753}
754
755Rect SurfaceTexture::getCurrentCrop() const {
756 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700757
758 Rect outCrop = mCurrentCrop;
759 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
760 int32_t newWidth = mCurrentCrop.width();
761 int32_t newHeight = mCurrentCrop.height();
762
763 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
764 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
765 ST_LOGV("too wide: newWidth = %d", newWidth);
766 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
767 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
768 ST_LOGV("too tall: newHeight = %d", newHeight);
769 }
770
771 // The crop is too wide
772 if (newWidth < mCurrentCrop.width()) {
773 int32_t dw = (newWidth - mCurrentCrop.width())/2;
774 outCrop.left -=dw;
775 outCrop.right += dw;
776 // The crop is too tall
777 } else if (newHeight < mCurrentCrop.height()) {
778 int32_t dh = (newHeight - mCurrentCrop.height())/2;
779 outCrop.top -= dh;
780 outCrop.bottom += dh;
781 }
782
783 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
784 outCrop.left, outCrop.top,
785 outCrop.right,outCrop.bottom);
786 }
787
Daniel Lam016c8cb2012-04-03 15:54:58 -0700788 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700789}
790
791uint32_t SurfaceTexture::getCurrentTransform() const {
792 Mutex::Autolock lock(mMutex);
793 return mCurrentTransform;
794}
795
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700796uint32_t SurfaceTexture::getCurrentScalingMode() const {
797 Mutex::Autolock lock(mMutex);
798 return mCurrentScalingMode;
799}
800
Jesse Halldc5b4852012-06-29 15:21:18 -0700801sp<Fence> SurfaceTexture::getCurrentFence() const {
802 Mutex::Autolock lock(mMutex);
803 return mCurrentFence;
804}
805
Jamie Gennis61e04b92012-09-09 17:48:42 -0700806status_t SurfaceTexture::doGLFenceWait() const {
807 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700808 return doGLFenceWaitLocked();
809}
810
811status_t SurfaceTexture::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700812
813 EGLDisplay dpy = eglGetCurrentDisplay();
814 EGLContext ctx = eglGetCurrentContext();
815
816 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
817 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
818 return INVALID_OPERATION;
819 }
820
821 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
822 ST_LOGE("doGLFenceWait: invalid current EGLContext");
823 return INVALID_OPERATION;
824 }
825
826 if (mCurrentFence != NULL) {
827 if (useWaitSync) {
828 // Create an EGLSyncKHR from the current fence.
829 int fenceFd = mCurrentFence->dup();
830 if (fenceFd == -1) {
831 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
832 return -errno;
833 }
834 EGLint attribs[] = {
835 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
836 EGL_NONE
837 };
838 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
839 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
840 if (sync == EGL_NO_SYNC_KHR) {
841 close(fenceFd);
842 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
843 eglGetError());
844 return UNKNOWN_ERROR;
845 }
846
847 // XXX: The spec draft is inconsistent as to whether this should
848 // return an EGLint or void. Ignore the return value for now, as
849 // it's not strictly needed.
850 eglWaitSyncANDROID(dpy, sync, 0);
851 EGLint eglErr = eglGetError();
852 eglDestroySyncKHR(dpy, sync);
853 if (eglErr != EGL_SUCCESS) {
854 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
855 eglErr);
856 return UNKNOWN_ERROR;
857 }
858 } else {
Jesse Hallba607d52012-10-01 14:05:20 -0700859 status_t err = mCurrentFence->waitForever(1000,
860 "SurfaceTexture::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700861 if (err != NO_ERROR) {
862 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
863 return err;
864 }
865 }
866 }
867
868 return NO_ERROR;
869}
870
Jamie Gennis59769462011-11-19 18:04:43 -0800871bool SurfaceTexture::isSynchronousMode() const {
872 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800873 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800874}
875
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700876void SurfaceTexture::freeBufferLocked(int slotIndex) {
877 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700878 if (slotIndex == mCurrentTexture) {
879 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
880 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700881 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700882 if (img != EGL_NO_IMAGE_KHR) {
883 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
884 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800885 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700886 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
887 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700888}
Daniel Lameae59d22012-01-22 15:26:27 -0800889
Jamie Gennis9fea3422012-08-07 18:03:04 -0700890void SurfaceTexture::abandonLocked() {
891 ST_LOGV("abandonLocked");
892 mCurrentTextureBuf.clear();
893 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700894}
895
Jamie Gennisfa28c352011-09-16 17:30:26 -0700896void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800897 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700898 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800899 mBufferQueue->setConsumerName(name);
900}
901
902status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
903 Mutex::Autolock lock(mMutex);
904 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
905}
906
907status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
908 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700909 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800910 return mBufferQueue->setConsumerUsageBits(usage);
911}
912
913status_t SurfaceTexture::setTransformHint(uint32_t hint) {
914 Mutex::Autolock lock(mMutex);
915 return mBufferQueue->setTransformHint(hint);
916}
917
918// Used for refactoring BufferQueue from SurfaceTexture
919// Should not be in final interface once users of SurfaceTexture are clean up.
920status_t SurfaceTexture::setSynchronousMode(bool enabled) {
921 Mutex::Autolock lock(mMutex);
922 return mBufferQueue->setSynchronousMode(enabled);
923}
924
Jamie Gennis9fea3422012-08-07 18:03:04 -0700925void SurfaceTexture::dumpLocked(String8& result, const char* prefix,
926 char* buffer, size_t size) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700927{
Jamie Gennis9fea3422012-08-07 18:03:04 -0700928 snprintf(buffer, size,
929 "%smTexName=%d mCurrentTexture=%d\n"
930 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
931 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
932 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
933 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700934 result.append(buffer);
935
Jamie Gennis9fea3422012-08-07 18:03:04 -0700936 ConsumerBase::dumpLocked(result, prefix, buffer, size);
Mathias Agopian68c77942011-05-09 19:08:33 -0700937}
938
Jamie Gennisf238e282011-01-09 16:33:17 -0800939static void mtxMul(float out[16], const float a[16], const float b[16]) {
940 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
941 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
942 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
943 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
944
945 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
946 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
947 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
948 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
949
950 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
951 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
952 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
953 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
954
955 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
956 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
957 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
958 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
959}
960
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800961}; // namespace android