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