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