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 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -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 | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 42 | // This compile option makes SurfaceTexture use the |
| 43 | // EGL_ANDROID_native_fence_sync extension to create Android native fences to |
| 44 | // signal when all GLES reads for a given buffer have completed. It is not |
| 45 | // compatible with using the EGL_KHR_fence_sync extension for the same |
| 46 | // purpose. |
| 47 | #ifdef USE_NATIVE_FENCE_SYNC |
| 48 | #ifdef USE_FENCE_SYNC |
| 49 | #error "USE_NATIVE_FENCE_SYNC and USE_FENCE_SYNC are incompatible" |
| 50 | #endif |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 51 | static const bool useNativeFenceSync = true; |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 52 | #else |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 53 | static const bool useNativeFenceSync = false; |
| 54 | #endif |
| 55 | |
| 56 | // This compile option makes SurfaceTexture use the EGL_ANDROID_sync_wait |
| 57 | // extension to insert server-side waits into the GLES command stream. This |
| 58 | // feature requires the EGL_ANDROID_native_fence_sync and |
| 59 | // EGL_ANDROID_wait_sync extensions. |
| 60 | #ifdef USE_WAIT_SYNC |
| 61 | static const bool useWaitSync = true; |
| 62 | #else |
| 63 | static const bool useWaitSync = false; |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 64 | #endif |
| 65 | |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 66 | // This compile option makes SurfaceTexture use the EGL_KHR_fence_sync extension |
| 67 | // to synchronize access to the buffers. It will cause dequeueBuffer to stall, |
| 68 | // waiting for the GL reads for the buffer being dequeued to complete before |
| 69 | // allowing the buffer to be dequeued. |
| 70 | #ifdef USE_FENCE_SYNC |
| 71 | #ifdef ALLOW_DEQUEUE_CURRENT_BUFFER |
| 72 | #error "USE_FENCE_SYNC and ALLOW_DEQUEUE_CURRENT_BUFFER are incompatible" |
| 73 | #endif |
| 74 | #endif |
| 75 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 76 | // Macros for including the SurfaceTexture name in log messages |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 77 | #define ST_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 78 | #define ST_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 79 | #define ST_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 80 | #define ST_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__) |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 81 | #define ST_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__) |
Mathias Agopian | 29b5762 | 2011-08-17 15:42:04 -0700 | [diff] [blame] | 82 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 83 | namespace android { |
| 84 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 85 | // Transform matrices |
| 86 | static float mtxIdentity[16] = { |
| 87 | 1, 0, 0, 0, |
| 88 | 0, 1, 0, 0, |
| 89 | 0, 0, 1, 0, |
| 90 | 0, 0, 0, 1, |
| 91 | }; |
| 92 | static float mtxFlipH[16] = { |
| 93 | -1, 0, 0, 0, |
| 94 | 0, 1, 0, 0, |
| 95 | 0, 0, 1, 0, |
| 96 | 1, 0, 0, 1, |
| 97 | }; |
| 98 | static float mtxFlipV[16] = { |
| 99 | 1, 0, 0, 0, |
| 100 | 0, -1, 0, 0, |
| 101 | 0, 0, 1, 0, |
| 102 | 0, 1, 0, 1, |
| 103 | }; |
| 104 | static float mtxRot90[16] = { |
| 105 | 0, 1, 0, 0, |
| 106 | -1, 0, 0, 0, |
| 107 | 0, 0, 1, 0, |
| 108 | 1, 0, 0, 1, |
| 109 | }; |
| 110 | static float mtxRot180[16] = { |
| 111 | -1, 0, 0, 0, |
| 112 | 0, -1, 0, 0, |
| 113 | 0, 0, 1, 0, |
| 114 | 1, 1, 0, 1, |
| 115 | }; |
| 116 | static float mtxRot270[16] = { |
| 117 | 0, -1, 0, 0, |
| 118 | 1, 0, 0, 0, |
| 119 | 0, 0, 1, 0, |
| 120 | 0, 1, 0, 1, |
| 121 | }; |
| 122 | |
| 123 | static void mtxMul(float out[16], const float a[16], const float b[16]); |
| 124 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 125 | |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 126 | SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode, |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 127 | GLenum texTarget, bool useFenceSync, const sp<BufferQueue> &bufferQueue) : |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 128 | ConsumerBase(bufferQueue == 0 ? new BufferQueue(allowSynchronousMode) : bufferQueue), |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 129 | mCurrentTransform(0), |
| 130 | mCurrentTimestamp(0), |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 131 | mFilteringEnabled(true), |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 132 | mTexName(tex), |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 133 | #ifdef USE_FENCE_SYNC |
| 134 | mUseFenceSync(useFenceSync), |
| 135 | #else |
| 136 | mUseFenceSync(false), |
| 137 | #endif |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 138 | mTexTarget(texTarget), |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 139 | mEglDisplay(EGL_NO_DISPLAY), |
| 140 | mEglContext(EGL_NO_CONTEXT), |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 141 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT), |
| 142 | mAttached(true) |
Daniel Lam | 6b091c5 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 143 | { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 144 | ST_LOGV("SurfaceTexture"); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 145 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 146 | memcpy(mCurrentTransformMatrix, mtxIdentity, |
| 147 | sizeof(mCurrentTransformMatrix)); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 148 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 149 | mBufferQueue->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 152 | status_t SurfaceTexture::setDefaultMaxBufferCount(int bufferCount) { |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 153 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 31a353d | 2012-08-24 17:25:13 -0700 | [diff] [blame] | 154 | return mBufferQueue->setDefaultMaxBufferCount(bufferCount); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 157 | |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 158 | status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h) |
| 159 | { |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 160 | Mutex::Autolock lock(mMutex); |
Daniel Lam | 016c8cb | 2012-04-03 15:54:58 -0700 | [diff] [blame] | 161 | mDefaultWidth = w; |
| 162 | mDefaultHeight = h; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 163 | return mBufferQueue->setDefaultBufferSize(w, h); |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 166 | status_t SurfaceTexture::updateTexImage() { |
Jamie Gennis | 3941cb2 | 2012-09-17 16:58:17 -0700 | [diff] [blame^] | 167 | return SurfaceTexture::updateTexImage(NULL, false); |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 170 | status_t SurfaceTexture::acquireBufferLocked(BufferQueue::BufferItem *item) { |
| 171 | status_t err = ConsumerBase::acquireBufferLocked(item); |
| 172 | if (err != NO_ERROR) { |
| 173 | return err; |
| 174 | } |
| 175 | |
| 176 | int slot = item->mBuf; |
| 177 | if (item->mGraphicBuffer != NULL) { |
| 178 | if (mEglSlots[slot].mEglImage != EGL_NO_IMAGE_KHR) { |
| 179 | eglDestroyImageKHR(mEglDisplay, mEglSlots[slot].mEglImage); |
| 180 | mEglSlots[slot].mEglImage = EGL_NO_IMAGE_KHR; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Update the GL texture object. We may have to do this even when |
| 185 | // item.mGraphicBuffer == NULL, if we destroyed the EGLImage when |
| 186 | // detaching from a context but the buffer has not been re-allocated. |
| 187 | if (mEglSlots[slot].mEglImage == EGL_NO_IMAGE_KHR) { |
| 188 | EGLImageKHR image = createImage(mEglDisplay, mSlots[slot].mGraphicBuffer); |
| 189 | if (image == EGL_NO_IMAGE_KHR) { |
| 190 | return UNKNOWN_ERROR; |
| 191 | } |
| 192 | mEglSlots[slot].mEglImage = image; |
| 193 | } |
| 194 | |
| 195 | return NO_ERROR; |
| 196 | } |
| 197 | |
| 198 | status_t SurfaceTexture::releaseBufferLocked(int buf, EGLDisplay display, |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 199 | EGLSyncKHR eglFence) { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 200 | status_t err = ConsumerBase::releaseBufferLocked(buf, mEglDisplay, |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 201 | eglFence); |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 202 | |
| 203 | mEglSlots[mCurrentTexture].mEglFence = EGL_NO_SYNC_KHR; |
| 204 | |
| 205 | return err; |
| 206 | } |
| 207 | |
Jamie Gennis | 3941cb2 | 2012-09-17 16:58:17 -0700 | [diff] [blame^] | 208 | status_t SurfaceTexture::updateTexImage(BufferRejecter* rejecter, bool skipSync) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 209 | ATRACE_CALL(); |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 210 | ST_LOGV("updateTexImage"); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 211 | Mutex::Autolock lock(mMutex); |
| 212 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 213 | status_t err = NO_ERROR; |
| 214 | |
Mathias Agopian | e47498f | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 215 | if (mAbandoned) { |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 216 | ST_LOGE("updateTexImage: SurfaceTexture is abandoned!"); |
Mathias Agopian | 8e19c2e | 2011-08-10 17:35:09 -0700 | [diff] [blame] | 217 | return NO_INIT; |
Mathias Agopian | e47498f | 2011-08-08 19:35:15 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 220 | if (!mAttached) { |
| 221 | ST_LOGE("updateTexImage: SurfaceTexture is not attached to an OpenGL " |
| 222 | "ES context"); |
| 223 | return INVALID_OPERATION; |
| 224 | } |
| 225 | |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 226 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 227 | EGLContext ctx = eglGetCurrentContext(); |
| 228 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 229 | if ((mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) || |
| 230 | dpy == EGL_NO_DISPLAY) { |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 231 | ST_LOGE("updateTexImage: invalid current EGLDisplay"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 232 | return INVALID_OPERATION; |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 235 | if ((mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) || |
| 236 | ctx == EGL_NO_CONTEXT) { |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 237 | ST_LOGE("updateTexImage: invalid current EGLContext"); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 238 | return INVALID_OPERATION; |
Jamie Gennis | ce56137 | 2012-03-19 18:33:05 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | mEglDisplay = dpy; |
| 242 | mEglContext = ctx; |
| 243 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 244 | BufferQueue::BufferItem item; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 245 | |
Jamie Gennis | 50c4efc | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 246 | // In asynchronous mode the list is guaranteed to be one buffer |
| 247 | // deep, while in synchronous mode we use the oldest buffer. |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 248 | err = acquireBufferLocked(&item); |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 249 | if (err == NO_ERROR) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 250 | int buf = item.mBuf; |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 251 | |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 252 | // we call the rejecter here, in case the caller has a reason to |
| 253 | // not accept this buffer. this is used by SurfaceFlinger to |
| 254 | // reject buffers which have the wrong size |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 255 | if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) { |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 256 | releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR); |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 257 | glBindTexture(mTexTarget, mTexName); |
| 258 | return NO_ERROR; |
| 259 | } |
| 260 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 261 | GLint error; |
| 262 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 263 | ST_LOGW("updateTexImage: clearing GL error: %#04x", error); |
| 264 | } |
| 265 | |
| 266 | EGLImageKHR image = mEglSlots[buf].mEglImage; |
| 267 | glBindTexture(mTexTarget, mTexName); |
| 268 | glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image); |
| 269 | |
| 270 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 271 | ST_LOGE("updateTexImage: error binding external texture image %p " |
| 272 | "(slot %d): %#04x", image, buf, error); |
| 273 | err = UNKNOWN_ERROR; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 274 | } |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 275 | |
Mathias Agopian | 7e477bf | 2012-05-18 16:50:58 -0700 | [diff] [blame] | 276 | if (err == NO_ERROR) { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 277 | err = syncForReleaseLocked(dpy); |
Jamie Gennis | 0eb8851 | 2011-01-26 11:52:02 -0800 | [diff] [blame] | 278 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 279 | |
Mathias Agopian | 7e477bf | 2012-05-18 16:50:58 -0700 | [diff] [blame] | 280 | if (err != NO_ERROR) { |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 281 | // Release the buffer we just acquired. It's not safe to |
| 282 | // release the old buffer, so instead we just drop the new frame. |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 283 | releaseBufferLocked(buf, dpy, EGL_NO_SYNC_KHR); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 284 | return err; |
Jamie Gennis | 86edf4f | 2011-11-14 14:51:01 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | ST_LOGV("updateTexImage: (slot=%d buf=%p) -> (slot=%d buf=%p)", |
| 288 | mCurrentTexture, |
| 289 | mCurrentTextureBuf != NULL ? mCurrentTextureBuf->handle : 0, |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 290 | buf, mSlots[buf].mGraphicBuffer->handle); |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 291 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 292 | // release old buffer |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 293 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 294 | status_t status = releaseBufferLocked(mCurrentTexture, dpy, |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 295 | mEglSlots[mCurrentTexture].mEglFence); |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 296 | if (status != NO_ERROR && status != BufferQueue::STALE_BUFFER_SLOT) { |
| 297 | ST_LOGE("updateTexImage: failed to release buffer: %s (%d)", |
| 298 | strerror(-status), status); |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 299 | err = status; |
| 300 | } |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 301 | } |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 302 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 303 | // Update the SurfaceTexture state. |
Mathias Agopian | b3e518c | 2011-04-21 18:52:51 -0700 | [diff] [blame] | 304 | mCurrentTexture = buf; |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 305 | mCurrentTextureBuf = mSlots[buf].mGraphicBuffer; |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 306 | mCurrentCrop = item.mCrop; |
| 307 | mCurrentTransform = item.mTransform; |
| 308 | mCurrentScalingMode = item.mScalingMode; |
| 309 | mCurrentTimestamp = item.mTimestamp; |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 310 | mCurrentFence = item.mFence; |
Jamie Gennis | 3941cb2 | 2012-09-17 16:58:17 -0700 | [diff] [blame^] | 311 | if (!skipSync) { |
| 312 | // SurfaceFlinger needs to lazily perform GLES synchronization |
| 313 | // only when it's actually going to use GLES for compositing. |
| 314 | // Eventually SurfaceFlinger should have its own consumer class, |
| 315 | // but for now we'll just hack it in to SurfaceTexture. |
| 316 | // SurfaceFlinger is responsible for calling doGLFenceWait before |
| 317 | // texturing from this SurfaceTexture. |
| 318 | doGLFenceWaitLocked(); |
| 319 | } |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 320 | computeCurrentTransformMatrix(); |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 321 | } else { |
| 322 | if (err < 0) { |
Jamie Gennis | d69097f | 2012-08-30 13:28:23 -0700 | [diff] [blame] | 323 | ST_LOGE("updateTexImage: acquire failed: %s (%d)", |
| 324 | strerror(-err), err); |
| 325 | return err; |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 326 | } |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 327 | // 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] | 328 | glBindTexture(mTexTarget, mTexName); |
Daniel Lam | fbcda93 | 2012-04-09 22:51:52 -0700 | [diff] [blame] | 329 | return OK; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 330 | } |
Jamie Gennis | 50c4efc | 2011-06-27 15:41:52 -0700 | [diff] [blame] | 331 | |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 332 | return err; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 335 | void SurfaceTexture::setReleaseFence(int fenceFd) { |
Jamie Gennis | 3d1d09c | 2012-08-08 15:39:55 -0700 | [diff] [blame] | 336 | sp<Fence> fence(new Fence(fenceFd)); |
Jamie Gennis | 45cb2ba | 2012-08-06 17:10:57 -0700 | [diff] [blame] | 337 | if (fenceFd == -1 || mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 338 | return; |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 339 | status_t err = addReleaseFence(mCurrentTexture, fence); |
| 340 | if (err != OK) { |
| 341 | ST_LOGE("setReleaseFence: failed to add the fence: %s (%d)", |
| 342 | strerror(-err), err); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 346 | status_t SurfaceTexture::detachFromContext() { |
| 347 | ATRACE_CALL(); |
| 348 | ST_LOGV("detachFromContext"); |
| 349 | Mutex::Autolock lock(mMutex); |
| 350 | |
| 351 | if (mAbandoned) { |
| 352 | ST_LOGE("detachFromContext: abandoned SurfaceTexture"); |
| 353 | return NO_INIT; |
| 354 | } |
| 355 | |
| 356 | if (!mAttached) { |
| 357 | ST_LOGE("detachFromContext: SurfaceTexture is not attached to a " |
| 358 | "context"); |
| 359 | return INVALID_OPERATION; |
| 360 | } |
| 361 | |
| 362 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 363 | EGLContext ctx = eglGetCurrentContext(); |
| 364 | |
| 365 | if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) { |
| 366 | ST_LOGE("detachFromContext: invalid current EGLDisplay"); |
| 367 | return INVALID_OPERATION; |
| 368 | } |
| 369 | |
| 370 | if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) { |
| 371 | ST_LOGE("detachFromContext: invalid current EGLContext"); |
| 372 | return INVALID_OPERATION; |
| 373 | } |
| 374 | |
| 375 | if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) { |
| 376 | status_t err = syncForReleaseLocked(dpy); |
| 377 | if (err != OK) { |
| 378 | return err; |
| 379 | } |
| 380 | |
| 381 | glDeleteTextures(1, &mTexName); |
| 382 | } |
| 383 | |
Jamie Gennis | 9aa74db | 2012-04-17 13:07:45 -0700 | [diff] [blame] | 384 | // Because we're giving up the EGLDisplay we need to free all the EGLImages |
| 385 | // that are associated with it. They'll be recreated when the |
| 386 | // SurfaceTexture gets attached to a new OpenGL ES context (and thus gets a |
| 387 | // new EGLDisplay). |
| 388 | for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 389 | EGLImageKHR img = mEglSlots[i].mEglImage; |
Jamie Gennis | 5c8a608 | 2012-05-02 13:10:56 -0700 | [diff] [blame] | 390 | if (img != EGL_NO_IMAGE_KHR) { |
Jamie Gennis | 9aa74db | 2012-04-17 13:07:45 -0700 | [diff] [blame] | 391 | eglDestroyImageKHR(mEglDisplay, img); |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 392 | mEglSlots[i].mEglImage = EGL_NO_IMAGE_KHR; |
Jamie Gennis | 9aa74db | 2012-04-17 13:07:45 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 396 | mEglDisplay = EGL_NO_DISPLAY; |
| 397 | mEglContext = EGL_NO_CONTEXT; |
| 398 | mAttached = false; |
| 399 | |
| 400 | return OK; |
| 401 | } |
| 402 | |
| 403 | status_t SurfaceTexture::attachToContext(GLuint tex) { |
| 404 | ATRACE_CALL(); |
| 405 | ST_LOGV("attachToContext"); |
| 406 | Mutex::Autolock lock(mMutex); |
| 407 | |
| 408 | if (mAbandoned) { |
| 409 | ST_LOGE("attachToContext: abandoned SurfaceTexture"); |
| 410 | return NO_INIT; |
| 411 | } |
| 412 | |
| 413 | if (mAttached) { |
| 414 | ST_LOGE("attachToContext: SurfaceTexture is already attached to a " |
| 415 | "context"); |
| 416 | return INVALID_OPERATION; |
| 417 | } |
| 418 | |
| 419 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 420 | EGLContext ctx = eglGetCurrentContext(); |
| 421 | |
| 422 | if (dpy == EGL_NO_DISPLAY) { |
| 423 | ST_LOGE("attachToContext: invalid current EGLDisplay"); |
| 424 | return INVALID_OPERATION; |
| 425 | } |
| 426 | |
| 427 | if (ctx == EGL_NO_CONTEXT) { |
| 428 | ST_LOGE("attachToContext: invalid current EGLContext"); |
| 429 | return INVALID_OPERATION; |
| 430 | } |
| 431 | |
| 432 | // We need to bind the texture regardless of whether there's a current |
| 433 | // buffer. |
| 434 | glBindTexture(mTexTarget, tex); |
| 435 | |
| 436 | if (mCurrentTextureBuf != NULL) { |
Jamie Gennis | 5c8a608 | 2012-05-02 13:10:56 -0700 | [diff] [blame] | 437 | // The EGLImageKHR that was associated with the slot was destroyed when |
| 438 | // the SurfaceTexture was detached from the old context, so we need to |
| 439 | // recreate it here. |
| 440 | EGLImageKHR image = createImage(dpy, mCurrentTextureBuf); |
| 441 | if (image == EGL_NO_IMAGE_KHR) { |
| 442 | return UNKNOWN_ERROR; |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Attach the current buffer to the GL texture. |
| 446 | glEGLImageTargetTexture2DOES(mTexTarget, (GLeglImageOES)image); |
| 447 | |
| 448 | GLint error; |
| 449 | status_t err = OK; |
| 450 | while ((error = glGetError()) != GL_NO_ERROR) { |
| 451 | ST_LOGE("attachToContext: error binding external texture image %p " |
| 452 | "(slot %d): %#04x", image, mCurrentTexture, error); |
| 453 | err = UNKNOWN_ERROR; |
| 454 | } |
| 455 | |
Jamie Gennis | 5c8a608 | 2012-05-02 13:10:56 -0700 | [diff] [blame] | 456 | // We destroy the EGLImageKHR here because the current buffer may no |
| 457 | // longer be associated with one of the buffer slots, so we have |
| 458 | // nowhere to to store it. If the buffer is still associated with a |
| 459 | // slot then another EGLImageKHR will be created next time that buffer |
| 460 | // gets acquired in updateTexImage. |
| 461 | eglDestroyImageKHR(dpy, image); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 462 | |
| 463 | if (err != OK) { |
| 464 | return err; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | mEglDisplay = dpy; |
| 469 | mEglContext = ctx; |
| 470 | mTexName = tex; |
| 471 | mAttached = true; |
| 472 | |
| 473 | return OK; |
| 474 | } |
| 475 | |
| 476 | status_t SurfaceTexture::syncForReleaseLocked(EGLDisplay dpy) { |
| 477 | ST_LOGV("syncForReleaseLocked"); |
| 478 | |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 479 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 480 | if (useNativeFenceSync) { |
| 481 | EGLSyncKHR sync = eglCreateSyncKHR(dpy, |
| 482 | EGL_SYNC_NATIVE_FENCE_ANDROID, NULL); |
| 483 | if (sync == EGL_NO_SYNC_KHR) { |
| 484 | ST_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", |
| 485 | eglGetError()); |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 486 | return UNKNOWN_ERROR; |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 487 | } |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 488 | glFlush(); |
| 489 | int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync); |
Jamie Gennis | 98ff059 | 2012-09-10 14:49:42 -0700 | [diff] [blame] | 490 | eglDestroySyncKHR(dpy, sync); |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 491 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 492 | ST_LOGE("syncForReleaseLocked: error dup'ing native fence " |
| 493 | "fd: %#x", eglGetError()); |
| 494 | return UNKNOWN_ERROR; |
| 495 | } |
| 496 | sp<Fence> fence(new Fence(fenceFd)); |
| 497 | status_t err = addReleaseFence(mCurrentTexture, fence); |
| 498 | if (err != OK) { |
| 499 | ST_LOGE("syncForReleaseLocked: error adding release fence: " |
| 500 | "%s (%d)", strerror(-err), err); |
| 501 | return err; |
| 502 | } |
| 503 | } else if (mUseFenceSync) { |
| 504 | EGLSyncKHR fence = mEglSlots[mCurrentTexture].mEglFence; |
| 505 | if (fence != EGL_NO_SYNC_KHR) { |
| 506 | // There is already a fence for the current slot. We need to |
| 507 | // wait on that before replacing it with another fence to |
| 508 | // ensure that all outstanding buffer accesses have completed |
| 509 | // before the producer accesses it. |
| 510 | EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000); |
| 511 | if (result == EGL_FALSE) { |
| 512 | ST_LOGE("syncForReleaseLocked: error waiting for previous " |
| 513 | "fence: %#x", eglGetError()); |
| 514 | return UNKNOWN_ERROR; |
| 515 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 516 | ST_LOGE("syncForReleaseLocked: timeout waiting for previous " |
| 517 | "fence"); |
| 518 | return TIMED_OUT; |
| 519 | } |
| 520 | eglDestroySyncKHR(dpy, fence); |
| 521 | } |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 522 | |
Jamie Gennis | 01dbf55 | 2012-09-06 14:54:19 -0700 | [diff] [blame] | 523 | // Create a fence for the outstanding accesses in the current |
| 524 | // OpenGL ES context. |
| 525 | fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL); |
| 526 | if (fence == EGL_NO_SYNC_KHR) { |
| 527 | ST_LOGE("syncForReleaseLocked: error creating fence: %#x", |
| 528 | eglGetError()); |
| 529 | return UNKNOWN_ERROR; |
| 530 | } |
| 531 | glFlush(); |
| 532 | mEglSlots[mCurrentTexture].mEglFence = fence; |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 533 | } |
Jamie Gennis | 74bed55 | 2012-03-28 19:05:54 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | return OK; |
| 537 | } |
| 538 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 539 | bool SurfaceTexture::isExternalFormat(uint32_t format) |
| 540 | { |
| 541 | switch (format) { |
| 542 | // supported YUV formats |
| 543 | case HAL_PIXEL_FORMAT_YV12: |
| 544 | // Legacy/deprecated YUV formats |
| 545 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 546 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 547 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | // Any OEM format needs to be considered |
| 552 | if (format>=0x100 && format<=0x1FF) |
| 553 | return true; |
| 554 | |
| 555 | return false; |
| 556 | } |
| 557 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 558 | GLenum SurfaceTexture::getCurrentTextureTarget() const { |
Jamie Gennis | fb1b5a2 | 2011-09-28 12:13:31 -0700 | [diff] [blame] | 559 | return mTexTarget; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 562 | void SurfaceTexture::getTransformMatrix(float mtx[16]) { |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 563 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 564 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 565 | } |
| 566 | |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 567 | void SurfaceTexture::setFilteringEnabled(bool enabled) { |
| 568 | Mutex::Autolock lock(mMutex); |
| 569 | bool needsRecompute = mFilteringEnabled != enabled; |
| 570 | mFilteringEnabled = enabled; |
| 571 | if (needsRecompute) { |
| 572 | computeCurrentTransformMatrix(); |
| 573 | } |
| 574 | } |
| 575 | |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 576 | void SurfaceTexture::computeCurrentTransformMatrix() { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 577 | ST_LOGV("computeCurrentTransformMatrix"); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 578 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 579 | float xform[16]; |
| 580 | for (int i = 0; i < 16; i++) { |
| 581 | xform[i] = mtxIdentity[i]; |
| 582 | } |
| 583 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 584 | float result[16]; |
| 585 | mtxMul(result, xform, mtxFlipH); |
| 586 | for (int i = 0; i < 16; i++) { |
| 587 | xform[i] = result[i]; |
| 588 | } |
| 589 | } |
| 590 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 591 | float result[16]; |
| 592 | mtxMul(result, xform, mtxFlipV); |
| 593 | for (int i = 0; i < 16; i++) { |
| 594 | xform[i] = result[i]; |
| 595 | } |
| 596 | } |
| 597 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 598 | float result[16]; |
| 599 | mtxMul(result, xform, mtxRot90); |
| 600 | for (int i = 0; i < 16; i++) { |
| 601 | xform[i] = result[i]; |
| 602 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 603 | } |
| 604 | |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 605 | sp<GraphicBuffer>& buf(mCurrentTextureBuf); |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame] | 606 | Rect cropRect = mCurrentCrop; |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 607 | float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f; |
| 608 | float bufferWidth = buf->getWidth(); |
| 609 | float bufferHeight = buf->getHeight(); |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame] | 610 | if (!cropRect.isEmpty()) { |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 611 | float shrinkAmount = 0.0f; |
| 612 | if (mFilteringEnabled) { |
| 613 | // In order to prevent bilinear sampling beyond the edge of the |
| 614 | // crop rectangle we may need to shrink it by 2 texels in each |
| 615 | // dimension. Normally this would just need to take 1/2 a texel |
| 616 | // off each end, but because the chroma channels of YUV420 images |
| 617 | // are subsampled we may need to shrink the crop region by a whole |
| 618 | // texel on each side. |
| 619 | switch (buf->getPixelFormat()) { |
| 620 | case PIXEL_FORMAT_RGBA_8888: |
| 621 | case PIXEL_FORMAT_RGBX_8888: |
| 622 | case PIXEL_FORMAT_RGB_888: |
| 623 | case PIXEL_FORMAT_RGB_565: |
| 624 | case PIXEL_FORMAT_BGRA_8888: |
| 625 | case PIXEL_FORMAT_RGBA_5551: |
| 626 | case PIXEL_FORMAT_RGBA_4444: |
| 627 | // We know there's no subsampling of any channels, so we |
| 628 | // only need to shrink by a half a pixel. |
| 629 | shrinkAmount = 0.5; |
Romain Guy | 4f9c284 | 2012-08-01 19:16:59 -0700 | [diff] [blame] | 630 | break; |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 631 | |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 632 | default: |
| 633 | // If we don't recognize the format, we must assume the |
| 634 | // worst case (that we care about), which is YUV420. |
| 635 | shrinkAmount = 1.0; |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 636 | break; |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame] | 639 | |
Jamie Gennis | 5c1139f | 2012-05-08 16:56:34 -0700 | [diff] [blame] | 640 | // Only shrink the dimensions that are not the size of the buffer. |
| 641 | if (cropRect.width() < bufferWidth) { |
| 642 | tx = (float(cropRect.left) + shrinkAmount) / bufferWidth; |
| 643 | sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) / |
| 644 | bufferWidth; |
| 645 | } |
| 646 | if (cropRect.height() < bufferHeight) { |
| 647 | ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) / |
| 648 | bufferHeight; |
| 649 | sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) / |
| 650 | bufferHeight; |
| 651 | } |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 652 | } |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 653 | float crop[16] = { |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 654 | sx, 0, 0, 0, |
| 655 | 0, sy, 0, 0, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 656 | 0, 0, 1, 0, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 657 | tx, ty, 0, 1, |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 658 | }; |
| 659 | |
Jamie Gennis | a214c64 | 2011-01-14 13:53:31 -0800 | [diff] [blame] | 660 | float mtxBeforeFlipV[16]; |
| 661 | mtxMul(mtxBeforeFlipV, crop, xform); |
| 662 | |
| 663 | // SurfaceFlinger expects the top of its window textures to be at a Y |
| 664 | // coordinate of 0, so SurfaceTexture must behave the same way. We don't |
| 665 | // want to expose this to applications, however, so we must add an |
| 666 | // additional vertical flip to the transform after all the other transforms. |
Jamie Gennis | 736aa95 | 2011-06-12 17:03:06 -0700 | [diff] [blame] | 667 | mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV); |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 670 | nsecs_t SurfaceTexture::getTimestamp() { |
Jamie Gennis | 6ee96ad | 2011-10-19 13:36:31 -0700 | [diff] [blame] | 671 | ST_LOGV("getTimestamp"); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 672 | Mutex::Autolock lock(mMutex); |
| 673 | return mCurrentTimestamp; |
| 674 | } |
| 675 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 676 | EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, |
| 677 | const sp<GraphicBuffer>& graphicBuffer) { |
| 678 | EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer(); |
| 679 | EGLint attrs[] = { |
| 680 | EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
| 681 | EGL_NONE, |
| 682 | }; |
| 683 | EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, |
| 684 | EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs); |
Mathias Agopian | 3cd5a11 | 2011-04-26 14:57:40 -0700 | [diff] [blame] | 685 | if (image == EGL_NO_IMAGE_KHR) { |
| 686 | EGLint error = eglGetError(); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 687 | ST_LOGE("error creating EGLImage: %#x", error); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 688 | } |
| 689 | return image; |
| 690 | } |
| 691 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 692 | sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const { |
| 693 | Mutex::Autolock lock(mMutex); |
| 694 | return mCurrentTextureBuf; |
| 695 | } |
| 696 | |
| 697 | Rect SurfaceTexture::getCurrentCrop() const { |
| 698 | Mutex::Autolock lock(mMutex); |
Daniel Lam | 016c8cb | 2012-04-03 15:54:58 -0700 | [diff] [blame] | 699 | |
| 700 | Rect outCrop = mCurrentCrop; |
| 701 | if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) { |
| 702 | int32_t newWidth = mCurrentCrop.width(); |
| 703 | int32_t newHeight = mCurrentCrop.height(); |
| 704 | |
| 705 | if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) { |
| 706 | newWidth = newHeight * mDefaultWidth / mDefaultHeight; |
| 707 | ST_LOGV("too wide: newWidth = %d", newWidth); |
| 708 | } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) { |
| 709 | newHeight = newWidth * mDefaultHeight / mDefaultWidth; |
| 710 | ST_LOGV("too tall: newHeight = %d", newHeight); |
| 711 | } |
| 712 | |
| 713 | // The crop is too wide |
| 714 | if (newWidth < mCurrentCrop.width()) { |
| 715 | int32_t dw = (newWidth - mCurrentCrop.width())/2; |
| 716 | outCrop.left -=dw; |
| 717 | outCrop.right += dw; |
| 718 | // The crop is too tall |
| 719 | } else if (newHeight < mCurrentCrop.height()) { |
| 720 | int32_t dh = (newHeight - mCurrentCrop.height())/2; |
| 721 | outCrop.top -= dh; |
| 722 | outCrop.bottom += dh; |
| 723 | } |
| 724 | |
| 725 | ST_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]", |
| 726 | outCrop.left, outCrop.top, |
| 727 | outCrop.right,outCrop.bottom); |
| 728 | } |
| 729 | |
Daniel Lam | 016c8cb | 2012-04-03 15:54:58 -0700 | [diff] [blame] | 730 | return outCrop; |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | uint32_t SurfaceTexture::getCurrentTransform() const { |
| 734 | Mutex::Autolock lock(mMutex); |
| 735 | return mCurrentTransform; |
| 736 | } |
| 737 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 738 | uint32_t SurfaceTexture::getCurrentScalingMode() const { |
| 739 | Mutex::Autolock lock(mMutex); |
| 740 | return mCurrentScalingMode; |
| 741 | } |
| 742 | |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 743 | sp<Fence> SurfaceTexture::getCurrentFence() const { |
| 744 | Mutex::Autolock lock(mMutex); |
| 745 | return mCurrentFence; |
| 746 | } |
| 747 | |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 748 | status_t SurfaceTexture::doGLFenceWait() const { |
| 749 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 3941cb2 | 2012-09-17 16:58:17 -0700 | [diff] [blame^] | 750 | return doGLFenceWaitLocked(); |
| 751 | } |
| 752 | |
| 753 | status_t SurfaceTexture::doGLFenceWaitLocked() const { |
Jamie Gennis | 61e04b9 | 2012-09-09 17:48:42 -0700 | [diff] [blame] | 754 | |
| 755 | EGLDisplay dpy = eglGetCurrentDisplay(); |
| 756 | EGLContext ctx = eglGetCurrentContext(); |
| 757 | |
| 758 | if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) { |
| 759 | ST_LOGE("doGLFenceWait: invalid current EGLDisplay"); |
| 760 | return INVALID_OPERATION; |
| 761 | } |
| 762 | |
| 763 | if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) { |
| 764 | ST_LOGE("doGLFenceWait: invalid current EGLContext"); |
| 765 | return INVALID_OPERATION; |
| 766 | } |
| 767 | |
| 768 | if (mCurrentFence != NULL) { |
| 769 | if (useWaitSync) { |
| 770 | // Create an EGLSyncKHR from the current fence. |
| 771 | int fenceFd = mCurrentFence->dup(); |
| 772 | if (fenceFd == -1) { |
| 773 | ST_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno); |
| 774 | return -errno; |
| 775 | } |
| 776 | EGLint attribs[] = { |
| 777 | EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, |
| 778 | EGL_NONE |
| 779 | }; |
| 780 | EGLSyncKHR sync = eglCreateSyncKHR(dpy, |
| 781 | EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| 782 | if (sync == EGL_NO_SYNC_KHR) { |
| 783 | close(fenceFd); |
| 784 | ST_LOGE("doGLFenceWait: error creating EGL fence: %#x", |
| 785 | eglGetError()); |
| 786 | return UNKNOWN_ERROR; |
| 787 | } |
| 788 | |
| 789 | // XXX: The spec draft is inconsistent as to whether this should |
| 790 | // return an EGLint or void. Ignore the return value for now, as |
| 791 | // it's not strictly needed. |
| 792 | eglWaitSyncANDROID(dpy, sync, 0); |
| 793 | EGLint eglErr = eglGetError(); |
| 794 | eglDestroySyncKHR(dpy, sync); |
| 795 | if (eglErr != EGL_SUCCESS) { |
| 796 | ST_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", |
| 797 | eglErr); |
| 798 | return UNKNOWN_ERROR; |
| 799 | } |
| 800 | } else { |
| 801 | status_t err = mCurrentFence->wait(Fence::TIMEOUT_NEVER); |
| 802 | if (err != NO_ERROR) { |
| 803 | ST_LOGE("doGLFenceWait: error waiting for fence: %d", err); |
| 804 | return err; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | return NO_ERROR; |
| 810 | } |
| 811 | |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 812 | bool SurfaceTexture::isSynchronousMode() const { |
| 813 | Mutex::Autolock lock(mMutex); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 814 | return mBufferQueue->isSynchronousMode(); |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 817 | void SurfaceTexture::freeBufferLocked(int slotIndex) { |
| 818 | ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
Daniel Lam | 9abe1eb | 2012-03-26 20:37:15 -0700 | [diff] [blame] | 819 | if (slotIndex == mCurrentTexture) { |
| 820 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 821 | } |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 822 | EGLImageKHR img = mEglSlots[slotIndex].mEglImage; |
Jamie Gennis | 9aa74db | 2012-04-17 13:07:45 -0700 | [diff] [blame] | 823 | if (img != EGL_NO_IMAGE_KHR) { |
| 824 | ST_LOGV("destroying EGLImage dpy=%p img=%p", mEglDisplay, img); |
| 825 | eglDestroyImageKHR(mEglDisplay, img); |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 826 | } |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 827 | mEglSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR; |
| 828 | ConsumerBase::freeBufferLocked(slotIndex); |
Jamie Gennis | fa5b40e | 2012-03-15 14:01:24 -0700 | [diff] [blame] | 829 | } |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 830 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 831 | void SurfaceTexture::abandonLocked() { |
| 832 | ST_LOGV("abandonLocked"); |
| 833 | mCurrentTextureBuf.clear(); |
| 834 | ConsumerBase::abandonLocked(); |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 837 | void SurfaceTexture::setName(const String8& name) { |
Daniel Lam | eae59d2 | 2012-01-22 15:26:27 -0800 | [diff] [blame] | 838 | Mutex::Autolock _l(mMutex); |
Jamie Gennis | fa28c35 | 2011-09-16 17:30:26 -0700 | [diff] [blame] | 839 | mName = name; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 840 | mBufferQueue->setConsumerName(name); |
| 841 | } |
| 842 | |
| 843 | status_t SurfaceTexture::setDefaultBufferFormat(uint32_t defaultFormat) { |
| 844 | Mutex::Autolock lock(mMutex); |
| 845 | return mBufferQueue->setDefaultBufferFormat(defaultFormat); |
| 846 | } |
| 847 | |
| 848 | status_t SurfaceTexture::setConsumerUsageBits(uint32_t usage) { |
| 849 | Mutex::Autolock lock(mMutex); |
Eino-Ville Talvala | 85b2176 | 2012-04-13 15:16:31 -0700 | [diff] [blame] | 850 | usage |= DEFAULT_USAGE_FLAGS; |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 851 | return mBufferQueue->setConsumerUsageBits(usage); |
| 852 | } |
| 853 | |
| 854 | status_t SurfaceTexture::setTransformHint(uint32_t hint) { |
| 855 | Mutex::Autolock lock(mMutex); |
| 856 | return mBufferQueue->setTransformHint(hint); |
| 857 | } |
| 858 | |
| 859 | // Used for refactoring BufferQueue from SurfaceTexture |
| 860 | // Should not be in final interface once users of SurfaceTexture are clean up. |
| 861 | status_t SurfaceTexture::setSynchronousMode(bool enabled) { |
| 862 | Mutex::Autolock lock(mMutex); |
| 863 | return mBufferQueue->setSynchronousMode(enabled); |
| 864 | } |
| 865 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 866 | void SurfaceTexture::dumpLocked(String8& result, const char* prefix, |
| 867 | char* buffer, size_t size) const |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 868 | { |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 869 | snprintf(buffer, size, |
| 870 | "%smTexName=%d mCurrentTexture=%d\n" |
| 871 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 872 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 873 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 874 | mCurrentTransform); |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 875 | result.append(buffer); |
| 876 | |
Jamie Gennis | 9fea342 | 2012-08-07 18:03:04 -0700 | [diff] [blame] | 877 | ConsumerBase::dumpLocked(result, prefix, buffer, size); |
Mathias Agopian | 68c7794 | 2011-05-09 19:08:33 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Jamie Gennis | f238e28 | 2011-01-09 16:33:17 -0800 | [diff] [blame] | 880 | static void mtxMul(float out[16], const float a[16], const float b[16]) { |
| 881 | out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; |
| 882 | out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]; |
| 883 | out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]; |
| 884 | out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]; |
| 885 | |
| 886 | out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]; |
| 887 | out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]; |
| 888 | out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]; |
| 889 | out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]; |
| 890 | |
| 891 | out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]; |
| 892 | out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]; |
| 893 | out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]; |
| 894 | out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]; |
| 895 | |
| 896 | out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]; |
| 897 | out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]; |
| 898 | out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]; |
| 899 | out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]; |
| 900 | } |
| 901 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 902 | }; // namespace android |