blob: 482317f2d0be09bd08e6ffe326937f5b6c7161b9 [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.
Mathias Agopian45155962013-08-08 18:16:21 -0700191 status_t err = NO_ERROR;
192
193 if (mAttached) {
194 err = checkAndUpdateEglStateLocked(true);
195 if (err != NO_ERROR) {
196 return err;
197 }
198 } else {
199 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700200 }
201
202 // Update the GLConsumer state.
203 int buf = mCurrentTexture;
204 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
205
Mathias Agopian45155962013-08-08 18:16:21 -0700206 ST_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700207
Mathias Agopian45155962013-08-08 18:16:21 -0700208 if (mAttached) {
209 // Do whatever sync ops we need to do before releasing the slot.
210 err = syncForReleaseLocked(mEglDisplay);
211 if (err != NO_ERROR) {
212 ST_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
213 return err;
214 }
215 } else {
216 // if we're detached, we just use the fence that was created in detachFromContext()
217 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700218 }
219
Mathias Agopian45155962013-08-08 18:16:21 -0700220 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700221 if (err < NO_ERROR) {
222 ST_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
223 strerror(-err), err);
224 return err;
225 }
226
Mathias Agopianad678e12013-07-23 17:28:53 -0700227 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700228 mCurrentTextureBuf = getDebugTexImageBuffer();
Mathias Agopianad678e12013-07-23 17:28:53 -0700229 mCurrentCrop.makeInvalid();
230 mCurrentTransform = 0;
231 mCurrentScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
232 mCurrentTimestamp = 0;
233 mCurrentFence = Fence::NO_FENCE;
234
Mathias Agopian45155962013-08-08 18:16:21 -0700235 if (mAttached) {
236 // bind a dummy texture
237 glBindTexture(mTexTarget, mTexName);
238 bindUnslottedBufferLocked(mEglDisplay);
239 } else {
240 // detached, don't touch the texture (and we may not even have an
241 // EGLDisplay here.
242 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700243 }
244
245 return NO_ERROR;
246}
247
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700248sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
249 Mutex::Autolock _l(sStaticInitLock);
250 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
251 // The first time, create the debug texture in case the application
252 // continues to use it.
253 sp<GraphicBuffer> buffer = new GraphicBuffer(
254 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
255 GraphicBuffer::USAGE_SW_WRITE_RARELY);
256 uint32_t* bits;
257 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
258 size_t w = buffer->getStride();
259 size_t h = buffer->getHeight();
260 memset(bits, 0, w*h*4);
261 for (size_t y=0 ; y<kDebugData.height ; y++) {
262 for (size_t x=0 ; x<kDebugData.width ; x++) {
263 bits[x] = (kDebugData.bits[y*kDebugData.width+x] == 'X') ? 0xFF000000 : 0xFFFFFFFF;
264 }
265 bits += w;
266 }
267 buffer->unlock();
268 sReleasedTexImageBuffer = buffer;
269 }
270 return sReleasedTexImageBuffer;
271}
272
Andy McFadden1585c4d2013-06-28 13:52:40 -0700273status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item,
274 nsecs_t presentWhen) {
275 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700276 if (err != NO_ERROR) {
277 return err;
278 }
279
280 int slot = item->mBuf;
281 if (item->mGraphicBuffer != NULL) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800282 // This buffer has not been acquired before, so we must assume
283 // that any EGLImage in mEglSlots is stale.
Jamie Gennis9fea3422012-08-07 18:03:04 -0700284 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800285 if (!eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage)) {
286 ST_LOGW("acquireBufferLocked: eglDestroyImageKHR failed for slot=%d",
287 slot);
288 // keep going
289 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700290 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
291 }
292 }
293
Jamie Gennis9fea3422012-08-07 18:03:04 -0700294 return NO_ERROR;
295}
296
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700297status_t GLConsumer::releaseBufferLocked(int buf,
298 sp<GraphicBuffer> graphicBuffer,
299 EGLDisplay display, EGLSyncKHR eglFence) {
300 // release the buffer if it hasn't already been discarded by the
301 // BufferQueue. This can happen, for example, when the producer of this
302 // buffer has reallocated the original buffer slot after this buffer
303 // was acquired.
304 status_t err = ConsumerBase::releaseBufferLocked(
305 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700306 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700307 return err;
308}
309
Mathias Agopianad678e12013-07-23 17:28:53 -0700310status_t GLConsumer::updateAndReleaseLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800311{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700312 status_t err = NO_ERROR;
313
Jamie Gennis74bed552012-03-28 19:05:54 -0700314 if (!mAttached) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700315 ST_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700316 "ES context");
317 return INVALID_OPERATION;
318 }
319
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800320 // Confirm state.
321 err = checkAndUpdateEglStateLocked();
322 if (err != NO_ERROR) {
323 return err;
324 }
325
326 int buf = item.mBuf;
327
328 // If the mEglSlot entry is empty, create an EGLImage for the gralloc
329 // buffer currently in the slot in ConsumerBase.
330 //
331 // We may have to do this even when item.mGraphicBuffer == NULL (which
332 // means the buffer was previously acquired), if we destroyed the
333 // EGLImage when detaching from a context but the buffer has not been
334 // re-allocated.
335 if (mEglSlots[buf].mEglImage == EGL_NO_IMAGE_KHR) {
336 EGLImageKHR image = createImage(mEglDisplay, mSlots[buf].mGraphicBuffer);
337 if (image == EGL_NO_IMAGE_KHR) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700338 ST_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800339 mEglDisplay, buf);
340 return UNKNOWN_ERROR;
341 }
342 mEglSlots[buf].mEglImage = image;
343 }
344
345 // Do whatever sync ops we need to do before releasing the old slot.
346 err = syncForReleaseLocked(mEglDisplay);
347 if (err != NO_ERROR) {
348 // Release the buffer we just acquired. It's not safe to
349 // release the old buffer, so instead we just drop the new frame.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700350 // As we are still under lock since acquireBuffer, it is safe to
351 // release by slot.
352 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
353 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800354 return err;
355 }
356
Mathias Agopianad678e12013-07-23 17:28:53 -0700357 ST_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800358 mCurrentTexture,
359 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
360 buf, mSlots[buf].mGraphicBuffer->handle);
361
362 // release old buffer
363 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700364 status_t status = releaseBufferLocked(
365 mCurrentTexture, mCurrentTextureBuf, mEglDisplay,
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800366 mEglSlots[mCurrentTexture].mEglFence);
Mathias Agopianad678e12013-07-23 17:28:53 -0700367 if (status < NO_ERROR) {
368 ST_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800369 strerror(-status), status);
370 err = status;
371 // keep going, with error raised [?]
372 }
373 }
374
Andy McFadden2adaf042012-12-18 09:49:45 -0800375 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800376 mCurrentTexture = buf;
377 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
378 mCurrentCrop = item.mCrop;
379 mCurrentTransform = item.mTransform;
380 mCurrentScalingMode = item.mScalingMode;
381 mCurrentTimestamp = item.mTimestamp;
382 mCurrentFence = item.mFence;
383
384 computeCurrentTransformMatrixLocked();
385
386 return err;
387}
388
Andy McFadden2adaf042012-12-18 09:49:45 -0800389status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800390 if (mEglDisplay == EGL_NO_DISPLAY) {
391 ALOGE("bindTextureImage: invalid display");
392 return INVALID_OPERATION;
393 }
394
395 GLint error;
396 while ((error = glGetError()) != GL_NO_ERROR) {
397 ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
398 }
399
400 glBindTexture(mTexTarget, mTexName);
401 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
402 if (mCurrentTextureBuf == NULL) {
403 ST_LOGE("bindTextureImage: no currently-bound texture");
404 return NO_INIT;
405 }
Andy McFadden97eba892012-12-11 15:21:45 -0800406 status_t err = bindUnslottedBufferLocked(mEglDisplay);
407 if (err != NO_ERROR) {
408 return err;
409 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800410 } else {
411 EGLImageKHR image = mEglSlots[mCurrentTexture].mEglImage;
412
413 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
414
415 while ((error = glGetError()) != GL_NO_ERROR) {
416 ST_LOGE("bindTextureImage: error binding external texture image %p"
417 ": %#04x", image, error);
418 return UNKNOWN_ERROR;
419 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800420 }
Andy McFadden97eba892012-12-11 15:21:45 -0800421
422 // Wait for the new buffer to be ready.
423 return doGLFenceWaitLocked();
424
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800425}
426
Mathias Agopian45155962013-08-08 18:16:21 -0700427status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700428 EGLDisplay dpy = eglGetCurrentDisplay();
429 EGLContext ctx = eglGetCurrentContext();
430
Mathias Agopian45155962013-08-08 18:16:21 -0700431 if (!contextCheck) {
432 // if this is the first time we're called, mEglDisplay/mEglContext have
433 // never been set, so don't error out (below).
434 if (mEglDisplay == EGL_NO_DISPLAY) {
435 mEglDisplay = dpy;
436 }
437 if (mEglContext == EGL_NO_DISPLAY) {
438 mEglContext = ctx;
439 }
440 }
441
442 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800443 ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700444 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700445 }
446
Mathias Agopian45155962013-08-08 18:16:21 -0700447 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800448 ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700449 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700450 }
451
452 mEglDisplay = dpy;
453 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800454 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800455}
456
Jesse Hall13f01cb2013-03-20 11:37:21 -0700457void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
458 if (fence->isValid() &&
459 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700460 status_t err = addReleaseFence(mCurrentTexture,
461 mCurrentTextureBuf, fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700462 if (err != OK) {
463 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
464 strerror(-err), err);
465 }
Jesse Hallef194142012-06-14 14:45:17 -0700466 }
467}
468
Andy McFadden2adaf042012-12-18 09:49:45 -0800469status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700470 ATRACE_CALL();
471 ST_LOGV("detachFromContext");
472 Mutex::Autolock lock(mMutex);
473
474 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800475 ST_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700476 return NO_INIT;
477 }
478
479 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800480 ST_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700481 "context");
482 return INVALID_OPERATION;
483 }
484
485 EGLDisplay dpy = eglGetCurrentDisplay();
486 EGLContext ctx = eglGetCurrentContext();
487
488 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
489 ST_LOGE("detachFromContext: invalid current EGLDisplay");
490 return INVALID_OPERATION;
491 }
492
493 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
494 ST_LOGE("detachFromContext: invalid current EGLContext");
495 return INVALID_OPERATION;
496 }
497
498 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
499 status_t err = syncForReleaseLocked(dpy);
500 if (err != OK) {
501 return err;
502 }
503
504 glDeleteTextures(1, &mTexName);
505 }
506
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700507 // Because we're giving up the EGLDisplay we need to free all the EGLImages
508 // that are associated with it. They'll be recreated when the
Andy McFadden2adaf042012-12-18 09:49:45 -0800509 // GLConsumer gets attached to a new OpenGL ES context (and thus gets a
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700510 // new EGLDisplay).
511 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700512 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700513 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700514 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700515 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700516 }
517 }
518
Jamie Gennis74bed552012-03-28 19:05:54 -0700519 mEglDisplay = EGL_NO_DISPLAY;
520 mEglContext = EGL_NO_CONTEXT;
521 mAttached = false;
522
523 return OK;
524}
525
Andy McFadden2adaf042012-12-18 09:49:45 -0800526status_t GLConsumer::attachToContext(GLuint tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700527 ATRACE_CALL();
528 ST_LOGV("attachToContext");
529 Mutex::Autolock lock(mMutex);
530
531 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800532 ST_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700533 return NO_INIT;
534 }
535
536 if (mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800537 ST_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700538 "context");
539 return INVALID_OPERATION;
540 }
541
542 EGLDisplay dpy = eglGetCurrentDisplay();
543 EGLContext ctx = eglGetCurrentContext();
544
545 if (dpy == EGL_NO_DISPLAY) {
546 ST_LOGE("attachToContext: invalid current EGLDisplay");
547 return INVALID_OPERATION;
548 }
549
550 if (ctx == EGL_NO_CONTEXT) {
551 ST_LOGE("attachToContext: invalid current EGLContext");
552 return INVALID_OPERATION;
553 }
554
555 // We need to bind the texture regardless of whether there's a current
556 // buffer.
557 glBindTexture(mTexTarget, tex);
558
559 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700560 // The EGLImageKHR that was associated with the slot was destroyed when
Andy McFadden2adaf042012-12-18 09:49:45 -0800561 // the GLConsumer was detached from the old context, so we need to
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700562 // recreate it here.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800563 status_t err = bindUnslottedBufferLocked(dpy);
564 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700565 return err;
566 }
567 }
568
569 mEglDisplay = dpy;
570 mEglContext = ctx;
571 mTexName = tex;
572 mAttached = true;
573
574 return OK;
575}
576
Andy McFadden2adaf042012-12-18 09:49:45 -0800577status_t GLConsumer::bindUnslottedBufferLocked(EGLDisplay dpy) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800578 ST_LOGV("bindUnslottedBuffer ct=%d ctb=%p",
579 mCurrentTexture, mCurrentTextureBuf.get());
580
581 // Create a temporary EGLImageKHR.
582 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf);
583 if (image == EGL_NO_IMAGE_KHR) {
584 return UNKNOWN_ERROR;
585 }
586
587 // Attach the current buffer to the GL texture.
588 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
589
590 GLint error;
591 status_t err = OK;
592 while ((error = glGetError()) != GL_NO_ERROR) {
593 ST_LOGE("bindUnslottedBuffer: error binding external texture image %p "
594 "(slot %d): %#04x", image, mCurrentTexture, error);
595 err = UNKNOWN_ERROR;
596 }
597
598 // We destroy the EGLImageKHR here because the current buffer may no
599 // longer be associated with one of the buffer slots, so we have
600 // nowhere to to store it. If the buffer is still associated with a
601 // slot then another EGLImageKHR will be created next time that buffer
602 // gets acquired in updateTexImage.
603 eglDestroyImageKHR(dpy, image);
604
605 return err;
606}
607
608
Andy McFadden2adaf042012-12-18 09:49:45 -0800609status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700610 ST_LOGV("syncForReleaseLocked");
611
Jamie Gennis01dbf552012-09-06 14:54:19 -0700612 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700613 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700614 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
615 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
616 if (sync == EGL_NO_SYNC_KHR) {
617 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
618 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700619 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700620 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700621 glFlush();
622 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700623 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700624 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
625 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
626 "fd: %#x", eglGetError());
627 return UNKNOWN_ERROR;
628 }
629 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700630 status_t err = addReleaseFenceLocked(mCurrentTexture,
631 mCurrentTextureBuf, fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700632 if (err != OK) {
633 ST_LOGE("syncForReleaseLocked: error adding release fence: "
634 "%s (%d)", strerror(-err), err);
635 return err;
636 }
Mathias Agopianca088332013-03-28 17:44:13 -0700637 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700638 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
639 if (fence != EGL_NO_SYNC_KHR) {
640 // There is already a fence for the current slot. We need to
641 // wait on that before replacing it with another fence to
642 // ensure that all outstanding buffer accesses have completed
643 // before the producer accesses it.
644 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
645 if (result == EGL_FALSE) {
646 ST_LOGE("syncForReleaseLocked: error waiting for previous "
647 "fence: %#x", eglGetError());
648 return UNKNOWN_ERROR;
649 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
650 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
651 "fence");
652 return TIMED_OUT;
653 }
654 eglDestroySyncKHR(dpy, fence);
655 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700656
Jamie Gennis01dbf552012-09-06 14:54:19 -0700657 // Create a fence for the outstanding accesses in the current
658 // OpenGL ES context.
659 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
660 if (fence == EGL_NO_SYNC_KHR) {
661 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
662 eglGetError());
663 return UNKNOWN_ERROR;
664 }
665 glFlush();
666 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700667 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700668 }
669
670 return OK;
671}
672
Andy McFadden2adaf042012-12-18 09:49:45 -0800673bool GLConsumer::isExternalFormat(uint32_t format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700674{
675 switch (format) {
676 // supported YUV formats
677 case HAL_PIXEL_FORMAT_YV12:
678 // Legacy/deprecated YUV formats
679 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
680 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
681 case HAL_PIXEL_FORMAT_YCbCr_422_I:
682 return true;
683 }
684
685 // Any OEM format needs to be considered
686 if (format>=0x100 && format<=0x1FF)
687 return true;
688
689 return false;
690}
691
Andy McFadden2adaf042012-12-18 09:49:45 -0800692GLenum GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700693 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700694}
695
Andy McFadden2adaf042012-12-18 09:49:45 -0800696void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800697 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700698 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
699}
700
Andy McFadden2adaf042012-12-18 09:49:45 -0800701void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700702 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700703 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800704 ST_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700705 return;
706 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700707 bool needsRecompute = mFilteringEnabled != enabled;
708 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700709
710 if (needsRecompute && mCurrentTextureBuf==NULL) {
711 ST_LOGD("setFilteringEnabled called with mCurrentTextureBuf == NULL");
712 }
713
714 if (needsRecompute && mCurrentTextureBuf != NULL) {
715 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700716 }
717}
718
Andy McFadden2adaf042012-12-18 09:49:45 -0800719void GLConsumer::computeCurrentTransformMatrixLocked() {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700720 ST_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800721
Jamie Gennisa214c642011-01-14 13:53:31 -0800722 float xform[16];
723 for (int i = 0; i < 16; i++) {
724 xform[i] = mtxIdentity[i];
725 }
726 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
727 float result[16];
728 mtxMul(result, xform, mtxFlipH);
729 for (int i = 0; i < 16; i++) {
730 xform[i] = result[i];
731 }
732 }
733 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
734 float result[16];
735 mtxMul(result, xform, mtxFlipV);
736 for (int i = 0; i < 16; i++) {
737 xform[i] = result[i];
738 }
739 }
740 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
741 float result[16];
742 mtxMul(result, xform, mtxRot90);
743 for (int i = 0; i < 16; i++) {
744 xform[i] = result[i];
745 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800746 }
747
Daniel Lameae59d22012-01-22 15:26:27 -0800748 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700749
750 if (buf == NULL) {
751 ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureBuf is NULL");
752 }
753
Jamie Gennisd72f2332012-05-07 13:50:11 -0700754 Rect cropRect = mCurrentCrop;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700755 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
756 float bufferWidth = buf->getWidth();
757 float bufferHeight = buf->getHeight();
Jamie Gennisd72f2332012-05-07 13:50:11 -0700758 if (!cropRect.isEmpty()) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700759 float shrinkAmount = 0.0f;
760 if (mFilteringEnabled) {
761 // In order to prevent bilinear sampling beyond the edge of the
762 // crop rectangle we may need to shrink it by 2 texels in each
763 // dimension. Normally this would just need to take 1/2 a texel
764 // off each end, but because the chroma channels of YUV420 images
765 // are subsampled we may need to shrink the crop region by a whole
766 // texel on each side.
767 switch (buf->getPixelFormat()) {
768 case PIXEL_FORMAT_RGBA_8888:
769 case PIXEL_FORMAT_RGBX_8888:
770 case PIXEL_FORMAT_RGB_888:
771 case PIXEL_FORMAT_RGB_565:
772 case PIXEL_FORMAT_BGRA_8888:
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700773 // We know there's no subsampling of any channels, so we
774 // only need to shrink by a half a pixel.
775 shrinkAmount = 0.5;
Romain Guy4f9c2842012-08-01 19:16:59 -0700776 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700777
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700778 default:
779 // If we don't recognize the format, we must assume the
780 // worst case (that we care about), which is YUV420.
781 shrinkAmount = 1.0;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700782 break;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700783 }
784 }
Jamie Gennisd72f2332012-05-07 13:50:11 -0700785
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700786 // Only shrink the dimensions that are not the size of the buffer.
787 if (cropRect.width() < bufferWidth) {
788 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
789 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
790 bufferWidth;
791 }
792 if (cropRect.height() < bufferHeight) {
793 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
794 bufferHeight;
795 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
796 bufferHeight;
797 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800798 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800799 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800800 sx, 0, 0, 0,
801 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800802 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800803 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800804 };
805
Jamie Gennisa214c642011-01-14 13:53:31 -0800806 float mtxBeforeFlipV[16];
807 mtxMul(mtxBeforeFlipV, crop, xform);
808
809 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800810 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800811 // want to expose this to applications, however, so we must add an
812 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700813 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800814}
815
Andy McFadden2adaf042012-12-18 09:49:45 -0800816nsecs_t GLConsumer::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700817 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800818 Mutex::Autolock lock(mMutex);
819 return mCurrentTimestamp;
820}
821
Andy McFadden2adaf042012-12-18 09:49:45 -0800822EGLImageKHR GLConsumer::createImage(EGLDisplay dpy,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800823 const sp<GraphicBuffer>& graphicBuffer) {
824 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
825 EGLint attrs[] = {
826 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
827 EGL_NONE,
828 };
829 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
830 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700831 if (image == EGL_NO_IMAGE_KHR) {
832 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700833 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800834 }
835 return image;
836}
837
Andy McFadden2adaf042012-12-18 09:49:45 -0800838sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700839 Mutex::Autolock lock(mMutex);
840 return mCurrentTextureBuf;
841}
842
Andy McFadden2adaf042012-12-18 09:49:45 -0800843Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700844 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700845
846 Rect outCrop = mCurrentCrop;
847 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
848 int32_t newWidth = mCurrentCrop.width();
849 int32_t newHeight = mCurrentCrop.height();
850
851 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
852 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
853 ST_LOGV("too wide: newWidth = %d", newWidth);
854 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
855 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
856 ST_LOGV("too tall: newHeight = %d", newHeight);
857 }
858
859 // The crop is too wide
860 if (newWidth < mCurrentCrop.width()) {
861 int32_t dw = (newWidth - mCurrentCrop.width())/2;
862 outCrop.left -=dw;
863 outCrop.right += dw;
864 // The crop is too tall
865 } else if (newHeight < mCurrentCrop.height()) {
866 int32_t dh = (newHeight - mCurrentCrop.height())/2;
867 outCrop.top -= dh;
868 outCrop.bottom += dh;
869 }
870
871 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
872 outCrop.left, outCrop.top,
873 outCrop.right,outCrop.bottom);
874 }
875
Daniel Lam016c8cb2012-04-03 15:54:58 -0700876 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700877}
878
Andy McFadden2adaf042012-12-18 09:49:45 -0800879uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700880 Mutex::Autolock lock(mMutex);
881 return mCurrentTransform;
882}
883
Andy McFadden2adaf042012-12-18 09:49:45 -0800884uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700885 Mutex::Autolock lock(mMutex);
886 return mCurrentScalingMode;
887}
888
Andy McFadden2adaf042012-12-18 09:49:45 -0800889sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700890 Mutex::Autolock lock(mMutex);
891 return mCurrentFence;
892}
893
Andy McFadden2adaf042012-12-18 09:49:45 -0800894status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700895 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700896 return doGLFenceWaitLocked();
897}
898
Andy McFadden2adaf042012-12-18 09:49:45 -0800899status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700900
901 EGLDisplay dpy = eglGetCurrentDisplay();
902 EGLContext ctx = eglGetCurrentContext();
903
904 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
905 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
906 return INVALID_OPERATION;
907 }
908
909 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
910 ST_LOGE("doGLFenceWait: invalid current EGLContext");
911 return INVALID_OPERATION;
912 }
913
Jamie Gennis1df8c342012-12-20 14:05:45 -0800914 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700915 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700916 // Create an EGLSyncKHR from the current fence.
917 int fenceFd = mCurrentFence->dup();
918 if (fenceFd == -1) {
919 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
920 return -errno;
921 }
922 EGLint attribs[] = {
923 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
924 EGL_NONE
925 };
926 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
927 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
928 if (sync == EGL_NO_SYNC_KHR) {
929 close(fenceFd);
930 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
931 eglGetError());
932 return UNKNOWN_ERROR;
933 }
934
935 // XXX: The spec draft is inconsistent as to whether this should
936 // return an EGLint or void. Ignore the return value for now, as
937 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700938 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700939 EGLint eglErr = eglGetError();
940 eglDestroySyncKHR(dpy, sync);
941 if (eglErr != EGL_SUCCESS) {
942 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
943 eglErr);
944 return UNKNOWN_ERROR;
945 }
946 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700947 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800948 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700949 if (err != NO_ERROR) {
950 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
951 return err;
952 }
953 }
954 }
955
956 return NO_ERROR;
957}
958
Andy McFadden2adaf042012-12-18 09:49:45 -0800959void GLConsumer::freeBufferLocked(int slotIndex) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700960 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700961 if (slotIndex == mCurrentTexture) {
962 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
963 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700964 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700965 if (img != EGL_NO_IMAGE_KHR) {
966 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
967 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -0800968 }
Jamie Gennis9fea3422012-08-07 18:03:04 -0700969 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
970 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700971}
Daniel Lameae59d22012-01-22 15:26:27 -0800972
Andy McFadden2adaf042012-12-18 09:49:45 -0800973void GLConsumer::abandonLocked() {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700974 ST_LOGV("abandonLocked");
975 mCurrentTextureBuf.clear();
976 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700977}
978
Andy McFadden2adaf042012-12-18 09:49:45 -0800979void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -0800980 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -0700981 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700982 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -0800983}
984
Andy McFadden2adaf042012-12-18 09:49:45 -0800985status_t GLConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -0800986 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700987 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -0800988}
989
Andy McFadden2adaf042012-12-18 09:49:45 -0800990status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -0800991 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -0700992 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700993 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -0800994}
995
Andy McFadden2adaf042012-12-18 09:49:45 -0800996status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -0800997 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700998 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -0800999}
1000
Mathias Agopian74d211a2013-04-22 16:55:35 +02001001void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001002{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001003 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001004 "%smTexName=%d mCurrentTexture=%d\n"
1005 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1006 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1007 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1008 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001009
Mathias Agopian74d211a2013-04-22 16:55:35 +02001010 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001011}
1012
Jamie Gennisf238e282011-01-09 16:33:17 -08001013static void mtxMul(float out[16], const float a[16], const float b[16]) {
1014 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1015 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1016 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1017 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1018
1019 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1020 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1021 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1022 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1023
1024 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1025 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1026 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1027 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1028
1029 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1030 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1031 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1032 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1033}
1034
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001035}; // namespace android