blob: 3bed959861726ed7b340698475f0b5bc429f6d38 [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 Gennis9d4d6c12011-02-27 14:10:20 -080099
100 if (bufferCount < MIN_BUFFER_SLOTS) {
101 return BAD_VALUE;
102 }
103
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800104 Mutex::Autolock lock(mMutex);
105 freeAllBuffers();
106 mBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800107 mCurrentTexture = INVALID_BUFFER_SLOT;
108 mLastQueued = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800109 return OK;
110}
111
112sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf,
113 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800114 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800115 Mutex::Autolock lock(mMutex);
116 if (buf < 0 || mBufferCount <= buf) {
117 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
118 mBufferCount, buf);
119 return 0;
120 }
121 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800122 sp<GraphicBuffer> graphicBuffer(
123 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800124 if (graphicBuffer == 0) {
125 LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed");
126 } else {
127 mSlots[buf].mGraphicBuffer = graphicBuffer;
128 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
129 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
130 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
131 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
132 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800133 mAllocdBuffers.add(graphicBuffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800134 }
135 return graphicBuffer;
136}
137
138status_t SurfaceTexture::dequeueBuffer(int *buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800139 LOGV("SurfaceTexture::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800140 Mutex::Autolock lock(mMutex);
141 int found = INVALID_BUFFER_SLOT;
142 for (int i = 0; i < mBufferCount; i++) {
Jamie Gennis235c4242011-01-11 15:00:09 -0800143 if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800144 mSlots[i].mOwnedByClient = true;
145 found = i;
146 break;
147 }
148 }
149 if (found == INVALID_BUFFER_SLOT) {
150 return -EBUSY;
151 }
152 *buf = found;
153 return OK;
154}
155
156status_t SurfaceTexture::queueBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800157 LOGV("SurfaceTexture::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800158 Mutex::Autolock lock(mMutex);
159 if (buf < 0 || mBufferCount <= buf) {
160 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
161 mBufferCount, buf);
162 return -EINVAL;
163 } else if (!mSlots[buf].mOwnedByClient) {
164 LOGE("queueBuffer: slot %d is not owned by the client", buf);
165 return -EINVAL;
166 } else if (mSlots[buf].mGraphicBuffer == 0) {
167 LOGE("queueBuffer: slot %d was enqueued without requesting a buffer",
168 buf);
169 return -EINVAL;
170 }
171 mSlots[buf].mOwnedByClient = false;
172 mLastQueued = buf;
Jamie Gennisf238e282011-01-09 16:33:17 -0800173 mLastQueuedCrop = mNextCrop;
174 mLastQueuedTransform = mNextTransform;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800175 if (mFrameAvailableListener != 0) {
176 mFrameAvailableListener->onFrameAvailable();
177 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800178 return OK;
179}
180
181void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800182 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800183 Mutex::Autolock lock(mMutex);
184 if (buf < 0 || mBufferCount <= buf) {
185 LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount,
186 buf);
187 return;
188 } else if (!mSlots[buf].mOwnedByClient) {
189 LOGE("cancelBuffer: slot %d is not owned by the client", buf);
190 return;
191 }
192 mSlots[buf].mOwnedByClient = false;
193}
194
Jamie Gennisf238e282011-01-09 16:33:17 -0800195status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800196 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800197 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800198 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800199 return OK;
200}
201
202status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800203 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800204 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800205 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800206 return OK;
207}
208
209status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800210 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800211 Mutex::Autolock lock(mMutex);
212
213 // We always bind the texture even if we don't update its contents.
214 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
215
216 // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT,
217 // so this check will fail until a buffer gets queued.
218 if (mCurrentTexture != mLastQueued) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800219 // Update the GL texture object.
Jamie Gennis9a78c902011-01-12 18:30:40 -0800220 EGLImageKHR image = mSlots[mLastQueued].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800221 if (image == EGL_NO_IMAGE_KHR) {
222 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis9a78c902011-01-12 18:30:40 -0800223 sp<GraphicBuffer> graphicBuffer = mSlots[mLastQueued].mGraphicBuffer;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800224 image = createImage(dpy, graphicBuffer);
Jamie Gennis9a78c902011-01-12 18:30:40 -0800225 mSlots[mLastQueued].mEglImage = image;
226 mSlots[mLastQueued].mEglDisplay = dpy;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800227 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800228
229 GLint error;
230 while ((error = glGetError()) != GL_NO_ERROR) {
231 LOGE("GL error cleared before updating SurfaceTexture: %#04x", error);
232 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800233 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800234 bool failed = false;
235 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800236 LOGE("error binding external texture image %p (slot %d): %#04x",
Jamie Gennis9a78c902011-01-12 18:30:40 -0800237 image, mLastQueued, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800238 failed = true;
239 }
240 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800241 return -EINVAL;
242 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800243
244 // Update the SurfaceTexture state.
245 mCurrentTexture = mLastQueued;
246 mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer;
247 mCurrentCrop = mLastQueuedCrop;
248 mCurrentTransform = mLastQueuedTransform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800249 }
250 return OK;
251}
252
Jamie Gennisf238e282011-01-09 16:33:17 -0800253void SurfaceTexture::getTransformMatrix(float mtx[16]) {
254 LOGV("SurfaceTexture::updateTexImage");
255 Mutex::Autolock lock(mMutex);
256
Jamie Gennisa214c642011-01-14 13:53:31 -0800257 float xform[16];
258 for (int i = 0; i < 16; i++) {
259 xform[i] = mtxIdentity[i];
260 }
261 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
262 float result[16];
263 mtxMul(result, xform, mtxFlipH);
264 for (int i = 0; i < 16; i++) {
265 xform[i] = result[i];
266 }
267 }
268 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
269 float result[16];
270 mtxMul(result, xform, mtxFlipV);
271 for (int i = 0; i < 16; i++) {
272 xform[i] = result[i];
273 }
274 }
275 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
276 float result[16];
277 mtxMul(result, xform, mtxRot90);
278 for (int i = 0; i < 16; i++) {
279 xform[i] = result[i];
280 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800281 }
282
283 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800284 float tx, ty, sx, sy;
285 if (!mCurrentCrop.isEmpty()) {
286 tx = float(mCurrentCrop.left) / float(buf->getWidth());
287 ty = float(buf->getHeight() - mCurrentCrop.bottom) /
288 float(buf->getHeight());
289 sx = float(mCurrentCrop.width()) / float(buf->getWidth());
290 sy = float(mCurrentCrop.height()) / float(buf->getHeight());
291 } else {
292 tx = 0.0f;
293 ty = 0.0f;
294 sx = 1.0f;
295 sy = 1.0f;
296 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800297 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800298 sx, 0, 0, 0,
299 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800300 0, 0, 1, 0,
Jamie Gennisa214c642011-01-14 13:53:31 -0800301 sx*tx, sy*ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800302 };
303
Jamie Gennisa214c642011-01-14 13:53:31 -0800304 float mtxBeforeFlipV[16];
305 mtxMul(mtxBeforeFlipV, crop, xform);
306
307 // SurfaceFlinger expects the top of its window textures to be at a Y
308 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
309 // want to expose this to applications, however, so we must add an
310 // additional vertical flip to the transform after all the other transforms.
311 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800312}
313
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800314void SurfaceTexture::setFrameAvailableListener(
315 const sp<FrameAvailableListener>& l) {
316 LOGV("SurfaceTexture::setFrameAvailableListener");
317 Mutex::Autolock lock(mMutex);
318 mFrameAvailableListener = l;
319}
320
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800321sp<IBinder> SurfaceTexture::getAllocator() {
322 LOGV("SurfaceTexture::getAllocator");
323 return mGraphicBufferAlloc->asBinder();
324}
325
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800326void SurfaceTexture::freeAllBuffers() {
327 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
328 mSlots[i].mGraphicBuffer = 0;
329 mSlots[i].mOwnedByClient = false;
330 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
331 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
332 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
333 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
334 }
335 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800336
337 int exceptBuf = -1;
338 for (size_t i = 0; i < mAllocdBuffers.size(); i++) {
339 if (mAllocdBuffers[i] == mCurrentTextureBuf) {
340 exceptBuf = i;
341 break;
342 }
343 }
344 mAllocdBuffers.clear();
345 if (exceptBuf >= 0) {
346 mAllocdBuffers.add(mCurrentTextureBuf);
347 }
348 mGraphicBufferAlloc->freeAllGraphicBuffersExcept(exceptBuf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800349}
350
351EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
352 const sp<GraphicBuffer>& graphicBuffer) {
353 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
354 EGLint attrs[] = {
355 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
356 EGL_NONE,
357 };
358 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
359 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
360 EGLint error = eglGetError();
361 if (error != EGL_SUCCESS) {
362 LOGE("error creating EGLImage: %#x", error);
363 } else if (image == EGL_NO_IMAGE_KHR) {
364 LOGE("no error reported, but no image was returned by "
365 "eglCreateImageKHR");
366 }
367 return image;
368}
369
Jamie Gennisf238e282011-01-09 16:33:17 -0800370static void mtxMul(float out[16], const float a[16], const float b[16]) {
371 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
372 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
373 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
374 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
375
376 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
377 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
378 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
379 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
380
381 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
382 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
383 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
384 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
385
386 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
387 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
388 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
389 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
390}
391
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800392}; // namespace android