blob: 0f212557810879bab853a90a8649398290ad994a [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),
121 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
Daniel Lam6b091c52012-01-22 15:26:27 -0800122{
Daniel Lameae59d22012-01-22 15:26:27 -0800123 // Choose a name using the PID and a process-unique ID.
124 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700125 ST_LOGV("SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800126 if (bufferQueue == 0) {
Daniel Lamb2675792012-02-23 14:35:13 -0800127 ST_LOGV("Creating a new BufferQueue");
128 mBufferQueue = new BufferQueue(allowSynchronousMode);
129 }
130 else {
131 mBufferQueue = bufferQueue;
132 }
Daniel Lamb2675792012-02-23 14:35:13 -0800133
Jamie Gennisfa28c352011-09-16 17:30:26 -0700134 memcpy(mCurrentTransformMatrix, mtxIdentity,
135 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700136
137 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
138 // reference once the ctor ends, as that would cause the refcount of 'this'
139 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
140 // that's what we create.
141 wp<BufferQueue::ConsumerListener> listener;
142 sp<BufferQueue::ConsumerListener> proxy;
143 listener = static_cast<BufferQueue::ConsumerListener*>(this);
144 proxy = new BufferQueue::ProxyConsumerListener(listener);
145
146 status_t err = mBufferQueue->consumerConnect(proxy);
147 if (err != NO_ERROR) {
148 ST_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)",
149 strerror(-err), err);
150 } else {
151 mBufferQueue->setConsumerName(mName);
152 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800153}
154
155SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700156 ST_LOGV("~SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800157
Daniel Lameae59d22012-01-22 15:26:27 -0800158 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800159}
160
Mathias Agopian80727112011-05-02 19:51:12 -0700161status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
162 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800163 return mBufferQueue->setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700164}
165
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800166
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700167status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
168{
Daniel Lamb2675792012-02-23 14:35:13 -0800169 Mutex::Autolock lock(mMutex);
170 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700171}
172
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800173status_t SurfaceTexture::updateTexImage() {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800174 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700175 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800176 Mutex::Autolock lock(mMutex);
177
Mathias Agopiane47498f2011-08-08 19:35:15 -0700178 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700179 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700180 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700181 }
182
Jamie Gennisce561372012-03-19 18:33:05 -0700183 EGLDisplay dpy = eglGetCurrentDisplay();
184 EGLContext ctx = eglGetCurrentContext();
185
186 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
187 ST_LOGE("updateTexImage: invalid current EGLDisplay");
188 return -EINVAL;
189 }
190
191 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
192 ST_LOGE("updateTexImage: invalid current EGLContext");
193 return -EINVAL;
194 }
195
196 mEglDisplay = dpy;
197 mEglContext = ctx;
198
Daniel Lamb2675792012-02-23 14:35:13 -0800199 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800200
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700201 // In asynchronous mode the list is guaranteed to be one buffer
202 // deep, while in synchronous mode we use the oldest buffer.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700203 if (mBufferQueue->acquireBuffer(&item) == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800204 int buf = item.mBuf;
205 // This buffer was newly allocated, so we need to clean up on our side
206 if (item.mGraphicBuffer != NULL) {
207 mEGLSlots[buf].mGraphicBuffer = 0;
208 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700209 eglDestroyImageKHR(dpy, mEGLSlots[buf].mEglImage);
Daniel Lameae59d22012-01-22 15:26:27 -0800210 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800211 }
212 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
213 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700214
Jamie Gennisf238e282011-01-09 16:33:17 -0800215 // Update the GL texture object.
Daniel Lameae59d22012-01-22 15:26:27 -0800216 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800217 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800218 if (item.mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700219 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700220 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700221 }
Daniel Lameae59d22012-01-22 15:26:27 -0800222 image = createImage(dpy, item.mGraphicBuffer);
223 mEGLSlots[buf].mEglImage = image;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700224 if (image == EGL_NO_IMAGE_KHR) {
225 // NOTE: if dpy was invalid, createImage() is guaranteed to
226 // fail. so we'd end up here.
227 return -EINVAL;
228 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800229 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800230
231 GLint error;
232 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700233 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800234 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700235
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700236 glBindTexture(mTexTarget, mTexName);
237 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700238
Jamie Gennis0eb88512011-01-26 11:52:02 -0800239 bool failed = false;
240 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700241 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700242 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800243 failed = true;
244 }
245 if (failed) {
Jamie Gennisce561372012-03-19 18:33:05 -0700246 mBufferQueue->releaseBuffer(buf, dpy, mEGLSlots[buf].mFence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800247 return -EINVAL;
248 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800249
Daniel Lamb2675792012-02-23 14:35:13 -0800250 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800251 if (mUseFenceSync) {
252 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
253 NULL);
254 if (fence == EGL_NO_SYNC_KHR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000255 ALOGE("updateTexImage: error creating fence: %#x",
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800256 eglGetError());
Jamie Gennisce561372012-03-19 18:33:05 -0700257 mBufferQueue->releaseBuffer(buf, dpy,
Daniel Lameae59d22012-01-22 15:26:27 -0800258 mEGLSlots[buf].mFence);
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800259 return -EINVAL;
260 }
261 glFlush();
Daniel Lameae59d22012-01-22 15:26:27 -0800262 mEGLSlots[mCurrentTexture].mFence = fence;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800263 }
264 }
265
266 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
267 mCurrentTexture,
268 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800269 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700270
Daniel Lameae59d22012-01-22 15:26:27 -0800271 // release old buffer
Jamie Gennisce561372012-03-19 18:33:05 -0700272 mBufferQueue->releaseBuffer(mCurrentTexture, dpy,
Daniel Lameae59d22012-01-22 15:26:27 -0800273 mEGLSlots[mCurrentTexture].mFence);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700274
Jamie Gennis9a78c902011-01-12 18:30:40 -0800275 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700276 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800277 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
278 mCurrentCrop = item.mCrop;
279 mCurrentTransform = item.mTransform;
280 mCurrentScalingMode = item.mScalingMode;
281 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700282 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700283
284 // Now that we've passed the point at which failures can happen,
285 // it's safe to remove the buffer from the front of the queue.
Daniel Lameae59d22012-01-22 15:26:27 -0800286
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700287 } else {
288 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700289 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800290 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700291
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800292 return OK;
293}
294
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700295bool SurfaceTexture::isExternalFormat(uint32_t format)
296{
297 switch (format) {
298 // supported YUV formats
299 case HAL_PIXEL_FORMAT_YV12:
300 // Legacy/deprecated YUV formats
301 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
302 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
303 case HAL_PIXEL_FORMAT_YCbCr_422_I:
304 return true;
305 }
306
307 // Any OEM format needs to be considered
308 if (format>=0x100 && format<=0x1FF)
309 return true;
310
311 return false;
312}
313
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700314GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700315 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700316}
317
Jamie Gennisf238e282011-01-09 16:33:17 -0800318void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800319 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700320 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
321}
322
323void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700324 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800325
Jamie Gennisa214c642011-01-14 13:53:31 -0800326 float xform[16];
327 for (int i = 0; i < 16; i++) {
328 xform[i] = mtxIdentity[i];
329 }
330 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
331 float result[16];
332 mtxMul(result, xform, mtxFlipH);
333 for (int i = 0; i < 16; i++) {
334 xform[i] = result[i];
335 }
336 }
337 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
338 float result[16];
339 mtxMul(result, xform, mtxFlipV);
340 for (int i = 0; i < 16; i++) {
341 xform[i] = result[i];
342 }
343 }
344 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
345 float result[16];
346 mtxMul(result, xform, mtxRot90);
347 for (int i = 0; i < 16; i++) {
348 xform[i] = result[i];
349 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800350 }
351
Daniel Lameae59d22012-01-22 15:26:27 -0800352 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisa214c642011-01-14 13:53:31 -0800353 float tx, ty, sx, sy;
354 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800355 // In order to prevent bilinear sampling at the of the crop rectangle we
356 // may need to shrink it by 2 texels in each direction. Normally this
357 // would just need to take 1/2 a texel off each end, but because the
358 // chroma channels will likely be subsampled we need to chop off a whole
359 // texel. This will cause artifacts if someone does nearest sampling
360 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
361 // accomodate the bilinear and nearest sampling uses.
362 //
363 // If nearest sampling turns out to be a desirable usage of these
364 // textures then we could add the ability to switch a SurfaceTexture to
365 // nearest-mode. Preferably, however, the image producers (video
366 // decoder, camera, etc.) would simply not use a crop rectangle (or at
367 // least not tell the framework about it) so that the GPU can do the
368 // correct edge behavior.
369 int xshrink = 0, yshrink = 0;
370 if (mCurrentCrop.left > 0) {
371 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
372 xshrink++;
373 } else {
374 tx = 0.0f;
375 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700376 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800377 xshrink++;
378 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700379 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800380 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
381 float(buf->getHeight());
382 yshrink++;
383 } else {
384 ty = 0.0f;
385 }
386 if (mCurrentCrop.top > 0) {
387 yshrink++;
388 }
389 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
390 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800391 } else {
392 tx = 0.0f;
393 ty = 0.0f;
394 sx = 1.0f;
395 sy = 1.0f;
396 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800397 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800398 sx, 0, 0, 0,
399 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800400 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800401 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800402 };
403
Jamie Gennisa214c642011-01-14 13:53:31 -0800404 float mtxBeforeFlipV[16];
405 mtxMul(mtxBeforeFlipV, crop, xform);
406
407 // SurfaceFlinger expects the top of its window textures to be at a Y
408 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
409 // want to expose this to applications, however, so we must add an
410 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700411 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800412}
413
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800414nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700415 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800416 Mutex::Autolock lock(mMutex);
417 return mCurrentTimestamp;
418}
419
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800420void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700421 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700422 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800423 Mutex::Autolock lock(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700424 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800425}
426
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800427EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
428 const sp<GraphicBuffer>& graphicBuffer) {
429 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
430 EGLint attrs[] = {
431 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
432 EGL_NONE,
433 };
434 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
435 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700436 if (image == EGL_NO_IMAGE_KHR) {
437 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700438 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800439 }
440 return image;
441}
442
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700443sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
444 Mutex::Autolock lock(mMutex);
445 return mCurrentTextureBuf;
446}
447
448Rect SurfaceTexture::getCurrentCrop() const {
449 Mutex::Autolock lock(mMutex);
450 return mCurrentCrop;
451}
452
453uint32_t SurfaceTexture::getCurrentTransform() const {
454 Mutex::Autolock lock(mMutex);
455 return mCurrentTransform;
456}
457
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700458uint32_t SurfaceTexture::getCurrentScalingMode() const {
459 Mutex::Autolock lock(mMutex);
460 return mCurrentScalingMode;
461}
462
Jamie Gennis59769462011-11-19 18:04:43 -0800463bool SurfaceTexture::isSynchronousMode() const {
464 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800465 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800466}
467
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700468void SurfaceTexture::freeBufferLocked(int slotIndex) {
469 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
470 mEGLSlots[slotIndex].mGraphicBuffer = 0;
471 if (mEGLSlots[slotIndex].mEglImage != EGL_NO_IMAGE_KHR) {
472 EGLImageKHR img = mEGLSlots[slotIndex].mEglImage;
473 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennisce561372012-03-19 18:33:05 -0700474 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800475 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700476 mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
Daniel Lameae59d22012-01-22 15:26:27 -0800477 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700478}
Daniel Lameae59d22012-01-22 15:26:27 -0800479
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700480void SurfaceTexture::abandon() {
481 ST_LOGV("abandon");
482 Mutex::Autolock lock(mMutex);
483
484 if (!mAbandoned) {
485 mAbandoned = true;
486 mCurrentTextureBuf.clear();
487
488 // destroy all egl buffers
489 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
490 freeBufferLocked(i);
491 }
492
493 // disconnect from the BufferQueue
494 mBufferQueue->consumerDisconnect();
495 mBufferQueue.clear();
496 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700497}
498
Jamie Gennisfa28c352011-09-16 17:30:26 -0700499void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800500 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700501 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800502 mBufferQueue->setConsumerName(name);
503}
504
505status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
506 Mutex::Autolock lock(mMutex);
507 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
508}
509
510status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
511 Mutex::Autolock lock(mMutex);
512 return mBufferQueue->setConsumerUsageBits(usage);
513}
514
515status_t SurfaceTexture::setTransformHint(uint32_t hint) {
516 Mutex::Autolock lock(mMutex);
517 return mBufferQueue->setTransformHint(hint);
518}
519
520// Used for refactoring BufferQueue from SurfaceTexture
521// Should not be in final interface once users of SurfaceTexture are clean up.
522status_t SurfaceTexture::setSynchronousMode(bool enabled) {
523 Mutex::Autolock lock(mMutex);
524 return mBufferQueue->setSynchronousMode(enabled);
525}
526
527// Used for refactoring, should not be in final interface
528sp<BufferQueue> SurfaceTexture::getBufferQueue() const {
529 Mutex::Autolock lock(mMutex);
530 return mBufferQueue;
531}
532
533// Used for refactoring, should not be in final interface
534status_t SurfaceTexture::setBufferCount(int bufferCount) {
535 Mutex::Autolock lock(mMutex);
536 return mBufferQueue->setBufferCount(bufferCount);
537}
538
539// Used for refactoring, should not be in final interface
540status_t SurfaceTexture::connect(int api,
541 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
542 Mutex::Autolock lock(mMutex);
543 return mBufferQueue->connect(api, outWidth, outHeight, outTransform);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700544}
545
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700546void SurfaceTexture::onFrameAvailable() {
547 ST_LOGV("onFrameAvailable");
548
549 sp<FrameAvailableListener> listener;
550 { // scope for the lock
551 Mutex::Autolock lock(mMutex);
552 listener = mFrameAvailableListener;
553 }
554
555 if (listener != NULL) {
556 ST_LOGV("actually calling onFrameAvailable");
557 listener->onFrameAvailable();
558 }
559}
560
561void SurfaceTexture::onBuffersReleased() {
562 ST_LOGV("onBuffersReleased");
563
564 Mutex::Autolock lock(mMutex);
565
566 if (mAbandoned) {
567 // Nothing to do if we're already abandoned.
568 return;
569 }
570
571 uint32_t mask = 0;
572 mBufferQueue->getReleasedBuffers(&mask);
573 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
574 if (mask & (1 << i)) {
575 freeBufferLocked(i);
576 }
577 }
578}
579
Mathias Agopian68c77942011-05-09 19:08:33 -0700580void SurfaceTexture::dump(String8& result) const
581{
582 char buffer[1024];
583 dump(result, "", buffer, 1024);
584}
585
586void SurfaceTexture::dump(String8& result, const char* prefix,
587 char* buffer, size_t SIZE) const
588{
589 Mutex::Autolock _l(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700590 snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName,
591 int(mAbandoned));
Mathias Agopian68c77942011-05-09 19:08:33 -0700592 result.append(buffer);
593
Mathias Agopian68c77942011-05-09 19:08:33 -0700594 snprintf(buffer, SIZE,
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700595 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n",
596 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700597 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800598 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700599 );
600 result.append(buffer);
601
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700602 if (!mAbandoned) {
603 mBufferQueue->dump(result, prefix, buffer, SIZE);
604 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700605}
606
Jamie Gennisf238e282011-01-09 16:33:17 -0800607static void mtxMul(float out[16], const float a[16], const float b[16]) {
608 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
609 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
610 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
611 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
612
613 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
614 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
615 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
616 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
617
618 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
619 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
620 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
621 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
622
623 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
624 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
625 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
626 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
627}
628
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800629}; // namespace android