blob: d8dd5bd755b6452076955ecfa04c212d9b15b8d4 [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),
118 mAbandoned(false),
119 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
Daniel Lam6b091c52012-01-22 15:26:27 -0800120{
Daniel Lameae59d22012-01-22 15:26:27 -0800121 // Choose a name using the PID and a process-unique ID.
122 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700123 ST_LOGV("SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800124 if (bufferQueue == 0) {
Daniel Lamb2675792012-02-23 14:35:13 -0800125 ST_LOGV("Creating a new BufferQueue");
126 mBufferQueue = new BufferQueue(allowSynchronousMode);
127 }
128 else {
129 mBufferQueue = bufferQueue;
130 }
Daniel Lamb2675792012-02-23 14:35:13 -0800131
Jamie Gennisfa28c352011-09-16 17:30:26 -0700132 memcpy(mCurrentTransformMatrix, mtxIdentity,
133 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700134
135 // Note that we can't create an sp<...>(this) in a ctor that will not keep a
136 // reference once the ctor ends, as that would cause the refcount of 'this'
137 // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
138 // that's what we create.
139 wp<BufferQueue::ConsumerListener> listener;
140 sp<BufferQueue::ConsumerListener> proxy;
141 listener = static_cast<BufferQueue::ConsumerListener*>(this);
142 proxy = new BufferQueue::ProxyConsumerListener(listener);
143
144 status_t err = mBufferQueue->consumerConnect(proxy);
145 if (err != NO_ERROR) {
146 ST_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)",
147 strerror(-err), err);
148 } else {
149 mBufferQueue->setConsumerName(mName);
150 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800151}
152
153SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700154 ST_LOGV("~SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800155
Daniel Lameae59d22012-01-22 15:26:27 -0800156 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800157}
158
Mathias Agopian80727112011-05-02 19:51:12 -0700159status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
160 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800161 return mBufferQueue->setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700162}
163
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800164
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700165status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
166{
Daniel Lamb2675792012-02-23 14:35:13 -0800167 Mutex::Autolock lock(mMutex);
168 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700169}
170
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800171status_t SurfaceTexture::updateTexImage() {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800172 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700173 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800174 Mutex::Autolock lock(mMutex);
175
Mathias Agopiane47498f2011-08-08 19:35:15 -0700176 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700177 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700178 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700179 }
180
Daniel Lamb2675792012-02-23 14:35:13 -0800181 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800182
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700183 // In asynchronous mode the list is guaranteed to be one buffer
184 // deep, while in synchronous mode we use the oldest buffer.
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700185 if (mBufferQueue->acquireBuffer(&item) == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800186 int buf = item.mBuf;
187 // This buffer was newly allocated, so we need to clean up on our side
188 if (item.mGraphicBuffer != NULL) {
189 mEGLSlots[buf].mGraphicBuffer = 0;
190 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
191 eglDestroyImageKHR(mEGLSlots[buf].mEglDisplay,
192 mEGLSlots[buf].mEglImage);
193 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
194 mEGLSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
195 }
196 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
197 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700198
Jamie Gennisf238e282011-01-09 16:33:17 -0800199 // Update the GL texture object.
Daniel Lameae59d22012-01-22 15:26:27 -0800200 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800201 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800202 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800203 if (item.mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700204 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700205 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700206 }
Daniel Lameae59d22012-01-22 15:26:27 -0800207 image = createImage(dpy, item.mGraphicBuffer);
208 mEGLSlots[buf].mEglImage = image;
209 mEGLSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700210 if (image == EGL_NO_IMAGE_KHR) {
211 // NOTE: if dpy was invalid, createImage() is guaranteed to
212 // fail. so we'd end up here.
213 return -EINVAL;
214 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800215 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800216
217 GLint error;
218 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700219 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800220 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700221
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700222 glBindTexture(mTexTarget, mTexName);
223 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700224
Jamie Gennis0eb88512011-01-26 11:52:02 -0800225 bool failed = false;
226 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700227 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700228 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800229 failed = true;
230 }
231 if (failed) {
Daniel Lamb2675792012-02-23 14:35:13 -0800232 mBufferQueue->releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
Daniel Lameae59d22012-01-22 15:26:27 -0800233 mEGLSlots[buf].mFence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800234 return -EINVAL;
235 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800236
Daniel Lamb2675792012-02-23 14:35:13 -0800237 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800238 if (mUseFenceSync) {
239 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
240 NULL);
241 if (fence == EGL_NO_SYNC_KHR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000242 ALOGE("updateTexImage: error creating fence: %#x",
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800243 eglGetError());
Daniel Lamb2675792012-02-23 14:35:13 -0800244 mBufferQueue->releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
Daniel Lameae59d22012-01-22 15:26:27 -0800245 mEGLSlots[buf].mFence);
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800246 return -EINVAL;
247 }
248 glFlush();
Daniel Lameae59d22012-01-22 15:26:27 -0800249 mEGLSlots[mCurrentTexture].mFence = fence;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800250 }
251 }
252
253 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
254 mCurrentTexture,
255 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800256 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700257
Daniel Lameae59d22012-01-22 15:26:27 -0800258 // release old buffer
Daniel Lamb2675792012-02-23 14:35:13 -0800259 mBufferQueue->releaseBuffer(mCurrentTexture,
Daniel Lameae59d22012-01-22 15:26:27 -0800260 mEGLSlots[mCurrentTexture].mEglDisplay,
261 mEGLSlots[mCurrentTexture].mFence);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700262
Jamie Gennis9a78c902011-01-12 18:30:40 -0800263 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700264 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800265 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
266 mCurrentCrop = item.mCrop;
267 mCurrentTransform = item.mTransform;
268 mCurrentScalingMode = item.mScalingMode;
269 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700270 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700271
272 // Now that we've passed the point at which failures can happen,
273 // it's safe to remove the buffer from the front of the queue.
Daniel Lameae59d22012-01-22 15:26:27 -0800274
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700275 } else {
276 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700277 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800278 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700279
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800280 return OK;
281}
282
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700283bool SurfaceTexture::isExternalFormat(uint32_t format)
284{
285 switch (format) {
286 // supported YUV formats
287 case HAL_PIXEL_FORMAT_YV12:
288 // Legacy/deprecated YUV formats
289 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
290 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
291 case HAL_PIXEL_FORMAT_YCbCr_422_I:
292 return true;
293 }
294
295 // Any OEM format needs to be considered
296 if (format>=0x100 && format<=0x1FF)
297 return true;
298
299 return false;
300}
301
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700302GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700303 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700304}
305
Jamie Gennisf238e282011-01-09 16:33:17 -0800306void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800307 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700308 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
309}
310
311void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700312 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800313
Jamie Gennisa214c642011-01-14 13:53:31 -0800314 float xform[16];
315 for (int i = 0; i < 16; i++) {
316 xform[i] = mtxIdentity[i];
317 }
318 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
319 float result[16];
320 mtxMul(result, xform, mtxFlipH);
321 for (int i = 0; i < 16; i++) {
322 xform[i] = result[i];
323 }
324 }
325 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
326 float result[16];
327 mtxMul(result, xform, mtxFlipV);
328 for (int i = 0; i < 16; i++) {
329 xform[i] = result[i];
330 }
331 }
332 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
333 float result[16];
334 mtxMul(result, xform, mtxRot90);
335 for (int i = 0; i < 16; i++) {
336 xform[i] = result[i];
337 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800338 }
339
Daniel Lameae59d22012-01-22 15:26:27 -0800340 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisa214c642011-01-14 13:53:31 -0800341 float tx, ty, sx, sy;
342 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800343 // In order to prevent bilinear sampling at the of the crop rectangle we
344 // may need to shrink it by 2 texels in each direction. Normally this
345 // would just need to take 1/2 a texel off each end, but because the
346 // chroma channels will likely be subsampled we need to chop off a whole
347 // texel. This will cause artifacts if someone does nearest sampling
348 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
349 // accomodate the bilinear and nearest sampling uses.
350 //
351 // If nearest sampling turns out to be a desirable usage of these
352 // textures then we could add the ability to switch a SurfaceTexture to
353 // nearest-mode. Preferably, however, the image producers (video
354 // decoder, camera, etc.) would simply not use a crop rectangle (or at
355 // least not tell the framework about it) so that the GPU can do the
356 // correct edge behavior.
357 int xshrink = 0, yshrink = 0;
358 if (mCurrentCrop.left > 0) {
359 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
360 xshrink++;
361 } else {
362 tx = 0.0f;
363 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700364 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800365 xshrink++;
366 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700367 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800368 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
369 float(buf->getHeight());
370 yshrink++;
371 } else {
372 ty = 0.0f;
373 }
374 if (mCurrentCrop.top > 0) {
375 yshrink++;
376 }
377 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
378 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800379 } else {
380 tx = 0.0f;
381 ty = 0.0f;
382 sx = 1.0f;
383 sy = 1.0f;
384 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800385 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800386 sx, 0, 0, 0,
387 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800388 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800389 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800390 };
391
Jamie Gennisa214c642011-01-14 13:53:31 -0800392 float mtxBeforeFlipV[16];
393 mtxMul(mtxBeforeFlipV, crop, xform);
394
395 // SurfaceFlinger expects the top of its window textures to be at a Y
396 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
397 // want to expose this to applications, however, so we must add an
398 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700399 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800400}
401
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800402nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700403 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800404 Mutex::Autolock lock(mMutex);
405 return mCurrentTimestamp;
406}
407
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800408void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700409 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700410 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800411 Mutex::Autolock lock(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700412 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800413}
414
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800415EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
416 const sp<GraphicBuffer>& graphicBuffer) {
417 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
418 EGLint attrs[] = {
419 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
420 EGL_NONE,
421 };
422 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
423 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700424 if (image == EGL_NO_IMAGE_KHR) {
425 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700426 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800427 }
428 return image;
429}
430
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700431sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
432 Mutex::Autolock lock(mMutex);
433 return mCurrentTextureBuf;
434}
435
436Rect SurfaceTexture::getCurrentCrop() const {
437 Mutex::Autolock lock(mMutex);
438 return mCurrentCrop;
439}
440
441uint32_t SurfaceTexture::getCurrentTransform() const {
442 Mutex::Autolock lock(mMutex);
443 return mCurrentTransform;
444}
445
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700446uint32_t SurfaceTexture::getCurrentScalingMode() const {
447 Mutex::Autolock lock(mMutex);
448 return mCurrentScalingMode;
449}
450
Jamie Gennis59769462011-11-19 18:04:43 -0800451bool SurfaceTexture::isSynchronousMode() const {
452 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800453 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800454}
455
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700456void SurfaceTexture::freeBufferLocked(int slotIndex) {
457 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
458 mEGLSlots[slotIndex].mGraphicBuffer = 0;
459 if (mEGLSlots[slotIndex].mEglImage != EGL_NO_IMAGE_KHR) {
460 EGLImageKHR img = mEGLSlots[slotIndex].mEglImage;
461 if (img != EGL_NO_IMAGE_KHR) {
462 eglDestroyImageKHR(mEGLSlots[slotIndex].mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800463 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700464 mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
465 mEGLSlots[slotIndex].mEglDisplay = EGL_NO_DISPLAY;
Daniel Lameae59d22012-01-22 15:26:27 -0800466 }
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700467}
Daniel Lameae59d22012-01-22 15:26:27 -0800468
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700469void SurfaceTexture::abandon() {
470 ST_LOGV("abandon");
471 Mutex::Autolock lock(mMutex);
472
473 if (!mAbandoned) {
474 mAbandoned = true;
475 mCurrentTextureBuf.clear();
476
477 // destroy all egl buffers
478 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
479 freeBufferLocked(i);
480 }
481
482 // disconnect from the BufferQueue
483 mBufferQueue->consumerDisconnect();
484 mBufferQueue.clear();
485 }
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700486}
487
Jamie Gennisfa28c352011-09-16 17:30:26 -0700488void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800489 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700490 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800491 mBufferQueue->setConsumerName(name);
492}
493
494status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
495 Mutex::Autolock lock(mMutex);
496 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
497}
498
499status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
500 Mutex::Autolock lock(mMutex);
501 return mBufferQueue->setConsumerUsageBits(usage);
502}
503
504status_t SurfaceTexture::setTransformHint(uint32_t hint) {
505 Mutex::Autolock lock(mMutex);
506 return mBufferQueue->setTransformHint(hint);
507}
508
509// Used for refactoring BufferQueue from SurfaceTexture
510// Should not be in final interface once users of SurfaceTexture are clean up.
511status_t SurfaceTexture::setSynchronousMode(bool enabled) {
512 Mutex::Autolock lock(mMutex);
513 return mBufferQueue->setSynchronousMode(enabled);
514}
515
516// Used for refactoring, should not be in final interface
517sp<BufferQueue> SurfaceTexture::getBufferQueue() const {
518 Mutex::Autolock lock(mMutex);
519 return mBufferQueue;
520}
521
522// Used for refactoring, should not be in final interface
523status_t SurfaceTexture::setBufferCount(int bufferCount) {
524 Mutex::Autolock lock(mMutex);
525 return mBufferQueue->setBufferCount(bufferCount);
526}
527
528// Used for refactoring, should not be in final interface
529status_t SurfaceTexture::connect(int api,
530 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
531 Mutex::Autolock lock(mMutex);
532 return mBufferQueue->connect(api, outWidth, outHeight, outTransform);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700533}
534
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700535void SurfaceTexture::onFrameAvailable() {
536 ST_LOGV("onFrameAvailable");
537
538 sp<FrameAvailableListener> listener;
539 { // scope for the lock
540 Mutex::Autolock lock(mMutex);
541 listener = mFrameAvailableListener;
542 }
543
544 if (listener != NULL) {
545 ST_LOGV("actually calling onFrameAvailable");
546 listener->onFrameAvailable();
547 }
548}
549
550void SurfaceTexture::onBuffersReleased() {
551 ST_LOGV("onBuffersReleased");
552
553 Mutex::Autolock lock(mMutex);
554
555 if (mAbandoned) {
556 // Nothing to do if we're already abandoned.
557 return;
558 }
559
560 uint32_t mask = 0;
561 mBufferQueue->getReleasedBuffers(&mask);
562 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
563 if (mask & (1 << i)) {
564 freeBufferLocked(i);
565 }
566 }
567}
568
Mathias Agopian68c77942011-05-09 19:08:33 -0700569void SurfaceTexture::dump(String8& result) const
570{
571 char buffer[1024];
572 dump(result, "", buffer, 1024);
573}
574
575void SurfaceTexture::dump(String8& result, const char* prefix,
576 char* buffer, size_t SIZE) const
577{
578 Mutex::Autolock _l(mMutex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700579 snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName,
580 int(mAbandoned));
Mathias Agopian68c77942011-05-09 19:08:33 -0700581 result.append(buffer);
582
Mathias Agopian68c77942011-05-09 19:08:33 -0700583 snprintf(buffer, SIZE,
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700584 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n",
585 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700586 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800587 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700588 );
589 result.append(buffer);
590
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700591 if (!mAbandoned) {
592 mBufferQueue->dump(result, prefix, buffer, SIZE);
593 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700594}
595
Jamie Gennisf238e282011-01-09 16:33:17 -0800596static void mtxMul(float out[16], const float a[16], const float b[16]) {
597 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
598 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
599 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
600 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
601
602 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
603 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
604 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
605 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
606
607 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
608 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
609 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
610 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
611
612 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
613 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
614 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
615 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
616}
617
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800618}; // namespace android