blob: 5fb933be191fb56f6eb530566dcddc11747eeffb [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) {
125
126 ST_LOGV("Creating a new BufferQueue");
127 mBufferQueue = new BufferQueue(allowSynchronousMode);
128 }
129 else {
130 mBufferQueue = bufferQueue;
131 }
132 mBufferQueue->setConsumerName(mName);
133
Jamie Gennisfa28c352011-09-16 17:30:26 -0700134 memcpy(mCurrentTransformMatrix, mtxIdentity,
135 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800136}
137
138SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700139 ST_LOGV("~SurfaceTexture");
Daniel Lamb2675792012-02-23 14:35:13 -0800140
Daniel Lameae59d22012-01-22 15:26:27 -0800141 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800142}
143
Mathias Agopian80727112011-05-02 19:51:12 -0700144status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
145 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800146 return mBufferQueue->setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700147}
148
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800149
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700150status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
151{
Daniel Lamb2675792012-02-23 14:35:13 -0800152 Mutex::Autolock lock(mMutex);
153 return mBufferQueue->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700154}
155
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800156status_t SurfaceTexture::updateTexImage() {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800157 ATRACE_CALL();
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700158 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800159 Mutex::Autolock lock(mMutex);
160
Mathias Agopiane47498f2011-08-08 19:35:15 -0700161 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700162 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700163 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700164 }
165
Daniel Lamb2675792012-02-23 14:35:13 -0800166 BufferQueue::BufferItem item;
Daniel Lameae59d22012-01-22 15:26:27 -0800167
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700168 // In asynchronous mode the list is guaranteed to be one buffer
169 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lamb2675792012-02-23 14:35:13 -0800170 if (mBufferQueue->acquire(&item) == NO_ERROR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800171 int buf = item.mBuf;
172 // This buffer was newly allocated, so we need to clean up on our side
173 if (item.mGraphicBuffer != NULL) {
174 mEGLSlots[buf].mGraphicBuffer = 0;
175 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
176 eglDestroyImageKHR(mEGLSlots[buf].mEglDisplay,
177 mEGLSlots[buf].mEglImage);
178 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
179 mEGLSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
180 }
181 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
182 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700183
Jamie Gennisf238e282011-01-09 16:33:17 -0800184 // Update the GL texture object.
Daniel Lameae59d22012-01-22 15:26:27 -0800185 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800186 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800187 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800188 if (item.mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700189 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700190 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700191 }
Daniel Lameae59d22012-01-22 15:26:27 -0800192 image = createImage(dpy, item.mGraphicBuffer);
193 mEGLSlots[buf].mEglImage = image;
194 mEGLSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700195 if (image == EGL_NO_IMAGE_KHR) {
196 // NOTE: if dpy was invalid, createImage() is guaranteed to
197 // fail. so we'd end up here.
198 return -EINVAL;
199 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800200 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800201
202 GLint error;
203 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700204 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800205 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700206
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700207 glBindTexture(mTexTarget, mTexName);
208 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700209
Jamie Gennis0eb88512011-01-26 11:52:02 -0800210 bool failed = false;
211 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700212 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700213 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800214 failed = true;
215 }
216 if (failed) {
Daniel Lamb2675792012-02-23 14:35:13 -0800217 mBufferQueue->releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
Daniel Lameae59d22012-01-22 15:26:27 -0800218 mEGLSlots[buf].mFence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 return -EINVAL;
220 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800221
Daniel Lamb2675792012-02-23 14:35:13 -0800222 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800223 if (mUseFenceSync) {
224 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
225 NULL);
226 if (fence == EGL_NO_SYNC_KHR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000227 ALOGE("updateTexImage: error creating fence: %#x",
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800228 eglGetError());
Daniel Lamb2675792012-02-23 14:35:13 -0800229 mBufferQueue->releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
Daniel Lameae59d22012-01-22 15:26:27 -0800230 mEGLSlots[buf].mFence);
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800231 return -EINVAL;
232 }
233 glFlush();
Daniel Lameae59d22012-01-22 15:26:27 -0800234 mEGLSlots[mCurrentTexture].mFence = fence;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800235 }
236 }
237
238 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
239 mCurrentTexture,
240 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800241 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700242
Daniel Lameae59d22012-01-22 15:26:27 -0800243 // release old buffer
Daniel Lamb2675792012-02-23 14:35:13 -0800244 mBufferQueue->releaseBuffer(mCurrentTexture,
Daniel Lameae59d22012-01-22 15:26:27 -0800245 mEGLSlots[mCurrentTexture].mEglDisplay,
246 mEGLSlots[mCurrentTexture].mFence);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700247
Jamie Gennis9a78c902011-01-12 18:30:40 -0800248 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700249 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800250 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
251 mCurrentCrop = item.mCrop;
252 mCurrentTransform = item.mTransform;
253 mCurrentScalingMode = item.mScalingMode;
254 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700255 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700256
257 // Now that we've passed the point at which failures can happen,
258 // it's safe to remove the buffer from the front of the queue.
Daniel Lameae59d22012-01-22 15:26:27 -0800259
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700260 } else {
261 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700262 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800263 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700264
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800265 return OK;
266}
267
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700268bool SurfaceTexture::isExternalFormat(uint32_t format)
269{
270 switch (format) {
271 // supported YUV formats
272 case HAL_PIXEL_FORMAT_YV12:
273 // Legacy/deprecated YUV formats
274 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
275 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
276 case HAL_PIXEL_FORMAT_YCbCr_422_I:
277 return true;
278 }
279
280 // Any OEM format needs to be considered
281 if (format>=0x100 && format<=0x1FF)
282 return true;
283
284 return false;
285}
286
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700287GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700288 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700289}
290
Jamie Gennisf238e282011-01-09 16:33:17 -0800291void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800292 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700293 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
294}
295
296void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700297 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800298
Jamie Gennisa214c642011-01-14 13:53:31 -0800299 float xform[16];
300 for (int i = 0; i < 16; i++) {
301 xform[i] = mtxIdentity[i];
302 }
303 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
304 float result[16];
305 mtxMul(result, xform, mtxFlipH);
306 for (int i = 0; i < 16; i++) {
307 xform[i] = result[i];
308 }
309 }
310 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
311 float result[16];
312 mtxMul(result, xform, mtxFlipV);
313 for (int i = 0; i < 16; i++) {
314 xform[i] = result[i];
315 }
316 }
317 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
318 float result[16];
319 mtxMul(result, xform, mtxRot90);
320 for (int i = 0; i < 16; i++) {
321 xform[i] = result[i];
322 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800323 }
324
Daniel Lameae59d22012-01-22 15:26:27 -0800325 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisa214c642011-01-14 13:53:31 -0800326 float tx, ty, sx, sy;
327 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800328 // In order to prevent bilinear sampling at the of the crop rectangle we
329 // may need to shrink it by 2 texels in each direction. Normally this
330 // would just need to take 1/2 a texel off each end, but because the
331 // chroma channels will likely be subsampled we need to chop off a whole
332 // texel. This will cause artifacts if someone does nearest sampling
333 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
334 // accomodate the bilinear and nearest sampling uses.
335 //
336 // If nearest sampling turns out to be a desirable usage of these
337 // textures then we could add the ability to switch a SurfaceTexture to
338 // nearest-mode. Preferably, however, the image producers (video
339 // decoder, camera, etc.) would simply not use a crop rectangle (or at
340 // least not tell the framework about it) so that the GPU can do the
341 // correct edge behavior.
342 int xshrink = 0, yshrink = 0;
343 if (mCurrentCrop.left > 0) {
344 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
345 xshrink++;
346 } else {
347 tx = 0.0f;
348 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700349 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800350 xshrink++;
351 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700352 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800353 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
354 float(buf->getHeight());
355 yshrink++;
356 } else {
357 ty = 0.0f;
358 }
359 if (mCurrentCrop.top > 0) {
360 yshrink++;
361 }
362 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
363 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800364 } else {
365 tx = 0.0f;
366 ty = 0.0f;
367 sx = 1.0f;
368 sy = 1.0f;
369 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800370 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800371 sx, 0, 0, 0,
372 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800373 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800374 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800375 };
376
Jamie Gennisa214c642011-01-14 13:53:31 -0800377 float mtxBeforeFlipV[16];
378 mtxMul(mtxBeforeFlipV, crop, xform);
379
380 // SurfaceFlinger expects the top of its window textures to be at a Y
381 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
382 // want to expose this to applications, however, so we must add an
383 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700384 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800385}
386
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800387nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700388 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800389 Mutex::Autolock lock(mMutex);
390 return mCurrentTimestamp;
391}
392
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800393void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700394 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700395 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800396 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800397 mBufferQueue->setFrameAvailableListener(listener);
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800398}
399
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800400EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
401 const sp<GraphicBuffer>& graphicBuffer) {
402 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
403 EGLint attrs[] = {
404 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
405 EGL_NONE,
406 };
407 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
408 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700409 if (image == EGL_NO_IMAGE_KHR) {
410 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700411 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800412 }
413 return image;
414}
415
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700416sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
417 Mutex::Autolock lock(mMutex);
418 return mCurrentTextureBuf;
419}
420
421Rect SurfaceTexture::getCurrentCrop() const {
422 Mutex::Autolock lock(mMutex);
423 return mCurrentCrop;
424}
425
426uint32_t SurfaceTexture::getCurrentTransform() const {
427 Mutex::Autolock lock(mMutex);
428 return mCurrentTransform;
429}
430
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700431uint32_t SurfaceTexture::getCurrentScalingMode() const {
432 Mutex::Autolock lock(mMutex);
433 return mCurrentScalingMode;
434}
435
Jamie Gennis59769462011-11-19 18:04:43 -0800436bool SurfaceTexture::isSynchronousMode() const {
437 Mutex::Autolock lock(mMutex);
Daniel Lamb2675792012-02-23 14:35:13 -0800438 return mBufferQueue->isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800439}
440
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700441void SurfaceTexture::abandon() {
442 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700443 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700444 mCurrentTextureBuf.clear();
Daniel Lameae59d22012-01-22 15:26:27 -0800445
446 // destroy all egl buffers
Daniel Lamb2675792012-02-23 14:35:13 -0800447 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Daniel Lameae59d22012-01-22 15:26:27 -0800448 mEGLSlots[i].mGraphicBuffer = 0;
449 if (mEGLSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
450 eglDestroyImageKHR(mEGLSlots[i].mEglDisplay,
451 mEGLSlots[i].mEglImage);
452 mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
453 mEGLSlots[i].mEglDisplay = EGL_NO_DISPLAY;
454 }
455 }
456
457 // disconnect from the BufferQueue
Daniel Lamb2675792012-02-23 14:35:13 -0800458 mBufferQueue->consumerDisconnect();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700459}
460
Jamie Gennisfa28c352011-09-16 17:30:26 -0700461void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800462 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700463 mName = name;
Daniel Lamb2675792012-02-23 14:35:13 -0800464 mBufferQueue->setConsumerName(name);
465}
466
467status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) {
468 Mutex::Autolock lock(mMutex);
469 return mBufferQueue->setDefaultBufferFormat(defaultFormat);
470}
471
472status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) {
473 Mutex::Autolock lock(mMutex);
474 return mBufferQueue->setConsumerUsageBits(usage);
475}
476
477status_t SurfaceTexture::setTransformHint(uint32_t hint) {
478 Mutex::Autolock lock(mMutex);
479 return mBufferQueue->setTransformHint(hint);
480}
481
482// Used for refactoring BufferQueue from SurfaceTexture
483// Should not be in final interface once users of SurfaceTexture are clean up.
484status_t SurfaceTexture::setSynchronousMode(bool enabled) {
485 Mutex::Autolock lock(mMutex);
486 return mBufferQueue->setSynchronousMode(enabled);
487}
488
489// Used for refactoring, should not be in final interface
490sp<BufferQueue> SurfaceTexture::getBufferQueue() const {
491 Mutex::Autolock lock(mMutex);
492 return mBufferQueue;
493}
494
495// Used for refactoring, should not be in final interface
496status_t SurfaceTexture::setBufferCount(int bufferCount) {
497 Mutex::Autolock lock(mMutex);
498 return mBufferQueue->setBufferCount(bufferCount);
499}
500
501// Used for refactoring, should not be in final interface
502status_t SurfaceTexture::connect(int api,
503 uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
504 Mutex::Autolock lock(mMutex);
505 return mBufferQueue->connect(api, outWidth, outHeight, outTransform);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700506}
507
Mathias Agopian68c77942011-05-09 19:08:33 -0700508void SurfaceTexture::dump(String8& result) const
509{
510 char buffer[1024];
511 dump(result, "", buffer, 1024);
512}
513
514void SurfaceTexture::dump(String8& result, const char* prefix,
515 char* buffer, size_t SIZE) const
516{
517 Mutex::Autolock _l(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800518 snprintf(buffer, SIZE, "%smTexName=%d\n", prefix, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -0700519 result.append(buffer);
520
Mathias Agopian68c77942011-05-09 19:08:33 -0700521 snprintf(buffer, SIZE,
Daniel Lameae59d22012-01-22 15:26:27 -0800522 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
523 ,prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700524 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800525 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700526 );
527 result.append(buffer);
528
Mathias Agopian68c77942011-05-09 19:08:33 -0700529
Daniel Lamb2675792012-02-23 14:35:13 -0800530 mBufferQueue->dump(result, prefix, buffer, SIZE);
Mathias Agopian68c77942011-05-09 19:08:33 -0700531}
532
Jamie Gennisf238e282011-01-09 16:33:17 -0800533static void mtxMul(float out[16], const float a[16], const float b[16]) {
534 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
535 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
536 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
537 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
538
539 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
540 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
541 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
542 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
543
544 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
545 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
546 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
547 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
548
549 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
550 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
551 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
552 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
553}
554
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800555}; // namespace android