blob: 2619629be8daaaeae2298b452cf6846af0584e70 [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();
Mathias Agopiancc57d6f2011-04-27 18:57:33 -0700103 mNextCrop.makeInvalid();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800104}
105
106SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800107 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800108 freeAllBuffers();
109}
110
111status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800112 LOGV("SurfaceTexture::setBufferCount");
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800113
114 if (bufferCount < MIN_BUFFER_SLOTS) {
115 return BAD_VALUE;
116 }
117
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800118 Mutex::Autolock lock(mMutex);
119 freeAllBuffers();
120 mBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800121 mCurrentTexture = INVALID_BUFFER_SLOT;
122 mLastQueued = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800123 return OK;
124}
125
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700126status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
127{
128 Mutex::Autolock lock(mMutex);
129 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
130 mDefaultWidth = w;
131 mDefaultHeight = h;
132 }
133 return OK;
134}
135
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800136sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf,
137 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800138 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800139 Mutex::Autolock lock(mMutex);
140 if (buf < 0 || mBufferCount <= buf) {
141 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
142 mBufferCount, buf);
143 return 0;
144 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700145 if ((w && !h) || (!w & h)) {
146 LOGE("requestBuffer: invalid size: w=%u, h=%u: %d", w, h, buf);
147 return 0;
148 }
149
150 const bool useDefaultSize = !w && !h;
151 if (useDefaultSize) {
152 // use the default size
153 w = mDefaultWidth;
154 h = mDefaultHeight;
155 }
156
157 const bool updateFormat = (format != 0);
158 if (!updateFormat) {
159 // keep the current (or default) format
160 format = mPixelFormat;
161 }
162
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800163 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800164 sp<GraphicBuffer> graphicBuffer(
165 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800166 if (graphicBuffer == 0) {
167 LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed");
168 } else {
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700169 mUseDefaultSize = useDefaultSize;
170 if (updateFormat) {
171 mPixelFormat = format;
172 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800173 mSlots[buf].mGraphicBuffer = graphicBuffer;
174 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
175 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
176 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
177 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
178 }
179 }
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();
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700278 image = createImage(dpy, mSlots[mLastQueued].mGraphicBuffer);
Jamie Gennis9a78c902011-01-12 18:30:40 -0800279 mSlots[mLastQueued].mEglImage = image;
280 mSlots[mLastQueued].mEglDisplay = dpy;
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700281 if (image == EGL_NO_IMAGE_KHR) {
282 // NOTE: if dpy was invalid, createImage() is guaranteed to
283 // fail. so we'd end up here.
284 return -EINVAL;
285 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800286 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800287
288 GLint error;
289 while ((error = glGetError()) != GL_NO_ERROR) {
290 LOGE("GL error cleared before updating SurfaceTexture: %#04x", error);
291 }
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700292
293 GLenum target = getTextureTarget(
294 mSlots[mLastQueued].mGraphicBuffer->format);
295 if (target != mCurrentTextureTarget) {
296 glDeleteTextures(1, &mTexName);
297 }
298 glBindTexture(target, mTexName);
299 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)image);
300
Jamie Gennis0eb88512011-01-26 11:52:02 -0800301 bool failed = false;
302 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800303 LOGE("error binding external texture image %p (slot %d): %#04x",
Jamie Gennis9a78c902011-01-12 18:30:40 -0800304 image, mLastQueued, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800305 failed = true;
306 }
307 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800308 return -EINVAL;
309 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800310
311 // Update the SurfaceTexture state.
312 mCurrentTexture = mLastQueued;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700313 mCurrentTextureTarget = target;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800314 mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer;
315 mCurrentCrop = mLastQueuedCrop;
316 mCurrentTransform = mLastQueuedTransform;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800317 mCurrentTimestamp = mLastQueuedTimestamp;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700318 } else {
319 // We always bind the texture even if we don't update its contents.
320 glBindTexture(mCurrentTextureTarget, mTexName);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800321 }
322 return OK;
323}
324
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700325bool SurfaceTexture::isExternalFormat(uint32_t format)
326{
327 switch (format) {
328 // supported YUV formats
329 case HAL_PIXEL_FORMAT_YV12:
330 // Legacy/deprecated YUV formats
331 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
332 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
333 case HAL_PIXEL_FORMAT_YCbCr_422_I:
334 return true;
335 }
336
337 // Any OEM format needs to be considered
338 if (format>=0x100 && format<=0x1FF)
339 return true;
340
341 return false;
342}
343
344GLenum SurfaceTexture::getTextureTarget(uint32_t format)
345{
346 GLenum target = GL_TEXTURE_2D;
347#if defined(GL_OES_EGL_image_external)
348 if (isExternalFormat(format)) {
349 target = GL_TEXTURE_EXTERNAL_OES;
350 }
351#endif
352 return target;
353}
354
355GLenum SurfaceTexture::getCurrentTextureTarget() const {
356 Mutex::Autolock lock(mMutex);
357 return mCurrentTextureTarget;
358}
359
Jamie Gennisf238e282011-01-09 16:33:17 -0800360void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800361 LOGV("SurfaceTexture::getTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800362 Mutex::Autolock lock(mMutex);
363
Jamie Gennisa214c642011-01-14 13:53:31 -0800364 float xform[16];
365 for (int i = 0; i < 16; i++) {
366 xform[i] = mtxIdentity[i];
367 }
368 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
369 float result[16];
370 mtxMul(result, xform, mtxFlipH);
371 for (int i = 0; i < 16; i++) {
372 xform[i] = result[i];
373 }
374 }
375 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
376 float result[16];
377 mtxMul(result, xform, mtxFlipV);
378 for (int i = 0; i < 16; i++) {
379 xform[i] = result[i];
380 }
381 }
382 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
383 float result[16];
384 mtxMul(result, xform, mtxRot90);
385 for (int i = 0; i < 16; i++) {
386 xform[i] = result[i];
387 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800388 }
389
390 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800391 float tx, ty, sx, sy;
392 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800393 // In order to prevent bilinear sampling at the of the crop rectangle we
394 // may need to shrink it by 2 texels in each direction. Normally this
395 // would just need to take 1/2 a texel off each end, but because the
396 // chroma channels will likely be subsampled we need to chop off a whole
397 // texel. This will cause artifacts if someone does nearest sampling
398 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
399 // accomodate the bilinear and nearest sampling uses.
400 //
401 // If nearest sampling turns out to be a desirable usage of these
402 // textures then we could add the ability to switch a SurfaceTexture to
403 // nearest-mode. Preferably, however, the image producers (video
404 // decoder, camera, etc.) would simply not use a crop rectangle (or at
405 // least not tell the framework about it) so that the GPU can do the
406 // correct edge behavior.
407 int xshrink = 0, yshrink = 0;
408 if (mCurrentCrop.left > 0) {
409 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
410 xshrink++;
411 } else {
412 tx = 0.0f;
413 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700414 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800415 xshrink++;
416 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700417 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800418 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
419 float(buf->getHeight());
420 yshrink++;
421 } else {
422 ty = 0.0f;
423 }
424 if (mCurrentCrop.top > 0) {
425 yshrink++;
426 }
427 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
428 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800429 } else {
430 tx = 0.0f;
431 ty = 0.0f;
432 sx = 1.0f;
433 sy = 1.0f;
434 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800435 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800436 sx, 0, 0, 0,
437 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800438 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800439 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800440 };
441
Jamie Gennisa214c642011-01-14 13:53:31 -0800442 float mtxBeforeFlipV[16];
443 mtxMul(mtxBeforeFlipV, crop, xform);
444
445 // SurfaceFlinger expects the top of its window textures to be at a Y
446 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
447 // want to expose this to applications, however, so we must add an
448 // additional vertical flip to the transform after all the other transforms.
449 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800450}
451
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800452nsecs_t SurfaceTexture::getTimestamp() {
453 LOGV("SurfaceTexture::getTimestamp");
454 Mutex::Autolock lock(mMutex);
455 return mCurrentTimestamp;
456}
457
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800458void SurfaceTexture::setFrameAvailableListener(
459 const sp<FrameAvailableListener>& l) {
460 LOGV("SurfaceTexture::setFrameAvailableListener");
461 Mutex::Autolock lock(mMutex);
462 mFrameAvailableListener = l;
463}
464
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800465sp<IBinder> SurfaceTexture::getAllocator() {
466 LOGV("SurfaceTexture::getAllocator");
467 return mGraphicBufferAlloc->asBinder();
468}
469
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800470void SurfaceTexture::freeAllBuffers() {
471 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
472 mSlots[i].mGraphicBuffer = 0;
473 mSlots[i].mOwnedByClient = false;
474 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
475 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
476 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
477 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
478 }
479 }
480}
481
482EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
483 const sp<GraphicBuffer>& graphicBuffer) {
484 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
485 EGLint attrs[] = {
486 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
487 EGL_NONE,
488 };
489 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
490 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700491 if (image == EGL_NO_IMAGE_KHR) {
492 EGLint error = eglGetError();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800493 LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800494 }
495 return image;
496}
497
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700498sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
499 Mutex::Autolock lock(mMutex);
500 return mCurrentTextureBuf;
501}
502
503Rect SurfaceTexture::getCurrentCrop() const {
504 Mutex::Autolock lock(mMutex);
505 return mCurrentCrop;
506}
507
508uint32_t SurfaceTexture::getCurrentTransform() const {
509 Mutex::Autolock lock(mMutex);
510 return mCurrentTransform;
511}
512
513
Jamie Gennisf238e282011-01-09 16:33:17 -0800514static void mtxMul(float out[16], const float a[16], const float b[16]) {
515 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
516 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
517 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
518 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
519
520 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
521 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
522 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
523 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
524
525 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
526 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
527 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
528 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
529
530 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
531 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
532 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
533 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
534}
535
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800536}; // namespace android