blob: 5c6d71b1501bc623ddfe5b66680939c3c8df93dc [file] [log] [blame]
Jamie Gennis68e4a7a2010-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 Gennis7dc00d52011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis68e4a7a2010-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 Gennisf7acf162011-01-12 18:30:40 -080032#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080033
34#include <utils/Log.h>
35
36namespace android {
37
Jamie Gennisb598fb92011-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 Gennis68e4a7a2010-12-20 11:27:26 -080078SurfaceTexture::SurfaceTexture(GLuint tex) :
79 mBufferCount(MIN_BUFFER_SLOTS), mCurrentTexture(INVALID_BUFFER_SLOT),
Jamie Gennis26722262011-02-04 13:46:38 -080080 mCurrentTransform(0), mLastQueued(INVALID_BUFFER_SLOT),
81 mLastQueuedTransform(0), mNextTransform(0), mTexName(tex) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -080082 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennisfd804f32011-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 Gennisf7acf162011-01-12 18:30:40 -080088 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
89 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080090}
91
92SurfaceTexture::~SurfaceTexture() {
Jamie Gennis7dc00d52011-01-09 13:24:09 -080093 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -080094 freeAllBuffers();
95}
96
97status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -080098 LOGV("SurfaceTexture::setBufferCount");
Jamie Gennis96dcc972011-02-27 14:10:20 -080099
100 if (bufferCount < MIN_BUFFER_SLOTS) {
101 return BAD_VALUE;
102 }
103
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800104 Mutex::Autolock lock(mMutex);
105 freeAllBuffers();
106 mBufferCount = bufferCount;
Jamie Gennisd369dc42011-01-09 13:25:39 -0800107 mCurrentTexture = INVALID_BUFFER_SLOT;
108 mLastQueued = INVALID_BUFFER_SLOT;
Jamie Gennis68e4a7a2010-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 Gennis7dc00d52011-01-09 13:24:09 -0800114 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis68e4a7a2010-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 Gennisf7acf162011-01-12 18:30:40 -0800122 sp<GraphicBuffer> graphicBuffer(
123 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
Jamie Gennis68e4a7a2010-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 Gennisf7acf162011-01-12 18:30:40 -0800133 mAllocdBuffers.add(graphicBuffer);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800134 }
135 return graphicBuffer;
136}
137
138status_t SurfaceTexture::dequeueBuffer(int *buf) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800139 LOGV("SurfaceTexture::dequeueBuffer");
Jamie Gennis68e4a7a2010-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 Gennisa7eacc12011-01-11 15:00:09 -0800143 if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) {
Jamie Gennis68e4a7a2010-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 Gennis7dc00d52011-01-09 13:24:09 -0800157 LOGV("SurfaceTexture::queueBuffer");
Jamie Gennis68e4a7a2010-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 Gennisb598fb92011-01-09 16:33:17 -0800173 mLastQueuedCrop = mNextCrop;
174 mLastQueuedTransform = mNextTransform;
Jamie Gennis376590d2011-01-13 14:43:36 -0800175 if (mFrameAvailableListener != 0) {
176 mFrameAvailableListener->onFrameAvailable();
177 }
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800178 return OK;
179}
180
181void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800182 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis68e4a7a2010-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 Gennisb598fb92011-01-09 16:33:17 -0800195status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800196 LOGV("SurfaceTexture::setCrop");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800197 Mutex::Autolock lock(mMutex);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800198 mNextCrop = crop;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800199 return OK;
200}
201
202status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800203 LOGV("SurfaceTexture::setTransform");
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800204 Mutex::Autolock lock(mMutex);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800205 mNextTransform = transform;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800206 return OK;
207}
208
209status_t SurfaceTexture::updateTexImage() {
Jamie Gennis7dc00d52011-01-09 13:24:09 -0800210 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis68e4a7a2010-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 Gennisb598fb92011-01-09 16:33:17 -0800219 // Update the GL texture object.
Jamie Gennisf7acf162011-01-12 18:30:40 -0800220 EGLImageKHR image = mSlots[mLastQueued].mEglImage;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800221 if (image == EGL_NO_IMAGE_KHR) {
222 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennisf7acf162011-01-12 18:30:40 -0800223 sp<GraphicBuffer> graphicBuffer = mSlots[mLastQueued].mGraphicBuffer;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800224 image = createImage(dpy, graphicBuffer);
Jamie Gennisf7acf162011-01-12 18:30:40 -0800225 mSlots[mLastQueued].mEglImage = image;
226 mSlots[mLastQueued].mEglDisplay = dpy;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800227 }
Jamie Gennis79d01fe2011-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 Gennis68e4a7a2010-12-20 11:27:26 -0800233 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800234 bool failed = false;
235 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800236 LOGE("error binding external texture image %p (slot %d): %#04x",
Jamie Gennisf7acf162011-01-12 18:30:40 -0800237 image, mLastQueued, error);
Jamie Gennis79d01fe2011-01-26 11:52:02 -0800238 failed = true;
239 }
240 if (failed) {
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800241 return -EINVAL;
242 }
Jamie Gennisf7acf162011-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 Gennis68e4a7a2010-12-20 11:27:26 -0800249 }
250 return OK;
251}
252
Jamie Gennisb598fb92011-01-09 16:33:17 -0800253void SurfaceTexture::getTransformMatrix(float mtx[16]) {
254 LOGV("SurfaceTexture::updateTexImage");
255 Mutex::Autolock lock(mMutex);
256
Jamie Gennis0fb736c2011-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 Gennisb598fb92011-01-09 16:33:17 -0800281 }
282
283 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800284 float tx, ty, sx, sy;
285 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800286 // In order to prevent bilinear sampling at the of the crop rectangle we
287 // may need to shrink it by 2 texels in each direction. Normally this
288 // would just need to take 1/2 a texel off each end, but because the
289 // chroma channels will likely be subsampled we need to chop off a whole
290 // texel. This will cause artifacts if someone does nearest sampling
291 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
292 // accomodate the bilinear and nearest sampling uses.
293 //
294 // If nearest sampling turns out to be a desirable usage of these
295 // textures then we could add the ability to switch a SurfaceTexture to
296 // nearest-mode. Preferably, however, the image producers (video
297 // decoder, camera, etc.) would simply not use a crop rectangle (or at
298 // least not tell the framework about it) so that the GPU can do the
299 // correct edge behavior.
300 int xshrink = 0, yshrink = 0;
301 if (mCurrentCrop.left > 0) {
302 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
303 xshrink++;
304 } else {
305 tx = 0.0f;
306 }
307 if (mCurrentCrop.right < buf->getWidth()) {
308 xshrink++;
309 }
310 if (mCurrentCrop.bottom < buf->getHeight()) {
311 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
312 float(buf->getHeight());
313 yshrink++;
314 } else {
315 ty = 0.0f;
316 }
317 if (mCurrentCrop.top > 0) {
318 yshrink++;
319 }
320 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
321 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800322 } else {
323 tx = 0.0f;
324 ty = 0.0f;
325 sx = 1.0f;
326 sy = 1.0f;
327 }
Jamie Gennisb598fb92011-01-09 16:33:17 -0800328 float crop[16] = {
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800329 sx, 0, 0, 0,
330 0, sy, 0, 0,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800331 0, 0, 1, 0,
Jamie Gennisf3cedb62011-03-10 16:24:46 -0800332 tx, ty, 0, 1,
Jamie Gennisb598fb92011-01-09 16:33:17 -0800333 };
334
Jamie Gennis0fb736c2011-01-14 13:53:31 -0800335 float mtxBeforeFlipV[16];
336 mtxMul(mtxBeforeFlipV, crop, xform);
337
338 // SurfaceFlinger expects the top of its window textures to be at a Y
339 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
340 // want to expose this to applications, however, so we must add an
341 // additional vertical flip to the transform after all the other transforms.
342 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisb598fb92011-01-09 16:33:17 -0800343}
344
Jamie Gennis376590d2011-01-13 14:43:36 -0800345void SurfaceTexture::setFrameAvailableListener(
346 const sp<FrameAvailableListener>& l) {
347 LOGV("SurfaceTexture::setFrameAvailableListener");
348 Mutex::Autolock lock(mMutex);
349 mFrameAvailableListener = l;
350}
351
Jamie Gennis83bac212011-02-02 15:31:47 -0800352sp<IBinder> SurfaceTexture::getAllocator() {
353 LOGV("SurfaceTexture::getAllocator");
354 return mGraphicBufferAlloc->asBinder();
355}
356
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800357void SurfaceTexture::freeAllBuffers() {
358 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
359 mSlots[i].mGraphicBuffer = 0;
360 mSlots[i].mOwnedByClient = false;
361 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
362 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
363 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
364 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
365 }
366 }
Jamie Gennisf7acf162011-01-12 18:30:40 -0800367
368 int exceptBuf = -1;
369 for (size_t i = 0; i < mAllocdBuffers.size(); i++) {
370 if (mAllocdBuffers[i] == mCurrentTextureBuf) {
371 exceptBuf = i;
372 break;
373 }
374 }
375 mAllocdBuffers.clear();
376 if (exceptBuf >= 0) {
377 mAllocdBuffers.add(mCurrentTextureBuf);
378 }
379 mGraphicBufferAlloc->freeAllGraphicBuffersExcept(exceptBuf);
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800380}
381
382EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
383 const sp<GraphicBuffer>& graphicBuffer) {
384 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
385 EGLint attrs[] = {
386 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
387 EGL_NONE,
388 };
389 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
390 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
391 EGLint error = eglGetError();
392 if (error != EGL_SUCCESS) {
393 LOGE("error creating EGLImage: %#x", error);
394 } else if (image == EGL_NO_IMAGE_KHR) {
395 LOGE("no error reported, but no image was returned by "
396 "eglCreateImageKHR");
397 }
398 return image;
399}
400
Jamie Gennisb598fb92011-01-09 16:33:17 -0800401static void mtxMul(float out[16], const float a[16], const float b[16]) {
402 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
403 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
404 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
405 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
406
407 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
408 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
409 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
410 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
411
412 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
413 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
414 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
415 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
416
417 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
418 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
419 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
420 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
421}
422
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800423}; // namespace android