blob: c999080ccdd493706644eea5002a1c3226a3cfc7 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise70d8b42011-01-09 13:24:09 -080019//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080020
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
28
Jamie Gennis9fea3422012-08-07 18:03:04 -070029#include <hardware/hardware.h>
30
Mathias Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/IGraphicBufferAlloc.h>
32#include <gui/ISurfaceComposer.h>
33#include <gui/SurfaceComposerClient.h>
34#include <gui/SurfaceTexture.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080035
Mathias Agopian90ac7992012-02-25 18:48:35 -080036#include <private/gui/ComposerService.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080037
38#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070039#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080040#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080041
Jamie Gennis01dbf552012-09-06 14:54:19 -070042// This compile option makes SurfaceTexture use the
43// EGL_ANDROID_native_fence_sync extension to create Android native fences to
44// signal when all GLES reads for a given buffer have completed. It is not
45// compatible with using the EGL_KHR_fence_sync extension for the same
46// purpose.
47#ifdef USE_NATIVE_FENCE_SYNC
48#ifdef USE_FENCE_SYNC
49#error "USE_NATIVE_FENCE_SYNC and USE_FENCE_SYNC are incompatible"
50#endif
51const bool useNativeFenceSync = true;
52#else
53const bool useNativeFenceSync = false;
54#endif
55
Jamie Gennis86edf4f2011-11-14 14:51:01 -080056// This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension
57// to synchronize access to the buffers. It will cause dequeueBuffer to stall,
58// waiting for the GL reads for the buffer being dequeued to complete before
59// allowing the buffer to be dequeued.
60#ifdef USE_FENCE_SYNC
61#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
62#error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible"
63#endif
64#endif
65
Jamie Gennisfa28c352011-09-16 17:30:26 -070066// Macros for including the SurfaceTexture name in log messages
Steve Block6807e592011-10-20 11:56:00 +010067#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000068#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000069#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000070#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000071#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070072
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080073namespace android {
74
Jamie Gennisf238e282011-01-09 16:33:17 -080075// Transform matrices
76static float mtxIdentity[16] = {
77 1, 0, 0, 0,
78 0, 1, 0, 0,
79 0, 0, 1, 0,
80 0, 0, 0, 1,
81};
82static float mtxFlipH[16] = {
83 -1, 0, 0, 0,
84 0, 1, 0, 0,
85 0, 0, 1, 0,
86 1, 0, 0, 1,
87};
88static float mtxFlipV[16] = {
89 1, 0, 0, 0,
90 0, -1, 0, 0,
91 0, 0, 1, 0,
92 0, 1, 0, 1,
93};
94static float mtxRot90[16] = {
95 0, 1, 0, 0,
96 -1, 0, 0, 0,
97 0, 0, 1, 0,
98 1, 0, 0, 1,
99};
100static float mtxRot180[16] = {
101 -1, 0, 0, 0,
102 0, -1, 0, 0,
103 0, 0, 1, 0,
104 1, 1, 0, 1,
105};
106static float mtxRot270[16] = {
107 0, -1, 0, 0,
108 1, 0, 0, 0,
109 0, 0, 1, 0,
110 0, 1, 0, 1,
111};
112
113static void mtxMul(float out[16], const float a[16], const float b[16]);
114
Jamie Gennisfa28c352011-09-16 17:30:26 -0700115
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700116SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Daniel Lamb2675792012-02-23 14:35:13 -0800117 GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) :
Jamie Gennis9fea3422012-08-07 18:03:04 -0700118 ConsumerBase(bufferQueue == 0 ? new BufferQueue(allowSynchronousMode) : bufferQueue),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800119 mCurrentTransform(0),
120 mCurrentTimestamp(0),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700121 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700122 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800123#ifdef USE_FENCE_SYNC
124 mUseFenceSync(useFenceSync),
125#else
126 mUseFenceSync(false),
127#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800128 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700129 mEglDisplay(EGL_NO_DISPLAY),
130 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700131 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
132 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800133{
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700134 ST_LOGV("SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800135
Jamie Gennisfa28c352011-09-16 17:30:26 -0700136 memcpy(mCurrentTransformMatrix, mtxIdentity,
137 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700138
Jamie Gennis9fea3422012-08-07 18:03:04 -0700139 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800140}
141
Jamie Gennis31a353d2012-08-24 17:25:13 -0700142status_t SurfaceTexture::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700143 Mutex::Autolock lock(mMutex);
Jamie Gennis31a353d2012-08-24 17:25:13 -0700144 return mBufferQueue->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700145}
146
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800147
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700148status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
149{
Daniel Lamb2675792012-02-23 14:35:13 -0800150 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700151 mDefaultWidth = w;
152 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800153 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700154}
155
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800156status_t SurfaceTexture::updateTexImage() {
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700157 return SurfaceTexture::updateTexImage(NULL);
158}
159
Jamie Gennis9fea3422012-08-07 18:03:04 -0700160status_t SurfaceTexture::acquireBufferLocked(BufferQueue::BufferItem *item) {
161 status_t err = ConsumerBase::acquireBufferLocked(item);
162 if (err != NO_ERROR) {
163 return err;
164 }
165
166 int slot = item->mBuf;
167 if (item->mGraphicBuffer != NULL) {
168 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
169 eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage);
170 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
171 }
172 }
173
174 // Update the GL texture object. We may have to do this even when
175 // item.mGraphicBuffer == NULL, if we destroyed the EGLImage when
176 // detaching from a context but the buffer has not been re-allocated.
177 if (mEglSlots[slot].mEglImage == EGL_NO_IMAGE_KHR) {
178 EGLImageKHR image = createImage(mEglDisplay, mSlots[slot].mGraphicBuffer);
179 if (image == EGL_NO_IMAGE_KHR) {
180 return UNKNOWN_ERROR;
181 }
182 mEglSlots[slot].mEglImage = image;
183 }
184
185 return NO_ERROR;
186}
187
188status_t SurfaceTexture::releaseBufferLocked(int buf, EGLDisplay display,
Jamie Gennisb2725412012-09-05 20:09:05 -0700189 EGLSyncKHR eglFence) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700190 status_t err = ConsumerBase::releaseBufferLocked(buf, mEglDisplay,
Jamie Gennisb2725412012-09-05 20:09:05 -0700191 eglFence);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700192
193 mEglSlots[mCurrentTexture].mEglFence = EGL_NO_SYNC_KHR;
194
195 return err;
196}
197
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700198status_t SurfaceTexture::updateTexImage(BufferRejecter* rejecter) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800199 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700200 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800201 Mutex::Autolock lock(mMutex);
202
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700203 status_t err = NO_ERROR;
204
Mathias Agopiane47498f2011-08-08 19:35:15 -0700205 if (mAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700206 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700207 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700208 }
209
Jamie Gennis74bed552012-03-28 19:05:54 -0700210 if (!mAttached) {
211 ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL "
212 "ES context");
213 return INVALID_OPERATION;
214 }
215
Jamie Gennisce561372012-03-19 18:33:05 -0700216 EGLDisplay dpy = eglGetCurrentDisplay();
217 EGLContext ctx = eglGetCurrentContext();
218
Jamie Gennis74bed552012-03-28 19:05:54 -0700219 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
220 dpy == EGL_NO_DISPLAY) {
Jamie Gennisce561372012-03-19 18:33:05 -0700221 ST_LOGE("updateTexImage: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700222 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700223 }
224
Jamie Gennis74bed552012-03-28 19:05:54 -0700225 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
226 ctx == EGL_NO_CONTEXT) {
Jamie Gennisce561372012-03-19 18:33:05 -0700227 ST_LOGE("updateTexImage: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700228 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700229 }
230
231 mEglDisplay = dpy;
232 mEglContext = ctx;
233
Daniel Lamb2675792012-02-23 14:35:13 -0800234 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800235
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700236 // In asynchronous mode the list is guaranteed to be one buffer
237 // deep, while in synchronous mode we use the oldest buffer.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700238 err = acquireBufferLocked(&item);
Daniel Lamfbcda932012-04-09 22:51:52 -0700239 if (err == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800240 int buf = item.mBuf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700241
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700242 // we call the rejecter here, in case the caller has a reason to
243 // not accept this buffer. this is used by SurfaceFlinger to
244 // reject buffers which have the wrong size
Jamie Gennis9fea3422012-08-07 18:03:04 -0700245 if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
Jamie Gennisb2725412012-09-05 20:09:05 -0700246 releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR);
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700247 glBindTexture(mTexTarget, mTexName);
248 return NO_ERROR;
249 }
250
Jamie Gennis9fea3422012-08-07 18:03:04 -0700251 GLint error;
252 while ((error = glGetError()) != GL_NO_ERROR) {
253 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
254 }
255
256 EGLImageKHR image = mEglSlots[buf].mEglImage;
257 glBindTexture(mTexTarget, mTexName);
258 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
259
260 while ((error = glGetError()) != GL_NO_ERROR) {
261 ST_LOGE("updateTexImage: error binding external texture image %p "
262 "(slot %d): %#04x", image, buf, error);
263 err = UNKNOWN_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800264 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800265
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700266 if (err == NO_ERROR) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700267 err = syncForReleaseLocked(dpy);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800268 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700269
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700270 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700271 // Release the buffer we just acquired. It's not safe to
272 // release the old buffer, so instead we just drop the new frame.
Jamie Gennisb2725412012-09-05 20:09:05 -0700273 releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700274 return err;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800275 }
276
277 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
278 mCurrentTexture,
279 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Jamie Gennis9fea3422012-08-07 18:03:04 -0700280 buf, mSlots[buf].mGraphicBuffer->handle);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700281
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700282 // release old buffer
Jamie Gennis74bed552012-03-28 19:05:54 -0700283 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700284 status_t status = releaseBufferLocked(mCurrentTexture, dpy,
Jamie Gennisb2725412012-09-05 20:09:05 -0700285 mEglSlots[mCurrentTexture].mEglFence);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700286 if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) {
287 ST_LOGE("updateTexImage: failed to release buffer: %s (%d)",
288 strerror(-status), status);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700289 err = status;
290 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700291 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700292
Jamie Gennis9a78c902011-01-12 18:30:40 -0800293 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700294 mCurrentTexture = buf;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700295 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Daniel Lameae59d22012-01-22 15:26:27 -0800296 mCurrentCrop = item.mCrop;
297 mCurrentTransform = item.mTransform;
298 mCurrentScalingMode = item.mScalingMode;
299 mCurrentTimestamp = item.mTimestamp;
Jesse Halldc5b4852012-06-29 15:21:18 -0700300 mCurrentFence = item.mFence;
Jamie Gennis736aa952011-06-12 17:03:06 -0700301 computeCurrentTransformMatrix();
Daniel Lamfbcda932012-04-09 22:51:52 -0700302 } else {
303 if (err < 0) {
Jamie Gennisd69097f2012-08-30 13:28:23 -0700304 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
305 strerror(-err), err);
306 return err;
Daniel Lamfbcda932012-04-09 22:51:52 -0700307 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700308 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700309 glBindTexture(mTexTarget, mTexName);
Daniel Lamfbcda932012-04-09 22:51:52 -0700310 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800311 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700312
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700313 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800314}
315
Jesse Hallef194142012-06-14 14:45:17 -0700316void SurfaceTexture::setReleaseFence(int fenceFd) {
Jamie Gennis3d1d09c2012-08-08 15:39:55 -0700317 sp<Fence> fence(new Fence(fenceFd));
Jamie Gennis45cb2ba2012-08-06 17:10:57 -0700318 if (fenceFd == -1 || mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT)
Jesse Hallef194142012-06-14 14:45:17 -0700319 return;
Jamie Gennisb2725412012-09-05 20:09:05 -0700320 status_t err = addReleaseFence(mCurrentTexture, fence);
321 if (err != OK) {
322 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
323 strerror(-err), err);
Jesse Hallef194142012-06-14 14:45:17 -0700324 }
325}
326
Jamie Gennis74bed552012-03-28 19:05:54 -0700327status_t SurfaceTexture::detachFromContext() {
328 ATRACE_CALL();
329 ST_LOGV("detachFromContext");
330 Mutex::Autolock lock(mMutex);
331
332 if (mAbandoned) {
333 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
334 return NO_INIT;
335 }
336
337 if (!mAttached) {
338 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
339 "context");
340 return INVALID_OPERATION;
341 }
342
343 EGLDisplay dpy = eglGetCurrentDisplay();
344 EGLContext ctx = eglGetCurrentContext();
345
346 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
347 ST_LOGE("detachFromContext: invalid current EGLDisplay");
348 return INVALID_OPERATION;
349 }
350
351 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
352 ST_LOGE("detachFromContext: invalid current EGLContext");
353 return INVALID_OPERATION;
354 }
355
356 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
357 status_t err = syncForReleaseLocked(dpy);
358 if (err != OK) {
359 return err;
360 }
361
362 glDeleteTextures(1, &mTexName);
363 }
364
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700365 // Because we're giving up the EGLDisplay we need to free all the EGLImages
366 // that are associated with it. They'll be recreated when the
367 // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a
368 // new EGLDisplay).
369 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700370 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700371 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700372 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700373 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700374 }
375 }
376
Jamie Gennis74bed552012-03-28 19:05:54 -0700377 mEglDisplay = EGL_NO_DISPLAY;
378 mEglContext = EGL_NO_CONTEXT;
379 mAttached = false;
380
381 return OK;
382}
383
384status_t SurfaceTexture::attachToContext(GLuint tex) {
385 ATRACE_CALL();
386 ST_LOGV("attachToContext");
387 Mutex::Autolock lock(mMutex);
388
389 if (mAbandoned) {
390 ST_LOGE("attachToContext: abandoned SurfaceTexture");
391 return NO_INIT;
392 }
393
394 if (mAttached) {
395 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
396 "context");
397 return INVALID_OPERATION;
398 }
399
400 EGLDisplay dpy = eglGetCurrentDisplay();
401 EGLContext ctx = eglGetCurrentContext();
402
403 if (dpy == EGL_NO_DISPLAY) {
404 ST_LOGE("attachToContext: invalid current EGLDisplay");
405 return INVALID_OPERATION;
406 }
407
408 if (ctx == EGL_NO_CONTEXT) {
409 ST_LOGE("attachToContext: invalid current EGLContext");
410 return INVALID_OPERATION;
411 }
412
413 // We need to bind the texture regardless of whether there's a current
414 // buffer.
415 glBindTexture(mTexTarget, tex);
416
417 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700418 // The EGLImageKHR that was associated with the slot was destroyed when
419 // the SurfaceTexture was detached from the old context, so we need to
420 // recreate it here.
421 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
422 if (image == EGL_NO_IMAGE_KHR) {
423 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700424 }
425
426 // Attach the current buffer to the GL texture.
427 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
428
429 GLint error;
430 status_t err = OK;
431 while ((error = glGetError()) != GL_NO_ERROR) {
432 ST_LOGE("attachToContext: error binding external texture image %p "
433 "(slot %d): %#04x", image, mCurrentTexture, error);
434 err = UNKNOWN_ERROR;
435 }
436
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700437 // We destroy the EGLImageKHR here because the current buffer may no
438 // longer be associated with one of the buffer slots, so we have
439 // nowhere to to store it. If the buffer is still associated with a
440 // slot then another EGLImageKHR will be created next time that buffer
441 // gets acquired in updateTexImage.
442 eglDestroyImageKHR(dpy, image);
Jamie Gennis74bed552012-03-28 19:05:54 -0700443
444 if (err != OK) {
445 return err;
446 }
447 }
448
449 mEglDisplay = dpy;
450 mEglContext = ctx;
451 mTexName = tex;
452 mAttached = true;
453
454 return OK;
455}
456
457status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
458 ST_LOGV("syncForReleaseLocked");
459
Jamie Gennis01dbf552012-09-06 14:54:19 -0700460 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
461 if (useNativeFenceSync) {
462 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
463 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
464 if (sync == EGL_NO_SYNC_KHR) {
465 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
466 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700467 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700468 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700469 glFlush();
470 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
471 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
472 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
473 "fd: %#x", eglGetError());
474 return UNKNOWN_ERROR;
475 }
476 sp<Fence> fence(new Fence(fenceFd));
477 status_t err = addReleaseFence(mCurrentTexture, fence);
478 if (err != OK) {
479 ST_LOGE("syncForReleaseLocked: error adding release fence: "
480 "%s (%d)", strerror(-err), err);
481 return err;
482 }
483 } else if (mUseFenceSync) {
484 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
485 if (fence != EGL_NO_SYNC_KHR) {
486 // There is already a fence for the current slot. We need to
487 // wait on that before replacing it with another fence to
488 // ensure that all outstanding buffer accesses have completed
489 // before the producer accesses it.
490 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
491 if (result == EGL_FALSE) {
492 ST_LOGE("syncForReleaseLocked: error waiting for previous "
493 "fence: %#x", eglGetError());
494 return UNKNOWN_ERROR;
495 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
496 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
497 "fence");
498 return TIMED_OUT;
499 }
500 eglDestroySyncKHR(dpy, fence);
501 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700502
Jamie Gennis01dbf552012-09-06 14:54:19 -0700503 // Create a fence for the outstanding accesses in the current
504 // OpenGL ES context.
505 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
506 if (fence == EGL_NO_SYNC_KHR) {
507 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
508 eglGetError());
509 return UNKNOWN_ERROR;
510 }
511 glFlush();
512 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700513 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700514 }
515
516 return OK;
517}
518
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700519bool SurfaceTexture::isExternalFormat(uint32_t format)
520{
521 switch (format) {
522 // supported YUV formats
523 case HAL_PIXEL_FORMAT_YV12:
524 // Legacy/deprecated YUV formats
525 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
526 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
527 case HAL_PIXEL_FORMAT_YCbCr_422_I:
528 return true;
529 }
530
531 // Any OEM format needs to be considered
532 if (format>=0x100 && format<=0x1FF)
533 return true;
534
535 return false;
536}
537
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700538GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700539 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700540}
541
Jamie Gennisf238e282011-01-09 16:33:17 -0800542void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800543 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700544 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
545}
546
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700547void SurfaceTexture::setFilteringEnabled(bool enabled) {
548 Mutex::Autolock lock(mMutex);
549 bool needsRecompute = mFilteringEnabled != enabled;
550 mFilteringEnabled = enabled;
551 if (needsRecompute) {
552 computeCurrentTransformMatrix();
553 }
554}
555
Jamie Gennis736aa952011-06-12 17:03:06 -0700556void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700557 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800558
Jamie Gennisa214c642011-01-14 13:53:31 -0800559 float xform[16];
560 for (int i = 0; i < 16; i++) {
561 xform[i] = mtxIdentity[i];
562 }
563 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
564 float result[16];
565 mtxMul(result, xform, mtxFlipH);
566 for (int i = 0; i < 16; i++) {
567 xform[i] = result[i];
568 }
569 }
570 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
571 float result[16];
572 mtxMul(result, xform, mtxFlipV);
573 for (int i = 0; i < 16; i++) {
574 xform[i] = result[i];
575 }
576 }
577 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
578 float result[16];
579 mtxMul(result, xform, mtxRot90);
580 for (int i = 0; i < 16; i++) {
581 xform[i] = result[i];
582 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800583 }
584
Daniel Lameae59d22012-01-22 15:26:27 -0800585 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisd72f2332012-05-07 13:50:11 -0700586 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700587 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
588 float bufferWidth = buf->getWidth();
589 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700590 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700591 float shrinkAmount = 0.0f;
592 if (mFilteringEnabled) {
593 // In order to prevent bilinear sampling beyond the edge of the
594 // crop rectangle we may need to shrink it by 2 texels in each
595 // dimension. Normally this would just need to take 1/2 a texel
596 // off each end, but because the chroma channels of YUV420 images
597 // are subsampled we may need to shrink the crop region by a whole
598 // texel on each side.
599 switch (buf->getPixelFormat()) {
600 case PIXEL_FORMAT_RGBA_8888:
601 case PIXEL_FORMAT_RGBX_8888:
602 case PIXEL_FORMAT_RGB_888:
603 case PIXEL_FORMAT_RGB_565:
604 case PIXEL_FORMAT_BGRA_8888:
605 case PIXEL_FORMAT_RGBA_5551:
606 case PIXEL_FORMAT_RGBA_4444:
607 // We know there's no subsampling of any channels, so we
608 // only need to shrink by a half a pixel.
609 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700610 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700611
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700612 default:
613 // If we don't recognize the format, we must assume the
614 // worst case (that we care about), which is YUV420.
615 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700616 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700617 }
618 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700619
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700620 // Only shrink the dimensions that are not the size of the buffer.
621 if (cropRect.width() < bufferWidth) {
622 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
623 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
624 bufferWidth;
625 }
626 if (cropRect.height() < bufferHeight) {
627 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
628 bufferHeight;
629 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
630 bufferHeight;
631 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800632 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800633 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800634 sx, 0, 0, 0,
635 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800636 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800637 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800638 };
639
Jamie Gennisa214c642011-01-14 13:53:31 -0800640 float mtxBeforeFlipV[16];
641 mtxMul(mtxBeforeFlipV, crop, xform);
642
643 // SurfaceFlinger expects the top of its window textures to be at a Y
644 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
645 // want to expose this to applications, however, so we must add an
646 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700647 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800648}
649
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800650nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700651 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800652 Mutex::Autolock lock(mMutex);
653 return mCurrentTimestamp;
654}
655
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800656EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
657 const sp<GraphicBuffer>& graphicBuffer) {
658 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
659 EGLint attrs[] = {
660 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
661 EGL_NONE,
662 };
663 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
664 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700665 if (image == EGL_NO_IMAGE_KHR) {
666 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700667 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800668 }
669 return image;
670}
671
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700672sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
673 Mutex::Autolock lock(mMutex);
674 return mCurrentTextureBuf;
675}
676
677Rect SurfaceTexture::getCurrentCrop() const {
678 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700679
680 Rect outCrop = mCurrentCrop;
681 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
682 int32_t newWidth = mCurrentCrop.width();
683 int32_t newHeight = mCurrentCrop.height();
684
685 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
686 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
687 ST_LOGV("too wide: newWidth = %d", newWidth);
688 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
689 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
690 ST_LOGV("too tall: newHeight = %d", newHeight);
691 }
692
693 // The crop is too wide
694 if (newWidth < mCurrentCrop.width()) {
695 int32_t dw = (newWidth - mCurrentCrop.width())/2;
696 outCrop.left -=dw;
697 outCrop.right += dw;
698 // The crop is too tall
699 } else if (newHeight < mCurrentCrop.height()) {
700 int32_t dh = (newHeight - mCurrentCrop.height())/2;
701 outCrop.top -= dh;
702 outCrop.bottom += dh;
703 }
704
705 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
706 outCrop.left, outCrop.top,
707 outCrop.right,outCrop.bottom);
708 }
709
Daniel Lam016c8cb2012-04-03 15:54:58 -0700710 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700711}
712
713uint32_t SurfaceTexture::getCurrentTransform() const {
714 Mutex::Autolock lock(mMutex);
715 return mCurrentTransform;
716}
717
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700718uint32_t SurfaceTexture::getCurrentScalingMode() const {
719 Mutex::Autolock lock(mMutex);
720 return mCurrentScalingMode;
721}
722
Jesse Halldc5b4852012-06-29 15:21:18 -0700723sp<Fence> SurfaceTexture::getCurrentFence() const {
724 Mutex::Autolock lock(mMutex);
725 return mCurrentFence;
726}
727
Jamie Gennis59769462011-11-19 18:04:43 -0800728bool SurfaceTexture::isSynchronousMode() const {
729 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800730 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800731}
732
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700733void SurfaceTexture::freeBufferLocked(int slotIndex) {
734 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700735 if (slotIndex == mCurrentTexture) {
736 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
737 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700738 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700739 if (img != EGL_NO_IMAGE_KHR) {
740 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
741 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800742 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700743 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
744 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700745}
Daniel Lameae59d22012-01-22 15:26:27 -0800746
Jamie Gennis9fea3422012-08-07 18:03:04 -0700747void SurfaceTexture::abandonLocked() {
748 ST_LOGV("abandonLocked");
749 mCurrentTextureBuf.clear();
750 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700751}
752
Jamie Gennisfa28c352011-09-16 17:30:26 -0700753void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800754 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700755 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800756 mBufferQueue->setConsumerName(name);
757}
758
759status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
760 Mutex::Autolock lock(mMutex);
761 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
762}
763
764status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
765 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700766 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800767 return mBufferQueue->setConsumerUsageBits(usage);
768}
769
770status_t SurfaceTexture::setTransformHint(uint32_t hint) {
771 Mutex::Autolock lock(mMutex);
772 return mBufferQueue->setTransformHint(hint);
773}
774
775// Used for refactoring BufferQueue from SurfaceTexture
776// Should not be in final interface once users of SurfaceTexture are clean up.
777status_t SurfaceTexture::setSynchronousMode(bool enabled) {
778 Mutex::Autolock lock(mMutex);
779 return mBufferQueue->setSynchronousMode(enabled);
780}
781
Jamie Gennis9fea3422012-08-07 18:03:04 -0700782void SurfaceTexture::dumpLocked(String8& result, const char* prefix,
783 char* buffer, size_t size) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700784{
Jamie Gennis9fea3422012-08-07 18:03:04 -0700785 snprintf(buffer, size,
786 "%smTexName=%d mCurrentTexture=%d\n"
787 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
788 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
789 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
790 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700791 result.append(buffer);
792
Jamie Gennis9fea3422012-08-07 18:03:04 -0700793 ConsumerBase::dumpLocked(result, prefix, buffer, size);
Mathias Agopian68c77942011-05-09 19:08:33 -0700794}
795
Jamie Gennisf238e282011-01-09 16:33:17 -0800796static void mtxMul(float out[16], const float a[16], const float b[16]) {
797 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
798 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
799 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
800 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
801
802 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
803 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
804 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
805 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
806
807 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
808 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
809 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
810 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
811
812 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
813 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
814 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
815 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
816}
817
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800818}; // namespace android