blob: a757bc4fe8073ebcc5e2b0f4570222ded49ade97 [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
Andy McFadden2adaf042012-12-18 09:49:45 -080017#define LOG_TAG "GLConsumer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jamie Gennise70d8b42011-01-09 13:24:09 -080019//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080020
21#define GL_GLEXT_PROTOTYPES
22#define EGL_EGLEXT_PROTOTYPES
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
Mathias Agopianad678e12013-07-23 17:28:53 -070028#include <cutils/compiler.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080029
Jamie Gennis9fea3422012-08-07 18:03:04 -070030#include <hardware/hardware.h>
31
Mathias Agopianca088332013-03-28 17:44:13 -070032#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080033#include <gui/IGraphicBufferAlloc.h>
34#include <gui/ISurfaceComposer.h>
35#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080036
Mathias Agopian90ac7992012-02-25 18:48:35 -080037#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070038#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080039
40#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070041#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080042#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080043
Andy McFadden97eba892012-12-11 15:21:45 -080044namespace android {
45
Andy McFadden2adaf042012-12-18 09:49:45 -080046// Macros for including the GLConsumer name in log messages
Steve Block6807e592011-10-20 11:56:00 +010047#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000048#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000049#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000050#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000051#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070052
Mathias Agopianad678e12013-07-23 17:28:53 -070053static const struct {
54 size_t width, height;
55 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070056} kDebugData = { 15, 12,
57 "___________________________________XX_XX_______X_X_____X_X____X_XXXXXXX_X____XXXXXXXXXXX__"
58 "___XX_XXX_XX_______XXXXXXX_________X___X_________X_____X__________________________________"
59};
Mathias Agopianad678e12013-07-23 17:28:53 -070060
Jamie Gennisf238e282011-01-09 16:33:17 -080061// Transform matrices
62static float mtxIdentity[16] = {
63 1, 0, 0, 0,
64 0, 1, 0, 0,
65 0, 0, 1, 0,
66 0, 0, 0, 1,
67};
68static float mtxFlipH[16] = {
69 -1, 0, 0, 0,
70 0, 1, 0, 0,
71 0, 0, 1, 0,
72 1, 0, 0, 1,
73};
74static float mtxFlipV[16] = {
75 1, 0, 0, 0,
76 0, -1, 0, 0,
77 0, 0, 1, 0,
78 0, 1, 0, 1,
79};
80static float mtxRot90[16] = {
81 0, 1, 0, 0,
82 -1, 0, 0, 0,
83 0, 0, 1, 0,
84 1, 0, 0, 1,
85};
Jamie Gennisf238e282011-01-09 16:33:17 -080086
87static void mtxMul(float out[16], const float a[16], const float b[16]);
88
Mathias Agopian9870c9b2013-08-08 17:46:48 -070089Mutex GLConsumer::sStaticInitLock;
90sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -070091
Mathias Agopiandb89edc2013-08-02 01:40:18 -070092GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, GLuint tex,
Mathias Agopian595264f2013-07-16 22:56:09 -070093 GLenum texTarget, bool useFenceSync, bool isControlledByApp) :
94 ConsumerBase(bq, isControlledByApp),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080095 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +020096 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -080097 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080098 mCurrentTimestamp(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +020099 mDefaultWidth(1),
100 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700101 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700102 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800103 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800104 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700105 mEglDisplay(EGL_NO_DISPLAY),
106 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700107 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
108 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800109{
Andy McFadden2adaf042012-12-18 09:49:45 -0800110 ST_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800111
Jamie Gennisfa28c352011-09-16 17:30:26 -0700112 memcpy(mCurrentTransformMatrix, mtxIdentity,
113 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700114
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700115 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800116}
117
Andy McFadden2adaf042012-12-18 09:49:45 -0800118status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700119 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700120 return mConsumer->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700121}
122
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800123
Andy McFadden2adaf042012-12-18 09:49:45 -0800124status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700125{
Daniel Lamb2675792012-02-23 14:35:13 -0800126 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700127 mDefaultWidth = w;
128 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700129 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700130}
131
Andy McFadden2adaf042012-12-18 09:49:45 -0800132status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800133 ATRACE_CALL();
134 ST_LOGV("updateTexImage");
135 Mutex::Autolock lock(mMutex);
136
137 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800138 ST_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800139 return NO_INIT;
140 }
141
142 // Make sure the EGL state is the same as in previous calls.
143 status_t err = checkAndUpdateEglStateLocked();
144 if (err != NO_ERROR) {
145 return err;
146 }
147
148 BufferQueue::BufferItem item;
149
150 // Acquire the next buffer.
151 // In asynchronous mode the list is guaranteed to be one buffer
152 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700153 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800154 if (err != NO_ERROR) {
155 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
156 // We always bind the texture even if we don't update its contents.
157 ST_LOGV("updateTexImage: no buffers were available");
158 glBindTexture(mTexTarget, mTexName);
159 err = NO_ERROR;
160 } else {
161 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
162 strerror(-err), err);
163 }
164 return err;
165 }
166
167 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700168 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800169 if (err != NO_ERROR) {
170 // We always bind the texture.
171 glBindTexture(mTexTarget, mTexName);
172 return err;
173 }
174
Andy McFadden97eba892012-12-11 15:21:45 -0800175 // Bind the new buffer to the GL texture, and wait until it's ready.
176 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700177}
178
Mathias Agopianad678e12013-07-23 17:28:53 -0700179
180status_t GLConsumer::releaseTexImage() {
181 ATRACE_CALL();
182 ST_LOGV("releaseTexImage");
183 Mutex::Autolock lock(mMutex);
184
185 if (mAbandoned) {
186 ST_LOGE("releaseTexImage: GLConsumer is abandoned!");
187 return NO_INIT;
188 }
189
190 // Make sure the EGL state is the same as in previous calls.
191 status_t err = checkAndUpdateEglStateLocked();
192 if (err != NO_ERROR) {
193 return err;
194 }
195
196 // Update the GLConsumer state.
197 int buf = mCurrentTexture;
198 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
199
200 ST_LOGV("releaseTexImage:(slot=%d", buf);
201
202 // Do whatever sync ops we need to do before releasing the slot.
203 err = syncForReleaseLocked(mEglDisplay);
204 if (err != NO_ERROR) {
205 ST_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
206 return err;
207 }
208
209 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
210 mEglDisplay, EGL_NO_SYNC_KHR);
211 if (err < NO_ERROR) {
212 ST_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
213 strerror(-err), err);
214 return err;
215 }
216
Mathias Agopianad678e12013-07-23 17:28:53 -0700217 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700218 mCurrentTextureBuf = getDebugTexImageBuffer();
Mathias Agopianad678e12013-07-23 17:28:53 -0700219 mCurrentCrop.makeInvalid();
220 mCurrentTransform = 0;
221 mCurrentScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
222 mCurrentTimestamp = 0;
223 mCurrentFence = Fence::NO_FENCE;
224
225 // bind a dummy texture
226 glBindTexture(mTexTarget, mTexName);
227 bindUnslottedBufferLocked(mEglDisplay);
228 }
229
230 return NO_ERROR;
231}
232
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700233sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
234 Mutex::Autolock _l(sStaticInitLock);
235 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
236 // The first time, create the debug texture in case the application
237 // continues to use it.
238 sp<GraphicBuffer> buffer = new GraphicBuffer(
239 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
240 GraphicBuffer::USAGE_SW_WRITE_RARELY);
241 uint32_t* bits;
242 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
243 size_t w = buffer->getStride();
244 size_t h = buffer->getHeight();
245 memset(bits, 0, w*h*4);
246 for (size_t y=0 ; y<kDebugData.height ; y++) {
247 for (size_t x=0 ; x<kDebugData.width ; x++) {
248 bits[x] = (kDebugData.bits[y*kDebugData.width+x] == 'X') ? 0xFF000000 : 0xFFFFFFFF;
249 }
250 bits += w;
251 }
252 buffer->unlock();
253 sReleasedTexImageBuffer = buffer;
254 }
255 return sReleasedTexImageBuffer;
256}
257
Andy McFadden1585c4d2013-06-28 13:52:40 -0700258status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item,
259 nsecs_t presentWhen) {
260 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700261 if (err != NO_ERROR) {
262 return err;
263 }
264
265 int slot = item->mBuf;
266 if (item->mGraphicBuffer != NULL) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800267 // This buffer has not been acquired before, so we must assume
268 // that any EGLImage in mEglSlots is stale.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700269 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800270 if (!eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage)) {
271 ST_LOGW("acquireBufferLocked: eglDestroyImageKHR failed for slot=%d",
272 slot);
273 // keep going
274 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700275 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
276 }
277 }
278
Jamie Gennis9fea3422012-08-07 18:03:04 -0700279 return NO_ERROR;
280}
281
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700282status_t GLConsumer::releaseBufferLocked(int buf,
283 sp<GraphicBuffer> graphicBuffer,
284 EGLDisplay display, EGLSyncKHR eglFence) {
285 // release the buffer if it hasn't already been discarded by the
286 // BufferQueue. This can happen, for example, when the producer of this
287 // buffer has reallocated the original buffer slot after this buffer
288 // was acquired.
289 status_t err = ConsumerBase::releaseBufferLocked(
290 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700291 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700292 return err;
293}
294
Mathias Agopianad678e12013-07-23 17:28:53 -0700295status_t GLConsumer::updateAndReleaseLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800296{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700297 status_t err = NO_ERROR;
298
Jamie Gennis74bed552012-03-28 19:05:54 -0700299 if (!mAttached) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700300 ST_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700301 "ES context");
302 return INVALID_OPERATION;
303 }
304
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800305 // Confirm state.
306 err = checkAndUpdateEglStateLocked();
307 if (err != NO_ERROR) {
308 return err;
309 }
310
311 int buf = item.mBuf;
312
313 // If the mEglSlot entry is empty, create an EGLImage for the gralloc
314 // buffer currently in the slot in ConsumerBase.
315 //
316 // We may have to do this even when item.mGraphicBuffer == NULL (which
317 // means the buffer was previously acquired), if we destroyed the
318 // EGLImage when detaching from a context but the buffer has not been
319 // re-allocated.
320 if (mEglSlots[buf].mEglImage == EGL_NO_IMAGE_KHR) {
321 EGLImageKHR image = createImage(mEglDisplay, mSlots[buf].mGraphicBuffer);
322 if (image == EGL_NO_IMAGE_KHR) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700323 ST_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800324 mEglDisplay, buf);
325 return UNKNOWN_ERROR;
326 }
327 mEglSlots[buf].mEglImage = image;
328 }
329
330 // Do whatever sync ops we need to do before releasing the old slot.
331 err = syncForReleaseLocked(mEglDisplay);
332 if (err != NO_ERROR) {
333 // Release the buffer we just acquired. It's not safe to
334 // release the old buffer, so instead we just drop the new frame.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700335 // As we are still under lock since acquireBuffer, it is safe to
336 // release by slot.
337 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
338 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800339 return err;
340 }
341
Mathias Agopianad678e12013-07-23 17:28:53 -0700342 ST_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800343 mCurrentTexture,
344 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
345 buf, mSlots[buf].mGraphicBuffer->handle);
346
347 // release old buffer
348 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700349 status_t status = releaseBufferLocked(
350 mCurrentTexture, mCurrentTextureBuf, mEglDisplay,
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800351 mEglSlots[mCurrentTexture].mEglFence);
Mathias Agopianad678e12013-07-23 17:28:53 -0700352 if (status < NO_ERROR) {
353 ST_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800354 strerror(-status), status);
355 err = status;
356 // keep going, with error raised [?]
357 }
358 }
359
Andy McFadden2adaf042012-12-18 09:49:45 -0800360 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800361 mCurrentTexture = buf;
362 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
363 mCurrentCrop = item.mCrop;
364 mCurrentTransform = item.mTransform;
365 mCurrentScalingMode = item.mScalingMode;
366 mCurrentTimestamp = item.mTimestamp;
367 mCurrentFence = item.mFence;
368
369 computeCurrentTransformMatrixLocked();
370
371 return err;
372}
373
Andy McFadden2adaf042012-12-18 09:49:45 -0800374status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800375 if (mEglDisplay == EGL_NO_DISPLAY) {
376 ALOGE("bindTextureImage: invalid display");
377 return INVALID_OPERATION;
378 }
379
380 GLint error;
381 while ((error = glGetError()) != GL_NO_ERROR) {
382 ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
383 }
384
385 glBindTexture(mTexTarget, mTexName);
386 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
387 if (mCurrentTextureBuf == NULL) {
388 ST_LOGE("bindTextureImage: no currently-bound texture");
389 return NO_INIT;
390 }
Andy McFadden97eba892012-12-11 15:21:45 -0800391 status_t err = bindUnslottedBufferLocked(mEglDisplay);
392 if (err != NO_ERROR) {
393 return err;
394 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800395 } else {
396 EGLImageKHR image = mEglSlots[mCurrentTexture].mEglImage;
397
398 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
399
400 while ((error = glGetError()) != GL_NO_ERROR) {
401 ST_LOGE("bindTextureImage: error binding external texture image %p"
402 ": %#04x", image, error);
403 return UNKNOWN_ERROR;
404 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800405 }
Andy McFadden97eba892012-12-11 15:21:45 -0800406
407 // Wait for the new buffer to be ready.
408 return doGLFenceWaitLocked();
409
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800410}
411
Andy McFadden2adaf042012-12-18 09:49:45 -0800412status_t GLConsumer::checkAndUpdateEglStateLocked() {
Jamie Gennisce561372012-03-19 18:33:05 -0700413 EGLDisplay dpy = eglGetCurrentDisplay();
414 EGLContext ctx = eglGetCurrentContext();
415
Jamie Gennis74bed552012-03-28 19:05:54 -0700416 if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) ||
417 dpy == EGL_NO_DISPLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800418 ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700419 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700420 }
421
Jamie Gennis74bed552012-03-28 19:05:54 -0700422 if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) ||
423 ctx == EGL_NO_CONTEXT) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800424 ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700425 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700426 }
427
428 mEglDisplay = dpy;
429 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800430 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800431}
432
Jesse Hall13f01cb2013-03-20 11:37:21 -0700433void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
434 if (fence->isValid() &&
435 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700436 status_t err = addReleaseFence(mCurrentTexture,
437 mCurrentTextureBuf, fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700438 if (err != OK) {
439 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
440 strerror(-err), err);
441 }
Jesse Hallef194142012-06-14 14:45:17 -0700442 }
443}
444
Andy McFadden2adaf042012-12-18 09:49:45 -0800445status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700446 ATRACE_CALL();
447 ST_LOGV("detachFromContext");
448 Mutex::Autolock lock(mMutex);
449
450 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800451 ST_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700452 return NO_INIT;
453 }
454
455 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800456 ST_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700457 "context");
458 return INVALID_OPERATION;
459 }
460
461 EGLDisplay dpy = eglGetCurrentDisplay();
462 EGLContext ctx = eglGetCurrentContext();
463
464 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
465 ST_LOGE("detachFromContext: invalid current EGLDisplay");
466 return INVALID_OPERATION;
467 }
468
469 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
470 ST_LOGE("detachFromContext: invalid current EGLContext");
471 return INVALID_OPERATION;
472 }
473
474 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
475 status_t err = syncForReleaseLocked(dpy);
476 if (err != OK) {
477 return err;
478 }
479
480 glDeleteTextures(1, &mTexName);
481 }
482
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700483 // Because we're giving up the EGLDisplay we need to free all the EGLImages
484 // that are associated with it. They'll be recreated when the
Andy McFadden2adaf042012-12-18 09:49:45 -0800485 // GLConsumer gets attached to a new OpenGL ES context (and thus gets a
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700486 // new EGLDisplay).
487 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700488 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700489 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700490 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700491 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700492 }
493 }
494
Jamie Gennis74bed552012-03-28 19:05:54 -0700495 mEglDisplay = EGL_NO_DISPLAY;
496 mEglContext = EGL_NO_CONTEXT;
497 mAttached = false;
498
499 return OK;
500}
501
Andy McFadden2adaf042012-12-18 09:49:45 -0800502status_t GLConsumer::attachToContext(GLuint tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700503 ATRACE_CALL();
504 ST_LOGV("attachToContext");
505 Mutex::Autolock lock(mMutex);
506
507 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800508 ST_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700509 return NO_INIT;
510 }
511
512 if (mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800513 ST_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700514 "context");
515 return INVALID_OPERATION;
516 }
517
518 EGLDisplay dpy = eglGetCurrentDisplay();
519 EGLContext ctx = eglGetCurrentContext();
520
521 if (dpy == EGL_NO_DISPLAY) {
522 ST_LOGE("attachToContext: invalid current EGLDisplay");
523 return INVALID_OPERATION;
524 }
525
526 if (ctx == EGL_NO_CONTEXT) {
527 ST_LOGE("attachToContext: invalid current EGLContext");
528 return INVALID_OPERATION;
529 }
530
531 // We need to bind the texture regardless of whether there's a current
532 // buffer.
533 glBindTexture(mTexTarget, tex);
534
535 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700536 // The EGLImageKHR that was associated with the slot was destroyed when
Andy McFadden2adaf042012-12-18 09:49:45 -0800537 // the GLConsumer was detached from the old context, so we need to
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700538 // recreate it here.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800539 status_t err = bindUnslottedBufferLocked(dpy);
540 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700541 return err;
542 }
543 }
544
545 mEglDisplay = dpy;
546 mEglContext = ctx;
547 mTexName = tex;
548 mAttached = true;
549
550 return OK;
551}
552
Andy McFadden2adaf042012-12-18 09:49:45 -0800553status_t GLConsumer::bindUnslottedBufferLocked(EGLDisplay dpy) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800554 ST_LOGV("bindUnslottedBuffer ct=%d ctb=%p",
555 mCurrentTexture, mCurrentTextureBuf.get());
556
557 // Create a temporary EGLImageKHR.
558 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
559 if (image == EGL_NO_IMAGE_KHR) {
560 return UNKNOWN_ERROR;
561 }
562
563 // Attach the current buffer to the GL texture.
564 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
565
566 GLint error;
567 status_t err = OK;
568 while ((error = glGetError()) != GL_NO_ERROR) {
569 ST_LOGE("bindUnslottedBuffer: error binding external texture image %p "
570 "(slot %d): %#04x", image, mCurrentTexture, error);
571 err = UNKNOWN_ERROR;
572 }
573
574 // We destroy the EGLImageKHR here because the current buffer may no
575 // longer be associated with one of the buffer slots, so we have
576 // nowhere to to store it. If the buffer is still associated with a
577 // slot then another EGLImageKHR will be created next time that buffer
578 // gets acquired in updateTexImage.
579 eglDestroyImageKHR(dpy, image);
580
581 return err;
582}
583
584
Andy McFadden2adaf042012-12-18 09:49:45 -0800585status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700586 ST_LOGV("syncForReleaseLocked");
587
Jamie Gennis01dbf552012-09-06 14:54:19 -0700588 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700589 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700590 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
591 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
592 if (sync == EGL_NO_SYNC_KHR) {
593 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
594 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700595 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700596 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700597 glFlush();
598 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700599 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700600 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
601 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
602 "fd: %#x", eglGetError());
603 return UNKNOWN_ERROR;
604 }
605 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700606 status_t err = addReleaseFenceLocked(mCurrentTexture,
607 mCurrentTextureBuf, fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700608 if (err != OK) {
609 ST_LOGE("syncForReleaseLocked: error adding release fence: "
610 "%s (%d)", strerror(-err), err);
611 return err;
612 }
Mathias Agopianca088332013-03-28 17:44:13 -0700613 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700614 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
615 if (fence != EGL_NO_SYNC_KHR) {
616 // There is already a fence for the current slot. We need to
617 // wait on that before replacing it with another fence to
618 // ensure that all outstanding buffer accesses have completed
619 // before the producer accesses it.
620 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
621 if (result == EGL_FALSE) {
622 ST_LOGE("syncForReleaseLocked: error waiting for previous "
623 "fence: %#x", eglGetError());
624 return UNKNOWN_ERROR;
625 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
626 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
627 "fence");
628 return TIMED_OUT;
629 }
630 eglDestroySyncKHR(dpy, fence);
631 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700632
Jamie Gennis01dbf552012-09-06 14:54:19 -0700633 // Create a fence for the outstanding accesses in the current
634 // OpenGL ES context.
635 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
636 if (fence == EGL_NO_SYNC_KHR) {
637 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
638 eglGetError());
639 return UNKNOWN_ERROR;
640 }
641 glFlush();
642 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700643 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700644 }
645
646 return OK;
647}
648
Andy McFadden2adaf042012-12-18 09:49:45 -0800649bool GLConsumer::isExternalFormat(uint32_t format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700650{
651 switch (format) {
652 // supported YUV formats
653 case HAL_PIXEL_FORMAT_YV12:
654 // Legacy/deprecated YUV formats
655 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
656 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
657 case HAL_PIXEL_FORMAT_YCbCr_422_I:
658 return true;
659 }
660
661 // Any OEM format needs to be considered
662 if (format>=0x100 && format<=0x1FF)
663 return true;
664
665 return false;
666}
667
Andy McFadden2adaf042012-12-18 09:49:45 -0800668GLenum GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700669 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700670}
671
Andy McFadden2adaf042012-12-18 09:49:45 -0800672void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800673 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700674 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
675}
676
Andy McFadden2adaf042012-12-18 09:49:45 -0800677void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700678 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700679 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800680 ST_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700681 return;
682 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700683 bool needsRecompute = mFilteringEnabled != enabled;
684 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700685
686 if (needsRecompute && mCurrentTextureBuf==NULL) {
687 ST_LOGD("setFilteringEnabled called with mCurrentTextureBuf == NULL");
688 }
689
690 if (needsRecompute && mCurrentTextureBuf != NULL) {
691 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700692 }
693}
694
Andy McFadden2adaf042012-12-18 09:49:45 -0800695void GLConsumer::computeCurrentTransformMatrixLocked() {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700696 ST_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800697
Jamie Gennisa214c642011-01-14 13:53:31 -0800698 float xform[16];
699 for (int i = 0; i < 16; i++) {
700 xform[i] = mtxIdentity[i];
701 }
702 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
703 float result[16];
704 mtxMul(result, xform, mtxFlipH);
705 for (int i = 0; i < 16; i++) {
706 xform[i] = result[i];
707 }
708 }
709 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
710 float result[16];
711 mtxMul(result, xform, mtxFlipV);
712 for (int i = 0; i < 16; i++) {
713 xform[i] = result[i];
714 }
715 }
716 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
717 float result[16];
718 mtxMul(result, xform, mtxRot90);
719 for (int i = 0; i < 16; i++) {
720 xform[i] = result[i];
721 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800722 }
723
Daniel Lameae59d22012-01-22 15:26:27 -0800724 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700725
726 if (buf == NULL) {
727 ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureBuf is NULL");
728 }
729
Jamie Gennisd72f2332012-05-07 13:50:11 -0700730 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700731 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
732 float bufferWidth = buf->getWidth();
733 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700734 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700735 float shrinkAmount = 0.0f;
736 if (mFilteringEnabled) {
737 // In order to prevent bilinear sampling beyond the edge of the
738 // crop rectangle we may need to shrink it by 2 texels in each
739 // dimension. Normally this would just need to take 1/2 a texel
740 // off each end, but because the chroma channels of YUV420 images
741 // are subsampled we may need to shrink the crop region by a whole
742 // texel on each side.
743 switch (buf->getPixelFormat()) {
744 case PIXEL_FORMAT_RGBA_8888:
745 case PIXEL_FORMAT_RGBX_8888:
746 case PIXEL_FORMAT_RGB_888:
747 case PIXEL_FORMAT_RGB_565:
748 case PIXEL_FORMAT_BGRA_8888:
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700749 // We know there's no subsampling of any channels, so we
750 // only need to shrink by a half a pixel.
751 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700752 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700753
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700754 default:
755 // If we don't recognize the format, we must assume the
756 // worst case (that we care about), which is YUV420.
757 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700758 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700759 }
760 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700761
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700762 // Only shrink the dimensions that are not the size of the buffer.
763 if (cropRect.width() < bufferWidth) {
764 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
765 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
766 bufferWidth;
767 }
768 if (cropRect.height() < bufferHeight) {
769 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
770 bufferHeight;
771 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
772 bufferHeight;
773 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800774 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800775 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800776 sx, 0, 0, 0,
777 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800778 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800779 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800780 };
781
Jamie Gennisa214c642011-01-14 13:53:31 -0800782 float mtxBeforeFlipV[16];
783 mtxMul(mtxBeforeFlipV, crop, xform);
784
785 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800786 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800787 // want to expose this to applications, however, so we must add an
788 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700789 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800790}
791
Andy McFadden2adaf042012-12-18 09:49:45 -0800792nsecs_t GLConsumer::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700793 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800794 Mutex::Autolock lock(mMutex);
795 return mCurrentTimestamp;
796}
797
Andy McFadden2adaf042012-12-18 09:49:45 -0800798EGLImageKHR GLConsumer::createImage(EGLDisplay dpy,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800799 const sp<GraphicBuffer>& graphicBuffer) {
800 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
801 EGLint attrs[] = {
802 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
803 EGL_NONE,
804 };
805 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
806 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700807 if (image == EGL_NO_IMAGE_KHR) {
808 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700809 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800810 }
811 return image;
812}
813
Andy McFadden2adaf042012-12-18 09:49:45 -0800814sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700815 Mutex::Autolock lock(mMutex);
816 return mCurrentTextureBuf;
817}
818
Andy McFadden2adaf042012-12-18 09:49:45 -0800819Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700820 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700821
822 Rect outCrop = mCurrentCrop;
823 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
824 int32_t newWidth = mCurrentCrop.width();
825 int32_t newHeight = mCurrentCrop.height();
826
827 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
828 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
829 ST_LOGV("too wide: newWidth = %d", newWidth);
830 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
831 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
832 ST_LOGV("too tall: newHeight = %d", newHeight);
833 }
834
835 // The crop is too wide
836 if (newWidth < mCurrentCrop.width()) {
837 int32_t dw = (newWidth - mCurrentCrop.width())/2;
838 outCrop.left -=dw;
839 outCrop.right += dw;
840 // The crop is too tall
841 } else if (newHeight < mCurrentCrop.height()) {
842 int32_t dh = (newHeight - mCurrentCrop.height())/2;
843 outCrop.top -= dh;
844 outCrop.bottom += dh;
845 }
846
847 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
848 outCrop.left, outCrop.top,
849 outCrop.right,outCrop.bottom);
850 }
851
Daniel Lam016c8cb2012-04-03 15:54:58 -0700852 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700853}
854
Andy McFadden2adaf042012-12-18 09:49:45 -0800855uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700856 Mutex::Autolock lock(mMutex);
857 return mCurrentTransform;
858}
859
Andy McFadden2adaf042012-12-18 09:49:45 -0800860uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700861 Mutex::Autolock lock(mMutex);
862 return mCurrentScalingMode;
863}
864
Andy McFadden2adaf042012-12-18 09:49:45 -0800865sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700866 Mutex::Autolock lock(mMutex);
867 return mCurrentFence;
868}
869
Andy McFadden2adaf042012-12-18 09:49:45 -0800870status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700871 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700872 return doGLFenceWaitLocked();
873}
874
Andy McFadden2adaf042012-12-18 09:49:45 -0800875status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700876
877 EGLDisplay dpy = eglGetCurrentDisplay();
878 EGLContext ctx = eglGetCurrentContext();
879
880 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
881 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
882 return INVALID_OPERATION;
883 }
884
885 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
886 ST_LOGE("doGLFenceWait: invalid current EGLContext");
887 return INVALID_OPERATION;
888 }
889
Jamie Gennis1df8c342012-12-20 14:05:45 -0800890 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700891 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700892 // Create an EGLSyncKHR from the current fence.
893 int fenceFd = mCurrentFence->dup();
894 if (fenceFd == -1) {
895 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
896 return -errno;
897 }
898 EGLint attribs[] = {
899 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
900 EGL_NONE
901 };
902 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
903 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
904 if (sync == EGL_NO_SYNC_KHR) {
905 close(fenceFd);
906 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
907 eglGetError());
908 return UNKNOWN_ERROR;
909 }
910
911 // XXX: The spec draft is inconsistent as to whether this should
912 // return an EGLint or void. Ignore the return value for now, as
913 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700914 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700915 EGLint eglErr = eglGetError();
916 eglDestroySyncKHR(dpy, sync);
917 if (eglErr != EGL_SUCCESS) {
918 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
919 eglErr);
920 return UNKNOWN_ERROR;
921 }
922 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700923 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800924 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700925 if (err != NO_ERROR) {
926 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
927 return err;
928 }
929 }
930 }
931
932 return NO_ERROR;
933}
934
Andy McFadden2adaf042012-12-18 09:49:45 -0800935void GLConsumer::freeBufferLocked(int slotIndex) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700936 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700937 if (slotIndex == mCurrentTexture) {
938 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
939 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700940 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700941 if (img != EGL_NO_IMAGE_KHR) {
942 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
943 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800944 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700945 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
946 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700947}
Daniel Lameae59d22012-01-22 15:26:27 -0800948
Andy McFadden2adaf042012-12-18 09:49:45 -0800949void GLConsumer::abandonLocked() {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700950 ST_LOGV("abandonLocked");
951 mCurrentTextureBuf.clear();
952 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700953}
954
Andy McFadden2adaf042012-12-18 09:49:45 -0800955void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800956 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700957 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700958 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -0800959}
960
Andy McFadden2adaf042012-12-18 09:49:45 -0800961status_t GLConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -0800962 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700963 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -0800964}
965
Andy McFadden2adaf042012-12-18 09:49:45 -0800966status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -0800967 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700968 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700969 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -0800970}
971
Andy McFadden2adaf042012-12-18 09:49:45 -0800972status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -0800973 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700974 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -0800975}
976
Mathias Agopian74d211a2013-04-22 16:55:35 +0200977void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700978{
Mathias Agopian74d211a2013-04-22 16:55:35 +0200979 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -0700980 "%smTexName=%d mCurrentTexture=%d\n"
981 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
982 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
983 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
984 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700985
Mathias Agopian74d211a2013-04-22 16:55:35 +0200986 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -0700987}
988
Jamie Gennisf238e282011-01-09 16:33:17 -0800989static void mtxMul(float out[16], const float a[16], const float b[16]) {
990 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
991 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
992 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
993 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
994
995 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
996 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
997 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
998 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
999
1000 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1001 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1002 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1003 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1004
1005 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1006 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1007 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1008 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1009}
1010
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001011}; // namespace android