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