blob: 0fa9ca1ed774441f725cd869eefc642601d2d269 [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);
153 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800154}
155
156SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700157 ST_LOGV("~SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800158
Daniel Lameae59d22012-01-22 15:26:27 -0800159 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800160}
161
Mathias Agopian80727112011-05-02 19:51:12 -0700162status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
163 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800164 return mBufferQueue->setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700165}
166
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800167
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700168status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
169{
Daniel Lamb2675792012-02-23 14:35:13 -0800170 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700171 mDefaultWidth = w;
172 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800173 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700174}
175
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800176status_t SurfaceTexture::updateTexImage() {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800177 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700178 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800179 Mutex::Autolock lock(mMutex);
180
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700181 status_t err = NO_ERROR;
182
Mathias Agopiane47498f2011-08-08 19:35:15 -0700183 if (mAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700184 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700185 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700186 }
187
Jamie Gennis74bed552012-03-28 19:05:54 -0700188 if (!mAttached) {
189 ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL "
190 "ES context");
191 return INVALID_OPERATION;
192 }
193
Jamie Gennisce561372012-03-19 18:33:05 -0700194 EGLDisplay dpy = eglGetCurrentDisplay();
195 EGLContext ctx = eglGetCurrentContext();
196
Jamie Gennis74bed552012-03-28 19:05:54 -0700197 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
198 dpy == EGL_NO_DISPLAY) {
Jamie Gennisce561372012-03-19 18:33:05 -0700199 ST_LOGE("updateTexImage: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700200 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700201 }
202
Jamie Gennis74bed552012-03-28 19:05:54 -0700203 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
204 ctx == EGL_NO_CONTEXT) {
Jamie Gennisce561372012-03-19 18:33:05 -0700205 ST_LOGE("updateTexImage: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700206 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700207 }
208
209 mEglDisplay = dpy;
210 mEglContext = ctx;
211
Daniel Lamb2675792012-02-23 14:35:13 -0800212 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800213
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700214 // In asynchronous mode the list is guaranteed to be one buffer
215 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lamfbcda932012-04-09 22:51:52 -0700216 err = mBufferQueue->acquireBuffer(&item);
217 if (err == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800218 int buf = item.mBuf;
219 // This buffer was newly allocated, so we need to clean up on our side
220 if (item.mGraphicBuffer != NULL) {
221 mEGLSlots[buf].mGraphicBuffer = 0;
222 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700223 eglDestroyImageKHR(dpy, mEGLSlots[buf].mEglImage);
Daniel Lameae59d22012-01-22 15:26:27 -0800224 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800225 }
226 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
227 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700228
Jamie Gennisf238e282011-01-09 16:33:17 -0800229 // Update the GL texture object.
Daniel Lameae59d22012-01-22 15:26:27 -0800230 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800231 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800232 if (item.mGraphicBuffer == 0) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700233 ST_LOGE("updateTexImage: buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700234 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700235 }
Daniel Lameae59d22012-01-22 15:26:27 -0800236 image = createImage(dpy, item.mGraphicBuffer);
237 mEGLSlots[buf].mEglImage = image;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700238 if (image == EGL_NO_IMAGE_KHR) {
239 // NOTE: if dpy was invalid, createImage() is guaranteed to
240 // fail. so we'd end up here.
Jamie Gennis74bed552012-03-28 19:05:54 -0700241 return UNKNOWN_ERROR;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700242 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800243 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800244
245 GLint error;
246 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700247 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800248 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700249
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700250 glBindTexture(mTexTarget, mTexName);
251 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700252
Jamie Gennis0eb88512011-01-26 11:52:02 -0800253 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700254 ST_LOGE("updateTexImage: error binding external texture image %p "
255 "(slot %d): %#04x", image, buf, error);
256 err = UNKNOWN_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800257 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800258
Jamie Gennis74bed552012-03-28 19:05:54 -0700259 if (err == OK) {
260 err = syncForReleaseLocked(dpy);
261 }
262
263 if (err != OK) {
264 // Release the buffer we just acquired. It's not safe to
265 // release the old buffer, so instead we just drop the new frame.
266 mBufferQueue->releaseBuffer(buf, dpy, mEGLSlots[buf].mFence);
267 mEGLSlots[buf].mFence = EGL_NO_SYNC_KHR;
268 return err;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800269 }
270
271 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
272 mCurrentTexture,
273 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800274 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700275
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700276 // release old buffer
Jamie Gennis74bed552012-03-28 19:05:54 -0700277 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700278 status_t status = mBufferQueue->releaseBuffer(mCurrentTexture, dpy, mEGLSlots[mCurrentTexture].mFence);
279
Jamie Gennis74bed552012-03-28 19:05:54 -0700280 mEGLSlots[mCurrentTexture].mFence = EGL_NO_SYNC_KHR;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700281 if (status == BufferQueue::STALE_BUFFER_SLOT) {
282 freeBufferLocked(mCurrentTexture);
283 } else if (status != OK) {
284 ST_LOGE("updateTexImage: released invalid buffer");
285 err = status;
286 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700287 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700288
Jamie Gennis9a78c902011-01-12 18:30:40 -0800289 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700290 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800291 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
292 mCurrentCrop = item.mCrop;
293 mCurrentTransform = item.mTransform;
294 mCurrentScalingMode = item.mScalingMode;
295 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700296 computeCurrentTransformMatrix();
Daniel Lamfbcda932012-04-09 22:51:52 -0700297 } else {
298 if (err < 0) {
299 ALOGE("updateTexImage failed on acquire %d", err);
300 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700301 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700302 glBindTexture(mTexTarget, mTexName);
Daniel Lamfbcda932012-04-09 22:51:52 -0700303 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800304 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700305
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700306 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800307}
308
Jamie Gennis74bed552012-03-28 19:05:54 -0700309status_t SurfaceTexture::detachFromContext() {
310 ATRACE_CALL();
311 ST_LOGV("detachFromContext");
312 Mutex::Autolock lock(mMutex);
313
314 if (mAbandoned) {
315 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
316 return NO_INIT;
317 }
318
319 if (!mAttached) {
320 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
321 "context");
322 return INVALID_OPERATION;
323 }
324
325 EGLDisplay dpy = eglGetCurrentDisplay();
326 EGLContext ctx = eglGetCurrentContext();
327
328 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
329 ST_LOGE("detachFromContext: invalid current EGLDisplay");
330 return INVALID_OPERATION;
331 }
332
333 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
334 ST_LOGE("detachFromContext: invalid current EGLContext");
335 return INVALID_OPERATION;
336 }
337
338 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
339 status_t err = syncForReleaseLocked(dpy);
340 if (err != OK) {
341 return err;
342 }
343
344 glDeleteTextures(1, &mTexName);
345 }
346
347 mEglDisplay = EGL_NO_DISPLAY;
348 mEglContext = EGL_NO_CONTEXT;
349 mAttached = false;
350
351 return OK;
352}
353
354status_t SurfaceTexture::attachToContext(GLuint tex) {
355 ATRACE_CALL();
356 ST_LOGV("attachToContext");
357 Mutex::Autolock lock(mMutex);
358
359 if (mAbandoned) {
360 ST_LOGE("attachToContext: abandoned SurfaceTexture");
361 return NO_INIT;
362 }
363
364 if (mAttached) {
365 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
366 "context");
367 return INVALID_OPERATION;
368 }
369
370 EGLDisplay dpy = eglGetCurrentDisplay();
371 EGLContext ctx = eglGetCurrentContext();
372
373 if (dpy == EGL_NO_DISPLAY) {
374 ST_LOGE("attachToContext: invalid current EGLDisplay");
375 return INVALID_OPERATION;
376 }
377
378 if (ctx == EGL_NO_CONTEXT) {
379 ST_LOGE("attachToContext: invalid current EGLContext");
380 return INVALID_OPERATION;
381 }
382
383 // We need to bind the texture regardless of whether there's a current
384 // buffer.
385 glBindTexture(mTexTarget, tex);
386
387 if (mCurrentTextureBuf != NULL) {
388 // If the current buffer is no longer associated with a slot, then it
389 // doesn't have an EGLImage. In that case we create one now, but we also
390 // destroy it once we've used it to attach the buffer to the OpenGL ES
391 // texture.
392 bool imageNeedsDestroy = false;
393 EGLImageKHR image = EGL_NO_IMAGE_KHR;
394 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
395 image = mEGLSlots[mCurrentTexture].mEglImage;
396 imageNeedsDestroy = false;
397 } else {
398 image = createImage(dpy, mCurrentTextureBuf);
399 if (image == EGL_NO_IMAGE_KHR) {
400 return UNKNOWN_ERROR;
401 }
402 imageNeedsDestroy = true;
403 }
404
405 // Attach the current buffer to the GL texture.
406 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
407
408 GLint error;
409 status_t err = OK;
410 while ((error = glGetError()) != GL_NO_ERROR) {
411 ST_LOGE("attachToContext: error binding external texture image %p "
412 "(slot %d): %#04x", image, mCurrentTexture, error);
413 err = UNKNOWN_ERROR;
414 }
415
416 if (imageNeedsDestroy) {
417 eglDestroyImageKHR(dpy, image);
418 }
419
420 if (err != OK) {
421 return err;
422 }
423 }
424
425 mEglDisplay = dpy;
426 mEglContext = ctx;
427 mTexName = tex;
428 mAttached = true;
429
430 return OK;
431}
432
433status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
434 ST_LOGV("syncForReleaseLocked");
435
436 if (mUseFenceSync && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
437 EGLSyncKHR fence = mEGLSlots[mCurrentTexture].mFence;
438 if (fence != EGL_NO_SYNC_KHR) {
439 // There is already a fence for the current slot. We need to wait
440 // on that before replacing it with another fence to ensure that all
441 // outstanding buffer accesses have completed before the producer
442 // accesses it.
443 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
444 if (result == EGL_FALSE) {
445 ST_LOGE("syncForReleaseLocked: error waiting for previous "
446 "fence: %#x", eglGetError());
447 return UNKNOWN_ERROR;
448 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
449 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
450 "fence");
451 return TIMED_OUT;
452 }
453 eglDestroySyncKHR(dpy, fence);
454 }
455
456 // Create a fence for the outstanding accesses in the current OpenGL ES
457 // context.
458 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
459 if (fence == EGL_NO_SYNC_KHR) {
460 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
461 eglGetError());
462 return UNKNOWN_ERROR;
463 }
464 glFlush();
465 mEGLSlots[mCurrentTexture].mFence = fence;
466 }
467
468 return OK;
469}
470
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700471bool SurfaceTexture::isExternalFormat(uint32_t format)
472{
473 switch (format) {
474 // supported YUV formats
475 case HAL_PIXEL_FORMAT_YV12:
476 // Legacy/deprecated YUV formats
477 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
478 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
479 case HAL_PIXEL_FORMAT_YCbCr_422_I:
480 return true;
481 }
482
483 // Any OEM format needs to be considered
484 if (format>=0x100 && format<=0x1FF)
485 return true;
486
487 return false;
488}
489
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700490GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700491 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700492}
493
Jamie Gennisf238e282011-01-09 16:33:17 -0800494void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800495 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700496 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
497}
498
499void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700500 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800501
Jamie Gennisa214c642011-01-14 13:53:31 -0800502 float xform[16];
503 for (int i = 0; i < 16; i++) {
504 xform[i] = mtxIdentity[i];
505 }
506 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
507 float result[16];
508 mtxMul(result, xform, mtxFlipH);
509 for (int i = 0; i < 16; i++) {
510 xform[i] = result[i];
511 }
512 }
513 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
514 float result[16];
515 mtxMul(result, xform, mtxFlipV);
516 for (int i = 0; i < 16; i++) {
517 xform[i] = result[i];
518 }
519 }
520 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
521 float result[16];
522 mtxMul(result, xform, mtxRot90);
523 for (int i = 0; i < 16; i++) {
524 xform[i] = result[i];
525 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800526 }
527
Daniel Lameae59d22012-01-22 15:26:27 -0800528 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisa214c642011-01-14 13:53:31 -0800529 float tx, ty, sx, sy;
530 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800531 // In order to prevent bilinear sampling at the of the crop rectangle we
532 // may need to shrink it by 2 texels in each direction. Normally this
533 // would just need to take 1/2 a texel off each end, but because the
534 // chroma channels will likely be subsampled we need to chop off a whole
535 // texel. This will cause artifacts if someone does nearest sampling
536 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
537 // accomodate the bilinear and nearest sampling uses.
538 //
539 // If nearest sampling turns out to be a desirable usage of these
540 // textures then we could add the ability to switch a SurfaceTexture to
541 // nearest-mode. Preferably, however, the image producers (video
542 // decoder, camera, etc.) would simply not use a crop rectangle (or at
543 // least not tell the framework about it) so that the GPU can do the
544 // correct edge behavior.
545 int xshrink = 0, yshrink = 0;
546 if (mCurrentCrop.left > 0) {
547 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
548 xshrink++;
549 } else {
550 tx = 0.0f;
551 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700552 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800553 xshrink++;
554 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700555 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800556 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
557 float(buf->getHeight());
558 yshrink++;
559 } else {
560 ty = 0.0f;
561 }
562 if (mCurrentCrop.top > 0) {
563 yshrink++;
564 }
565 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
566 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800567 } else {
568 tx = 0.0f;
569 ty = 0.0f;
570 sx = 1.0f;
571 sy = 1.0f;
572 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800573 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800574 sx, 0, 0, 0,
575 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800576 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800577 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800578 };
579
Jamie Gennisa214c642011-01-14 13:53:31 -0800580 float mtxBeforeFlipV[16];
581 mtxMul(mtxBeforeFlipV, crop, xform);
582
583 // SurfaceFlinger expects the top of its window textures to be at a Y
584 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
585 // want to expose this to applications, however, so we must add an
586 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700587 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800588}
589
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800590nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700591 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800592 Mutex::Autolock lock(mMutex);
593 return mCurrentTimestamp;
594}
595
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800596void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700597 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700598 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800599 Mutex::Autolock lock(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700600 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800601}
602
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800603EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
604 const sp<GraphicBuffer>& graphicBuffer) {
605 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
606 EGLint attrs[] = {
607 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
608 EGL_NONE,
609 };
610 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
611 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700612 if (image == EGL_NO_IMAGE_KHR) {
613 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700614 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800615 }
616 return image;
617}
618
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700619sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
620 Mutex::Autolock lock(mMutex);
621 return mCurrentTextureBuf;
622}
623
624Rect SurfaceTexture::getCurrentCrop() const {
625 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700626
627 Rect outCrop = mCurrentCrop;
628 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
629 int32_t newWidth = mCurrentCrop.width();
630 int32_t newHeight = mCurrentCrop.height();
631
632 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
633 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
634 ST_LOGV("too wide: newWidth = %d", newWidth);
635 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
636 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
637 ST_LOGV("too tall: newHeight = %d", newHeight);
638 }
639
640 // The crop is too wide
641 if (newWidth < mCurrentCrop.width()) {
642 int32_t dw = (newWidth - mCurrentCrop.width())/2;
643 outCrop.left -=dw;
644 outCrop.right += dw;
645 // The crop is too tall
646 } else if (newHeight < mCurrentCrop.height()) {
647 int32_t dh = (newHeight - mCurrentCrop.height())/2;
648 outCrop.top -= dh;
649 outCrop.bottom += dh;
650 }
651
652 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
653 outCrop.left, outCrop.top,
654 outCrop.right,outCrop.bottom);
655 }
656
657
658
659 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700660}
661
662uint32_t SurfaceTexture::getCurrentTransform() const {
663 Mutex::Autolock lock(mMutex);
664 return mCurrentTransform;
665}
666
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700667uint32_t SurfaceTexture::getCurrentScalingMode() const {
668 Mutex::Autolock lock(mMutex);
669 return mCurrentScalingMode;
670}
671
Jamie Gennis59769462011-11-19 18:04:43 -0800672bool SurfaceTexture::isSynchronousMode() const {
673 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800674 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800675}
676
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700677void SurfaceTexture::freeBufferLocked(int slotIndex) {
678 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
679 mEGLSlots[slotIndex].mGraphicBuffer = 0;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700680 if (slotIndex == mCurrentTexture) {
681 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
682 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700683 if (mEGLSlots[slotIndex].mEglImage != EGL_NO_IMAGE_KHR) {
684 EGLImageKHR img = mEGLSlots[slotIndex].mEglImage;
685 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700686 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800687 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700688 mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800689 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700690}
Daniel Lameae59d22012-01-22 15:26:27 -0800691
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700692void SurfaceTexture::abandon() {
693 ST_LOGV("abandon");
694 Mutex::Autolock lock(mMutex);
695
696 if (!mAbandoned) {
697 mAbandoned = true;
698 mCurrentTextureBuf.clear();
699
700 // destroy all egl buffers
701 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
702 freeBufferLocked(i);
703 }
704
705 // disconnect from the BufferQueue
706 mBufferQueue->consumerDisconnect();
707 mBufferQueue.clear();
708 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700709}
710
Jamie Gennisfa28c352011-09-16 17:30:26 -0700711void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800712 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700713 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800714 mBufferQueue->setConsumerName(name);
715}
716
717status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
718 Mutex::Autolock lock(mMutex);
719 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
720}
721
722status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
723 Mutex::Autolock lock(mMutex);
724 return mBufferQueue->setConsumerUsageBits(usage);
725}
726
727status_t SurfaceTexture::setTransformHint(uint32_t hint) {
728 Mutex::Autolock lock(mMutex);
729 return mBufferQueue->setTransformHint(hint);
730}
731
732// Used for refactoring BufferQueue from SurfaceTexture
733// Should not be in final interface once users of SurfaceTexture are clean up.
734status_t SurfaceTexture::setSynchronousMode(bool enabled) {
735 Mutex::Autolock lock(mMutex);
736 return mBufferQueue->setSynchronousMode(enabled);
737}
738
739// Used for refactoring, should not be in final interface
740sp<BufferQueue> SurfaceTexture::getBufferQueue() const {
741 Mutex::Autolock lock(mMutex);
742 return mBufferQueue;
743}
744
745// Used for refactoring, should not be in final interface
Daniel Lamb2675792012-02-23 14:35:13 -0800746status_t SurfaceTexture::connect(int api,
747 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
748 Mutex::Autolock lock(mMutex);
749 return mBufferQueue->connect(api, outWidth, outHeight, outTransform);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700750}
751
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700752void SurfaceTexture::onFrameAvailable() {
753 ST_LOGV("onFrameAvailable");
754
755 sp<FrameAvailableListener> listener;
756 { // scope for the lock
757 Mutex::Autolock lock(mMutex);
758 listener = mFrameAvailableListener;
759 }
760
761 if (listener != NULL) {
762 ST_LOGV("actually calling onFrameAvailable");
763 listener->onFrameAvailable();
764 }
765}
766
767void SurfaceTexture::onBuffersReleased() {
768 ST_LOGV("onBuffersReleased");
769
770 Mutex::Autolock lock(mMutex);
771
772 if (mAbandoned) {
773 // Nothing to do if we're already abandoned.
774 return;
775 }
776
777 uint32_t mask = 0;
778 mBufferQueue->getReleasedBuffers(&mask);
779 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
780 if (mask & (1 << i)) {
781 freeBufferLocked(i);
782 }
783 }
784}
785
Mathias Agopian68c77942011-05-09 19:08:33 -0700786void SurfaceTexture::dump(String8& result) const
787{
788 char buffer[1024];
789 dump(result, "", buffer, 1024);
790}
791
792void SurfaceTexture::dump(String8& result, const char* prefix,
793 char* buffer, size_t SIZE) const
794{
795 Mutex::Autolock _l(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700796 snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName,
797 int(mAbandoned));
Mathias Agopian68c77942011-05-09 19:08:33 -0700798 result.append(buffer);
799
Mathias Agopian68c77942011-05-09 19:08:33 -0700800 snprintf(buffer, SIZE,
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700801 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n",
802 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700803 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800804 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700805 );
806 result.append(buffer);
807
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700808 if (!mAbandoned) {
809 mBufferQueue->dump(result, prefix, buffer, SIZE);
810 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700811}
812
Jamie Gennisf238e282011-01-09 16:33:17 -0800813static void mtxMul(float out[16], const float a[16], const float b[16]) {
814 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
815 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
816 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
817 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
818
819 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
820 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
821 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
822 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
823
824 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
825 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
826 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
827 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
828
829 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
830 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
831 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
832 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
833}
834
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800835}; // namespace android