blob: 9937399a87eaf55b99e33ec7b627fd194f0983f6 [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
Dan Stozadd264162015-03-12 13:58:47 -070032#include <gui/BufferItem.h>
Mathias Agopianca088332013-03-28 17:44:13 -070033#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080034#include <gui/IGraphicBufferAlloc.h>
35#include <gui/ISurfaceComposer.h>
36#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080037
Mathias Agopian90ac7992012-02-25 18:48:35 -080038#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070039#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080040
41#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070042#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080044
Jamie Gennisdbe92452013-09-23 17:22:10 -070045EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
46#define CROP_EXT_STR "EGL_ANDROID_image_crop"
47
Andy McFadden97eba892012-12-11 15:21:45 -080048namespace android {
49
Andy McFadden2adaf042012-12-18 09:49:45 -080050// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080051#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
52#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
53//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
54#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
55#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070056
Mathias Agopianad678e12013-07-23 17:28:53 -070057static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080058 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070059 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070060} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080061 "_______________"
62 "_______________"
63 "_____XX_XX_____"
64 "__X_X_____X_X__"
65 "__X_XXXXXXX_X__"
66 "__XXXXXXXXXXX__"
67 "___XX_XXX_XX___"
68 "____XXXXXXX____"
69 "_____X___X_____"
70 "____X_____X____"
71 "_______________"
72 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070073};
Mathias Agopianad678e12013-07-23 17:28:53 -070074
Jamie Gennisf238e282011-01-09 16:33:17 -080075// Transform matrices
76static float mtxIdentity[16] = {
77 1, 0, 0, 0,
78 0, 1, 0, 0,
79 0, 0, 1, 0,
80 0, 0, 0, 1,
81};
82static float mtxFlipH[16] = {
83 -1, 0, 0, 0,
84 0, 1, 0, 0,
85 0, 0, 1, 0,
86 1, 0, 0, 1,
87};
88static float mtxFlipV[16] = {
89 1, 0, 0, 0,
90 0, -1, 0, 0,
91 0, 0, 1, 0,
92 0, 1, 0, 1,
93};
94static float mtxRot90[16] = {
95 0, 1, 0, 0,
96 -1, 0, 0, 0,
97 0, 0, 1, 0,
98 1, 0, 0, 1,
99};
Jamie Gennisf238e282011-01-09 16:33:17 -0800100
101static void mtxMul(float out[16], const float a[16], const float b[16]);
102
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700103Mutex GLConsumer::sStaticInitLock;
104sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -0700105
Jamie Gennisdbe92452013-09-23 17:22:10 -0700106static bool hasEglAndroidImageCropImpl() {
107 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
108 const char* exts = eglQueryStringImplementationANDROID(dpy, EGL_EXTENSIONS);
109 size_t cropExtLen = strlen(CROP_EXT_STR);
110 size_t extsLen = strlen(exts);
111 bool equal = !strcmp(CROP_EXT_STR, exts);
112 bool atStart = !strncmp(CROP_EXT_STR " ", exts, cropExtLen+1);
113 bool atEnd = (cropExtLen+1) < extsLen &&
114 !strcmp(" " CROP_EXT_STR, exts + extsLen - (cropExtLen+1));
115 bool inMiddle = strstr(exts, " " CROP_EXT_STR " ");
116 return equal || atStart || atEnd || inMiddle;
117}
118
119static bool hasEglAndroidImageCrop() {
120 // Only compute whether the extension is present once the first time this
121 // function is called.
122 static bool hasIt = hasEglAndroidImageCropImpl();
123 return hasIt;
124}
125
126static bool isEglImageCroppable(const Rect& crop) {
127 return hasEglAndroidImageCrop() && (crop.left == 0 && crop.top == 0);
128}
129
Mathias Agopian3f844832013-08-07 21:24:32 -0700130GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
131 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700132 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700133 mCurrentCrop(Rect::EMPTY_RECT),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800134 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200135 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800136 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800137 mCurrentTimestamp(0),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700138 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200139 mDefaultWidth(1),
140 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700141 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700142 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800143 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800144 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700145 mEglDisplay(EGL_NO_DISPLAY),
146 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700147 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
148 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800149{
Dan Stozad723bd72014-11-18 10:24:03 -0800150 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800151
Jamie Gennisfa28c352011-09-16 17:30:26 -0700152 memcpy(mCurrentTransformMatrix, mtxIdentity,
153 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700154
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700155 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800156}
157
Dan Stozaab574912014-06-25 14:21:45 -0700158GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
159 bool useFenceSync, bool isControlledByApp) :
160 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700161 mCurrentCrop(Rect::EMPTY_RECT),
Dan Stozaab574912014-06-25 14:21:45 -0700162 mCurrentTransform(0),
163 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
164 mCurrentFence(Fence::NO_FENCE),
165 mCurrentTimestamp(0),
166 mCurrentFrameNumber(0),
167 mDefaultWidth(1),
168 mDefaultHeight(1),
169 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800170 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700171 mUseFenceSync(useFenceSync),
172 mTexTarget(texTarget),
173 mEglDisplay(EGL_NO_DISPLAY),
174 mEglContext(EGL_NO_CONTEXT),
175 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
176 mAttached(false)
177{
Dan Stozad723bd72014-11-18 10:24:03 -0800178 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700179
180 memcpy(mCurrentTransformMatrix, mtxIdentity,
181 sizeof(mCurrentTransformMatrix));
182
183 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
184}
185
Andy McFadden2adaf042012-12-18 09:49:45 -0800186status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700187{
Daniel Lamb2675792012-02-23 14:35:13 -0800188 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700189 mDefaultWidth = w;
190 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700191 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700192}
193
Andy McFadden2adaf042012-12-18 09:49:45 -0800194status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800195 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800196 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800197 Mutex::Autolock lock(mMutex);
198
199 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800200 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800201 return NO_INIT;
202 }
203
204 // Make sure the EGL state is the same as in previous calls.
205 status_t err = checkAndUpdateEglStateLocked();
206 if (err != NO_ERROR) {
207 return err;
208 }
209
Dan Stozadd264162015-03-12 13:58:47 -0700210 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800211
212 // Acquire the next buffer.
213 // In asynchronous mode the list is guaranteed to be one buffer
214 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700215 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800216 if (err != NO_ERROR) {
217 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
218 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800219 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800220 glBindTexture(mTexTarget, mTexName);
221 err = NO_ERROR;
222 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800223 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800224 strerror(-err), err);
225 }
226 return err;
227 }
228
229 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700230 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800231 if (err != NO_ERROR) {
232 // We always bind the texture.
233 glBindTexture(mTexTarget, mTexName);
234 return err;
235 }
236
Andy McFadden97eba892012-12-11 15:21:45 -0800237 // Bind the new buffer to the GL texture, and wait until it's ready.
238 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700239}
240
Mathias Agopianad678e12013-07-23 17:28:53 -0700241
242status_t GLConsumer::releaseTexImage() {
243 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800244 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700245 Mutex::Autolock lock(mMutex);
246
247 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800248 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700249 return NO_INIT;
250 }
251
252 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700253 status_t err = NO_ERROR;
254
255 if (mAttached) {
256 err = checkAndUpdateEglStateLocked(true);
257 if (err != NO_ERROR) {
258 return err;
259 }
260 } else {
261 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700262 }
263
264 // Update the GLConsumer state.
265 int buf = mCurrentTexture;
266 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
267
Dan Stozad723bd72014-11-18 10:24:03 -0800268 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700269
Mathias Agopian45155962013-08-08 18:16:21 -0700270 if (mAttached) {
271 // Do whatever sync ops we need to do before releasing the slot.
272 err = syncForReleaseLocked(mEglDisplay);
273 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800274 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700275 return err;
276 }
277 } else {
278 // if we're detached, we just use the fence that was created in detachFromContext()
279 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700280 }
281
Mathias Agopian45155962013-08-08 18:16:21 -0700282 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700283 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800284 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700285 strerror(-err), err);
286 return err;
287 }
288
Eric Penner5c3d2432014-07-11 19:08:04 -0700289 if (mReleasedTexImage == NULL) {
290 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
291 }
292
Mathias Agopianad678e12013-07-23 17:28:53 -0700293 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700294 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700295 mCurrentCrop.makeInvalid();
296 mCurrentTransform = 0;
Mathias Agopianad678e12013-07-23 17:28:53 -0700297 mCurrentTimestamp = 0;
298 mCurrentFence = Fence::NO_FENCE;
299
Mathias Agopian45155962013-08-08 18:16:21 -0700300 if (mAttached) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700301 // This binds a dummy buffer (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800302 status_t result = bindTextureImageLocked();
303 if (result != NO_ERROR) {
304 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700305 }
Mathias Agopian45155962013-08-08 18:16:21 -0700306 } else {
307 // detached, don't touch the texture (and we may not even have an
308 // EGLDisplay here.
309 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700310 }
311
312 return NO_ERROR;
313}
314
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700315sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
316 Mutex::Autolock _l(sStaticInitLock);
317 if (CC_UNLIKELY(sReleasedTexImageBuffer == NULL)) {
318 // The first time, create the debug texture in case the application
319 // continues to use it.
320 sp<GraphicBuffer> buffer = new GraphicBuffer(
321 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
322 GraphicBuffer::USAGE_SW_WRITE_RARELY);
323 uint32_t* bits;
324 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800325 uint32_t stride = buffer->getStride();
326 uint32_t height = buffer->getHeight();
327 memset(bits, 0, stride * height * 4);
328 for (uint32_t y = 0; y < kDebugData.height; y++) {
329 for (uint32_t x = 0; x < kDebugData.width; x++) {
330 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
331 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700332 }
Dan Stozad723bd72014-11-18 10:24:03 -0800333 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700334 }
335 buffer->unlock();
336 sReleasedTexImageBuffer = buffer;
337 }
338 return sReleasedTexImageBuffer;
339}
340
Dan Stozadd264162015-03-12 13:58:47 -0700341status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700342 nsecs_t presentWhen, uint64_t maxFrameNumber) {
343 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
344 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700345 if (err != NO_ERROR) {
346 return err;
347 }
348
Eric Penner5c3d2432014-07-11 19:08:04 -0700349 // If item->mGraphicBuffer is not null, this buffer has not been acquired
350 // before, so any prior EglImage created is using a stale buffer. This
351 // replaces any old EglImage with a new one (using the new buffer).
352 if (item->mGraphicBuffer != NULL) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700353 int slot = item->mSlot;
Eric Penner5c3d2432014-07-11 19:08:04 -0700354 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700355 }
356
Jamie Gennis9fea3422012-08-07 18:03:04 -0700357 return NO_ERROR;
358}
359
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700360status_t GLConsumer::releaseBufferLocked(int buf,
361 sp<GraphicBuffer> graphicBuffer,
362 EGLDisplay display, EGLSyncKHR eglFence) {
363 // release the buffer if it hasn't already been discarded by the
364 // BufferQueue. This can happen, for example, when the producer of this
365 // buffer has reallocated the original buffer slot after this buffer
366 // was acquired.
367 status_t err = ConsumerBase::releaseBufferLocked(
368 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700369 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700370 return err;
371}
372
Dan Stoza3ce46042015-11-17 17:00:45 -0800373status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item,
374 PendingRelease* pendingRelease)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800375{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700376 status_t err = NO_ERROR;
377
Pablo Ceballos47650f42015-08-04 16:38:17 -0700378 int slot = item.mSlot;
Andy McFadden87a67842014-04-04 15:37:08 -0700379
Jamie Gennis74bed552012-03-28 19:05:54 -0700380 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800381 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700382 "ES context");
Pablo Ceballos47650f42015-08-04 16:38:17 -0700383 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700384 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700385 return INVALID_OPERATION;
386 }
387
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800388 // Confirm state.
389 err = checkAndUpdateEglStateLocked();
390 if (err != NO_ERROR) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700391 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700392 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800393 return err;
394 }
395
Pablo Ceballos6366a102016-03-21 16:54:08 -0700396 // For investigating b/27674961
397 if (mEglSlots[slot].mEglImage == nullptr) {
398 ALOGE("If you see this message in a log please post the log to "
399 "b/27674961");
400 ALOGE("slot = %d, mCurrentTexture = %d, mCurrentTextureImage = %p",
401 slot, mCurrentTexture, mCurrentTextureImage.get());
402 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
403 ALOGE("mEglSlots[%d].mEglImage = %p", i,
404 mEglSlots[i].mEglImage.get());
405 }
406 String8 dump;
407 dumpLocked(dump, "");
408 ALOGE("%s", dump.string());
409 }
410
Eric Penner5c3d2432014-07-11 19:08:04 -0700411 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
412 // if nessessary, for the gralloc buffer currently in the slot in
413 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800414 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700415 // means the buffer was previously acquired).
Pablo Ceballos47650f42015-08-04 16:38:17 -0700416 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay, item.mCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700417 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800418 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700419 mEglDisplay, slot);
420 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Eric Penner5c3d2432014-07-11 19:08:04 -0700421 mEglDisplay, EGL_NO_SYNC_KHR);
422 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800423 }
424
425 // Do whatever sync ops we need to do before releasing the old slot.
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800426 if (slot != mCurrentTexture) {
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800427 err = syncForReleaseLocked(mEglDisplay);
428 if (err != NO_ERROR) {
429 // Release the buffer we just acquired. It's not safe to
430 // release the old buffer, so instead we just drop the new frame.
431 // As we are still under lock since acquireBuffer, it is safe to
432 // release by slot.
433 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
434 mEglDisplay, EGL_NO_SYNC_KHR);
435 return err;
436 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800437 }
438
Dan Stozad723bd72014-11-18 10:24:03 -0800439 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Eric Penner5c3d2432014-07-11 19:08:04 -0700440 mCurrentTexture, mCurrentTextureImage != NULL ?
441 mCurrentTextureImage->graphicBufferHandle() : 0,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700442 slot, mSlots[slot].mGraphicBuffer->handle);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800443
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700444 // Hang onto the pointer so that it isn't freed in the call to
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700445 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700446 // the same.
447 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
448
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800449 // release old buffer
450 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Dan Stoza3ce46042015-11-17 17:00:45 -0800451 if (pendingRelease == nullptr) {
452 status_t status = releaseBufferLocked(
453 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
454 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
455 if (status < NO_ERROR) {
456 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
457 strerror(-status), status);
458 err = status;
459 // keep going, with error raised [?]
460 }
461 } else {
462 pendingRelease->currentTexture = mCurrentTexture;
463 pendingRelease->graphicBuffer =
464 mCurrentTextureImage->graphicBuffer();
465 pendingRelease->display = mEglDisplay;
466 pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence;
467 pendingRelease->isPending = true;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800468 }
469 }
470
Andy McFadden2adaf042012-12-18 09:49:45 -0800471 // Update the GLConsumer state.
Pablo Ceballos47650f42015-08-04 16:38:17 -0700472 mCurrentTexture = slot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700473 mCurrentTextureImage = nextTextureImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800474 mCurrentCrop = item.mCrop;
475 mCurrentTransform = item.mTransform;
476 mCurrentScalingMode = item.mScalingMode;
477 mCurrentTimestamp = item.mTimestamp;
478 mCurrentFence = item.mFence;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700479 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800480
481 computeCurrentTransformMatrixLocked();
482
483 return err;
484}
485
Andy McFadden2adaf042012-12-18 09:49:45 -0800486status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800487 if (mEglDisplay == EGL_NO_DISPLAY) {
488 ALOGE("bindTextureImage: invalid display");
489 return INVALID_OPERATION;
490 }
491
Dan Stozad723bd72014-11-18 10:24:03 -0800492 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800493 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800494 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800495 }
496
497 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700498 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
499 mCurrentTextureImage == NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800500 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700501 return NO_INIT;
502 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800503
Eric Penner5c3d2432014-07-11 19:08:04 -0700504 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700505 mCurrentCrop);
Eric Penner5c3d2432014-07-11 19:08:04 -0700506 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800507 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700508 mEglDisplay, mCurrentTexture);
509 return UNKNOWN_ERROR;
510 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700511 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
512
Eric Penner2d14a0e2014-08-25 20:16:37 -0700513 // In the rare case that the display is terminated and then initialized
514 // again, we can't detect that the display changed (it didn't), but the
515 // image is invalid. In this case, repeat the exact same steps while
516 // forcing the creation of a new image.
517 if ((error = glGetError()) != GL_NO_ERROR) {
518 glBindTexture(mTexTarget, mTexName);
Dan Stozad723bd72014-11-18 10:24:03 -0800519 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay,
520 mCurrentCrop,
521 true);
522 if (result != NO_ERROR) {
523 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700524 mEglDisplay, mCurrentTexture);
525 return UNKNOWN_ERROR;
526 }
527 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
528 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800529 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700530 return UNKNOWN_ERROR;
531 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800532 }
Andy McFadden97eba892012-12-11 15:21:45 -0800533
534 // Wait for the new buffer to be ready.
535 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800536}
537
Mathias Agopian45155962013-08-08 18:16:21 -0700538status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700539 EGLDisplay dpy = eglGetCurrentDisplay();
540 EGLContext ctx = eglGetCurrentContext();
541
Mathias Agopian45155962013-08-08 18:16:21 -0700542 if (!contextCheck) {
543 // if this is the first time we're called, mEglDisplay/mEglContext have
544 // never been set, so don't error out (below).
545 if (mEglDisplay == EGL_NO_DISPLAY) {
546 mEglDisplay = dpy;
547 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700548 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700549 mEglContext = ctx;
550 }
551 }
552
553 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800554 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700555 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700556 }
557
Mathias Agopian45155962013-08-08 18:16:21 -0700558 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800559 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700560 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700561 }
562
563 mEglDisplay = dpy;
564 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800565 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800566}
567
Jesse Hall13f01cb2013-03-20 11:37:21 -0700568void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
569 if (fence->isValid() &&
570 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700571 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700572 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700573 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800574 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700575 strerror(-err), err);
576 }
Jesse Hallef194142012-06-14 14:45:17 -0700577 }
578}
579
Andy McFadden2adaf042012-12-18 09:49:45 -0800580status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700581 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800582 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700583 Mutex::Autolock lock(mMutex);
584
585 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800586 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700587 return NO_INIT;
588 }
589
590 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800591 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700592 "context");
593 return INVALID_OPERATION;
594 }
595
596 EGLDisplay dpy = eglGetCurrentDisplay();
597 EGLContext ctx = eglGetCurrentContext();
598
599 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800600 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700601 return INVALID_OPERATION;
602 }
603
604 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800605 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700606 return INVALID_OPERATION;
607 }
608
609 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
610 status_t err = syncForReleaseLocked(dpy);
611 if (err != OK) {
612 return err;
613 }
614
615 glDeleteTextures(1, &mTexName);
616 }
617
618 mEglDisplay = EGL_NO_DISPLAY;
619 mEglContext = EGL_NO_CONTEXT;
620 mAttached = false;
621
622 return OK;
623}
624
Mathias Agopian3f844832013-08-07 21:24:32 -0700625status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700626 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800627 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700628 Mutex::Autolock lock(mMutex);
629
630 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800631 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700632 return NO_INIT;
633 }
634
635 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800636 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700637 "context");
638 return INVALID_OPERATION;
639 }
640
641 EGLDisplay dpy = eglGetCurrentDisplay();
642 EGLContext ctx = eglGetCurrentContext();
643
644 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800645 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700646 return INVALID_OPERATION;
647 }
648
649 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800650 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700651 return INVALID_OPERATION;
652 }
653
654 // We need to bind the texture regardless of whether there's a current
655 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700656 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700657
Jamie Gennis74bed552012-03-28 19:05:54 -0700658 mEglDisplay = dpy;
659 mEglContext = ctx;
660 mTexName = tex;
661 mAttached = true;
662
Eric Penner5c3d2432014-07-11 19:08:04 -0700663 if (mCurrentTextureImage != NULL) {
664 // This may wait for a buffer a second time. This is likely required if
665 // this is a different context, since otherwise the wait could be skipped
666 // by bouncing through another context. For the same context the extra
667 // wait is redundant.
668 status_t err = bindTextureImageLocked();
669 if (err != NO_ERROR) {
670 return err;
671 }
672 }
673
Jamie Gennis74bed552012-03-28 19:05:54 -0700674 return OK;
675}
676
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800677
Andy McFadden2adaf042012-12-18 09:49:45 -0800678status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800679 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700680
Jamie Gennis01dbf552012-09-06 14:54:19 -0700681 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700682 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700683 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
684 EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
685 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800686 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700687 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700688 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700689 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700690 glFlush();
691 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700692 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700693 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800694 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700695 "fd: %#x", eglGetError());
696 return UNKNOWN_ERROR;
697 }
698 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700699 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700700 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700701 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800702 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700703 "%s (%d)", strerror(-err), err);
704 return err;
705 }
Mathias Agopianca088332013-03-28 17:44:13 -0700706 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700707 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
708 if (fence != EGL_NO_SYNC_KHR) {
709 // There is already a fence for the current slot. We need to
710 // wait on that before replacing it with another fence to
711 // ensure that all outstanding buffer accesses have completed
712 // before the producer accesses it.
713 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
714 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800715 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700716 "fence: %#x", eglGetError());
717 return UNKNOWN_ERROR;
718 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800719 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700720 "fence");
721 return TIMED_OUT;
722 }
723 eglDestroySyncKHR(dpy, fence);
724 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700725
Jamie Gennis01dbf552012-09-06 14:54:19 -0700726 // Create a fence for the outstanding accesses in the current
727 // OpenGL ES context.
728 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
729 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800730 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700731 eglGetError());
732 return UNKNOWN_ERROR;
733 }
734 glFlush();
735 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700736 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700737 }
738
739 return OK;
740}
741
Dan Stozad723bd72014-11-18 10:24:03 -0800742bool GLConsumer::isExternalFormat(PixelFormat format)
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700743{
744 switch (format) {
745 // supported YUV formats
746 case HAL_PIXEL_FORMAT_YV12:
747 // Legacy/deprecated YUV formats
748 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
749 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
750 case HAL_PIXEL_FORMAT_YCbCr_422_I:
751 return true;
752 }
753
754 // Any OEM format needs to be considered
755 if (format>=0x100 && format<=0x1FF)
756 return true;
757
758 return false;
759}
760
Mathias Agopian3f844832013-08-07 21:24:32 -0700761uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700762 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700763}
764
Andy McFadden2adaf042012-12-18 09:49:45 -0800765void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800766 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700767 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
768}
769
Andy McFadden2adaf042012-12-18 09:49:45 -0800770void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700771 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700772 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800773 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700774 return;
775 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700776 bool needsRecompute = mFilteringEnabled != enabled;
777 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700778
Eric Penner5c3d2432014-07-11 19:08:04 -0700779 if (needsRecompute && mCurrentTextureImage==NULL) {
Dan Stozad723bd72014-11-18 10:24:03 -0800780 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700781 }
782
Eric Penner5c3d2432014-07-11 19:08:04 -0700783 if (needsRecompute && mCurrentTextureImage != NULL) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700784 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700785 }
786}
787
Andy McFadden2adaf042012-12-18 09:49:45 -0800788void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800789 GLC_LOGV("computeCurrentTransformMatrixLocked");
John Reck1a61da52016-04-28 13:18:15 -0700790 sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ?
791 nullptr : mCurrentTextureImage->graphicBuffer();
792 if (buf == nullptr) {
793 GLC_LOGD("computeCurrentTransformMatrixLocked: "
794 "mCurrentTextureImage is NULL");
795 }
796 computeTransformMatrix(mCurrentTransformMatrix, buf,
797 isEglImageCroppable(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop,
798 mCurrentTransform, mFilteringEnabled);
799}
800
801void GLConsumer::computeTransformMatrix(float outTransform[16],
802 const sp<GraphicBuffer>& buf, const Rect& cropRect, uint32_t transform,
803 bool filtering) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800804
Jamie Gennisa214c642011-01-14 13:53:31 -0800805 float xform[16];
806 for (int i = 0; i < 16; i++) {
807 xform[i] = mtxIdentity[i];
808 }
John Reck1a61da52016-04-28 13:18:15 -0700809 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800810 float result[16];
811 mtxMul(result, xform, mtxFlipH);
812 for (int i = 0; i < 16; i++) {
813 xform[i] = result[i];
814 }
815 }
John Reck1a61da52016-04-28 13:18:15 -0700816 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800817 float result[16];
818 mtxMul(result, xform, mtxFlipV);
819 for (int i = 0; i < 16; i++) {
820 xform[i] = result[i];
821 }
822 }
John Reck1a61da52016-04-28 13:18:15 -0700823 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Jamie Gennisa214c642011-01-14 13:53:31 -0800824 float result[16];
825 mtxMul(result, xform, mtxRot90);
826 for (int i = 0; i < 16; i++) {
827 xform[i] = result[i];
828 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800829 }
830
Jamie Gennisdbe92452013-09-23 17:22:10 -0700831 float mtxBeforeFlipV[16];
John Reck1a61da52016-04-28 13:18:15 -0700832 if (!cropRect.isEmpty()) {
Jamie Gennisdbe92452013-09-23 17:22:10 -0700833 float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
834 float bufferWidth = buf->getWidth();
835 float bufferHeight = buf->getHeight();
John Reck1a61da52016-04-28 13:18:15 -0700836 float shrinkAmount = 0.0f;
837 if (filtering) {
838 // In order to prevent bilinear sampling beyond the edge of the
839 // crop rectangle we may need to shrink it by 2 texels in each
840 // dimension. Normally this would just need to take 1/2 a texel
841 // off each end, but because the chroma channels of YUV420 images
842 // are subsampled we may need to shrink the crop region by a whole
843 // texel on each side.
844 switch (buf->getPixelFormat()) {
845 case PIXEL_FORMAT_RGBA_8888:
846 case PIXEL_FORMAT_RGBX_8888:
847 case PIXEL_FORMAT_RGB_888:
848 case PIXEL_FORMAT_RGB_565:
849 case PIXEL_FORMAT_BGRA_8888:
850 // We know there's no subsampling of any channels, so we
851 // only need to shrink by a half a pixel.
852 shrinkAmount = 0.5;
853 break;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700854
John Reck1a61da52016-04-28 13:18:15 -0700855 default:
856 // If we don't recognize the format, we must assume the
857 // worst case (that we care about), which is YUV420.
858 shrinkAmount = 1.0;
859 break;
Jamie Gennisdbe92452013-09-23 17:22:10 -0700860 }
John Reck1a61da52016-04-28 13:18:15 -0700861 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700862
John Reck1a61da52016-04-28 13:18:15 -0700863 // Only shrink the dimensions that are not the size of the buffer.
864 if (cropRect.width() < bufferWidth) {
865 tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
866 sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) /
867 bufferWidth;
868 }
869 if (cropRect.height() < bufferHeight) {
870 ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) /
871 bufferHeight;
872 sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) /
873 bufferHeight;
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700874 }
Jamie Gennisdbe92452013-09-23 17:22:10 -0700875 float crop[16] = {
876 sx, 0, 0, 0,
877 0, sy, 0, 0,
878 0, 0, 1, 0,
879 tx, ty, 0, 1,
880 };
Jamie Gennisd72f2332012-05-07 13:50:11 -0700881
Jamie Gennisdbe92452013-09-23 17:22:10 -0700882 mtxMul(mtxBeforeFlipV, crop, xform);
883 } else {
884 for (int i = 0; i < 16; i++) {
885 mtxBeforeFlipV[i] = xform[i];
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700886 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800887 }
Jamie Gennisa214c642011-01-14 13:53:31 -0800888
889 // SurfaceFlinger expects the top of its window textures to be at a Y
Andy McFadden2adaf042012-12-18 09:49:45 -0800890 // coordinate of 0, so GLConsumer must behave the same way. We don't
Jamie Gennisa214c642011-01-14 13:53:31 -0800891 // want to expose this to applications, however, so we must add an
892 // additional vertical flip to the transform after all the other transforms.
John Reck1a61da52016-04-28 13:18:15 -0700893 mtxMul(outTransform, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800894}
895
Andy McFadden2adaf042012-12-18 09:49:45 -0800896nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800897 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800898 Mutex::Autolock lock(mMutex);
899 return mCurrentTimestamp;
900}
901
Dan Stozad723bd72014-11-18 10:24:03 -0800902uint64_t GLConsumer::getFrameNumber() {
903 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700904 Mutex::Autolock lock(mMutex);
905 return mCurrentFrameNumber;
906}
907
Andy McFadden2adaf042012-12-18 09:49:45 -0800908sp<GraphicBuffer> GLConsumer::getCurrentBuffer() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700909 Mutex::Autolock lock(mMutex);
Eric Penner5c3d2432014-07-11 19:08:04 -0700910 return (mCurrentTextureImage == NULL) ?
911 NULL : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700912}
913
Andy McFadden2adaf042012-12-18 09:49:45 -0800914Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700915 Mutex::Autolock lock(mMutex);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700916
917 Rect outCrop = mCurrentCrop;
918 if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
Dan Stozad723bd72014-11-18 10:24:03 -0800919 uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
920 uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700921
922 if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
923 newWidth = newHeight * mDefaultWidth / mDefaultHeight;
Dan Stozad723bd72014-11-18 10:24:03 -0800924 GLC_LOGV("too wide: newWidth = %d", newWidth);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700925 } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
926 newHeight = newWidth * mDefaultHeight / mDefaultWidth;
Dan Stozad723bd72014-11-18 10:24:03 -0800927 GLC_LOGV("too tall: newHeight = %d", newHeight);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700928 }
929
Dan Stozad723bd72014-11-18 10:24:03 -0800930 uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
931 uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());
932
Daniel Lam016c8cb2012-04-03 15:54:58 -0700933 // The crop is too wide
Dan Stozad723bd72014-11-18 10:24:03 -0800934 if (newWidth < currentWidth) {
Dan Stozaec4cb382015-06-09 15:05:23 -0700935 uint32_t dw = currentWidth - newWidth;
936 auto halfdw = dw / 2;
937 outCrop.left += halfdw;
938 // Not halfdw because it would subtract 1 too few when dw is odd
939 outCrop.right -= (dw - halfdw);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700940 // The crop is too tall
Dan Stozad723bd72014-11-18 10:24:03 -0800941 } else if (newHeight < currentHeight) {
Dan Stozaec4cb382015-06-09 15:05:23 -0700942 uint32_t dh = currentHeight - newHeight;
943 auto halfdh = dh / 2;
944 outCrop.top += halfdh;
945 // Not halfdh because it would subtract 1 too few when dh is odd
946 outCrop.bottom -= (dh - halfdh);
Daniel Lam016c8cb2012-04-03 15:54:58 -0700947 }
948
Dan Stozad723bd72014-11-18 10:24:03 -0800949 GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
Daniel Lam016c8cb2012-04-03 15:54:58 -0700950 outCrop.left, outCrop.top,
951 outCrop.right,outCrop.bottom);
952 }
953
Daniel Lam016c8cb2012-04-03 15:54:58 -0700954 return outCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700955}
956
Andy McFadden2adaf042012-12-18 09:49:45 -0800957uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700958 Mutex::Autolock lock(mMutex);
959 return mCurrentTransform;
960}
961
Andy McFadden2adaf042012-12-18 09:49:45 -0800962uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700963 Mutex::Autolock lock(mMutex);
964 return mCurrentScalingMode;
965}
966
Andy McFadden2adaf042012-12-18 09:49:45 -0800967sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700968 Mutex::Autolock lock(mMutex);
969 return mCurrentFence;
970}
971
Andy McFadden2adaf042012-12-18 09:49:45 -0800972status_t GLConsumer::doGLFenceWait() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700973 Mutex::Autolock lock(mMutex);
Jamie Gennis3941cb22012-09-17 16:58:17 -0700974 return doGLFenceWaitLocked();
975}
976
Andy McFadden2adaf042012-12-18 09:49:45 -0800977status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700978
979 EGLDisplay dpy = eglGetCurrentDisplay();
980 EGLContext ctx = eglGetCurrentContext();
981
982 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800983 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700984 return INVALID_OPERATION;
985 }
986
987 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800988 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700989 return INVALID_OPERATION;
990 }
991
Jamie Gennis1df8c342012-12-20 14:05:45 -0800992 if (mCurrentFence->isValid()) {
Mathias Agopianca088332013-03-28 17:44:13 -0700993 if (SyncFeatures::getInstance().useWaitSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700994 // Create an EGLSyncKHR from the current fence.
995 int fenceFd = mCurrentFence->dup();
996 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800997 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700998 return -errno;
999 }
1000 EGLint attribs[] = {
1001 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
1002 EGL_NONE
1003 };
1004 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
1005 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
1006 if (sync == EGL_NO_SYNC_KHR) {
1007 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -08001008 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001009 eglGetError());
1010 return UNKNOWN_ERROR;
1011 }
1012
1013 // XXX: The spec draft is inconsistent as to whether this should
1014 // return an EGLint or void. Ignore the return value for now, as
1015 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -07001016 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001017 EGLint eglErr = eglGetError();
1018 eglDestroySyncKHR(dpy, sync);
1019 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -08001020 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -07001021 eglErr);
1022 return UNKNOWN_ERROR;
1023 }
1024 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -07001025 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -08001026 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -07001027 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -08001028 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -07001029 return err;
1030 }
1031 }
1032 }
1033
1034 return NO_ERROR;
1035}
1036
Andy McFadden2adaf042012-12-18 09:49:45 -08001037void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -08001038 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001039 if (slotIndex == mCurrentTexture) {
1040 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
1041 }
Eric Penner5c3d2432014-07-11 19:08:04 -07001042 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001043 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001044}
Daniel Lameae59d22012-01-22 15:26:27 -08001045
Andy McFadden2adaf042012-12-18 09:49:45 -08001046void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -08001047 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -07001048 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -07001049 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001050}
1051
Andy McFadden2adaf042012-12-18 09:49:45 -08001052void GLConsumer::setName(const String8& name) {
Daniel Lameae59d22012-01-22 15:26:27 -08001053 Mutex::Autolock _l(mMutex);
Jamie Gennisfa28c352011-09-16 17:30:26 -07001054 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001055 mConsumer->setConsumerName(name);
Daniel Lamb2675792012-02-23 14:35:13 -08001056}
1057
Dan Stozad723bd72014-11-18 10:24:03 -08001058status_t GLConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
Daniel Lamb2675792012-02-23 14:35:13 -08001059 Mutex::Autolock lock(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001060 return mConsumer->setDefaultBufferFormat(defaultFormat);
Daniel Lamb2675792012-02-23 14:35:13 -08001061}
1062
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -08001063status_t GLConsumer::setDefaultBufferDataSpace(
1064 android_dataspace defaultDataSpace) {
1065 Mutex::Autolock lock(mMutex);
1066 return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
1067}
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
Pablo Ceballos19e3e062015-08-19 16:16:06 -07001080status_t GLConsumer::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
1081 Mutex::Autolock lock(mMutex);
1082 return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
1083}
1084
Mathias Agopian74d211a2013-04-22 16:55:35 +02001085void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -07001086{
Mathias Agopian74d211a2013-04-22 16:55:35 +02001087 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -07001088 "%smTexName=%d mCurrentTexture=%d\n"
1089 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
1090 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
1091 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
1092 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -07001093
Mathias Agopian74d211a2013-04-22 16:55:35 +02001094 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -07001095}
1096
Jamie Gennisf238e282011-01-09 16:33:17 -08001097static void mtxMul(float out[16], const float a[16], const float b[16]) {
1098 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
1099 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
1100 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
1101 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1102
1103 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1104 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1105 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1106 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1107
1108 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1109 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1110 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1111 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1112
1113 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1114 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1115 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1116 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1117}
1118
Eric Penner5c3d2432014-07-11 19:08:04 -07001119GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
1120 mGraphicBuffer(graphicBuffer),
1121 mEglImage(EGL_NO_IMAGE_KHR),
Pablo Ceballos60d69222015-08-07 14:47:20 -07001122 mEglDisplay(EGL_NO_DISPLAY),
1123 mCropRect(Rect::EMPTY_RECT) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001124}
1125
1126GLConsumer::EglImage::~EglImage() {
1127 if (mEglImage != EGL_NO_IMAGE_KHR) {
1128 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1129 ALOGE("~EglImage: eglDestroyImageKHR failed");
1130 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001131 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001132 }
1133}
1134
1135status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001136 const Rect& cropRect,
1137 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001138 // If there's an image and it's no longer valid, destroy it.
1139 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1140 bool displayInvalid = mEglDisplay != eglDisplay;
1141 bool cropInvalid = hasEglAndroidImageCrop() && mCropRect != cropRect;
Eric Penner2d14a0e2014-08-25 20:16:37 -07001142 if (haveImage && (displayInvalid || cropInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001143 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1144 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1145 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001146 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001147 mEglImage = EGL_NO_IMAGE_KHR;
1148 mEglDisplay = EGL_NO_DISPLAY;
1149 }
1150
1151 // If there's no image, create one.
1152 if (mEglImage == EGL_NO_IMAGE_KHR) {
1153 mEglDisplay = eglDisplay;
1154 mCropRect = cropRect;
1155 mEglImage = createImage(mEglDisplay, mGraphicBuffer, mCropRect);
1156 }
1157
1158 // Fail if we can't create a valid image.
1159 if (mEglImage == EGL_NO_IMAGE_KHR) {
1160 mEglDisplay = EGL_NO_DISPLAY;
1161 mCropRect.makeInvalid();
1162 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
1163 ALOGE("Failed to create image. size=%ux%u st=%u usage=0x%x fmt=%d",
1164 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1165 buffer->getUsage(), buffer->getPixelFormat());
1166 return UNKNOWN_ERROR;
1167 }
1168
1169 return OK;
1170}
1171
1172void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001173 glEGLImageTargetTexture2DOES(texTarget,
1174 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001175}
1176
1177EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
1178 const sp<GraphicBuffer>& graphicBuffer, const Rect& crop) {
Dan Stozad723bd72014-11-18 10:24:03 -08001179 EGLClientBuffer cbuf =
1180 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Eric Penner5c3d2432014-07-11 19:08:04 -07001181 EGLint attrs[] = {
1182 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
1183 EGL_IMAGE_CROP_LEFT_ANDROID, crop.left,
1184 EGL_IMAGE_CROP_TOP_ANDROID, crop.top,
1185 EGL_IMAGE_CROP_RIGHT_ANDROID, crop.right,
1186 EGL_IMAGE_CROP_BOTTOM_ANDROID, crop.bottom,
1187 EGL_NONE,
1188 };
1189 if (!crop.isValid()) {
1190 // No crop rect to set, so terminate the attrib array before the crop.
1191 attrs[2] = EGL_NONE;
1192 } else if (!isEglImageCroppable(crop)) {
1193 // The crop rect is not at the origin, so we can't set the crop on the
1194 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
1195 // extension. In the future we can add a layered extension that
1196 // removes this restriction if there is hardware that can support it.
1197 attrs[2] = EGL_NONE;
1198 }
Michael Lentine78be65e2014-10-02 12:10:07 -07001199 eglInitialize(dpy, 0, 0);
Eric Penner5c3d2432014-07-11 19:08:04 -07001200 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1201 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1202 if (image == EGL_NO_IMAGE_KHR) {
1203 EGLint error = eglGetError();
1204 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001205 eglTerminate(dpy);
Eric Penner5c3d2432014-07-11 19:08:04 -07001206 }
1207 return image;
1208}
1209
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001210}; // namespace android