Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1 | /* |
| 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 McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 17 | #define LOG_TAG "GLConsumer" |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Jamie Gennis | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 20 | |
| 21 | #define GL_GLEXT_PROTOTYPES |
| 22 | #define EGL_EGLEXT_PROTOTYPES |
| 23 | |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
| 25 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 26 | #include <EGL/egl.h> |
| 27 | #include <EGL/eglext.h> |
| 28 | #include <GLES2/gl2.h> |
| 29 | #include <GLES2/gl2ext.h> |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 30 | #include <cutils/compiler.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 31 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 32 | #include <hardware/hardware.h> |
| 33 | |
Chia-I Wu | 3bd03ff | 2017-11-17 12:04:57 -0800 | [diff] [blame] | 34 | #include <math/mat4.h> |
| 35 | |
Dan Stoza | dd26416 | 2015-03-12 13:58:47 -0700 | [diff] [blame] | 36 | #include <gui/BufferItem.h> |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 37 | #include <gui/DebugEGLImageTracker.h> |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 38 | #include <gui/GLConsumer.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 39 | #include <gui/ISurfaceComposer.h> |
Jim Shargo | df578c3 | 2025-02-24 16:15:09 +0000 | [diff] [blame] | 40 | #include <gui/Surface.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 41 | #include <gui/SurfaceComposerClient.h> |
Mathias Agopian | 41f673c | 2011-11-17 17:48:35 -0800 | [diff] [blame] | 42 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 43 | #include <private/gui/ComposerService.h> |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 44 | #include <private/gui/SyncFeatures.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 45 | |
| 46 | #include <utils/Log.h> |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 47 | #include <utils/String8.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 48 | #include <utils/Trace.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 49 | |
Craig Donner | aec8697 | 2016-04-28 18:09:40 -0700 | [diff] [blame] | 50 | #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" |
| 51 | #define EGL_PROTECTED_CONTENT_EXT 0x32C0 |
Jamie Gennis | dbe9245 | 2013-09-23 17:22:10 -0700 | [diff] [blame] | 52 | |
Andy McFadden | 97eba89 | 2012-12-11 15:21:45 -0800 | [diff] [blame] | 53 | namespace android { |
| 54 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 55 | // Macros for including the GLConsumer name in log messages |
Tomasz Wasilczyk | b19fe0c | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 56 | #define GLC_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
| 57 | #define GLC_LOGD(x, ...) ALOGD("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
| 58 | // #define GLC_LOGI(x, ...) ALOGI("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
| 59 | #define GLC_LOGW(x, ...) ALOGW("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
| 60 | #define GLC_LOGE(x, ...) ALOGE("[%s] " x, mName.c_str(), ##__VA_ARGS__) |
Mathias Agopian | 29b5762 | 2011-08-17 15:42:04 -0700 | [diff] [blame] | 61 | |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 62 | static const struct { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 63 | uint32_t width, height; |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 64 | char const* bits; |
Mathias Agopian | 45263e2 | 2013-08-07 13:35:20 -0700 | [diff] [blame] | 65 | } kDebugData = { 15, 12, |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 66 | "_______________" |
| 67 | "_______________" |
| 68 | "_____XX_XX_____" |
| 69 | "__X_X_____X_X__" |
| 70 | "__X_XXXXXXX_X__" |
| 71 | "__XXXXXXXXXXX__" |
| 72 | "___XX_XXX_XX___" |
| 73 | "____XXXXXXX____" |
| 74 | "_____X___X_____" |
| 75 | "____X_____X____" |
| 76 | "_______________" |
| 77 | "_______________" |
Mathias Agopian | 45263e2 | 2013-08-07 13:35:20 -0700 | [diff] [blame] | 78 | }; |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 79 | |
Chia-I Wu | 3bd03ff | 2017-11-17 12:04:57 -0800 | [diff] [blame] | 80 | static const mat4 mtxIdentity; |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 81 | |
Mathias Agopian | 9870c9b | 2013-08-08 17:46:48 -0700 | [diff] [blame] | 82 | Mutex GLConsumer::sStaticInitLock; |
| 83 | sp<GraphicBuffer> GLConsumer::sReleasedTexImageBuffer; |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 84 | |
Craig Donner | aec8697 | 2016-04-28 18:09:40 -0700 | [diff] [blame] | 85 | static bool hasEglProtectedContentImpl() { |
| 86 | EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 87 | const char* exts = eglQueryString(dpy, EGL_EXTENSIONS); |
| 88 | size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR); |
| 89 | size_t extsLen = strlen(exts); |
| 90 | bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts); |
| 91 | bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1); |
| 92 | bool atEnd = (cropExtLen+1) < extsLen && |
| 93 | !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen+1)); |
| 94 | bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " "); |
Craig Donner | 4df76b2 | 2016-06-13 22:14:15 +0000 | [diff] [blame] | 95 | return equal || atStart || atEnd || inMiddle; |
Craig Donner | aec8697 | 2016-04-28 18:09:40 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static bool hasEglProtectedContent() { |
| 99 | // Only compute whether the extension is present once the first time this |
| 100 | // function is called. |
| 101 | static bool hasIt = hasEglProtectedContentImpl(); |
| 102 | return hasIt; |
| 103 | } |
| 104 | |
Jim Shargo | df578c3 | 2025-02-24 16:15:09 +0000 | [diff] [blame] | 105 | std::tuple<sp<GLConsumer>, sp<Surface>> GLConsumer::create(uint32_t tex, uint32_t textureTarget, |
| 106 | bool useFenceSync, |
| 107 | bool isControlledByApp) { |
| 108 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 109 | sp<GLConsumer> consumer = |
| 110 | sp<GLConsumer>::make(tex, textureTarget, useFenceSync, isControlledByApp); |
| 111 | return {consumer, consumer->getSurface()}; |
| 112 | #else |
| 113 | sp<IGraphicBufferProducer> igbp; |
| 114 | sp<IGraphicBufferConsumer> igbc; |
| 115 | BufferQueue::createBufferQueue(&igbp, &igbc); |
| 116 | |
| 117 | return {sp<GLConsumer>::make(igbc, tex, textureTarget, useFenceSync, isControlledByApp), |
| 118 | sp<Surface>::make(igbp, isControlledByApp)}; |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | std::tuple<sp<GLConsumer>, sp<Surface>> GLConsumer::create(uint32_t textureTarget, |
| 123 | bool useFenceSync, |
| 124 | bool isControlledByApp) { |
| 125 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 126 | sp<GLConsumer> consumer = sp<GLConsumer>::make(textureTarget, useFenceSync, isControlledByApp); |
| 127 | return {consumer, consumer->getSurface()}; |
| 128 | #else |
| 129 | sp<IGraphicBufferProducer> igbp; |
| 130 | sp<IGraphicBufferConsumer> igbc; |
| 131 | BufferQueue::createBufferQueue(&igbp, &igbc); |
| 132 | |
| 133 | return {sp<GLConsumer>::make(igbc, textureTarget, useFenceSync, isControlledByApp), |
| 134 | sp<Surface>::make(igbp, isControlledByApp)}; |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | sp<GLConsumer> GLConsumer::create(const sp<IGraphicBufferConsumer>& bq, uint32_t tex, |
| 139 | uint32_t textureTarget, bool useFenceSync, |
| 140 | bool isControlledByApp) { |
| 141 | return sp<GLConsumer>::make(bq, tex, textureTarget, useFenceSync, isControlledByApp); |
| 142 | } |
| 143 | |
| 144 | sp<GLConsumer> GLConsumer::create(const sp<IGraphicBufferConsumer>& bq, uint32_t textureTarget, |
| 145 | bool useFenceSync, bool isControlledByApp) { |
| 146 | return sp<GLConsumer>::make(bq, textureTarget, useFenceSync, isControlledByApp); |
| 147 | } |
| 148 | |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 149 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 150 | GLConsumer::GLConsumer(uint32_t tex, uint32_t texTarget, bool useFenceSync, bool isControlledByApp) |
| 151 | : ConsumerBase(isControlledByApp, /* isConsumerSurfaceFlinger */ false), |
| 152 | mCurrentCrop(Rect::EMPTY_RECT), |
| 153 | mCurrentTransform(0), |
| 154 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 155 | mCurrentFence(Fence::NO_FENCE), |
| 156 | mCurrentTimestamp(0), |
| 157 | mCurrentDataSpace(HAL_DATASPACE_UNKNOWN), |
| 158 | mCurrentFrameNumber(0), |
| 159 | mDefaultWidth(1), |
| 160 | mDefaultHeight(1), |
| 161 | mFilteringEnabled(true), |
| 162 | mTexName(tex), |
| 163 | mUseFenceSync(useFenceSync), |
| 164 | mTexTarget(texTarget), |
| 165 | mEglDisplay(EGL_NO_DISPLAY), |
| 166 | mEglContext(EGL_NO_CONTEXT), |
Jim Shargo | 4f9c275 | 2024-10-22 20:29:12 +0000 | [diff] [blame] | 167 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS) |
| 168 | mEglSlots(BufferQueueDefs::NUM_BUFFER_SLOTS), |
| 169 | #endif |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 170 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), |
| 171 | mAttached(true) { |
| 172 | GLC_LOGV("GLConsumer"); |
| 173 | |
| 174 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 175 | |
| 176 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 177 | } |
| 178 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 179 | |
Jim Shargo | 4f9c275 | 2024-10-22 20:29:12 +0000 | [diff] [blame] | 180 | GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex, uint32_t texTarget, |
| 181 | bool useFenceSync, bool isControlledByApp) |
| 182 | : ConsumerBase(bq, isControlledByApp), |
| 183 | mCurrentCrop(Rect::EMPTY_RECT), |
| 184 | mCurrentTransform(0), |
| 185 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 186 | mCurrentFence(Fence::NO_FENCE), |
| 187 | mCurrentTimestamp(0), |
| 188 | mCurrentDataSpace(HAL_DATASPACE_UNKNOWN), |
| 189 | mCurrentFrameNumber(0), |
| 190 | mDefaultWidth(1), |
| 191 | mDefaultHeight(1), |
| 192 | mFilteringEnabled(true), |
| 193 | mTexName(tex), |
| 194 | mUseFenceSync(useFenceSync), |
| 195 | mTexTarget(texTarget), |
| 196 | mEglDisplay(EGL_NO_DISPLAY), |
| 197 | mEglContext(EGL_NO_CONTEXT), |
| 198 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS) |
| 199 | mEglSlots(BufferQueueDefs::NUM_BUFFER_SLOTS), |
| 200 | #endif |
| 201 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), |
| 202 | mAttached(true) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 203 | GLC_LOGV("GLConsumer"); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 204 | |
Chia-I Wu | 3bd03ff | 2017-11-17 12:04:57 -0800 | [diff] [blame] | 205 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 206 | sizeof(mCurrentTransformMatrix)); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 207 | |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 208 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 211 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 212 | GLConsumer::GLConsumer(uint32_t texTarget, bool useFenceSync, bool isControlledByApp) |
| 213 | : ConsumerBase(isControlledByApp, /* isConsumerSurfaceFlinger */ false), |
| 214 | mCurrentCrop(Rect::EMPTY_RECT), |
| 215 | mCurrentTransform(0), |
| 216 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 217 | mCurrentFence(Fence::NO_FENCE), |
| 218 | mCurrentTimestamp(0), |
| 219 | mCurrentDataSpace(HAL_DATASPACE_UNKNOWN), |
| 220 | mCurrentFrameNumber(0), |
| 221 | mDefaultWidth(1), |
| 222 | mDefaultHeight(1), |
| 223 | mFilteringEnabled(true), |
| 224 | mTexName(0), |
| 225 | mUseFenceSync(useFenceSync), |
| 226 | mTexTarget(texTarget), |
| 227 | mEglDisplay(EGL_NO_DISPLAY), |
| 228 | mEglContext(EGL_NO_CONTEXT), |
Jim Shargo | 4f9c275 | 2024-10-22 20:29:12 +0000 | [diff] [blame] | 229 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS) |
| 230 | mEglSlots(BufferQueueDefs::NUM_BUFFER_SLOTS), |
| 231 | #endif |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 232 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), |
| 233 | mAttached(false) { |
| 234 | GLC_LOGV("GLConsumer"); |
| 235 | |
| 236 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 237 | |
| 238 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 239 | } |
| 240 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) |
| 241 | |
| 242 | GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget, bool useFenceSync, |
| 243 | bool isControlledByApp) |
| 244 | : ConsumerBase(bq, isControlledByApp), |
| 245 | mCurrentCrop(Rect::EMPTY_RECT), |
| 246 | mCurrentTransform(0), |
| 247 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 248 | mCurrentFence(Fence::NO_FENCE), |
| 249 | mCurrentTimestamp(0), |
| 250 | mCurrentDataSpace(HAL_DATASPACE_UNKNOWN), |
| 251 | mCurrentFrameNumber(0), |
| 252 | mDefaultWidth(1), |
| 253 | mDefaultHeight(1), |
| 254 | mFilteringEnabled(true), |
| 255 | mTexName(0), |
| 256 | mUseFenceSync(useFenceSync), |
| 257 | mTexTarget(texTarget), |
| 258 | mEglDisplay(EGL_NO_DISPLAY), |
| 259 | mEglContext(EGL_NO_CONTEXT), |
Jim Shargo | 4f9c275 | 2024-10-22 20:29:12 +0000 | [diff] [blame] | 260 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS) |
| 261 | mEglSlots(BufferQueueDefs::NUM_BUFFER_SLOTS), |
| 262 | #endif |
Jim Shargo | d30823a | 2024-07-27 02:49:39 +0000 | [diff] [blame] | 263 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), |
| 264 | mAttached(false) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 265 | GLC_LOGV("GLConsumer"); |
Dan Stoza | ab57491 | 2014-06-25 14:21:45 -0700 | [diff] [blame] | 266 | |
Chia-I Wu | 3bd03ff | 2017-11-17 12:04:57 -0800 | [diff] [blame] | 267 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), |
Dan Stoza | ab57491 | 2014-06-25 14:21:45 -0700 | [diff] [blame] | 268 | sizeof(mCurrentTransformMatrix)); |
| 269 | |
| 270 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 271 | } |
| 272 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 273 | status_t GLConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 274 | { |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 275 | Mutex::Autolock lock(mMutex); |
Pablo Ceballos | 65d9f6d | 2016-05-04 13:59:35 -0700 | [diff] [blame] | 276 | if (mAbandoned) { |
| 277 | GLC_LOGE("setDefaultBufferSize: GLConsumer is abandoned!"); |
| 278 | return NO_INIT; |
| 279 | } |
Daniel Lam | 016c8cb | 2012-04-03 15:54:58 -0700 | [diff] [blame] | 280 | mDefaultWidth = w; |
| 281 | mDefaultHeight = h; |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 282 | return mConsumer->setDefaultBufferSize(w, h); |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 285 | status_t GLConsumer::updateTexImage() { |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 286 | ATRACE_CALL(); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 287 | GLC_LOGV("updateTexImage"); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 288 | Mutex::Autolock lock(mMutex); |
| 289 | |
| 290 | if (mAbandoned) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 291 | GLC_LOGE("updateTexImage: GLConsumer is abandoned!"); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 292 | return NO_INIT; |
| 293 | } |
| 294 | |
| 295 | // Make sure the EGL state is the same as in previous calls. |
| 296 | status_t err = checkAndUpdateEglStateLocked(); |
| 297 | if (err != NO_ERROR) { |
| 298 | return err; |
| 299 | } |
| 300 | |
Dan Stoza | dd26416 | 2015-03-12 13:58:47 -0700 | [diff] [blame] | 301 | BufferItem item; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 302 | |
| 303 | // Acquire the next buffer. |
| 304 | // In asynchronous mode the list is guaranteed to be one buffer |
| 305 | // deep, while in synchronous mode we use the oldest buffer. |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 306 | err = acquireBufferLocked(&item, 0); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 307 | if (err != NO_ERROR) { |
| 308 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
| 309 | // We always bind the texture even if we don't update its contents. |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 310 | GLC_LOGV("updateTexImage: no buffers were available"); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 311 | glBindTexture(mTexTarget, mTexName); |
| 312 | err = NO_ERROR; |
| 313 | } else { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 314 | GLC_LOGE("updateTexImage: acquire failed: %s (%d)", |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 315 | strerror(-err), err); |
| 316 | } |
| 317 | return err; |
| 318 | } |
| 319 | |
| 320 | // Release the previous buffer. |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 321 | err = updateAndReleaseLocked(item); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 322 | if (err != NO_ERROR) { |
| 323 | // We always bind the texture. |
| 324 | glBindTexture(mTexTarget, mTexName); |
| 325 | return err; |
| 326 | } |
| 327 | |
Andy McFadden | 97eba89 | 2012-12-11 15:21:45 -0800 | [diff] [blame] | 328 | // Bind the new buffer to the GL texture, and wait until it's ready. |
| 329 | return bindTextureImageLocked(); |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 332 | |
| 333 | status_t GLConsumer::releaseTexImage() { |
| 334 | ATRACE_CALL(); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 335 | GLC_LOGV("releaseTexImage"); |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 336 | Mutex::Autolock lock(mMutex); |
| 337 | |
| 338 | if (mAbandoned) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 339 | GLC_LOGE("releaseTexImage: GLConsumer is abandoned!"); |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 340 | return NO_INIT; |
| 341 | } |
| 342 | |
| 343 | // Make sure the EGL state is the same as in previous calls. |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 344 | status_t err = NO_ERROR; |
| 345 | |
| 346 | if (mAttached) { |
| 347 | err = checkAndUpdateEglStateLocked(true); |
| 348 | if (err != NO_ERROR) { |
| 349 | return err; |
| 350 | } |
| 351 | } else { |
| 352 | // if we're detached, no need to validate EGL's state -- we won't use it. |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Update the GLConsumer state. |
| 356 | int buf = mCurrentTexture; |
| 357 | if (buf != BufferQueue::INVALID_BUFFER_SLOT) { |
| 358 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 359 | GLC_LOGV("releaseTexImage: (slot=%d, mAttached=%d)", buf, mAttached); |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 360 | |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 361 | if (mAttached) { |
| 362 | // Do whatever sync ops we need to do before releasing the slot. |
| 363 | err = syncForReleaseLocked(mEglDisplay); |
| 364 | if (err != NO_ERROR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 365 | GLC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err); |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 366 | return err; |
| 367 | } |
| 368 | } else { |
| 369 | // if we're detached, we just use the fence that was created in detachFromContext() |
| 370 | // so... basically, nothing more to do here. |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Jim Shargo | 5210808 | 2024-11-15 16:53:57 +0000 | [diff] [blame] | 373 | err = releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer); |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 374 | if (err < NO_ERROR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 375 | GLC_LOGE("releaseTexImage: failed to release buffer: %s (%d)", |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 376 | strerror(-err), err); |
| 377 | return err; |
| 378 | } |
| 379 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 380 | if (mReleasedTexImage == nullptr) { |
Anton Ivanov | 8ed8659 | 2025-02-20 11:52:50 -0800 | [diff] [blame] | 381 | mReleasedTexImage = sp<EglImage>::make(getDebugTexImageBuffer()); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 384 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 385 | mCurrentTextureImage = mReleasedTexImage; |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 386 | mCurrentCrop.makeInvalid(); |
| 387 | mCurrentTransform = 0; |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 388 | mCurrentTimestamp = 0; |
Courtney Goeltzenleuchter | bb09b43 | 2016-11-30 13:51:28 -0700 | [diff] [blame] | 389 | mCurrentDataSpace = HAL_DATASPACE_UNKNOWN; |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 390 | mCurrentFence = Fence::NO_FENCE; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 391 | mCurrentFenceTime = FenceTime::NO_FENCE; |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 392 | |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 393 | if (mAttached) { |
Xin Li | e8b4e70 | 2020-08-29 01:34:09 -0700 | [diff] [blame] | 394 | // This binds a buffer placeholder (mReleasedTexImage). |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 395 | status_t result = bindTextureImageLocked(); |
| 396 | if (result != NO_ERROR) { |
| 397 | return result; |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 398 | } |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 399 | } else { |
| 400 | // detached, don't touch the texture (and we may not even have an |
| 401 | // EGLDisplay here. |
| 402 | } |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | return NO_ERROR; |
| 406 | } |
| 407 | |
Mathias Agopian | 9870c9b | 2013-08-08 17:46:48 -0700 | [diff] [blame] | 408 | sp<GraphicBuffer> GLConsumer::getDebugTexImageBuffer() { |
| 409 | Mutex::Autolock _l(sStaticInitLock); |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 410 | if (CC_UNLIKELY(sReleasedTexImageBuffer == nullptr)) { |
Mathias Agopian | 9870c9b | 2013-08-08 17:46:48 -0700 | [diff] [blame] | 411 | // The first time, create the debug texture in case the application |
| 412 | // continues to use it. |
Anton Ivanov | 8ed8659 | 2025-02-20 11:52:50 -0800 | [diff] [blame] | 413 | sp<GraphicBuffer> buffer = |
| 414 | sp<GraphicBuffer>::make(kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888, |
| 415 | DEFAULT_USAGE_FLAGS | GraphicBuffer::USAGE_SW_WRITE_RARELY, |
| 416 | "[GLConsumer debug texture]"); |
Mathias Agopian | 9870c9b | 2013-08-08 17:46:48 -0700 | [diff] [blame] | 417 | uint32_t* bits; |
| 418 | buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits)); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 419 | uint32_t stride = buffer->getStride(); |
| 420 | uint32_t height = buffer->getHeight(); |
| 421 | memset(bits, 0, stride * height * 4); |
| 422 | for (uint32_t y = 0; y < kDebugData.height; y++) { |
| 423 | for (uint32_t x = 0; x < kDebugData.width; x++) { |
| 424 | bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ? |
| 425 | 0xFF000000 : 0xFFFFFFFF; |
Mathias Agopian | 9870c9b | 2013-08-08 17:46:48 -0700 | [diff] [blame] | 426 | } |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 427 | bits += stride; |
Mathias Agopian | 9870c9b | 2013-08-08 17:46:48 -0700 | [diff] [blame] | 428 | } |
| 429 | buffer->unlock(); |
| 430 | sReleasedTexImageBuffer = buffer; |
| 431 | } |
| 432 | return sReleasedTexImageBuffer; |
| 433 | } |
| 434 | |
Dan Stoza | dd26416 | 2015-03-12 13:58:47 -0700 | [diff] [blame] | 435 | status_t GLConsumer::acquireBufferLocked(BufferItem *item, |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 436 | nsecs_t presentWhen, uint64_t maxFrameNumber) { |
| 437 | status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, |
| 438 | maxFrameNumber); |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 439 | if (err != NO_ERROR) { |
| 440 | return err; |
| 441 | } |
| 442 | |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 443 | // If item->mGraphicBuffer is not null, this buffer has not been acquired |
| 444 | // before, so any prior EglImage created is using a stale buffer. This |
| 445 | // replaces any old EglImage with a new one (using the new buffer). |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 446 | if (item->mGraphicBuffer != nullptr) { |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 447 | int slot = item->mSlot; |
Anton Ivanov | 8ed8659 | 2025-02-20 11:52:50 -0800 | [diff] [blame] | 448 | mEglSlots[slot].mEglImage = sp<EglImage>::make(item->mGraphicBuffer); |
Jamie Gennis | dbe9245 | 2013-09-23 17:22:10 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 451 | return NO_ERROR; |
| 452 | } |
| 453 | |
Jim Shargo | 4f9c275 | 2024-10-22 20:29:12 +0000 | [diff] [blame] | 454 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS) |
| 455 | void GLConsumer::onSlotCountChanged(int slotCount) { |
| 456 | ConsumerBase::onSlotCountChanged(slotCount); |
| 457 | |
| 458 | Mutex::Autolock lock(mMutex); |
| 459 | if (slotCount > (int)mEglSlots.size()) { |
| 460 | mEglSlots.resize(slotCount); |
| 461 | } |
| 462 | } |
| 463 | #endif |
| 464 | |
Jim Shargo | defc49e | 2024-11-15 01:06:41 +0000 | [diff] [blame] | 465 | #if !COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
| 466 | status_t GLConsumer::releaseBufferLocked(int buf, sp<GraphicBuffer> graphicBuffer, |
| 467 | EGLDisplay display, EGLSyncKHR eglFence) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 468 | // release the buffer if it hasn't already been discarded by the |
| 469 | // BufferQueue. This can happen, for example, when the producer of this |
| 470 | // buffer has reallocated the original buffer slot after this buffer |
| 471 | // was acquired. |
Jim Shargo | defc49e | 2024-11-15 01:06:41 +0000 | [diff] [blame] | 472 | status_t err = ConsumerBase::releaseBufferLocked(buf, graphicBuffer, display, eglFence); |
Jamie Gennis | d1b330d | 2012-09-21 11:55:35 -0700 | [diff] [blame] | 473 | mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR; |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 474 | return err; |
| 475 | } |
Jim Shargo | defc49e | 2024-11-15 01:06:41 +0000 | [diff] [blame] | 476 | #endif |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 477 | |
Dan Stoza | 3ce4604 | 2015-11-17 17:00:45 -0800 | [diff] [blame] | 478 | status_t GLConsumer::updateAndReleaseLocked(const BufferItem& item, |
| 479 | PendingRelease* pendingRelease) |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 480 | { |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 481 | status_t err = NO_ERROR; |
| 482 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 483 | int slot = item.mSlot; |
Andy McFadden | 87a6784 | 2014-04-04 15:37:08 -0700 | [diff] [blame] | 484 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 485 | if (!mAttached) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 486 | GLC_LOGE("updateAndRelease: GLConsumer is not attached to an OpenGL " |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 487 | "ES context"); |
Jim Shargo | 5210808 | 2024-11-15 16:53:57 +0000 | [diff] [blame] | 488 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 489 | return INVALID_OPERATION; |
| 490 | } |
| 491 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 492 | // Confirm state. |
| 493 | err = checkAndUpdateEglStateLocked(); |
| 494 | if (err != NO_ERROR) { |
Jim Shargo | 5210808 | 2024-11-15 16:53:57 +0000 | [diff] [blame] | 495 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 496 | return err; |
| 497 | } |
| 498 | |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 499 | // Ensure we have a valid EglImageKHR for the slot, creating an EglImage |
| 500 | // if nessessary, for the gralloc buffer currently in the slot in |
| 501 | // ConsumerBase. |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 502 | // We may have to do this even when item.mGraphicBuffer == NULL (which |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 503 | // means the buffer was previously acquired). |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 504 | err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 505 | if (err != NO_ERROR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 506 | GLC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d", |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 507 | mEglDisplay, slot); |
Jim Shargo | 5210808 | 2024-11-15 16:53:57 +0000 | [diff] [blame] | 508 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 509 | return UNKNOWN_ERROR; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // Do whatever sync ops we need to do before releasing the old slot. |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 513 | if (slot != mCurrentTexture) { |
Pablo Ceballos | 33fcc2e | 2015-11-20 15:44:51 -0800 | [diff] [blame] | 514 | err = syncForReleaseLocked(mEglDisplay); |
| 515 | if (err != NO_ERROR) { |
| 516 | // Release the buffer we just acquired. It's not safe to |
| 517 | // release the old buffer, so instead we just drop the new frame. |
| 518 | // As we are still under lock since acquireBuffer, it is safe to |
| 519 | // release by slot. |
Jim Shargo | 5210808 | 2024-11-15 16:53:57 +0000 | [diff] [blame] | 520 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Pablo Ceballos | 33fcc2e | 2015-11-20 15:44:51 -0800 | [diff] [blame] | 521 | return err; |
| 522 | } |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 525 | GLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", |
Yi Kong | a03e044 | 2018-07-17 11:16:57 -0700 | [diff] [blame] | 526 | mCurrentTexture, mCurrentTextureImage != nullptr ? |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 527 | mCurrentTextureImage->graphicBufferHandle() : nullptr, |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 528 | slot, mSlots[slot].mGraphicBuffer->handle); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 529 | |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 530 | // Hang onto the pointer so that it isn't freed in the call to |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 531 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 532 | // the same. |
| 533 | sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage; |
| 534 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 535 | // release old buffer |
| 536 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
Dan Stoza | 3ce4604 | 2015-11-17 17:00:45 -0800 | [diff] [blame] | 537 | if (pendingRelease == nullptr) { |
Jim Shargo | defc49e | 2024-11-15 01:06:41 +0000 | [diff] [blame] | 538 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
| 539 | status_t status = |
| 540 | releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer()); |
| 541 | #else |
Dan Stoza | 3ce4604 | 2015-11-17 17:00:45 -0800 | [diff] [blame] | 542 | status_t status = releaseBufferLocked( |
| 543 | mCurrentTexture, mCurrentTextureImage->graphicBuffer(), |
| 544 | mEglDisplay, mEglSlots[mCurrentTexture].mEglFence); |
Jim Shargo | defc49e | 2024-11-15 01:06:41 +0000 | [diff] [blame] | 545 | #endif |
Dan Stoza | 3ce4604 | 2015-11-17 17:00:45 -0800 | [diff] [blame] | 546 | if (status < NO_ERROR) { |
| 547 | GLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", |
| 548 | strerror(-status), status); |
| 549 | err = status; |
| 550 | // keep going, with error raised [?] |
| 551 | } |
| 552 | } else { |
| 553 | pendingRelease->currentTexture = mCurrentTexture; |
Jim Shargo | defc49e | 2024-11-15 01:06:41 +0000 | [diff] [blame] | 554 | pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer(); |
Dan Stoza | 3ce4604 | 2015-11-17 17:00:45 -0800 | [diff] [blame] | 555 | pendingRelease->isPending = true; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 559 | // Update the GLConsumer state. |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 560 | mCurrentTexture = slot; |
Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 561 | mCurrentTextureImage = nextTextureImage; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 562 | mCurrentCrop = item.mCrop; |
| 563 | mCurrentTransform = item.mTransform; |
| 564 | mCurrentScalingMode = item.mScalingMode; |
| 565 | mCurrentTimestamp = item.mTimestamp; |
Courtney Goeltzenleuchter | bb09b43 | 2016-11-30 13:51:28 -0700 | [diff] [blame] | 566 | mCurrentDataSpace = item.mDataSpace; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 567 | mCurrentFence = item.mFence; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 568 | mCurrentFenceTime = item.mFenceTime; |
Eino-Ville Talvala | d171da9 | 2013-09-19 13:36:07 -0700 | [diff] [blame] | 569 | mCurrentFrameNumber = item.mFrameNumber; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 570 | |
| 571 | computeCurrentTransformMatrixLocked(); |
| 572 | |
| 573 | return err; |
| 574 | } |
| 575 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 576 | status_t GLConsumer::bindTextureImageLocked() { |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 577 | if (mEglDisplay == EGL_NO_DISPLAY) { |
| 578 | ALOGE("bindTextureImage: invalid display"); |
| 579 | return INVALID_OPERATION; |
| 580 | } |
| 581 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 582 | GLenum error; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 583 | while ((error = glGetError()) != GL_NO_ERROR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 584 | GLC_LOGW("bindTextureImage: clearing GL error: %#04x", error); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | glBindTexture(mTexTarget, mTexName); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 588 | if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 589 | mCurrentTextureImage == nullptr) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 590 | GLC_LOGE("bindTextureImage: no currently-bound texture"); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 591 | return NO_INIT; |
| 592 | } |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 593 | |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 594 | status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 595 | if (err != NO_ERROR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 596 | GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 597 | mEglDisplay, mCurrentTexture); |
| 598 | return UNKNOWN_ERROR; |
| 599 | } |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 600 | mCurrentTextureImage->bindToTextureTarget(mTexTarget); |
| 601 | |
Eric Penner | 2d14a0e | 2014-08-25 20:16:37 -0700 | [diff] [blame] | 602 | // In the rare case that the display is terminated and then initialized |
| 603 | // again, we can't detect that the display changed (it didn't), but the |
| 604 | // image is invalid. In this case, repeat the exact same steps while |
| 605 | // forcing the creation of a new image. |
| 606 | if ((error = glGetError()) != GL_NO_ERROR) { |
| 607 | glBindTexture(mTexTarget, mTexName); |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 608 | status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay, true); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 609 | if (result != NO_ERROR) { |
| 610 | GLC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", |
Eric Penner | 2d14a0e | 2014-08-25 20:16:37 -0700 | [diff] [blame] | 611 | mEglDisplay, mCurrentTexture); |
| 612 | return UNKNOWN_ERROR; |
| 613 | } |
| 614 | mCurrentTextureImage->bindToTextureTarget(mTexTarget); |
| 615 | if ((error = glGetError()) != GL_NO_ERROR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 616 | GLC_LOGE("bindTextureImage: error binding external image: %#04x", error); |
Eric Penner | 2d14a0e | 2014-08-25 20:16:37 -0700 | [diff] [blame] | 617 | return UNKNOWN_ERROR; |
| 618 | } |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 619 | } |
Andy McFadden | 97eba89 | 2012-12-11 15:21:45 -0800 | [diff] [blame] | 620 | |
| 621 | // Wait for the new buffer to be ready. |
| 622 | return doGLFenceWaitLocked(); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 625 | status_t GLConsumer::checkAndUpdateEglStateLocked(bool contextCheck) { |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 626 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 627 | EGLContext ctx = eglGetCurrentContext(); |
| 628 | |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 629 | if (!contextCheck) { |
| 630 | // if this is the first time we're called, mEglDisplay/mEglContext have |
| 631 | // never been set, so don't error out (below). |
| 632 | if (mEglDisplay == EGL_NO_DISPLAY) { |
| 633 | mEglDisplay = dpy; |
| 634 | } |
Jesse Hall | 46a1f6b | 2014-08-06 12:15:15 -0700 | [diff] [blame] | 635 | if (mEglContext == EGL_NO_CONTEXT) { |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 636 | mEglContext = ctx; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 641 | GLC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 642 | return INVALID_OPERATION; |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Mathias Agopian | 4515596 | 2013-08-08 18:16:21 -0700 | [diff] [blame] | 645 | if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 646 | GLC_LOGE("checkAndUpdateEglState: invalid current EGLContext"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 647 | return INVALID_OPERATION; |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | mEglDisplay = dpy; |
| 651 | mEglContext = ctx; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 652 | return NO_ERROR; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Jesse Hall | 13f01cb | 2013-03-20 11:37:21 -0700 | [diff] [blame] | 655 | void GLConsumer::setReleaseFence(const sp<Fence>& fence) { |
| 656 | if (fence->isValid() && |
| 657 | mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 658 | status_t err = addReleaseFence(mCurrentTexture, |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 659 | mCurrentTextureImage->graphicBuffer(), fence); |
Jesse Hall | 13f01cb | 2013-03-20 11:37:21 -0700 | [diff] [blame] | 660 | if (err != OK) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 661 | GLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", |
Jesse Hall | 13f01cb | 2013-03-20 11:37:21 -0700 | [diff] [blame] | 662 | strerror(-err), err); |
| 663 | } |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 667 | status_t GLConsumer::detachFromContext() { |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 668 | ATRACE_CALL(); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 669 | GLC_LOGV("detachFromContext"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 670 | Mutex::Autolock lock(mMutex); |
| 671 | |
| 672 | if (mAbandoned) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 673 | GLC_LOGE("detachFromContext: abandoned GLConsumer"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 674 | return NO_INIT; |
| 675 | } |
| 676 | |
| 677 | if (!mAttached) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 678 | GLC_LOGE("detachFromContext: GLConsumer is not attached to a " |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 679 | "context"); |
| 680 | return INVALID_OPERATION; |
| 681 | } |
| 682 | |
| 683 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 684 | EGLContext ctx = eglGetCurrentContext(); |
| 685 | |
| 686 | if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 687 | GLC_LOGE("detachFromContext: invalid current EGLDisplay"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 688 | return INVALID_OPERATION; |
| 689 | } |
| 690 | |
| 691 | if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 692 | GLC_LOGE("detachFromContext: invalid current EGLContext"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 693 | return INVALID_OPERATION; |
| 694 | } |
| 695 | |
| 696 | if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) { |
| 697 | status_t err = syncForReleaseLocked(dpy); |
| 698 | if (err != OK) { |
| 699 | return err; |
| 700 | } |
| 701 | |
| 702 | glDeleteTextures(1, &mTexName); |
| 703 | } |
| 704 | |
| 705 | mEglDisplay = EGL_NO_DISPLAY; |
| 706 | mEglContext = EGL_NO_CONTEXT; |
| 707 | mAttached = false; |
| 708 | |
| 709 | return OK; |
| 710 | } |
| 711 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 712 | status_t GLConsumer::attachToContext(uint32_t tex) { |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 713 | ATRACE_CALL(); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 714 | GLC_LOGV("attachToContext"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 715 | Mutex::Autolock lock(mMutex); |
| 716 | |
| 717 | if (mAbandoned) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 718 | GLC_LOGE("attachToContext: abandoned GLConsumer"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 719 | return NO_INIT; |
| 720 | } |
| 721 | |
| 722 | if (mAttached) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 723 | GLC_LOGE("attachToContext: GLConsumer is already attached to a " |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 724 | "context"); |
| 725 | return INVALID_OPERATION; |
| 726 | } |
| 727 | |
| 728 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 729 | EGLContext ctx = eglGetCurrentContext(); |
| 730 | |
| 731 | if (dpy == EGL_NO_DISPLAY) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 732 | GLC_LOGE("attachToContext: invalid current EGLDisplay"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 733 | return INVALID_OPERATION; |
| 734 | } |
| 735 | |
| 736 | if (ctx == EGL_NO_CONTEXT) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 737 | GLC_LOGE("attachToContext: invalid current EGLContext"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 738 | return INVALID_OPERATION; |
| 739 | } |
| 740 | |
| 741 | // We need to bind the texture regardless of whether there's a current |
| 742 | // buffer. |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 743 | glBindTexture(mTexTarget, GLuint(tex)); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 744 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 745 | mEglDisplay = dpy; |
| 746 | mEglContext = ctx; |
| 747 | mTexName = tex; |
| 748 | mAttached = true; |
| 749 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 750 | if (mCurrentTextureImage != nullptr) { |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 751 | // This may wait for a buffer a second time. This is likely required if |
| 752 | // this is a different context, since otherwise the wait could be skipped |
| 753 | // by bouncing through another context. For the same context the extra |
| 754 | // wait is redundant. |
| 755 | status_t err = bindTextureImageLocked(); |
| 756 | if (err != NO_ERROR) { |
| 757 | return err; |
| 758 | } |
| 759 | } |
| 760 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 761 | return OK; |
| 762 | } |
| 763 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 764 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 765 | status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 766 | GLC_LOGV("syncForReleaseLocked"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 767 | |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 768 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 769 | if (SyncFeatures::getInstance().useNativeFenceSync()) { |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 770 | EGLSyncKHR sync = eglCreateSyncKHR(dpy, |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 771 | EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 772 | if (sync == EGL_NO_SYNC_KHR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 773 | GLC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 774 | eglGetError()); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 775 | return UNKNOWN_ERROR; |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 776 | } |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 777 | glFlush(); |
| 778 | int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync); |
Jamie Gennis | 98ff059 | 2012-09-10 14:49:42 -0700 | [diff] [blame] | 779 | eglDestroySyncKHR(dpy, sync); |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 780 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 781 | GLC_LOGE("syncForReleaseLocked: error dup'ing native fence " |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 782 | "fd: %#x", eglGetError()); |
| 783 | return UNKNOWN_ERROR; |
| 784 | } |
Anton Ivanov | 8ed8659 | 2025-02-20 11:52:50 -0800 | [diff] [blame] | 785 | sp<Fence> fence = sp<Fence>::make(fenceFd); |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 786 | status_t err = addReleaseFenceLocked(mCurrentTexture, |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 787 | mCurrentTextureImage->graphicBuffer(), fence); |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 788 | if (err != OK) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 789 | GLC_LOGE("syncForReleaseLocked: error adding release fence: " |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 790 | "%s (%d)", strerror(-err), err); |
| 791 | return err; |
| 792 | } |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 793 | } else if (mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) { |
Jim Shargo | defc49e | 2024-11-15 01:06:41 +0000 | [diff] [blame] | 794 | #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
| 795 | // Basically all clients are using native fence syncs. If they aren't, we lose nothing |
| 796 | // by waiting here, because the alternative can cause deadlocks (b/339705065). |
| 797 | glFinish(); |
| 798 | #else |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 799 | EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence; |
| 800 | if (fence != EGL_NO_SYNC_KHR) { |
| 801 | // There is already a fence for the current slot. We need to |
| 802 | // wait on that before replacing it with another fence to |
| 803 | // ensure that all outstanding buffer accesses have completed |
| 804 | // before the producer accesses it. |
| 805 | EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000); |
| 806 | if (result == EGL_FALSE) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 807 | GLC_LOGE("syncForReleaseLocked: error waiting for previous " |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 808 | "fence: %#x", eglGetError()); |
| 809 | return UNKNOWN_ERROR; |
| 810 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 811 | GLC_LOGE("syncForReleaseLocked: timeout waiting for previous " |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 812 | "fence"); |
| 813 | return TIMED_OUT; |
| 814 | } |
| 815 | eglDestroySyncKHR(dpy, fence); |
| 816 | } |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 817 | |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 818 | // Create a fence for the outstanding accesses in the current |
| 819 | // OpenGL ES context. |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 820 | fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr); |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 821 | if (fence == EGL_NO_SYNC_KHR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 822 | GLC_LOGE("syncForReleaseLocked: error creating fence: %#x", |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 823 | eglGetError()); |
| 824 | return UNKNOWN_ERROR; |
| 825 | } |
| 826 | glFlush(); |
| 827 | mEglSlots[mCurrentTexture].mEglFence = fence; |
Jim Shargo | defc49e | 2024-11-15 01:06:41 +0000 | [diff] [blame] | 828 | #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_GL_FENCE_CLEANUP) |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 829 | } |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | return OK; |
| 833 | } |
| 834 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 835 | uint32_t GLConsumer::getCurrentTextureTarget() const { |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 836 | return mTexTarget; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 839 | void GLConsumer::getTransformMatrix(float mtx[16]) { |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 840 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 841 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 842 | } |
| 843 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 844 | void GLConsumer::setFilteringEnabled(bool enabled) { |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 845 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | e96e9e1 | 2012-09-24 19:26:11 -0700 | [diff] [blame] | 846 | if (mAbandoned) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 847 | GLC_LOGE("setFilteringEnabled: GLConsumer is abandoned!"); |
Mathias Agopian | e96e9e1 | 2012-09-24 19:26:11 -0700 | [diff] [blame] | 848 | return; |
| 849 | } |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 850 | bool needsRecompute = mFilteringEnabled != enabled; |
| 851 | mFilteringEnabled = enabled; |
Mathias Agopian | e96e9e1 | 2012-09-24 19:26:11 -0700 | [diff] [blame] | 852 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 853 | if (needsRecompute && mCurrentTextureImage==nullptr) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 854 | GLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == NULL"); |
Mathias Agopian | e96e9e1 | 2012-09-24 19:26:11 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 857 | if (needsRecompute && mCurrentTextureImage != nullptr) { |
Mathias Agopian | e96e9e1 | 2012-09-24 19:26:11 -0700 | [diff] [blame] | 858 | computeCurrentTransformMatrixLocked(); |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 862 | void GLConsumer::computeCurrentTransformMatrixLocked() { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 863 | GLC_LOGV("computeCurrentTransformMatrixLocked"); |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 864 | sp<GraphicBuffer> buf = (mCurrentTextureImage == nullptr) ? |
| 865 | nullptr : mCurrentTextureImage->graphicBuffer(); |
| 866 | if (buf == nullptr) { |
| 867 | GLC_LOGD("computeCurrentTransformMatrixLocked: " |
| 868 | "mCurrentTextureImage is NULL"); |
| 869 | } |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 870 | computeTransformMatrix(mCurrentTransformMatrix, buf, mCurrentCrop, |
John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 871 | mCurrentTransform, mFilteringEnabled); |
| 872 | } |
| 873 | |
Chia-I Wu | 93b122f | 2017-12-01 08:51:39 -0800 | [diff] [blame] | 874 | Rect GLConsumer::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight) { |
| 875 | Rect outCrop = crop; |
| 876 | |
| 877 | uint32_t newWidth = static_cast<uint32_t>(crop.width()); |
| 878 | uint32_t newHeight = static_cast<uint32_t>(crop.height()); |
| 879 | |
| 880 | if (newWidth * bufferHeight > newHeight * bufferWidth) { |
| 881 | newWidth = newHeight * bufferWidth / bufferHeight; |
| 882 | ALOGV("too wide: newWidth = %d", newWidth); |
| 883 | } else if (newWidth * bufferHeight < newHeight * bufferWidth) { |
| 884 | newHeight = newWidth * bufferHeight / bufferWidth; |
| 885 | ALOGV("too tall: newHeight = %d", newHeight); |
| 886 | } |
| 887 | |
| 888 | uint32_t currentWidth = static_cast<uint32_t>(crop.width()); |
| 889 | uint32_t currentHeight = static_cast<uint32_t>(crop.height()); |
| 890 | |
| 891 | // The crop is too wide |
| 892 | if (newWidth < currentWidth) { |
| 893 | uint32_t dw = currentWidth - newWidth; |
| 894 | auto halfdw = dw / 2; |
| 895 | outCrop.left += halfdw; |
| 896 | // Not halfdw because it would subtract 1 too few when dw is odd |
| 897 | outCrop.right -= (dw - halfdw); |
| 898 | // The crop is too tall |
| 899 | } else if (newHeight < currentHeight) { |
| 900 | uint32_t dh = currentHeight - newHeight; |
| 901 | auto halfdh = dh / 2; |
| 902 | outCrop.top += halfdh; |
| 903 | // Not halfdh because it would subtract 1 too few when dh is odd |
| 904 | outCrop.bottom -= (dh - halfdh); |
| 905 | } |
| 906 | |
| 907 | ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]", |
| 908 | outCrop.left, outCrop.top, |
| 909 | outCrop.right,outCrop.bottom); |
| 910 | |
| 911 | return outCrop; |
| 912 | } |
| 913 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 914 | nsecs_t GLConsumer::getTimestamp() { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 915 | GLC_LOGV("getTimestamp"); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 916 | Mutex::Autolock lock(mMutex); |
| 917 | return mCurrentTimestamp; |
| 918 | } |
| 919 | |
Courtney Goeltzenleuchter | bb09b43 | 2016-11-30 13:51:28 -0700 | [diff] [blame] | 920 | android_dataspace GLConsumer::getCurrentDataSpace() { |
| 921 | GLC_LOGV("getCurrentDataSpace"); |
| 922 | Mutex::Autolock lock(mMutex); |
| 923 | return mCurrentDataSpace; |
| 924 | } |
| 925 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 926 | uint64_t GLConsumer::getFrameNumber() { |
| 927 | GLC_LOGV("getFrameNumber"); |
Eino-Ville Talvala | d171da9 | 2013-09-19 13:36:07 -0700 | [diff] [blame] | 928 | Mutex::Autolock lock(mMutex); |
| 929 | return mCurrentFrameNumber; |
| 930 | } |
| 931 | |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 932 | sp<GraphicBuffer> GLConsumer::getCurrentBuffer(int* outSlot) const { |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 933 | Mutex::Autolock lock(mMutex); |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 934 | |
| 935 | if (outSlot != nullptr) { |
| 936 | *outSlot = mCurrentTexture; |
| 937 | } |
| 938 | |
| 939 | return (mCurrentTextureImage == nullptr) ? |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 940 | nullptr : mCurrentTextureImage->graphicBuffer(); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 943 | Rect GLConsumer::getCurrentCrop() const { |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 944 | Mutex::Autolock lock(mMutex); |
Chia-I Wu | 93b122f | 2017-12-01 08:51:39 -0800 | [diff] [blame] | 945 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
| 946 | ? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
| 947 | : mCurrentCrop; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 950 | uint32_t GLConsumer::getCurrentTransform() const { |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 951 | Mutex::Autolock lock(mMutex); |
| 952 | return mCurrentTransform; |
| 953 | } |
| 954 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 955 | uint32_t GLConsumer::getCurrentScalingMode() const { |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 956 | Mutex::Autolock lock(mMutex); |
| 957 | return mCurrentScalingMode; |
| 958 | } |
| 959 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 960 | sp<Fence> GLConsumer::getCurrentFence() const { |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 961 | Mutex::Autolock lock(mMutex); |
| 962 | return mCurrentFence; |
| 963 | } |
| 964 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 965 | std::shared_ptr<FenceTime> GLConsumer::getCurrentFenceTime() const { |
| 966 | Mutex::Autolock lock(mMutex); |
| 967 | return mCurrentFenceTime; |
| 968 | } |
| 969 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 970 | status_t GLConsumer::doGLFenceWaitLocked() const { |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 971 | |
| 972 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 973 | EGLContext ctx = eglGetCurrentContext(); |
| 974 | |
| 975 | if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 976 | GLC_LOGE("doGLFenceWait: invalid current EGLDisplay"); |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 977 | return INVALID_OPERATION; |
| 978 | } |
| 979 | |
| 980 | if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 981 | GLC_LOGE("doGLFenceWait: invalid current EGLContext"); |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 982 | return INVALID_OPERATION; |
| 983 | } |
| 984 | |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 985 | if (mCurrentFence->isValid()) { |
Jeffrey Kardatzke | a45e1c8 | 2018-03-13 12:07:28 -0700 | [diff] [blame] | 986 | if (SyncFeatures::getInstance().useWaitSync() && |
| 987 | SyncFeatures::getInstance().useNativeFenceSync()) { |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 988 | // Create an EGLSyncKHR from the current fence. |
| 989 | int fenceFd = mCurrentFence->dup(); |
| 990 | if (fenceFd == -1) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 991 | GLC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno); |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 992 | return -errno; |
| 993 | } |
| 994 | EGLint attribs[] = { |
| 995 | EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, |
| 996 | EGL_NONE |
| 997 | }; |
| 998 | EGLSyncKHR sync = eglCreateSyncKHR(dpy, |
| 999 | EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| 1000 | if (sync == EGL_NO_SYNC_KHR) { |
| 1001 | close(fenceFd); |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1002 | GLC_LOGE("doGLFenceWait: error creating EGL fence: %#x", |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 1003 | eglGetError()); |
| 1004 | return UNKNOWN_ERROR; |
| 1005 | } |
| 1006 | |
| 1007 | // XXX: The spec draft is inconsistent as to whether this should |
| 1008 | // return an EGLint or void. Ignore the return value for now, as |
| 1009 | // it's not strictly needed. |
Mathias Agopian | 2bb7168 | 2013-03-27 17:32:41 -0700 | [diff] [blame] | 1010 | eglWaitSyncKHR(dpy, sync, 0); |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 1011 | EGLint eglErr = eglGetError(); |
| 1012 | eglDestroySyncKHR(dpy, sync); |
| 1013 | if (eglErr != EGL_SUCCESS) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1014 | GLC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 1015 | eglErr); |
| 1016 | return UNKNOWN_ERROR; |
| 1017 | } |
| 1018 | } else { |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 1019 | status_t err = mCurrentFence->waitForever( |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 1020 | "GLConsumer::doGLFenceWaitLocked"); |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 1021 | if (err != NO_ERROR) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1022 | GLC_LOGE("doGLFenceWait: error waiting for fence: %d", err); |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 1023 | return err; |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | return NO_ERROR; |
| 1029 | } |
| 1030 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 1031 | void GLConsumer::freeBufferLocked(int slotIndex) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1032 | GLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 1033 | if (slotIndex == mCurrentTexture) { |
| 1034 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 1035 | } |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1036 | mEglSlots[slotIndex].mEglImage.clear(); |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 1037 | ConsumerBase::freeBufferLocked(slotIndex); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 1038 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 1039 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 1040 | void GLConsumer::abandonLocked() { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1041 | GLC_LOGV("abandonLocked"); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1042 | mCurrentTextureImage.clear(); |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 1043 | ConsumerBase::abandonLocked(); |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1046 | status_t GLConsumer::setConsumerUsageBits(uint64_t usage) { |
Chia-I Wu | a81bc49 | 2017-11-27 10:16:00 -0800 | [diff] [blame] | 1047 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 1050 | void GLConsumer::dumpLocked(String8& result, const char* prefix) const |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 1051 | { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 1052 | result.appendFormat( |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 1053 | "%smTexName=%d mCurrentTexture=%d\n" |
| 1054 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 1055 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 1056 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 1057 | mCurrentTransform); |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 1058 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 1059 | ConsumerBase::dumpLocked(result, prefix); |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1062 | GLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer) : |
| 1063 | mGraphicBuffer(graphicBuffer), |
| 1064 | mEglImage(EGL_NO_IMAGE_KHR), |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 1065 | mEglDisplay(EGL_NO_DISPLAY) { |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | GLConsumer::EglImage::~EglImage() { |
| 1069 | if (mEglImage != EGL_NO_IMAGE_KHR) { |
| 1070 | if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) { |
| 1071 | ALOGE("~EglImage: eglDestroyImageKHR failed"); |
| 1072 | } |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 1073 | DEBUG_EGL_IMAGE_TRACKER_DESTROY(); |
Michael Lentine | 78be65e | 2014-10-02 12:10:07 -0700 | [diff] [blame] | 1074 | eglTerminate(mEglDisplay); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | status_t GLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay, |
Eric Penner | 2d14a0e | 2014-08-25 20:16:37 -0700 | [diff] [blame] | 1079 | bool forceCreation) { |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1080 | // If there's an image and it's no longer valid, destroy it. |
| 1081 | bool haveImage = mEglImage != EGL_NO_IMAGE_KHR; |
| 1082 | bool displayInvalid = mEglDisplay != eglDisplay; |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 1083 | if (haveImage && (displayInvalid || forceCreation)) { |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1084 | if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) { |
| 1085 | ALOGE("createIfNeeded: eglDestroyImageKHR failed"); |
| 1086 | } |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 1087 | DEBUG_EGL_IMAGE_TRACKER_DESTROY(); |
Michael Lentine | 78be65e | 2014-10-02 12:10:07 -0700 | [diff] [blame] | 1088 | eglTerminate(mEglDisplay); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1089 | mEglImage = EGL_NO_IMAGE_KHR; |
| 1090 | mEglDisplay = EGL_NO_DISPLAY; |
| 1091 | } |
| 1092 | |
| 1093 | // If there's no image, create one. |
| 1094 | if (mEglImage == EGL_NO_IMAGE_KHR) { |
| 1095 | mEglDisplay = eglDisplay; |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 1096 | mEglImage = createImage(mEglDisplay, mGraphicBuffer); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | // Fail if we can't create a valid image. |
| 1100 | if (mEglImage == EGL_NO_IMAGE_KHR) { |
| 1101 | mEglDisplay = EGL_NO_DISPLAY; |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1102 | const sp<GraphicBuffer>& buffer = mGraphicBuffer; |
Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1103 | ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d", |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1104 | buffer->getWidth(), buffer->getHeight(), buffer->getStride(), |
| 1105 | buffer->getUsage(), buffer->getPixelFormat()); |
| 1106 | return UNKNOWN_ERROR; |
| 1107 | } |
| 1108 | |
| 1109 | return OK; |
| 1110 | } |
| 1111 | |
| 1112 | void GLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1113 | glEGLImageTargetTexture2DOES(texTarget, |
| 1114 | static_cast<GLeglImageOES>(mEglImage)); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | EGLImageKHR GLConsumer::EglImage::createImage(EGLDisplay dpy, |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 1118 | const sp<GraphicBuffer>& graphicBuffer) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1119 | EGLClientBuffer cbuf = |
| 1120 | static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer()); |
Craig Donner | aec8697 | 2016-04-28 18:09:40 -0700 | [diff] [blame] | 1121 | const bool createProtectedImage = |
| 1122 | (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) && |
| 1123 | hasEglProtectedContent(); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1124 | EGLint attrs[] = { |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 1125 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
Craig Donner | aec8697 | 2016-04-28 18:09:40 -0700 | [diff] [blame] | 1126 | createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE, |
| 1127 | createProtectedImage ? EGL_TRUE : EGL_NONE, |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1128 | EGL_NONE, |
| 1129 | }; |
Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1130 | eglInitialize(dpy, nullptr, nullptr); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1131 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 1132 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
| 1133 | if (image == EGL_NO_IMAGE_KHR) { |
| 1134 | EGLint error = eglGetError(); |
| 1135 | ALOGE("error creating EGLImage: %#x", error); |
Michael Lentine | 78be65e | 2014-10-02 12:10:07 -0700 | [diff] [blame] | 1136 | eglTerminate(dpy); |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 1137 | } else { |
| 1138 | DEBUG_EGL_IMAGE_TRACKER_CREATE(); |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1139 | } |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 1140 | |
Eric Penner | 5c3d243 | 2014-07-11 19:08:04 -0700 | [diff] [blame] | 1141 | return image; |
| 1142 | } |
| 1143 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1144 | }; // namespace android |