blob: 3215b2fdebb94a9f2a3b49de522e9853e5837768 [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
Jamie Gennisdbe92452013-09-23 17:22:10 -070044EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
45#define CROP_EXT_STR "EGL_ANDROID_image_crop"
46
Andy McFadden97eba892012-12-11 15:21:45 -080047namespace android {
48
Andy McFadden2adaf042012-12-18 09:49:45 -080049// Macros for including the GLConsumer name in log messages
Steve Block6807e592011-10-20 11:56:00 +010050#define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block9d453682011-12-20 16:23:08 +000051#define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocka19954a2012-01-04 20:05:49 +000052#define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Block32397c12012-01-05 23:22:43 +000053#define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__)
Steve Blocke6f43dd2012-01-06 19:20:56 +000054#define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070055
Mathias Agopianad678e12013-07-23 17:28:53 -070056static const struct {
57 size_t width, height;
58 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070059} kDebugData = { 15, 12,
60 "___________________________________XX_XX_______X_X_____X_X____X_XXXXXXX_X____XXXXXXXXXXX__"
61 "___XX_XXX_XX_______XXXXXXX_________X___X_________X_____X__________________________________"
62};
Mathias Agopianad678e12013-07-23 17:28:53 -070063
Jamie Gennisf238e282011-01-09 16:33:17 -080064// Transform matrices
65static float mtxIdentity[16] = {
66 1, 0, 0, 0,
67 0, 1, 0, 0,
68 0, 0, 1, 0,
69 0, 0, 0, 1,
70};
71static float mtxFlipH[16] = {
72 -1, 0, 0, 0,
73 0, 1, 0, 0,
74 0, 0, 1, 0,
75 1, 0, 0, 1,
76};
77static float mtxFlipV[16] = {
78 1, 0, 0, 0,
79 0, -1, 0, 0,
80 0, 0, 1, 0,
81 0, 1, 0, 1,
82};
83static float mtxRot90[16] = {
84 0, 1, 0, 0,
85 -1, 0, 0, 0,
86 0, 0, 1, 0,
87 1, 0, 0, 1,
88};
Jamie Gennisf238e282011-01-09 16:33:17 -080089
90static void mtxMul(float out[16], const float a[16], const float b[16]);
91
Mathias Agopian9870c9b2013-08-08 17:46:48 -070092Mutex GLConsumer::sStaticInitLock;
93sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -070094
Jamie Gennisdbe92452013-09-23 17:22:10 -070095static bool hasEglAndroidImageCropImpl() {
96 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
97 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
98 size_t cropExtLen = strlen(CROP_EXT_STR);
99 size_t extsLen = strlen(exts);
100 bool equal = !strcmp(CROP_EXT_STR, exts);
101 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
102 bool atEnd = (cropExtLen+1) < extsLen &&
103 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
104 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
105 return equal || atStart || atEnd || inMiddle;
106}
107
108static bool hasEglAndroidImageCrop() {
109 // Only compute whether the extension is present once the first time this
110 // function is called.
111 static bool hasIt = hasEglAndroidImageCropImpl();
112 return hasIt;
113}
114
115static bool isEglImageCroppable(const Rect& crop) {
116 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
117}
118
Mathias Agopian3f844832013-08-07 21:24:32 -0700119GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
120 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700121 ConsumerBase(bq, isControlledByApp),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800122 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200123 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800124 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800125 mCurrentTimestamp(0),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700126 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200127 mDefaultWidth(1),
128 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700129 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700130 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800131 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800132 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700133 mEglDisplay(EGL_NO_DISPLAY),
134 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700135 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
136 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800137{
Andy McFadden2adaf042012-12-18 09:49:45 -0800138 ST_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800139
Jamie Gennisfa28c352011-09-16 17:30:26 -0700140 memcpy(mCurrentTransformMatrix, mtxIdentity,
141 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700142
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700143 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800144}
145
Andy McFadden2adaf042012-12-18 09:49:45 -0800146status_t GLConsumer::setDefaultMaxBufferCount(int bufferCount) {
Mathias Agopian80727112011-05-02 19:51:12 -0700147 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700148 return mConsumer->setDefaultMaxBufferCount(bufferCount);
Mathias Agopian80727112011-05-02 19:51:12 -0700149}
150
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800151
Andy McFadden2adaf042012-12-18 09:49:45 -0800152status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700153{
Daniel Lamb2675792012-02-23 14:35:13 -0800154 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700155 mDefaultWidth = w;
156 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700157 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700158}
159
Andy McFadden2adaf042012-12-18 09:49:45 -0800160status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800161 ATRACE_CALL();
162 ST_LOGV("updateTexImage");
163 Mutex::Autolock lock(mMutex);
164
165 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800166 ST_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800167 return NO_INIT;
168 }
169
170 // Make sure the EGL state is the same as in previous calls.
171 status_t err = checkAndUpdateEglStateLocked();
172 if (err != NO_ERROR) {
173 return err;
174 }
175
176 BufferQueue::BufferItem item;
177
178 // Acquire the next buffer.
179 // In asynchronous mode the list is guaranteed to be one buffer
180 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700181 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800182 if (err != NO_ERROR) {
183 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
184 // We always bind the texture even if we don't update its contents.
185 ST_LOGV("updateTexImage: no buffers were available");
186 glBindTexture(mTexTarget, mTexName);
187 err = NO_ERROR;
188 } else {
189 ST_LOGE("updateTexImage: acquire failed: %s (%d)",
190 strerror(-err), err);
191 }
192 return err;
193 }
194
195 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700196 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800197 if (err != NO_ERROR) {
198 // We always bind the texture.
199 glBindTexture(mTexTarget, mTexName);
200 return err;
201 }
202
Andy McFadden97eba892012-12-11 15:21:45 -0800203 // Bind the new buffer to the GL texture, and wait until it's ready.
204 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700205}
206
Mathias Agopianad678e12013-07-23 17:28:53 -0700207
208status_t GLConsumer::releaseTexImage() {
209 ATRACE_CALL();
210 ST_LOGV("releaseTexImage");
211 Mutex::Autolock lock(mMutex);
212
213 if (mAbandoned) {
214 ST_LOGE("releaseTexImage: GLConsumer is abandoned!");
215 return NO_INIT;
216 }
217
218 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700219 status_t err = NO_ERROR;
220
221 if (mAttached) {
222 err = checkAndUpdateEglStateLocked(true);
223 if (err != NO_ERROR) {
224 return err;
225 }
226 } else {
227 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700228 }
229
230 // Update the GLConsumer state.
231 int buf = mCurrentTexture;
232 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
233
Mathias Agopian45155962013-08-08 18:16:21 -0700234 ST_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700235
Mathias Agopian45155962013-08-08 18:16:21 -0700236 if (mAttached) {
237 // Do whatever sync ops we need to do before releasing the slot.
238 err = syncForReleaseLocked(mEglDisplay);
239 if (err != NO_ERROR) {
240 ST_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
241 return err;
242 }
243 } else {
244 // if we're detached, we just use the fence that was created in detachFromContext()
245 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700246 }
247
Mathias Agopian45155962013-08-08 18:16:21 -0700248 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700249 if (err < NO_ERROR) {
250 ST_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
251 strerror(-err), err);
252 return err;
253 }
254
Mathias Agopianad678e12013-07-23 17:28:53 -0700255 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700256 mCurrentTextureBuf = getDebugTexImageBuffer();
Mathias Agopianad678e12013-07-23 17:28:53 -0700257 mCurrentCrop.makeInvalid();
258 mCurrentTransform = 0;
259 mCurrentScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
260 mCurrentTimestamp = 0;
261 mCurrentFence = Fence::NO_FENCE;
262
Mathias Agopian45155962013-08-08 18:16:21 -0700263 if (mAttached) {
264 // bind a dummy texture
265 glBindTexture(mTexTarget, mTexName);
266 bindUnslottedBufferLocked(mEglDisplay);
267 } else {
268 // detached, don't touch the texture (and we may not even have an
269 // EGLDisplay here.
270 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700271 }
272
273 return NO_ERROR;
274}
275
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700276sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
277 Mutex::Autolock _l(sStaticInitLock);
278 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
279 // The first time, create the debug texture in case the application
280 // continues to use it.
281 sp<GraphicBuffer> buffer = new GraphicBuffer(
282 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
283 GraphicBuffer::USAGE_SW_WRITE_RARELY);
284 uint32_t* bits;
285 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
286 size_t w = buffer->getStride();
287 size_t h = buffer->getHeight();
288 memset(bits, 0, w*h*4);
289 for (size_t y=0 ; y<kDebugData.height ; y++) {
290 for (size_t x=0 ; x<kDebugData.width ; x++) {
291 bits[x] = (kDebugData.bits[y*kDebugData.width+x] == 'X') ? 0xFF000000 : 0xFFFFFFFF;
292 }
293 bits += w;
294 }
295 buffer->unlock();
296 sReleasedTexImageBuffer = buffer;
297 }
298 return sReleasedTexImageBuffer;
299}
300
Andy McFadden1585c4d2013-06-28 13:52:40 -0700301status_t GLConsumer::acquireBufferLocked(BufferQueue::BufferItem *item,
302 nsecs_t presentWhen) {
303 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700304 if (err != NO_ERROR) {
305 return err;
306 }
307
308 int slot = item->mBuf;
Jamie Gennisdbe92452013-09-23 17:22:10 -0700309 bool destroyEglImage = false;
310
311 if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) {
312 if (item->mGraphicBuffer != NULL) {
313 // This buffer has not been acquired before, so we must assume
314 // that any EGLImage in mEglSlots is stale.
315 destroyEglImage = true;
316 } else if (mEglSlots[slot].mCropRect != item->mCrop) {
317 // We've already seen this buffer before, but it now has a
318 // different crop rect, so we'll need to recreate the EGLImage if
319 // we're using the EGL_ANDROID_image_crop extension.
320 destroyEglImage = hasEglAndroidImageCrop();
Jamie Gennis9fea3422012-08-07 18:03:04 -0700321 }
322 }
323
Jamie Gennisdbe92452013-09-23 17:22:10 -0700324 if (destroyEglImage) {
325 if (!eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage)) {
326 ST_LOGW("acquireBufferLocked: eglDestroyImageKHR failed for slot=%d",
327 slot);
328 // keep going
329 }
330 mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR;
331 }
332
Jamie Gennis9fea3422012-08-07 18:03:04 -0700333 return NO_ERROR;
334}
335
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700336status_t GLConsumer::releaseBufferLocked(int buf,
337 sp<GraphicBuffer> graphicBuffer,
338 EGLDisplay display, EGLSyncKHR eglFence) {
339 // release the buffer if it hasn't already been discarded by the
340 // BufferQueue. This can happen, for example, when the producer of this
341 // buffer has reallocated the original buffer slot after this buffer
342 // was acquired.
343 status_t err = ConsumerBase::releaseBufferLocked(
344 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700345 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700346 return err;
347}
348
Mathias Agopianad678e12013-07-23 17:28:53 -0700349status_t GLConsumer::updateAndReleaseLocked(const BufferQueue::BufferItem& item)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800350{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700351 status_t err = NO_ERROR;
352
Andy McFadden87a67842014-04-04 15:37:08 -0700353 int buf = item.mBuf;
354
Jamie Gennis74bed552012-03-28 19:05:54 -0700355 if (!mAttached) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700356 ST_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700357 "ES context");
Andy McFadden87a67842014-04-04 15:37:08 -0700358 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
359 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700360 return INVALID_OPERATION;
361 }
362
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800363 // Confirm state.
364 err = checkAndUpdateEglStateLocked();
365 if (err != NO_ERROR) {
Andy McFadden87a67842014-04-04 15:37:08 -0700366 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
367 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800368 return err;
369 }
370
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800371 // If the mEglSlot entry is empty, create an EGLImage for the gralloc
372 // buffer currently in the slot in ConsumerBase.
373 //
374 // We may have to do this even when item.mGraphicBuffer == NULL (which
375 // means the buffer was previously acquired), if we destroyed the
376 // EGLImage when detaching from a context but the buffer has not been
377 // re-allocated.
378 if (mEglSlots[buf].mEglImage == EGL_NO_IMAGE_KHR) {
Jamie Gennisdbe92452013-09-23 17:22:10 -0700379 EGLImageKHR image = createImage(mEglDisplay,
380 mSlots[buf].mGraphicBuffer, item.mCrop);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800381 if (image == EGL_NO_IMAGE_KHR) {
Mathias Agopianad678e12013-07-23 17:28:53 -0700382 ST_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800383 mEglDisplay, buf);
Andy McFadden87a67842014-04-04 15:37:08 -0700384 const sp<GraphicBuffer>& gb = mSlots[buf].mGraphicBuffer;
385 ST_LOGW("buffer size=%ux%u st=%u usage=0x%x fmt=%d",
386 gb->getWidth(), gb->getHeight(), gb->getStride(),
387 gb->getUsage(), gb->getPixelFormat());
388 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
389 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800390 return UNKNOWN_ERROR;
391 }
392 mEglSlots[buf].mEglImage = image;
Jamie Gennisdbe92452013-09-23 17:22:10 -0700393 mEglSlots[buf].mCropRect = item.mCrop;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800394 }
395
396 // Do whatever sync ops we need to do before releasing the old slot.
397 err = syncForReleaseLocked(mEglDisplay);
398 if (err != NO_ERROR) {
399 // Release the buffer we just acquired. It's not safe to
400 // release the old buffer, so instead we just drop the new frame.
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700401 // As we are still under lock since acquireBuffer, it is safe to
402 // release by slot.
403 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer,
404 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800405 return err;
406 }
407
Mathias Agopianad678e12013-07-23 17:28:53 -0700408 ST_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800409 mCurrentTexture,
410 mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0,
411 buf, mSlots[buf].mGraphicBuffer->handle);
412
413 // release old buffer
414 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700415 status_t status = releaseBufferLocked(
416 mCurrentTexture, mCurrentTextureBuf, mEglDisplay,
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800417 mEglSlots[mCurrentTexture].mEglFence);
Mathias Agopianad678e12013-07-23 17:28:53 -0700418 if (status < NO_ERROR) {
419 ST_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800420 strerror(-status), status);
421 err = status;
422 // keep going, with error raised [?]
423 }
424 }
425
Andy McFadden2adaf042012-12-18 09:49:45 -0800426 // Update the GLConsumer state.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800427 mCurrentTexture = buf;
428 mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
429 mCurrentCrop = item.mCrop;
430 mCurrentTransform = item.mTransform;
431 mCurrentScalingMode = item.mScalingMode;
432 mCurrentTimestamp = item.mTimestamp;
433 mCurrentFence = item.mFence;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700434 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800435
436 computeCurrentTransformMatrixLocked();
437
438 return err;
439}
440
Andy McFadden2adaf042012-12-18 09:49:45 -0800441status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800442 if (mEglDisplay == EGL_NO_DISPLAY) {
443 ALOGE("bindTextureImage: invalid display");
444 return INVALID_OPERATION;
445 }
446
447 GLint error;
448 while ((error = glGetError()) != GL_NO_ERROR) {
449 ST_LOGW("bindTextureImage: clearing GL error: %#04x", error);
450 }
451
452 glBindTexture(mTexTarget, mTexName);
453 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
454 if (mCurrentTextureBuf == NULL) {
455 ST_LOGE("bindTextureImage: no currently-bound texture");
456 return NO_INIT;
457 }
Andy McFadden97eba892012-12-11 15:21:45 -0800458 status_t err = bindUnslottedBufferLocked(mEglDisplay);
459 if (err != NO_ERROR) {
460 return err;
461 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800462 } else {
463 EGLImageKHR image = mEglSlots[mCurrentTexture].mEglImage;
464
465 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
466
467 while ((error = glGetError()) != GL_NO_ERROR) {
468 ST_LOGE("bindTextureImage: error binding external texture image %p"
469 ": %#04x", image, error);
470 return UNKNOWN_ERROR;
471 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800472 }
Andy McFadden97eba892012-12-11 15:21:45 -0800473
474 // Wait for the new buffer to be ready.
475 return doGLFenceWaitLocked();
476
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800477}
478
Mathias Agopian45155962013-08-08 18:16:21 -0700479status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700480 EGLDisplay dpy = eglGetCurrentDisplay();
481 EGLContext ctx = eglGetCurrentContext();
482
Mathias Agopian45155962013-08-08 18:16:21 -0700483 if (!contextCheck) {
484 // if this is the first time we're called, mEglDisplay/mEglContext have
485 // never been set, so don't error out (below).
486 if (mEglDisplay == EGL_NO_DISPLAY) {
487 mEglDisplay = dpy;
488 }
489 if (mEglContext == EGL_NO_DISPLAY) {
490 mEglContext = ctx;
491 }
492 }
493
494 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800495 ST_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700496 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700497 }
498
Mathias Agopian45155962013-08-08 18:16:21 -0700499 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800500 ST_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700501 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700502 }
503
504 mEglDisplay = dpy;
505 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800506 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800507}
508
Jesse Hall13f01cb2013-03-20 11:37:21 -0700509void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
510 if (fence->isValid() &&
511 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700512 status_t err = addReleaseFence(mCurrentTexture,
513 mCurrentTextureBuf, fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700514 if (err != OK) {
515 ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
516 strerror(-err), err);
517 }
Jesse Hallef194142012-06-14 14:45:17 -0700518 }
519}
520
Andy McFadden2adaf042012-12-18 09:49:45 -0800521status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700522 ATRACE_CALL();
523 ST_LOGV("detachFromContext");
524 Mutex::Autolock lock(mMutex);
525
526 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800527 ST_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700528 return NO_INIT;
529 }
530
531 if (!mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800532 ST_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700533 "context");
534 return INVALID_OPERATION;
535 }
536
537 EGLDisplay dpy = eglGetCurrentDisplay();
538 EGLContext ctx = eglGetCurrentContext();
539
540 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
541 ST_LOGE("detachFromContext: invalid current EGLDisplay");
542 return INVALID_OPERATION;
543 }
544
545 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
546 ST_LOGE("detachFromContext: invalid current EGLContext");
547 return INVALID_OPERATION;
548 }
549
550 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
551 status_t err = syncForReleaseLocked(dpy);
552 if (err != OK) {
553 return err;
554 }
555
556 glDeleteTextures(1, &mTexName);
557 }
558
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700559 // Because we're giving up the EGLDisplay we need to free all the EGLImages
560 // that are associated with it. They'll be recreated when the
Andy McFadden2adaf042012-12-18 09:49:45 -0800561 // GLConsumer gets attached to a new OpenGL ES context (and thus gets a
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700562 // new EGLDisplay).
563 for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Jamie Gennis9fea3422012-08-07 18:03:04 -0700564 EGLImageKHR img = mEglSlots[i].mEglImage;
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700565 if (img != EGL_NO_IMAGE_KHR) {
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700566 eglDestroyImageKHR(mEglDisplay, img);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700567 mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
Jamie Gennis9aa74db2012-04-17 13:07:45 -0700568 }
569 }
570
Jamie Gennis74bed552012-03-28 19:05:54 -0700571 mEglDisplay = EGL_NO_DISPLAY;
572 mEglContext = EGL_NO_CONTEXT;
573 mAttached = false;
574
575 return OK;
576}
577
Mathias Agopian3f844832013-08-07 21:24:32 -0700578status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700579 ATRACE_CALL();
580 ST_LOGV("attachToContext");
581 Mutex::Autolock lock(mMutex);
582
583 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800584 ST_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700585 return NO_INIT;
586 }
587
588 if (mAttached) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800589 ST_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700590 "context");
591 return INVALID_OPERATION;
592 }
593
594 EGLDisplay dpy = eglGetCurrentDisplay();
595 EGLContext ctx = eglGetCurrentContext();
596
597 if (dpy == EGL_NO_DISPLAY) {
598 ST_LOGE("attachToContext: invalid current EGLDisplay");
599 return INVALID_OPERATION;
600 }
601
602 if (ctx == EGL_NO_CONTEXT) {
603 ST_LOGE("attachToContext: invalid current EGLContext");
604 return INVALID_OPERATION;
605 }
606
607 // We need to bind the texture regardless of whether there's a current
608 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700609 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700610
611 if (mCurrentTextureBuf != NULL) {
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700612 // The EGLImageKHR that was associated with the slot was destroyed when
Andy McFadden2adaf042012-12-18 09:49:45 -0800613 // the GLConsumer was detached from the old context, so we need to
Jamie Gennis5c8a6082012-05-02 13:10:56 -0700614 // recreate it here.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800615 status_t err = bindUnslottedBufferLocked(dpy);
616 if (err != NO_ERROR) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700617 return err;
618 }
619 }
620
621 mEglDisplay = dpy;
622 mEglContext = ctx;
623 mTexName = tex;
624 mAttached = true;
625
626 return OK;
627}
628
Andy McFadden2adaf042012-12-18 09:49:45 -0800629status_t GLConsumer::bindUnslottedBufferLocked(EGLDisplay dpy) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800630 ST_LOGV("bindUnslottedBuffer ct=%d ctb=%p",
631 mCurrentTexture, mCurrentTextureBuf.get());
632
633 // Create a temporary EGLImageKHR.
Jamie Gennisdbe92452013-09-23 17:22:10 -0700634 Rect crop;
635 EGLImageKHR image = createImage(dpy, mCurrentTextureBuf, mCurrentCrop);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800636 if (image == EGL_NO_IMAGE_KHR) {
637 return UNKNOWN_ERROR;
638 }
639
640 // Attach the current buffer to the GL texture.
641 glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image);
642
643 GLint error;
644 status_t err = OK;
645 while ((error = glGetError()) != GL_NO_ERROR) {
646 ST_LOGE("bindUnslottedBuffer: error binding external texture image %p "
647 "(slot %d): %#04x", image, mCurrentTexture, error);
648 err = UNKNOWN_ERROR;
649 }
650
651 // We destroy the EGLImageKHR here because the current buffer may no
652 // longer be associated with one of the buffer slots, so we have
653 // nowhere to to store it. If the buffer is still associated with a
654 // slot then another EGLImageKHR will be created next time that buffer
655 // gets acquired in updateTexImage.
656 eglDestroyImageKHR(dpy, image);
657
658 return err;
659}
660
661
Andy McFadden2adaf042012-12-18 09:49:45 -0800662status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700663 ST_LOGV("syncForReleaseLocked");
664
Jamie Gennis01dbf552012-09-06 14:54:19 -0700665 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700666 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700667 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
668 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
669 if (sync == EGL_NO_SYNC_KHR) {
670 ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
671 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700672 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700673 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700674 glFlush();
675 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700676 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700677 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
678 ST_LOGE("syncForReleaseLocked: error dup'ing native fence "
679 "fd: %#x", eglGetError());
680 return UNKNOWN_ERROR;
681 }
682 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700683 status_t err = addReleaseFenceLocked(mCurrentTexture,
684 mCurrentTextureBuf, fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700685 if (err != OK) {
686 ST_LOGE("syncForReleaseLocked: error adding release fence: "
687 "%s (%d)", strerror(-err), err);
688 return err;
689 }
Mathias Agopianca088332013-03-28 17:44:13 -0700690 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700691 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
692 if (fence != EGL_NO_SYNC_KHR) {
693 // There is already a fence for the current slot. We need to
694 // wait on that before replacing it with another fence to
695 // ensure that all outstanding buffer accesses have completed
696 // before the producer accesses it.
697 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
698 if (result == EGL_FALSE) {
699 ST_LOGE("syncForReleaseLocked: error waiting for previous "
700 "fence: %#x", eglGetError());
701 return UNKNOWN_ERROR;
702 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
703 ST_LOGE("syncForReleaseLocked: timeout waiting for previous "
704 "fence");
705 return TIMED_OUT;
706 }
707 eglDestroySyncKHR(dpy, fence);
708 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700709
Jamie Gennis01dbf552012-09-06 14:54:19 -0700710 // Create a fence for the outstanding accesses in the current
711 // OpenGL ES context.
712 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
713 if (fence == EGL_NO_SYNC_KHR) {
714 ST_LOGE("syncForReleaseLocked: error creating fence: %#x",
715 eglGetError());
716 return UNKNOWN_ERROR;
717 }
718 glFlush();
719 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700720 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700721 }
722
723 return OK;
724}
725
Andy McFadden2adaf042012-12-18 09:49:45 -0800726bool GLConsumer::isExternalFormat(uint32_t format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700727{
728 switch (format) {
729 // supported YUV formats
730 case HAL_PIXEL_FORMAT_YV12:
731 // Legacy/deprecated YUV formats
732 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
733 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
734 case HAL_PIXEL_FORMAT_YCbCr_422_I:
735 return true;
736 }
737
738 // Any OEM format needs to be considered
739 if (format>=0x100 && format<=0x1FF)
740 return true;
741
742 return false;
743}
744
Mathias Agopian3f844832013-08-07 21:24:32 -0700745uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700746 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700747}
748
Andy McFadden2adaf042012-12-18 09:49:45 -0800749void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800750 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700751 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
752}
753
Andy McFadden2adaf042012-12-18 09:49:45 -0800754void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700755 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700756 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -0800757 ST_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700758 return;
759 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700760 bool needsRecompute = mFilteringEnabled != enabled;
761 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700762
763 if (needsRecompute && mCurrentTextureBuf==NULL) {
764 ST_LOGD("setFilteringEnabled called with mCurrentTextureBuf == NULL");
765 }
766
767 if (needsRecompute && mCurrentTextureBuf != NULL) {
768 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700769 }
770}
771
Andy McFadden2adaf042012-12-18 09:49:45 -0800772void GLConsumer::computeCurrentTransformMatrixLocked() {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700773 ST_LOGV("computeCurrentTransformMatrixLocked");
Jamie Gennisf238e282011-01-09 16:33:17 -0800774
Jamie Gennisa214c642011-01-14 13:53:31 -0800775 float xform[16];
776 for (int i = 0; i < 16; i++) {
777 xform[i] = mtxIdentity[i];
778 }
779 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
780 float result[16];
781 mtxMul(result, xform, mtxFlipH);
782 for (int i = 0; i < 16; i++) {
783 xform[i] = result[i];
784 }
785 }
786 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
787 float result[16];
788 mtxMul(result, xform, mtxFlipV);
789 for (int i = 0; i < 16; i++) {
790 xform[i] = result[i];
791 }
792 }
793 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
794 float result[16];
795 mtxMul(result, xform, mtxRot90);
796 for (int i = 0; i < 16; i++) {
797 xform[i] = result[i];
798 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800799 }
800
Daniel Lameae59d22012-01-22 15:26:27 -0800801 sp<GraphicBuffer>& buf(mCurrentTextureBuf);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700802
803 if (buf == NULL) {
804 ST_LOGD("computeCurrentTransformMatrixLocked: mCurrentTextureBuf is NULL");
805 }
806
Jamie Gennisdbe92452013-09-23 17:22:10 -0700807 float mtxBeforeFlipV[16];
808 if (!isEglImageCroppable(mCurrentCrop)) {
809 Rect cropRect = mCurrentCrop;
810 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
811 float bufferWidth = buf->getWidth();
812 float bufferHeight = buf->getHeight();
813 if (!cropRect.isEmpty()) {
814 float shrinkAmount = 0.0f;
815 if (mFilteringEnabled) {
816 // In order to prevent bilinear sampling beyond the edge of the
817 // crop rectangle we may need to shrink it by 2 texels in each
818 // dimension. Normally this would just need to take 1/2 a texel
819 // off each end, but because the chroma channels of YUV420 images
820 // are subsampled we may need to shrink the crop region by a whole
821 // texel on each side.
822 switch (buf->getPixelFormat()) {
823 case PIXEL_FORMAT_RGBA_8888:
824 case PIXEL_FORMAT_RGBX_8888:
825 case PIXEL_FORMAT_RGB_888:
826 case PIXEL_FORMAT_RGB_565:
827 case PIXEL_FORMAT_BGRA_8888:
828 // We know there's no subsampling of any channels, so we
829 // only need to shrink by a half a pixel.
830 shrinkAmount = 0.5;
831 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700832
Jamie Gennisdbe92452013-09-23 17:22:10 -0700833 default:
834 // If we don't recognize the format, we must assume the
835 // worst case (that we care about), which is YUV420.
836 shrinkAmount = 1.0;
837 break;
838 }
839 }
840
841 // Only shrink the dimensions that are not the size of the buffer.
842 if (cropRect.width() < bufferWidth) {
843 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
844 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
845 bufferWidth;
846 }
847 if (cropRect.height() < bufferHeight) {
848 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
849 bufferHeight;
850 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
851 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700852 }
853 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700854 float crop[16] = {
855 sx, 0, 0, 0,
856 0, sy, 0, 0,
857 0, 0, 1, 0,
858 tx, ty, 0, 1,
859 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700860
Jamie Gennisdbe92452013-09-23 17:22:10 -0700861 mtxMul(mtxBeforeFlipV, crop, xform);
862 } else {
863 for (int i = 0; i < 16; i++) {
864 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700865 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800866 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800867
868 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800869 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800870 // want to expose this to applications, however, so we must add an
871 // additional vertical flip to the transform after all the other transforms.
Jamie Gennis736aa952011-06-12 17:03:06 -0700872 mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800873}
874
Andy McFadden2adaf042012-12-18 09:49:45 -0800875nsecs_t GLConsumer::getTimestamp() {
Jamie Gennis6ee96ad2011-10-19 13:36:31 -0700876 ST_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800877 Mutex::Autolock lock(mMutex);
878 return mCurrentTimestamp;
879}
880
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700881nsecs_t GLConsumer::getFrameNumber() {
882 ST_LOGV("getFrameNumber");
883 Mutex::Autolock lock(mMutex);
884 return mCurrentFrameNumber;
885}
886
Andy McFadden2adaf042012-12-18 09:49:45 -0800887EGLImageKHR GLConsumer::createImage(EGLDisplay dpy,
Jamie Gennisdbe92452013-09-23 17:22:10 -0700888 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800889 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
890 EGLint attrs[] = {
Jamie Gennisdbe92452013-09-23 17:22:10 -0700891 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
892 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
893 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
894 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
895 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800896 EGL_NONE,
897 };
Jamie Gennisdbe92452013-09-23 17:22:10 -0700898 if (!crop.isValid()) {
899 // No crop rect to set, so terminate the attrib array before the crop.
900 attrs[2] = EGL_NONE;
901 } else if (!isEglImageCroppable(crop)) {
902 // The crop rect is not at the origin, so we can't set the crop on the
903 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
904 // extension. In the future we can add a layered extension that
905 // removes this restriction if there is hardware that can support it.
906 attrs[2] = EGL_NONE;
907 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800908 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
909 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
Mathias Agopian3cd5a112011-04-26 14:57:40 -0700910 if (image == EGL_NO_IMAGE_KHR) {
911 EGLint error = eglGetError();
Jamie Gennisfa28c352011-09-16 17:30:26 -0700912 ST_LOGE("error creating EGLImage: %#x", error);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800913 }
914 return image;
915}
916
Andy McFadden2adaf042012-12-18 09:49:45 -0800917sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700918 Mutex::Autolock lock(mMutex);
919 return mCurrentTextureBuf;
920}
921
Andy McFadden2adaf042012-12-18 09:49:45 -0800922Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700923 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700924
925 Rect outCrop = mCurrentCrop;
926 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
927 int32_t newWidth = mCurrentCrop.width();
928 int32_t newHeight = mCurrentCrop.height();
929
930 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
931 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
932 ST_LOGV("too wide: newWidth = %d", newWidth);
933 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
934 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
935 ST_LOGV("too tall: newHeight = %d", newHeight);
936 }
937
938 // The crop is too wide
939 if (newWidth < mCurrentCrop.width()) {
940 int32_t dw = (newWidth - mCurrentCrop.width())/2;
941 outCrop.left -=dw;
942 outCrop.right += dw;
943 // The crop is too tall
944 } else if (newHeight < mCurrentCrop.height()) {
945 int32_t dh = (newHeight - mCurrentCrop.height())/2;
946 outCrop.top -= dh;
947 outCrop.bottom += dh;
948 }
949
950 ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
951 outCrop.left, outCrop.top,
952 outCrop.right,outCrop.bottom);
953 }
954
Daniel Lam016c8cb2012-04-03 15:54:58 -0700955 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700956}
957
Andy McFadden2adaf042012-12-18 09:49:45 -0800958uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700959 Mutex::Autolock lock(mMutex);
960 return mCurrentTransform;
961}
962
Andy McFadden2adaf042012-12-18 09:49:45 -0800963uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700964 Mutex::Autolock lock(mMutex);
965 return mCurrentScalingMode;
966}
967
Andy McFadden2adaf042012-12-18 09:49:45 -0800968sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700969 Mutex::Autolock lock(mMutex);
970 return mCurrentFence;
971}
972
Andy McFadden2adaf042012-12-18 09:49:45 -0800973status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700974 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700975 return doGLFenceWaitLocked();
976}
977
Andy McFadden2adaf042012-12-18 09:49:45 -0800978status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700979
980 EGLDisplay dpy = eglGetCurrentDisplay();
981 EGLContext ctx = eglGetCurrentContext();
982
983 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
984 ST_LOGE("doGLFenceWait: invalid current EGLDisplay");
985 return INVALID_OPERATION;
986 }
987
988 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
989 ST_LOGE("doGLFenceWait: invalid current EGLContext");
990 return INVALID_OPERATION;
991 }
992
Jamie Gennis1df8c342012-12-20 14:05:45 -0800993 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700994 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700995 // Create an EGLSyncKHR from the current fence.
996 int fenceFd = mCurrentFence->dup();
997 if (fenceFd == -1) {
998 ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
999 return -errno;
1000 }
1001 EGLint attribs[] = {
1002 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
1003 EGL_NONE
1004 };
1005 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
1006 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
1007 if (sync == EGL_NO_SYNC_KHR) {
1008 close(fenceFd);
1009 ST_LOGE("doGLFenceWait: error creating EGL fence: %#x",
1010 eglGetError());
1011 return UNKNOWN_ERROR;
1012 }
1013
1014 // XXX: The spec draft is inconsistent as to whether this should
1015 // return an EGLint or void. Ignore the return value for now, as
1016 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -07001017 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001018 EGLint eglErr = eglGetError();
1019 eglDestroySyncKHR(dpy, sync);
1020 if (eglErr != EGL_SUCCESS) {
1021 ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
1022 eglErr);
1023 return UNKNOWN_ERROR;
1024 }
1025 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -07001026 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -08001027 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001028 if (err != NO_ERROR) {
1029 ST_LOGE("doGLFenceWait: error waiting for fence: %d", err);
1030 return err;
1031 }
1032 }
1033 }
1034
1035 return NO_ERROR;
1036}
1037
Andy McFadden2adaf042012-12-18 09:49:45 -08001038void GLConsumer::freeBufferLocked(int slotIndex) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001039 ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001040 if (slotIndex == mCurrentTexture) {
1041 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1042 }
Jamie Gennis9fea3422012-08-07 18:03:04 -07001043 EGLImageKHR img = mEglSlots[slotIndex].mEglImage;
Jamie Gennis9aa74db2012-04-17 13:07:45 -07001044 if (img != EGL_NO_IMAGE_KHR) {
1045 ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img);
1046 eglDestroyImageKHR(mEglDisplay, img);
Daniel Lameae59d22012-01-22 15:26:27 -08001047 }
Jamie Gennis9fea3422012-08-07 18:03:04 -07001048 mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR;
1049 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001050}
Daniel Lameae59d22012-01-22 15:26:27 -08001051
Andy McFadden2adaf042012-12-18 09:49:45 -08001052void GLConsumer::abandonLocked() {
Jamie Gennis9fea3422012-08-07 18:03:04 -07001053 ST_LOGV("abandonLocked");
1054 mCurrentTextureBuf.clear();
1055 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001056}
1057
Andy McFadden2adaf042012-12-18 09:49:45 -08001058void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001059 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -07001060 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001061 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001062}
1063
Andy McFadden2adaf042012-12-18 09:49:45 -08001064status_t GLConsumer::setDefaultBufferFormat(uint32_t defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001065 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001066 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001067}
1068
Andy McFadden2adaf042012-12-18 09:49:45 -08001069status_t GLConsumer::setConsumerUsageBits(uint32_t usage) {
Daniel Lamb2675792012-02-23 14:35:13 -08001070 Mutex::Autolock lock(mMutex);
Eino-Ville Talvala85b21762012-04-13 15:16:31 -07001071 usage |= DEFAULT_USAGE_FLAGS;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001072 return mConsumer->setConsumerUsageBits(usage);
Daniel Lamb2675792012-02-23 14:35:13 -08001073}
1074
Andy McFadden2adaf042012-12-18 09:49:45 -08001075status_t GLConsumer::setTransformHint(uint32_t hint) {
Daniel Lamb2675792012-02-23 14:35:13 -08001076 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001077 return mConsumer->setTransformHint(hint);
Daniel Lamb2675792012-02-23 14:35:13 -08001078}
1079
Mathias Agopian74d211a2013-04-22 16:55:35 +02001080void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001081{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001082 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001083 "%smTexName=%d mCurrentTexture=%d\n"
1084 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1085 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1086 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1087 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001088
Mathias Agopian74d211a2013-04-22 16:55:35 +02001089 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001090}
1091
Jamie Gennisf238e282011-01-09 16:33:17 -08001092static void mtxMul(float out[16], const float a[16], const float b[16]) {
1093 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1094 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1095 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1096 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1097
1098 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1099 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1100 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1101 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1102
1103 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1104 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1105 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1106 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1107
1108 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1109 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1110 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1111 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1112}
1113
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001114}; // namespace android