blob: a23a32801d7bfc8374298930085f1782de652d41 [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
Daniel Lameae59d22012-01-22 15:26:27 -080099// Get an ID that's unique within this process.
100static int32_t createProcessUniqueId() {
101 static volatile int32_t globalCounter = 0;
102 return android_atomic_inc(&globalCounter);
103}
Jamie Gennisfa28c352011-09-16 17:30:26 -0700104
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700105SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode,
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800106 GLenum texTarget, bool useFenceSync) :
Daniel Lam6b091c52012-01-22 15:26:27 -0800107 BufferQueue(allowSynchronousMode),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800108 mCurrentTransform(0),
109 mCurrentTimestamp(0),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700110 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800111#ifdef USE_FENCE_SYNC
112 mUseFenceSync(useFenceSync),
113#else
114 mUseFenceSync(false),
115#endif
Daniel Lameae59d22012-01-22 15:26:27 -0800116 mTexTarget(texTarget),
117 mAbandoned(false),
118 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
Daniel Lam6b091c52012-01-22 15:26:27 -0800119{
Daniel Lameae59d22012-01-22 15:26:27 -0800120 // Choose a name using the PID and a process-unique ID.
121 mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
122 BufferQueue::setConsumerName(mName);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700123
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700124 ST_LOGV("SurfaceTexture");
Jamie Gennisfa28c352011-09-16 17:30:26 -0700125 memcpy(mCurrentTransformMatrix, mtxIdentity,
126 sizeof(mCurrentTransformMatrix));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800127}
128
129SurfaceTexture::~SurfaceTexture() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700130 ST_LOGV("~SurfaceTexture");
Daniel Lameae59d22012-01-22 15:26:27 -0800131 abandon();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800132}
133
Mathias Agopian80727112011-05-02 19:51:12 -0700134status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
135 Mutex::Autolock lock(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800136 return BufferQueue::setBufferCountServer(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700137}
138
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800139
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700140status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
141{
Daniel Lameae59d22012-01-22 15:26:27 -0800142 return BufferQueue::setDefaultBufferSize(w, h);
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
Daniel Lameae59d22012-01-22 15:26:27 -0800154 BufferItem item;
155
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700156 // In asynchronous mode the list is guaranteed to be one buffer
157 // deep, while in synchronous mode we use the oldest buffer.
Daniel Lameae59d22012-01-22 15:26:27 -0800158 if (acquire(&item) == NO_ERROR) {
159 int buf = item.mBuf;
160 // This buffer was newly allocated, so we need to clean up on our side
161 if (item.mGraphicBuffer != NULL) {
162 mEGLSlots[buf].mGraphicBuffer = 0;
163 if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
164 eglDestroyImageKHR(mEGLSlots[buf].mEglDisplay,
165 mEGLSlots[buf].mEglImage);
166 mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
167 mEGLSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
168 }
169 mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer;
170 }
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700171
Jamie Gennisf238e282011-01-09 16:33:17 -0800172 // Update the GL texture object.
Daniel Lameae59d22012-01-22 15:26:27 -0800173 EGLImageKHR image = mEGLSlots[buf].mEglImage;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800174 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800175 if (image == EGL_NO_IMAGE_KHR) {
Daniel Lameae59d22012-01-22 15:26:27 -0800176 if (item.mGraphicBuffer == 0) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700177 ST_LOGE("buffer at slot %d is null", buf);
Mathias Agopian8e19c2e2011-08-10 17:35:09 -0700178 return BAD_VALUE;
Mathias Agopiane47498f2011-08-08 19:35:15 -0700179 }
Daniel Lameae59d22012-01-22 15:26:27 -0800180 image = createImage(dpy, item.mGraphicBuffer);
181 mEGLSlots[buf].mEglImage = image;
182 mEGLSlots[buf].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700183 if (image == EGL_NO_IMAGE_KHR) {
184 // NOTE: if dpy was invalid, createImage() is guaranteed to
185 // fail. so we'd end up here.
186 return -EINVAL;
187 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800188 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800189
190 GLint error;
191 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700192 ST_LOGW("updateTexImage: clearing GL error: %#04x", error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800193 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700194
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700195 glBindTexture(mTexTarget, mTexName);
196 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700197
Jamie Gennis0eb88512011-01-26 11:52:02 -0800198 bool failed = false;
199 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennisfa28c352011-09-16 17:30:26 -0700200 ST_LOGE("error binding external texture image %p (slot %d): %#04x",
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700201 image, buf, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800202 failed = true;
203 }
204 if (failed) {
Daniel Lameae59d22012-01-22 15:26:27 -0800205 releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
206 mEGLSlots[buf].mFence);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800207 return -EINVAL;
208 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800209
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800210 if (mCurrentTexture != INVALID_BUFFER_SLOT) {
211 if (mUseFenceSync) {
212 EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR,
213 NULL);
214 if (fence == EGL_NO_SYNC_KHR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000215 ALOGE("updateTexImage: error creating fence: %#x",
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800216 eglGetError());
Daniel Lameae59d22012-01-22 15:26:27 -0800217 releaseBuffer(buf, mEGLSlots[buf].mEglDisplay,
218 mEGLSlots[buf].mFence);
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800219 return -EINVAL;
220 }
221 glFlush();
Daniel Lameae59d22012-01-22 15:26:27 -0800222 mEGLSlots[mCurrentTexture].mFence = fence;
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800223 }
224 }
225
226 ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)",
227 mCurrentTexture,
228 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
Daniel Lameae59d22012-01-22 15:26:27 -0800229 buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0);
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700230
Daniel Lameae59d22012-01-22 15:26:27 -0800231 // release old buffer
232 releaseBuffer(mCurrentTexture,
233 mEGLSlots[mCurrentTexture].mEglDisplay,
234 mEGLSlots[mCurrentTexture].mFence);
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700235
Jamie Gennis9a78c902011-01-12 18:30:40 -0800236 // Update the SurfaceTexture state.
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700237 mCurrentTexture = buf;
Daniel Lameae59d22012-01-22 15:26:27 -0800238 mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer;
239 mCurrentCrop = item.mCrop;
240 mCurrentTransform = item.mTransform;
241 mCurrentScalingMode = item.mScalingMode;
242 mCurrentTimestamp = item.mTimestamp;
Jamie Gennis736aa952011-06-12 17:03:06 -0700243 computeCurrentTransformMatrix();
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700244
245 // Now that we've passed the point at which failures can happen,
246 // it's safe to remove the buffer from the front of the queue.
Daniel Lameae59d22012-01-22 15:26:27 -0800247
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700248 } else {
249 // We always bind the texture even if we don't update its contents.
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700250 glBindTexture(mTexTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800251 }
Jamie Gennis50c4efc2011-06-27 15:41:52 -0700252
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800253 return OK;
254}
255
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700256bool SurfaceTexture::isExternalFormat(uint32_t format)
257{
258 switch (format) {
259 // supported YUV formats
260 case HAL_PIXEL_FORMAT_YV12:
261 // Legacy/deprecated YUV formats
262 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
263 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
264 case HAL_PIXEL_FORMAT_YCbCr_422_I:
265 return true;
266 }
267
268 // Any OEM format needs to be considered
269 if (format>=0x100 && format<=0x1FF)
270 return true;
271
272 return false;
273}
274
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700275GLenum SurfaceTexture::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700276 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700277}
278
Jamie Gennisf238e282011-01-09 16:33:17 -0800279void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800280 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700281 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
282}
283
284void SurfaceTexture::computeCurrentTransformMatrix() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700285 ST_LOGV("computeCurrentTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800286
Jamie Gennisa214c642011-01-14 13:53:31 -0800287 float xform[16];
288 for (int i = 0; i < 16; i++) {
289 xform[i] = mtxIdentity[i];
290 }
291 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
292 float result[16];
293 mtxMul(result, xform, mtxFlipH);
294 for (int i = 0; i < 16; i++) {
295 xform[i] = result[i];
296 }
297 }
298 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
299 float result[16];
300 mtxMul(result, xform, mtxFlipV);
301 for (int i = 0; i < 16; i++) {
302 xform[i] = result[i];
303 }
304 }
305 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
306 float result[16];
307 mtxMul(result, xform, mtxRot90);
308 for (int i = 0; i < 16; i++) {
309 xform[i] = result[i];
310 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800311 }
312
Daniel Lameae59d22012-01-22 15:26:27 -0800313 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Jamie Gennisa214c642011-01-14 13:53:31 -0800314 float tx, ty, sx, sy;
315 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800316 // In order to prevent bilinear sampling at the of the crop rectangle we
317 // may need to shrink it by 2 texels in each direction. Normally this
318 // would just need to take 1/2 a texel off each end, but because the
319 // chroma channels will likely be subsampled we need to chop off a whole
320 // texel. This will cause artifacts if someone does nearest sampling
321 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
322 // accomodate the bilinear and nearest sampling uses.
323 //
324 // If nearest sampling turns out to be a desirable usage of these
325 // textures then we could add the ability to switch a SurfaceTexture to
326 // nearest-mode. Preferably, however, the image producers (video
327 // decoder, camera, etc.) would simply not use a crop rectangle (or at
328 // least not tell the framework about it) so that the GPU can do the
329 // correct edge behavior.
330 int xshrink = 0, yshrink = 0;
331 if (mCurrentCrop.left > 0) {
332 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
333 xshrink++;
334 } else {
335 tx = 0.0f;
336 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700337 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800338 xshrink++;
339 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700340 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800341 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
342 float(buf->getHeight());
343 yshrink++;
344 } else {
345 ty = 0.0f;
346 }
347 if (mCurrentCrop.top > 0) {
348 yshrink++;
349 }
350 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
351 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800352 } else {
353 tx = 0.0f;
354 ty = 0.0f;
355 sx = 1.0f;
356 sy = 1.0f;
357 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800358 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800359 sx, 0, 0, 0,
360 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800361 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800362 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800363 };
364
Jamie Gennisa214c642011-01-14 13:53:31 -0800365 float mtxBeforeFlipV[16];
366 mtxMul(mtxBeforeFlipV, crop, xform);
367
368 // SurfaceFlinger expects the top of its window textures to be at a Y
369 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
370 // want to expose this to applications, however, so we must add an
371 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700372 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800373}
374
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800375nsecs_t SurfaceTexture::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700376 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800377 Mutex::Autolock lock(mMutex);
378 return mCurrentTimestamp;
379}
380
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800381void SurfaceTexture::setFrameAvailableListener(
Pannag Sanketi292a31a2011-06-24 09:56:27 -0700382 const sp<FrameAvailableListener>& listener) {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700383 ST_LOGV("setFrameAvailableListener");
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800384 Mutex::Autolock lock(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800385 BufferQueue::setFrameAvailableListener(listener);
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800386}
387
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800388EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
389 const sp<GraphicBuffer>& graphicBuffer) {
390 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
391 EGLint attrs[] = {
392 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
393 EGL_NONE,
394 };
395 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
396 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700397 if (image == EGL_NO_IMAGE_KHR) {
398 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700399 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800400 }
401 return image;
402}
403
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700404sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
405 Mutex::Autolock lock(mMutex);
406 return mCurrentTextureBuf;
407}
408
409Rect SurfaceTexture::getCurrentCrop() const {
410 Mutex::Autolock lock(mMutex);
411 return mCurrentCrop;
412}
413
414uint32_t SurfaceTexture::getCurrentTransform() const {
415 Mutex::Autolock lock(mMutex);
416 return mCurrentTransform;
417}
418
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700419uint32_t SurfaceTexture::getCurrentScalingMode() const {
420 Mutex::Autolock lock(mMutex);
421 return mCurrentScalingMode;
422}
423
Jamie Gennis59769462011-11-19 18:04:43 -0800424bool SurfaceTexture::isSynchronousMode() const {
425 Mutex::Autolock lock(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800426 return BufferQueue::isSynchronousMode();
Jamie Gennis59769462011-11-19 18:04:43 -0800427}
428
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700429void SurfaceTexture::abandon() {
430 Mutex::Autolock lock(mMutex);
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700431 mAbandoned = true;
Mathias Agopian97a98842011-08-03 15:18:36 -0700432 mCurrentTextureBuf.clear();
Daniel Lameae59d22012-01-22 15:26:27 -0800433
434 // destroy all egl buffers
435 for (int i =0; i < NUM_BUFFER_SLOTS; i++) {
436 mEGLSlots[i].mGraphicBuffer = 0;
437 if (mEGLSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
438 eglDestroyImageKHR(mEGLSlots[i].mEglDisplay,
439 mEGLSlots[i].mEglImage);
440 mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
441 mEGLSlots[i].mEglDisplay = EGL_NO_DISPLAY;
442 }
443 }
444
445 // disconnect from the BufferQueue
446 BufferQueue::consumerDisconnect();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700447}
448
Jamie Gennisfa28c352011-09-16 17:30:26 -0700449void SurfaceTexture::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800450 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700451 mName = name;
Daniel Lameae59d22012-01-22 15:26:27 -0800452 BufferQueue::setConsumerName(name);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700453}
454
Mathias Agopian68c77942011-05-09 19:08:33 -0700455void SurfaceTexture::dump(String8& result) const
456{
457 char buffer[1024];
458 dump(result, "", buffer, 1024);
459}
460
461void SurfaceTexture::dump(String8& result, const char* prefix,
462 char* buffer, size_t SIZE) const
463{
464 Mutex::Autolock _l(mMutex);
Daniel Lameae59d22012-01-22 15:26:27 -0800465 snprintf(buffer, SIZE, "%smTexName=%d\n", prefix, mTexName);
Mathias Agopian68c77942011-05-09 19:08:33 -0700466 result.append(buffer);
467
Mathias Agopian68c77942011-05-09 19:08:33 -0700468 snprintf(buffer, SIZE,
Daniel Lameae59d22012-01-22 15:26:27 -0800469 "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
470 ,prefix, mCurrentCrop.left,
Mathias Agopian68c77942011-05-09 19:08:33 -0700471 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
Daniel Lameae59d22012-01-22 15:26:27 -0800472 mCurrentTransform, mCurrentTexture
Mathias Agopian68c77942011-05-09 19:08:33 -0700473 );
474 result.append(buffer);
475
Mathias Agopian68c77942011-05-09 19:08:33 -0700476
Daniel Lameae59d22012-01-22 15:26:27 -0800477 BufferQueue::dump(result, prefix, buffer, SIZE);
Mathias Agopian68c77942011-05-09 19:08:33 -0700478}
479
Jamie Gennisf238e282011-01-09 16:33:17 -0800480static void mtxMul(float out[16], const float a[16], const float b[16]) {
481 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
482 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
483 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
484 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
485
486 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
487 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
488 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
489 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
490
491 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
492 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
493 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
494 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
495
496 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
497 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
498 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
499 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
500}
501
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800502}; // namespace android