Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "SharedSurfaceEGL.h" |
michael@0 | 7 | #include "GLContextEGL.h" |
michael@0 | 8 | #include "GLBlitHelper.h" |
michael@0 | 9 | #include "ScopedGLHelpers.h" |
michael@0 | 10 | #include "SharedSurfaceGL.h" |
michael@0 | 11 | #include "SurfaceFactory.h" |
michael@0 | 12 | #include "GLLibraryEGL.h" |
michael@0 | 13 | #include "TextureGarbageBin.h" |
michael@0 | 14 | #include "GLReadTexImageHelper.h" |
michael@0 | 15 | |
michael@0 | 16 | using namespace mozilla::gfx; |
michael@0 | 17 | |
michael@0 | 18 | namespace mozilla { |
michael@0 | 19 | namespace gl { |
michael@0 | 20 | |
michael@0 | 21 | SharedSurface_EGLImage* |
michael@0 | 22 | SharedSurface_EGLImage::Create(GLContext* prodGL, |
michael@0 | 23 | const GLFormats& formats, |
michael@0 | 24 | const gfx::IntSize& size, |
michael@0 | 25 | bool hasAlpha, |
michael@0 | 26 | EGLContext context) |
michael@0 | 27 | { |
michael@0 | 28 | GLLibraryEGL* egl = &sEGLLibrary; |
michael@0 | 29 | MOZ_ASSERT(egl); |
michael@0 | 30 | MOZ_ASSERT(context); |
michael@0 | 31 | |
michael@0 | 32 | if (!HasExtensions(egl, prodGL)) { |
michael@0 | 33 | return nullptr; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | MOZ_ALWAYS_TRUE(prodGL->MakeCurrent()); |
michael@0 | 37 | GLuint prodTex = CreateTextureForOffscreen(prodGL, formats, size); |
michael@0 | 38 | if (!prodTex) { |
michael@0 | 39 | return nullptr; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | EGLClientBuffer buffer = reinterpret_cast<EGLClientBuffer>(prodTex); |
michael@0 | 43 | EGLImage image = egl->fCreateImage(egl->Display(), context, |
michael@0 | 44 | LOCAL_EGL_GL_TEXTURE_2D, buffer, |
michael@0 | 45 | nullptr); |
michael@0 | 46 | if (!image) { |
michael@0 | 47 | prodGL->fDeleteTextures(1, &prodTex); |
michael@0 | 48 | return nullptr; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | return new SharedSurface_EGLImage(prodGL, egl, |
michael@0 | 52 | size, hasAlpha, |
michael@0 | 53 | formats, prodTex, image); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | bool |
michael@0 | 58 | SharedSurface_EGLImage::HasExtensions(GLLibraryEGL* egl, GLContext* gl) |
michael@0 | 59 | { |
michael@0 | 60 | return egl->HasKHRImageBase() && |
michael@0 | 61 | egl->IsExtensionSupported(GLLibraryEGL::KHR_gl_texture_2D_image) && |
michael@0 | 62 | gl->IsExtensionSupported(GLContext::OES_EGL_image_external); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | SharedSurface_EGLImage::SharedSurface_EGLImage(GLContext* gl, |
michael@0 | 66 | GLLibraryEGL* egl, |
michael@0 | 67 | const gfx::IntSize& size, |
michael@0 | 68 | bool hasAlpha, |
michael@0 | 69 | const GLFormats& formats, |
michael@0 | 70 | GLuint prodTex, |
michael@0 | 71 | EGLImage image) |
michael@0 | 72 | : SharedSurface_GL(SharedSurfaceType::EGLImageShare, |
michael@0 | 73 | AttachmentType::GLTexture, |
michael@0 | 74 | gl, |
michael@0 | 75 | size, |
michael@0 | 76 | hasAlpha) |
michael@0 | 77 | , mMutex("SharedSurface_EGLImage mutex") |
michael@0 | 78 | , mEGL(egl) |
michael@0 | 79 | , mFormats(formats) |
michael@0 | 80 | , mProdTex(prodTex) |
michael@0 | 81 | , mImage(image) |
michael@0 | 82 | , mCurConsGL(nullptr) |
michael@0 | 83 | , mConsTex(0) |
michael@0 | 84 | , mSync(0) |
michael@0 | 85 | {} |
michael@0 | 86 | |
michael@0 | 87 | SharedSurface_EGLImage::~SharedSurface_EGLImage() |
michael@0 | 88 | { |
michael@0 | 89 | mEGL->fDestroyImage(Display(), mImage); |
michael@0 | 90 | mImage = 0; |
michael@0 | 91 | |
michael@0 | 92 | mGL->MakeCurrent(); |
michael@0 | 93 | mGL->fDeleteTextures(1, &mProdTex); |
michael@0 | 94 | mProdTex = 0; |
michael@0 | 95 | |
michael@0 | 96 | if (mConsTex) { |
michael@0 | 97 | MOZ_ASSERT(mGarbageBin); |
michael@0 | 98 | mGarbageBin->Trash(mConsTex); |
michael@0 | 99 | mConsTex = 0; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | if (mSync) { |
michael@0 | 103 | // We can't call this unless we have the ext, but we will always have |
michael@0 | 104 | // the ext if we have something to destroy. |
michael@0 | 105 | mEGL->fDestroySync(Display(), mSync); |
michael@0 | 106 | mSync = 0; |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | void |
michael@0 | 111 | SharedSurface_EGLImage::Fence() |
michael@0 | 112 | { |
michael@0 | 113 | MutexAutoLock lock(mMutex); |
michael@0 | 114 | mGL->MakeCurrent(); |
michael@0 | 115 | |
michael@0 | 116 | if (mEGL->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync) && |
michael@0 | 117 | mGL->IsExtensionSupported(GLContext::OES_EGL_sync)) |
michael@0 | 118 | { |
michael@0 | 119 | if (mSync) { |
michael@0 | 120 | MOZ_ALWAYS_TRUE( mEGL->fDestroySync(Display(), mSync) ); |
michael@0 | 121 | mSync = 0; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | mSync = mEGL->fCreateSync(Display(), |
michael@0 | 125 | LOCAL_EGL_SYNC_FENCE, |
michael@0 | 126 | nullptr); |
michael@0 | 127 | if (mSync) { |
michael@0 | 128 | mGL->fFlush(); |
michael@0 | 129 | return; |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | MOZ_ASSERT(!mSync); |
michael@0 | 134 | mGL->fFinish(); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | bool |
michael@0 | 138 | SharedSurface_EGLImage::WaitSync() |
michael@0 | 139 | { |
michael@0 | 140 | MutexAutoLock lock(mMutex); |
michael@0 | 141 | if (!mSync) { |
michael@0 | 142 | // We must not be needed. |
michael@0 | 143 | return true; |
michael@0 | 144 | } |
michael@0 | 145 | MOZ_ASSERT(mEGL->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync)); |
michael@0 | 146 | |
michael@0 | 147 | // Wait FOREVER, primarily because some NVIDIA (at least Tegra) drivers |
michael@0 | 148 | // have ClientWaitSync returning immediately if the timeout delay is anything |
michael@0 | 149 | // else than FOREVER. |
michael@0 | 150 | // |
michael@0 | 151 | // FIXME: should we try to use a finite timeout delay where possible? |
michael@0 | 152 | EGLint status = mEGL->fClientWaitSync(Display(), |
michael@0 | 153 | mSync, |
michael@0 | 154 | 0, |
michael@0 | 155 | LOCAL_EGL_FOREVER); |
michael@0 | 156 | |
michael@0 | 157 | if (status != LOCAL_EGL_CONDITION_SATISFIED) { |
michael@0 | 158 | return false; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | MOZ_ALWAYS_TRUE( mEGL->fDestroySync(Display(), mSync) ); |
michael@0 | 162 | mSync = 0; |
michael@0 | 163 | |
michael@0 | 164 | return true; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | |
michael@0 | 168 | EGLDisplay |
michael@0 | 169 | SharedSurface_EGLImage::Display() const |
michael@0 | 170 | { |
michael@0 | 171 | return mEGL->Display(); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | void |
michael@0 | 175 | SharedSurface_EGLImage::AcquireConsumerTexture(GLContext* consGL, GLuint* out_texture, GLuint* out_target) |
michael@0 | 176 | { |
michael@0 | 177 | MutexAutoLock lock(mMutex); |
michael@0 | 178 | MOZ_ASSERT(!mCurConsGL || consGL == mCurConsGL); |
michael@0 | 179 | |
michael@0 | 180 | if (!mConsTex) { |
michael@0 | 181 | consGL->fGenTextures(1, &mConsTex); |
michael@0 | 182 | MOZ_ASSERT(mConsTex); |
michael@0 | 183 | |
michael@0 | 184 | ScopedBindTexture autoTex(consGL, mConsTex, LOCAL_GL_TEXTURE_EXTERNAL); |
michael@0 | 185 | consGL->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_EXTERNAL, mImage); |
michael@0 | 186 | |
michael@0 | 187 | mCurConsGL = consGL; |
michael@0 | 188 | mGarbageBin = consGL->TexGarbageBin(); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | MOZ_ASSERT(consGL == mCurConsGL); |
michael@0 | 192 | *out_texture = mConsTex; |
michael@0 | 193 | *out_target = LOCAL_GL_TEXTURE_EXTERNAL; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | |
michael@0 | 197 | SurfaceFactory_EGLImage* |
michael@0 | 198 | SurfaceFactory_EGLImage::Create(GLContext* prodGL, |
michael@0 | 199 | const SurfaceCaps& caps) |
michael@0 | 200 | { |
michael@0 | 201 | EGLContext context = GLContextEGL::Cast(prodGL)->GetEGLContext(); |
michael@0 | 202 | |
michael@0 | 203 | GLLibraryEGL* egl = &sEGLLibrary; |
michael@0 | 204 | if (!SharedSurface_EGLImage::HasExtensions(egl, prodGL)) { |
michael@0 | 205 | return nullptr; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | return new SurfaceFactory_EGLImage(prodGL, context, caps); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | } /* namespace gfx */ |
michael@0 | 212 | } /* namespace mozilla */ |