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