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