blob: be1bcd1a8104dd092c05b2bfee3085f0dbb09e65 [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 Lam6b091c52012-01-22 15:26:27 -0800112 mTexTarget(texTarget)
113{
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");
Mathias Agopianef51b992011-08-10 15:28:58 -0700122 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);
127 return setBufferCountServerLocked(bufferCount);
128}
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{
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700133 ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700134 if (!w || !h) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700135 ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)",
136 w, h);
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700137 return BAD_VALUE;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700138 }
Mathias Agopian3fbce7c2011-07-25 19:56:08 -0700139
140 Mutex::Autolock lock(mMutex);
141 mDefaultWidth = w;
142 mDefaultHeight = h;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700143 return OK;
144}
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.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700157 if (!mQueue.empty()) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700158 Fifo::iterator front(mQueue.begin());
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700159 int buf = *front;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700160
Jamie Gennisf238e282011-01-09 16:33:17 -0800161 // Update the GL texture object.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700162 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) {
Mathias Agopiane47498f2011-08-08 19:35:15 -0700165 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 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700169 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();
207 mSlots[mCurrentTexture].mFence = fence;
208 }
209 }
210
211 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
212 mCurrentTexture,
213 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
214 buf, mSlots[buf].mGraphicBuffer->handle);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700215
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700216 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700217 // The current buffer becomes FREE if it was still in the queued
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700218 // state. If it has already been given to the client
219 // (synchronous mode), then it stays in DEQUEUED state.
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800220 if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED) {
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700221 mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800222 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700223 }
224
Jamie Gennis9a78c902011-01-12 18:30:40 -0800225 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700226 mCurrentTexture = buf;
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700227 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700228 mCurrentCrop = mSlots[buf].mCrop;
229 mCurrentTransform = mSlots[buf].mTransform;
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700230 mCurrentScalingMode = mSlots[buf].mScalingMode;
Jamie Gennis8cd5ba42011-05-19 13:33:00 -0700231 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.
236 mQueue.erase(front);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700237 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
303 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);
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700375 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);
416 return mSynchronousMode;
417}
418
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700419int SurfaceTexture::query(int what, int* outValue)
420{
421 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700422
423 if (mAbandoned) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700424 ST_LOGE("query: SurfaceTexture has been abandoned!");
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700425 return NO_INIT;
426 }
427
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700428 int value;
429 switch (what) {
430 case NATIVE_WINDOW_WIDTH:
431 value = mDefaultWidth;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700432 break;
433 case NATIVE_WINDOW_HEIGHT:
434 value = mDefaultHeight;
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700435 break;
436 case NATIVE_WINDOW_FORMAT:
437 value = mPixelFormat;
438 break;
439 case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
440 value = mSynchronousMode ?
441 (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
442 break;
443 default:
444 return BAD_VALUE;
445 }
446 outValue[0] = value;
447 return NO_ERROR;
448}
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700449
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700450void SurfaceTexture::abandon() {
451 Mutex::Autolock lock(mMutex);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700452 mQueue.clear();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700453 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700454 mCurrentTextureBuf.clear();
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700455 freeAllBuffersLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700456 mDequeueCondition.signal();
457}
458
Jamie Gennisfa28c352011-09-16 17:30:26 -0700459void SurfaceTexture::setName(const String8& name) {
460 mName = name;
461}
462
Mathias Agopian68c77942011-05-09 19:08:33 -0700463void SurfaceTexture::dump(String8& result) const
464{
465 char buffer[1024];
466 dump(result, "", buffer, 1024);
467}
468
469void SurfaceTexture::dump(String8& result, const char* prefix,
470 char* buffer, size_t SIZE) const
471{
472 Mutex::Autolock _l(mMutex);
473 snprintf(buffer, SIZE,
474 "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
475 "mPixelFormat=%d, mTexName=%d\n",
Jamie Gennisfa28c352011-09-16 17:30:26 -0700476 prefix, mBufferCount, mSynchronousMode, mDefaultWidth,
477 mDefaultHeight, mPixelFormat, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -0700478 result.append(buffer);
479
480 String8 fifo;
481 int fifoSize = 0;
482 Fifo::const_iterator i(mQueue.begin());
483 while (i != mQueue.end()) {
484 snprintf(buffer, SIZE, "%02d ", *i++);
485 fifoSize++;
486 fifo.append(buffer);
487 }
488
489 snprintf(buffer, SIZE,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700490 "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
Mathias Agopian68c77942011-05-09 19:08:33 -0700491 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
492 ,
493 prefix, mCurrentCrop.left,
494 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700495 mCurrentTransform, mCurrentTexture,
Jamie Gennisfa28c352011-09-16 17:30:26 -0700496 prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right,
497 mNextCrop.bottom, mNextTransform, fifoSize, fifo.string()
Mathias Agopian68c77942011-05-09 19:08:33 -0700498 );
499 result.append(buffer);
500
501 struct {
502 const char * operator()(int state) const {
503 switch (state) {
504 case BufferSlot::DEQUEUED: return "DEQUEUED";
505 case BufferSlot::QUEUED: return "QUEUED";
506 case BufferSlot::FREE: return "FREE";
507 default: return "Unknown";
508 }
509 }
510 } stateName;
511
512 for (int i=0 ; i<mBufferCount ; i++) {
513 const BufferSlot& slot(mSlots[i]);
514 snprintf(buffer, SIZE,
Mathias Agopianad795ba2011-08-08 16:02:13 -0700515 "%s%s[%02d] "
Mathias Agopianad795ba2011-08-08 16:02:13 -0700516 "state=%-8s, crop=[%d,%d,%d,%d], "
Mathias Agopian2db6f0a2011-08-09 15:48:43 -0700517 "transform=0x%02x, timestamp=%lld",
Mathias Agopianad795ba2011-08-08 16:02:13 -0700518 prefix, (i==mCurrentTexture)?">":" ", i,
Mathias Agopianad795ba2011-08-08 16:02:13 -0700519 stateName(slot.mBufferState),
Jamie Gennisfa28c352011-09-16 17:30:26 -0700520 slot.mCrop.left, slot.mCrop.top, slot.mCrop.right,
521 slot.mCrop.bottom, slot.mTransform, slot.mTimestamp
Mathias Agopian68c77942011-05-09 19:08:33 -0700522 );
523 result.append(buffer);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -0700524
525 const sp<GraphicBuffer>& buf(slot.mGraphicBuffer);
526 if (buf != NULL) {
527 snprintf(buffer, SIZE,
528 ", %p [%4ux%4u:%4u,%3X]",
Jamie Gennisfa28c352011-09-16 17:30:26 -0700529 buf->handle, buf->width, buf->height, buf->stride,
530 buf->format);
Mathias Agopian2db6f0a2011-08-09 15:48:43 -0700531 result.append(buffer);
532 }
533 result.append("\n");
Mathias Agopian68c77942011-05-09 19:08:33 -0700534 }
535}
536
Jamie Gennisf238e282011-01-09 16:33:17 -0800537static void mtxMul(float out[16], const float a[16], const float b[16]) {
538 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
539 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
540 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
541 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
542
543 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
544 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
545 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
546 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
547
548 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
549 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
550 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
551 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
552
553 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
554 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
555 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
556 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
557}
558
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800559}; // namespace android