blob: 39418f03adae75a06f9358c97c88f8fc019a12d2 [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
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080034#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035
36#include <utils/Log.h>
37
38namespace android {
39
Jamie Gennisf238e282011-01-09 16:33:17 -080040// Transform matrices
41static float mtxIdentity[16] = {
42 1, 0, 0, 0,
43 0, 1, 0, 0,
44 0, 0, 1, 0,
45 0, 0, 0, 1,
46};
47static float mtxFlipH[16] = {
48 -1, 0, 0, 0,
49 0, 1, 0, 0,
50 0, 0, 1, 0,
51 1, 0, 0, 1,
52};
53static float mtxFlipV[16] = {
54 1, 0, 0, 0,
55 0, -1, 0, 0,
56 0, 0, 1, 0,
57 0, 1, 0, 1,
58};
59static float mtxRot90[16] = {
60 0, 1, 0, 0,
61 -1, 0, 0, 0,
62 0, 0, 1, 0,
63 1, 0, 0, 1,
64};
65static float mtxRot180[16] = {
66 -1, 0, 0, 0,
67 0, -1, 0, 0,
68 0, 0, 1, 0,
69 1, 1, 0, 1,
70};
71static float mtxRot270[16] = {
72 0, -1, 0, 0,
73 1, 0, 0, 0,
74 0, 0, 1, 0,
75 0, 1, 0, 1,
76};
77
78static void mtxMul(float out[16], const float a[16], const float b[16]);
79
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080080SurfaceTexture::SurfaceTexture(GLuint tex) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070081 mDefaultWidth(1),
82 mDefaultHeight(1),
83 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
84 mUseDefaultSize(true),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080085 mBufferCount(MIN_BUFFER_SLOTS),
86 mCurrentTexture(INVALID_BUFFER_SLOT),
Mathias Agopian7a042bf2011-04-11 21:19:55 -070087 mCurrentTextureTarget(GL_TEXTURE_EXTERNAL_OES),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080088 mCurrentTransform(0),
89 mCurrentTimestamp(0),
90 mLastQueued(INVALID_BUFFER_SLOT),
91 mLastQueuedTransform(0),
92 mLastQueuedTimestamp(0),
93 mNextTransform(0),
94 mTexName(tex) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080095 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis3461e0f2011-01-07 16:05:47 -080096 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
97 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
98 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
99 mSlots[i].mOwnedByClient = false;
100 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800101 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
102 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800103}
104
105SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800106 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800107 freeAllBuffers();
108}
109
110status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800111 LOGV("SurfaceTexture::setBufferCount");
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800112
113 if (bufferCount < MIN_BUFFER_SLOTS) {
114 return BAD_VALUE;
115 }
116
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800117 Mutex::Autolock lock(mMutex);
118 freeAllBuffers();
119 mBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800120 mCurrentTexture = INVALID_BUFFER_SLOT;
121 mLastQueued = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800122 return OK;
123}
124
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700125status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
126{
127 Mutex::Autolock lock(mMutex);
128 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
129 mDefaultWidth = w;
130 mDefaultHeight = h;
131 }
132 return OK;
133}
134
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf,
136 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800137 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800138 Mutex::Autolock lock(mMutex);
139 if (buf < 0 || mBufferCount <= buf) {
140 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
141 mBufferCount, buf);
142 return 0;
143 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700144 if ((w && !h) || (!w & h)) {
145 LOGE("requestBuffer: invalid size: w=%u, h=%u: %d", w, h, buf);
146 return 0;
147 }
148
149 const bool useDefaultSize = !w && !h;
150 if (useDefaultSize) {
151 // use the default size
152 w = mDefaultWidth;
153 h = mDefaultHeight;
154 }
155
156 const bool updateFormat = (format != 0);
157 if (!updateFormat) {
158 // keep the current (or default) format
159 format = mPixelFormat;
160 }
161
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800162 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800163 sp<GraphicBuffer> graphicBuffer(
164 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800165 if (graphicBuffer == 0) {
166 LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed");
167 } else {
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700168 mUseDefaultSize = useDefaultSize;
169 if (updateFormat) {
170 mPixelFormat = format;
171 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800172 mSlots[buf].mGraphicBuffer = graphicBuffer;
173 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
174 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
175 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
176 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
177 }
178 }
179 return graphicBuffer;
180}
181
182status_t SurfaceTexture::dequeueBuffer(int *buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800183 LOGV("SurfaceTexture::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800184 Mutex::Autolock lock(mMutex);
185 int found = INVALID_BUFFER_SLOT;
186 for (int i = 0; i < mBufferCount; i++) {
Jamie Gennis235c4242011-01-11 15:00:09 -0800187 if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800188 mSlots[i].mOwnedByClient = true;
189 found = i;
190 break;
191 }
192 }
193 if (found == INVALID_BUFFER_SLOT) {
194 return -EBUSY;
195 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700196
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800197 *buf = found;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700198
199 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
200 if (buffer == NULL) {
201 return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
202 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700203
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700204 if ((mUseDefaultSize) &&
205 ((uint32_t(buffer->width) != mDefaultWidth) ||
206 (uint32_t(buffer->height) != mDefaultHeight))) {
207 return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
208 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800209 return OK;
210}
211
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800212status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800213 LOGV("SurfaceTexture::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800214 Mutex::Autolock lock(mMutex);
215 if (buf < 0 || mBufferCount <= buf) {
216 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
217 mBufferCount, buf);
218 return -EINVAL;
219 } else if (!mSlots[buf].mOwnedByClient) {
220 LOGE("queueBuffer: slot %d is not owned by the client", buf);
221 return -EINVAL;
222 } else if (mSlots[buf].mGraphicBuffer == 0) {
223 LOGE("queueBuffer: slot %d was enqueued without requesting a buffer",
224 buf);
225 return -EINVAL;
226 }
227 mSlots[buf].mOwnedByClient = false;
228 mLastQueued = buf;
Jamie Gennisf238e282011-01-09 16:33:17 -0800229 mLastQueuedCrop = mNextCrop;
230 mLastQueuedTransform = mNextTransform;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800231 mLastQueuedTimestamp = timestamp;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800232 if (mFrameAvailableListener != 0) {
233 mFrameAvailableListener->onFrameAvailable();
234 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800235 return OK;
236}
237
238void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800239 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800240 Mutex::Autolock lock(mMutex);
241 if (buf < 0 || mBufferCount <= buf) {
242 LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount,
243 buf);
244 return;
245 } else if (!mSlots[buf].mOwnedByClient) {
246 LOGE("cancelBuffer: slot %d is not owned by the client", buf);
247 return;
248 }
249 mSlots[buf].mOwnedByClient = false;
250}
251
Jamie Gennisf238e282011-01-09 16:33:17 -0800252status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800253 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800254 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800255 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800256 return OK;
257}
258
259status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800260 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800261 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800262 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800263 return OK;
264}
265
266status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800267 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800268 Mutex::Autolock lock(mMutex);
269
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800270 // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT,
271 // so this check will fail until a buffer gets queued.
272 if (mCurrentTexture != mLastQueued) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800273 // Update the GL texture object.
Jamie Gennis9a78c902011-01-12 18:30:40 -0800274 EGLImageKHR image = mSlots[mLastQueued].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800275 if (image == EGL_NO_IMAGE_KHR) {
276 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis9a78c902011-01-12 18:30:40 -0800277 sp<GraphicBuffer> graphicBuffer = mSlots[mLastQueued].mGraphicBuffer;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800278 image = createImage(dpy, graphicBuffer);
Jamie Gennis9a78c902011-01-12 18:30:40 -0800279 mSlots[mLastQueued].mEglImage = image;
280 mSlots[mLastQueued].mEglDisplay = dpy;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800281 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800282
283 GLint error;
284 while ((error = glGetError()) != GL_NO_ERROR) {
285 LOGE("GL error cleared before updating SurfaceTexture: %#04x", error);
286 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700287
288 GLenum target = getTextureTarget(
289 mSlots[mLastQueued].mGraphicBuffer->format);
290 if (target != mCurrentTextureTarget) {
291 glDeleteTextures(1, &mTexName);
292 }
293 glBindTexture(target, mTexName);
294 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
295
Jamie Gennis0eb88512011-01-26 11:52:02 -0800296 bool failed = false;
297 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800298 LOGE("error binding external texture image %p (slot %d): %#04x",
Jamie Gennis9a78c902011-01-12 18:30:40 -0800299 image, mLastQueued, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800300 failed = true;
301 }
302 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800303 return -EINVAL;
304 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800305
306 // Update the SurfaceTexture state.
307 mCurrentTexture = mLastQueued;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700308 mCurrentTextureTarget = target;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800309 mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer;
310 mCurrentCrop = mLastQueuedCrop;
311 mCurrentTransform = mLastQueuedTransform;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800312 mCurrentTimestamp = mLastQueuedTimestamp;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700313 } else {
314 // We always bind the texture even if we don't update its contents.
315 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800316 }
317 return OK;
318}
319
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700320bool SurfaceTexture::isExternalFormat(uint32_t format)
321{
322 switch (format) {
323 // supported YUV formats
324 case HAL_PIXEL_FORMAT_YV12:
325 // Legacy/deprecated YUV formats
326 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
327 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
328 case HAL_PIXEL_FORMAT_YCbCr_422_I:
329 return true;
330 }
331
332 // Any OEM format needs to be considered
333 if (format>=0x100 && format<=0x1FF)
334 return true;
335
336 return false;
337}
338
339GLenum SurfaceTexture::getTextureTarget(uint32_t format)
340{
341 GLenum target = GL_TEXTURE_2D;
342#if defined(GL_OES_EGL_image_external)
343 if (isExternalFormat(format)) {
344 target = GL_TEXTURE_EXTERNAL_OES;
345 }
346#endif
347 return target;
348}
349
350GLenum SurfaceTexture::getCurrentTextureTarget() const {
351 Mutex::Autolock lock(mMutex);
352 return mCurrentTextureTarget;
353}
354
Jamie Gennisf238e282011-01-09 16:33:17 -0800355void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800356 LOGV("SurfaceTexture::getTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800357 Mutex::Autolock lock(mMutex);
358
Jamie Gennisa214c642011-01-14 13:53:31 -0800359 float xform[16];
360 for (int i = 0; i < 16; i++) {
361 xform[i] = mtxIdentity[i];
362 }
363 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
364 float result[16];
365 mtxMul(result, xform, mtxFlipH);
366 for (int i = 0; i < 16; i++) {
367 xform[i] = result[i];
368 }
369 }
370 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
371 float result[16];
372 mtxMul(result, xform, mtxFlipV);
373 for (int i = 0; i < 16; i++) {
374 xform[i] = result[i];
375 }
376 }
377 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
378 float result[16];
379 mtxMul(result, xform, mtxRot90);
380 for (int i = 0; i < 16; i++) {
381 xform[i] = result[i];
382 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800383 }
384
385 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800386 float tx, ty, sx, sy;
387 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800388 // In order to prevent bilinear sampling at the of the crop rectangle we
389 // may need to shrink it by 2 texels in each direction. Normally this
390 // would just need to take 1/2 a texel off each end, but because the
391 // chroma channels will likely be subsampled we need to chop off a whole
392 // texel. This will cause artifacts if someone does nearest sampling
393 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
394 // accomodate the bilinear and nearest sampling uses.
395 //
396 // If nearest sampling turns out to be a desirable usage of these
397 // textures then we could add the ability to switch a SurfaceTexture to
398 // nearest-mode. Preferably, however, the image producers (video
399 // decoder, camera, etc.) would simply not use a crop rectangle (or at
400 // least not tell the framework about it) so that the GPU can do the
401 // correct edge behavior.
402 int xshrink = 0, yshrink = 0;
403 if (mCurrentCrop.left > 0) {
404 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
405 xshrink++;
406 } else {
407 tx = 0.0f;
408 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700409 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800410 xshrink++;
411 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700412 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800413 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
414 float(buf->getHeight());
415 yshrink++;
416 } else {
417 ty = 0.0f;
418 }
419 if (mCurrentCrop.top > 0) {
420 yshrink++;
421 }
422 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
423 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800424 } else {
425 tx = 0.0f;
426 ty = 0.0f;
427 sx = 1.0f;
428 sy = 1.0f;
429 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800430 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800431 sx, 0, 0, 0,
432 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800433 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800434 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800435 };
436
Jamie Gennisa214c642011-01-14 13:53:31 -0800437 float mtxBeforeFlipV[16];
438 mtxMul(mtxBeforeFlipV, crop, xform);
439
440 // SurfaceFlinger expects the top of its window textures to be at a Y
441 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
442 // want to expose this to applications, however, so we must add an
443 // additional vertical flip to the transform after all the other transforms.
444 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800445}
446
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800447nsecs_t SurfaceTexture::getTimestamp() {
448 LOGV("SurfaceTexture::getTimestamp");
449 Mutex::Autolock lock(mMutex);
450 return mCurrentTimestamp;
451}
452
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800453void SurfaceTexture::setFrameAvailableListener(
454 const sp<FrameAvailableListener>& l) {
455 LOGV("SurfaceTexture::setFrameAvailableListener");
456 Mutex::Autolock lock(mMutex);
457 mFrameAvailableListener = l;
458}
459
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800460sp<IBinder> SurfaceTexture::getAllocator() {
461 LOGV("SurfaceTexture::getAllocator");
462 return mGraphicBufferAlloc->asBinder();
463}
464
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800465void SurfaceTexture::freeAllBuffers() {
466 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
467 mSlots[i].mGraphicBuffer = 0;
468 mSlots[i].mOwnedByClient = false;
469 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
470 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
471 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
472 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
473 }
474 }
475}
476
477EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
478 const sp<GraphicBuffer>& graphicBuffer) {
479 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
480 EGLint attrs[] = {
481 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
482 EGL_NONE,
483 };
484 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
485 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
486 EGLint error = eglGetError();
487 if (error != EGL_SUCCESS) {
488 LOGE("error creating EGLImage: %#x", error);
489 } else if (image == EGL_NO_IMAGE_KHR) {
490 LOGE("no error reported, but no image was returned by "
491 "eglCreateImageKHR");
492 }
493 return image;
494}
495
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700496sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
497 Mutex::Autolock lock(mMutex);
498 return mCurrentTextureBuf;
499}
500
501Rect SurfaceTexture::getCurrentCrop() const {
502 Mutex::Autolock lock(mMutex);
503 return mCurrentCrop;
504}
505
506uint32_t SurfaceTexture::getCurrentTransform() const {
507 Mutex::Autolock lock(mMutex);
508 return mCurrentTransform;
509}
510
511
Jamie Gennisf238e282011-01-09 16:33:17 -0800512static void mtxMul(float out[16], const float a[16], const float b[16]) {
513 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
514 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
515 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
516 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
517
518 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
519 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
520 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
521 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
522
523 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
524 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
525 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
526 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
527
528 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
529 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
530 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
531 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
532}
533
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800534}; // namespace android