blob: 30c0d9bfb1adf9a46e53904e94a1c857b359dfaf [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() {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800179 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700180 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800181 Mutex::Autolock lock(mMutex);
182
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700183 status_t err = NO_ERROR;
184
Mathias Agopiane47498f2011-08-08 19:35:15 -0700185 if (mAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700186 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700187 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700188 }
189
Jamie Gennis74bed552012-03-28 19:05:54 -0700190 if (!mAttached) {
191 ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL "
192 "ES context");
193 return INVALID_OPERATION;
194 }
195
Jamie Gennisce561372012-03-19 18:33:05 -0700196 EGLDisplay dpy = eglGetCurrentDisplay();
197 EGLContext ctx = eglGetCurrentContext();
198
Jamie Gennis74bed552012-03-28 19:05:54 -0700199 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
200 dpy == EGL_NO_DISPLAY) {
Jamie Gennisce561372012-03-19 18:33:05 -0700201 ST_LOGE("updateTexImage: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700202 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700203 }
204
Jamie Gennis74bed552012-03-28 19:05:54 -0700205 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
206 ctx == EGL_NO_CONTEXT) {
Jamie Gennisce561372012-03-19 18:33:05 -0700207 ST_LOGE("updateTexImage: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700208 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700209 }
210
211 mEglDisplay = dpy;
212 mEglContext = ctx;
213
Daniel Lamb2675792012-02-23 14:35:13 -0800214 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800215
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700216 // In asynchronous mode the list is guaranteed to be one buffer
217 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lamfbcda932012-04-09 22:51:52 -0700218 err = mBufferQueue->acquireBuffer(&item);
219 if (err == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800220 int buf = item.mBuf;
221 // This buffer was newly allocated, so we need to clean up on our side
222 if (item.mGraphicBuffer != NULL) {
223 mEGLSlots[buf].mGraphicBuffer = 0;
224 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700225 eglDestroyImageKHR(dpy, mEGLSlots[buf].mEglImage);
Daniel Lameae59d22012-01-22 15:26:27 -0800226 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800227 }
228 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
229 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700230
Jesse Hall90ed8502012-05-16 23:44:34 -0700231 // Update the GL texture object. We may have to do this even when
232 // item.mGraphicBuffer == NULL, if we destroyed the EGLImage when
233 // detaching from a context but the buffer has not been re-allocated.
Daniel Lameae59d22012-01-22 15:26:27 -0800234 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800235 if (image == EGL_NO_IMAGE_KHR) {
Jesse Hall90ed8502012-05-16 23:44:34 -0700236 if (mEGLSlots[buf].mGraphicBuffer == NULL) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700237 ST_LOGE("updateTexImage: buffer at slot %d is null", buf);
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700238 err = BAD_VALUE;
239 } else {
240 image = createImage(dpy, mEGLSlots[buf].mGraphicBuffer);
241 mEGLSlots[buf].mEglImage = image;
242 if (image == EGL_NO_IMAGE_KHR) {
243 // NOTE: if dpy was invalid, createImage() is guaranteed to
244 // fail. so we'd end up here.
245 err = UNKNOWN_ERROR;
246 }
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700247 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800248 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800249
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700250 if (err == NO_ERROR) {
251 GLint error;
252 while ((error = glGetError()) != GL_NO_ERROR) {
253 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
254 }
255
256 glBindTexture(mTexTarget, mTexName);
257 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
258
259 while ((error = glGetError()) != GL_NO_ERROR) {
260 ST_LOGE("updateTexImage: error binding external texture image %p "
261 "(slot %d): %#04x", image, buf, error);
262 err = UNKNOWN_ERROR;
263 }
264
265 if (err == NO_ERROR) {
266 err = syncForReleaseLocked(dpy);
267 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800268 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700269
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700270 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700271 // Release the buffer we just acquired. It's not safe to
272 // release the old buffer, so instead we just drop the new frame.
273 mBufferQueue->releaseBuffer(buf, dpy, mEGLSlots[buf].mFence);
274 mEGLSlots[buf].mFence = EGL_NO_SYNC_KHR;
275 return err;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800276 }
277
278 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
279 mCurrentTexture,
280 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800281 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700282
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700283 // release old buffer
Jamie Gennis74bed552012-03-28 19:05:54 -0700284 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700285 status_t status = mBufferQueue->releaseBuffer(mCurrentTexture, dpy,
286 mEGLSlots[mCurrentTexture].mFence);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700287
Jamie Gennis74bed552012-03-28 19:05:54 -0700288 mEGLSlots[mCurrentTexture].mFence = EGL_NO_SYNC_KHR;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700289 if (status == BufferQueue::STALE_BUFFER_SLOT) {
290 freeBufferLocked(mCurrentTexture);
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700291 } else if (status != NO_ERROR) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700292 ST_LOGE("updateTexImage: released invalid buffer");
293 err = status;
294 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700295 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700296
Jamie Gennis9a78c902011-01-12 18:30:40 -0800297 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700298 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800299 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
300 mCurrentCrop = item.mCrop;
301 mCurrentTransform = item.mTransform;
302 mCurrentScalingMode = item.mScalingMode;
303 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700304 computeCurrentTransformMatrix();
Daniel Lamfbcda932012-04-09 22:51:52 -0700305 } else {
306 if (err < 0) {
307 ALOGE("updateTexImage failed on acquire %d", err);
308 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700309 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700310 glBindTexture(mTexTarget, mTexName);
Daniel Lamfbcda932012-04-09 22:51:52 -0700311 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800312 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700313
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700314 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800315}
316
Jamie Gennis74bed552012-03-28 19:05:54 -0700317status_t SurfaceTexture::detachFromContext() {
318 ATRACE_CALL();
319 ST_LOGV("detachFromContext");
320 Mutex::Autolock lock(mMutex);
321
322 if (mAbandoned) {
323 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
324 return NO_INIT;
325 }
326
327 if (!mAttached) {
328 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
329 "context");
330 return INVALID_OPERATION;
331 }
332
333 EGLDisplay dpy = eglGetCurrentDisplay();
334 EGLContext ctx = eglGetCurrentContext();
335
336 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
337 ST_LOGE("detachFromContext: invalid current EGLDisplay");
338 return INVALID_OPERATION;
339 }
340
341 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
342 ST_LOGE("detachFromContext: invalid current EGLContext");
343 return INVALID_OPERATION;
344 }
345
346 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
347 status_t err = syncForReleaseLocked(dpy);
348 if (err != OK) {
349 return err;
350 }
351
352 glDeleteTextures(1, &mTexName);
353 }
354
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700355 // Because we're giving up the EGLDisplay we need to free all the EGLImages
356 // that are associated with it. They'll be recreated when the
357 // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a
358 // new EGLDisplay).
359 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
360 EGLImageKHR img = mEGLSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700361 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700362 eglDestroyImageKHR(mEglDisplay, img);
363 mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
364 }
365 }
366
Jamie Gennis74bed552012-03-28 19:05:54 -0700367 mEglDisplay = EGL_NO_DISPLAY;
368 mEglContext = EGL_NO_CONTEXT;
369 mAttached = false;
370
371 return OK;
372}
373
374status_t SurfaceTexture::attachToContext(GLuint tex) {
375 ATRACE_CALL();
376 ST_LOGV("attachToContext");
377 Mutex::Autolock lock(mMutex);
378
379 if (mAbandoned) {
380 ST_LOGE("attachToContext: abandoned SurfaceTexture");
381 return NO_INIT;
382 }
383
384 if (mAttached) {
385 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
386 "context");
387 return INVALID_OPERATION;
388 }
389
390 EGLDisplay dpy = eglGetCurrentDisplay();
391 EGLContext ctx = eglGetCurrentContext();
392
393 if (dpy == EGL_NO_DISPLAY) {
394 ST_LOGE("attachToContext: invalid current EGLDisplay");
395 return INVALID_OPERATION;
396 }
397
398 if (ctx == EGL_NO_CONTEXT) {
399 ST_LOGE("attachToContext: invalid current EGLContext");
400 return INVALID_OPERATION;
401 }
402
403 // We need to bind the texture regardless of whether there's a current
404 // buffer.
405 glBindTexture(mTexTarget, tex);
406
407 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700408 // The EGLImageKHR that was associated with the slot was destroyed when
409 // the SurfaceTexture was detached from the old context, so we need to
410 // recreate it here.
411 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
412 if (image == EGL_NO_IMAGE_KHR) {
413 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700414 }
415
416 // Attach the current buffer to the GL texture.
417 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
418
419 GLint error;
420 status_t err = OK;
421 while ((error = glGetError()) != GL_NO_ERROR) {
422 ST_LOGE("attachToContext: error binding external texture image %p "
423 "(slot %d): %#04x", image, mCurrentTexture, error);
424 err = UNKNOWN_ERROR;
425 }
426
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700427 // We destroy the EGLImageKHR here because the current buffer may no
428 // longer be associated with one of the buffer slots, so we have
429 // nowhere to to store it. If the buffer is still associated with a
430 // slot then another EGLImageKHR will be created next time that buffer
431 // gets acquired in updateTexImage.
432 eglDestroyImageKHR(dpy, image);
Jamie Gennis74bed552012-03-28 19:05:54 -0700433
434 if (err != OK) {
435 return err;
436 }
437 }
438
439 mEglDisplay = dpy;
440 mEglContext = ctx;
441 mTexName = tex;
442 mAttached = true;
443
444 return OK;
445}
446
447status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
448 ST_LOGV("syncForReleaseLocked");
449
450 if (mUseFenceSync && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
451 EGLSyncKHR fence = mEGLSlots[mCurrentTexture].mFence;
452 if (fence != EGL_NO_SYNC_KHR) {
453 // There is already a fence for the current slot. We need to wait
454 // on that before replacing it with another fence to ensure that all
455 // outstanding buffer accesses have completed before the producer
456 // accesses it.
457 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
458 if (result == EGL_FALSE) {
459 ST_LOGE("syncForReleaseLocked: error waiting for previous "
460 "fence: %#x", eglGetError());
461 return UNKNOWN_ERROR;
462 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
463 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
464 "fence");
465 return TIMED_OUT;
466 }
467 eglDestroySyncKHR(dpy, fence);
468 }
469
470 // Create a fence for the outstanding accesses in the current OpenGL ES
471 // context.
472 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
473 if (fence == EGL_NO_SYNC_KHR) {
474 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
475 eglGetError());
476 return UNKNOWN_ERROR;
477 }
478 glFlush();
479 mEGLSlots[mCurrentTexture].mFence = fence;
480 }
481
482 return OK;
483}
484
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700485bool SurfaceTexture::isExternalFormat(uint32_t format)
486{
487 switch (format) {
488 // supported YUV formats
489 case HAL_PIXEL_FORMAT_YV12:
490 // Legacy/deprecated YUV formats
491 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
492 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
493 case HAL_PIXEL_FORMAT_YCbCr_422_I:
494 return true;
495 }
496
497 // Any OEM format needs to be considered
498 if (format>=0x100 && format<=0x1FF)
499 return true;
500
501 return false;
502}
503
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700504GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700505 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700506}
507
Jamie Gennisf238e282011-01-09 16:33:17 -0800508void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800509 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700510 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
511}
512
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700513void SurfaceTexture::setFilteringEnabled(bool enabled) {
514 Mutex::Autolock lock(mMutex);
515 bool needsRecompute = mFilteringEnabled != enabled;
516 mFilteringEnabled = enabled;
517 if (needsRecompute) {
518 computeCurrentTransformMatrix();
519 }
520}
521
Jamie Gennis736aa952011-06-12 17:03:06 -0700522void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700523 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800524
Jamie Gennisa214c642011-01-14 13:53:31 -0800525 float xform[16];
526 for (int i = 0; i < 16; i++) {
527 xform[i] = mtxIdentity[i];
528 }
529 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
530 float result[16];
531 mtxMul(result, xform, mtxFlipH);
532 for (int i = 0; i < 16; i++) {
533 xform[i] = result[i];
534 }
535 }
536 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
537 float result[16];
538 mtxMul(result, xform, mtxFlipV);
539 for (int i = 0; i < 16; i++) {
540 xform[i] = result[i];
541 }
542 }
543 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
544 float result[16];
545 mtxMul(result, xform, mtxRot90);
546 for (int i = 0; i < 16; i++) {
547 xform[i] = result[i];
548 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800549 }
550
Daniel Lameae59d22012-01-22 15:26:27 -0800551 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisd72f2332012-05-07 13:50:11 -0700552 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700553 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
554 float bufferWidth = buf->getWidth();
555 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700556 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700557 float shrinkAmount = 0.0f;
558 if (mFilteringEnabled) {
559 // In order to prevent bilinear sampling beyond the edge of the
560 // crop rectangle we may need to shrink it by 2 texels in each
561 // dimension. Normally this would just need to take 1/2 a texel
562 // off each end, but because the chroma channels of YUV420 images
563 // are subsampled we may need to shrink the crop region by a whole
564 // texel on each side.
565 switch (buf->getPixelFormat()) {
566 case PIXEL_FORMAT_RGBA_8888:
567 case PIXEL_FORMAT_RGBX_8888:
568 case PIXEL_FORMAT_RGB_888:
569 case PIXEL_FORMAT_RGB_565:
570 case PIXEL_FORMAT_BGRA_8888:
571 case PIXEL_FORMAT_RGBA_5551:
572 case PIXEL_FORMAT_RGBA_4444:
573 // We know there's no subsampling of any channels, so we
574 // only need to shrink by a half a pixel.
575 shrinkAmount = 0.5;
Jamie Gennis91a68262012-04-16 13:41:05 -0700576
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700577 default:
578 // If we don't recognize the format, we must assume the
579 // worst case (that we care about), which is YUV420.
580 shrinkAmount = 1.0;
581 }
582 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700583
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700584 // Only shrink the dimensions that are not the size of the buffer.
585 if (cropRect.width() < bufferWidth) {
586 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
587 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
588 bufferWidth;
589 }
590 if (cropRect.height() < bufferHeight) {
591 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
592 bufferHeight;
593 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
594 bufferHeight;
595 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800596 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800597 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800598 sx, 0, 0, 0,
599 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800600 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800601 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800602 };
603
Jamie Gennisa214c642011-01-14 13:53:31 -0800604 float mtxBeforeFlipV[16];
605 mtxMul(mtxBeforeFlipV, crop, xform);
606
607 // SurfaceFlinger expects the top of its window textures to be at a Y
608 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
609 // want to expose this to applications, however, so we must add an
610 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700611 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800612}
613
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800614nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700615 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800616 Mutex::Autolock lock(mMutex);
617 return mCurrentTimestamp;
618}
619
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800620void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700621 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700622 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800623 Mutex::Autolock lock(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700624 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800625}
626
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800627EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
628 const sp<GraphicBuffer>& graphicBuffer) {
629 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
630 EGLint attrs[] = {
631 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
632 EGL_NONE,
633 };
634 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
635 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700636 if (image == EGL_NO_IMAGE_KHR) {
637 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700638 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800639 }
640 return image;
641}
642
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700643sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
644 Mutex::Autolock lock(mMutex);
645 return mCurrentTextureBuf;
646}
647
648Rect SurfaceTexture::getCurrentCrop() const {
649 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700650
651 Rect outCrop = mCurrentCrop;
652 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
653 int32_t newWidth = mCurrentCrop.width();
654 int32_t newHeight = mCurrentCrop.height();
655
656 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
657 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
658 ST_LOGV("too wide: newWidth = %d", newWidth);
659 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
660 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
661 ST_LOGV("too tall: newHeight = %d", newHeight);
662 }
663
664 // The crop is too wide
665 if (newWidth < mCurrentCrop.width()) {
666 int32_t dw = (newWidth - mCurrentCrop.width())/2;
667 outCrop.left -=dw;
668 outCrop.right += dw;
669 // The crop is too tall
670 } else if (newHeight < mCurrentCrop.height()) {
671 int32_t dh = (newHeight - mCurrentCrop.height())/2;
672 outCrop.top -= dh;
673 outCrop.bottom += dh;
674 }
675
676 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
677 outCrop.left, outCrop.top,
678 outCrop.right,outCrop.bottom);
679 }
680
Daniel Lam016c8cb2012-04-03 15:54:58 -0700681 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700682}
683
684uint32_t SurfaceTexture::getCurrentTransform() const {
685 Mutex::Autolock lock(mMutex);
686 return mCurrentTransform;
687}
688
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700689uint32_t SurfaceTexture::getCurrentScalingMode() const {
690 Mutex::Autolock lock(mMutex);
691 return mCurrentScalingMode;
692}
693
Jamie Gennis59769462011-11-19 18:04:43 -0800694bool SurfaceTexture::isSynchronousMode() const {
695 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800696 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800697}
698
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700699void SurfaceTexture::freeBufferLocked(int slotIndex) {
700 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
701 mEGLSlots[slotIndex].mGraphicBuffer = 0;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700702 if (slotIndex == mCurrentTexture) {
703 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
704 }
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700705 EGLImageKHR img = mEGLSlots[slotIndex].mEglImage;
706 if (img != EGL_NO_IMAGE_KHR) {
707 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
708 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800709 }
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700710 mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700711}
Daniel Lameae59d22012-01-22 15:26:27 -0800712
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700713void SurfaceTexture::abandon() {
714 ST_LOGV("abandon");
715 Mutex::Autolock lock(mMutex);
716
717 if (!mAbandoned) {
718 mAbandoned = true;
719 mCurrentTextureBuf.clear();
720
721 // destroy all egl buffers
722 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
723 freeBufferLocked(i);
724 }
725
726 // disconnect from the BufferQueue
727 mBufferQueue->consumerDisconnect();
728 mBufferQueue.clear();
729 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700730}
731
Jamie Gennisfa28c352011-09-16 17:30:26 -0700732void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800733 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700734 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800735 mBufferQueue->setConsumerName(name);
736}
737
738status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
739 Mutex::Autolock lock(mMutex);
740 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
741}
742
743status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
744 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700745 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800746 return mBufferQueue->setConsumerUsageBits(usage);
747}
748
749status_t SurfaceTexture::setTransformHint(uint32_t hint) {
750 Mutex::Autolock lock(mMutex);
751 return mBufferQueue->setTransformHint(hint);
752}
753
754// Used for refactoring BufferQueue from SurfaceTexture
755// Should not be in final interface once users of SurfaceTexture are clean up.
756status_t SurfaceTexture::setSynchronousMode(bool enabled) {
757 Mutex::Autolock lock(mMutex);
758 return mBufferQueue->setSynchronousMode(enabled);
759}
760
761// Used for refactoring, should not be in final interface
762sp<BufferQueue> SurfaceTexture::getBufferQueue() const {
763 Mutex::Autolock lock(mMutex);
764 return mBufferQueue;
765}
766
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700767void SurfaceTexture::onFrameAvailable() {
768 ST_LOGV("onFrameAvailable");
769
770 sp<FrameAvailableListener> listener;
771 { // scope for the lock
772 Mutex::Autolock lock(mMutex);
773 listener = mFrameAvailableListener;
774 }
775
776 if (listener != NULL) {
777 ST_LOGV("actually calling onFrameAvailable");
778 listener->onFrameAvailable();
779 }
780}
781
782void SurfaceTexture::onBuffersReleased() {
783 ST_LOGV("onBuffersReleased");
784
785 Mutex::Autolock lock(mMutex);
786
787 if (mAbandoned) {
788 // Nothing to do if we're already abandoned.
789 return;
790 }
791
792 uint32_t mask = 0;
793 mBufferQueue->getReleasedBuffers(&mask);
794 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
795 if (mask & (1 << i)) {
796 freeBufferLocked(i);
797 }
798 }
799}
800
Mathias Agopian68c77942011-05-09 19:08:33 -0700801void SurfaceTexture::dump(String8& result) const
802{
803 char buffer[1024];
804 dump(result, "", buffer, 1024);
805}
806
807void SurfaceTexture::dump(String8& result, const char* prefix,
808 char* buffer, size_t SIZE) const
809{
810 Mutex::Autolock _l(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700811 snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName,
812 int(mAbandoned));
Mathias Agopian68c77942011-05-09 19:08:33 -0700813 result.append(buffer);
814
Mathias Agopian68c77942011-05-09 19:08:33 -0700815 snprintf(buffer, SIZE,
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700816 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n",
817 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700818 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800819 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700820 );
821 result.append(buffer);
822
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700823 if (!mAbandoned) {
824 mBufferQueue->dump(result, prefix, buffer, SIZE);
825 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700826}
827
Jamie Gennisf238e282011-01-09 16:33:17 -0800828static void mtxMul(float out[16], const float a[16], const float b[16]) {
829 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
830 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
831 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
832 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
833
834 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
835 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
836 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
837 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
838
839 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
840 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
841 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
842 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
843
844 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
845 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
846 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
847 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
848}
849
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800850}; // namespace android