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