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