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