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