blob: 5f0013c06a29ff5b44dc1fb66082519641265ebf [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 Gennise70d8b42011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#define GL_GLEXT_PROTOTYPES
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
Mathias Agopian7a042bf2011-04-11 21:19:55 -070028#include <hardware/hardware.h>
29
Mathias Agopian90ac7992012-02-25 18:48:35 -080030#include <gui/IGraphicBufferAlloc.h>
31#include <gui/ISurfaceComposer.h>
32#include <gui/SurfaceComposerClient.h>
33#include <gui/SurfaceTexture.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080034
Mathias Agopian90ac7992012-02-25 18:48:35 -080035#include <private/gui/ComposerService.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080036
37#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070038#include <utils/String8.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080039
Jamie Gennis86edf4f2011-11-14 14:51:01 -080040// This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension
41// to synchronize access to the buffers. It will cause dequeueBuffer to stall,
42// waiting for the GL reads for the buffer being dequeued to complete before
43// allowing the buffer to be dequeued.
44#ifdef USE_FENCE_SYNC
45#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
46#error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible"
47#endif
48#endif
49
Jamie Gennisfa28c352011-09-16 17:30:26 -070050// Macros for including the SurfaceTexture name in log messages
Steve Block6807e592011-10-20 11:56:00 +010051#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000052#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000053#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000054#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000055#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070056
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057namespace android {
58
Jamie Gennisf238e282011-01-09 16:33:17 -080059// Transform matrices
60static float mtxIdentity[16] = {
61 1, 0, 0, 0,
62 0, 1, 0, 0,
63 0, 0, 1, 0,
64 0, 0, 0, 1,
65};
66static float mtxFlipH[16] = {
67 -1, 0, 0, 0,
68 0, 1, 0, 0,
69 0, 0, 1, 0,
70 1, 0, 0, 1,
71};
72static float mtxFlipV[16] = {
73 1, 0, 0, 0,
74 0, -1, 0, 0,
75 0, 0, 1, 0,
76 0, 1, 0, 1,
77};
78static float mtxRot90[16] = {
79 0, 1, 0, 0,
80 -1, 0, 0, 0,
81 0, 0, 1, 0,
82 1, 0, 0, 1,
83};
84static float mtxRot180[16] = {
85 -1, 0, 0, 0,
86 0, -1, 0, 0,
87 0, 0, 1, 0,
88 1, 1, 0, 1,
89};
90static float mtxRot270[16] = {
91 0, -1, 0, 0,
92 1, 0, 0, 0,
93 0, 0, 1, 0,
94 0, 1, 0, 1,
95};
96
97static void mtxMul(float out[16], const float a[16], const float b[16]);
98
Jamie Gennisfa28c352011-09-16 17:30:26 -070099
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700100SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800101 GLenum texTarget, bool useFenceSync) :
Daniel Lam6b091c52012-01-22 15:26:27 -0800102 BufferQueue(allowSynchronousMode),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800103 mCurrentTransform(0),
104 mCurrentTimestamp(0),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700105 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800106#ifdef USE_FENCE_SYNC
107 mUseFenceSync(useFenceSync),
108#else
109 mUseFenceSync(false),
110#endif
Daniel Lam9b10c472012-02-27 11:32:06 -0800111 mTexTarget(texTarget)
Daniel Lam6b091c52012-01-22 15:26:27 -0800112{
Jamie Gennisfa28c352011-09-16 17:30:26 -0700113
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700114 ST_LOGV("SurfaceTexture");
Jamie Gennisfa28c352011-09-16 17:30:26 -0700115 memcpy(mCurrentTransformMatrix, mtxIdentity,
116 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800117}
118
119SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700120 ST_LOGV("~SurfaceTexture");
Daniel Lam9b10c472012-02-27 11:32:06 -0800121 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800122}
123
Mathias Agopian80727112011-05-02 19:51:12 -0700124status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
125 Mutex::Autolock lock(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800126 return setBufferCountServerLocked(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700127}
128
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700130status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
131{
Daniel Lam9b10c472012-02-27 11:32:06 -0800132 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
133 if (!w || !h) {
134 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
135 w, h);
136 return BAD_VALUE;
137 }
138
139 Mutex::Autolock lock(mMutex);
140 mDefaultWidth = w;
141 mDefaultHeight = h;
142 return OK;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700143}
144
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800145status_t SurfaceTexture::updateTexImage() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700146 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800147 Mutex::Autolock lock(mMutex);
148
Mathias Agopiane47498f2011-08-08 19:35:15 -0700149 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700150 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700151 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700152 }
153
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700154 // In asynchronous mode the list is guaranteed to be one buffer
155 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lam9b10c472012-02-27 11:32:06 -0800156 if (!mQueue.empty()) {
157 Fifo::iterator front(mQueue.begin());
158 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700159
Jamie Gennisf238e282011-01-09 16:33:17 -0800160 // Update the GL texture object.
Daniel Lam9b10c472012-02-27 11:32:06 -0800161 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800162 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800163 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lam9b10c472012-02-27 11:32:06 -0800164 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700165 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700166 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700167 }
Daniel Lam9b10c472012-02-27 11:32:06 -0800168 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
169 mSlots[buf].mEglImage = image;
170 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700171 if (image == EGL_NO_IMAGE_KHR) {
172 // NOTE: if dpy was invalid, createImage() is guaranteed to
173 // fail. so we'd end up here.
174 return -EINVAL;
175 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800176 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800177
178 GLint error;
179 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700180 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800181 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700182
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700183 glBindTexture(mTexTarget, mTexName);
184 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700185
Jamie Gennis0eb88512011-01-26 11:52:02 -0800186 bool failed = false;
187 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700188 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700189 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800190 failed = true;
191 }
192 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800193 return -EINVAL;
194 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800195
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800196 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
197 if (mUseFenceSync) {
198 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
199 NULL);
200 if (fence == EGL_NO_SYNC_KHR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000201 ALOGE("updateTexImage: error creating fence: %#x",
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800202 eglGetError());
203 return -EINVAL;
204 }
205 glFlush();
Daniel Lam9b10c472012-02-27 11:32:06 -0800206 mSlots[mCurrentTexture].mFence = fence;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800207 }
208 }
209
210 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
211 mCurrentTexture,
212 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lam9b10c472012-02-27 11:32:06 -0800213 buf, mSlots[buf].mGraphicBuffer->handle);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700214
Daniel Lam9b10c472012-02-27 11:32:06 -0800215 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
216 // The current buffer becomes FREE if it was still in the queued
217 // state. If it has already been given to the client
218 // (synchronous mode), then it stays in DEQUEUED state.
219 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED) {
220 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
221 }
222 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700223
Jamie Gennis9a78c902011-01-12 18:30:40 -0800224 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700225 mCurrentTexture = buf;
Daniel Lam9b10c472012-02-27 11:32:06 -0800226 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
227 mCurrentCrop = mSlots[buf].mCrop;
228 mCurrentTransform = mSlots[buf].mTransform;
229 mCurrentScalingMode = mSlots[buf].mScalingMode;
230 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700231 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700232
233 // Now that we've passed the point at which failures can happen,
234 // it's safe to remove the buffer from the front of the queue.
Daniel Lam9b10c472012-02-27 11:32:06 -0800235 mQueue.erase(front);
236 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700237 } else {
238 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700239 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800240 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700241
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800242 return OK;
243}
244
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700245bool SurfaceTexture::isExternalFormat(uint32_t format)
246{
247 switch (format) {
248 // supported YUV formats
249 case HAL_PIXEL_FORMAT_YV12:
250 // Legacy/deprecated YUV formats
251 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
252 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
253 case HAL_PIXEL_FORMAT_YCbCr_422_I:
254 return true;
255 }
256
257 // Any OEM format needs to be considered
258 if (format>=0x100 && format<=0x1FF)
259 return true;
260
261 return false;
262}
263
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700264GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700265 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700266}
267
Jamie Gennisf238e282011-01-09 16:33:17 -0800268void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800269 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700270 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
271}
272
273void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700274 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800275
Jamie Gennisa214c642011-01-14 13:53:31 -0800276 float xform[16];
277 for (int i = 0; i < 16; i++) {
278 xform[i] = mtxIdentity[i];
279 }
280 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
281 float result[16];
282 mtxMul(result, xform, mtxFlipH);
283 for (int i = 0; i < 16; i++) {
284 xform[i] = result[i];
285 }
286 }
287 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
288 float result[16];
289 mtxMul(result, xform, mtxFlipV);
290 for (int i = 0; i < 16; i++) {
291 xform[i] = result[i];
292 }
293 }
294 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
295 float result[16];
296 mtxMul(result, xform, mtxRot90);
297 for (int i = 0; i < 16; i++) {
298 xform[i] = result[i];
299 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800300 }
301
Daniel Lam9b10c472012-02-27 11:32:06 -0800302 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800303 float tx, ty, sx, sy;
304 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800305 // In order to prevent bilinear sampling at the of the crop rectangle we
306 // may need to shrink it by 2 texels in each direction. Normally this
307 // would just need to take 1/2 a texel off each end, but because the
308 // chroma channels will likely be subsampled we need to chop off a whole
309 // texel. This will cause artifacts if someone does nearest sampling
310 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
311 // accomodate the bilinear and nearest sampling uses.
312 //
313 // If nearest sampling turns out to be a desirable usage of these
314 // textures then we could add the ability to switch a SurfaceTexture to
315 // nearest-mode. Preferably, however, the image producers (video
316 // decoder, camera, etc.) would simply not use a crop rectangle (or at
317 // least not tell the framework about it) so that the GPU can do the
318 // correct edge behavior.
319 int xshrink = 0, yshrink = 0;
320 if (mCurrentCrop.left > 0) {
321 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
322 xshrink++;
323 } else {
324 tx = 0.0f;
325 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700326 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800327 xshrink++;
328 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700329 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800330 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
331 float(buf->getHeight());
332 yshrink++;
333 } else {
334 ty = 0.0f;
335 }
336 if (mCurrentCrop.top > 0) {
337 yshrink++;
338 }
339 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
340 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800341 } else {
342 tx = 0.0f;
343 ty = 0.0f;
344 sx = 1.0f;
345 sy = 1.0f;
346 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800347 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800348 sx, 0, 0, 0,
349 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800350 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800351 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800352 };
353
Jamie Gennisa214c642011-01-14 13:53:31 -0800354 float mtxBeforeFlipV[16];
355 mtxMul(mtxBeforeFlipV, crop, xform);
356
357 // SurfaceFlinger expects the top of its window textures to be at a Y
358 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
359 // want to expose this to applications, however, so we must add an
360 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700361 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800362}
363
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800364nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700365 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800366 Mutex::Autolock lock(mMutex);
367 return mCurrentTimestamp;
368}
369
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800370void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700371 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700372 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800373 Mutex::Autolock lock(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800374 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800375}
376
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800377EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
378 const sp<GraphicBuffer>& graphicBuffer) {
379 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
380 EGLint attrs[] = {
381 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
382 EGL_NONE,
383 };
384 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
385 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700386 if (image == EGL_NO_IMAGE_KHR) {
387 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700388 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800389 }
390 return image;
391}
392
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700393sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
394 Mutex::Autolock lock(mMutex);
395 return mCurrentTextureBuf;
396}
397
398Rect SurfaceTexture::getCurrentCrop() const {
399 Mutex::Autolock lock(mMutex);
400 return mCurrentCrop;
401}
402
403uint32_t SurfaceTexture::getCurrentTransform() const {
404 Mutex::Autolock lock(mMutex);
405 return mCurrentTransform;
406}
407
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700408uint32_t SurfaceTexture::getCurrentScalingMode() const {
409 Mutex::Autolock lock(mMutex);
410 return mCurrentScalingMode;
411}
412
Jamie Gennis59769462011-11-19 18:04:43 -0800413bool SurfaceTexture::isSynchronousMode() const {
414 Mutex::Autolock lock(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800415 return mSynchronousMode;
Jamie Gennis59769462011-11-19 18:04:43 -0800416}
417
Daniel Lam9b10c472012-02-27 11:32:06 -0800418
419
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700420void SurfaceTexture::abandon() {
421 Mutex::Autolock lock(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800422 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700423 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700424 mCurrentTextureBuf.clear();
Daniel Lam9b10c472012-02-27 11:32:06 -0800425 freeAllBuffersLocked();
426 mDequeueCondition.signal();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700427}
428
Jamie Gennisfa28c352011-09-16 17:30:26 -0700429void SurfaceTexture::setName(const String8& name) {
430 mName = name;
431}
432
Mathias Agopian68c77942011-05-09 19:08:33 -0700433void SurfaceTexture::dump(String8& result) const
434{
435 char buffer[1024];
436 dump(result, "", buffer, 1024);
437}
438
439void SurfaceTexture::dump(String8& result, const char* prefix,
440 char* buffer, size_t SIZE) const
441{
442 Mutex::Autolock _l(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800443 snprintf(buffer, SIZE,
444 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
445 "mPixelFormat=%d, mTexName=%d\n",
446 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
447 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -0700448 result.append(buffer);
449
Daniel Lam9b10c472012-02-27 11:32:06 -0800450 String8 fifo;
451 int fifoSize = 0;
452 Fifo::const_iterator i(mQueue.begin());
453 while (i != mQueue.end()) {
454 snprintf(buffer, SIZE, "%02d ", *i++);
455 fifoSize++;
456 fifo.append(buffer);
457 }
458
Mathias Agopian68c77942011-05-09 19:08:33 -0700459 snprintf(buffer, SIZE,
Daniel Lam9b10c472012-02-27 11:32:06 -0800460 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
461 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
462 ,
463 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700464 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lam9b10c472012-02-27 11:32:06 -0800465 mCurrentTransform, mCurrentTexture,
466 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
467 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -0700468 );
469 result.append(buffer);
470
Daniel Lam9b10c472012-02-27 11:32:06 -0800471 struct {
472 const char * operator()(int state) const {
473 switch (state) {
474 case BufferSlot::DEQUEUED: return "DEQUEUED";
475 case BufferSlot::QUEUED: return "QUEUED";
476 case BufferSlot::FREE: return "FREE";
477 default: return "Unknown";
478 }
479 }
480 } stateName;
Mathias Agopian68c77942011-05-09 19:08:33 -0700481
Daniel Lam9b10c472012-02-27 11:32:06 -0800482 for (int i=0 ; i<mBufferCount ; i++) {
483 const BufferSlot& slot(mSlots[i]);
484 snprintf(buffer, SIZE,
485 "%s%s[%02d] "
486 "state=%-8s, crop=[%d,%d,%d,%d], "
487 "transform=0x%02x, timestamp=%lld",
488 prefix, (i==mCurrentTexture)?">":" ", i,
489 stateName(slot.mBufferState),
490 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
491 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
492 );
493 result.append(buffer);
494
495 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
496 if (buf != NULL) {
497 snprintf(buffer, SIZE,
498 ", %p [%4ux%4u:%4u,%3X]",
499 buf->handle, buf->width, buf->height, buf->stride,
500 buf->format);
501 result.append(buffer);
502 }
503 result.append("\n");
504 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700505}
506
Jamie Gennisf238e282011-01-09 16:33:17 -0800507static void mtxMul(float out[16], const float a[16], const float b[16]) {
508 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
509 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
510 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
511 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
512
513 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
514 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
515 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
516 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
517
518 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
519 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
520 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
521 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
522
523 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
524 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
525 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
526 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
527}
528
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800529}; // namespace android