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