blob: 223cf091e7bda2b1a56cc1840380812d926407ba [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
30#include <surfaceflinger/ISurfaceComposer.h>
31#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080032#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080033
34#include <utils/Log.h>
35
36namespace android {
37
Jamie Gennisf238e282011-01-09 16:33:17 -080038// Transform matrices
39static float mtxIdentity[16] = {
40 1, 0, 0, 0,
41 0, 1, 0, 0,
42 0, 0, 1, 0,
43 0, 0, 0, 1,
44};
45static float mtxFlipH[16] = {
46 -1, 0, 0, 0,
47 0, 1, 0, 0,
48 0, 0, 1, 0,
49 1, 0, 0, 1,
50};
51static float mtxFlipV[16] = {
52 1, 0, 0, 0,
53 0, -1, 0, 0,
54 0, 0, 1, 0,
55 0, 1, 0, 1,
56};
57static float mtxRot90[16] = {
58 0, 1, 0, 0,
59 -1, 0, 0, 0,
60 0, 0, 1, 0,
61 1, 0, 0, 1,
62};
63static float mtxRot180[16] = {
64 -1, 0, 0, 0,
65 0, -1, 0, 0,
66 0, 0, 1, 0,
67 1, 1, 0, 1,
68};
69static float mtxRot270[16] = {
70 0, -1, 0, 0,
71 1, 0, 0, 0,
72 0, 0, 1, 0,
73 0, 1, 0, 1,
74};
75
76static void mtxMul(float out[16], const float a[16], const float b[16]);
77
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080078SurfaceTexture::SurfaceTexture(GLuint tex) :
79 mBufferCount(MIN_BUFFER_SLOTS), mCurrentTexture(INVALID_BUFFER_SLOT),
Jamie Gennisf73935b2011-02-04 13:46:38 -080080 mCurrentTransform(0), mLastQueued(INVALID_BUFFER_SLOT),
81 mLastQueuedTransform(0), mNextTransform(0), mTexName(tex) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080082 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis3461e0f2011-01-07 16:05:47 -080083 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
84 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
85 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
86 mSlots[i].mOwnedByClient = false;
87 }
Jamie Gennis9a78c902011-01-12 18:30:40 -080088 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
89 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080090}
91
92SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -080093 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080094 freeAllBuffers();
95}
96
97status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080098 LOGV("SurfaceTexture::setBufferCount");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080099 Mutex::Autolock lock(mMutex);
100 freeAllBuffers();
101 mBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800102 mCurrentTexture = INVALID_BUFFER_SLOT;
103 mLastQueued = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800104 return OK;
105}
106
107sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf,
108 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800109 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800110 Mutex::Autolock lock(mMutex);
111 if (buf < 0 || mBufferCount <= buf) {
112 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
113 mBufferCount, buf);
114 return 0;
115 }
116 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800117 sp<GraphicBuffer> graphicBuffer(
118 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800119 if (graphicBuffer == 0) {
120 LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed");
121 } else {
122 mSlots[buf].mGraphicBuffer = graphicBuffer;
123 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
124 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
125 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
126 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
127 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800128 mAllocdBuffers.add(graphicBuffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129 }
130 return graphicBuffer;
131}
132
133status_t SurfaceTexture::dequeueBuffer(int *buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800134 LOGV("SurfaceTexture::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135 Mutex::Autolock lock(mMutex);
136 int found = INVALID_BUFFER_SLOT;
137 for (int i = 0; i < mBufferCount; i++) {
Jamie Gennis235c4242011-01-11 15:00:09 -0800138 if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800139 mSlots[i].mOwnedByClient = true;
140 found = i;
141 break;
142 }
143 }
144 if (found == INVALID_BUFFER_SLOT) {
145 return -EBUSY;
146 }
147 *buf = found;
148 return OK;
149}
150
151status_t SurfaceTexture::queueBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800152 LOGV("SurfaceTexture::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800153 Mutex::Autolock lock(mMutex);
154 if (buf < 0 || mBufferCount <= buf) {
155 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
156 mBufferCount, buf);
157 return -EINVAL;
158 } else if (!mSlots[buf].mOwnedByClient) {
159 LOGE("queueBuffer: slot %d is not owned by the client", buf);
160 return -EINVAL;
161 } else if (mSlots[buf].mGraphicBuffer == 0) {
162 LOGE("queueBuffer: slot %d was enqueued without requesting a buffer",
163 buf);
164 return -EINVAL;
165 }
166 mSlots[buf].mOwnedByClient = false;
167 mLastQueued = buf;
Jamie Gennisf238e282011-01-09 16:33:17 -0800168 mLastQueuedCrop = mNextCrop;
169 mLastQueuedTransform = mNextTransform;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800170 if (mFrameAvailableListener != 0) {
171 mFrameAvailableListener->onFrameAvailable();
172 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800173 return OK;
174}
175
176void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800177 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178 Mutex::Autolock lock(mMutex);
179 if (buf < 0 || mBufferCount <= buf) {
180 LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount,
181 buf);
182 return;
183 } else if (!mSlots[buf].mOwnedByClient) {
184 LOGE("cancelBuffer: slot %d is not owned by the client", buf);
185 return;
186 }
187 mSlots[buf].mOwnedByClient = false;
188}
189
Jamie Gennisf238e282011-01-09 16:33:17 -0800190status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800191 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800192 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800193 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800194 return OK;
195}
196
197status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800198 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800199 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800200 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800201 return OK;
202}
203
204status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800205 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800206 Mutex::Autolock lock(mMutex);
207
208 // We always bind the texture even if we don't update its contents.
209 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
210
211 // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT,
212 // so this check will fail until a buffer gets queued.
213 if (mCurrentTexture != mLastQueued) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800214 // Update the GL texture object.
Jamie Gennis9a78c902011-01-12 18:30:40 -0800215 EGLImageKHR image = mSlots[mLastQueued].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800216 if (image == EGL_NO_IMAGE_KHR) {
217 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis9a78c902011-01-12 18:30:40 -0800218 sp<GraphicBuffer> graphicBuffer = mSlots[mLastQueued].mGraphicBuffer;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800219 image = createImage(dpy, graphicBuffer);
Jamie Gennis9a78c902011-01-12 18:30:40 -0800220 mSlots[mLastQueued].mEglImage = image;
221 mSlots[mLastQueued].mEglDisplay = dpy;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800222 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800223
224 GLint error;
225 while ((error = glGetError()) != GL_NO_ERROR) {
226 LOGE("GL error cleared before updating SurfaceTexture: %#04x", error);
227 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800228 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800229 bool failed = false;
230 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800231 LOGE("error binding external texture image %p (slot %d): %#04x",
Jamie Gennis9a78c902011-01-12 18:30:40 -0800232 image, mLastQueued, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800233 failed = true;
234 }
235 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 return -EINVAL;
237 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800238
239 // Update the SurfaceTexture state.
240 mCurrentTexture = mLastQueued;
241 mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer;
242 mCurrentCrop = mLastQueuedCrop;
243 mCurrentTransform = mLastQueuedTransform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800244 }
245 return OK;
246}
247
Jamie Gennisf238e282011-01-09 16:33:17 -0800248void SurfaceTexture::getTransformMatrix(float mtx[16]) {
249 LOGV("SurfaceTexture::updateTexImage");
250 Mutex::Autolock lock(mMutex);
251
Jamie Gennisa214c642011-01-14 13:53:31 -0800252 float xform[16];
253 for (int i = 0; i < 16; i++) {
254 xform[i] = mtxIdentity[i];
255 }
256 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
257 float result[16];
258 mtxMul(result, xform, mtxFlipH);
259 for (int i = 0; i < 16; i++) {
260 xform[i] = result[i];
261 }
262 }
263 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
264 float result[16];
265 mtxMul(result, xform, mtxFlipV);
266 for (int i = 0; i < 16; i++) {
267 xform[i] = result[i];
268 }
269 }
270 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
271 float result[16];
272 mtxMul(result, xform, mtxRot90);
273 for (int i = 0; i < 16; i++) {
274 xform[i] = result[i];
275 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800276 }
277
278 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800279 float tx, ty, sx, sy;
280 if (!mCurrentCrop.isEmpty()) {
281 tx = float(mCurrentCrop.left) / float(buf->getWidth());
282 ty = float(buf->getHeight() - mCurrentCrop.bottom) /
283 float(buf->getHeight());
284 sx = float(mCurrentCrop.width()) / float(buf->getWidth());
285 sy = float(mCurrentCrop.height()) / float(buf->getHeight());
286 } else {
287 tx = 0.0f;
288 ty = 0.0f;
289 sx = 1.0f;
290 sy = 1.0f;
291 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800292 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800293 sx, 0, 0, 0,
294 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800295 0, 0, 1, 0,
Jamie Gennisa214c642011-01-14 13:53:31 -0800296 sx*tx, sy*ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800297 };
298
Jamie Gennisa214c642011-01-14 13:53:31 -0800299 float mtxBeforeFlipV[16];
300 mtxMul(mtxBeforeFlipV, crop, xform);
301
302 // SurfaceFlinger expects the top of its window textures to be at a Y
303 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
304 // want to expose this to applications, however, so we must add an
305 // additional vertical flip to the transform after all the other transforms.
306 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800307}
308
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800309void SurfaceTexture::setFrameAvailableListener(
310 const sp<FrameAvailableListener>& l) {
311 LOGV("SurfaceTexture::setFrameAvailableListener");
312 Mutex::Autolock lock(mMutex);
313 mFrameAvailableListener = l;
314}
315
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800316sp<IBinder> SurfaceTexture::getAllocator() {
317 LOGV("SurfaceTexture::getAllocator");
318 return mGraphicBufferAlloc->asBinder();
319}
320
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800321void SurfaceTexture::freeAllBuffers() {
322 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
323 mSlots[i].mGraphicBuffer = 0;
324 mSlots[i].mOwnedByClient = false;
325 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
326 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
327 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
328 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
329 }
330 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800331
332 int exceptBuf = -1;
333 for (size_t i = 0; i < mAllocdBuffers.size(); i++) {
334 if (mAllocdBuffers[i] == mCurrentTextureBuf) {
335 exceptBuf = i;
336 break;
337 }
338 }
339 mAllocdBuffers.clear();
340 if (exceptBuf >= 0) {
341 mAllocdBuffers.add(mCurrentTextureBuf);
342 }
343 mGraphicBufferAlloc->freeAllGraphicBuffersExcept(exceptBuf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800344}
345
346EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
347 const sp<GraphicBuffer>& graphicBuffer) {
348 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
349 EGLint attrs[] = {
350 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
351 EGL_NONE,
352 };
353 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
354 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
355 EGLint error = eglGetError();
356 if (error != EGL_SUCCESS) {
357 LOGE("error creating EGLImage: %#x", error);
358 } else if (image == EGL_NO_IMAGE_KHR) {
359 LOGE("no error reported, but no image was returned by "
360 "eglCreateImageKHR");
361 }
362 return image;
363}
364
Jamie Gennisf238e282011-01-09 16:33:17 -0800365static void mtxMul(float out[16], const float a[16], const float b[16]) {
366 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
367 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
368 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
369 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
370
371 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
372 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
373 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
374 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
375
376 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
377 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
378 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
379 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
380
381 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
382 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
383 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
384 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
385}
386
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800387}; // namespace android