blob: 4fe2ccae8d4ed62cb54c6c28a5802c617a368799 [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);
171 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700172}
173
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800174status_t SurfaceTexture::updateTexImage() {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800175 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700176 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800177 Mutex::Autolock lock(mMutex);
178
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700179 status_t err = NO_ERROR;
180
Mathias Agopiane47498f2011-08-08 19:35:15 -0700181 if (mAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700182 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700183 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700184 }
185
Jamie Gennis74bed552012-03-28 19:05:54 -0700186 if (!mAttached) {
187 ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL "
188 "ES context");
189 return INVALID_OPERATION;
190 }
191
Jamie Gennisce561372012-03-19 18:33:05 -0700192 EGLDisplay dpy = eglGetCurrentDisplay();
193 EGLContext ctx = eglGetCurrentContext();
194
Jamie Gennis74bed552012-03-28 19:05:54 -0700195 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
196 dpy == EGL_NO_DISPLAY) {
Jamie Gennisce561372012-03-19 18:33:05 -0700197 ST_LOGE("updateTexImage: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700198 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700199 }
200
Jamie Gennis74bed552012-03-28 19:05:54 -0700201 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
202 ctx == EGL_NO_CONTEXT) {
Jamie Gennisce561372012-03-19 18:33:05 -0700203 ST_LOGE("updateTexImage: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700204 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700205 }
206
207 mEglDisplay = dpy;
208 mEglContext = ctx;
209
Daniel Lamb2675792012-02-23 14:35:13 -0800210 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800211
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700212 // In asynchronous mode the list is guaranteed to be one buffer
213 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lamfbcda932012-04-09 22:51:52 -0700214 err = mBufferQueue->acquireBuffer(&item);
215 if (err == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800216 int buf = item.mBuf;
217 // This buffer was newly allocated, so we need to clean up on our side
218 if (item.mGraphicBuffer != NULL) {
219 mEGLSlots[buf].mGraphicBuffer = 0;
220 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700221 eglDestroyImageKHR(dpy, mEGLSlots[buf].mEglImage);
Daniel Lameae59d22012-01-22 15:26:27 -0800222 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800223 }
224 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
225 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700226
Jamie Gennisf238e282011-01-09 16:33:17 -0800227 // Update the GL texture object.
Daniel Lameae59d22012-01-22 15:26:27 -0800228 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800229 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800230 if (item.mGraphicBuffer == 0) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700231 ST_LOGE("updateTexImage: buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700232 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700233 }
Daniel Lameae59d22012-01-22 15:26:27 -0800234 image = createImage(dpy, item.mGraphicBuffer);
235 mEGLSlots[buf].mEglImage = image;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700236 if (image == EGL_NO_IMAGE_KHR) {
237 // NOTE: if dpy was invalid, createImage() is guaranteed to
238 // fail. so we'd end up here.
Jamie Gennis74bed552012-03-28 19:05:54 -0700239 return UNKNOWN_ERROR;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700240 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800241 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800242
243 GLint error;
244 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700245 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800246 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700247
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700248 glBindTexture(mTexTarget, mTexName);
249 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700250
Jamie Gennis0eb88512011-01-26 11:52:02 -0800251 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700252 ST_LOGE("updateTexImage: error binding external texture image %p "
253 "(slot %d): %#04x", image, buf, error);
254 err = UNKNOWN_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800255 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800256
Jamie Gennis74bed552012-03-28 19:05:54 -0700257 if (err == OK) {
258 err = syncForReleaseLocked(dpy);
259 }
260
261 if (err != OK) {
262 // Release the buffer we just acquired. It's not safe to
263 // release the old buffer, so instead we just drop the new frame.
264 mBufferQueue->releaseBuffer(buf, dpy, mEGLSlots[buf].mFence);
265 mEGLSlots[buf].mFence = EGL_NO_SYNC_KHR;
266 return err;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800267 }
268
269 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
270 mCurrentTexture,
271 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800272 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700273
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700274 // release old buffer
Jamie Gennis74bed552012-03-28 19:05:54 -0700275 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700276 status_t status = mBufferQueue->releaseBuffer(mCurrentTexture, dpy, mEGLSlots[mCurrentTexture].mFence);
277
Jamie Gennis74bed552012-03-28 19:05:54 -0700278 mEGLSlots[mCurrentTexture].mFence = EGL_NO_SYNC_KHR;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700279 if (status == BufferQueue::STALE_BUFFER_SLOT) {
280 freeBufferLocked(mCurrentTexture);
281 } else if (status != OK) {
282 ST_LOGE("updateTexImage: released invalid buffer");
283 err = status;
284 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700285 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700286
Jamie Gennis9a78c902011-01-12 18:30:40 -0800287 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700288 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800289 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
290 mCurrentCrop = item.mCrop;
291 mCurrentTransform = item.mTransform;
292 mCurrentScalingMode = item.mScalingMode;
293 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700294 computeCurrentTransformMatrix();
Daniel Lamfbcda932012-04-09 22:51:52 -0700295 } else {
296 if (err < 0) {
297 ALOGE("updateTexImage failed on acquire %d", err);
298 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700299 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700300 glBindTexture(mTexTarget, mTexName);
Daniel Lamfbcda932012-04-09 22:51:52 -0700301 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800302 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700303
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700304 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800305}
306
Jamie Gennis74bed552012-03-28 19:05:54 -0700307status_t SurfaceTexture::detachFromContext() {
308 ATRACE_CALL();
309 ST_LOGV("detachFromContext");
310 Mutex::Autolock lock(mMutex);
311
312 if (mAbandoned) {
313 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
314 return NO_INIT;
315 }
316
317 if (!mAttached) {
318 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
319 "context");
320 return INVALID_OPERATION;
321 }
322
323 EGLDisplay dpy = eglGetCurrentDisplay();
324 EGLContext ctx = eglGetCurrentContext();
325
326 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
327 ST_LOGE("detachFromContext: invalid current EGLDisplay");
328 return INVALID_OPERATION;
329 }
330
331 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
332 ST_LOGE("detachFromContext: invalid current EGLContext");
333 return INVALID_OPERATION;
334 }
335
336 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
337 status_t err = syncForReleaseLocked(dpy);
338 if (err != OK) {
339 return err;
340 }
341
342 glDeleteTextures(1, &mTexName);
343 }
344
345 mEglDisplay = EGL_NO_DISPLAY;
346 mEglContext = EGL_NO_CONTEXT;
347 mAttached = false;
348
349 return OK;
350}
351
352status_t SurfaceTexture::attachToContext(GLuint tex) {
353 ATRACE_CALL();
354 ST_LOGV("attachToContext");
355 Mutex::Autolock lock(mMutex);
356
357 if (mAbandoned) {
358 ST_LOGE("attachToContext: abandoned SurfaceTexture");
359 return NO_INIT;
360 }
361
362 if (mAttached) {
363 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
364 "context");
365 return INVALID_OPERATION;
366 }
367
368 EGLDisplay dpy = eglGetCurrentDisplay();
369 EGLContext ctx = eglGetCurrentContext();
370
371 if (dpy == EGL_NO_DISPLAY) {
372 ST_LOGE("attachToContext: invalid current EGLDisplay");
373 return INVALID_OPERATION;
374 }
375
376 if (ctx == EGL_NO_CONTEXT) {
377 ST_LOGE("attachToContext: invalid current EGLContext");
378 return INVALID_OPERATION;
379 }
380
381 // We need to bind the texture regardless of whether there's a current
382 // buffer.
383 glBindTexture(mTexTarget, tex);
384
385 if (mCurrentTextureBuf != NULL) {
386 // If the current buffer is no longer associated with a slot, then it
387 // doesn't have an EGLImage. In that case we create one now, but we also
388 // destroy it once we've used it to attach the buffer to the OpenGL ES
389 // texture.
390 bool imageNeedsDestroy = false;
391 EGLImageKHR image = EGL_NO_IMAGE_KHR;
392 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
393 image = mEGLSlots[mCurrentTexture].mEglImage;
394 imageNeedsDestroy = false;
395 } else {
396 image = createImage(dpy, mCurrentTextureBuf);
397 if (image == EGL_NO_IMAGE_KHR) {
398 return UNKNOWN_ERROR;
399 }
400 imageNeedsDestroy = true;
401 }
402
403 // Attach the current buffer to the GL texture.
404 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
405
406 GLint error;
407 status_t err = OK;
408 while ((error = glGetError()) != GL_NO_ERROR) {
409 ST_LOGE("attachToContext: error binding external texture image %p "
410 "(slot %d): %#04x", image, mCurrentTexture, error);
411 err = UNKNOWN_ERROR;
412 }
413
414 if (imageNeedsDestroy) {
415 eglDestroyImageKHR(dpy, image);
416 }
417
418 if (err != OK) {
419 return err;
420 }
421 }
422
423 mEglDisplay = dpy;
424 mEglContext = ctx;
425 mTexName = tex;
426 mAttached = true;
427
428 return OK;
429}
430
431status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
432 ST_LOGV("syncForReleaseLocked");
433
434 if (mUseFenceSync && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
435 EGLSyncKHR fence = mEGLSlots[mCurrentTexture].mFence;
436 if (fence != EGL_NO_SYNC_KHR) {
437 // There is already a fence for the current slot. We need to wait
438 // on that before replacing it with another fence to ensure that all
439 // outstanding buffer accesses have completed before the producer
440 // accesses it.
441 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
442 if (result == EGL_FALSE) {
443 ST_LOGE("syncForReleaseLocked: error waiting for previous "
444 "fence: %#x", eglGetError());
445 return UNKNOWN_ERROR;
446 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
447 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
448 "fence");
449 return TIMED_OUT;
450 }
451 eglDestroySyncKHR(dpy, fence);
452 }
453
454 // Create a fence for the outstanding accesses in the current OpenGL ES
455 // context.
456 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
457 if (fence == EGL_NO_SYNC_KHR) {
458 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
459 eglGetError());
460 return UNKNOWN_ERROR;
461 }
462 glFlush();
463 mEGLSlots[mCurrentTexture].mFence = fence;
464 }
465
466 return OK;
467}
468
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700469bool SurfaceTexture::isExternalFormat(uint32_t format)
470{
471 switch (format) {
472 // supported YUV formats
473 case HAL_PIXEL_FORMAT_YV12:
474 // Legacy/deprecated YUV formats
475 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
476 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
477 case HAL_PIXEL_FORMAT_YCbCr_422_I:
478 return true;
479 }
480
481 // Any OEM format needs to be considered
482 if (format>=0x100 && format<=0x1FF)
483 return true;
484
485 return false;
486}
487
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700488GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700489 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700490}
491
Jamie Gennisf238e282011-01-09 16:33:17 -0800492void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800493 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700494 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
495}
496
497void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700498 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800499
Jamie Gennisa214c642011-01-14 13:53:31 -0800500 float xform[16];
501 for (int i = 0; i < 16; i++) {
502 xform[i] = mtxIdentity[i];
503 }
504 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
505 float result[16];
506 mtxMul(result, xform, mtxFlipH);
507 for (int i = 0; i < 16; i++) {
508 xform[i] = result[i];
509 }
510 }
511 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
512 float result[16];
513 mtxMul(result, xform, mtxFlipV);
514 for (int i = 0; i < 16; i++) {
515 xform[i] = result[i];
516 }
517 }
518 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
519 float result[16];
520 mtxMul(result, xform, mtxRot90);
521 for (int i = 0; i < 16; i++) {
522 xform[i] = result[i];
523 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800524 }
525
Daniel Lameae59d22012-01-22 15:26:27 -0800526 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisa214c642011-01-14 13:53:31 -0800527 float tx, ty, sx, sy;
528 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800529 // In order to prevent bilinear sampling at the of the crop rectangle we
530 // may need to shrink it by 2 texels in each direction. Normally this
531 // would just need to take 1/2 a texel off each end, but because the
532 // chroma channels will likely be subsampled we need to chop off a whole
533 // texel. This will cause artifacts if someone does nearest sampling
534 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
535 // accomodate the bilinear and nearest sampling uses.
536 //
537 // If nearest sampling turns out to be a desirable usage of these
538 // textures then we could add the ability to switch a SurfaceTexture to
539 // nearest-mode. Preferably, however, the image producers (video
540 // decoder, camera, etc.) would simply not use a crop rectangle (or at
541 // least not tell the framework about it) so that the GPU can do the
542 // correct edge behavior.
543 int xshrink = 0, yshrink = 0;
544 if (mCurrentCrop.left > 0) {
545 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
546 xshrink++;
547 } else {
548 tx = 0.0f;
549 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700550 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800551 xshrink++;
552 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700553 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800554 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
555 float(buf->getHeight());
556 yshrink++;
557 } else {
558 ty = 0.0f;
559 }
560 if (mCurrentCrop.top > 0) {
561 yshrink++;
562 }
563 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
564 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
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);
624 return mCurrentCrop;
625}
626
627uint32_t SurfaceTexture::getCurrentTransform() const {
628 Mutex::Autolock lock(mMutex);
629 return mCurrentTransform;
630}
631
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700632uint32_t SurfaceTexture::getCurrentScalingMode() const {
633 Mutex::Autolock lock(mMutex);
634 return mCurrentScalingMode;
635}
636
Jamie Gennis59769462011-11-19 18:04:43 -0800637bool SurfaceTexture::isSynchronousMode() const {
638 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800639 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800640}
641
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700642void SurfaceTexture::freeBufferLocked(int slotIndex) {
643 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
644 mEGLSlots[slotIndex].mGraphicBuffer = 0;
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700645 if (slotIndex == mCurrentTexture) {
646 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
647 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700648 if (mEGLSlots[slotIndex].mEglImage != EGL_NO_IMAGE_KHR) {
649 EGLImageKHR img = mEGLSlots[slotIndex].mEglImage;
650 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700651 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800652 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700653 mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800654 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700655}
Daniel Lameae59d22012-01-22 15:26:27 -0800656
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700657void SurfaceTexture::abandon() {
658 ST_LOGV("abandon");
659 Mutex::Autolock lock(mMutex);
660
661 if (!mAbandoned) {
662 mAbandoned = true;
663 mCurrentTextureBuf.clear();
664
665 // destroy all egl buffers
666 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
667 freeBufferLocked(i);
668 }
669
670 // disconnect from the BufferQueue
671 mBufferQueue->consumerDisconnect();
672 mBufferQueue.clear();
673 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700674}
675
Jamie Gennisfa28c352011-09-16 17:30:26 -0700676void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800677 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700678 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800679 mBufferQueue->setConsumerName(name);
680}
681
682status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
683 Mutex::Autolock lock(mMutex);
684 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
685}
686
687status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
688 Mutex::Autolock lock(mMutex);
689 return mBufferQueue->setConsumerUsageBits(usage);
690}
691
692status_t SurfaceTexture::setTransformHint(uint32_t hint) {
693 Mutex::Autolock lock(mMutex);
694 return mBufferQueue->setTransformHint(hint);
695}
696
697// Used for refactoring BufferQueue from SurfaceTexture
698// Should not be in final interface once users of SurfaceTexture are clean up.
699status_t SurfaceTexture::setSynchronousMode(bool enabled) {
700 Mutex::Autolock lock(mMutex);
701 return mBufferQueue->setSynchronousMode(enabled);
702}
703
704// Used for refactoring, should not be in final interface
705sp<BufferQueue> SurfaceTexture::getBufferQueue() const {
706 Mutex::Autolock lock(mMutex);
707 return mBufferQueue;
708}
709
710// Used for refactoring, should not be in final interface
Daniel Lamb2675792012-02-23 14:35:13 -0800711status_t SurfaceTexture::connect(int api,
712 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
713 Mutex::Autolock lock(mMutex);
714 return mBufferQueue->connect(api, outWidth, outHeight, outTransform);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700715}
716
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700717void SurfaceTexture::onFrameAvailable() {
718 ST_LOGV("onFrameAvailable");
719
720 sp<FrameAvailableListener> listener;
721 { // scope for the lock
722 Mutex::Autolock lock(mMutex);
723 listener = mFrameAvailableListener;
724 }
725
726 if (listener != NULL) {
727 ST_LOGV("actually calling onFrameAvailable");
728 listener->onFrameAvailable();
729 }
730}
731
732void SurfaceTexture::onBuffersReleased() {
733 ST_LOGV("onBuffersReleased");
734
735 Mutex::Autolock lock(mMutex);
736
737 if (mAbandoned) {
738 // Nothing to do if we're already abandoned.
739 return;
740 }
741
742 uint32_t mask = 0;
743 mBufferQueue->getReleasedBuffers(&mask);
744 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
745 if (mask & (1 << i)) {
746 freeBufferLocked(i);
747 }
748 }
749}
750
Mathias Agopian68c77942011-05-09 19:08:33 -0700751void SurfaceTexture::dump(String8& result) const
752{
753 char buffer[1024];
754 dump(result, "", buffer, 1024);
755}
756
757void SurfaceTexture::dump(String8& result, const char* prefix,
758 char* buffer, size_t SIZE) const
759{
760 Mutex::Autolock _l(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700761 snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName,
762 int(mAbandoned));
Mathias Agopian68c77942011-05-09 19:08:33 -0700763 result.append(buffer);
764
Mathias Agopian68c77942011-05-09 19:08:33 -0700765 snprintf(buffer, SIZE,
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700766 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n",
767 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700768 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800769 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700770 );
771 result.append(buffer);
772
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700773 if (!mAbandoned) {
774 mBufferQueue->dump(result, prefix, buffer, SIZE);
775 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700776}
777
Jamie Gennisf238e282011-01-09 16:33:17 -0800778static void mtxMul(float out[16], const float a[16], const float b[16]) {
779 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
780 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
781 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
782 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
783
784 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
785 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
786 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
787 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
788
789 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
790 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
791 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
792 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
793
794 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
795 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
796 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
797 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
798}
799
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800800}; // namespace android