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