gfx/gl/SharedSurfaceGL.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 #ifndef SHARED_SURFACE_GL_H_
michael@0 7 #define SHARED_SURFACE_GL_H_
michael@0 8
michael@0 9 #include "ScopedGLHelpers.h"
michael@0 10 #include "SharedSurface.h"
michael@0 11 #include "SurfaceFactory.h"
michael@0 12 #include "SurfaceTypes.h"
michael@0 13 #include "GLContextTypes.h"
michael@0 14 #include "nsAutoPtr.h"
michael@0 15 #include "gfxTypes.h"
michael@0 16 #include "mozilla/Mutex.h"
michael@0 17
michael@0 18 #include <queue>
michael@0 19
michael@0 20 // Forwards:
michael@0 21 namespace mozilla {
michael@0 22 namespace gl {
michael@0 23 class GLContext;
michael@0 24 }
michael@0 25 namespace gfx {
michael@0 26 class DataSourceSurface;
michael@0 27 }
michael@0 28 }
michael@0 29
michael@0 30 namespace mozilla {
michael@0 31 namespace gl {
michael@0 32
michael@0 33 class SurfaceFactory_GL;
michael@0 34
michael@0 35 class SharedSurface_GL
michael@0 36 : public gfx::SharedSurface
michael@0 37 {
michael@0 38 protected:
michael@0 39 typedef class gfx::SharedSurface SharedSurface;
michael@0 40 typedef gfx::SharedSurfaceType SharedSurfaceType;
michael@0 41 typedef gfx::APITypeT APITypeT;
michael@0 42 typedef gfx::AttachmentType AttachmentType;
michael@0 43
michael@0 44 GLContext* const mGL;
michael@0 45
michael@0 46 SharedSurface_GL(SharedSurfaceType type,
michael@0 47 AttachmentType attachType,
michael@0 48 GLContext* gl,
michael@0 49 const gfx::IntSize& size,
michael@0 50 bool hasAlpha)
michael@0 51 : SharedSurface(type, APITypeT::OpenGL, attachType, size, hasAlpha)
michael@0 52 , mGL(gl)
michael@0 53 {}
michael@0 54
michael@0 55 public:
michael@0 56 static void ProdCopy(SharedSurface_GL* src, SharedSurface_GL* dest,
michael@0 57 SurfaceFactory_GL* factory);
michael@0 58
michael@0 59 static SharedSurface_GL* Cast(SharedSurface* surf) {
michael@0 60 MOZ_ASSERT(surf->APIType() == APITypeT::OpenGL);
michael@0 61
michael@0 62 return (SharedSurface_GL*)surf;
michael@0 63 }
michael@0 64
michael@0 65 // For use when AttachType is correct.
michael@0 66 virtual GLuint ProdTexture() {
michael@0 67 MOZ_ASSERT(AttachType() == AttachmentType::GLTexture);
michael@0 68 MOZ_CRASH("Did you forget to override this function?");
michael@0 69 }
michael@0 70
michael@0 71 virtual GLenum ProdTextureTarget() const {
michael@0 72 return LOCAL_GL_TEXTURE_2D;
michael@0 73 }
michael@0 74
michael@0 75 virtual GLuint ProdRenderbuffer() {
michael@0 76 MOZ_ASSERT(AttachType() == AttachmentType::GLRenderbuffer);
michael@0 77 MOZ_CRASH("Did you forget to override this function?");
michael@0 78 }
michael@0 79
michael@0 80 virtual bool ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
michael@0 81 GLenum format, GLenum type, GLvoid *pixels) {
michael@0 82 return false;
michael@0 83 }
michael@0 84
michael@0 85 virtual void LockProd() MOZ_OVERRIDE;
michael@0 86 virtual void UnlockProd() MOZ_OVERRIDE;
michael@0 87
michael@0 88 GLContext* GL() const {
michael@0 89 return mGL;
michael@0 90 }
michael@0 91 };
michael@0 92
michael@0 93 class SurfaceFactory_GL
michael@0 94 : public gfx::SurfaceFactory
michael@0 95 {
michael@0 96 protected:
michael@0 97 typedef struct gfx::SurfaceCaps SurfaceCaps;
michael@0 98 typedef class gfx::SurfaceFactory SurfaceFactory;
michael@0 99 typedef class gfx::SharedSurface SharedSurface;
michael@0 100 typedef gfx::SharedSurfaceType SharedSurfaceType;
michael@0 101
michael@0 102 GLContext* const mGL;
michael@0 103 const GLFormats mFormats;
michael@0 104
michael@0 105 SurfaceCaps mDrawCaps;
michael@0 106 SurfaceCaps mReadCaps;
michael@0 107
michael@0 108 // This uses ChooseBufferBits to pick drawBits/readBits.
michael@0 109 SurfaceFactory_GL(GLContext* gl,
michael@0 110 SharedSurfaceType type,
michael@0 111 const SurfaceCaps& caps);
michael@0 112
michael@0 113 virtual void ChooseBufferBits(const SurfaceCaps& caps,
michael@0 114 SurfaceCaps& drawCaps,
michael@0 115 SurfaceCaps& readCaps) const;
michael@0 116
michael@0 117 public:
michael@0 118 GLContext* GL() const {
michael@0 119 return mGL;
michael@0 120 }
michael@0 121
michael@0 122 const GLFormats& Formats() const {
michael@0 123 return mFormats;
michael@0 124 }
michael@0 125
michael@0 126 const SurfaceCaps& DrawCaps() const {
michael@0 127 return mDrawCaps;
michael@0 128 }
michael@0 129
michael@0 130 const SurfaceCaps& ReadCaps() const {
michael@0 131 return mReadCaps;
michael@0 132 }
michael@0 133 };
michael@0 134
michael@0 135 // For readback and bootstrapping:
michael@0 136 class SharedSurface_Basic
michael@0 137 : public SharedSurface_GL
michael@0 138 {
michael@0 139 public:
michael@0 140 static SharedSurface_Basic* Create(GLContext* gl,
michael@0 141 const GLFormats& formats,
michael@0 142 const gfx::IntSize& size,
michael@0 143 bool hasAlpha);
michael@0 144
michael@0 145 static SharedSurface_Basic* Cast(SharedSurface* surf) {
michael@0 146 MOZ_ASSERT(surf->Type() == SharedSurfaceType::Basic);
michael@0 147
michael@0 148 return (SharedSurface_Basic*)surf;
michael@0 149 }
michael@0 150
michael@0 151 protected:
michael@0 152 const GLuint mTex;
michael@0 153 GLuint mFB;
michael@0 154
michael@0 155 RefPtr<gfx::DataSourceSurface> mData;
michael@0 156
michael@0 157 SharedSurface_Basic(GLContext* gl,
michael@0 158 const gfx::IntSize& size,
michael@0 159 bool hasAlpha,
michael@0 160 gfx::SurfaceFormat format,
michael@0 161 GLuint tex);
michael@0 162
michael@0 163 public:
michael@0 164 virtual ~SharedSurface_Basic();
michael@0 165
michael@0 166 virtual void LockProdImpl() MOZ_OVERRIDE {}
michael@0 167 virtual void UnlockProdImpl() MOZ_OVERRIDE {}
michael@0 168
michael@0 169
michael@0 170 virtual void Fence() MOZ_OVERRIDE;
michael@0 171
michael@0 172 virtual bool WaitSync() MOZ_OVERRIDE {
michael@0 173 // Since we already store the data in Fence, we're always done already.
michael@0 174 return true;
michael@0 175 }
michael@0 176
michael@0 177 virtual GLuint ProdTexture() MOZ_OVERRIDE {
michael@0 178 return mTex;
michael@0 179 }
michael@0 180
michael@0 181 // Implementation-specific functions below:
michael@0 182 gfx::DataSourceSurface* GetData() {
michael@0 183 return mData;
michael@0 184 }
michael@0 185 };
michael@0 186
michael@0 187 class SurfaceFactory_Basic
michael@0 188 : public SurfaceFactory_GL
michael@0 189 {
michael@0 190 public:
michael@0 191 SurfaceFactory_Basic(GLContext* gl, const SurfaceCaps& caps)
michael@0 192 : SurfaceFactory_GL(gl, SharedSurfaceType::Basic, caps)
michael@0 193 {}
michael@0 194
michael@0 195 virtual SharedSurface* CreateShared(const gfx::IntSize& size) MOZ_OVERRIDE {
michael@0 196 bool hasAlpha = mReadCaps.alpha;
michael@0 197 return SharedSurface_Basic::Create(mGL, mFormats, size, hasAlpha);
michael@0 198 }
michael@0 199 };
michael@0 200
michael@0 201
michael@0 202 // Using shared GL textures:
michael@0 203 class SharedSurface_GLTexture
michael@0 204 : public SharedSurface_GL
michael@0 205 {
michael@0 206 public:
michael@0 207 static SharedSurface_GLTexture* Create(GLContext* prodGL,
michael@0 208 GLContext* consGL,
michael@0 209 const GLFormats& formats,
michael@0 210 const gfx::IntSize& size,
michael@0 211 bool hasAlpha,
michael@0 212 GLuint texture = 0);
michael@0 213
michael@0 214 static SharedSurface_GLTexture* Cast(SharedSurface* surf) {
michael@0 215 MOZ_ASSERT(surf->Type() == SharedSurfaceType::GLTextureShare);
michael@0 216
michael@0 217 return (SharedSurface_GLTexture*)surf;
michael@0 218 }
michael@0 219
michael@0 220 protected:
michael@0 221 GLContext* mConsGL;
michael@0 222 const GLuint mTex;
michael@0 223 const bool mOwnsTex;
michael@0 224 GLsync mSync;
michael@0 225 mutable Mutex mMutex;
michael@0 226
michael@0 227 SharedSurface_GLTexture(GLContext* prodGL,
michael@0 228 GLContext* consGL,
michael@0 229 const gfx::IntSize& size,
michael@0 230 bool hasAlpha,
michael@0 231 GLuint tex,
michael@0 232 bool ownsTex)
michael@0 233 : SharedSurface_GL(SharedSurfaceType::GLTextureShare,
michael@0 234 AttachmentType::GLTexture,
michael@0 235 prodGL,
michael@0 236 size,
michael@0 237 hasAlpha)
michael@0 238 , mConsGL(consGL)
michael@0 239 , mTex(tex)
michael@0 240 , mOwnsTex(ownsTex)
michael@0 241 , mSync(0)
michael@0 242 , mMutex("SharedSurface_GLTexture mutex")
michael@0 243 {
michael@0 244 }
michael@0 245
michael@0 246 public:
michael@0 247 virtual ~SharedSurface_GLTexture();
michael@0 248
michael@0 249 virtual void LockProdImpl() MOZ_OVERRIDE {}
michael@0 250 virtual void UnlockProdImpl() MOZ_OVERRIDE {}
michael@0 251
michael@0 252
michael@0 253 virtual void Fence() MOZ_OVERRIDE;
michael@0 254 virtual bool WaitSync() MOZ_OVERRIDE;
michael@0 255
michael@0 256
michael@0 257 virtual GLuint ProdTexture() MOZ_OVERRIDE {
michael@0 258 return mTex;
michael@0 259 }
michael@0 260
michael@0 261 // Custom:
michael@0 262
michael@0 263 GLuint ConsTexture(GLContext* consGL);
michael@0 264
michael@0 265 GLenum ConsTextureTarget() const {
michael@0 266 return ProdTextureTarget();
michael@0 267 }
michael@0 268 };
michael@0 269
michael@0 270 class SurfaceFactory_GLTexture
michael@0 271 : public SurfaceFactory_GL
michael@0 272 {
michael@0 273 protected:
michael@0 274 GLContext* const mConsGL;
michael@0 275
michael@0 276 public:
michael@0 277 // If we don't know `consGL` at construction time, use `nullptr`, and call
michael@0 278 // `SetConsumerGL()` on each `SharedSurface_GLTexture` before calling its
michael@0 279 // `WaitSync()`.
michael@0 280 SurfaceFactory_GLTexture(GLContext* prodGL,
michael@0 281 GLContext* consGL,
michael@0 282 const SurfaceCaps& caps)
michael@0 283 : SurfaceFactory_GL(prodGL, SharedSurfaceType::GLTextureShare, caps)
michael@0 284 , mConsGL(consGL)
michael@0 285 {
michael@0 286 MOZ_ASSERT(consGL != prodGL);
michael@0 287 }
michael@0 288
michael@0 289 virtual SharedSurface* CreateShared(const gfx::IntSize& size) MOZ_OVERRIDE {
michael@0 290 bool hasAlpha = mReadCaps.alpha;
michael@0 291 return SharedSurface_GLTexture::Create(mGL, mConsGL, mFormats, size, hasAlpha);
michael@0 292 }
michael@0 293 };
michael@0 294
michael@0 295 } /* namespace gfx */
michael@0 296 } /* namespace mozilla */
michael@0 297
michael@0 298 #endif /* SHARED_SURFACE_GL_H_ */

mercurial