gfx/gl/SharedSurfaceEGL.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/gl/SharedSurfaceEGL.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "SharedSurfaceEGL.h"
    1.10 +#include "GLContextEGL.h"
    1.11 +#include "GLBlitHelper.h"
    1.12 +#include "ScopedGLHelpers.h"
    1.13 +#include "SharedSurfaceGL.h"
    1.14 +#include "SurfaceFactory.h"
    1.15 +#include "GLLibraryEGL.h"
    1.16 +#include "TextureGarbageBin.h"
    1.17 +#include "GLReadTexImageHelper.h"
    1.18 +
    1.19 +using namespace mozilla::gfx;
    1.20 +
    1.21 +namespace mozilla {
    1.22 +namespace gl {
    1.23 +
    1.24 +SharedSurface_EGLImage*
    1.25 +SharedSurface_EGLImage::Create(GLContext* prodGL,
    1.26 +                               const GLFormats& formats,
    1.27 +                               const gfx::IntSize& size,
    1.28 +                               bool hasAlpha,
    1.29 +                               EGLContext context)
    1.30 +{
    1.31 +    GLLibraryEGL* egl = &sEGLLibrary;
    1.32 +    MOZ_ASSERT(egl);
    1.33 +    MOZ_ASSERT(context);
    1.34 +
    1.35 +    if (!HasExtensions(egl, prodGL)) {
    1.36 +        return nullptr;
    1.37 +    }
    1.38 +
    1.39 +    MOZ_ALWAYS_TRUE(prodGL->MakeCurrent());
    1.40 +    GLuint prodTex = CreateTextureForOffscreen(prodGL, formats, size);
    1.41 +    if (!prodTex) {
    1.42 +        return nullptr;
    1.43 +    }
    1.44 +
    1.45 +    EGLClientBuffer buffer = reinterpret_cast<EGLClientBuffer>(prodTex);
    1.46 +    EGLImage image = egl->fCreateImage(egl->Display(), context,
    1.47 +                                       LOCAL_EGL_GL_TEXTURE_2D, buffer,
    1.48 +                                       nullptr);
    1.49 +    if (!image) {
    1.50 +        prodGL->fDeleteTextures(1, &prodTex);
    1.51 +        return nullptr;
    1.52 +    }
    1.53 +
    1.54 +    return new SharedSurface_EGLImage(prodGL, egl,
    1.55 +                                      size, hasAlpha,
    1.56 +                                      formats, prodTex, image);
    1.57 +}
    1.58 +
    1.59 +
    1.60 +bool
    1.61 +SharedSurface_EGLImage::HasExtensions(GLLibraryEGL* egl, GLContext* gl)
    1.62 +{
    1.63 +    return egl->HasKHRImageBase() &&
    1.64 +           egl->IsExtensionSupported(GLLibraryEGL::KHR_gl_texture_2D_image) &&
    1.65 +           gl->IsExtensionSupported(GLContext::OES_EGL_image_external);
    1.66 +}
    1.67 +
    1.68 +SharedSurface_EGLImage::SharedSurface_EGLImage(GLContext* gl,
    1.69 +                                               GLLibraryEGL* egl,
    1.70 +                                               const gfx::IntSize& size,
    1.71 +                                               bool hasAlpha,
    1.72 +                                               const GLFormats& formats,
    1.73 +                                               GLuint prodTex,
    1.74 +                                               EGLImage image)
    1.75 +    : SharedSurface_GL(SharedSurfaceType::EGLImageShare,
    1.76 +                        AttachmentType::GLTexture,
    1.77 +                        gl,
    1.78 +                        size,
    1.79 +                        hasAlpha)
    1.80 +    , mMutex("SharedSurface_EGLImage mutex")
    1.81 +    , mEGL(egl)
    1.82 +    , mFormats(formats)
    1.83 +    , mProdTex(prodTex)
    1.84 +    , mImage(image)
    1.85 +    , mCurConsGL(nullptr)
    1.86 +    , mConsTex(0)
    1.87 +    , mSync(0)
    1.88 +{}
    1.89 +
    1.90 +SharedSurface_EGLImage::~SharedSurface_EGLImage()
    1.91 +{
    1.92 +    mEGL->fDestroyImage(Display(), mImage);
    1.93 +    mImage = 0;
    1.94 +
    1.95 +    mGL->MakeCurrent();
    1.96 +    mGL->fDeleteTextures(1, &mProdTex);
    1.97 +    mProdTex = 0;
    1.98 +
    1.99 +    if (mConsTex) {
   1.100 +        MOZ_ASSERT(mGarbageBin);
   1.101 +        mGarbageBin->Trash(mConsTex);
   1.102 +        mConsTex = 0;
   1.103 +    }
   1.104 +
   1.105 +    if (mSync) {
   1.106 +        // We can't call this unless we have the ext, but we will always have
   1.107 +        // the ext if we have something to destroy.
   1.108 +        mEGL->fDestroySync(Display(), mSync);
   1.109 +        mSync = 0;
   1.110 +    }
   1.111 +}
   1.112 +
   1.113 +void
   1.114 +SharedSurface_EGLImage::Fence()
   1.115 +{
   1.116 +    MutexAutoLock lock(mMutex);
   1.117 +    mGL->MakeCurrent();
   1.118 +
   1.119 +    if (mEGL->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync) &&
   1.120 +        mGL->IsExtensionSupported(GLContext::OES_EGL_sync))
   1.121 +    {
   1.122 +        if (mSync) {
   1.123 +            MOZ_ALWAYS_TRUE( mEGL->fDestroySync(Display(), mSync) );
   1.124 +            mSync = 0;
   1.125 +        }
   1.126 +
   1.127 +        mSync = mEGL->fCreateSync(Display(),
   1.128 +                                  LOCAL_EGL_SYNC_FENCE,
   1.129 +                                  nullptr);
   1.130 +        if (mSync) {
   1.131 +            mGL->fFlush();
   1.132 +            return;
   1.133 +        }
   1.134 +    }
   1.135 +
   1.136 +    MOZ_ASSERT(!mSync);
   1.137 +    mGL->fFinish();
   1.138 +}
   1.139 +
   1.140 +bool
   1.141 +SharedSurface_EGLImage::WaitSync()
   1.142 +{
   1.143 +    MutexAutoLock lock(mMutex);
   1.144 +    if (!mSync) {
   1.145 +        // We must not be needed.
   1.146 +        return true;
   1.147 +    }
   1.148 +    MOZ_ASSERT(mEGL->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync));
   1.149 +
   1.150 +    // Wait FOREVER, primarily because some NVIDIA (at least Tegra) drivers
   1.151 +    // have ClientWaitSync returning immediately if the timeout delay is anything
   1.152 +    // else than FOREVER.
   1.153 +    //
   1.154 +    // FIXME: should we try to use a finite timeout delay where possible?
   1.155 +    EGLint status = mEGL->fClientWaitSync(Display(),
   1.156 +                                          mSync,
   1.157 +                                          0,
   1.158 +                                          LOCAL_EGL_FOREVER);
   1.159 +
   1.160 +    if (status != LOCAL_EGL_CONDITION_SATISFIED) {
   1.161 +        return false;
   1.162 +    }
   1.163 +
   1.164 +    MOZ_ALWAYS_TRUE( mEGL->fDestroySync(Display(), mSync) );
   1.165 +    mSync = 0;
   1.166 +
   1.167 +    return true;
   1.168 +}
   1.169 +
   1.170 +
   1.171 +EGLDisplay
   1.172 +SharedSurface_EGLImage::Display() const
   1.173 +{
   1.174 +    return mEGL->Display();
   1.175 +}
   1.176 +
   1.177 +void
   1.178 +SharedSurface_EGLImage::AcquireConsumerTexture(GLContext* consGL, GLuint* out_texture, GLuint* out_target)
   1.179 +{
   1.180 +    MutexAutoLock lock(mMutex);
   1.181 +    MOZ_ASSERT(!mCurConsGL || consGL == mCurConsGL);
   1.182 +
   1.183 +    if (!mConsTex) {
   1.184 +        consGL->fGenTextures(1, &mConsTex);
   1.185 +        MOZ_ASSERT(mConsTex);
   1.186 +
   1.187 +        ScopedBindTexture autoTex(consGL, mConsTex, LOCAL_GL_TEXTURE_EXTERNAL);
   1.188 +        consGL->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_EXTERNAL, mImage);
   1.189 +
   1.190 +        mCurConsGL = consGL;
   1.191 +        mGarbageBin = consGL->TexGarbageBin();
   1.192 +    }
   1.193 +
   1.194 +    MOZ_ASSERT(consGL == mCurConsGL);
   1.195 +    *out_texture = mConsTex;
   1.196 +    *out_target = LOCAL_GL_TEXTURE_EXTERNAL;
   1.197 +}
   1.198 +
   1.199 +
   1.200 +SurfaceFactory_EGLImage*
   1.201 +SurfaceFactory_EGLImage::Create(GLContext* prodGL,
   1.202 +                                        const SurfaceCaps& caps)
   1.203 +{
   1.204 +    EGLContext context = GLContextEGL::Cast(prodGL)->GetEGLContext();
   1.205 +
   1.206 +    GLLibraryEGL* egl = &sEGLLibrary;
   1.207 +    if (!SharedSurface_EGLImage::HasExtensions(egl, prodGL)) {
   1.208 +        return nullptr;
   1.209 +    }
   1.210 +
   1.211 +    return new SurfaceFactory_EGLImage(prodGL, context, caps);
   1.212 +}
   1.213 +
   1.214 +} /* namespace gfx */
   1.215 +} /* namespace mozilla */

mercurial