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