blob: f2173cd7407e71730b6fc4464a43d4ec0c62d533 [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
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +000055#define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
56#define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.c_str(), ##__VA_ARGS__)
57// #define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.c_str(), ##__VA_ARGS__)
58#define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.c_str(), ##__VA_ARGS__)
59#define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.c_str(), ##__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
Jim Shargod30823a2024-07-27 02:49:39 +0000104#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
105GLConsumer::GLConsumer(uint32_t tex, uint32_t texTarget, bool useFenceSync, bool isControlledByApp)
106 : ConsumerBase(isControlledByApp, /* isConsumerSurfaceFlinger */ false),
107 mCurrentCrop(Rect::EMPTY_RECT),
108 mCurrentTransform(0),
109 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
110 mCurrentFence(Fence::NO_FENCE),
111 mCurrentTimestamp(0),
112 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
113 mCurrentFrameNumber(0),
114 mDefaultWidth(1),
115 mDefaultHeight(1),
116 mFilteringEnabled(true),
117 mTexName(tex),
118 mUseFenceSync(useFenceSync),
119 mTexTarget(texTarget),
120 mEglDisplay(EGL_NO_DISPLAY),
121 mEglContext(EGL_NO_CONTEXT),
122 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
123 mAttached(true) {
124 GLC_LOGV("GLConsumer");
125
126 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
127
128 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
129}
130#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
131
Mathias Agopian3f844832013-08-07 21:24:32 -0700132GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
133 uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -0700134 ConsumerBase(bq, isControlledByApp),
Pablo Ceballos60d69222015-08-07 14:47:20 -0700135 mCurrentCrop(Rect::EMPTY_RECT),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800136 mCurrentTransform(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200137 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
Jamie Gennis1df8c342012-12-20 14:05:45 -0800138 mCurrentFence(Fence::NO_FENCE),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800139 mCurrentTimestamp(0),
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700140 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700141 mCurrentFrameNumber(0),
Mathias Agopiane692ab92013-04-22 11:24:02 +0200142 mDefaultWidth(1),
143 mDefaultHeight(1),
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700144 mFilteringEnabled(true),
Mathias Agopianb3e518c2011-04-21 18:52:51 -0700145 mTexName(tex),
Jamie Gennis86edf4f2011-11-14 14:51:01 -0800146 mUseFenceSync(useFenceSync),
Daniel Lameae59d22012-01-22 15:26:27 -0800147 mTexTarget(texTarget),
Jamie Gennisce561372012-03-19 18:33:05 -0700148 mEglDisplay(EGL_NO_DISPLAY),
149 mEglContext(EGL_NO_CONTEXT),
Jamie Gennis74bed552012-03-28 19:05:54 -0700150 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
151 mAttached(true)
Daniel Lam6b091c52012-01-22 15:26:27 -0800152{
Dan Stozad723bd72014-11-18 10:24:03 -0800153 GLC_LOGV("GLConsumer");
Daniel Lamb2675792012-02-23 14:35:13 -0800154
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800155 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Jamie Gennisfa28c352011-09-16 17:30:26 -0700156 sizeof(mCurrentTransformMatrix));
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700157
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700158 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800159}
160
Jim Shargod30823a2024-07-27 02:49:39 +0000161#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
162GLConsumer::GLConsumer(uint32_t texTarget, bool useFenceSync, bool isControlledByApp)
163 : ConsumerBase(isControlledByApp, /* isConsumerSurfaceFlinger */ false),
164 mCurrentCrop(Rect::EMPTY_RECT),
165 mCurrentTransform(0),
166 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
167 mCurrentFence(Fence::NO_FENCE),
168 mCurrentTimestamp(0),
169 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
170 mCurrentFrameNumber(0),
171 mDefaultWidth(1),
172 mDefaultHeight(1),
173 mFilteringEnabled(true),
174 mTexName(0),
175 mUseFenceSync(useFenceSync),
176 mTexTarget(texTarget),
177 mEglDisplay(EGL_NO_DISPLAY),
178 mEglContext(EGL_NO_CONTEXT),
179 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
180 mAttached(false) {
181 GLC_LOGV("GLConsumer");
182
183 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
184
185 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
186}
187#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
188
189GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget, bool useFenceSync,
190 bool isControlledByApp)
191 : ConsumerBase(bq, isControlledByApp),
192 mCurrentCrop(Rect::EMPTY_RECT),
193 mCurrentTransform(0),
194 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
195 mCurrentFence(Fence::NO_FENCE),
196 mCurrentTimestamp(0),
197 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
198 mCurrentFrameNumber(0),
199 mDefaultWidth(1),
200 mDefaultHeight(1),
201 mFilteringEnabled(true),
202 mTexName(0),
203 mUseFenceSync(useFenceSync),
204 mTexTarget(texTarget),
205 mEglDisplay(EGL_NO_DISPLAY),
206 mEglContext(EGL_NO_CONTEXT),
207 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT),
208 mAttached(false) {
Dan Stozad723bd72014-11-18 10:24:03 -0800209 GLC_LOGV("GLConsumer");
Dan Stozaab574912014-06-25 14:21:45 -0700210
Chia-I Wu3bd03ff2017-11-17 12:04:57 -0800211 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(),
Dan Stozaab574912014-06-25 14:21:45 -0700212 sizeof(mCurrentTransformMatrix));
213
214 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
215}
216
Andy McFadden2adaf042012-12-18 09:49:45 -0800217status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h)
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700218{
Daniel Lamb2675792012-02-23 14:35:13 -0800219 Mutex::Autolock lock(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -0700220 if (mAbandoned) {
221 GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!");
222 return NO_INIT;
223 }
Daniel Lam016c8cb2012-04-03 15:54:58 -0700224 mDefaultWidth = w;
225 mDefaultHeight = h;
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700226 return mConsumer->setDefaultBufferSize(w, h);
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700227}
228
Andy McFadden2adaf042012-12-18 09:49:45 -0800229status_t GLConsumer::updateTexImage() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800230 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800231 GLC_LOGV("updateTexImage");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800232 Mutex::Autolock lock(mMutex);
233
234 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800235 GLC_LOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800236 return NO_INIT;
237 }
238
239 // Make sure the EGL state is the same as in previous calls.
240 status_t err = checkAndUpdateEglStateLocked();
241 if (err != NO_ERROR) {
242 return err;
243 }
244
Dan Stozadd264162015-03-12 13:58:47 -0700245 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800246
247 // Acquire the next buffer.
248 // In asynchronous mode the list is guaranteed to be one buffer
249 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -0700250 err = acquireBufferLocked(&item, 0);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800251 if (err != NO_ERROR) {
252 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
253 // We always bind the texture even if we don't update its contents.
Dan Stozad723bd72014-11-18 10:24:03 -0800254 GLC_LOGV("updateTexImage: no buffers were available");
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800255 glBindTexture(mTexTarget, mTexName);
256 err = NO_ERROR;
257 } else {
Dan Stozad723bd72014-11-18 10:24:03 -0800258 GLC_LOGE("updateTexImage: acquire failed: %s (%d)",
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800259 strerror(-err), err);
260 }
261 return err;
262 }
263
264 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -0700265 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800266 if (err != NO_ERROR) {
267 // We always bind the texture.
268 glBindTexture(mTexTarget, mTexName);
269 return err;
270 }
271
Andy McFadden97eba892012-12-11 15:21:45 -0800272 // Bind the new buffer to the GL texture, and wait until it's ready.
273 return bindTextureImageLocked();
Mathias Agopian2c8207e2012-05-23 17:56:42 -0700274}
275
Mathias Agopianad678e12013-07-23 17:28:53 -0700276
277status_t GLConsumer::releaseTexImage() {
278 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800279 GLC_LOGV("releaseTexImage");
Mathias Agopianad678e12013-07-23 17:28:53 -0700280 Mutex::Autolock lock(mMutex);
281
282 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800283 GLC_LOGE("releaseTexImage: GLConsumer is abandoned!");
Mathias Agopianad678e12013-07-23 17:28:53 -0700284 return NO_INIT;
285 }
286
287 // Make sure the EGL state is the same as in previous calls.
Mathias Agopian45155962013-08-08 18:16:21 -0700288 status_t err = NO_ERROR;
289
290 if (mAttached) {
291 err = checkAndUpdateEglStateLocked(true);
292 if (err != NO_ERROR) {
293 return err;
294 }
295 } else {
296 // if we're detached, no need to validate EGL's state -- we won't use it.
Mathias Agopianad678e12013-07-23 17:28:53 -0700297 }
298
299 // Update the GLConsumer state.
300 int buf = mCurrentTexture;
301 if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
302
Dan Stozad723bd72014-11-18 10:24:03 -0800303 GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached);
Mathias Agopianad678e12013-07-23 17:28:53 -0700304
Mathias Agopian45155962013-08-08 18:16:21 -0700305 if (mAttached) {
306 // Do whatever sync ops we need to do before releasing the slot.
307 err = syncForReleaseLocked(mEglDisplay);
308 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800309 GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
Mathias Agopian45155962013-08-08 18:16:21 -0700310 return err;
311 }
312 } else {
313 // if we're detached, we just use the fence that was created in detachFromContext()
314 // so... basically, nothing more to do here.
Mathias Agopianad678e12013-07-23 17:28:53 -0700315 }
316
Jim Shargo52108082024-11-15 16:53:57 +0000317 err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer);
Mathias Agopianad678e12013-07-23 17:28:53 -0700318 if (err < NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800319 GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)",
Mathias Agopianad678e12013-07-23 17:28:53 -0700320 strerror(-err), err);
321 return err;
322 }
323
Yi Kong48a619f2018-06-05 16:34:59 -0700324 if (mReleasedTexImage == nullptr) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700325 mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
326 }
327
Mathias Agopianad678e12013-07-23 17:28:53 -0700328 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Eric Penner5c3d2432014-07-11 19:08:04 -0700329 mCurrentTextureImage = mReleasedTexImage;
Mathias Agopianad678e12013-07-23 17:28:53 -0700330 mCurrentCrop.makeInvalid();
331 mCurrentTransform = 0;
Mathias Agopianad678e12013-07-23 17:28:53 -0700332 mCurrentTimestamp = 0;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700333 mCurrentDataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopianad678e12013-07-23 17:28:53 -0700334 mCurrentFence = Fence::NO_FENCE;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700335 mCurrentFenceTime = FenceTime::NO_FENCE;
Mathias Agopianad678e12013-07-23 17:28:53 -0700336
Mathias Agopian45155962013-08-08 18:16:21 -0700337 if (mAttached) {
Xin Lie8b4e702020-08-29 01:34:09 -0700338 // This binds a buffer placeholder (mReleasedTexImage).
Dan Stozad723bd72014-11-18 10:24:03 -0800339 status_t result = bindTextureImageLocked();
340 if (result != NO_ERROR) {
341 return result;
Eric Penner5c3d2432014-07-11 19:08:04 -0700342 }
Mathias Agopian45155962013-08-08 18:16:21 -0700343 } else {
344 // detached, don't touch the texture (and we may not even have an
345 // EGLDisplay here.
346 }
Mathias Agopianad678e12013-07-23 17:28:53 -0700347 }
348
349 return NO_ERROR;
350}
351
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700352sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() {
353 Mutex::Autolock _l(sStaticInitLock);
Yi Kong48a619f2018-06-05 16:34:59 -0700354 if (CC_UNLIKELY(sReleasedTexImageBuffer == nullptr)) {
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700355 // The first time, create the debug texture in case the application
356 // continues to use it.
357 sp<GraphicBuffer> buffer = new GraphicBuffer(
358 kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
SOOJONG HWANG0babcd32021-10-01 00:36:06 +0900359 DEFAULT_USAGE_FLAGS | GraphicBuffer::USAGE_SW_WRITE_RARELY,
Dan Stozad4079af2016-08-22 17:26:41 -0700360 "[GLConsumer debug texture]");
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700361 uint32_t* bits;
362 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
Dan Stozad723bd72014-11-18 10:24:03 -0800363 uint32_t stride = buffer->getStride();
364 uint32_t height = buffer->getHeight();
365 memset(bits, 0, stride * height * 4);
366 for (uint32_t y = 0; y < kDebugData.height; y++) {
367 for (uint32_t x = 0; x < kDebugData.width; x++) {
368 bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ?
369 0xFF000000 : 0xFFFFFFFF;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700370 }
Dan Stozad723bd72014-11-18 10:24:03 -0800371 bits += stride;
Mathias Agopian9870c9b2013-08-08 17:46:48 -0700372 }
373 buffer->unlock();
374 sReleasedTexImageBuffer = buffer;
375 }
376 return sReleasedTexImageBuffer;
377}
378
Dan Stozadd264162015-03-12 13:58:47 -0700379status_t GLConsumer::acquireBufferLocked(BufferItem *item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700380 nsecs_t presentWhen, uint64_t maxFrameNumber) {
381 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen,
382 maxFrameNumber);
Jamie Gennis9fea3422012-08-07 18:03:04 -0700383 if (err != NO_ERROR) {
384 return err;
385 }
386
Eric Penner5c3d2432014-07-11 19:08:04 -0700387 // If item->mGraphicBuffer is not null, this buffer has not been acquired
388 // before, so any prior EglImage created is using a stale buffer. This
389 // replaces any old EglImage with a new one (using the new buffer).
Yi Kong48a619f2018-06-05 16:34:59 -0700390 if (item->mGraphicBuffer != nullptr) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700391 int slot = item->mSlot;
Eric Penner5c3d2432014-07-11 19:08:04 -0700392 mEglSlots[slot].mEglImage = new EglImage(item->mGraphicBuffer);
Jamie Gennisdbe92452013-09-23 17:22:10 -0700393 }
394
Jamie Gennis9fea3422012-08-07 18:03:04 -0700395 return NO_ERROR;
396}
397
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700398status_t GLConsumer::releaseBufferLocked(int buf,
399 sp<GraphicBuffer> graphicBuffer,
400 EGLDisplay display, EGLSyncKHR eglFence) {
401 // release the buffer if it hasn't already been discarded by the
402 // BufferQueue. This can happen, for example, when the producer of this
403 // buffer has reallocated the original buffer slot after this buffer
404 // was acquired.
405 status_t err = ConsumerBase::releaseBufferLocked(
406 buf, graphicBuffer, display, eglFence);
Jamie Gennisd1b330d2012-09-21 11:55:35 -0700407 mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
Jamie Gennis9fea3422012-08-07 18:03:04 -0700408 return err;
409}
410
Dan Stoza3ce46042015-11-17 17:00:45 -0800411status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item,
412 PendingRelease* pendingRelease)
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800413{
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700414 status_t err = NO_ERROR;
415
Pablo Ceballos47650f42015-08-04 16:38:17 -0700416 int slot = item.mSlot;
Andy McFadden87a67842014-04-04 15:37:08 -0700417
Jamie Gennis74bed552012-03-28 19:05:54 -0700418 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800419 GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL "
Jamie Gennis74bed552012-03-28 19:05:54 -0700420 "ES context");
Jim Shargo52108082024-11-15 16:53:57 +0000421 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Jamie Gennis74bed552012-03-28 19:05:54 -0700422 return INVALID_OPERATION;
423 }
424
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800425 // Confirm state.
426 err = checkAndUpdateEglStateLocked();
427 if (err != NO_ERROR) {
Jim Shargo52108082024-11-15 16:53:57 +0000428 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800429 return err;
430 }
431
Eric Penner5c3d2432014-07-11 19:08:04 -0700432 // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
433 // if nessessary, for the gralloc buffer currently in the slot in
434 // ConsumerBase.
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800435 // We may have to do this even when item.mGraphicBuffer == NULL (which
Eric Penner5c3d2432014-07-11 19:08:04 -0700436 // means the buffer was previously acquired).
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700437 err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -0700438 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800439 GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d",
Pablo Ceballos47650f42015-08-04 16:38:17 -0700440 mEglDisplay, slot);
Jim Shargo52108082024-11-15 16:53:57 +0000441 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Eric Penner5c3d2432014-07-11 19:08:04 -0700442 return UNKNOWN_ERROR;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800443 }
444
445 // Do whatever sync ops we need to do before releasing the old slot.
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800446 if (slot != mCurrentTexture) {
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800447 err = syncForReleaseLocked(mEglDisplay);
448 if (err != NO_ERROR) {
449 // Release the buffer we just acquired. It's not safe to
450 // release the old buffer, so instead we just drop the new frame.
451 // As we are still under lock since acquireBuffer, it is safe to
452 // release by slot.
Jim Shargo52108082024-11-15 16:53:57 +0000453 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Pablo Ceballos33fcc2e2015-11-20 15:44:51 -0800454 return err;
455 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800456 }
457
Dan Stozad723bd72014-11-18 10:24:03 -0800458 GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)",
Yi Konga03e0442018-07-17 11:16:57 -0700459 mCurrentTexture, mCurrentTextureImage != nullptr ?
Yi Kong48a619f2018-06-05 16:34:59 -0700460 mCurrentTextureImage->graphicBufferHandle() : nullptr,
Pablo Ceballos47650f42015-08-04 16:38:17 -0700461 slot, mSlots[slot].mGraphicBuffer->handle);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800462
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700463 // Hang onto the pointer so that it isn't freed in the call to
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700464 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700465 // the same.
466 sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
467
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800468 // release old buffer
469 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Dan Stoza3ce46042015-11-17 17:00:45 -0800470 if (pendingRelease == nullptr) {
471 status_t status = releaseBufferLocked(
472 mCurrentTexture, mCurrentTextureImage->graphicBuffer(),
473 mEglDisplay, mEglSlots[mCurrentTexture].mEglFence);
474 if (status < NO_ERROR) {
475 GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)",
476 strerror(-status), status);
477 err = status;
478 // keep going, with error raised [?]
479 }
480 } else {
481 pendingRelease->currentTexture = mCurrentTexture;
482 pendingRelease->graphicBuffer =
483 mCurrentTextureImage->graphicBuffer();
484 pendingRelease->display = mEglDisplay;
485 pendingRelease->fence = mEglSlots[mCurrentTexture].mEglFence;
486 pendingRelease->isPending = true;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800487 }
488 }
489
Andy McFadden2adaf042012-12-18 09:49:45 -0800490 // Update the GLConsumer state.
Pablo Ceballos47650f42015-08-04 16:38:17 -0700491 mCurrentTexture = slot;
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700492 mCurrentTextureImage = nextTextureImage;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800493 mCurrentCrop = item.mCrop;
494 mCurrentTransform = item.mTransform;
495 mCurrentScalingMode = item.mScalingMode;
496 mCurrentTimestamp = item.mTimestamp;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700497 mCurrentDataSpace = item.mDataSpace;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800498 mCurrentFence = item.mFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700499 mCurrentFenceTime = item.mFenceTime;
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700500 mCurrentFrameNumber = item.mFrameNumber;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800501
502 computeCurrentTransformMatrixLocked();
503
504 return err;
505}
506
Andy McFadden2adaf042012-12-18 09:49:45 -0800507status_t GLConsumer::bindTextureImageLocked() {
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800508 if (mEglDisplay == EGL_NO_DISPLAY) {
509 ALOGE("bindTextureImage: invalid display");
510 return INVALID_OPERATION;
511 }
512
Dan Stozad723bd72014-11-18 10:24:03 -0800513 GLenum error;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800514 while ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800515 GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800516 }
517
518 glBindTexture(mTexTarget, mTexName);
Eric Penner5c3d2432014-07-11 19:08:04 -0700519 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT &&
Yi Kong48a619f2018-06-05 16:34:59 -0700520 mCurrentTextureImage == nullptr) {
Dan Stozad723bd72014-11-18 10:24:03 -0800521 GLC_LOGE("bindTextureImage: no currently-bound texture");
Eric Penner5c3d2432014-07-11 19:08:04 -0700522 return NO_INIT;
523 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800524
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700525 status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -0700526 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800527 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -0700528 mEglDisplay, mCurrentTexture);
529 return UNKNOWN_ERROR;
530 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700531 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
532
Eric Penner2d14a0e2014-08-25 20:16:37 -0700533 // In the rare case that the display is terminated and then initialized
534 // again, we can't detect that the display changed (it didn't), but the
535 // image is invalid. In this case, repeat the exact same steps while
536 // forcing the creation of a new image.
537 if ((error = glGetError()) != GL_NO_ERROR) {
538 glBindTexture(mTexTarget, mTexName);
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700539 status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay, true);
Dan Stozad723bd72014-11-18 10:24:03 -0800540 if (result != NO_ERROR) {
541 GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d",
Eric Penner2d14a0e2014-08-25 20:16:37 -0700542 mEglDisplay, mCurrentTexture);
543 return UNKNOWN_ERROR;
544 }
545 mCurrentTextureImage->bindToTextureTarget(mTexTarget);
546 if ((error = glGetError()) != GL_NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800547 GLC_LOGE("bindTextureImage: error binding external image: %#04x", error);
Eric Penner2d14a0e2014-08-25 20:16:37 -0700548 return UNKNOWN_ERROR;
549 }
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800550 }
Andy McFadden97eba892012-12-11 15:21:45 -0800551
552 // Wait for the new buffer to be ready.
553 return doGLFenceWaitLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800554}
555
Mathias Agopian45155962013-08-08 18:16:21 -0700556status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) {
Jamie Gennisce561372012-03-19 18:33:05 -0700557 EGLDisplay dpy = eglGetCurrentDisplay();
558 EGLContext ctx = eglGetCurrentContext();
559
Mathias Agopian45155962013-08-08 18:16:21 -0700560 if (!contextCheck) {
561 // if this is the first time we're called, mEglDisplay/mEglContext have
562 // never been set, so don't error out (below).
563 if (mEglDisplay == EGL_NO_DISPLAY) {
564 mEglDisplay = dpy;
565 }
Jesse Hall46a1f6b2014-08-06 12:15:15 -0700566 if (mEglContext == EGL_NO_CONTEXT) {
Mathias Agopian45155962013-08-08 18:16:21 -0700567 mEglContext = ctx;
568 }
569 }
570
571 if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800572 GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700573 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700574 }
575
Mathias Agopian45155962013-08-08 18:16:21 -0700576 if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800577 GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700578 return INVALID_OPERATION;
Jamie Gennisce561372012-03-19 18:33:05 -0700579 }
580
581 mEglDisplay = dpy;
582 mEglContext = ctx;
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800583 return NO_ERROR;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800584}
585
Jesse Hall13f01cb2013-03-20 11:37:21 -0700586void GLConsumer::setReleaseFence(const sp<Fence>& fence) {
587 if (fence->isValid() &&
588 mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700589 status_t err = addReleaseFence(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700590 mCurrentTextureImage->graphicBuffer(), fence);
Jesse Hall13f01cb2013-03-20 11:37:21 -0700591 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800592 GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)",
Jesse Hall13f01cb2013-03-20 11:37:21 -0700593 strerror(-err), err);
594 }
Jesse Hallef194142012-06-14 14:45:17 -0700595 }
596}
597
Andy McFadden2adaf042012-12-18 09:49:45 -0800598status_t GLConsumer::detachFromContext() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700599 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800600 GLC_LOGV("detachFromContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700601 Mutex::Autolock lock(mMutex);
602
603 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800604 GLC_LOGE("detachFromContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700605 return NO_INIT;
606 }
607
608 if (!mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800609 GLC_LOGE("detachFromContext: GLConsumer is not attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700610 "context");
611 return INVALID_OPERATION;
612 }
613
614 EGLDisplay dpy = eglGetCurrentDisplay();
615 EGLContext ctx = eglGetCurrentContext();
616
617 if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800618 GLC_LOGE("detachFromContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700619 return INVALID_OPERATION;
620 }
621
622 if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800623 GLC_LOGE("detachFromContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700624 return INVALID_OPERATION;
625 }
626
627 if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
628 status_t err = syncForReleaseLocked(dpy);
629 if (err != OK) {
630 return err;
631 }
632
633 glDeleteTextures(1, &mTexName);
634 }
635
636 mEglDisplay = EGL_NO_DISPLAY;
637 mEglContext = EGL_NO_CONTEXT;
638 mAttached = false;
639
640 return OK;
641}
642
Mathias Agopian3f844832013-08-07 21:24:32 -0700643status_t GLConsumer::attachToContext(uint32_t tex) {
Jamie Gennis74bed552012-03-28 19:05:54 -0700644 ATRACE_CALL();
Dan Stozad723bd72014-11-18 10:24:03 -0800645 GLC_LOGV("attachToContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700646 Mutex::Autolock lock(mMutex);
647
648 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800649 GLC_LOGE("attachToContext: abandoned GLConsumer");
Jamie Gennis74bed552012-03-28 19:05:54 -0700650 return NO_INIT;
651 }
652
653 if (mAttached) {
Dan Stozad723bd72014-11-18 10:24:03 -0800654 GLC_LOGE("attachToContext: GLConsumer is already attached to a "
Jamie Gennis74bed552012-03-28 19:05:54 -0700655 "context");
656 return INVALID_OPERATION;
657 }
658
659 EGLDisplay dpy = eglGetCurrentDisplay();
660 EGLContext ctx = eglGetCurrentContext();
661
662 if (dpy == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800663 GLC_LOGE("attachToContext: invalid current EGLDisplay");
Jamie Gennis74bed552012-03-28 19:05:54 -0700664 return INVALID_OPERATION;
665 }
666
667 if (ctx == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800668 GLC_LOGE("attachToContext: invalid current EGLContext");
Jamie Gennis74bed552012-03-28 19:05:54 -0700669 return INVALID_OPERATION;
670 }
671
672 // We need to bind the texture regardless of whether there's a current
673 // buffer.
Mathias Agopian3f844832013-08-07 21:24:32 -0700674 glBindTexture(mTexTarget, GLuint(tex));
Jamie Gennis74bed552012-03-28 19:05:54 -0700675
Jamie Gennis74bed552012-03-28 19:05:54 -0700676 mEglDisplay = dpy;
677 mEglContext = ctx;
678 mTexName = tex;
679 mAttached = true;
680
Yi Kong48a619f2018-06-05 16:34:59 -0700681 if (mCurrentTextureImage != nullptr) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700682 // This may wait for a buffer a second time. This is likely required if
683 // this is a different context, since otherwise the wait could be skipped
684 // by bouncing through another context. For the same context the extra
685 // wait is redundant.
686 status_t err = bindTextureImageLocked();
687 if (err != NO_ERROR) {
688 return err;
689 }
690 }
691
Jamie Gennis74bed552012-03-28 19:05:54 -0700692 return OK;
693}
694
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800695
Andy McFadden2adaf042012-12-18 09:49:45 -0800696status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {
Dan Stozad723bd72014-11-18 10:24:03 -0800697 GLC_LOGV("syncForReleaseLocked");
Jamie Gennis74bed552012-03-28 19:05:54 -0700698
Jamie Gennis01dbf552012-09-06 14:54:19 -0700699 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Mathias Agopianca088332013-03-28 17:44:13 -0700700 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700701 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
Yi Kong48a619f2018-06-05 16:34:59 -0700702 EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700703 if (sync == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800704 GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700705 eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -0700706 return UNKNOWN_ERROR;
Jamie Gennis74bed552012-03-28 19:05:54 -0700707 }
Jamie Gennis01dbf552012-09-06 14:54:19 -0700708 glFlush();
709 int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
Jamie Gennis98ff0592012-09-10 14:49:42 -0700710 eglDestroySyncKHR(dpy, sync);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700711 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
Dan Stozad723bd72014-11-18 10:24:03 -0800712 GLC_LOGE("syncForReleaseLocked: error dup'ing native fence "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700713 "fd: %#x", eglGetError());
714 return UNKNOWN_ERROR;
715 }
716 sp<Fence> fence(new Fence(fenceFd));
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700717 status_t err = addReleaseFenceLocked(mCurrentTexture,
Eric Penner5c3d2432014-07-11 19:08:04 -0700718 mCurrentTextureImage->graphicBuffer(), fence);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700719 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800720 GLC_LOGE("syncForReleaseLocked: error adding release fence: "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700721 "%s (%d)", strerror(-err), err);
722 return err;
723 }
Mathias Agopianca088332013-03-28 17:44:13 -0700724 } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
Jamie Gennis01dbf552012-09-06 14:54:19 -0700725 EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence;
726 if (fence != EGL_NO_SYNC_KHR) {
727 // There is already a fence for the current slot. We need to
728 // wait on that before replacing it with another fence to
729 // ensure that all outstanding buffer accesses have completed
730 // before the producer accesses it.
731 EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
732 if (result == EGL_FALSE) {
Dan Stozad723bd72014-11-18 10:24:03 -0800733 GLC_LOGE("syncForReleaseLocked: error waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700734 "fence: %#x", eglGetError());
735 return UNKNOWN_ERROR;
736 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800737 GLC_LOGE("syncForReleaseLocked: timeout waiting for previous "
Jamie Gennis01dbf552012-09-06 14:54:19 -0700738 "fence");
739 return TIMED_OUT;
740 }
741 eglDestroySyncKHR(dpy, fence);
742 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700743
Jamie Gennis01dbf552012-09-06 14:54:19 -0700744 // Create a fence for the outstanding accesses in the current
745 // OpenGL ES context.
Yi Kong48a619f2018-06-05 16:34:59 -0700746 fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
Jamie Gennis01dbf552012-09-06 14:54:19 -0700747 if (fence == EGL_NO_SYNC_KHR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800748 GLC_LOGE("syncForReleaseLocked: error creating fence: %#x",
Jamie Gennis01dbf552012-09-06 14:54:19 -0700749 eglGetError());
750 return UNKNOWN_ERROR;
751 }
752 glFlush();
753 mEglSlots[mCurrentTexture].mEglFence = fence;
Jamie Gennis74bed552012-03-28 19:05:54 -0700754 }
Jamie Gennis74bed552012-03-28 19:05:54 -0700755 }
756
757 return OK;
758}
759
Mathias Agopian3f844832013-08-07 21:24:32 -0700760uint32_t GLConsumer::getCurrentTextureTarget() const {
Jamie Gennisfb1b5a22011-09-28 12:13:31 -0700761 return mTexTarget;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700762}
763
Andy McFadden2adaf042012-12-18 09:49:45 -0800764void GLConsumer::getTransformMatrix(float mtx[16]) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800765 Mutex::Autolock lock(mMutex);
Jamie Gennis736aa952011-06-12 17:03:06 -0700766 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
767}
768
Andy McFadden2adaf042012-12-18 09:49:45 -0800769void GLConsumer::setFilteringEnabled(bool enabled) {
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700770 Mutex::Autolock lock(mMutex);
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700771 if (mAbandoned) {
Dan Stozad723bd72014-11-18 10:24:03 -0800772 GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700773 return;
774 }
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700775 bool needsRecompute = mFilteringEnabled != enabled;
776 mFilteringEnabled = enabled;
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700777
Yi Kong48a619f2018-06-05 16:34:59 -0700778 if (needsRecompute && mCurrentTextureImage==nullptr) {
Dan Stozad723bd72014-11-18 10:24:03 -0800779 GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL");
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700780 }
781
Yi Kong48a619f2018-06-05 16:34:59 -0700782 if (needsRecompute && mCurrentTextureImage != nullptr) {
Mathias Agopiane96e9e12012-09-24 19:26:11 -0700783 computeCurrentTransformMatrixLocked();
Jamie Gennis5c1139f2012-05-08 16:56:34 -0700784 }
785}
786
Andy McFadden2adaf042012-12-18 09:49:45 -0800787void GLConsumer::computeCurrentTransformMatrixLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800788 GLC_LOGV("computeCurrentTransformMatrixLocked");
John Reck1a61da52016-04-28 13:18:15 -0700789 sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ?
790 nullptr : mCurrentTextureImage->graphicBuffer();
791 if (buf == nullptr) {
792 GLC_LOGD("computeCurrentTransformMatrixLocked: "
793 "mCurrentTextureImage is NULL");
794 }
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700795 computeTransformMatrix(mCurrentTransformMatrix, buf, mCurrentCrop,
John Reck1a61da52016-04-28 13:18:15 -0700796 mCurrentTransform, mFilteringEnabled);
797}
798
Chia-I Wu93b122f2017-12-01 08:51:39 -0800799Rect GLConsumer::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight) {
800 Rect outCrop = crop;
801
802 uint32_t newWidth = static_cast<uint32_t>(crop.width());
803 uint32_t newHeight = static_cast<uint32_t>(crop.height());
804
805 if (newWidth * bufferHeight > newHeight * bufferWidth) {
806 newWidth = newHeight * bufferWidth / bufferHeight;
807 ALOGV("too wide: newWidth = %d", newWidth);
808 } else if (newWidth * bufferHeight < newHeight * bufferWidth) {
809 newHeight = newWidth * bufferHeight / bufferWidth;
810 ALOGV("too tall: newHeight = %d", newHeight);
811 }
812
813 uint32_t currentWidth = static_cast<uint32_t>(crop.width());
814 uint32_t currentHeight = static_cast<uint32_t>(crop.height());
815
816 // The crop is too wide
817 if (newWidth < currentWidth) {
818 uint32_t dw = currentWidth - newWidth;
819 auto halfdw = dw / 2;
820 outCrop.left += halfdw;
821 // Not halfdw because it would subtract 1 too few when dw is odd
822 outCrop.right -= (dw - halfdw);
823 // The crop is too tall
824 } else if (newHeight < currentHeight) {
825 uint32_t dh = currentHeight - newHeight;
826 auto halfdh = dh / 2;
827 outCrop.top += halfdh;
828 // Not halfdh because it would subtract 1 too few when dh is odd
829 outCrop.bottom -= (dh - halfdh);
830 }
831
832 ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
833 outCrop.left, outCrop.top,
834 outCrop.right,outCrop.bottom);
835
836 return outCrop;
837}
838
Andy McFadden2adaf042012-12-18 09:49:45 -0800839nsecs_t GLConsumer::getTimestamp() {
Dan Stozad723bd72014-11-18 10:24:03 -0800840 GLC_LOGV("getTimestamp");
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800841 Mutex::Autolock lock(mMutex);
842 return mCurrentTimestamp;
843}
844
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700845android_dataspace GLConsumer::getCurrentDataSpace() {
846 GLC_LOGV("getCurrentDataSpace");
847 Mutex::Autolock lock(mMutex);
848 return mCurrentDataSpace;
849}
850
Dan Stozad723bd72014-11-18 10:24:03 -0800851uint64_t GLConsumer::getFrameNumber() {
852 GLC_LOGV("getFrameNumber");
Eino-Ville Talvalad171da92013-09-19 13:36:07 -0700853 Mutex::Autolock lock(mMutex);
854 return mCurrentFrameNumber;
855}
856
Chia-I Wu06d63de2017-01-04 14:58:51 +0800857sp<GraphicBuffer> GLConsumer::getCurrentBuffer(int* outSlot) const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700858 Mutex::Autolock lock(mMutex);
Chia-I Wu06d63de2017-01-04 14:58:51 +0800859
860 if (outSlot != nullptr) {
861 *outSlot = mCurrentTexture;
862 }
863
864 return (mCurrentTextureImage == nullptr) ?
Yi Kong48a619f2018-06-05 16:34:59 -0700865 nullptr : mCurrentTextureImage->graphicBuffer();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700866}
867
Andy McFadden2adaf042012-12-18 09:49:45 -0800868Rect GLConsumer::getCurrentCrop() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700869 Mutex::Autolock lock(mMutex);
Chia-I Wu93b122f2017-12-01 08:51:39 -0800870 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
871 ? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
872 : mCurrentCrop;
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700873}
874
Andy McFadden2adaf042012-12-18 09:49:45 -0800875uint32_t GLConsumer::getCurrentTransform() const {
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700876 Mutex::Autolock lock(mMutex);
877 return mCurrentTransform;
878}
879
Andy McFadden2adaf042012-12-18 09:49:45 -0800880uint32_t GLConsumer::getCurrentScalingMode() const {
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700881 Mutex::Autolock lock(mMutex);
882 return mCurrentScalingMode;
883}
884
Andy McFadden2adaf042012-12-18 09:49:45 -0800885sp<Fence> GLConsumer::getCurrentFence() const {
Jesse Halldc5b4852012-06-29 15:21:18 -0700886 Mutex::Autolock lock(mMutex);
887 return mCurrentFence;
888}
889
Brian Anderson3d4039d2016-09-23 16:31:30 -0700890std::shared_ptr<FenceTime> GLConsumer::getCurrentFenceTime() const {
891 Mutex::Autolock lock(mMutex);
892 return mCurrentFenceTime;
893}
894
Andy McFadden2adaf042012-12-18 09:49:45 -0800895status_t GLConsumer::doGLFenceWaitLocked() const {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700896
897 EGLDisplay dpy = eglGetCurrentDisplay();
898 EGLContext ctx = eglGetCurrentContext();
899
900 if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
Dan Stozad723bd72014-11-18 10:24:03 -0800901 GLC_LOGE("doGLFenceWait: invalid current EGLDisplay");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700902 return INVALID_OPERATION;
903 }
904
905 if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
Dan Stozad723bd72014-11-18 10:24:03 -0800906 GLC_LOGE("doGLFenceWait: invalid current EGLContext");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700907 return INVALID_OPERATION;
908 }
909
Jamie Gennis1df8c342012-12-20 14:05:45 -0800910 if (mCurrentFence->isValid()) {
Jeffrey Kardatzkea45e1c82018-03-13 12:07:28 -0700911 if (SyncFeatures::getInstance().useWaitSync() &&
912 SyncFeatures::getInstance().useNativeFenceSync()) {
Jamie Gennis61e04b92012-09-09 17:48:42 -0700913 // Create an EGLSyncKHR from the current fence.
914 int fenceFd = mCurrentFence->dup();
915 if (fenceFd == -1) {
Dan Stozad723bd72014-11-18 10:24:03 -0800916 GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700917 return -errno;
918 }
919 EGLint attribs[] = {
920 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
921 EGL_NONE
922 };
923 EGLSyncKHR sync = eglCreateSyncKHR(dpy,
924 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
925 if (sync == EGL_NO_SYNC_KHR) {
926 close(fenceFd);
Dan Stozad723bd72014-11-18 10:24:03 -0800927 GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700928 eglGetError());
929 return UNKNOWN_ERROR;
930 }
931
932 // XXX: The spec draft is inconsistent as to whether this should
933 // return an EGLint or void. Ignore the return value for now, as
934 // it's not strictly needed.
Mathias Agopian2bb71682013-03-27 17:32:41 -0700935 eglWaitSyncKHR(dpy, sync, 0);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700936 EGLint eglErr = eglGetError();
937 eglDestroySyncKHR(dpy, sync);
938 if (eglErr != EGL_SUCCESS) {
Dan Stozad723bd72014-11-18 10:24:03 -0800939 GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x",
Jamie Gennis61e04b92012-09-09 17:48:42 -0700940 eglErr);
941 return UNKNOWN_ERROR;
942 }
943 } else {
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700944 status_t err = mCurrentFence->waitForever(
Andy McFadden2adaf042012-12-18 09:49:45 -0800945 "GLConsumer::doGLFenceWaitLocked");
Jamie Gennis61e04b92012-09-09 17:48:42 -0700946 if (err != NO_ERROR) {
Dan Stozad723bd72014-11-18 10:24:03 -0800947 GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
Jamie Gennis61e04b92012-09-09 17:48:42 -0700948 return err;
949 }
950 }
951 }
952
953 return NO_ERROR;
954}
955
Andy McFadden2adaf042012-12-18 09:49:45 -0800956void GLConsumer::freeBufferLocked(int slotIndex) {
Dan Stozad723bd72014-11-18 10:24:03 -0800957 GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700958 if (slotIndex == mCurrentTexture) {
959 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
960 }
Eric Penner5c3d2432014-07-11 19:08:04 -0700961 mEglSlots[slotIndex].mEglImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -0700962 ConsumerBase::freeBufferLocked(slotIndex);
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700963}
Daniel Lameae59d22012-01-22 15:26:27 -0800964
Andy McFadden2adaf042012-12-18 09:49:45 -0800965void GLConsumer::abandonLocked() {
Dan Stozad723bd72014-11-18 10:24:03 -0800966 GLC_LOGV("abandonLocked");
Eric Penner5c3d2432014-07-11 19:08:04 -0700967 mCurrentTextureImage.clear();
Jamie Gennis9fea3422012-08-07 18:03:04 -0700968 ConsumerBase::abandonLocked();
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700969}
970
Chia-I Wue2786ea2017-08-07 10:36:08 -0700971status_t GLConsumer::setConsumerUsageBits(uint64_t usage) {
Chia-I Wua81bc492017-11-27 10:16:00 -0800972 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
Pablo Ceballos19e3e062015-08-19 16:16:06 -0700973}
974
Mathias Agopian74d211a2013-04-22 16:55:35 +0200975void GLConsumer::dumpLocked(String8& result, const char* prefix) const
Mathias Agopian68c77942011-05-09 19:08:33 -0700976{
Mathias Agopian74d211a2013-04-22 16:55:35 +0200977 result.appendFormat(
Jamie Gennis9fea3422012-08-07 18:03:04 -0700978 "%smTexName=%d mCurrentTexture=%d\n"
979 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
980 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
981 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
982 mCurrentTransform);
Mathias Agopian68c77942011-05-09 19:08:33 -0700983
Mathias Agopian74d211a2013-04-22 16:55:35 +0200984 ConsumerBase::dumpLocked(result, prefix);
Mathias Agopian68c77942011-05-09 19:08:33 -0700985}
986
Eric Penner5c3d2432014-07-11 19:08:04 -0700987GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) :
988 mGraphicBuffer(graphicBuffer),
989 mEglImage(EGL_NO_IMAGE_KHR),
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700990 mEglDisplay(EGL_NO_DISPLAY) {
Eric Penner5c3d2432014-07-11 19:08:04 -0700991}
992
993GLConsumer::EglImage::~EglImage() {
994 if (mEglImage != EGL_NO_IMAGE_KHR) {
995 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
996 ALOGE("~EglImage: eglDestroyImageKHR failed");
997 }
Ady Abrahama3b08ef2019-07-15 18:43:10 -0700998 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
Michael Lentine78be65e2014-10-02 12:10:07 -0700999 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001000 }
1001}
1002
1003status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay,
Eric Penner2d14a0e2014-08-25 20:16:37 -07001004 bool forceCreation) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001005 // If there's an image and it's no longer valid, destroy it.
1006 bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
1007 bool displayInvalid = mEglDisplay != eglDisplay;
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001008 if (haveImage && (displayInvalid || forceCreation)) {
Eric Penner5c3d2432014-07-11 19:08:04 -07001009 if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
1010 ALOGE("createIfNeeded: eglDestroyImageKHR failed");
1011 }
Ady Abrahama3b08ef2019-07-15 18:43:10 -07001012 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
Michael Lentine78be65e2014-10-02 12:10:07 -07001013 eglTerminate(mEglDisplay);
Eric Penner5c3d2432014-07-11 19:08:04 -07001014 mEglImage = EGL_NO_IMAGE_KHR;
1015 mEglDisplay = EGL_NO_DISPLAY;
1016 }
1017
1018 // If there's no image, create one.
1019 if (mEglImage == EGL_NO_IMAGE_KHR) {
1020 mEglDisplay = eglDisplay;
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001021 mEglImage = createImage(mEglDisplay, mGraphicBuffer);
Eric Penner5c3d2432014-07-11 19:08:04 -07001022 }
1023
1024 // Fail if we can't create a valid image.
1025 if (mEglImage == EGL_NO_IMAGE_KHR) {
1026 mEglDisplay = EGL_NO_DISPLAY;
Eric Penner5c3d2432014-07-11 19:08:04 -07001027 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
Mathias Agopiancb496ac2017-05-22 14:21:00 -07001028 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
Eric Penner5c3d2432014-07-11 19:08:04 -07001029 buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
1030 buffer->getUsage(), buffer->getPixelFormat());
1031 return UNKNOWN_ERROR;
1032 }
1033
1034 return OK;
1035}
1036
1037void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
Dan Stozad723bd72014-11-18 10:24:03 -08001038 glEGLImageTargetTexture2DOES(texTarget,
1039 static_cast<GLeglImageOES>(mEglImage));
Eric Penner5c3d2432014-07-11 19:08:04 -07001040}
1041
1042EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy,
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001043 const sp<GraphicBuffer>& graphicBuffer) {
Dan Stozad723bd72014-11-18 10:24:03 -08001044 EGLClientBuffer cbuf =
1045 static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
Craig Donneraec86972016-04-28 18:09:40 -07001046 const bool createProtectedImage =
1047 (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) &&
1048 hasEglProtectedContent();
Eric Penner5c3d2432014-07-11 19:08:04 -07001049 EGLint attrs[] = {
Krzysztof Kosiński7108c312018-06-27 19:12:54 -07001050 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
Craig Donneraec86972016-04-28 18:09:40 -07001051 createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
1052 createProtectedImage ? EGL_TRUE : EGL_NONE,
Eric Penner5c3d2432014-07-11 19:08:04 -07001053 EGL_NONE,
1054 };
Yi Kong48a619f2018-06-05 16:34:59 -07001055 eglInitialize(dpy, nullptr, nullptr);
Eric Penner5c3d2432014-07-11 19:08:04 -07001056 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
1057 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
1058 if (image == EGL_NO_IMAGE_KHR) {
1059 EGLint error = eglGetError();
1060 ALOGE("error creating EGLImage: %#x", error);
Michael Lentine78be65e2014-10-02 12:10:07 -07001061 eglTerminate(dpy);
Ady Abrahama3b08ef2019-07-15 18:43:10 -07001062 } else {
1063 DEBUG_EGL_IMAGE_TRACKER_CREATE();
Eric Penner5c3d2432014-07-11 19:08:04 -07001064 }
Ady Abrahama3b08ef2019-07-15 18:43:10 -07001065
Eric Penner5c3d2432014-07-11 19:08:04 -07001066 return image;
1067}
1068
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001069}; // namespace android