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