blob: a7bfc613aa25fa4836d6d1d0db7df1b726c396d3 [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
28#include <gui/SurfaceTexture.h>
29
Mathias Agopian7a042bf2011-04-11 21:19:55 -070030#include <hardware/hardware.h>
31
Mathias Agopian41f673c2011-11-17 17:48:35 -080032#include <private/gui/ComposerService.h>
33
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080034#include <surfaceflinger/ISurfaceComposer.h>
35#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080036#include <surfaceflinger/IGraphicBufferAlloc.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 Gennis8ba32fa2010-12-20 11:27:26 -080040
Jamie Gennis86edf4f2011-11-14 14:51:01 -080041// This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension
42// to synchronize access to the buffers. It will cause dequeueBuffer to stall,
43// waiting for the GL reads for the buffer being dequeued to complete before
44// allowing the buffer to be dequeued.
45#ifdef USE_FENCE_SYNC
46#ifdef ALLOW_DEQUEUE_CURRENT_BUFFER
47#error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible"
48#endif
49#endif
50
Jamie Gennisfa28c352011-09-16 17:30:26 -070051// Macros for including the SurfaceTexture name in log messages
Steve Block6807e592011-10-20 11:56:00 +010052#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000053#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000054#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000055#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000056#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070057
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080058namespace android {
59
Jamie Gennisf238e282011-01-09 16:33:17 -080060// Transform matrices
61static float mtxIdentity[16] = {
62 1, 0, 0, 0,
63 0, 1, 0, 0,
64 0, 0, 1, 0,
65 0, 0, 0, 1,
66};
67static float mtxFlipH[16] = {
68 -1, 0, 0, 0,
69 0, 1, 0, 0,
70 0, 0, 1, 0,
71 1, 0, 0, 1,
72};
73static float mtxFlipV[16] = {
74 1, 0, 0, 0,
75 0, -1, 0, 0,
76 0, 0, 1, 0,
77 0, 1, 0, 1,
78};
79static float mtxRot90[16] = {
80 0, 1, 0, 0,
81 -1, 0, 0, 0,
82 0, 0, 1, 0,
83 1, 0, 0, 1,
84};
85static float mtxRot180[16] = {
86 -1, 0, 0, 0,
87 0, -1, 0, 0,
88 0, 0, 1, 0,
89 1, 1, 0, 1,
90};
91static float mtxRot270[16] = {
92 0, -1, 0, 0,
93 1, 0, 0, 0,
94 0, 0, 1, 0,
95 0, 1, 0, 1,
96};
97
98static void mtxMul(float out[16], const float a[16], const float b[16]);
99
Jamie Gennisfa28c352011-09-16 17:30:26 -0700100
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700101SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800102 GLenum texTarget, bool useFenceSync) :
Daniel Lam6b091c52012-01-22 15:26:27 -0800103 BufferQueue(allowSynchronousMode),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800104 mCurrentTransform(0),
105 mCurrentTimestamp(0),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700106 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800107#ifdef USE_FENCE_SYNC
108 mUseFenceSync(useFenceSync),
109#else
110 mUseFenceSync(false),
111#endif
Daniel Lam9b10c472012-02-27 11:32:06 -0800112 mTexTarget(texTarget)
Daniel Lam6b091c52012-01-22 15:26:27 -0800113{
Jamie Gennisfa28c352011-09-16 17:30:26 -0700114
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700115 ST_LOGV("SurfaceTexture");
Jamie Gennisfa28c352011-09-16 17:30:26 -0700116 memcpy(mCurrentTransformMatrix, mtxIdentity,
117 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800118}
119
120SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700121 ST_LOGV("~SurfaceTexture");
Daniel Lam9b10c472012-02-27 11:32:06 -0800122 freeAllBuffersLocked();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800123}
124
Mathias Agopian80727112011-05-02 19:51:12 -0700125status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
126 Mutex::Autolock lock(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800127 return setBufferCountServerLocked(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700128}
129
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800130
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700131status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
132{
Daniel Lam9b10c472012-02-27 11:32:06 -0800133 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
134 if (!w || !h) {
135 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
136 w, h);
137 return BAD_VALUE;
138 }
139
140 Mutex::Autolock lock(mMutex);
141 mDefaultWidth = w;
142 mDefaultHeight = h;
143 return OK;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700144}
145
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800146status_t SurfaceTexture::updateTexImage() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700147 ST_LOGV("updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800148 Mutex::Autolock lock(mMutex);
149
Mathias Agopiane47498f2011-08-08 19:35:15 -0700150 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700151 ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700152 return NO_INIT;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700153 }
154
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700155 // In asynchronous mode the list is guaranteed to be one buffer
156 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lam9b10c472012-02-27 11:32:06 -0800157 if (!mQueue.empty()) {
158 Fifo::iterator front(mQueue.begin());
159 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700160
Jamie Gennisf238e282011-01-09 16:33:17 -0800161 // Update the GL texture object.
Daniel Lam9b10c472012-02-27 11:32:06 -0800162 EGLImageKHR image = mSlots[buf].mEglImage;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800163 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800164 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lam9b10c472012-02-27 11:32:06 -0800165 if (mSlots[buf].mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700166 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700167 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700168 }
Daniel Lam9b10c472012-02-27 11:32:06 -0800169 image = createImage(dpy, mSlots[buf].mGraphicBuffer);
170 mSlots[buf].mEglImage = image;
171 mSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700172 if (image == EGL_NO_IMAGE_KHR) {
173 // NOTE: if dpy was invalid, createImage() is guaranteed to
174 // fail. so we'd end up here.
175 return -EINVAL;
176 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800177 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800178
179 GLint error;
180 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700181 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800182 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700183
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700184 glBindTexture(mTexTarget, mTexName);
185 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700186
Jamie Gennis0eb88512011-01-26 11:52:02 -0800187 bool failed = false;
188 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700189 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700190 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800191 failed = true;
192 }
193 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800194 return -EINVAL;
195 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800196
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800197 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
198 if (mUseFenceSync) {
199 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
200 NULL);
201 if (fence == EGL_NO_SYNC_KHR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000202 ALOGE("updateTexImage: error creating fence: %#x",
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800203 eglGetError());
204 return -EINVAL;
205 }
206 glFlush();
Daniel Lam9b10c472012-02-27 11:32:06 -0800207 mSlots[mCurrentTexture].mFence = fence;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800208 }
209 }
210
211 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
212 mCurrentTexture,
213 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lam9b10c472012-02-27 11:32:06 -0800214 buf, mSlots[buf].mGraphicBuffer->handle);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700215
Daniel Lam9b10c472012-02-27 11:32:06 -0800216 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
217 // The current buffer becomes FREE if it was still in the queued
218 // state. If it has already been given to the client
219 // (synchronous mode), then it stays in DEQUEUED state.
220 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED) {
221 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
222 }
223 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700224
Jamie Gennis9a78c902011-01-12 18:30:40 -0800225 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700226 mCurrentTexture = buf;
Daniel Lam9b10c472012-02-27 11:32:06 -0800227 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
228 mCurrentCrop = mSlots[buf].mCrop;
229 mCurrentTransform = mSlots[buf].mTransform;
230 mCurrentScalingMode = mSlots[buf].mScalingMode;
231 mCurrentTimestamp = mSlots[buf].mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700232 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700233
234 // Now that we've passed the point at which failures can happen,
235 // it's safe to remove the buffer from the front of the queue.
Daniel Lam9b10c472012-02-27 11:32:06 -0800236 mQueue.erase(front);
237 mDequeueCondition.signal();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700238 } else {
239 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700240 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800241 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700242
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800243 return OK;
244}
245
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700246bool SurfaceTexture::isExternalFormat(uint32_t format)
247{
248 switch (format) {
249 // supported YUV formats
250 case HAL_PIXEL_FORMAT_YV12:
251 // Legacy/deprecated YUV formats
252 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
253 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
254 case HAL_PIXEL_FORMAT_YCbCr_422_I:
255 return true;
256 }
257
258 // Any OEM format needs to be considered
259 if (format>=0x100 && format<=0x1FF)
260 return true;
261
262 return false;
263}
264
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700265GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700266 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700267}
268
Jamie Gennisf238e282011-01-09 16:33:17 -0800269void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800270 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700271 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
272}
273
274void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700275 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800276
Jamie Gennisa214c642011-01-14 13:53:31 -0800277 float xform[16];
278 for (int i = 0; i < 16; i++) {
279 xform[i] = mtxIdentity[i];
280 }
281 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
282 float result[16];
283 mtxMul(result, xform, mtxFlipH);
284 for (int i = 0; i < 16; i++) {
285 xform[i] = result[i];
286 }
287 }
288 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
289 float result[16];
290 mtxMul(result, xform, mtxFlipV);
291 for (int i = 0; i < 16; i++) {
292 xform[i] = result[i];
293 }
294 }
295 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
296 float result[16];
297 mtxMul(result, xform, mtxRot90);
298 for (int i = 0; i < 16; i++) {
299 xform[i] = result[i];
300 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800301 }
302
Daniel Lam9b10c472012-02-27 11:32:06 -0800303 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800304 float tx, ty, sx, sy;
305 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800306 // In order to prevent bilinear sampling at the of the crop rectangle we
307 // may need to shrink it by 2 texels in each direction. Normally this
308 // would just need to take 1/2 a texel off each end, but because the
309 // chroma channels will likely be subsampled we need to chop off a whole
310 // texel. This will cause artifacts if someone does nearest sampling
311 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
312 // accomodate the bilinear and nearest sampling uses.
313 //
314 // If nearest sampling turns out to be a desirable usage of these
315 // textures then we could add the ability to switch a SurfaceTexture to
316 // nearest-mode. Preferably, however, the image producers (video
317 // decoder, camera, etc.) would simply not use a crop rectangle (or at
318 // least not tell the framework about it) so that the GPU can do the
319 // correct edge behavior.
320 int xshrink = 0, yshrink = 0;
321 if (mCurrentCrop.left > 0) {
322 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
323 xshrink++;
324 } else {
325 tx = 0.0f;
326 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700327 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800328 xshrink++;
329 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700330 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800331 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
332 float(buf->getHeight());
333 yshrink++;
334 } else {
335 ty = 0.0f;
336 }
337 if (mCurrentCrop.top > 0) {
338 yshrink++;
339 }
340 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
341 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800342 } else {
343 tx = 0.0f;
344 ty = 0.0f;
345 sx = 1.0f;
346 sy = 1.0f;
347 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800348 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800349 sx, 0, 0, 0,
350 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800351 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800352 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800353 };
354
Jamie Gennisa214c642011-01-14 13:53:31 -0800355 float mtxBeforeFlipV[16];
356 mtxMul(mtxBeforeFlipV, crop, xform);
357
358 // SurfaceFlinger expects the top of its window textures to be at a Y
359 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
360 // want to expose this to applications, however, so we must add an
361 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700362 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800363}
364
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800365nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700366 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800367 Mutex::Autolock lock(mMutex);
368 return mCurrentTimestamp;
369}
370
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800371void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700372 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700373 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800374 Mutex::Autolock lock(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800375 mFrameAvailableListener = listener;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800376}
377
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800378EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
379 const sp<GraphicBuffer>& graphicBuffer) {
380 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
381 EGLint attrs[] = {
382 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
383 EGL_NONE,
384 };
385 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
386 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700387 if (image == EGL_NO_IMAGE_KHR) {
388 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700389 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800390 }
391 return image;
392}
393
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700394sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
395 Mutex::Autolock lock(mMutex);
396 return mCurrentTextureBuf;
397}
398
399Rect SurfaceTexture::getCurrentCrop() const {
400 Mutex::Autolock lock(mMutex);
401 return mCurrentCrop;
402}
403
404uint32_t SurfaceTexture::getCurrentTransform() const {
405 Mutex::Autolock lock(mMutex);
406 return mCurrentTransform;
407}
408
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700409uint32_t SurfaceTexture::getCurrentScalingMode() const {
410 Mutex::Autolock lock(mMutex);
411 return mCurrentScalingMode;
412}
413
Jamie Gennis59769462011-11-19 18:04:43 -0800414bool SurfaceTexture::isSynchronousMode() const {
415 Mutex::Autolock lock(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800416 return mSynchronousMode;
Jamie Gennis59769462011-11-19 18:04:43 -0800417}
418
Daniel Lam9b10c472012-02-27 11:32:06 -0800419
420
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700421void SurfaceTexture::abandon() {
422 Mutex::Autolock lock(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800423 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700424 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700425 mCurrentTextureBuf.clear();
Daniel Lam9b10c472012-02-27 11:32:06 -0800426 freeAllBuffersLocked();
427 mDequeueCondition.signal();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700428}
429
Jamie Gennisfa28c352011-09-16 17:30:26 -0700430void SurfaceTexture::setName(const String8& name) {
431 mName = name;
432}
433
Mathias Agopian68c77942011-05-09 19:08:33 -0700434void SurfaceTexture::dump(String8& result) const
435{
436 char buffer[1024];
437 dump(result, "", buffer, 1024);
438}
439
440void SurfaceTexture::dump(String8& result, const char* prefix,
441 char* buffer, size_t SIZE) const
442{
443 Mutex::Autolock _l(mMutex);
Daniel Lam9b10c472012-02-27 11:32:06 -0800444 snprintf(buffer, SIZE,
445 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
446 "mPixelFormat=%d, mTexName=%d\n",
447 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
448 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -0700449 result.append(buffer);
450
Daniel Lam9b10c472012-02-27 11:32:06 -0800451 String8 fifo;
452 int fifoSize = 0;
453 Fifo::const_iterator i(mQueue.begin());
454 while (i != mQueue.end()) {
455 snprintf(buffer, SIZE, "%02d ", *i++);
456 fifoSize++;
457 fifo.append(buffer);
458 }
459
Mathias Agopian68c77942011-05-09 19:08:33 -0700460 snprintf(buffer, SIZE,
Daniel Lam9b10c472012-02-27 11:32:06 -0800461 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
462 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
463 ,
464 prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700465 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lam9b10c472012-02-27 11:32:06 -0800466 mCurrentTransform, mCurrentTexture,
467 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
468 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -0700469 );
470 result.append(buffer);
471
Daniel Lam9b10c472012-02-27 11:32:06 -0800472 struct {
473 const char * operator()(int state) const {
474 switch (state) {
475 case BufferSlot::DEQUEUED: return "DEQUEUED";
476 case BufferSlot::QUEUED: return "QUEUED";
477 case BufferSlot::FREE: return "FREE";
478 default: return "Unknown";
479 }
480 }
481 } stateName;
Mathias Agopian68c77942011-05-09 19:08:33 -0700482
Daniel Lam9b10c472012-02-27 11:32:06 -0800483 for (int i=0 ; i<mBufferCount ; i++) {
484 const BufferSlot& slot(mSlots[i]);
485 snprintf(buffer, SIZE,
486 "%s%s[%02d] "
487 "state=%-8s, crop=[%d,%d,%d,%d], "
488 "transform=0x%02x, timestamp=%lld",
489 prefix, (i==mCurrentTexture)?">":" ", i,
490 stateName(slot.mBufferState),
491 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
492 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
493 );
494 result.append(buffer);
495
496 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
497 if (buf != NULL) {
498 snprintf(buffer, SIZE,
499 ", %p [%4ux%4u:%4u,%3X]",
500 buf->handle, buf->width, buf->height, buf->stride,
501 buf->format);
502 result.append(buffer);
503 }
504 result.append("\n");
505 }
Mathias Agopian68c77942011-05-09 19:08:33 -0700506}
507
Jamie Gennisf238e282011-01-09 16:33:17 -0800508static void mtxMul(float out[16], const float a[16], const float b[16]) {
509 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
510 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
511 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
512 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
513
514 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
515 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
516 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
517 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
518
519 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
520 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
521 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
522 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
523
524 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
525 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
526 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
527 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
528}
529
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800530}; // namespace android