blob: d6cc8ce03443e0796ce60cfc030cd1a9a293e189 [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 }
223 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
224 GLint error = glGetError();
225 if (error != GL_NO_ERROR) {
226 LOGE("error binding external texture image %p (slot %d): %#04x",
Jamie Gennis9a78c902011-01-12 18:30:40 -0800227 image, mLastQueued, error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800228 return -EINVAL;
229 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800230
231 // Update the SurfaceTexture state.
232 mCurrentTexture = mLastQueued;
233 mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer;
234 mCurrentCrop = mLastQueuedCrop;
235 mCurrentTransform = mLastQueuedTransform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 }
237 return OK;
238}
239
Jamie Gennisf238e282011-01-09 16:33:17 -0800240void SurfaceTexture::getTransformMatrix(float mtx[16]) {
241 LOGV("SurfaceTexture::updateTexImage");
242 Mutex::Autolock lock(mMutex);
243
Jamie Gennisa214c642011-01-14 13:53:31 -0800244 float xform[16];
245 for (int i = 0; i < 16; i++) {
246 xform[i] = mtxIdentity[i];
247 }
248 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
249 float result[16];
250 mtxMul(result, xform, mtxFlipH);
251 for (int i = 0; i < 16; i++) {
252 xform[i] = result[i];
253 }
254 }
255 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
256 float result[16];
257 mtxMul(result, xform, mtxFlipV);
258 for (int i = 0; i < 16; i++) {
259 xform[i] = result[i];
260 }
261 }
262 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
263 float result[16];
264 mtxMul(result, xform, mtxRot90);
265 for (int i = 0; i < 16; i++) {
266 xform[i] = result[i];
267 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800268 }
269
270 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800271 float tx, ty, sx, sy;
272 if (!mCurrentCrop.isEmpty()) {
273 tx = float(mCurrentCrop.left) / float(buf->getWidth());
274 ty = float(buf->getHeight() - mCurrentCrop.bottom) /
275 float(buf->getHeight());
276 sx = float(mCurrentCrop.width()) / float(buf->getWidth());
277 sy = float(mCurrentCrop.height()) / float(buf->getHeight());
278 } else {
279 tx = 0.0f;
280 ty = 0.0f;
281 sx = 1.0f;
282 sy = 1.0f;
283 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800284 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800285 sx, 0, 0, 0,
286 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800287 0, 0, 1, 0,
Jamie Gennisa214c642011-01-14 13:53:31 -0800288 sx*tx, sy*ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800289 };
290
Jamie Gennisa214c642011-01-14 13:53:31 -0800291 float mtxBeforeFlipV[16];
292 mtxMul(mtxBeforeFlipV, crop, xform);
293
294 // SurfaceFlinger expects the top of its window textures to be at a Y
295 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
296 // want to expose this to applications, however, so we must add an
297 // additional vertical flip to the transform after all the other transforms.
298 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800299}
300
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800301void SurfaceTexture::setFrameAvailableListener(
302 const sp<FrameAvailableListener>& l) {
303 LOGV("SurfaceTexture::setFrameAvailableListener");
304 Mutex::Autolock lock(mMutex);
305 mFrameAvailableListener = l;
306}
307
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800308sp<IBinder> SurfaceTexture::getAllocator() {
309 LOGV("SurfaceTexture::getAllocator");
310 return mGraphicBufferAlloc->asBinder();
311}
312
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800313void SurfaceTexture::freeAllBuffers() {
314 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
315 mSlots[i].mGraphicBuffer = 0;
316 mSlots[i].mOwnedByClient = false;
317 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
318 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
319 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
320 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
321 }
322 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800323
324 int exceptBuf = -1;
325 for (size_t i = 0; i < mAllocdBuffers.size(); i++) {
326 if (mAllocdBuffers[i] == mCurrentTextureBuf) {
327 exceptBuf = i;
328 break;
329 }
330 }
331 mAllocdBuffers.clear();
332 if (exceptBuf >= 0) {
333 mAllocdBuffers.add(mCurrentTextureBuf);
334 }
335 mGraphicBufferAlloc->freeAllGraphicBuffersExcept(exceptBuf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800336}
337
338EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
339 const sp<GraphicBuffer>& graphicBuffer) {
340 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
341 EGLint attrs[] = {
342 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
343 EGL_NONE,
344 };
345 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
346 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
347 EGLint error = eglGetError();
348 if (error != EGL_SUCCESS) {
349 LOGE("error creating EGLImage: %#x", error);
350 } else if (image == EGL_NO_IMAGE_KHR) {
351 LOGE("no error reported, but no image was returned by "
352 "eglCreateImageKHR");
353 }
354 return image;
355}
356
Jamie Gennisf238e282011-01-09 16:33:17 -0800357static void mtxMul(float out[16], const float a[16], const float b[16]) {
358 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
359 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
360 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
361 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
362
363 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
364 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
365 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
366 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
367
368 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
369 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
370 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
371 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
372
373 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
374 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
375 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
376 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
377}
378
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800379}; // namespace android