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 | e70d8b4 | 2011-01-09 13:24:09 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 19 | |
| 20 | #define GL_GLEXT_PROTOTYPES |
| 21 | #define EGL_EGLEXT_PROTOTYPES |
| 22 | |
| 23 | #include <EGL/egl.h> |
| 24 | #include <EGL/eglext.h> |
| 25 | #include <GLES2/gl2.h> |
| 26 | #include <GLES2/gl2ext.h> |
| 27 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 28 | #include <hardware/hardware.h> |
| 29 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 30 | #include <gui/IGraphicBufferAlloc.h> |
| 31 | #include <gui/ISurfaceComposer.h> |
| 32 | #include <gui/SurfaceComposerClient.h> |
| 33 | #include <gui/SurfaceTexture.h> |
Mathias Agopian | 41f673c | 2011-11-17 17:48:35 -0800 | [diff] [blame] | 34 | |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 35 | #include <private/gui/ComposerService.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 36 | |
| 37 | #include <utils/Log.h> |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 38 | #include <utils/String8.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 39 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 40 | // This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension |
| 41 | // to synchronize access to the buffers. It will cause dequeueBuffer to stall, |
| 42 | // waiting for the GL reads for the buffer being dequeued to complete before |
| 43 | // allowing the buffer to be dequeued. |
| 44 | #ifdef USE_FENCE_SYNC |
| 45 | #ifdef ALLOW_DEQUEUE_CURRENT_BUFFER |
| 46 | #error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible" |
| 47 | #endif |
| 48 | #endif |
| 49 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 50 | // Macros for including the SurfaceTexture name in log messages |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 51 | #define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 52 | #define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 53 | #define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 54 | #define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 55 | #define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__) |
Mathias Agopian | 29b5762 | 2011-08-17 15:42:04 -0700 | [diff] [blame] | 56 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 57 | namespace android { |
| 58 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 59 | // Transform matrices |
| 60 | static float mtxIdentity[16] = { |
| 61 | 1, 0, 0, 0, |
| 62 | 0, 1, 0, 0, |
| 63 | 0, 0, 1, 0, |
| 64 | 0, 0, 0, 1, |
| 65 | }; |
| 66 | static float mtxFlipH[16] = { |
| 67 | -1, 0, 0, 0, |
| 68 | 0, 1, 0, 0, |
| 69 | 0, 0, 1, 0, |
| 70 | 1, 0, 0, 1, |
| 71 | }; |
| 72 | static float mtxFlipV[16] = { |
| 73 | 1, 0, 0, 0, |
| 74 | 0, -1, 0, 0, |
| 75 | 0, 0, 1, 0, |
| 76 | 0, 1, 0, 1, |
| 77 | }; |
| 78 | static float mtxRot90[16] = { |
| 79 | 0, 1, 0, 0, |
| 80 | -1, 0, 0, 0, |
| 81 | 0, 0, 1, 0, |
| 82 | 1, 0, 0, 1, |
| 83 | }; |
| 84 | static float mtxRot180[16] = { |
| 85 | -1, 0, 0, 0, |
| 86 | 0, -1, 0, 0, |
| 87 | 0, 0, 1, 0, |
| 88 | 1, 1, 0, 1, |
| 89 | }; |
| 90 | static float mtxRot270[16] = { |
| 91 | 0, -1, 0, 0, |
| 92 | 1, 0, 0, 0, |
| 93 | 0, 0, 1, 0, |
| 94 | 0, 1, 0, 1, |
| 95 | }; |
| 96 | |
| 97 | static void mtxMul(float out[16], const float a[16], const float b[16]); |
| 98 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 99 | // Get an ID that's unique within this process. |
| 100 | static int32_t createProcessUniqueId() { |
| 101 | static volatile int32_t globalCounter = 0; |
| 102 | return android_atomic_inc(&globalCounter); |
| 103 | } |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 104 | |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 105 | SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode, |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 106 | GLenum texTarget, bool useFenceSync) : |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 107 | BufferQueue(allowSynchronousMode), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 108 | mCurrentTransform(0), |
| 109 | mCurrentTimestamp(0), |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 110 | mTexName(tex), |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 111 | #ifdef USE_FENCE_SYNC |
| 112 | mUseFenceSync(useFenceSync), |
| 113 | #else |
| 114 | mUseFenceSync(false), |
| 115 | #endif |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 116 | mTexTarget(texTarget), |
| 117 | mAbandoned(false), |
| 118 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 119 | { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 120 | // Choose a name using the PID and a process-unique ID. |
| 121 | mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); |
| 122 | BufferQueue::setConsumerName(mName); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 123 | |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 124 | ST_LOGV("SurfaceTexture"); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 125 | memcpy(mCurrentTransformMatrix, mtxIdentity, |
| 126 | sizeof(mCurrentTransformMatrix)); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | SurfaceTexture::~SurfaceTexture() { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 130 | ST_LOGV("~SurfaceTexture"); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 131 | abandon(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 134 | status_t SurfaceTexture::setBufferCountServer(int bufferCount) { |
| 135 | Mutex::Autolock lock(mMutex); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 136 | return BufferQueue::setBufferCountServer(bufferCount); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 139 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 140 | status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h) |
| 141 | { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 142 | return BufferQueue::setDefaultBufferSize(w, h); |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 145 | status_t SurfaceTexture::updateTexImage() { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 146 | ST_LOGV("updateTexImage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 147 | Mutex::Autolock lock(mMutex); |
| 148 | |
Mathias Agopian | e47498f | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 149 | if (mAbandoned) { |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 150 | ST_LOGE("calling updateTexImage() on an abandoned SurfaceTexture"); |
Mathias Agopian | 8e19c2e | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 151 | return NO_INIT; |
Mathias Agopian | e47498f | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 154 | BufferItem item; |
| 155 | |
Jamie Gennis | 50c4efc | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 156 | // In asynchronous mode the list is guaranteed to be one buffer |
| 157 | // deep, while in synchronous mode we use the oldest buffer. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 158 | if (acquire(&item) == NO_ERROR) { |
| 159 | int buf = item.mBuf; |
| 160 | // This buffer was newly allocated, so we need to clean up on our side |
| 161 | if (item.mGraphicBuffer != NULL) { |
| 162 | mEGLSlots[buf].mGraphicBuffer = 0; |
| 163 | if (mEGLSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) { |
| 164 | eglDestroyImageKHR(mEGLSlots[buf].mEglDisplay, |
| 165 | mEGLSlots[buf].mEglImage); |
| 166 | mEGLSlots[buf].mEglImage = EGL_NO_IMAGE_KHR; |
| 167 | mEGLSlots[buf].mEglDisplay = EGL_NO_DISPLAY; |
| 168 | } |
| 169 | mEGLSlots[buf].mGraphicBuffer = item.mGraphicBuffer; |
| 170 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 171 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 172 | // Update the GL texture object. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 173 | EGLImageKHR image = mEGLSlots[buf].mEglImage; |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 174 | EGLDisplay dpy = eglGetCurrentDisplay(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 175 | if (image == EGL_NO_IMAGE_KHR) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 176 | if (item.mGraphicBuffer == 0) { |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 177 | ST_LOGE("buffer at slot %d is null", buf); |
Mathias Agopian | 8e19c2e | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 178 | return BAD_VALUE; |
Mathias Agopian | e47498f | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 179 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 180 | image = createImage(dpy, item.mGraphicBuffer); |
| 181 | mEGLSlots[buf].mEglImage = image; |
| 182 | mEGLSlots[buf].mEglDisplay = dpy; |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 183 | if (image == EGL_NO_IMAGE_KHR) { |
| 184 | // NOTE: if dpy was invalid, createImage() is guaranteed to |
| 185 | // fail. so we'd end up here. |
| 186 | return -EINVAL; |
| 187 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 188 | } |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 189 | |
| 190 | GLint error; |
| 191 | while ((error = glGetError()) != GL_NO_ERROR) { |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 192 | ST_LOGW("updateTexImage: clearing GL error: %#04x", error); |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 193 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 194 | |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 195 | glBindTexture(mTexTarget, mTexName); |
| 196 | glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 197 | |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 198 | bool failed = false; |
| 199 | while ((error = glGetError()) != GL_NO_ERROR) { |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 200 | ST_LOGE("error binding external texture image %p (slot %d): %#04x", |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 201 | image, buf, error); |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 202 | failed = true; |
| 203 | } |
| 204 | if (failed) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 205 | releaseBuffer(buf, mEGLSlots[buf].mEglDisplay, |
| 206 | mEGLSlots[buf].mFence); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 207 | return -EINVAL; |
| 208 | } |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 209 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 210 | if (mCurrentTexture != INVALID_BUFFER_SLOT) { |
| 211 | if (mUseFenceSync) { |
| 212 | EGLSyncKHR fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, |
| 213 | NULL); |
| 214 | if (fence == EGL_NO_SYNC_KHR) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 215 | ALOGE("updateTexImage: error creating fence: %#x", |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 216 | eglGetError()); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 217 | releaseBuffer(buf, mEGLSlots[buf].mEglDisplay, |
| 218 | mEGLSlots[buf].mFence); |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 219 | return -EINVAL; |
| 220 | } |
| 221 | glFlush(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 222 | mEGLSlots[mCurrentTexture].mFence = fence; |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", |
| 227 | mCurrentTexture, |
| 228 | mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 229 | buf, item.mGraphicBuffer != NULL ? item.mGraphicBuffer->handle : 0); |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 230 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 231 | // release old buffer |
| 232 | releaseBuffer(mCurrentTexture, |
| 233 | mEGLSlots[mCurrentTexture].mEglDisplay, |
| 234 | mEGLSlots[mCurrentTexture].mFence); |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 235 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 236 | // Update the SurfaceTexture state. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 237 | mCurrentTexture = buf; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 238 | mCurrentTextureBuf = mEGLSlots[buf].mGraphicBuffer; |
| 239 | mCurrentCrop = item.mCrop; |
| 240 | mCurrentTransform = item.mTransform; |
| 241 | mCurrentScalingMode = item.mScalingMode; |
| 242 | mCurrentTimestamp = item.mTimestamp; |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 243 | computeCurrentTransformMatrix(); |
Jamie Gennis | 50c4efc | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 244 | |
| 245 | // Now that we've passed the point at which failures can happen, |
| 246 | // it's safe to remove the buffer from the front of the queue. |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 247 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 248 | } else { |
| 249 | // 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] | 250 | glBindTexture(mTexTarget, mTexName); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 251 | } |
Jamie Gennis | 50c4efc | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 252 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 253 | return OK; |
| 254 | } |
| 255 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 256 | bool SurfaceTexture::isExternalFormat(uint32_t format) |
| 257 | { |
| 258 | switch (format) { |
| 259 | // supported YUV formats |
| 260 | case HAL_PIXEL_FORMAT_YV12: |
| 261 | // Legacy/deprecated YUV formats |
| 262 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 263 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 264 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | // Any OEM format needs to be considered |
| 269 | if (format>=0x100 && format<=0x1FF) |
| 270 | return true; |
| 271 | |
| 272 | return false; |
| 273 | } |
| 274 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 275 | GLenum SurfaceTexture::getCurrentTextureTarget() const { |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 276 | return mTexTarget; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 279 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 280 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 281 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 282 | } |
| 283 | |
| 284 | void SurfaceTexture::computeCurrentTransformMatrix() { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 285 | ST_LOGV("computeCurrentTransformMatrix"); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 286 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 287 | float xform[16]; |
| 288 | for (int i = 0; i < 16; i++) { |
| 289 | xform[i] = mtxIdentity[i]; |
| 290 | } |
| 291 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 292 | float result[16]; |
| 293 | mtxMul(result, xform, mtxFlipH); |
| 294 | for (int i = 0; i < 16; i++) { |
| 295 | xform[i] = result[i]; |
| 296 | } |
| 297 | } |
| 298 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 299 | float result[16]; |
| 300 | mtxMul(result, xform, mtxFlipV); |
| 301 | for (int i = 0; i < 16; i++) { |
| 302 | xform[i] = result[i]; |
| 303 | } |
| 304 | } |
| 305 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 306 | float result[16]; |
| 307 | mtxMul(result, xform, mtxRot90); |
| 308 | for (int i = 0; i < 16; i++) { |
| 309 | xform[i] = result[i]; |
| 310 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 313 | sp<GraphicBuffer>& buf(mCurrentTextureBuf); |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 314 | float tx, ty, sx, sy; |
| 315 | if (!mCurrentCrop.isEmpty()) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 316 | // In order to prevent bilinear sampling at the of the crop rectangle we |
| 317 | // may need to shrink it by 2 texels in each direction. Normally this |
| 318 | // would just need to take 1/2 a texel off each end, but because the |
| 319 | // chroma channels will likely be subsampled we need to chop off a whole |
| 320 | // texel. This will cause artifacts if someone does nearest sampling |
| 321 | // with 1:1 pixel:texel ratio, but it's impossible to simultaneously |
| 322 | // accomodate the bilinear and nearest sampling uses. |
| 323 | // |
| 324 | // If nearest sampling turns out to be a desirable usage of these |
| 325 | // textures then we could add the ability to switch a SurfaceTexture to |
| 326 | // nearest-mode. Preferably, however, the image producers (video |
| 327 | // decoder, camera, etc.) would simply not use a crop rectangle (or at |
| 328 | // least not tell the framework about it) so that the GPU can do the |
| 329 | // correct edge behavior. |
| 330 | int xshrink = 0, yshrink = 0; |
| 331 | if (mCurrentCrop.left > 0) { |
| 332 | tx = float(mCurrentCrop.left + 1) / float(buf->getWidth()); |
| 333 | xshrink++; |
| 334 | } else { |
| 335 | tx = 0.0f; |
| 336 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 337 | if (mCurrentCrop.right < int32_t(buf->getWidth())) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 338 | xshrink++; |
| 339 | } |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 340 | if (mCurrentCrop.bottom < int32_t(buf->getHeight())) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 341 | ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) / |
| 342 | float(buf->getHeight()); |
| 343 | yshrink++; |
| 344 | } else { |
| 345 | ty = 0.0f; |
| 346 | } |
| 347 | if (mCurrentCrop.top > 0) { |
| 348 | yshrink++; |
| 349 | } |
| 350 | sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth()); |
| 351 | sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight()); |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 352 | } else { |
| 353 | tx = 0.0f; |
| 354 | ty = 0.0f; |
| 355 | sx = 1.0f; |
| 356 | sy = 1.0f; |
| 357 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 358 | float crop[16] = { |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 359 | sx, 0, 0, 0, |
| 360 | 0, sy, 0, 0, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 361 | 0, 0, 1, 0, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 362 | tx, ty, 0, 1, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 363 | }; |
| 364 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 365 | float mtxBeforeFlipV[16]; |
| 366 | mtxMul(mtxBeforeFlipV, crop, xform); |
| 367 | |
| 368 | // SurfaceFlinger expects the top of its window textures to be at a Y |
| 369 | // coordinate of 0, so SurfaceTexture must behave the same way. We don't |
| 370 | // want to expose this to applications, however, so we must add an |
| 371 | // additional vertical flip to the transform after all the other transforms. |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 372 | mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 375 | nsecs_t SurfaceTexture::getTimestamp() { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 376 | ST_LOGV("getTimestamp"); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 377 | Mutex::Autolock lock(mMutex); |
| 378 | return mCurrentTimestamp; |
| 379 | } |
| 380 | |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 381 | void SurfaceTexture::setFrameAvailableListener( |
Pannag Sanketi | 292a31a | 2011-06-24 09:56:27 -0700 | [diff] [blame] | 382 | const sp<FrameAvailableListener>& listener) { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 383 | ST_LOGV("setFrameAvailableListener"); |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 384 | Mutex::Autolock lock(mMutex); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 385 | BufferQueue::setFrameAvailableListener(listener); |
Jamie Gennis | c4d4aea | 2011-01-13 14:43:36 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 388 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, |
| 389 | const sp<GraphicBuffer>& graphicBuffer) { |
| 390 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 391 | EGLint attrs[] = { |
| 392 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 393 | EGL_NONE, |
| 394 | }; |
| 395 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 396 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 397 | if (image == EGL_NO_IMAGE_KHR) { |
| 398 | EGLint error = eglGetError(); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 399 | ST_LOGE("error creating EGLImage: %#x", error); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 400 | } |
| 401 | return image; |
| 402 | } |
| 403 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 404 | sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const { |
| 405 | Mutex::Autolock lock(mMutex); |
| 406 | return mCurrentTextureBuf; |
| 407 | } |
| 408 | |
| 409 | Rect SurfaceTexture::getCurrentCrop() const { |
| 410 | Mutex::Autolock lock(mMutex); |
| 411 | return mCurrentCrop; |
| 412 | } |
| 413 | |
| 414 | uint32_t SurfaceTexture::getCurrentTransform() const { |
| 415 | Mutex::Autolock lock(mMutex); |
| 416 | return mCurrentTransform; |
| 417 | } |
| 418 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 419 | uint32_t SurfaceTexture::getCurrentScalingMode() const { |
| 420 | Mutex::Autolock lock(mMutex); |
| 421 | return mCurrentScalingMode; |
| 422 | } |
| 423 | |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 424 | bool SurfaceTexture::isSynchronousMode() const { |
| 425 | Mutex::Autolock lock(mMutex); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 426 | return BufferQueue::isSynchronousMode(); |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 429 | void SurfaceTexture::abandon() { |
| 430 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 431 | mAbandoned = true; |
Mathias Agopian | 97a9884 | 2011-08-03 15:18:36 -0700 | [diff] [blame] | 432 | mCurrentTextureBuf.clear(); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 433 | |
| 434 | // destroy all egl buffers |
| 435 | for (int i =0; i < NUM_BUFFER_SLOTS; i++) { |
| 436 | mEGLSlots[i].mGraphicBuffer = 0; |
| 437 | if (mEGLSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { |
| 438 | eglDestroyImageKHR(mEGLSlots[i].mEglDisplay, |
| 439 | mEGLSlots[i].mEglImage); |
| 440 | mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
| 441 | mEGLSlots[i].mEglDisplay = EGL_NO_DISPLAY; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // disconnect from the BufferQueue |
| 446 | BufferQueue::consumerDisconnect(); |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 449 | void SurfaceTexture::setName(const String8& name) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 450 | Mutex::Autolock _l(mMutex); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 451 | mName = name; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 452 | BufferQueue::setConsumerName(name); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 455 | void SurfaceTexture::dump(String8& result) const |
| 456 | { |
| 457 | char buffer[1024]; |
| 458 | dump(result, "", buffer, 1024); |
| 459 | } |
| 460 | |
| 461 | void SurfaceTexture::dump(String8& result, const char* prefix, |
| 462 | char* buffer, size_t SIZE) const |
| 463 | { |
| 464 | Mutex::Autolock _l(mMutex); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 465 | snprintf(buffer, SIZE, "%smTexName=%d\n", prefix, mTexName); |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 466 | result.append(buffer); |
| 467 | |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 468 | snprintf(buffer, SIZE, |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 469 | "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n" |
| 470 | ,prefix, mCurrentCrop.left, |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 471 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 472 | mCurrentTransform, mCurrentTexture |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 473 | ); |
| 474 | result.append(buffer); |
| 475 | |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 476 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 477 | BufferQueue::dump(result, prefix, buffer, SIZE); |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 480 | static void mtxMul(float out[16], const float a[16], const float b[16]) { |
| 481 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; |
| 482 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; |
| 483 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; |
| 484 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; |
| 485 | |
| 486 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; |
| 487 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; |
| 488 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; |
| 489 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; |
| 490 | |
| 491 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; |
| 492 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; |
| 493 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; |
| 494 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; |
| 495 | |
| 496 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; |
| 497 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; |
| 498 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; |
| 499 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; |
| 500 | } |
| 501 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 502 | }; // namespace android |