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