blob: c0b20dfdae4606fb710d4d68900db112af261387 [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 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
Jamie Gennisfa28c352011-09-16 17:30:26 -0700101
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700102SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Daniel Lamb2675792012-02-23 14:35:13 -0800103 GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) :
Jamie Gennis9fea3422012-08-07 18:03:04 -0700104 ConsumerBase(bufferQueue == 0 ? new BufferQueue(allowSynchronousMode) : bufferQueue),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800105 mCurrentTransform(0),
106 mCurrentTimestamp(0),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700107 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700108 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800109#ifdef USE_FENCE_SYNC
110 mUseFenceSync(useFenceSync),
111#else
112 mUseFenceSync(false),
113#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800114 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700115 mEglDisplay(EGL_NO_DISPLAY),
116 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700117 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
118 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800119{
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700120 ST_LOGV("SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800121
Jamie Gennisfa28c352011-09-16 17:30:26 -0700122 memcpy(mCurrentTransformMatrix, mtxIdentity,
123 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700124
Jamie Gennis9fea3422012-08-07 18:03:04 -0700125 mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800126}
127
Mathias Agopian80727112011-05-02 19:51:12 -0700128status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
129 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800130 return mBufferQueue->setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700131}
132
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800133
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700134status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
135{
Daniel Lamb2675792012-02-23 14:35:13 -0800136 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700137 mDefaultWidth = w;
138 mDefaultHeight = h;
Daniel Lamb2675792012-02-23 14:35:13 -0800139 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700140}
141
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800142status_t SurfaceTexture::updateTexImage() {
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700143 return SurfaceTexture::updateTexImage(NULL);
144}
145
Jamie Gennis9fea3422012-08-07 18:03:04 -0700146status_t SurfaceTexture::acquireBufferLocked(BufferQueue::BufferItem *item) {
147 status_t err = ConsumerBase::acquireBufferLocked(item);
148 if (err != NO_ERROR) {
149 return err;
150 }
151
152 int slot = item->mBuf;
153 if (item->mGraphicBuffer != NULL) {
154 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
155 eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage);
156 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
157 }
158 }
159
160 // Update the GL texture object. We may have to do this even when
161 // item.mGraphicBuffer == NULL, if we destroyed the EGLImage when
162 // detaching from a context but the buffer has not been re-allocated.
163 if (mEglSlots[slot].mEglImage == EGL_NO_IMAGE_KHR) {
164 EGLImageKHR image = createImage(mEglDisplay, mSlots[slot].mGraphicBuffer);
165 if (image == EGL_NO_IMAGE_KHR) {
166 return UNKNOWN_ERROR;
167 }
168 mEglSlots[slot].mEglImage = image;
169 }
170
171 return NO_ERROR;
172}
173
174status_t SurfaceTexture::releaseBufferLocked(int buf, EGLDisplay display,
175 EGLSyncKHR eglFence, const sp<Fence>& fence) {
176 status_t err = ConsumerBase::releaseBufferLocked(buf, mEglDisplay,
177 eglFence, fence);
178
179 mEglSlots[mCurrentTexture].mEglFence = EGL_NO_SYNC_KHR;
180
181 return err;
182}
183
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700184status_t SurfaceTexture::updateTexImage(BufferRejecter* rejecter) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800185 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700186 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800187 Mutex::Autolock lock(mMutex);
188
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700189 status_t err = NO_ERROR;
190
Mathias Agopiane47498f2011-08-08 19:35:15 -0700191 if (mAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700192 ST_LOGE("updateTexImage: SurfaceTexture is abandoned!");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700193 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700194 }
195
Jamie Gennis74bed552012-03-28 19:05:54 -0700196 if (!mAttached) {
197 ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL "
198 "ES context");
199 return INVALID_OPERATION;
200 }
201
Jamie Gennisce561372012-03-19 18:33:05 -0700202 EGLDisplay dpy = eglGetCurrentDisplay();
203 EGLContext ctx = eglGetCurrentContext();
204
Jamie Gennis74bed552012-03-28 19:05:54 -0700205 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
206 dpy == EGL_NO_DISPLAY) {
Jamie Gennisce561372012-03-19 18:33:05 -0700207 ST_LOGE("updateTexImage: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700208 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700209 }
210
Jamie Gennis74bed552012-03-28 19:05:54 -0700211 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
212 ctx == EGL_NO_CONTEXT) {
Jamie Gennisce561372012-03-19 18:33:05 -0700213 ST_LOGE("updateTexImage: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700214 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700215 }
216
217 mEglDisplay = dpy;
218 mEglContext = ctx;
219
Daniel Lamb2675792012-02-23 14:35:13 -0800220 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800221
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700222 // In asynchronous mode the list is guaranteed to be one buffer
223 // deep, while in synchronous mode we use the oldest buffer.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700224 err = acquireBufferLocked(&item);
Daniel Lamfbcda932012-04-09 22:51:52 -0700225 if (err == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800226 int buf = item.mBuf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700227
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700228 // we call the rejecter here, in case the caller has a reason to
229 // not accept this buffer. this is used by SurfaceFlinger to
230 // reject buffers which have the wrong size
Jamie Gennis9fea3422012-08-07 18:03:04 -0700231 if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
232 releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR, item.mFence);
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700233 glBindTexture(mTexTarget, mTexName);
234 return NO_ERROR;
235 }
236
Jamie Gennis9fea3422012-08-07 18:03:04 -0700237 GLint error;
238 while ((error = glGetError()) != GL_NO_ERROR) {
239 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
240 }
241
242 EGLImageKHR image = mEglSlots[buf].mEglImage;
243 glBindTexture(mTexTarget, mTexName);
244 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
245
246 while ((error = glGetError()) != GL_NO_ERROR) {
247 ST_LOGE("updateTexImage: error binding external texture image %p "
248 "(slot %d): %#04x", image, buf, error);
249 err = UNKNOWN_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800250 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800251
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700252 if (err == NO_ERROR) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700253 err = syncForReleaseLocked(dpy);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800254 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700255
Mathias Agopian7e477bf2012-05-18 16:50:58 -0700256 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700257 // Release the buffer we just acquired. It's not safe to
258 // release the old buffer, so instead we just drop the new frame.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700259 releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR, item.mFence);
Jamie Gennis74bed552012-03-28 19:05:54 -0700260 return err;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800261 }
262
263 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
264 mCurrentTexture,
265 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Jamie Gennis9fea3422012-08-07 18:03:04 -0700266 buf, mSlots[buf].mGraphicBuffer->handle);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700267
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700268 // release old buffer
Jamie Gennis74bed552012-03-28 19:05:54 -0700269 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700270 status_t status = releaseBufferLocked(mCurrentTexture, dpy,
271 mEglSlots[mCurrentTexture].mEglFence,
272 mSlots[mCurrentTexture].mFence);
273 if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) {
274 ST_LOGE("updateTexImage: failed to release buffer: %s (%d)",
275 strerror(-status), status);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700276 err = status;
277 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700278 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700279
Jamie Gennis9a78c902011-01-12 18:30:40 -0800280 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700281 mCurrentTexture = buf;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700282 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Daniel Lameae59d22012-01-22 15:26:27 -0800283 mCurrentCrop = item.mCrop;
284 mCurrentTransform = item.mTransform;
285 mCurrentScalingMode = item.mScalingMode;
286 mCurrentTimestamp = item.mTimestamp;
Jesse Halldc5b4852012-06-29 15:21:18 -0700287 mCurrentFence = item.mFence;
Jamie Gennis736aa952011-06-12 17:03:06 -0700288 computeCurrentTransformMatrix();
Daniel Lamfbcda932012-04-09 22:51:52 -0700289 } else {
290 if (err < 0) {
Jamie Gennis45cb2ba2012-08-06 17:10:57 -0700291 ST_LOGE("updateTexImage failed on acquire %d", err);
Daniel Lamfbcda932012-04-09 22:51:52 -0700292 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700293 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700294 glBindTexture(mTexTarget, mTexName);
Daniel Lamfbcda932012-04-09 22:51:52 -0700295 return OK;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800296 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700297
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700298 return err;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800299}
300
Jesse Hallef194142012-06-14 14:45:17 -0700301void SurfaceTexture::setReleaseFence(int fenceFd) {
Jamie Gennis3d1d09c2012-08-08 15:39:55 -0700302 sp<Fence> fence(new Fence(fenceFd));
Jamie Gennis45cb2ba2012-08-06 17:10:57 -0700303 if (fenceFd == -1 || mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT)
Jesse Hallef194142012-06-14 14:45:17 -0700304 return;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700305 if (!mSlots[mCurrentTexture].mFence.get()) {
306 mSlots[mCurrentTexture].mFence = fence;
Jesse Hallef194142012-06-14 14:45:17 -0700307 } else {
308 sp<Fence> mergedFence = Fence::merge(
309 String8("SurfaceTexture merged release"),
Jamie Gennis9fea3422012-08-07 18:03:04 -0700310 mSlots[mCurrentTexture].mFence, fence);
Jesse Halle7db7242012-07-03 13:41:54 -0700311 if (!mergedFence.get()) {
Jamie Gennis45cb2ba2012-08-06 17:10:57 -0700312 ST_LOGE("failed to merge release fences");
Jesse Hallef194142012-06-14 14:45:17 -0700313 // synchronization is broken, the best we can do is hope fences
314 // signal in order so the new fence will act like a union
Jamie Gennis9fea3422012-08-07 18:03:04 -0700315 mSlots[mCurrentTexture].mFence = fence;
Jesse Halle7db7242012-07-03 13:41:54 -0700316 return;
Jesse Hallef194142012-06-14 14:45:17 -0700317 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700318 mSlots[mCurrentTexture].mFence = mergedFence;
Jesse Hallef194142012-06-14 14:45:17 -0700319 }
320}
321
Jamie Gennis74bed552012-03-28 19:05:54 -0700322status_t SurfaceTexture::detachFromContext() {
323 ATRACE_CALL();
324 ST_LOGV("detachFromContext");
325 Mutex::Autolock lock(mMutex);
326
327 if (mAbandoned) {
328 ST_LOGE("detachFromContext: abandoned SurfaceTexture");
329 return NO_INIT;
330 }
331
332 if (!mAttached) {
333 ST_LOGE("detachFromContext: SurfaceTexture is not attached to a "
334 "context");
335 return INVALID_OPERATION;
336 }
337
338 EGLDisplay dpy = eglGetCurrentDisplay();
339 EGLContext ctx = eglGetCurrentContext();
340
341 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
342 ST_LOGE("detachFromContext: invalid current EGLDisplay");
343 return INVALID_OPERATION;
344 }
345
346 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
347 ST_LOGE("detachFromContext: invalid current EGLContext");
348 return INVALID_OPERATION;
349 }
350
351 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
352 status_t err = syncForReleaseLocked(dpy);
353 if (err != OK) {
354 return err;
355 }
356
357 glDeleteTextures(1, &mTexName);
358 }
359
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700360 // Because we're giving up the EGLDisplay we need to free all the EGLImages
361 // that are associated with it. They'll be recreated when the
362 // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a
363 // new EGLDisplay).
364 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700365 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700366 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700367 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700368 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700369 }
370 }
371
Jamie Gennis74bed552012-03-28 19:05:54 -0700372 mEglDisplay = EGL_NO_DISPLAY;
373 mEglContext = EGL_NO_CONTEXT;
374 mAttached = false;
375
376 return OK;
377}
378
379status_t SurfaceTexture::attachToContext(GLuint tex) {
380 ATRACE_CALL();
381 ST_LOGV("attachToContext");
382 Mutex::Autolock lock(mMutex);
383
384 if (mAbandoned) {
385 ST_LOGE("attachToContext: abandoned SurfaceTexture");
386 return NO_INIT;
387 }
388
389 if (mAttached) {
390 ST_LOGE("attachToContext: SurfaceTexture is already attached to a "
391 "context");
392 return INVALID_OPERATION;
393 }
394
395 EGLDisplay dpy = eglGetCurrentDisplay();
396 EGLContext ctx = eglGetCurrentContext();
397
398 if (dpy == EGL_NO_DISPLAY) {
399 ST_LOGE("attachToContext: invalid current EGLDisplay");
400 return INVALID_OPERATION;
401 }
402
403 if (ctx == EGL_NO_CONTEXT) {
404 ST_LOGE("attachToContext: invalid current EGLContext");
405 return INVALID_OPERATION;
406 }
407
408 // We need to bind the texture regardless of whether there's a current
409 // buffer.
410 glBindTexture(mTexTarget, tex);
411
412 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700413 // The EGLImageKHR that was associated with the slot was destroyed when
414 // the SurfaceTexture was detached from the old context, so we need to
415 // recreate it here.
416 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
417 if (image == EGL_NO_IMAGE_KHR) {
418 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700419 }
420
421 // Attach the current buffer to the GL texture.
422 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
423
424 GLint error;
425 status_t err = OK;
426 while ((error = glGetError()) != GL_NO_ERROR) {
427 ST_LOGE("attachToContext: error binding external texture image %p "
428 "(slot %d): %#04x", image, mCurrentTexture, error);
429 err = UNKNOWN_ERROR;
430 }
431
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700432 // We destroy the EGLImageKHR here because the current buffer may no
433 // longer be associated with one of the buffer slots, so we have
434 // nowhere to to store it. If the buffer is still associated with a
435 // slot then another EGLImageKHR will be created next time that buffer
436 // gets acquired in updateTexImage.
437 eglDestroyImageKHR(dpy, image);
Jamie Gennis74bed552012-03-28 19:05:54 -0700438
439 if (err != OK) {
440 return err;
441 }
442 }
443
444 mEglDisplay = dpy;
445 mEglContext = ctx;
446 mTexName = tex;
447 mAttached = true;
448
449 return OK;
450}
451
452status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) {
453 ST_LOGV("syncForReleaseLocked");
454
455 if (mUseFenceSync && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700456 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700457 if (fence != EGL_NO_SYNC_KHR) {
458 // There is already a fence for the current slot. We need to wait
459 // on that before replacing it with another fence to ensure that all
460 // outstanding buffer accesses have completed before the producer
461 // accesses it.
462 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
463 if (result == EGL_FALSE) {
464 ST_LOGE("syncForReleaseLocked: error waiting for previous "
465 "fence: %#x", eglGetError());
466 return UNKNOWN_ERROR;
467 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
468 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
469 "fence");
470 return TIMED_OUT;
471 }
472 eglDestroySyncKHR(dpy, fence);
473 }
474
475 // Create a fence for the outstanding accesses in the current OpenGL ES
476 // context.
477 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
478 if (fence == EGL_NO_SYNC_KHR) {
479 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
480 eglGetError());
481 return UNKNOWN_ERROR;
482 }
483 glFlush();
Jamie Gennis9fea3422012-08-07 18:03:04 -0700484 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700485 }
486
487 return OK;
488}
489
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700490bool SurfaceTexture::isExternalFormat(uint32_t format)
491{
492 switch (format) {
493 // supported YUV formats
494 case HAL_PIXEL_FORMAT_YV12:
495 // Legacy/deprecated YUV formats
496 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
497 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
498 case HAL_PIXEL_FORMAT_YCbCr_422_I:
499 return true;
500 }
501
502 // Any OEM format needs to be considered
503 if (format>=0x100 && format<=0x1FF)
504 return true;
505
506 return false;
507}
508
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700509GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700510 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700511}
512
Jamie Gennisf238e282011-01-09 16:33:17 -0800513void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800514 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700515 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
516}
517
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700518void SurfaceTexture::setFilteringEnabled(bool enabled) {
519 Mutex::Autolock lock(mMutex);
520 bool needsRecompute = mFilteringEnabled != enabled;
521 mFilteringEnabled = enabled;
522 if (needsRecompute) {
523 computeCurrentTransformMatrix();
524 }
525}
526
Jamie Gennis736aa952011-06-12 17:03:06 -0700527void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700528 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800529
Jamie Gennisa214c642011-01-14 13:53:31 -0800530 float xform[16];
531 for (int i = 0; i < 16; i++) {
532 xform[i] = mtxIdentity[i];
533 }
534 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
535 float result[16];
536 mtxMul(result, xform, mtxFlipH);
537 for (int i = 0; i < 16; i++) {
538 xform[i] = result[i];
539 }
540 }
541 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
542 float result[16];
543 mtxMul(result, xform, mtxFlipV);
544 for (int i = 0; i < 16; i++) {
545 xform[i] = result[i];
546 }
547 }
548 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
549 float result[16];
550 mtxMul(result, xform, mtxRot90);
551 for (int i = 0; i < 16; i++) {
552 xform[i] = result[i];
553 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800554 }
555
Daniel Lameae59d22012-01-22 15:26:27 -0800556 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisd72f2332012-05-07 13:50:11 -0700557 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700558 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
559 float bufferWidth = buf->getWidth();
560 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700561 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700562 float shrinkAmount = 0.0f;
563 if (mFilteringEnabled) {
564 // In order to prevent bilinear sampling beyond the edge of the
565 // crop rectangle we may need to shrink it by 2 texels in each
566 // dimension. Normally this would just need to take 1/2 a texel
567 // off each end, but because the chroma channels of YUV420 images
568 // are subsampled we may need to shrink the crop region by a whole
569 // texel on each side.
570 switch (buf->getPixelFormat()) {
571 case PIXEL_FORMAT_RGBA_8888:
572 case PIXEL_FORMAT_RGBX_8888:
573 case PIXEL_FORMAT_RGB_888:
574 case PIXEL_FORMAT_RGB_565:
575 case PIXEL_FORMAT_BGRA_8888:
576 case PIXEL_FORMAT_RGBA_5551:
577 case PIXEL_FORMAT_RGBA_4444:
578 // We know there's no subsampling of any channels, so we
579 // only need to shrink by a half a pixel.
580 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700581 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700582
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700583 default:
584 // If we don't recognize the format, we must assume the
585 // worst case (that we care about), which is YUV420.
586 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700587 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700588 }
589 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700590
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700591 // Only shrink the dimensions that are not the size of the buffer.
592 if (cropRect.width() < bufferWidth) {
593 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
594 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
595 bufferWidth;
596 }
597 if (cropRect.height() < bufferHeight) {
598 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
599 bufferHeight;
600 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
601 bufferHeight;
602 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800603 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800604 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800605 sx, 0, 0, 0,
606 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800607 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800608 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800609 };
610
Jamie Gennisa214c642011-01-14 13:53:31 -0800611 float mtxBeforeFlipV[16];
612 mtxMul(mtxBeforeFlipV, crop, xform);
613
614 // SurfaceFlinger expects the top of its window textures to be at a Y
615 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
616 // want to expose this to applications, however, so we must add an
617 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700618 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800619}
620
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800621nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700622 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800623 Mutex::Autolock lock(mMutex);
624 return mCurrentTimestamp;
625}
626
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800627EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
628 const sp<GraphicBuffer>& graphicBuffer) {
629 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
630 EGLint attrs[] = {
631 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
632 EGL_NONE,
633 };
634 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
635 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700636 if (image == EGL_NO_IMAGE_KHR) {
637 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700638 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800639 }
640 return image;
641}
642
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700643sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
644 Mutex::Autolock lock(mMutex);
645 return mCurrentTextureBuf;
646}
647
648Rect SurfaceTexture::getCurrentCrop() const {
649 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700650
651 Rect outCrop = mCurrentCrop;
652 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
653 int32_t newWidth = mCurrentCrop.width();
654 int32_t newHeight = mCurrentCrop.height();
655
656 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
657 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
658 ST_LOGV("too wide: newWidth = %d", newWidth);
659 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
660 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
661 ST_LOGV("too tall: newHeight = %d", newHeight);
662 }
663
664 // The crop is too wide
665 if (newWidth < mCurrentCrop.width()) {
666 int32_t dw = (newWidth - mCurrentCrop.width())/2;
667 outCrop.left -=dw;
668 outCrop.right += dw;
669 // The crop is too tall
670 } else if (newHeight < mCurrentCrop.height()) {
671 int32_t dh = (newHeight - mCurrentCrop.height())/2;
672 outCrop.top -= dh;
673 outCrop.bottom += dh;
674 }
675
676 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
677 outCrop.left, outCrop.top,
678 outCrop.right,outCrop.bottom);
679 }
680
Daniel Lam016c8cb2012-04-03 15:54:58 -0700681 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700682}
683
684uint32_t SurfaceTexture::getCurrentTransform() const {
685 Mutex::Autolock lock(mMutex);
686 return mCurrentTransform;
687}
688
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700689uint32_t SurfaceTexture::getCurrentScalingMode() const {
690 Mutex::Autolock lock(mMutex);
691 return mCurrentScalingMode;
692}
693
Jesse Halldc5b4852012-06-29 15:21:18 -0700694sp<Fence> SurfaceTexture::getCurrentFence() const {
695 Mutex::Autolock lock(mMutex);
696 return mCurrentFence;
697}
698
Jamie Gennis59769462011-11-19 18:04:43 -0800699bool SurfaceTexture::isSynchronousMode() const {
700 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800701 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800702}
703
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700704void SurfaceTexture::freeBufferLocked(int slotIndex) {
705 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700706 if (slotIndex == mCurrentTexture) {
707 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
708 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700709 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700710 if (img != EGL_NO_IMAGE_KHR) {
711 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
712 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800713 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700714 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
715 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700716}
Daniel Lameae59d22012-01-22 15:26:27 -0800717
Jamie Gennis9fea3422012-08-07 18:03:04 -0700718void SurfaceTexture::abandonLocked() {
719 ST_LOGV("abandonLocked");
720 mCurrentTextureBuf.clear();
721 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700722}
723
Jamie Gennisfa28c352011-09-16 17:30:26 -0700724void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800725 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700726 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800727 mBufferQueue->setConsumerName(name);
728}
729
730status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
731 Mutex::Autolock lock(mMutex);
732 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
733}
734
735status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
736 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700737 usage |= DEFAULT_USAGE_FLAGS;
Daniel Lamb2675792012-02-23 14:35:13 -0800738 return mBufferQueue->setConsumerUsageBits(usage);
739}
740
741status_t SurfaceTexture::setTransformHint(uint32_t hint) {
742 Mutex::Autolock lock(mMutex);
743 return mBufferQueue->setTransformHint(hint);
744}
745
746// Used for refactoring BufferQueue from SurfaceTexture
747// Should not be in final interface once users of SurfaceTexture are clean up.
748status_t SurfaceTexture::setSynchronousMode(bool enabled) {
749 Mutex::Autolock lock(mMutex);
750 return mBufferQueue->setSynchronousMode(enabled);
751}
752
Jamie Gennis9fea3422012-08-07 18:03:04 -0700753void SurfaceTexture::dumpLocked(String8& result, const char* prefix,
754 char* buffer, size_t size) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700755{
Jamie Gennis9fea3422012-08-07 18:03:04 -0700756 snprintf(buffer, size,
757 "%smTexName=%d mCurrentTexture=%d\n"
758 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
759 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
760 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
761 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700762 result.append(buffer);
763
Jamie Gennis9fea3422012-08-07 18:03:04 -0700764 ConsumerBase::dumpLocked(result, prefix, buffer, size);
Mathias Agopian68c77942011-05-09 19:08:33 -0700765}
766
Jamie Gennisf238e282011-01-09 16:33:17 -0800767static void mtxMul(float out[16], const float a[16], const float b[16]) {
768 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
769 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
770 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
771 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
772
773 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
774 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
775 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
776 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
777
778 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
779 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
780 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
781 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
782
783 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
784 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
785 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
786 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
787}
788
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800789}; // namespace android