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