blob: 73f7c54433cd031fb393bba3b9e7b8dcb878bb9a [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),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700111 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800112#ifdef USE_FENCE_SYNC
113 mUseFenceSync(useFenceSync),
114#else
115 mUseFenceSync(false),
116#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800117 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700118 mEglDisplay(EGL_NO_DISPLAY),
119 mEglContext(EGL_NO_CONTEXT),
Daniel Lameae59d22012-01-22 15:26:27 -0800120 mAbandoned(false),
Jamie Gennis74bed552012-03-28 19:05:54 -0700121 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
122 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800123{
Daniel Lameae59d22012-01-22 15:26:27 -0800124 // Choose a name using the PID and a process-unique ID.
125 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700126 ST_LOGV("SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800127 if (bufferQueue == 0) {
Daniel Lamb2675792012-02-23 14:35:13 -0800128 ST_LOGV("Creating a new BufferQueue");
129 mBufferQueue = new BufferQueue(allowSynchronousMode);
130 }
131 else {
132 mBufferQueue = bufferQueue;
133 }
Daniel Lamb2675792012-02-23 14:35:13 -0800134
Jamie Gennisfa28c352011-09-16 17:30:26 -0700135 memcpy(mCurrentTransformMatrix, mtxIdentity,
136 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700137
138 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
139 // reference once the ctor ends, as that would cause the refcount of 'this'
140 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
141 // that's what we create.
142 wp<BufferQueue::ConsumerListener> listener;
143 sp<BufferQueue::ConsumerListener> proxy;
144 listener = static_cast<BufferQueue::ConsumerListener*>(this);
145 proxy = new BufferQueue::ProxyConsumerListener(listener);
146
147 status_t err = mBufferQueue->consumerConnect(proxy);
148 if (err != NO_ERROR) {
149 ST_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)",
150 strerror(-err), err);
151 } else {
152 mBufferQueue->setConsumerName(mName);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700153 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700154 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800155}
156
157SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700158 ST_LOGV("~SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800159
Daniel Lameae59d22012-01-22 15:26:27 -0800160 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800161}
162
Mathias Agopian80727112011-05-02 19:51:12 -0700163status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
164 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800165 return mBufferQueue->setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700166}
167
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800168
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700169status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
170{
Daniel Lamb2675792012-02-23 14:35:13 -0800171 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700172 mDefaultWidth = w;
173 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800174 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700175}
176
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800177status_t SurfaceTexture::updateTexImage() {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800178 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700179 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800180 Mutex::Autolock lock(mMutex);
181
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700182 status_t err = NO_ERROR;
183
Mathias Agopiane47498f2011-08-08 19:35:15 -0700184 if (mAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700185 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700186 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700187 }
188
Jamie Gennis74bed552012-03-28 19:05:54 -0700189 if (!mAttached) {
190 ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL "
191 "ES context");
192 return INVALID_OPERATION;
193 }
194
Jamie Gennisce561372012-03-19 18:33:05 -0700195 EGLDisplay dpy = eglGetCurrentDisplay();
196 EGLContext ctx = eglGetCurrentContext();
197
Jamie Gennis74bed552012-03-28 19:05:54 -0700198 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
199 dpy == EGL_NO_DISPLAY) {
Jamie Gennisce561372012-03-19 18:33:05 -0700200 ST_LOGE("updateTexImage: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700201 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700202 }
203
Jamie Gennis74bed552012-03-28 19:05:54 -0700204 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
205 ctx == EGL_NO_CONTEXT) {
Jamie Gennisce561372012-03-19 18:33:05 -0700206 ST_LOGE("updateTexImage: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700207 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700208 }
209
210 mEglDisplay = dpy;
211 mEglContext = ctx;
212
Daniel Lamb2675792012-02-23 14:35:13 -0800213 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800214
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700215 // In asynchronous mode the list is guaranteed to be one buffer
216 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lamfbcda932012-04-09 22:51:52 -0700217 err = mBufferQueue->acquireBuffer(&item);
218 if (err == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800219 int buf = item.mBuf;
220 // This buffer was newly allocated, so we need to clean up on our side
221 if (item.mGraphicBuffer != NULL) {
222 mEGLSlots[buf].mGraphicBuffer = 0;
223 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700224 eglDestroyImageKHR(dpy, mEGLSlots[buf].mEglImage);
Daniel Lameae59d22012-01-22 15:26:27 -0800225 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800226 }
227 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
228 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700229
Jamie Gennisf238e282011-01-09 16:33:17 -0800230 // Update the GL texture object.
Daniel Lameae59d22012-01-22 15:26:27 -0800231 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800232 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800233 if (item.mGraphicBuffer == 0) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700234 ST_LOGE("updateTexImage: buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700235 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700236 }
Daniel Lameae59d22012-01-22 15:26:27 -0800237 image = createImage(dpy, item.mGraphicBuffer);
238 mEGLSlots[buf].mEglImage = image;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700239 if (image == EGL_NO_IMAGE_KHR) {
240 // NOTE: if dpy was invalid, createImage() is guaranteed to
241 // fail. so we'd end up here.
Jamie Gennis74bed552012-03-28 19:05:54 -0700242 return UNKNOWN_ERROR;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700243 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800244 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800245
246 GLint error;
247 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700248 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800249 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700250
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700251 glBindTexture(mTexTarget, mTexName);
252 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700253
Jamie Gennis0eb88512011-01-26 11:52:02 -0800254 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700255 ST_LOGE("updateTexImage: error binding external texture image %p "
256 "(slot %d): %#04x", image, buf, error);
257 err = UNKNOWN_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800258 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800259
Jamie Gennis74bed552012-03-28 19:05:54 -0700260 if (err == OK) {
261 err = syncForReleaseLocked(dpy);
262 }
263
264 if (err != OK) {
265 // Release the buffer we just acquired. It's not safe to
266 // release the old buffer, so instead we just drop the new frame.
267 mBufferQueue->releaseBuffer(buf, dpy, mEGLSlots[buf].mFence);
268 mEGLSlots[buf].mFence = EGL_NO_SYNC_KHR;
269 return err;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800270 }
271
272 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
273 mCurrentTexture,
274 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800275 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700276
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700277 // release old buffer
Jamie Gennis74bed552012-03-28 19:05:54 -0700278 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700279 status_t status = mBufferQueue->releaseBuffer(mCurrentTexture, dpy, mEGLSlots[mCurrentTexture].mFence);
280
Jamie Gennis74bed552012-03-28 19:05:54 -0700281 mEGLSlots[mCurrentTexture].mFence = EGL_NO_SYNC_KHR;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700282 if (status == BufferQueue::STALE_BUFFER_SLOT) {
283 freeBufferLocked(mCurrentTexture);
284 } else if (status != OK) {
285 ST_LOGE("updateTexImage: released invalid buffer");
286 err = status;
287 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700288 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700289
Jamie Gennis9a78c902011-01-12 18:30:40 -0800290 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700291 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800292 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
293 mCurrentCrop = item.mCrop;
294 mCurrentTransform = item.mTransform;
295 mCurrentScalingMode = item.mScalingMode;
296 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700297 computeCurrentTransformMatrix();
Daniel Lamfbcda932012-04-09 22:51:52 -0700298 } else {
299 if (err < 0) {
300 ALOGE("updateTexImage failed on acquire %d", err);
301 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700302 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700303 glBindTexture(mTexTarget, mTexName);
Daniel Lamfbcda932012-04-09 22:51:52 -0700304 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800305 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700306
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700307 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800308}
309
Jamie Gennis74bed552012-03-28 19:05:54 -0700310status_t SurfaceTexture::detachFromContext() {
311 ATRACE_CALL();
312 ST_LOGV("detachFromContext");
313 Mutex::Autolock lock(mMutex);
314
315 if (mAbandoned) {
316 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
317 return NO_INIT;
318 }
319
320 if (!mAttached) {
321 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
322 "context");
323 return INVALID_OPERATION;
324 }
325
326 EGLDisplay dpy = eglGetCurrentDisplay();
327 EGLContext ctx = eglGetCurrentContext();
328
329 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
330 ST_LOGE("detachFromContext: invalid current EGLDisplay");
331 return INVALID_OPERATION;
332 }
333
334 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
335 ST_LOGE("detachFromContext: invalid current EGLContext");
336 return INVALID_OPERATION;
337 }
338
339 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
340 status_t err = syncForReleaseLocked(dpy);
341 if (err != OK) {
342 return err;
343 }
344
345 glDeleteTextures(1, &mTexName);
346 }
347
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700348 // Because we're giving up the EGLDisplay we need to free all the EGLImages
349 // that are associated with it. They'll be recreated when the
350 // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a
351 // new EGLDisplay).
352 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
353 EGLImageKHR img = mEGLSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700354 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700355 eglDestroyImageKHR(mEglDisplay, img);
356 mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
357 }
358 }
359
Jamie Gennis74bed552012-03-28 19:05:54 -0700360 mEglDisplay = EGL_NO_DISPLAY;
361 mEglContext = EGL_NO_CONTEXT;
362 mAttached = false;
363
364 return OK;
365}
366
367status_t SurfaceTexture::attachToContext(GLuint tex) {
368 ATRACE_CALL();
369 ST_LOGV("attachToContext");
370 Mutex::Autolock lock(mMutex);
371
372 if (mAbandoned) {
373 ST_LOGE("attachToContext: abandoned SurfaceTexture");
374 return NO_INIT;
375 }
376
377 if (mAttached) {
378 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
379 "context");
380 return INVALID_OPERATION;
381 }
382
383 EGLDisplay dpy = eglGetCurrentDisplay();
384 EGLContext ctx = eglGetCurrentContext();
385
386 if (dpy == EGL_NO_DISPLAY) {
387 ST_LOGE("attachToContext: invalid current EGLDisplay");
388 return INVALID_OPERATION;
389 }
390
391 if (ctx == EGL_NO_CONTEXT) {
392 ST_LOGE("attachToContext: invalid current EGLContext");
393 return INVALID_OPERATION;
394 }
395
396 // We need to bind the texture regardless of whether there's a current
397 // buffer.
398 glBindTexture(mTexTarget, tex);
399
400 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700401 // The EGLImageKHR that was associated with the slot was destroyed when
402 // the SurfaceTexture was detached from the old context, so we need to
403 // recreate it here.
404 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
405 if (image == EGL_NO_IMAGE_KHR) {
406 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700407 }
408
409 // Attach the current buffer to the GL texture.
410 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
411
412 GLint error;
413 status_t err = OK;
414 while ((error = glGetError()) != GL_NO_ERROR) {
415 ST_LOGE("attachToContext: error binding external texture image %p "
416 "(slot %d): %#04x", image, mCurrentTexture, error);
417 err = UNKNOWN_ERROR;
418 }
419
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700420 // We destroy the EGLImageKHR here because the current buffer may no
421 // longer be associated with one of the buffer slots, so we have
422 // nowhere to to store it. If the buffer is still associated with a
423 // slot then another EGLImageKHR will be created next time that buffer
424 // gets acquired in updateTexImage.
425 eglDestroyImageKHR(dpy, image);
Jamie Gennis74bed552012-03-28 19:05:54 -0700426
427 if (err != OK) {
428 return err;
429 }
430 }
431
432 mEglDisplay = dpy;
433 mEglContext = ctx;
434 mTexName = tex;
435 mAttached = true;
436
437 return OK;
438}
439
440status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
441 ST_LOGV("syncForReleaseLocked");
442
443 if (mUseFenceSync && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
444 EGLSyncKHR fence = mEGLSlots[mCurrentTexture].mFence;
445 if (fence != EGL_NO_SYNC_KHR) {
446 // There is already a fence for the current slot. We need to wait
447 // on that before replacing it with another fence to ensure that all
448 // outstanding buffer accesses have completed before the producer
449 // accesses it.
450 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
451 if (result == EGL_FALSE) {
452 ST_LOGE("syncForReleaseLocked: error waiting for previous "
453 "fence: %#x", eglGetError());
454 return UNKNOWN_ERROR;
455 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
456 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
457 "fence");
458 return TIMED_OUT;
459 }
460 eglDestroySyncKHR(dpy, fence);
461 }
462
463 // Create a fence for the outstanding accesses in the current OpenGL ES
464 // context.
465 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
466 if (fence == EGL_NO_SYNC_KHR) {
467 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
468 eglGetError());
469 return UNKNOWN_ERROR;
470 }
471 glFlush();
472 mEGLSlots[mCurrentTexture].mFence = fence;
473 }
474
475 return OK;
476}
477
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700478bool SurfaceTexture::isExternalFormat(uint32_t format)
479{
480 switch (format) {
481 // supported YUV formats
482 case HAL_PIXEL_FORMAT_YV12:
483 // Legacy/deprecated YUV formats
484 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
485 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
486 case HAL_PIXEL_FORMAT_YCbCr_422_I:
487 return true;
488 }
489
490 // Any OEM format needs to be considered
491 if (format>=0x100 && format<=0x1FF)
492 return true;
493
494 return false;
495}
496
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700497GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700498 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700499}
500
Jamie Gennisf238e282011-01-09 16:33:17 -0800501void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800502 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700503 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
504}
505
506void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700507 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800508
Jamie Gennisa214c642011-01-14 13:53:31 -0800509 float xform[16];
510 for (int i = 0; i < 16; i++) {
511 xform[i] = mtxIdentity[i];
512 }
513 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
514 float result[16];
515 mtxMul(result, xform, mtxFlipH);
516 for (int i = 0; i < 16; i++) {
517 xform[i] = result[i];
518 }
519 }
520 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
521 float result[16];
522 mtxMul(result, xform, mtxFlipV);
523 for (int i = 0; i < 16; i++) {
524 xform[i] = result[i];
525 }
526 }
527 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
528 float result[16];
529 mtxMul(result, xform, mtxRot90);
530 for (int i = 0; i < 16; i++) {
531 xform[i] = result[i];
532 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800533 }
534
Daniel Lameae59d22012-01-22 15:26:27 -0800535 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisd72f2332012-05-07 13:50:11 -0700536 Rect cropRect = mCurrentCrop;
Jamie Gennisa214c642011-01-14 13:53:31 -0800537 float tx, ty, sx, sy;
Jamie Gennisd72f2332012-05-07 13:50:11 -0700538 if (!cropRect.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800539 // In order to prevent bilinear sampling at the of the crop rectangle we
540 // may need to shrink it by 2 texels in each direction. Normally this
541 // would just need to take 1/2 a texel off each end, but because the
542 // chroma channels will likely be subsampled we need to chop off a whole
543 // texel. This will cause artifacts if someone does nearest sampling
544 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
545 // accomodate the bilinear and nearest sampling uses.
546 //
547 // If nearest sampling turns out to be a desirable usage of these
548 // textures then we could add the ability to switch a SurfaceTexture to
549 // nearest-mode. Preferably, however, the image producers (video
550 // decoder, camera, etc.) would simply not use a crop rectangle (or at
551 // least not tell the framework about it) so that the GPU can do the
552 // correct edge behavior.
Jamie Gennis91a68262012-04-16 13:41:05 -0700553 const float shrinkAmount = 1.0f; // the amount that each edge is shrunk
554
Jamie Gennisd72f2332012-05-07 13:50:11 -0700555 float bufferWidth = buf->getWidth();
556 float bufferHeight = buf->getHeight();
557
558 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
559 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
560 bufferHeight;
561 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
562 bufferWidth;
563 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
564 bufferHeight;
Jamie Gennisa214c642011-01-14 13:53:31 -0800565 } else {
566 tx = 0.0f;
567 ty = 0.0f;
568 sx = 1.0f;
569 sy = 1.0f;
570 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800571 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800572 sx, 0, 0, 0,
573 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800574 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800575 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800576 };
577
Jamie Gennisa214c642011-01-14 13:53:31 -0800578 float mtxBeforeFlipV[16];
579 mtxMul(mtxBeforeFlipV, crop, xform);
580
581 // SurfaceFlinger expects the top of its window textures to be at a Y
582 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
583 // want to expose this to applications, however, so we must add an
584 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700585 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800586}
587
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800588nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700589 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800590 Mutex::Autolock lock(mMutex);
591 return mCurrentTimestamp;
592}
593
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800594void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700595 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700596 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800597 Mutex::Autolock lock(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700598 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800599}
600
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800601EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
602 const sp<GraphicBuffer>& graphicBuffer) {
603 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
604 EGLint attrs[] = {
605 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
606 EGL_NONE,
607 };
608 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
609 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700610 if (image == EGL_NO_IMAGE_KHR) {
611 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700612 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800613 }
614 return image;
615}
616
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700617sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
618 Mutex::Autolock lock(mMutex);
619 return mCurrentTextureBuf;
620}
621
622Rect SurfaceTexture::getCurrentCrop() const {
623 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700624
625 Rect outCrop = mCurrentCrop;
626 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
627 int32_t newWidth = mCurrentCrop.width();
628 int32_t newHeight = mCurrentCrop.height();
629
630 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
631 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
632 ST_LOGV("too wide: newWidth = %d", newWidth);
633 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
634 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
635 ST_LOGV("too tall: newHeight = %d", newHeight);
636 }
637
638 // The crop is too wide
639 if (newWidth < mCurrentCrop.width()) {
640 int32_t dw = (newWidth - mCurrentCrop.width())/2;
641 outCrop.left -=dw;
642 outCrop.right += dw;
643 // The crop is too tall
644 } else if (newHeight < mCurrentCrop.height()) {
645 int32_t dh = (newHeight - mCurrentCrop.height())/2;
646 outCrop.top -= dh;
647 outCrop.bottom += dh;
648 }
649
650 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
651 outCrop.left, outCrop.top,
652 outCrop.right,outCrop.bottom);
653 }
654
Daniel Lam016c8cb2012-04-03 15:54:58 -0700655 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700656}
657
658uint32_t SurfaceTexture::getCurrentTransform() const {
659 Mutex::Autolock lock(mMutex);
660 return mCurrentTransform;
661}
662
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700663uint32_t SurfaceTexture::getCurrentScalingMode() const {
664 Mutex::Autolock lock(mMutex);
665 return mCurrentScalingMode;
666}
667
Jamie Gennis59769462011-11-19 18:04:43 -0800668bool SurfaceTexture::isSynchronousMode() const {
669 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800670 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800671}
672
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700673void SurfaceTexture::freeBufferLocked(int slotIndex) {
674 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
675 mEGLSlots[slotIndex].mGraphicBuffer = 0;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700676 if (slotIndex == mCurrentTexture) {
677 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
678 }
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700679 EGLImageKHR img = mEGLSlots[slotIndex].mEglImage;
680 if (img != EGL_NO_IMAGE_KHR) {
681 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
682 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800683 }
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700684 mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700685}
Daniel Lameae59d22012-01-22 15:26:27 -0800686
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700687void SurfaceTexture::abandon() {
688 ST_LOGV("abandon");
689 Mutex::Autolock lock(mMutex);
690
691 if (!mAbandoned) {
692 mAbandoned = true;
693 mCurrentTextureBuf.clear();
694
695 // destroy all egl buffers
696 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
697 freeBufferLocked(i);
698 }
699
700 // disconnect from the BufferQueue
701 mBufferQueue->consumerDisconnect();
702 mBufferQueue.clear();
703 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700704}
705
Jamie Gennisfa28c352011-09-16 17:30:26 -0700706void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800707 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700708 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800709 mBufferQueue->setConsumerName(name);
710}
711
712status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
713 Mutex::Autolock lock(mMutex);
714 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
715}
716
717status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
718 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700719 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800720 return mBufferQueue->setConsumerUsageBits(usage);
721}
722
723status_t SurfaceTexture::setTransformHint(uint32_t hint) {
724 Mutex::Autolock lock(mMutex);
725 return mBufferQueue->setTransformHint(hint);
726}
727
728// Used for refactoring BufferQueue from SurfaceTexture
729// Should not be in final interface once users of SurfaceTexture are clean up.
730status_t SurfaceTexture::setSynchronousMode(bool enabled) {
731 Mutex::Autolock lock(mMutex);
732 return mBufferQueue->setSynchronousMode(enabled);
733}
734
735// Used for refactoring, should not be in final interface
736sp<BufferQueue> SurfaceTexture::getBufferQueue() const {
737 Mutex::Autolock lock(mMutex);
738 return mBufferQueue;
739}
740
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700741void SurfaceTexture::onFrameAvailable() {
742 ST_LOGV("onFrameAvailable");
743
744 sp<FrameAvailableListener> listener;
745 { // scope for the lock
746 Mutex::Autolock lock(mMutex);
747 listener = mFrameAvailableListener;
748 }
749
750 if (listener != NULL) {
751 ST_LOGV("actually calling onFrameAvailable");
752 listener->onFrameAvailable();
753 }
754}
755
756void SurfaceTexture::onBuffersReleased() {
757 ST_LOGV("onBuffersReleased");
758
759 Mutex::Autolock lock(mMutex);
760
761 if (mAbandoned) {
762 // Nothing to do if we're already abandoned.
763 return;
764 }
765
766 uint32_t mask = 0;
767 mBufferQueue->getReleasedBuffers(&mask);
768 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
769 if (mask & (1 << i)) {
770 freeBufferLocked(i);
771 }
772 }
773}
774
Mathias Agopian68c77942011-05-09 19:08:33 -0700775void SurfaceTexture::dump(String8& result) const
776{
777 char buffer[1024];
778 dump(result, "", buffer, 1024);
779}
780
781void SurfaceTexture::dump(String8& result, const char* prefix,
782 char* buffer, size_t SIZE) const
783{
784 Mutex::Autolock _l(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700785 snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName,
786 int(mAbandoned));
Mathias Agopian68c77942011-05-09 19:08:33 -0700787 result.append(buffer);
788
Mathias Agopian68c77942011-05-09 19:08:33 -0700789 snprintf(buffer, SIZE,
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700790 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n",
791 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700792 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800793 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700794 );
795 result.append(buffer);
796
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700797 if (!mAbandoned) {
798 mBufferQueue->dump(result, prefix, buffer, SIZE);
799 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700800}
801
Jamie Gennisf238e282011-01-09 16:33:17 -0800802static void mtxMul(float out[16], const float a[16], const float b[16]) {
803 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
804 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
805 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
806 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
807
808 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
809 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
810 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
811 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
812
813 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
814 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
815 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
816 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
817
818 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
819 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
820 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
821 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
822}
823
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800824}; // namespace android