blob: 38599bffc641f13cd72815ef7ac76ee18a9bb424 [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 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800178 mAllocdBuffers.add(graphicBuffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800179 }
180 return graphicBuffer;
181}
182
183status_t SurfaceTexture::dequeueBuffer(int *buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800184 LOGV("SurfaceTexture::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800185 Mutex::Autolock lock(mMutex);
186 int found = INVALID_BUFFER_SLOT;
187 for (int i = 0; i < mBufferCount; i++) {
Jamie Gennis235c4242011-01-11 15:00:09 -0800188 if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800189 mSlots[i].mOwnedByClient = true;
190 found = i;
191 break;
192 }
193 }
194 if (found == INVALID_BUFFER_SLOT) {
195 return -EBUSY;
196 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700197
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800198 *buf = found;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700199
200 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
201 if (buffer == NULL) {
202 return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
203 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700204
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700205 if ((mUseDefaultSize) &&
206 ((uint32_t(buffer->width) != mDefaultWidth) ||
207 (uint32_t(buffer->height) != mDefaultHeight))) {
208 return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
209 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800210 return OK;
211}
212
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800213status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800214 LOGV("SurfaceTexture::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800215 Mutex::Autolock lock(mMutex);
216 if (buf < 0 || mBufferCount <= buf) {
217 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
218 mBufferCount, buf);
219 return -EINVAL;
220 } else if (!mSlots[buf].mOwnedByClient) {
221 LOGE("queueBuffer: slot %d is not owned by the client", buf);
222 return -EINVAL;
223 } else if (mSlots[buf].mGraphicBuffer == 0) {
224 LOGE("queueBuffer: slot %d was enqueued without requesting a buffer",
225 buf);
226 return -EINVAL;
227 }
228 mSlots[buf].mOwnedByClient = false;
229 mLastQueued = buf;
Jamie Gennisf238e282011-01-09 16:33:17 -0800230 mLastQueuedCrop = mNextCrop;
231 mLastQueuedTransform = mNextTransform;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800232 mLastQueuedTimestamp = timestamp;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800233 if (mFrameAvailableListener != 0) {
234 mFrameAvailableListener->onFrameAvailable();
235 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 return OK;
237}
238
239void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800240 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800241 Mutex::Autolock lock(mMutex);
242 if (buf < 0 || mBufferCount <= buf) {
243 LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount,
244 buf);
245 return;
246 } else if (!mSlots[buf].mOwnedByClient) {
247 LOGE("cancelBuffer: slot %d is not owned by the client", buf);
248 return;
249 }
250 mSlots[buf].mOwnedByClient = false;
251}
252
Jamie Gennisf238e282011-01-09 16:33:17 -0800253status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800254 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800255 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800256 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800257 return OK;
258}
259
260status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800261 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800262 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800263 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800264 return OK;
265}
266
267status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800268 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800269 Mutex::Autolock lock(mMutex);
270
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800271 // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT,
272 // so this check will fail until a buffer gets queued.
273 if (mCurrentTexture != mLastQueued) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800274 // Update the GL texture object.
Jamie Gennis9a78c902011-01-12 18:30:40 -0800275 EGLImageKHR image = mSlots[mLastQueued].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800276 if (image == EGL_NO_IMAGE_KHR) {
277 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis9a78c902011-01-12 18:30:40 -0800278 sp<GraphicBuffer> graphicBuffer = mSlots[mLastQueued].mGraphicBuffer;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800279 image = createImage(dpy, graphicBuffer);
Jamie Gennis9a78c902011-01-12 18:30:40 -0800280 mSlots[mLastQueued].mEglImage = image;
281 mSlots[mLastQueued].mEglDisplay = dpy;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800282 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800283
284 GLint error;
285 while ((error = glGetError()) != GL_NO_ERROR) {
286 LOGE("GL error cleared before updating SurfaceTexture: %#04x", error);
287 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700288
289 GLenum target = getTextureTarget(
290 mSlots[mLastQueued].mGraphicBuffer->format);
291 if (target != mCurrentTextureTarget) {
292 glDeleteTextures(1, &mTexName);
293 }
294 glBindTexture(target, mTexName);
295 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
296
Jamie Gennis0eb88512011-01-26 11:52:02 -0800297 bool failed = false;
298 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800299 LOGE("error binding external texture image %p (slot %d): %#04x",
Jamie Gennis9a78c902011-01-12 18:30:40 -0800300 image, mLastQueued, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800301 failed = true;
302 }
303 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800304 return -EINVAL;
305 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800306
307 // Update the SurfaceTexture state.
308 mCurrentTexture = mLastQueued;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700309 mCurrentTextureTarget = target;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800310 mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer;
311 mCurrentCrop = mLastQueuedCrop;
312 mCurrentTransform = mLastQueuedTransform;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800313 mCurrentTimestamp = mLastQueuedTimestamp;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700314 } else {
315 // We always bind the texture even if we don't update its contents.
316 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800317 }
318 return OK;
319}
320
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700321bool SurfaceTexture::isExternalFormat(uint32_t format)
322{
323 switch (format) {
324 // supported YUV formats
325 case HAL_PIXEL_FORMAT_YV12:
326 // Legacy/deprecated YUV formats
327 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
328 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
329 case HAL_PIXEL_FORMAT_YCbCr_422_I:
330 return true;
331 }
332
333 // Any OEM format needs to be considered
334 if (format>=0x100 && format<=0x1FF)
335 return true;
336
337 return false;
338}
339
340GLenum SurfaceTexture::getTextureTarget(uint32_t format)
341{
342 GLenum target = GL_TEXTURE_2D;
343#if defined(GL_OES_EGL_image_external)
344 if (isExternalFormat(format)) {
345 target = GL_TEXTURE_EXTERNAL_OES;
346 }
347#endif
348 return target;
349}
350
351GLenum SurfaceTexture::getCurrentTextureTarget() const {
352 Mutex::Autolock lock(mMutex);
353 return mCurrentTextureTarget;
354}
355
Jamie Gennisf238e282011-01-09 16:33:17 -0800356void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800357 LOGV("SurfaceTexture::getTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800358 Mutex::Autolock lock(mMutex);
359
Jamie Gennisa214c642011-01-14 13:53:31 -0800360 float xform[16];
361 for (int i = 0; i < 16; i++) {
362 xform[i] = mtxIdentity[i];
363 }
364 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
365 float result[16];
366 mtxMul(result, xform, mtxFlipH);
367 for (int i = 0; i < 16; i++) {
368 xform[i] = result[i];
369 }
370 }
371 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
372 float result[16];
373 mtxMul(result, xform, mtxFlipV);
374 for (int i = 0; i < 16; i++) {
375 xform[i] = result[i];
376 }
377 }
378 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
379 float result[16];
380 mtxMul(result, xform, mtxRot90);
381 for (int i = 0; i < 16; i++) {
382 xform[i] = result[i];
383 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800384 }
385
386 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800387 float tx, ty, sx, sy;
388 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800389 // In order to prevent bilinear sampling at the of the crop rectangle we
390 // may need to shrink it by 2 texels in each direction. Normally this
391 // would just need to take 1/2 a texel off each end, but because the
392 // chroma channels will likely be subsampled we need to chop off a whole
393 // texel. This will cause artifacts if someone does nearest sampling
394 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
395 // accomodate the bilinear and nearest sampling uses.
396 //
397 // If nearest sampling turns out to be a desirable usage of these
398 // textures then we could add the ability to switch a SurfaceTexture to
399 // nearest-mode. Preferably, however, the image producers (video
400 // decoder, camera, etc.) would simply not use a crop rectangle (or at
401 // least not tell the framework about it) so that the GPU can do the
402 // correct edge behavior.
403 int xshrink = 0, yshrink = 0;
404 if (mCurrentCrop.left > 0) {
405 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
406 xshrink++;
407 } else {
408 tx = 0.0f;
409 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700410 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800411 xshrink++;
412 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700413 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800414 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
415 float(buf->getHeight());
416 yshrink++;
417 } else {
418 ty = 0.0f;
419 }
420 if (mCurrentCrop.top > 0) {
421 yshrink++;
422 }
423 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
424 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800425 } else {
426 tx = 0.0f;
427 ty = 0.0f;
428 sx = 1.0f;
429 sy = 1.0f;
430 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800431 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800432 sx, 0, 0, 0,
433 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800434 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800435 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800436 };
437
Jamie Gennisa214c642011-01-14 13:53:31 -0800438 float mtxBeforeFlipV[16];
439 mtxMul(mtxBeforeFlipV, crop, xform);
440
441 // SurfaceFlinger expects the top of its window textures to be at a Y
442 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
443 // want to expose this to applications, however, so we must add an
444 // additional vertical flip to the transform after all the other transforms.
445 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800446}
447
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800448nsecs_t SurfaceTexture::getTimestamp() {
449 LOGV("SurfaceTexture::getTimestamp");
450 Mutex::Autolock lock(mMutex);
451 return mCurrentTimestamp;
452}
453
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800454void SurfaceTexture::setFrameAvailableListener(
455 const sp<FrameAvailableListener>& l) {
456 LOGV("SurfaceTexture::setFrameAvailableListener");
457 Mutex::Autolock lock(mMutex);
458 mFrameAvailableListener = l;
459}
460
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800461sp<IBinder> SurfaceTexture::getAllocator() {
462 LOGV("SurfaceTexture::getAllocator");
463 return mGraphicBufferAlloc->asBinder();
464}
465
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800466void SurfaceTexture::freeAllBuffers() {
467 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
468 mSlots[i].mGraphicBuffer = 0;
469 mSlots[i].mOwnedByClient = false;
470 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
471 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
472 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
473 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
474 }
475 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800476
477 int exceptBuf = -1;
478 for (size_t i = 0; i < mAllocdBuffers.size(); i++) {
479 if (mAllocdBuffers[i] == mCurrentTextureBuf) {
480 exceptBuf = i;
481 break;
482 }
483 }
484 mAllocdBuffers.clear();
485 if (exceptBuf >= 0) {
486 mAllocdBuffers.add(mCurrentTextureBuf);
487 }
488 mGraphicBufferAlloc->freeAllGraphicBuffersExcept(exceptBuf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800489}
490
491EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
492 const sp<GraphicBuffer>& graphicBuffer) {
493 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
494 EGLint attrs[] = {
495 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
496 EGL_NONE,
497 };
498 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
499 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
500 EGLint error = eglGetError();
501 if (error != EGL_SUCCESS) {
502 LOGE("error creating EGLImage: %#x", error);
503 } else if (image == EGL_NO_IMAGE_KHR) {
504 LOGE("no error reported, but no image was returned by "
505 "eglCreateImageKHR");
506 }
507 return image;
508}
509
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700510sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
511 Mutex::Autolock lock(mMutex);
512 return mCurrentTextureBuf;
513}
514
515Rect SurfaceTexture::getCurrentCrop() const {
516 Mutex::Autolock lock(mMutex);
517 return mCurrentCrop;
518}
519
520uint32_t SurfaceTexture::getCurrentTransform() const {
521 Mutex::Autolock lock(mMutex);
522 return mCurrentTransform;
523}
524
525
Jamie Gennisf238e282011-01-09 16:33:17 -0800526static void mtxMul(float out[16], const float a[16], const float b[16]) {
527 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
528 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
529 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
530 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
531
532 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
533 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
534 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
535 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
536
537 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
538 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
539 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
540 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
541
542 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
543 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
544 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
545 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
546}
547
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800548}; // namespace android