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