gfx/gl/SharedSurfaceGL.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/gl/SharedSurfaceGL.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,298 @@
     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 +#ifndef SHARED_SURFACE_GL_H_
    1.10 +#define SHARED_SURFACE_GL_H_
    1.11 +
    1.12 +#include "ScopedGLHelpers.h"
    1.13 +#include "SharedSurface.h"
    1.14 +#include "SurfaceFactory.h"
    1.15 +#include "SurfaceTypes.h"
    1.16 +#include "GLContextTypes.h"
    1.17 +#include "nsAutoPtr.h"
    1.18 +#include "gfxTypes.h"
    1.19 +#include "mozilla/Mutex.h"
    1.20 +
    1.21 +#include <queue>
    1.22 +
    1.23 +// Forwards:
    1.24 +namespace mozilla {
    1.25 +    namespace gl {
    1.26 +        class GLContext;
    1.27 +    }
    1.28 +    namespace gfx {
    1.29 +        class DataSourceSurface;
    1.30 +    }
    1.31 +}
    1.32 +
    1.33 +namespace mozilla {
    1.34 +namespace gl {
    1.35 +
    1.36 +class SurfaceFactory_GL;
    1.37 +
    1.38 +class SharedSurface_GL
    1.39 +    : public gfx::SharedSurface
    1.40 +{
    1.41 +protected:
    1.42 +    typedef class gfx::SharedSurface SharedSurface;
    1.43 +    typedef gfx::SharedSurfaceType SharedSurfaceType;
    1.44 +    typedef gfx::APITypeT APITypeT;
    1.45 +    typedef gfx::AttachmentType AttachmentType;
    1.46 +
    1.47 +    GLContext* const mGL;
    1.48 +
    1.49 +    SharedSurface_GL(SharedSurfaceType type,
    1.50 +                     AttachmentType attachType,
    1.51 +                     GLContext* gl,
    1.52 +                     const gfx::IntSize& size,
    1.53 +                     bool hasAlpha)
    1.54 +        : SharedSurface(type, APITypeT::OpenGL, attachType, size, hasAlpha)
    1.55 +        , mGL(gl)
    1.56 +    {}
    1.57 +
    1.58 +public:
    1.59 +    static void ProdCopy(SharedSurface_GL* src, SharedSurface_GL* dest,
    1.60 +                         SurfaceFactory_GL* factory);
    1.61 +
    1.62 +    static SharedSurface_GL* Cast(SharedSurface* surf) {
    1.63 +        MOZ_ASSERT(surf->APIType() == APITypeT::OpenGL);
    1.64 +
    1.65 +        return (SharedSurface_GL*)surf;
    1.66 +    }
    1.67 +
    1.68 +    // For use when AttachType is correct.
    1.69 +    virtual GLuint ProdTexture() {
    1.70 +        MOZ_ASSERT(AttachType() == AttachmentType::GLTexture);
    1.71 +        MOZ_CRASH("Did you forget to override this function?");
    1.72 +    }
    1.73 +
    1.74 +    virtual GLenum ProdTextureTarget() const {
    1.75 +        return LOCAL_GL_TEXTURE_2D;
    1.76 +    }
    1.77 +
    1.78 +    virtual GLuint ProdRenderbuffer() {
    1.79 +        MOZ_ASSERT(AttachType() == AttachmentType::GLRenderbuffer);
    1.80 +        MOZ_CRASH("Did you forget to override this function?");
    1.81 +    }
    1.82 +
    1.83 +    virtual bool ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
    1.84 +                            GLenum format, GLenum type, GLvoid *pixels) {
    1.85 +        return false;
    1.86 +    }
    1.87 +
    1.88 +    virtual void LockProd() MOZ_OVERRIDE;
    1.89 +    virtual void UnlockProd() MOZ_OVERRIDE;
    1.90 +
    1.91 +    GLContext* GL() const {
    1.92 +        return mGL;
    1.93 +    }
    1.94 +};
    1.95 +
    1.96 +class SurfaceFactory_GL
    1.97 +    : public gfx::SurfaceFactory
    1.98 +{
    1.99 +protected:
   1.100 +    typedef struct gfx::SurfaceCaps SurfaceCaps;
   1.101 +    typedef class gfx::SurfaceFactory SurfaceFactory;
   1.102 +    typedef class gfx::SharedSurface SharedSurface;
   1.103 +    typedef gfx::SharedSurfaceType SharedSurfaceType;
   1.104 +
   1.105 +    GLContext* const mGL;
   1.106 +    const GLFormats mFormats;
   1.107 +
   1.108 +    SurfaceCaps mDrawCaps;
   1.109 +    SurfaceCaps mReadCaps;
   1.110 +
   1.111 +    // This uses ChooseBufferBits to pick drawBits/readBits.
   1.112 +    SurfaceFactory_GL(GLContext* gl,
   1.113 +                      SharedSurfaceType type,
   1.114 +                      const SurfaceCaps& caps);
   1.115 +
   1.116 +    virtual void ChooseBufferBits(const SurfaceCaps& caps,
   1.117 +                                  SurfaceCaps& drawCaps,
   1.118 +                                  SurfaceCaps& readCaps) const;
   1.119 +
   1.120 +public:
   1.121 +    GLContext* GL() const {
   1.122 +        return mGL;
   1.123 +    }
   1.124 +
   1.125 +    const GLFormats& Formats() const {
   1.126 +        return mFormats;
   1.127 +    }
   1.128 +
   1.129 +    const SurfaceCaps& DrawCaps() const {
   1.130 +        return mDrawCaps;
   1.131 +    }
   1.132 +
   1.133 +    const SurfaceCaps& ReadCaps() const {
   1.134 +        return mReadCaps;
   1.135 +    }
   1.136 +};
   1.137 +
   1.138 +// For readback and bootstrapping:
   1.139 +class SharedSurface_Basic
   1.140 +    : public SharedSurface_GL
   1.141 +{
   1.142 +public:
   1.143 +    static SharedSurface_Basic* Create(GLContext* gl,
   1.144 +                                       const GLFormats& formats,
   1.145 +                                       const gfx::IntSize& size,
   1.146 +                                       bool hasAlpha);
   1.147 +
   1.148 +    static SharedSurface_Basic* Cast(SharedSurface* surf) {
   1.149 +        MOZ_ASSERT(surf->Type() == SharedSurfaceType::Basic);
   1.150 +
   1.151 +        return (SharedSurface_Basic*)surf;
   1.152 +    }
   1.153 +
   1.154 +protected:
   1.155 +    const GLuint mTex;
   1.156 +    GLuint mFB;
   1.157 +
   1.158 +    RefPtr<gfx::DataSourceSurface> mData;
   1.159 +
   1.160 +    SharedSurface_Basic(GLContext* gl,
   1.161 +                        const gfx::IntSize& size,
   1.162 +                        bool hasAlpha,
   1.163 +                        gfx::SurfaceFormat format,
   1.164 +                        GLuint tex);
   1.165 +
   1.166 +public:
   1.167 +    virtual ~SharedSurface_Basic();
   1.168 +
   1.169 +    virtual void LockProdImpl() MOZ_OVERRIDE {}
   1.170 +    virtual void UnlockProdImpl() MOZ_OVERRIDE {}
   1.171 +
   1.172 +
   1.173 +    virtual void Fence() MOZ_OVERRIDE;
   1.174 +
   1.175 +    virtual bool WaitSync() MOZ_OVERRIDE {
   1.176 +        // Since we already store the data in Fence, we're always done already.
   1.177 +        return true;
   1.178 +    }
   1.179 +
   1.180 +    virtual GLuint ProdTexture() MOZ_OVERRIDE {
   1.181 +        return mTex;
   1.182 +    }
   1.183 +
   1.184 +    // Implementation-specific functions below:
   1.185 +    gfx::DataSourceSurface* GetData() {
   1.186 +        return mData;
   1.187 +    }
   1.188 +};
   1.189 +
   1.190 +class SurfaceFactory_Basic
   1.191 +    : public SurfaceFactory_GL
   1.192 +{
   1.193 +public:
   1.194 +    SurfaceFactory_Basic(GLContext* gl, const SurfaceCaps& caps)
   1.195 +        : SurfaceFactory_GL(gl, SharedSurfaceType::Basic, caps)
   1.196 +    {}
   1.197 +
   1.198 +    virtual SharedSurface* CreateShared(const gfx::IntSize& size) MOZ_OVERRIDE {
   1.199 +        bool hasAlpha = mReadCaps.alpha;
   1.200 +        return SharedSurface_Basic::Create(mGL, mFormats, size, hasAlpha);
   1.201 +    }
   1.202 +};
   1.203 +
   1.204 +
   1.205 +// Using shared GL textures:
   1.206 +class SharedSurface_GLTexture
   1.207 +    : public SharedSurface_GL
   1.208 +{
   1.209 +public:
   1.210 +    static SharedSurface_GLTexture* Create(GLContext* prodGL,
   1.211 +                                           GLContext* consGL,
   1.212 +                                           const GLFormats& formats,
   1.213 +                                           const gfx::IntSize& size,
   1.214 +                                           bool hasAlpha,
   1.215 +                                           GLuint texture = 0);
   1.216 +
   1.217 +    static SharedSurface_GLTexture* Cast(SharedSurface* surf) {
   1.218 +        MOZ_ASSERT(surf->Type() == SharedSurfaceType::GLTextureShare);
   1.219 +
   1.220 +        return (SharedSurface_GLTexture*)surf;
   1.221 +    }
   1.222 +
   1.223 +protected:
   1.224 +    GLContext* mConsGL;
   1.225 +    const GLuint mTex;
   1.226 +    const bool mOwnsTex;
   1.227 +    GLsync mSync;
   1.228 +    mutable Mutex mMutex;
   1.229 +
   1.230 +    SharedSurface_GLTexture(GLContext* prodGL,
   1.231 +                            GLContext* consGL,
   1.232 +                            const gfx::IntSize& size,
   1.233 +                            bool hasAlpha,
   1.234 +                            GLuint tex,
   1.235 +                            bool ownsTex)
   1.236 +        : SharedSurface_GL(SharedSurfaceType::GLTextureShare,
   1.237 +                           AttachmentType::GLTexture,
   1.238 +                           prodGL,
   1.239 +                           size,
   1.240 +                           hasAlpha)
   1.241 +        , mConsGL(consGL)
   1.242 +        , mTex(tex)
   1.243 +        , mOwnsTex(ownsTex)
   1.244 +        , mSync(0)
   1.245 +        , mMutex("SharedSurface_GLTexture mutex")
   1.246 +    {
   1.247 +    }
   1.248 +
   1.249 +public:
   1.250 +    virtual ~SharedSurface_GLTexture();
   1.251 +
   1.252 +    virtual void LockProdImpl() MOZ_OVERRIDE {}
   1.253 +    virtual void UnlockProdImpl() MOZ_OVERRIDE {}
   1.254 +
   1.255 +
   1.256 +    virtual void Fence() MOZ_OVERRIDE;
   1.257 +    virtual bool WaitSync() MOZ_OVERRIDE;
   1.258 +
   1.259 +
   1.260 +    virtual GLuint ProdTexture() MOZ_OVERRIDE {
   1.261 +        return mTex;
   1.262 +    }
   1.263 +
   1.264 +    // Custom:
   1.265 +
   1.266 +    GLuint ConsTexture(GLContext* consGL);
   1.267 +
   1.268 +    GLenum ConsTextureTarget() const {
   1.269 +        return ProdTextureTarget();
   1.270 +    }
   1.271 +};
   1.272 +
   1.273 +class SurfaceFactory_GLTexture
   1.274 +    : public SurfaceFactory_GL
   1.275 +{
   1.276 +protected:
   1.277 +    GLContext* const mConsGL;
   1.278 +
   1.279 +public:
   1.280 +    // If we don't know `consGL` at construction time, use `nullptr`, and call
   1.281 +    // `SetConsumerGL()` on each `SharedSurface_GLTexture` before calling its
   1.282 +    // `WaitSync()`.
   1.283 +    SurfaceFactory_GLTexture(GLContext* prodGL,
   1.284 +                             GLContext* consGL,
   1.285 +                             const SurfaceCaps& caps)
   1.286 +        : SurfaceFactory_GL(prodGL, SharedSurfaceType::GLTextureShare, caps)
   1.287 +        , mConsGL(consGL)
   1.288 +    {
   1.289 +        MOZ_ASSERT(consGL != prodGL);
   1.290 +    }
   1.291 +
   1.292 +    virtual SharedSurface* CreateShared(const gfx::IntSize& size) MOZ_OVERRIDE {
   1.293 +        bool hasAlpha = mReadCaps.alpha;
   1.294 +        return SharedSurface_GLTexture::Create(mGL, mConsGL, mFormats, size, hasAlpha);
   1.295 +    }
   1.296 +};
   1.297 +
   1.298 +} /* namespace gfx */
   1.299 +} /* namespace mozilla */
   1.300 +
   1.301 +#endif /* SHARED_SURFACE_GL_H_ */

mercurial