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