blob: ee5deb3b2089312ece24f9c8dea3b135e831b701 [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
Daniel Lam33302382012-01-22 15:26:27 -0800100// Get an ID that's unique within this process.
101static int32_t createProcessUniqueId() {
102 static volatile int32_t globalCounter = 0;
103 return android_atomic_inc(&globalCounter);
104}
Jamie Gennisfa28c352011-09-16 17:30:26 -0700105
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700106SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800107 GLenum texTarget, bool useFenceSync) :
Daniel Lam6b091c52012-01-22 15:26:27 -0800108 BufferQueue(allowSynchronousMode),
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 Lam33302382012-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 Lam33302382012-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());
123 BufferQueue::setConsumerName(mName);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700124
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700125 ST_LOGV("SurfaceTexture");
Jamie Gennisfa28c352011-09-16 17:30:26 -0700126 memcpy(mCurrentTransformMatrix, mtxIdentity,
127 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800128}
129
130SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700131 ST_LOGV("~SurfaceTexture");
Daniel Lam33302382012-01-22 15:26:27 -0800132 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800133}
134
Mathias Agopian80727112011-05-02 19:51:12 -0700135status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
136 Mutex::Autolock lock(mMutex);
Daniel Lam33302382012-01-22 15:26:27 -0800137 return BufferQueue::setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700138}
139
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800140
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700141status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
142{
Daniel Lam33302382012-01-22 15:26:27 -0800143 return BufferQueue::setDefaultBufferSize(w, h);
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
Daniel Lam33302382012-01-22 15:26:27 -0800155 BufferItem item;
156
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700157 // In asynchronous mode the list is guaranteed to be one buffer
158 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lam33302382012-01-22 15:26:27 -0800159 if (acquire(&item) == NO_ERROR) {
160 int buf = item.mBuf;
161 // This buffer was newly allocated, so we need to clean up on our side
162 if (item.mGraphicBuffer != NULL) {
163 mEGLSlots[buf].mGraphicBuffer = 0;
164 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
165 eglDestroyImageKHR(mEGLSlots[buf].mEglDisplay,
166 mEGLSlots[buf].mEglImage);
167 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
168 mEGLSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
169 }
170 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
171 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700172
Jamie Gennisf238e282011-01-09 16:33:17 -0800173 // Update the GL texture object.
Daniel Lam33302382012-01-22 15:26:27 -0800174 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800175 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800176 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lam33302382012-01-22 15:26:27 -0800177 if (item.mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700178 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700179 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700180 }
Daniel Lam33302382012-01-22 15:26:27 -0800181 image = createImage(dpy, item.mGraphicBuffer);
182 mEGLSlots[buf].mEglImage = image;
183 mEGLSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700184 if (image == EGL_NO_IMAGE_KHR) {
185 // NOTE: if dpy was invalid, createImage() is guaranteed to
186 // fail. so we'd end up here.
187 return -EINVAL;
188 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800190
191 GLint error;
192 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700193 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800194 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700195
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700196 glBindTexture(mTexTarget, mTexName);
197 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700198
Jamie Gennis0eb88512011-01-26 11:52:02 -0800199 bool failed = false;
200 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700201 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700202 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800203 failed = true;
204 }
205 if (failed) {
Daniel Lam33302382012-01-22 15:26:27 -0800206 releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
207 mEGLSlots[buf].mFence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800208 return -EINVAL;
209 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800210
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800211 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
212 if (mUseFenceSync) {
213 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
214 NULL);
215 if (fence == EGL_NO_SYNC_KHR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000216 ALOGE("updateTexImage: error creating fence: %#x",
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800217 eglGetError());
Daniel Lam33302382012-01-22 15:26:27 -0800218 releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
219 mEGLSlots[buf].mFence);
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800220 return -EINVAL;
221 }
222 glFlush();
Daniel Lam33302382012-01-22 15:26:27 -0800223 mEGLSlots[mCurrentTexture].mFence = fence;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800224 }
225 }
226
227 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
228 mCurrentTexture,
229 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lam33302382012-01-22 15:26:27 -0800230 buf, item.mGraphicBuffer->handle);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700231
Daniel Lam33302382012-01-22 15:26:27 -0800232 // release old buffer
233 releaseBuffer(mCurrentTexture,
234 mEGLSlots[mCurrentTexture].mEglDisplay,
235 mEGLSlots[mCurrentTexture].mFence);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700236
Jamie Gennis9a78c902011-01-12 18:30:40 -0800237 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700238 mCurrentTexture = buf;
Daniel Lam33302382012-01-22 15:26:27 -0800239 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
240 mCurrentCrop = item.mCrop;
241 mCurrentTransform = item.mTransform;
242 mCurrentScalingMode = item.mScalingMode;
243 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700244 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700245
246 // Now that we've passed the point at which failures can happen,
247 // it's safe to remove the buffer from the front of the queue.
Daniel Lam33302382012-01-22 15:26:27 -0800248
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700249 } else {
250 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700251 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800252 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700253
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800254 return OK;
255}
256
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700257bool SurfaceTexture::isExternalFormat(uint32_t format)
258{
259 switch (format) {
260 // supported YUV formats
261 case HAL_PIXEL_FORMAT_YV12:
262 // Legacy/deprecated YUV formats
263 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
264 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
265 case HAL_PIXEL_FORMAT_YCbCr_422_I:
266 return true;
267 }
268
269 // Any OEM format needs to be considered
270 if (format>=0x100 && format<=0x1FF)
271 return true;
272
273 return false;
274}
275
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700276GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700277 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700278}
279
Jamie Gennisf238e282011-01-09 16:33:17 -0800280void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800281 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700282 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
283}
284
285void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700286 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800287
Jamie Gennisa214c642011-01-14 13:53:31 -0800288 float xform[16];
289 for (int i = 0; i < 16; i++) {
290 xform[i] = mtxIdentity[i];
291 }
292 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
293 float result[16];
294 mtxMul(result, xform, mtxFlipH);
295 for (int i = 0; i < 16; i++) {
296 xform[i] = result[i];
297 }
298 }
299 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
300 float result[16];
301 mtxMul(result, xform, mtxFlipV);
302 for (int i = 0; i < 16; i++) {
303 xform[i] = result[i];
304 }
305 }
306 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
307 float result[16];
308 mtxMul(result, xform, mtxRot90);
309 for (int i = 0; i < 16; i++) {
310 xform[i] = result[i];
311 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800312 }
313
Daniel Lam33302382012-01-22 15:26:27 -0800314 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisa214c642011-01-14 13:53:31 -0800315 float tx, ty, sx, sy;
316 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800317 // In order to prevent bilinear sampling at the of the crop rectangle we
318 // may need to shrink it by 2 texels in each direction. Normally this
319 // would just need to take 1/2 a texel off each end, but because the
320 // chroma channels will likely be subsampled we need to chop off a whole
321 // texel. This will cause artifacts if someone does nearest sampling
322 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
323 // accomodate the bilinear and nearest sampling uses.
324 //
325 // If nearest sampling turns out to be a desirable usage of these
326 // textures then we could add the ability to switch a SurfaceTexture to
327 // nearest-mode. Preferably, however, the image producers (video
328 // decoder, camera, etc.) would simply not use a crop rectangle (or at
329 // least not tell the framework about it) so that the GPU can do the
330 // correct edge behavior.
331 int xshrink = 0, yshrink = 0;
332 if (mCurrentCrop.left > 0) {
333 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
334 xshrink++;
335 } else {
336 tx = 0.0f;
337 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700338 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800339 xshrink++;
340 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700341 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800342 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
343 float(buf->getHeight());
344 yshrink++;
345 } else {
346 ty = 0.0f;
347 }
348 if (mCurrentCrop.top > 0) {
349 yshrink++;
350 }
351 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
352 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800353 } else {
354 tx = 0.0f;
355 ty = 0.0f;
356 sx = 1.0f;
357 sy = 1.0f;
358 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800359 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800360 sx, 0, 0, 0,
361 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800362 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800363 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800364 };
365
Jamie Gennisa214c642011-01-14 13:53:31 -0800366 float mtxBeforeFlipV[16];
367 mtxMul(mtxBeforeFlipV, crop, xform);
368
369 // SurfaceFlinger expects the top of its window textures to be at a Y
370 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
371 // want to expose this to applications, however, so we must add an
372 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700373 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800374}
375
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800376nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700377 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800378 Mutex::Autolock lock(mMutex);
379 return mCurrentTimestamp;
380}
381
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800382void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700383 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700384 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800385 Mutex::Autolock lock(mMutex);
Daniel Lam33302382012-01-22 15:26:27 -0800386 BufferQueue::setFrameAvailableListener(listener);
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800387}
388
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800389EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
390 const sp<GraphicBuffer>& graphicBuffer) {
391 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
392 EGLint attrs[] = {
393 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
394 EGL_NONE,
395 };
396 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
397 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700398 if (image == EGL_NO_IMAGE_KHR) {
399 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700400 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800401 }
402 return image;
403}
404
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700405sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
406 Mutex::Autolock lock(mMutex);
407 return mCurrentTextureBuf;
408}
409
410Rect SurfaceTexture::getCurrentCrop() const {
411 Mutex::Autolock lock(mMutex);
412 return mCurrentCrop;
413}
414
415uint32_t SurfaceTexture::getCurrentTransform() const {
416 Mutex::Autolock lock(mMutex);
417 return mCurrentTransform;
418}
419
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700420uint32_t SurfaceTexture::getCurrentScalingMode() const {
421 Mutex::Autolock lock(mMutex);
422 return mCurrentScalingMode;
423}
424
Jamie Gennis59769462011-11-19 18:04:43 -0800425bool SurfaceTexture::isSynchronousMode() const {
426 Mutex::Autolock lock(mMutex);
Daniel Lam33302382012-01-22 15:26:27 -0800427 return BufferQueue::isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800428}
429
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700430void SurfaceTexture::abandon() {
431 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700432 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700433 mCurrentTextureBuf.clear();
Daniel Lam33302382012-01-22 15:26:27 -0800434
435 // destroy all egl buffers
436 for (int i =0; i < NUM_BUFFER_SLOTS; i++) {
437 mEGLSlots[i].mGraphicBuffer = 0;
438 if (mEGLSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
439 eglDestroyImageKHR(mEGLSlots[i].mEglDisplay,
440 mEGLSlots[i].mEglImage);
441 mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
442 mEGLSlots[i].mEglDisplay = EGL_NO_DISPLAY;
443 }
444 }
445
446 // disconnect from the BufferQueue
447 BufferQueue::consumerDisconnect();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700448}
449
Jamie Gennisfa28c352011-09-16 17:30:26 -0700450void SurfaceTexture::setName(const String8& name) {
Daniel Lam33302382012-01-22 15:26:27 -0800451 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700452 mName = name;
Daniel Lam33302382012-01-22 15:26:27 -0800453 BufferQueue::setConsumerName(name);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700454}
455
Mathias Agopian68c77942011-05-09 19:08:33 -0700456void SurfaceTexture::dump(String8& result) const
457{
458 char buffer[1024];
459 dump(result, "", buffer, 1024);
460}
461
462void SurfaceTexture::dump(String8& result, const char* prefix,
463 char* buffer, size_t SIZE) const
464{
465 Mutex::Autolock _l(mMutex);
Daniel Lam33302382012-01-22 15:26:27 -0800466 snprintf(buffer, SIZE, "%smTexName=%d\n", prefix, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -0700467 result.append(buffer);
468
Mathias Agopian68c77942011-05-09 19:08:33 -0700469 snprintf(buffer, SIZE,
Daniel Lam33302382012-01-22 15:26:27 -0800470 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
471 ,prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700472 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lam33302382012-01-22 15:26:27 -0800473 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700474 );
475 result.append(buffer);
476
Mathias Agopian68c77942011-05-09 19:08:33 -0700477
Daniel Lam33302382012-01-22 15:26:27 -0800478 BufferQueue::dump(result, prefix, buffer, SIZE);
Mathias Agopian68c77942011-05-09 19:08:33 -0700479}
480
Jamie Gennisf238e282011-01-09 16:33:17 -0800481static void mtxMul(float out[16], const float a[16], const float b[16]) {
482 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
483 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
484 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
485 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
486
487 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
488 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
489 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
490 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
491
492 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
493 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
494 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
495 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
496
497 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
498 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
499 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
500 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
501}
502
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800503}; // namespace android