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 | |
| 17 | #define LOG_TAG "SurfaceTexture" |
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> |
| 28 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 29 | #include <hardware/hardware.h> |
| 30 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 31 | #include <gui/IGraphicBufferAlloc.h> |
| 32 | #include <gui/ISurfaceComposer.h> |
| 33 | #include <gui/SurfaceComposerClient.h> |
| 34 | #include <gui/SurfaceTexture.h> |
Mathias Agopian | 41f673c | 2011-11-17 17:48:35 -0800 | [diff] [blame] | 35 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 36 | #include <private/gui/ComposerService.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 37 | |
| 38 | #include <utils/Log.h> |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 39 | #include <utils/String8.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 40 | #include <utils/Trace.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 41 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 42 | // This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension |
| 43 | // to synchronize access to the buffers. It will cause dequeueBuffer to stall, |
| 44 | // waiting for the GL reads for the buffer being dequeued to complete before |
| 45 | // allowing the buffer to be dequeued. |
| 46 | #ifdef USE_FENCE_SYNC |
| 47 | #ifdef ALLOW_DEQUEUE_CURRENT_BUFFER |
| 48 | #error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible" |
| 49 | #endif |
| 50 | #endif |
| 51 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 52 | // Macros for including the SurfaceTexture name in log messages |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 53 | #define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 54 | #define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 55 | #define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 56 | #define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 57 | #define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__) |
Mathias Agopian | 29b5762 | 2011-08-17 15:42:04 -0700 | [diff] [blame] | 58 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 59 | namespace android { |
| 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 | }; |
| 86 | static float mtxRot180[16] = { |
| 87 | -1, 0, 0, 0, |
| 88 | 0, -1, 0, 0, |
| 89 | 0, 0, 1, 0, |
| 90 | 1, 1, 0, 1, |
| 91 | }; |
| 92 | static float mtxRot270[16] = { |
| 93 | 0, -1, 0, 0, |
| 94 | 1, 0, 0, 0, |
| 95 | 0, 0, 1, 0, |
| 96 | 0, 1, 0, 1, |
| 97 | }; |
| 98 | |
| 99 | static void mtxMul(float out[16], const float a[16], const float b[16]); |
| 100 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 101 | |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 102 | SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode, |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 103 | GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) : |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 104 | ConsumerBase(bufferQueue == 0 ? new BufferQueue(allowSynchronousMode) : bufferQueue), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 105 | mCurrentTransform(0), |
| 106 | mCurrentTimestamp(0), |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 107 | mFilteringEnabled(true), |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 108 | mTexName(tex), |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 109 | #ifdef USE_FENCE_SYNC |
| 110 | mUseFenceSync(useFenceSync), |
| 111 | #else |
| 112 | mUseFenceSync(false), |
| 113 | #endif |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 114 | mTexTarget(texTarget), |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 115 | mEglDisplay(EGL_NO_DISPLAY), |
| 116 | mEglContext(EGL_NO_CONTEXT), |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 117 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), |
| 118 | mAttached(true) |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 119 | { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 120 | ST_LOGV("SurfaceTexture"); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 121 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 122 | memcpy(mCurrentTransformMatrix, mtxIdentity, |
| 123 | sizeof(mCurrentTransformMatrix)); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 124 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 125 | mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 128 | status_t SurfaceTexture::setDefaultMaxBufferCount(int bufferCount) { |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 129 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 130 | return mBufferQueue->setDefaultMaxBufferCount(bufferCount); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 133 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 134 | status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h) |
| 135 | { |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 136 | Mutex::Autolock lock(mMutex); |
Daniel Lam | 016c8cb | 2012-04-03 15:54:58 -0700 | [diff] [blame] | 137 | mDefaultWidth = w; |
| 138 | mDefaultHeight = h; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 139 | return mBufferQueue->setDefaultBufferSize(w, h); |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 142 | status_t SurfaceTexture::updateTexImage() { |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 143 | return SurfaceTexture::updateTexImage(NULL); |
| 144 | } |
| 145 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 146 | status_t SurfaceTexture::acquireBufferLocked(BufferQueue::BufferItem *item) { |
| 147 | status_t err = ConsumerBase::acquireBufferLocked(item); |
| 148 | if (err != NO_ERROR) { |
| 149 | return err; |
| 150 | } |
| 151 | |
| 152 | int slot = item->mBuf; |
| 153 | if (item->mGraphicBuffer != NULL) { |
| 154 | if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) { |
| 155 | eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage); |
| 156 | mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Update the GL texture object. We may have to do this even when |
| 161 | // item.mGraphicBuffer == NULL, if we destroyed the EGLImage when |
| 162 | // detaching from a context but the buffer has not been re-allocated. |
| 163 | if (mEglSlots[slot].mEglImage == EGL_NO_IMAGE_KHR) { |
| 164 | EGLImageKHR image = createImage(mEglDisplay, mSlots[slot].mGraphicBuffer); |
| 165 | if (image == EGL_NO_IMAGE_KHR) { |
| 166 | return UNKNOWN_ERROR; |
| 167 | } |
| 168 | mEglSlots[slot].mEglImage = image; |
| 169 | } |
| 170 | |
| 171 | return NO_ERROR; |
| 172 | } |
| 173 | |
| 174 | status_t SurfaceTexture::releaseBufferLocked(int buf, EGLDisplay display, |
| 175 | EGLSyncKHR eglFence, const sp<Fence>& fence) { |
| 176 | status_t err = ConsumerBase::releaseBufferLocked(buf, mEglDisplay, |
| 177 | eglFence, fence); |
| 178 | |
| 179 | mEglSlots[mCurrentTexture].mEglFence = EGL_NO_SYNC_KHR; |
| 180 | |
| 181 | return err; |
| 182 | } |
| 183 | |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 184 | status_t SurfaceTexture::updateTexImage(BufferRejecter* rejecter) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 185 | ATRACE_CALL(); |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 186 | ST_LOGV("updateTexImage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 187 | Mutex::Autolock lock(mMutex); |
| 188 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 189 | status_t err = NO_ERROR; |
| 190 | |
Mathias Agopian | e47498f | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 191 | if (mAbandoned) { |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 192 | ST_LOGE("updateTexImage: SurfaceTexture is abandoned!"); |
Mathias Agopian | 8e19c2e | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 193 | return NO_INIT; |
Mathias Agopian | e47498f | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 196 | if (!mAttached) { |
| 197 | ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL " |
| 198 | "ES context"); |
| 199 | return INVALID_OPERATION; |
| 200 | } |
| 201 | |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 202 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 203 | EGLContext ctx = eglGetCurrentContext(); |
| 204 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 205 | if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) || |
| 206 | dpy == EGL_NO_DISPLAY) { |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 207 | ST_LOGE("updateTexImage: invalid current EGLDisplay"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 208 | return INVALID_OPERATION; |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 211 | if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) || |
| 212 | ctx == EGL_NO_CONTEXT) { |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 213 | ST_LOGE("updateTexImage: invalid current EGLContext"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 214 | return INVALID_OPERATION; |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | mEglDisplay = dpy; |
| 218 | mEglContext = ctx; |
| 219 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 220 | BufferQueue::BufferItem item; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 221 | |
Jamie Gennis | 50c4efc | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 222 | // In asynchronous mode the list is guaranteed to be one buffer |
| 223 | // deep, while in synchronous mode we use the oldest buffer. |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 224 | err = acquireBufferLocked(&item); |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 225 | if (err == NO_ERROR) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 226 | int buf = item.mBuf; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 227 | |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 228 | // we call the rejecter here, in case the caller has a reason to |
| 229 | // not accept this buffer. this is used by SurfaceFlinger to |
| 230 | // reject buffers which have the wrong size |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 231 | if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) { |
| 232 | releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR, item.mFence); |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 233 | glBindTexture(mTexTarget, mTexName); |
| 234 | return NO_ERROR; |
| 235 | } |
| 236 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 237 | GLint error; |
| 238 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 239 | ST_LOGW("updateTexImage: clearing GL error: %#04x", error); |
| 240 | } |
| 241 | |
| 242 | EGLImageKHR image = mEglSlots[buf].mEglImage; |
| 243 | glBindTexture(mTexTarget, mTexName); |
| 244 | glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image); |
| 245 | |
| 246 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 247 | ST_LOGE("updateTexImage: error binding external texture image %p " |
| 248 | "(slot %d): %#04x", image, buf, error); |
| 249 | err = UNKNOWN_ERROR; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 250 | } |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 251 | |
Mathias Agopian | 7e477bf | 2012-05-18 16:50:58 -0700 | [diff] [blame] | 252 | if (err == NO_ERROR) { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 253 | err = syncForReleaseLocked(dpy); |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 254 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 255 | |
Mathias Agopian | 7e477bf | 2012-05-18 16:50:58 -0700 | [diff] [blame] | 256 | if (err != NO_ERROR) { |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 257 | // Release the buffer we just acquired. It's not safe to |
| 258 | // release the old buffer, so instead we just drop the new frame. |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 259 | releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR, item.mFence); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 260 | return err; |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", |
| 264 | mCurrentTexture, |
| 265 | mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 266 | buf, mSlots[buf].mGraphicBuffer->handle); |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 267 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 268 | // release old buffer |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 269 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 270 | status_t status = releaseBufferLocked(mCurrentTexture, dpy, |
| 271 | mEglSlots[mCurrentTexture].mEglFence, |
| 272 | mSlots[mCurrentTexture].mFence); |
| 273 | if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) { |
| 274 | ST_LOGE("updateTexImage: failed to release buffer: %s (%d)", |
| 275 | strerror(-status), status); |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 276 | err = status; |
| 277 | } |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 278 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 279 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 280 | // Update the SurfaceTexture state. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 281 | mCurrentTexture = buf; |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 282 | mCurrentTextureBuf = mSlots[buf].mGraphicBuffer; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 283 | mCurrentCrop = item.mCrop; |
| 284 | mCurrentTransform = item.mTransform; |
| 285 | mCurrentScalingMode = item.mScalingMode; |
| 286 | mCurrentTimestamp = item.mTimestamp; |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 287 | mCurrentFence = item.mFence; |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 288 | computeCurrentTransformMatrix(); |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 289 | } else { |
| 290 | if (err < 0) { |
Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame^] | 291 | ST_LOGE("updateTexImage: acquire failed: %s (%d)", |
| 292 | strerror(-err), err); |
| 293 | return err; |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 294 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 295 | // We always bind the texture even if we don't update its contents. |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 296 | glBindTexture(mTexTarget, mTexName); |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 297 | return OK; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 298 | } |
Jamie Gennis | 50c4efc | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 299 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 300 | return err; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 303 | void SurfaceTexture::setReleaseFence(int fenceFd) { |
Jamie Gennis | 3d1d09c | 2012-08-08 15:39:55 -0700 | [diff] [blame] | 304 | sp<Fence> fence(new Fence(fenceFd)); |
Jamie Gennis | 45cb2ba | 2012-08-06 17:10:57 -0700 | [diff] [blame] | 305 | if (fenceFd == -1 || mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 306 | return; |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 307 | if (!mSlots[mCurrentTexture].mFence.get()) { |
| 308 | mSlots[mCurrentTexture].mFence = fence; |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 309 | } else { |
| 310 | sp<Fence> mergedFence = Fence::merge( |
| 311 | String8("SurfaceTexture merged release"), |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 312 | mSlots[mCurrentTexture].mFence, fence); |
Jesse Hall | e7db724 | 2012-07-03 13:41:54 -0700 | [diff] [blame] | 313 | if (!mergedFence.get()) { |
Jamie Gennis | 45cb2ba | 2012-08-06 17:10:57 -0700 | [diff] [blame] | 314 | ST_LOGE("failed to merge release fences"); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 315 | // synchronization is broken, the best we can do is hope fences |
| 316 | // signal in order so the new fence will act like a union |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 317 | mSlots[mCurrentTexture].mFence = fence; |
Jesse Hall | e7db724 | 2012-07-03 13:41:54 -0700 | [diff] [blame] | 318 | return; |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 319 | } |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 320 | mSlots[mCurrentTexture].mFence = mergedFence; |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 324 | status_t SurfaceTexture::detachFromContext() { |
| 325 | ATRACE_CALL(); |
| 326 | ST_LOGV("detachFromContext"); |
| 327 | Mutex::Autolock lock(mMutex); |
| 328 | |
| 329 | if (mAbandoned) { |
| 330 | ST_LOGE("detachFromContext: abandoned SurfaceTexture"); |
| 331 | return NO_INIT; |
| 332 | } |
| 333 | |
| 334 | if (!mAttached) { |
| 335 | ST_LOGE("detachFromContext: SurfaceTexture is not attached to a " |
| 336 | "context"); |
| 337 | return INVALID_OPERATION; |
| 338 | } |
| 339 | |
| 340 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 341 | EGLContext ctx = eglGetCurrentContext(); |
| 342 | |
| 343 | if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) { |
| 344 | ST_LOGE("detachFromContext: invalid current EGLDisplay"); |
| 345 | return INVALID_OPERATION; |
| 346 | } |
| 347 | |
| 348 | if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) { |
| 349 | ST_LOGE("detachFromContext: invalid current EGLContext"); |
| 350 | return INVALID_OPERATION; |
| 351 | } |
| 352 | |
| 353 | if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) { |
| 354 | status_t err = syncForReleaseLocked(dpy); |
| 355 | if (err != OK) { |
| 356 | return err; |
| 357 | } |
| 358 | |
| 359 | glDeleteTextures(1, &mTexName); |
| 360 | } |
| 361 | |
Jamie Gennis | 9aa74db | 2012-04-17 13:07:45 -0700 | [diff] [blame] | 362 | // Because we're giving up the EGLDisplay we need to free all the EGLImages |
| 363 | // that are associated with it. They'll be recreated when the |
| 364 | // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a |
| 365 | // new EGLDisplay). |
| 366 | for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 367 | EGLImageKHR img = mEglSlots[i].mEglImage; |
Jamie Gennis | 5c8a608 | 2012-05-02 13:10:56 -0700 | [diff] [blame] | 368 | if (img != EGL_NO_IMAGE_KHR) { |
Jamie Gennis | 9aa74db | 2012-04-17 13:07:45 -0700 | [diff] [blame] | 369 | eglDestroyImageKHR(mEglDisplay, img); |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 370 | mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
Jamie Gennis | 9aa74db | 2012-04-17 13:07:45 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 374 | mEglDisplay = EGL_NO_DISPLAY; |
| 375 | mEglContext = EGL_NO_CONTEXT; |
| 376 | mAttached = false; |
| 377 | |
| 378 | return OK; |
| 379 | } |
| 380 | |
| 381 | status_t SurfaceTexture::attachToContext(GLuint tex) { |
| 382 | ATRACE_CALL(); |
| 383 | ST_LOGV("attachToContext"); |
| 384 | Mutex::Autolock lock(mMutex); |
| 385 | |
| 386 | if (mAbandoned) { |
| 387 | ST_LOGE("attachToContext: abandoned SurfaceTexture"); |
| 388 | return NO_INIT; |
| 389 | } |
| 390 | |
| 391 | if (mAttached) { |
| 392 | ST_LOGE("attachToContext: SurfaceTexture is already attached to a " |
| 393 | "context"); |
| 394 | return INVALID_OPERATION; |
| 395 | } |
| 396 | |
| 397 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 398 | EGLContext ctx = eglGetCurrentContext(); |
| 399 | |
| 400 | if (dpy == EGL_NO_DISPLAY) { |
| 401 | ST_LOGE("attachToContext: invalid current EGLDisplay"); |
| 402 | return INVALID_OPERATION; |
| 403 | } |
| 404 | |
| 405 | if (ctx == EGL_NO_CONTEXT) { |
| 406 | ST_LOGE("attachToContext: invalid current EGLContext"); |
| 407 | return INVALID_OPERATION; |
| 408 | } |
| 409 | |
| 410 | // We need to bind the texture regardless of whether there's a current |
| 411 | // buffer. |
| 412 | glBindTexture(mTexTarget, tex); |
| 413 | |
| 414 | if (mCurrentTextureBuf != NULL) { |
Jamie Gennis | 5c8a608 | 2012-05-02 13:10:56 -0700 | [diff] [blame] | 415 | // The EGLImageKHR that was associated with the slot was destroyed when |
| 416 | // the SurfaceTexture was detached from the old context, so we need to |
| 417 | // recreate it here. |
| 418 | EGLImageKHR image = createImage(dpy, mCurrentTextureBuf); |
| 419 | if (image == EGL_NO_IMAGE_KHR) { |
| 420 | return UNKNOWN_ERROR; |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // Attach the current buffer to the GL texture. |
| 424 | glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image); |
| 425 | |
| 426 | GLint error; |
| 427 | status_t err = OK; |
| 428 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 429 | ST_LOGE("attachToContext: error binding external texture image %p " |
| 430 | "(slot %d): %#04x", image, mCurrentTexture, error); |
| 431 | err = UNKNOWN_ERROR; |
| 432 | } |
| 433 | |
Jamie Gennis | 5c8a608 | 2012-05-02 13:10:56 -0700 | [diff] [blame] | 434 | // We destroy the EGLImageKHR here because the current buffer may no |
| 435 | // longer be associated with one of the buffer slots, so we have |
| 436 | // nowhere to to store it. If the buffer is still associated with a |
| 437 | // slot then another EGLImageKHR will be created next time that buffer |
| 438 | // gets acquired in updateTexImage. |
| 439 | eglDestroyImageKHR(dpy, image); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 440 | |
| 441 | if (err != OK) { |
| 442 | return err; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | mEglDisplay = dpy; |
| 447 | mEglContext = ctx; |
| 448 | mTexName = tex; |
| 449 | mAttached = true; |
| 450 | |
| 451 | return OK; |
| 452 | } |
| 453 | |
| 454 | status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) { |
| 455 | ST_LOGV("syncForReleaseLocked"); |
| 456 | |
| 457 | if (mUseFenceSync && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 458 | EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence; |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 459 | if (fence != EGL_NO_SYNC_KHR) { |
| 460 | // There is already a fence for the current slot. We need to wait |
| 461 | // on that before replacing it with another fence to ensure that all |
| 462 | // outstanding buffer accesses have completed before the producer |
| 463 | // accesses it. |
| 464 | EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000); |
| 465 | if (result == EGL_FALSE) { |
| 466 | ST_LOGE("syncForReleaseLocked: error waiting for previous " |
| 467 | "fence: %#x", eglGetError()); |
| 468 | return UNKNOWN_ERROR; |
| 469 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 470 | ST_LOGE("syncForReleaseLocked: timeout waiting for previous " |
| 471 | "fence"); |
| 472 | return TIMED_OUT; |
| 473 | } |
| 474 | eglDestroySyncKHR(dpy, fence); |
| 475 | } |
| 476 | |
| 477 | // Create a fence for the outstanding accesses in the current OpenGL ES |
| 478 | // context. |
| 479 | fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL); |
| 480 | if (fence == EGL_NO_SYNC_KHR) { |
| 481 | ST_LOGE("syncForReleaseLocked: error creating fence: %#x", |
| 482 | eglGetError()); |
| 483 | return UNKNOWN_ERROR; |
| 484 | } |
| 485 | glFlush(); |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 486 | mEglSlots[mCurrentTexture].mEglFence = fence; |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | return OK; |
| 490 | } |
| 491 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 492 | bool SurfaceTexture::isExternalFormat(uint32_t format) |
| 493 | { |
| 494 | switch (format) { |
| 495 | // supported YUV formats |
| 496 | case HAL_PIXEL_FORMAT_YV12: |
| 497 | // Legacy/deprecated YUV formats |
| 498 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 499 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 500 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | // Any OEM format needs to be considered |
| 505 | if (format>=0x100 && format<=0x1FF) |
| 506 | return true; |
| 507 | |
| 508 | return false; |
| 509 | } |
| 510 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 511 | GLenum SurfaceTexture::getCurrentTextureTarget() const { |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 512 | return mTexTarget; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 515 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 516 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 517 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 518 | } |
| 519 | |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 520 | void SurfaceTexture::setFilteringEnabled(bool enabled) { |
| 521 | Mutex::Autolock lock(mMutex); |
| 522 | bool needsRecompute = mFilteringEnabled != enabled; |
| 523 | mFilteringEnabled = enabled; |
| 524 | if (needsRecompute) { |
| 525 | computeCurrentTransformMatrix(); |
| 526 | } |
| 527 | } |
| 528 | |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 529 | void SurfaceTexture::computeCurrentTransformMatrix() { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 530 | ST_LOGV("computeCurrentTransformMatrix"); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 531 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 532 | float xform[16]; |
| 533 | for (int i = 0; i < 16; i++) { |
| 534 | xform[i] = mtxIdentity[i]; |
| 535 | } |
| 536 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 537 | float result[16]; |
| 538 | mtxMul(result, xform, mtxFlipH); |
| 539 | for (int i = 0; i < 16; i++) { |
| 540 | xform[i] = result[i]; |
| 541 | } |
| 542 | } |
| 543 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 544 | float result[16]; |
| 545 | mtxMul(result, xform, mtxFlipV); |
| 546 | for (int i = 0; i < 16; i++) { |
| 547 | xform[i] = result[i]; |
| 548 | } |
| 549 | } |
| 550 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 551 | float result[16]; |
| 552 | mtxMul(result, xform, mtxRot90); |
| 553 | for (int i = 0; i < 16; i++) { |
| 554 | xform[i] = result[i]; |
| 555 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 558 | sp<GraphicBuffer>& buf(mCurrentTextureBuf); |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame] | 559 | Rect cropRect = mCurrentCrop; |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 560 | float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f; |
| 561 | float bufferWidth = buf->getWidth(); |
| 562 | float bufferHeight = buf->getHeight(); |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame] | 563 | if (!cropRect.isEmpty()) { |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 564 | float shrinkAmount = 0.0f; |
| 565 | if (mFilteringEnabled) { |
| 566 | // In order to prevent bilinear sampling beyond the edge of the |
| 567 | // crop rectangle we may need to shrink it by 2 texels in each |
| 568 | // dimension. Normally this would just need to take 1/2 a texel |
| 569 | // off each end, but because the chroma channels of YUV420 images |
| 570 | // are subsampled we may need to shrink the crop region by a whole |
| 571 | // texel on each side. |
| 572 | switch (buf->getPixelFormat()) { |
| 573 | case PIXEL_FORMAT_RGBA_8888: |
| 574 | case PIXEL_FORMAT_RGBX_8888: |
| 575 | case PIXEL_FORMAT_RGB_888: |
| 576 | case PIXEL_FORMAT_RGB_565: |
| 577 | case PIXEL_FORMAT_BGRA_8888: |
| 578 | case PIXEL_FORMAT_RGBA_5551: |
| 579 | case PIXEL_FORMAT_RGBA_4444: |
| 580 | // We know there's no subsampling of any channels, so we |
| 581 | // only need to shrink by a half a pixel. |
| 582 | shrinkAmount = 0.5; |
Romain Guy | 4f9c284 | 2012-08-01 19:16:59 -0700 | [diff] [blame] | 583 | break; |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 584 | |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 585 | default: |
| 586 | // If we don't recognize the format, we must assume the |
| 587 | // worst case (that we care about), which is YUV420. |
| 588 | shrinkAmount = 1.0; |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 589 | break; |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame] | 592 | |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 593 | // Only shrink the dimensions that are not the size of the buffer. |
| 594 | if (cropRect.width() < bufferWidth) { |
| 595 | tx = (float(cropRect.left) + shrinkAmount) / bufferWidth; |
| 596 | sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) / |
| 597 | bufferWidth; |
| 598 | } |
| 599 | if (cropRect.height() < bufferHeight) { |
| 600 | ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) / |
| 601 | bufferHeight; |
| 602 | sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) / |
| 603 | bufferHeight; |
| 604 | } |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 605 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 606 | float crop[16] = { |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 607 | sx, 0, 0, 0, |
| 608 | 0, sy, 0, 0, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 609 | 0, 0, 1, 0, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 610 | tx, ty, 0, 1, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 611 | }; |
| 612 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 613 | float mtxBeforeFlipV[16]; |
| 614 | mtxMul(mtxBeforeFlipV, crop, xform); |
| 615 | |
| 616 | // SurfaceFlinger expects the top of its window textures to be at a Y |
| 617 | // coordinate of 0, so SurfaceTexture must behave the same way. We don't |
| 618 | // want to expose this to applications, however, so we must add an |
| 619 | // additional vertical flip to the transform after all the other transforms. |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 620 | mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 623 | nsecs_t SurfaceTexture::getTimestamp() { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 624 | ST_LOGV("getTimestamp"); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 625 | Mutex::Autolock lock(mMutex); |
| 626 | return mCurrentTimestamp; |
| 627 | } |
| 628 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 629 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, |
| 630 | const sp<GraphicBuffer>& graphicBuffer) { |
| 631 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 632 | EGLint attrs[] = { |
| 633 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 634 | EGL_NONE, |
| 635 | }; |
| 636 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 637 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 638 | if (image == EGL_NO_IMAGE_KHR) { |
| 639 | EGLint error = eglGetError(); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 640 | ST_LOGE("error creating EGLImage: %#x", error); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 641 | } |
| 642 | return image; |
| 643 | } |
| 644 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 645 | sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const { |
| 646 | Mutex::Autolock lock(mMutex); |
| 647 | return mCurrentTextureBuf; |
| 648 | } |
| 649 | |
| 650 | Rect SurfaceTexture::getCurrentCrop() const { |
| 651 | Mutex::Autolock lock(mMutex); |
Daniel Lam | 016c8cb | 2012-04-03 15:54:58 -0700 | [diff] [blame] | 652 | |
| 653 | Rect outCrop = mCurrentCrop; |
| 654 | if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) { |
| 655 | int32_t newWidth = mCurrentCrop.width(); |
| 656 | int32_t newHeight = mCurrentCrop.height(); |
| 657 | |
| 658 | if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) { |
| 659 | newWidth = newHeight * mDefaultWidth / mDefaultHeight; |
| 660 | ST_LOGV("too wide: newWidth = %d", newWidth); |
| 661 | } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) { |
| 662 | newHeight = newWidth * mDefaultHeight / mDefaultWidth; |
| 663 | ST_LOGV("too tall: newHeight = %d", newHeight); |
| 664 | } |
| 665 | |
| 666 | // The crop is too wide |
| 667 | if (newWidth < mCurrentCrop.width()) { |
| 668 | int32_t dw = (newWidth - mCurrentCrop.width())/2; |
| 669 | outCrop.left -=dw; |
| 670 | outCrop.right += dw; |
| 671 | // The crop is too tall |
| 672 | } else if (newHeight < mCurrentCrop.height()) { |
| 673 | int32_t dh = (newHeight - mCurrentCrop.height())/2; |
| 674 | outCrop.top -= dh; |
| 675 | outCrop.bottom += dh; |
| 676 | } |
| 677 | |
| 678 | ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]", |
| 679 | outCrop.left, outCrop.top, |
| 680 | outCrop.right,outCrop.bottom); |
| 681 | } |
| 682 | |
Daniel Lam | 016c8cb | 2012-04-03 15:54:58 -0700 | [diff] [blame] | 683 | return outCrop; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | uint32_t SurfaceTexture::getCurrentTransform() const { |
| 687 | Mutex::Autolock lock(mMutex); |
| 688 | return mCurrentTransform; |
| 689 | } |
| 690 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 691 | uint32_t SurfaceTexture::getCurrentScalingMode() const { |
| 692 | Mutex::Autolock lock(mMutex); |
| 693 | return mCurrentScalingMode; |
| 694 | } |
| 695 | |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 696 | sp<Fence> SurfaceTexture::getCurrentFence() const { |
| 697 | Mutex::Autolock lock(mMutex); |
| 698 | return mCurrentFence; |
| 699 | } |
| 700 | |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 701 | bool SurfaceTexture::isSynchronousMode() const { |
| 702 | Mutex::Autolock lock(mMutex); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 703 | return mBufferQueue->isSynchronousMode(); |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 706 | void SurfaceTexture::freeBufferLocked(int slotIndex) { |
| 707 | ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 708 | if (slotIndex == mCurrentTexture) { |
| 709 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 710 | } |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 711 | EGLImageKHR img = mEglSlots[slotIndex].mEglImage; |
Jamie Gennis | 9aa74db | 2012-04-17 13:07:45 -0700 | [diff] [blame] | 712 | if (img != EGL_NO_IMAGE_KHR) { |
| 713 | ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img); |
| 714 | eglDestroyImageKHR(mEglDisplay, img); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 715 | } |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 716 | mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR; |
| 717 | ConsumerBase::freeBufferLocked(slotIndex); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 718 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 719 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 720 | void SurfaceTexture::abandonLocked() { |
| 721 | ST_LOGV("abandonLocked"); |
| 722 | mCurrentTextureBuf.clear(); |
| 723 | ConsumerBase::abandonLocked(); |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 726 | void SurfaceTexture::setName(const String8& name) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 727 | Mutex::Autolock _l(mMutex); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 728 | mName = name; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 729 | mBufferQueue->setConsumerName(name); |
| 730 | } |
| 731 | |
| 732 | status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) { |
| 733 | Mutex::Autolock lock(mMutex); |
| 734 | return mBufferQueue->setDefaultBufferFormat(defaultFormat); |
| 735 | } |
| 736 | |
| 737 | status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) { |
| 738 | Mutex::Autolock lock(mMutex); |
Eino-Ville Talvala | 85b2176 | 2012-04-13 15:16:31 -0700 | [diff] [blame] | 739 | usage |= DEFAULT_USAGE_FLAGS; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 740 | return mBufferQueue->setConsumerUsageBits(usage); |
| 741 | } |
| 742 | |
| 743 | status_t SurfaceTexture::setTransformHint(uint32_t hint) { |
| 744 | Mutex::Autolock lock(mMutex); |
| 745 | return mBufferQueue->setTransformHint(hint); |
| 746 | } |
| 747 | |
| 748 | // Used for refactoring BufferQueue from SurfaceTexture |
| 749 | // Should not be in final interface once users of SurfaceTexture are clean up. |
| 750 | status_t SurfaceTexture::setSynchronousMode(bool enabled) { |
| 751 | Mutex::Autolock lock(mMutex); |
| 752 | return mBufferQueue->setSynchronousMode(enabled); |
| 753 | } |
| 754 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 755 | void SurfaceTexture::dumpLocked(String8& result, const char* prefix, |
| 756 | char* buffer, size_t size) const |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 757 | { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 758 | snprintf(buffer, size, |
| 759 | "%smTexName=%d mCurrentTexture=%d\n" |
| 760 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 761 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 762 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 763 | mCurrentTransform); |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 764 | result.append(buffer); |
| 765 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 766 | ConsumerBase::dumpLocked(result, prefix, buffer, size); |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 769 | static void mtxMul(float out[16], const float a[16], const float b[16]) { |
| 770 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; |
| 771 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; |
| 772 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; |
| 773 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; |
| 774 | |
| 775 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; |
| 776 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; |
| 777 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; |
| 778 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; |
| 779 | |
| 780 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; |
| 781 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; |
| 782 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; |
| 783 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; |
| 784 | |
| 785 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; |
| 786 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; |
| 787 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; |
| 788 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; |
| 789 | } |
| 790 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 791 | }; // namespace android |