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