blob: b3647d6126b3788217b6c2b82d5ee965e67af9c3 [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
Mathias Agopiancb496ac2017-05-22 14:21:00 -070024#include <inttypes.h>
25
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080026#include <EGL/egl.h>
27#include <EGL/eglext.h>
28#include <GLES2/gl2.h>
29#include <GLES2/gl2ext.h>
Mathias Agopianad678e12013-07-23 17:28:53 -070030#include <cutils/compiler.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080031
Jamie Gennis9fea3422012-08-07 18:03:04 -070032#include <hardware/hardware.h>
33
Chia-I Wu3bd03ff2017-11-17 12:04:57 -080034#include <math/mat4.h>
35
Dan Stozadd264162015-03-12 13:58:47 -070036#include <gui/BufferItem.h>
Ady Abrahama3b08ef2019-07-15 18:43:10 -070037#include <gui/DebugEGLImageTracker.h>
Mathias Agopianca088332013-03-28 17:44:13 -070038#include <gui/GLConsumer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080039#include <gui/ISurfaceComposer.h>
40#include <gui/SurfaceComposerClient.h>
Mathias Agopian41f673c2011-11-17 17:48:35 -080041
Mathias Agopian90ac7992012-02-25 18:48:35 -080042#include <private/gui/ComposerService.h>
Mathias Agopianca088332013-03-28 17:44:13 -070043#include <private/gui/SyncFeatures.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080044
45#include <utils/Log.h>
Mathias Agopian68c77942011-05-09 19:08:33 -070046#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080047#include <utils/Trace.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080048
Craig Donneraec86972016-04-28 18:09:40 -070049#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
50#define EGL_PROTECTED_CONTENT_EXT 0x32C0
Jamie Gennisdbe92452013-09-23 17:22:10 -070051
Andy McFadden97eba892012-12-11 15:21:45 -080052namespace android {
53
Andy McFadden2adaf042012-12-18 09:49:45 -080054// Macros for including the GLConsumer name in log messages
Dan Stozad723bd72014-11-18 10:24:03 -080055#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
56#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
57//#define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
58#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
59#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Mathias Agopian29b57622011-08-17 15:42:04 -070060
Mathias Agopianad678e12013-07-23 17:28:53 -070061static const struct {
Dan Stozad723bd72014-11-18 10:24:03 -080062 uint32_t width, height;
Mathias Agopianad678e12013-07-23 17:28:53 -070063 char const* bits;
Mathias Agopian45263e22013-08-07 13:35:20 -070064} kDebugData = { 15, 12,
Dan Stozad723bd72014-11-18 10:24:03 -080065 "_______________"
66 "_______________"
67 "_____XX_XX_____"
68 "__X_X_____X_X__"
69 "__X_XXXXXXX_X__"
70 "__XXXXXXXXXXX__"
71 "___XX_XXX_XX___"
72 "____XXXXXXX____"
73 "_____X___X_____"
74 "____X_____X____"
75 "_______________"
76 "_______________"
Mathias Agopian45263e22013-08-07 13:35:20 -070077};
Mathias Agopianad678e12013-07-23 17:28:53 -070078
Chia-I Wu3bd03ff2017-11-17 12:04:57 -080079static const mat4 mtxIdentity;
Jamie Gennisf238e282011-01-09 16:33:17 -080080
Mathias Agopian9870c9b2013-08-08 17:46:48 -070081Mutex GLConsumer::sStaticInitLock;
82sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer;
Jamie Gennisfa28c352011-09-16 17:30:26 -070083
Craig Donneraec86972016-04-28 18:09:40 -070084static bool hasEglProtectedContentImpl() {
85 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
86 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
87 size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
88 size_t extsLen = strlen(exts);
89 bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
90 bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1);
91 bool atEnd = (cropExtLen+1) < extsLen &&
92 !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen+1));
93 bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
Craig Donner4df76b22016-06-13 22:14:15 +000094 return equal || atStart || atEnd || inMiddle;
Craig Donneraec86972016-04-28 18:09:40 -070095}
96
97static bool hasEglProtectedContent() {
98 // Only compute whether the extension is present once the first time this
99 // function is called.
100 static bool hasIt = hasEglProtectedContentImpl();
101 return hasIt;
102}
103
Mathias Agopian3f844832013-08-07 21:24:32 -0700104GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
105 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700106 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700107 mCurrentCrop(Rect::EMPTY_RECT),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800108 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200109 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800110 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800111 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700112 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700113 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200114 mDefaultWidth(1),
115 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700116 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700117 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800118 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800119 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700120 mEglDisplay(EGL_NO_DISPLAY),
121 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700122 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
123 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800124{
Dan Stozad723bd72014-11-18 10:24:03 -0800125 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800126
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800127 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Jamie Gennisfa28c352011-09-16 17:30:26 -0700128 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700129
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700130 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800131}
132
Dan Stozaab574912014-06-25 14:21:45 -0700133GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
134 bool useFenceSync, bool isControlledByApp) :
135 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700136 mCurrentCrop(Rect::EMPTY_RECT),
Dan Stozaab574912014-06-25 14:21:45 -0700137 mCurrentTransform(0),
138 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
139 mCurrentFence(Fence::NO_FENCE),
140 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700141 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Dan Stozaab574912014-06-25 14:21:45 -0700142 mCurrentFrameNumber(0),
143 mDefaultWidth(1),
144 mDefaultHeight(1),
145 mFilteringEnabled(true),
Dan Stozad723bd72014-11-18 10:24:03 -0800146 mTexName(0),
Dan Stozaab574912014-06-25 14:21:45 -0700147 mUseFenceSync(useFenceSync),
148 mTexTarget(texTarget),
149 mEglDisplay(EGL_NO_DISPLAY),
150 mEglContext(EGL_NO_CONTEXT),
151 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
152 mAttached(false)
153{
Dan Stozad723bd72014-11-18 10:24:03 -0800154 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700155
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800156 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Dan Stozaab574912014-06-25 14:21:45 -0700157 sizeof(mCurrentTransformMatrix));
158
159 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
160}
161
Andy McFadden2adaf042012-12-18 09:49:45 -0800162status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700163{
Daniel Lamb2675792012-02-23 14:35:13 -0800164 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700165 if (mAbandoned) {
166 GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!");
167 return NO_INIT;
168 }
Daniel Lam016c8cb2012-04-03 15:54:58 -0700169 mDefaultWidth = w;
170 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700171 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700172}
173
Andy McFadden2adaf042012-12-18 09:49:45 -0800174status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800175 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800176 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800177 Mutex::Autolock lock(mMutex);
178
179 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800180 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800181 return NO_INIT;
182 }
183
184 // Make sure the EGL state is the same as in previous calls.
185 status_t err = checkAndUpdateEglStateLocked();
186 if (err != NO_ERROR) {
187 return err;
188 }
189
Dan Stozadd264162015-03-12 13:58:47 -0700190 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800191
192 // Acquire the next buffer.
193 // In asynchronous mode the list is guaranteed to be one buffer
194 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700195 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800196 if (err != NO_ERROR) {
197 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
198 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800199 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800200 glBindTexture(mTexTarget, mTexName);
201 err = NO_ERROR;
202 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800203 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800204 strerror(-err), err);
205 }
206 return err;
207 }
208
209 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700210 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800211 if (err != NO_ERROR) {
212 // We always bind the texture.
213 glBindTexture(mTexTarget, mTexName);
214 return err;
215 }
216
Andy McFadden97eba892012-12-11 15:21:45 -0800217 // Bind the new buffer to the GL texture, and wait until it's ready.
218 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700219}
220
Mathias Agopianad678e12013-07-23 17:28:53 -0700221
222status_t GLConsumer::releaseTexImage() {
223 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800224 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700225 Mutex::Autolock lock(mMutex);
226
227 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800228 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700229 return NO_INIT;
230 }
231
232 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700233 status_t err = NO_ERROR;
234
235 if (mAttached) {
236 err = checkAndUpdateEglStateLocked(true);
237 if (err != NO_ERROR) {
238 return err;
239 }
240 } else {
241 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700242 }
243
244 // Update the GLConsumer state.
245 int buf = mCurrentTexture;
246 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
247
Dan Stozad723bd72014-11-18 10:24:03 -0800248 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700249
Mathias Agopian45155962013-08-08 18:16:21 -0700250 if (mAttached) {
251 // Do whatever sync ops we need to do before releasing the slot.
252 err = syncForReleaseLocked(mEglDisplay);
253 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800254 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700255 return err;
256 }
257 } else {
258 // if we're detached, we just use the fence that was created in detachFromContext()
259 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700260 }
261
Mathias Agopian45155962013-08-08 18:16:21 -0700262 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
Mathias Agopianad678e12013-07-23 17:28:53 -0700263 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800264 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700265 strerror(-err), err);
266 return err;
267 }
268
Yi Kong48a619f2018-06-05 16:34:59 -0700269 if (mReleasedTexImage == nullptr) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700270 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
271 }
272
Mathias Agopianad678e12013-07-23 17:28:53 -0700273 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700274 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700275 mCurrentCrop.makeInvalid();
276 mCurrentTransform = 0;
Mathias Agopianad678e12013-07-23 17:28:53 -0700277 mCurrentTimestamp = 0;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700278 mCurrentDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopianad678e12013-07-23 17:28:53 -0700279 mCurrentFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700280 mCurrentFenceTime = FenceTime::NO_FENCE;
Mathias Agopianad678e12013-07-23 17:28:53 -0700281
Mathias Agopian45155962013-08-08 18:16:21 -0700282 if (mAttached) {
Xin Lie8b4e702020-08-29 01:34:09 -0700283 // This binds a buffer placeholder (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800284 status_t result = bindTextureImageLocked();
285 if (result != NO_ERROR) {
286 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700287 }
Mathias Agopian45155962013-08-08 18:16:21 -0700288 } else {
289 // detached, don't touch the texture (and we may not even have an
290 // EGLDisplay here.
291 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700292 }
293
294 return NO_ERROR;
295}
296
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700297sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
298 Mutex::Autolock _l(sStaticInitLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700299 if (CC_UNLIKELY(sReleasedTexImageBuffer == nullptr)) {
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700300 // The first time, create the debug texture in case the application
301 // continues to use it.
302 sp<GraphicBuffer> buffer = new GraphicBuffer(
303 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
SOOJONG HWANG0babcd32021-10-01 00:36:06 +0900304 DEFAULT_USAGE_FLAGS | GraphicBuffer::USAGE_SW_WRITE_RARELY,
Dan Stozad4079af2016-08-22 17:26:41 -0700305 "[GLConsumer debug texture]");
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700306 uint32_t* bits;
307 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800308 uint32_t stride = buffer->getStride();
309 uint32_t height = buffer->getHeight();
310 memset(bits, 0, stride * height * 4);
311 for (uint32_t y = 0; y < kDebugData.height; y++) {
312 for (uint32_t x = 0; x < kDebugData.width; x++) {
313 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
314 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700315 }
Dan Stozad723bd72014-11-18 10:24:03 -0800316 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700317 }
318 buffer->unlock();
319 sReleasedTexImageBuffer = buffer;
320 }
321 return sReleasedTexImageBuffer;
322}
323
Dan Stozadd264162015-03-12 13:58:47 -0700324status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700325 nsecs_t presentWhen, uint64_t maxFrameNumber) {
326 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
327 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700328 if (err != NO_ERROR) {
329 return err;
330 }
331
Eric Penner5c3d2432014-07-11 19:08:04 -0700332 // If item->mGraphicBuffer is not null, this buffer has not been acquired
333 // before, so any prior EglImage created is using a stale buffer. This
334 // replaces any old EglImage with a new one (using the new buffer).
Yi Kong48a619f2018-06-05 16:34:59 -0700335 if (item->mGraphicBuffer != nullptr) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700336 int slot = item->mSlot;
Eric Penner5c3d2432014-07-11 19:08:04 -0700337 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700338 }
339
Jamie Gennis9fea3422012-08-07 18:03:04 -0700340 return NO_ERROR;
341}
342
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700343status_t GLConsumer::releaseBufferLocked(int buf,
344 sp<GraphicBuffer> graphicBuffer,
345 EGLDisplay display, EGLSyncKHR eglFence) {
346 // release the buffer if it hasn't already been discarded by the
347 // BufferQueue. This can happen, for example, when the producer of this
348 // buffer has reallocated the original buffer slot after this buffer
349 // was acquired.
350 status_t err = ConsumerBase::releaseBufferLocked(
351 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700352 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700353 return err;
354}
355
Dan Stoza3ce46042015-11-17 17:00:45 -0800356status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item,
357 PendingRelease* pendingRelease)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800358{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700359 status_t err = NO_ERROR;
360
Pablo Ceballos47650f42015-08-04 16:38:17 -0700361 int slot = item.mSlot;
Andy McFadden87a67842014-04-04 15:37:08 -0700362
Jamie Gennis74bed552012-03-28 19:05:54 -0700363 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800364 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700365 "ES context");
Pablo Ceballos47650f42015-08-04 16:38:17 -0700366 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700367 mEglDisplay, EGL_NO_SYNC_KHR);
Jamie Gennis74bed552012-03-28 19:05:54 -0700368 return INVALID_OPERATION;
369 }
370
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800371 // Confirm state.
372 err = checkAndUpdateEglStateLocked();
373 if (err != NO_ERROR) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700374 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Andy McFadden87a67842014-04-04 15:37:08 -0700375 mEglDisplay, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800376 return err;
377 }
378
Eric Penner5c3d2432014-07-11 19:08:04 -0700379 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
380 // if nessessary, for the gralloc buffer currently in the slot in
381 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800382 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700383 // means the buffer was previously acquired).
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700384 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -0700385 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800386 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700387 mEglDisplay, slot);
388 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
Eric Penner5c3d2432014-07-11 19:08:04 -0700389 mEglDisplay, EGL_NO_SYNC_KHR);
390 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800391 }
392
393 // Do whatever sync ops we need to do before releasing the old slot.
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800394 if (slot != mCurrentTexture) {
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800395 err = syncForReleaseLocked(mEglDisplay);
396 if (err != NO_ERROR) {
397 // Release the buffer we just acquired. It's not safe to
398 // release the old buffer, so instead we just drop the new frame.
399 // As we are still under lock since acquireBuffer, it is safe to
400 // release by slot.
401 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer,
402 mEglDisplay, EGL_NO_SYNC_KHR);
403 return err;
404 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800405 }
406
Dan Stozad723bd72014-11-18 10:24:03 -0800407 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Yi Konga03e0442018-07-17 11:16:57 -0700408 mCurrentTexture, mCurrentTextureImage != nullptr ?
Yi Kong48a619f2018-06-05 16:34:59 -0700409 mCurrentTextureImage->graphicBufferHandle() : nullptr,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700410 slot, mSlots[slot].mGraphicBuffer->handle);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800411
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700412 // Hang onto the pointer so that it isn't freed in the call to
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700413 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700414 // the same.
415 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
416
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800417 // release old buffer
418 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Dan Stoza3ce46042015-11-17 17:00:45 -0800419 if (pendingRelease == nullptr) {
420 status_t status = releaseBufferLocked(
421 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
422 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
423 if (status < NO_ERROR) {
424 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
425 strerror(-status), status);
426 err = status;
427 // keep going, with error raised [?]
428 }
429 } else {
430 pendingRelease->currentTexture = mCurrentTexture;
431 pendingRelease->graphicBuffer =
432 mCurrentTextureImage->graphicBuffer();
433 pendingRelease->display = mEglDisplay;
434 pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence;
435 pendingRelease->isPending = true;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800436 }
437 }
438
Andy McFadden2adaf042012-12-18 09:49:45 -0800439 // Update the GLConsumer state.
Pablo Ceballos47650f42015-08-04 16:38:17 -0700440 mCurrentTexture = slot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700441 mCurrentTextureImage = nextTextureImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800442 mCurrentCrop = item.mCrop;
443 mCurrentTransform = item.mTransform;
444 mCurrentScalingMode = item.mScalingMode;
445 mCurrentTimestamp = item.mTimestamp;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700446 mCurrentDataSpace = item.mDataSpace;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800447 mCurrentFence = item.mFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700448 mCurrentFenceTime = item.mFenceTime;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700449 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800450
451 computeCurrentTransformMatrixLocked();
452
453 return err;
454}
455
Andy McFadden2adaf042012-12-18 09:49:45 -0800456status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800457 if (mEglDisplay == EGL_NO_DISPLAY) {
458 ALOGE("bindTextureImage: invalid display");
459 return INVALID_OPERATION;
460 }
461
Dan Stozad723bd72014-11-18 10:24:03 -0800462 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800463 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800464 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800465 }
466
467 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700468 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
Yi Kong48a619f2018-06-05 16:34:59 -0700469 mCurrentTextureImage == nullptr) {
Dan Stozad723bd72014-11-18 10:24:03 -0800470 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700471 return NO_INIT;
472 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800473
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700474 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -0700475 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800476 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700477 mEglDisplay, mCurrentTexture);
478 return UNKNOWN_ERROR;
479 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700480 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
481
Eric Penner2d14a0e2014-08-25 20:16:37 -0700482 // In the rare case that the display is terminated and then initialized
483 // again, we can't detect that the display changed (it didn't), but the
484 // image is invalid. In this case, repeat the exact same steps while
485 // forcing the creation of a new image.
486 if ((error = glGetError()) != GL_NO_ERROR) {
487 glBindTexture(mTexTarget, mTexName);
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700488 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay, true);
Dan Stozad723bd72014-11-18 10:24:03 -0800489 if (result != NO_ERROR) {
490 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700491 mEglDisplay, mCurrentTexture);
492 return UNKNOWN_ERROR;
493 }
494 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
495 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800496 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700497 return UNKNOWN_ERROR;
498 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800499 }
Andy McFadden97eba892012-12-11 15:21:45 -0800500
501 // Wait for the new buffer to be ready.
502 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800503}
504
Mathias Agopian45155962013-08-08 18:16:21 -0700505status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700506 EGLDisplay dpy = eglGetCurrentDisplay();
507 EGLContext ctx = eglGetCurrentContext();
508
Mathias Agopian45155962013-08-08 18:16:21 -0700509 if (!contextCheck) {
510 // if this is the first time we're called, mEglDisplay/mEglContext have
511 // never been set, so don't error out (below).
512 if (mEglDisplay == EGL_NO_DISPLAY) {
513 mEglDisplay = dpy;
514 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700515 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700516 mEglContext = ctx;
517 }
518 }
519
520 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800521 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700522 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700523 }
524
Mathias Agopian45155962013-08-08 18:16:21 -0700525 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800526 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700527 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700528 }
529
530 mEglDisplay = dpy;
531 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800532 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800533}
534
Jesse Hall13f01cb2013-03-20 11:37:21 -0700535void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
536 if (fence->isValid() &&
537 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700538 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700539 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700540 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800541 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700542 strerror(-err), err);
543 }
Jesse Hallef194142012-06-14 14:45:17 -0700544 }
545}
546
Andy McFadden2adaf042012-12-18 09:49:45 -0800547status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700548 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800549 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700550 Mutex::Autolock lock(mMutex);
551
552 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800553 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700554 return NO_INIT;
555 }
556
557 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800558 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700559 "context");
560 return INVALID_OPERATION;
561 }
562
563 EGLDisplay dpy = eglGetCurrentDisplay();
564 EGLContext ctx = eglGetCurrentContext();
565
566 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800567 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700568 return INVALID_OPERATION;
569 }
570
571 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800572 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700573 return INVALID_OPERATION;
574 }
575
576 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
577 status_t err = syncForReleaseLocked(dpy);
578 if (err != OK) {
579 return err;
580 }
581
582 glDeleteTextures(1, &mTexName);
583 }
584
585 mEglDisplay = EGL_NO_DISPLAY;
586 mEglContext = EGL_NO_CONTEXT;
587 mAttached = false;
588
589 return OK;
590}
591
Mathias Agopian3f844832013-08-07 21:24:32 -0700592status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700593 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800594 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700595 Mutex::Autolock lock(mMutex);
596
597 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800598 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700599 return NO_INIT;
600 }
601
602 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800603 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700604 "context");
605 return INVALID_OPERATION;
606 }
607
608 EGLDisplay dpy = eglGetCurrentDisplay();
609 EGLContext ctx = eglGetCurrentContext();
610
611 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800612 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700613 return INVALID_OPERATION;
614 }
615
616 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800617 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700618 return INVALID_OPERATION;
619 }
620
621 // We need to bind the texture regardless of whether there's a current
622 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700623 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700624
Jamie Gennis74bed552012-03-28 19:05:54 -0700625 mEglDisplay = dpy;
626 mEglContext = ctx;
627 mTexName = tex;
628 mAttached = true;
629
Yi Kong48a619f2018-06-05 16:34:59 -0700630 if (mCurrentTextureImage != nullptr) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700631 // This may wait for a buffer a second time. This is likely required if
632 // this is a different context, since otherwise the wait could be skipped
633 // by bouncing through another context. For the same context the extra
634 // wait is redundant.
635 status_t err = bindTextureImageLocked();
636 if (err != NO_ERROR) {
637 return err;
638 }
639 }
640
Jamie Gennis74bed552012-03-28 19:05:54 -0700641 return OK;
642}
643
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800644
Andy McFadden2adaf042012-12-18 09:49:45 -0800645status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800646 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700647
Jamie Gennis01dbf552012-09-06 14:54:19 -0700648 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700649 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700650 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
Yi Kong48a619f2018-06-05 16:34:59 -0700651 EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700652 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800653 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700654 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700655 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700656 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700657 glFlush();
658 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700659 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700660 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800661 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700662 "fd: %#x", eglGetError());
663 return UNKNOWN_ERROR;
664 }
665 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700666 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700667 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700668 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800669 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700670 "%s (%d)", strerror(-err), err);
671 return err;
672 }
Mathias Agopianca088332013-03-28 17:44:13 -0700673 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700674 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
675 if (fence != EGL_NO_SYNC_KHR) {
676 // There is already a fence for the current slot. We need to
677 // wait on that before replacing it with another fence to
678 // ensure that all outstanding buffer accesses have completed
679 // before the producer accesses it.
680 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
681 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800682 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700683 "fence: %#x", eglGetError());
684 return UNKNOWN_ERROR;
685 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800686 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700687 "fence");
688 return TIMED_OUT;
689 }
690 eglDestroySyncKHR(dpy, fence);
691 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700692
Jamie Gennis01dbf552012-09-06 14:54:19 -0700693 // Create a fence for the outstanding accesses in the current
694 // OpenGL ES context.
Yi Kong48a619f2018-06-05 16:34:59 -0700695 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700696 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800697 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700698 eglGetError());
699 return UNKNOWN_ERROR;
700 }
701 glFlush();
702 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700703 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700704 }
705
706 return OK;
707}
708
Mathias Agopian3f844832013-08-07 21:24:32 -0700709uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700710 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700711}
712
Andy McFadden2adaf042012-12-18 09:49:45 -0800713void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800714 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700715 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
716}
717
Andy McFadden2adaf042012-12-18 09:49:45 -0800718void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700719 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700720 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800721 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700722 return;
723 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700724 bool needsRecompute = mFilteringEnabled != enabled;
725 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700726
Yi Kong48a619f2018-06-05 16:34:59 -0700727 if (needsRecompute && mCurrentTextureImage==nullptr) {
Dan Stozad723bd72014-11-18 10:24:03 -0800728 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700729 }
730
Yi Kong48a619f2018-06-05 16:34:59 -0700731 if (needsRecompute && mCurrentTextureImage != nullptr) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700732 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700733 }
734}
735
Andy McFadden2adaf042012-12-18 09:49:45 -0800736void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800737 GLC_LOGV("computeCurrentTransformMatrixLocked");
John Reck1a61da52016-04-28 13:18:15 -0700738 sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ?
739 nullptr : mCurrentTextureImage->graphicBuffer();
740 if (buf == nullptr) {
741 GLC_LOGD("computeCurrentTransformMatrixLocked: "
742 "mCurrentTextureImage is NULL");
743 }
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700744 computeTransformMatrix(mCurrentTransformMatrix, buf, mCurrentCrop,
John Reck1a61da52016-04-28 13:18:15 -0700745 mCurrentTransform, mFilteringEnabled);
746}
747
Chia-I Wu93b122f2017-12-01 08:51:39 -0800748Rect GLConsumer::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight) {
749 Rect outCrop = crop;
750
751 uint32_t newWidth = static_cast<uint32_t>(crop.width());
752 uint32_t newHeight = static_cast<uint32_t>(crop.height());
753
754 if (newWidth * bufferHeight > newHeight * bufferWidth) {
755 newWidth = newHeight * bufferWidth / bufferHeight;
756 ALOGV("too wide: newWidth = %d", newWidth);
757 } else if (newWidth * bufferHeight < newHeight * bufferWidth) {
758 newHeight = newWidth * bufferHeight / bufferWidth;
759 ALOGV("too tall: newHeight = %d", newHeight);
760 }
761
762 uint32_t currentWidth = static_cast<uint32_t>(crop.width());
763 uint32_t currentHeight = static_cast<uint32_t>(crop.height());
764
765 // The crop is too wide
766 if (newWidth < currentWidth) {
767 uint32_t dw = currentWidth - newWidth;
768 auto halfdw = dw / 2;
769 outCrop.left += halfdw;
770 // Not halfdw because it would subtract 1 too few when dw is odd
771 outCrop.right -= (dw - halfdw);
772 // The crop is too tall
773 } else if (newHeight < currentHeight) {
774 uint32_t dh = currentHeight - newHeight;
775 auto halfdh = dh / 2;
776 outCrop.top += halfdh;
777 // Not halfdh because it would subtract 1 too few when dh is odd
778 outCrop.bottom -= (dh - halfdh);
779 }
780
781 ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
782 outCrop.left, outCrop.top,
783 outCrop.right,outCrop.bottom);
784
785 return outCrop;
786}
787
Andy McFadden2adaf042012-12-18 09:49:45 -0800788nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800789 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800790 Mutex::Autolock lock(mMutex);
791 return mCurrentTimestamp;
792}
793
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700794android_dataspace GLConsumer::getCurrentDataSpace() {
795 GLC_LOGV("getCurrentDataSpace");
796 Mutex::Autolock lock(mMutex);
797 return mCurrentDataSpace;
798}
799
Dan Stozad723bd72014-11-18 10:24:03 -0800800uint64_t GLConsumer::getFrameNumber() {
801 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700802 Mutex::Autolock lock(mMutex);
803 return mCurrentFrameNumber;
804}
805
Chia-I Wu06d63de2017-01-04 14:58:51 +0800806sp<GraphicBuffer> GLConsumer::getCurrentBuffer(int* outSlot) const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700807 Mutex::Autolock lock(mMutex);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800808
809 if (outSlot != nullptr) {
810 *outSlot = mCurrentTexture;
811 }
812
813 return (mCurrentTextureImage == nullptr) ?
Yi Kong48a619f2018-06-05 16:34:59 -0700814 nullptr : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700815}
816
Andy McFadden2adaf042012-12-18 09:49:45 -0800817Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700818 Mutex::Autolock lock(mMutex);
Chia-I Wu93b122f2017-12-01 08:51:39 -0800819 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
820 ? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
821 : mCurrentCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700822}
823
Andy McFadden2adaf042012-12-18 09:49:45 -0800824uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700825 Mutex::Autolock lock(mMutex);
826 return mCurrentTransform;
827}
828
Andy McFadden2adaf042012-12-18 09:49:45 -0800829uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700830 Mutex::Autolock lock(mMutex);
831 return mCurrentScalingMode;
832}
833
Andy McFadden2adaf042012-12-18 09:49:45 -0800834sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700835 Mutex::Autolock lock(mMutex);
836 return mCurrentFence;
837}
838
Brian Anderson3d4039d2016-09-23 16:31:30 -0700839std::shared_ptr<FenceTime> GLConsumer::getCurrentFenceTime() const {
840 Mutex::Autolock lock(mMutex);
841 return mCurrentFenceTime;
842}
843
Andy McFadden2adaf042012-12-18 09:49:45 -0800844status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700845
846 EGLDisplay dpy = eglGetCurrentDisplay();
847 EGLContext ctx = eglGetCurrentContext();
848
849 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800850 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700851 return INVALID_OPERATION;
852 }
853
854 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800855 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700856 return INVALID_OPERATION;
857 }
858
Jamie Gennis1df8c342012-12-20 14:05:45 -0800859 if (mCurrentFence->isValid()) {
Jeffrey Kardatzkea45e1c82018-03-13 12:07:28 -0700860 if (SyncFeatures::getInstance().useWaitSync() &&
861 SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700862 // Create an EGLSyncKHR from the current fence.
863 int fenceFd = mCurrentFence->dup();
864 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800865 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700866 return -errno;
867 }
868 EGLint attribs[] = {
869 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
870 EGL_NONE
871 };
872 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
873 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
874 if (sync == EGL_NO_SYNC_KHR) {
875 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -0800876 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700877 eglGetError());
878 return UNKNOWN_ERROR;
879 }
880
881 // XXX: The spec draft is inconsistent as to whether this should
882 // return an EGLint or void. Ignore the return value for now, as
883 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700884 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700885 EGLint eglErr = eglGetError();
886 eglDestroySyncKHR(dpy, sync);
887 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -0800888 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700889 eglErr);
890 return UNKNOWN_ERROR;
891 }
892 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700893 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800894 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700895 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800896 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700897 return err;
898 }
899 }
900 }
901
902 return NO_ERROR;
903}
904
Andy McFadden2adaf042012-12-18 09:49:45 -0800905void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -0800906 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700907 if (slotIndex == mCurrentTexture) {
908 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
909 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700910 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -0700911 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700912}
Daniel Lameae59d22012-01-22 15:26:27 -0800913
Andy McFadden2adaf042012-12-18 09:49:45 -0800914void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800915 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -0700916 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -0700917 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700918}
919
Chia-I Wue2786ea2017-08-07 10:36:08 -0700920status_t GLConsumer::setConsumerUsageBits(uint64_t usage) {
Chia-I Wua81bc492017-11-27 10:16:00 -0800921 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700922}
923
Mathias Agopian74d211a2013-04-22 16:55:35 +0200924void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700925{
Mathias Agopian74d211a2013-04-22 16:55:35 +0200926 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -0700927 "%smTexName=%d mCurrentTexture=%d\n"
928 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
929 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
930 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
931 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700932
Mathias Agopian74d211a2013-04-22 16:55:35 +0200933 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -0700934}
935
Eric Penner5c3d2432014-07-11 19:08:04 -0700936GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
937 mGraphicBuffer(graphicBuffer),
938 mEglImage(EGL_NO_IMAGE_KHR),
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700939 mEglDisplay(EGL_NO_DISPLAY) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700940}
941
942GLConsumer::EglImage::~EglImage() {
943 if (mEglImage != EGL_NO_IMAGE_KHR) {
944 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
945 ALOGE("~EglImage: eglDestroyImageKHR failed");
946 }
Ady Abrahama3b08ef2019-07-15 18:43:10 -0700947 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
Michael Lentine78be65e2014-10-02 12:10:07 -0700948 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -0700949 }
950}
951
952status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -0700953 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700954 // If there's an image and it's no longer valid, destroy it.
955 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
956 bool displayInvalid = mEglDisplay != eglDisplay;
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700957 if (haveImage && (displayInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700958 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
959 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
960 }
Ady Abrahama3b08ef2019-07-15 18:43:10 -0700961 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
Michael Lentine78be65e2014-10-02 12:10:07 -0700962 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -0700963 mEglImage = EGL_NO_IMAGE_KHR;
964 mEglDisplay = EGL_NO_DISPLAY;
965 }
966
967 // If there's no image, create one.
968 if (mEglImage == EGL_NO_IMAGE_KHR) {
969 mEglDisplay = eglDisplay;
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700970 mEglImage = createImage(mEglDisplay, mGraphicBuffer);
Eric Penner5c3d2432014-07-11 19:08:04 -0700971 }
972
973 // Fail if we can't create a valid image.
974 if (mEglImage == EGL_NO_IMAGE_KHR) {
975 mEglDisplay = EGL_NO_DISPLAY;
Eric Penner5c3d2432014-07-11 19:08:04 -0700976 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700977 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700978 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
979 buffer->getUsage(), buffer->getPixelFormat());
980 return UNKNOWN_ERROR;
981 }
982
983 return OK;
984}
985
986void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -0800987 glEGLImageTargetTexture2DOES(texTarget,
988 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -0700989}
990
991EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700992 const sp<GraphicBuffer>& graphicBuffer) {
Dan Stozad723bd72014-11-18 10:24:03 -0800993 EGLClientBuffer cbuf =
994 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Craig Donneraec86972016-04-28 18:09:40 -0700995 const bool createProtectedImage =
996 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) &&
997 hasEglProtectedContent();
Eric Penner5c3d2432014-07-11 19:08:04 -0700998 EGLint attrs[] = {
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700999 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
Craig Donneraec86972016-04-28 18:09:40 -07001000 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
1001 createProtectedImage ? EGL_TRUE : EGL_NONE,
Eric Penner5c3d2432014-07-11 19:08:04 -07001002 EGL_NONE,
1003 };
Yi Kong48a619f2018-06-05 16:34:59 -07001004 eglInitialize(dpy, nullptr, nullptr);
Eric Penner5c3d2432014-07-11 19:08:04 -07001005 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1006 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1007 if (image == EGL_NO_IMAGE_KHR) {
1008 EGLint error = eglGetError();
1009 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001010 eglTerminate(dpy);
Ady Abrahama3b08ef2019-07-15 18:43:10 -07001011 } else {
1012 DEBUG_EGL_IMAGE_TRACKER_CREATE();
Eric Penner5c3d2432014-07-11 19:08:04 -07001013 }
Ady Abrahama3b08ef2019-07-15 18:43:10 -07001014
Eric Penner5c3d2432014-07-11 19:08:04 -07001015 return image;
1016}
1017
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001018}; // namespace android