blob: b42aa34d76529a60cfd1a74c961f9677f807d60c [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,
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800108 GLenum texTarget, bool useFenceSync) :
Daniel Lam6b091c52012-01-22 15:26:27 -0800109 BufferQueue(allowSynchronousMode),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800110 mCurrentTransform(0),
111 mCurrentTimestamp(0),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700112 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800113#ifdef USE_FENCE_SYNC
114 mUseFenceSync(useFenceSync),
115#else
116 mUseFenceSync(false),
117#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800118 mTexTarget(texTarget),
119 mAbandoned(false),
120 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
Daniel Lam6b091c52012-01-22 15:26:27 -0800121{
Daniel Lameae59d22012-01-22 15:26:27 -0800122 // Choose a name using the PID and a process-unique ID.
123 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
124 BufferQueue::setConsumerName(mName);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700125
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700126 ST_LOGV("SurfaceTexture");
Jamie Gennisfa28c352011-09-16 17:30:26 -0700127 memcpy(mCurrentTransformMatrix, mtxIdentity,
128 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129}
130
131SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700132 ST_LOGV("~SurfaceTexture");
Daniel Lameae59d22012-01-22 15:26:27 -0800133 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800134}
135
Mathias Agopian80727112011-05-02 19:51:12 -0700136status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
137 Mutex::Autolock lock(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800138 return BufferQueue::setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700139}
140
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800141
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700142status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
143{
Daniel Lameae59d22012-01-22 15:26:27 -0800144 return BufferQueue::setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700145}
146
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800147status_t SurfaceTexture::updateTexImage() {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800148 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700149 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800150 Mutex::Autolock lock(mMutex);
151
Mathias Agopiane47498f2011-08-08 19:35:15 -0700152 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700153 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700154 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700155 }
156
Daniel Lameae59d22012-01-22 15:26:27 -0800157 BufferItem item;
158
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700159 // In asynchronous mode the list is guaranteed to be one buffer
160 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lameae59d22012-01-22 15:26:27 -0800161 if (acquire(&item) == NO_ERROR) {
162 int buf = item.mBuf;
163 // This buffer was newly allocated, so we need to clean up on our side
164 if (item.mGraphicBuffer != NULL) {
165 mEGLSlots[buf].mGraphicBuffer = 0;
166 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
167 eglDestroyImageKHR(mEGLSlots[buf].mEglDisplay,
168 mEGLSlots[buf].mEglImage);
169 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
170 mEGLSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
171 }
172 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
173 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700174
Jamie Gennisf238e282011-01-09 16:33:17 -0800175 // Update the GL texture object.
Daniel Lameae59d22012-01-22 15:26:27 -0800176 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800177 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800179 if (item.mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700180 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700181 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700182 }
Daniel Lameae59d22012-01-22 15:26:27 -0800183 image = createImage(dpy, item.mGraphicBuffer);
184 mEGLSlots[buf].mEglImage = image;
185 mEGLSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700186 if (image == EGL_NO_IMAGE_KHR) {
187 // NOTE: if dpy was invalid, createImage() is guaranteed to
188 // fail. so we'd end up here.
189 return -EINVAL;
190 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800191 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800192
193 GLint error;
194 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700195 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800196 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700197
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700198 glBindTexture(mTexTarget, mTexName);
199 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700200
Jamie Gennis0eb88512011-01-26 11:52:02 -0800201 bool failed = false;
202 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700203 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700204 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800205 failed = true;
206 }
207 if (failed) {
Daniel Lameae59d22012-01-22 15:26:27 -0800208 releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
209 mEGLSlots[buf].mFence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800210 return -EINVAL;
211 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800212
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800213 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
214 if (mUseFenceSync) {
215 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
216 NULL);
217 if (fence == EGL_NO_SYNC_KHR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000218 ALOGE("updateTexImage: error creating fence: %#x",
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800219 eglGetError());
Daniel Lameae59d22012-01-22 15:26:27 -0800220 releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
221 mEGLSlots[buf].mFence);
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800222 return -EINVAL;
223 }
224 glFlush();
Daniel Lameae59d22012-01-22 15:26:27 -0800225 mEGLSlots[mCurrentTexture].mFence = fence;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800226 }
227 }
228
229 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
230 mCurrentTexture,
231 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800232 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700233
Daniel Lameae59d22012-01-22 15:26:27 -0800234 // release old buffer
235 releaseBuffer(mCurrentTexture,
236 mEGLSlots[mCurrentTexture].mEglDisplay,
237 mEGLSlots[mCurrentTexture].mFence);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700238
Jamie Gennis9a78c902011-01-12 18:30:40 -0800239 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700240 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800241 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
242 mCurrentCrop = item.mCrop;
243 mCurrentTransform = item.mTransform;
244 mCurrentScalingMode = item.mScalingMode;
245 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700246 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700247
248 // Now that we've passed the point at which failures can happen,
249 // it's safe to remove the buffer from the front of the queue.
Daniel Lameae59d22012-01-22 15:26:27 -0800250
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700251 } else {
252 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700253 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800254 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700255
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800256 return OK;
257}
258
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700259bool SurfaceTexture::isExternalFormat(uint32_t format)
260{
261 switch (format) {
262 // supported YUV formats
263 case HAL_PIXEL_FORMAT_YV12:
264 // Legacy/deprecated YUV formats
265 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
266 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
267 case HAL_PIXEL_FORMAT_YCbCr_422_I:
268 return true;
269 }
270
271 // Any OEM format needs to be considered
272 if (format>=0x100 && format<=0x1FF)
273 return true;
274
275 return false;
276}
277
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700278GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700279 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700280}
281
Jamie Gennisf238e282011-01-09 16:33:17 -0800282void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800283 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700284 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
285}
286
287void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700288 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800289
Jamie Gennisa214c642011-01-14 13:53:31 -0800290 float xform[16];
291 for (int i = 0; i < 16; i++) {
292 xform[i] = mtxIdentity[i];
293 }
294 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
295 float result[16];
296 mtxMul(result, xform, mtxFlipH);
297 for (int i = 0; i < 16; i++) {
298 xform[i] = result[i];
299 }
300 }
301 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
302 float result[16];
303 mtxMul(result, xform, mtxFlipV);
304 for (int i = 0; i < 16; i++) {
305 xform[i] = result[i];
306 }
307 }
308 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
309 float result[16];
310 mtxMul(result, xform, mtxRot90);
311 for (int i = 0; i < 16; i++) {
312 xform[i] = result[i];
313 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800314 }
315
Daniel Lameae59d22012-01-22 15:26:27 -0800316 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisa214c642011-01-14 13:53:31 -0800317 float tx, ty, sx, sy;
318 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800319 // In order to prevent bilinear sampling at the of the crop rectangle we
320 // may need to shrink it by 2 texels in each direction. Normally this
321 // would just need to take 1/2 a texel off each end, but because the
322 // chroma channels will likely be subsampled we need to chop off a whole
323 // texel. This will cause artifacts if someone does nearest sampling
324 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
325 // accomodate the bilinear and nearest sampling uses.
326 //
327 // If nearest sampling turns out to be a desirable usage of these
328 // textures then we could add the ability to switch a SurfaceTexture to
329 // nearest-mode. Preferably, however, the image producers (video
330 // decoder, camera, etc.) would simply not use a crop rectangle (or at
331 // least not tell the framework about it) so that the GPU can do the
332 // correct edge behavior.
333 int xshrink = 0, yshrink = 0;
334 if (mCurrentCrop.left > 0) {
335 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
336 xshrink++;
337 } else {
338 tx = 0.0f;
339 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700340 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800341 xshrink++;
342 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700343 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800344 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
345 float(buf->getHeight());
346 yshrink++;
347 } else {
348 ty = 0.0f;
349 }
350 if (mCurrentCrop.top > 0) {
351 yshrink++;
352 }
353 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
354 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800355 } else {
356 tx = 0.0f;
357 ty = 0.0f;
358 sx = 1.0f;
359 sy = 1.0f;
360 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800361 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800362 sx, 0, 0, 0,
363 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800364 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800365 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800366 };
367
Jamie Gennisa214c642011-01-14 13:53:31 -0800368 float mtxBeforeFlipV[16];
369 mtxMul(mtxBeforeFlipV, crop, xform);
370
371 // SurfaceFlinger expects the top of its window textures to be at a Y
372 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
373 // want to expose this to applications, however, so we must add an
374 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700375 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800376}
377
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800378nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700379 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800380 Mutex::Autolock lock(mMutex);
381 return mCurrentTimestamp;
382}
383
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800384void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700385 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700386 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800387 Mutex::Autolock lock(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800388 BufferQueue::setFrameAvailableListener(listener);
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800389}
390
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800391EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
392 const sp<GraphicBuffer>& graphicBuffer) {
393 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
394 EGLint attrs[] = {
395 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
396 EGL_NONE,
397 };
398 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
399 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700400 if (image == EGL_NO_IMAGE_KHR) {
401 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700402 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800403 }
404 return image;
405}
406
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700407sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
408 Mutex::Autolock lock(mMutex);
409 return mCurrentTextureBuf;
410}
411
412Rect SurfaceTexture::getCurrentCrop() const {
413 Mutex::Autolock lock(mMutex);
414 return mCurrentCrop;
415}
416
417uint32_t SurfaceTexture::getCurrentTransform() const {
418 Mutex::Autolock lock(mMutex);
419 return mCurrentTransform;
420}
421
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700422uint32_t SurfaceTexture::getCurrentScalingMode() const {
423 Mutex::Autolock lock(mMutex);
424 return mCurrentScalingMode;
425}
426
Jamie Gennis59769462011-11-19 18:04:43 -0800427bool SurfaceTexture::isSynchronousMode() const {
428 Mutex::Autolock lock(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800429 return BufferQueue::isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800430}
431
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700432void SurfaceTexture::abandon() {
433 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700434 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700435 mCurrentTextureBuf.clear();
Daniel Lameae59d22012-01-22 15:26:27 -0800436
437 // destroy all egl buffers
438 for (int i =0; i < NUM_BUFFER_SLOTS; i++) {
439 mEGLSlots[i].mGraphicBuffer = 0;
440 if (mEGLSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
441 eglDestroyImageKHR(mEGLSlots[i].mEglDisplay,
442 mEGLSlots[i].mEglImage);
443 mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
444 mEGLSlots[i].mEglDisplay = EGL_NO_DISPLAY;
445 }
446 }
447
448 // disconnect from the BufferQueue
449 BufferQueue::consumerDisconnect();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700450}
451
Jamie Gennisfa28c352011-09-16 17:30:26 -0700452void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800453 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700454 mName = name;
Daniel Lameae59d22012-01-22 15:26:27 -0800455 BufferQueue::setConsumerName(name);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700456}
457
Mathias Agopian68c77942011-05-09 19:08:33 -0700458void SurfaceTexture::dump(String8& result) const
459{
460 char buffer[1024];
461 dump(result, "", buffer, 1024);
462}
463
464void SurfaceTexture::dump(String8& result, const char* prefix,
465 char* buffer, size_t SIZE) const
466{
467 Mutex::Autolock _l(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800468 snprintf(buffer, SIZE, "%smTexName=%d\n", prefix, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -0700469 result.append(buffer);
470
Mathias Agopian68c77942011-05-09 19:08:33 -0700471 snprintf(buffer, SIZE,
Daniel Lameae59d22012-01-22 15:26:27 -0800472 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
473 ,prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700474 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800475 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700476 );
477 result.append(buffer);
478
Mathias Agopian68c77942011-05-09 19:08:33 -0700479
Daniel Lameae59d22012-01-22 15:26:27 -0800480 BufferQueue::dump(result, prefix, buffer, SIZE);
Mathias Agopian68c77942011-05-09 19:08:33 -0700481}
482
Jamie Gennisf238e282011-01-09 16:33:17 -0800483static void mtxMul(float out[16], const float a[16], const float b[16]) {
484 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
485 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
486 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
487 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
488
489 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
490 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
491 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
492 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
493
494 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
495 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
496 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
497 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
498
499 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
500 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
501 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
502 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
503}
504
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800505}; // namespace android