gfx/layers/opengl/CompositorOGL.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 MOZILLA_GFX_COMPOSITOROGL_H
michael@0 7 #define MOZILLA_GFX_COMPOSITOROGL_H
michael@0 8
michael@0 9 #include "gfx2DGlue.h"
michael@0 10 #include "GLContextTypes.h" // for GLContext, etc
michael@0 11 #include "GLDefs.h" // for GLuint, LOCAL_GL_TEXTURE_2D, etc
michael@0 12 #include "OGLShaderProgram.h" // for ShaderProgramOGL, etc
michael@0 13 #include "Units.h" // for ScreenPoint
michael@0 14 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
michael@0 15 #include "mozilla/Attributes.h" // for MOZ_OVERRIDE, MOZ_FINAL
michael@0 16 #include "mozilla/RefPtr.h" // for TemporaryRef, RefPtr
michael@0 17 #include "mozilla/gfx/2D.h" // for DrawTarget
michael@0 18 #include "mozilla/gfx/BaseSize.h" // for BaseSize
michael@0 19 #include "mozilla/gfx/Point.h" // for IntSize, Point
michael@0 20 #include "mozilla/gfx/Rect.h" // for Rect, IntRect
michael@0 21 #include "mozilla/gfx/Types.h" // for Float, SurfaceFormat, etc
michael@0 22 #include "mozilla/layers/Compositor.h" // for SurfaceInitMode, Compositor, etc
michael@0 23 #include "mozilla/layers/CompositorTypes.h" // for MaskType::NumMaskTypes, etc
michael@0 24 #include "mozilla/layers/LayersTypes.h"
michael@0 25 #include "nsAutoPtr.h" // for nsRefPtr, nsAutoPtr
michael@0 26 #include "nsCOMPtr.h" // for already_AddRefed
michael@0 27 #include "nsDebug.h" // for NS_ASSERTION, NS_WARNING
michael@0 28 #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
michael@0 29 #include "nsSize.h" // for nsIntSize
michael@0 30 #include "nsTArray.h" // for nsAutoTArray, nsTArray, etc
michael@0 31 #include "nsThreadUtils.h" // for nsRunnable
michael@0 32 #include "nsXULAppAPI.h" // for XRE_GetProcessType
michael@0 33 #include "nscore.h" // for NS_IMETHOD
michael@0 34 #include "VBOArena.h" // for gl::VBOArena
michael@0 35 #ifdef MOZ_WIDGET_GONK
michael@0 36 #include <ui/GraphicBuffer.h>
michael@0 37 #endif
michael@0 38
michael@0 39 class gfx3DMatrix;
michael@0 40 class nsIWidget;
michael@0 41
michael@0 42 namespace mozilla {
michael@0 43 class TimeStamp;
michael@0 44
michael@0 45 namespace gfx {
michael@0 46 class Matrix4x4;
michael@0 47 }
michael@0 48
michael@0 49 namespace layers {
michael@0 50
michael@0 51 class CompositingRenderTarget;
michael@0 52 class CompositingRenderTargetOGL;
michael@0 53 class DataTextureSource;
michael@0 54 class GLManagerCompositor;
michael@0 55 class TextureSource;
michael@0 56 struct Effect;
michael@0 57 struct EffectChain;
michael@0 58
michael@0 59 /**
michael@0 60 * Interface for pools of temporary gl textures for the compositor.
michael@0 61 * The textures are fully owned by the pool, so the latter is responsible
michael@0 62 * calling fDeleteTextures accordingly.
michael@0 63 * Users of GetTexture receive a texture that is only valid for the duration
michael@0 64 * of the current frame.
michael@0 65 * This is primarily intended for direct texturing APIs that need to attach
michael@0 66 * shared objects (such as an EGLImage) to a gl texture.
michael@0 67 */
michael@0 68 class CompositorTexturePoolOGL
michael@0 69 {
michael@0 70 protected:
michael@0 71 virtual ~CompositorTexturePoolOGL() {}
michael@0 72
michael@0 73 public:
michael@0 74 NS_INLINE_DECL_REFCOUNTING(CompositorTexturePoolOGL)
michael@0 75
michael@0 76 virtual void Clear() = 0;
michael@0 77
michael@0 78 virtual GLuint GetTexture(GLenum aTarget, GLenum aEnum) = 0;
michael@0 79
michael@0 80 virtual void EndFrame() = 0;
michael@0 81 };
michael@0 82
michael@0 83 /**
michael@0 84 * Agressively reuses textures. One gl texture per texture unit in total.
michael@0 85 * So far this hasn't shown the best results on b2g.
michael@0 86 */
michael@0 87 class PerUnitTexturePoolOGL : public CompositorTexturePoolOGL
michael@0 88 {
michael@0 89 public:
michael@0 90 PerUnitTexturePoolOGL(gl::GLContext* aGL)
michael@0 91 : mTextureTarget(0) // zero is never a valid texture target
michael@0 92 , mGL(aGL)
michael@0 93 {}
michael@0 94
michael@0 95 virtual ~PerUnitTexturePoolOGL()
michael@0 96 {
michael@0 97 DestroyTextures();
michael@0 98 }
michael@0 99
michael@0 100 virtual void Clear() MOZ_OVERRIDE
michael@0 101 {
michael@0 102 DestroyTextures();
michael@0 103 }
michael@0 104
michael@0 105 virtual GLuint GetTexture(GLenum aTarget, GLenum aUnit) MOZ_OVERRIDE;
michael@0 106
michael@0 107 virtual void EndFrame() MOZ_OVERRIDE {}
michael@0 108
michael@0 109 protected:
michael@0 110 void DestroyTextures();
michael@0 111
michael@0 112 GLenum mTextureTarget;
michael@0 113 nsTArray<GLuint> mTextures;
michael@0 114 RefPtr<gl::GLContext> mGL;
michael@0 115 };
michael@0 116
michael@0 117 /**
michael@0 118 * Reuse gl textures from a pool of textures that haven't yet been
michael@0 119 * used during the current frame.
michael@0 120 * All the textures that are not used at the end of a frame are
michael@0 121 * deleted.
michael@0 122 * This strategy seems to work well with gralloc textures because destroying
michael@0 123 * unused textures which are bound to gralloc buffers let drivers know that it
michael@0 124 * can unlock the gralloc buffers.
michael@0 125 */
michael@0 126 class PerFrameTexturePoolOGL : public CompositorTexturePoolOGL
michael@0 127 {
michael@0 128 public:
michael@0 129 PerFrameTexturePoolOGL(gl::GLContext* aGL)
michael@0 130 : mTextureTarget(0) // zero is never a valid texture target
michael@0 131 , mGL(aGL)
michael@0 132 {}
michael@0 133
michael@0 134 virtual ~PerFrameTexturePoolOGL()
michael@0 135 {
michael@0 136 DestroyTextures();
michael@0 137 }
michael@0 138
michael@0 139 virtual void Clear() MOZ_OVERRIDE
michael@0 140 {
michael@0 141 DestroyTextures();
michael@0 142 }
michael@0 143
michael@0 144 virtual GLuint GetTexture(GLenum aTarget, GLenum aUnit) MOZ_OVERRIDE;
michael@0 145
michael@0 146 virtual void EndFrame() MOZ_OVERRIDE;
michael@0 147
michael@0 148 protected:
michael@0 149 void DestroyTextures();
michael@0 150
michael@0 151 GLenum mTextureTarget;
michael@0 152 RefPtr<gl::GLContext> mGL;
michael@0 153 nsTArray<GLuint> mCreatedTextures;
michael@0 154 nsTArray<GLuint> mUnusedTextures;
michael@0 155 };
michael@0 156
michael@0 157 class CompositorOGL : public Compositor
michael@0 158 {
michael@0 159 typedef mozilla::gl::GLContext GLContext;
michael@0 160
michael@0 161 friend class GLManagerCompositor;
michael@0 162
michael@0 163 std::map<ShaderConfigOGL, ShaderProgramOGL*> mPrograms;
michael@0 164 public:
michael@0 165 CompositorOGL(nsIWidget *aWidget, int aSurfaceWidth = -1, int aSurfaceHeight = -1,
michael@0 166 bool aUseExternalSurfaceSize = false);
michael@0 167
michael@0 168 virtual ~CompositorOGL();
michael@0 169
michael@0 170 virtual TemporaryRef<DataTextureSource>
michael@0 171 CreateDataTextureSource(TextureFlags aFlags = 0) MOZ_OVERRIDE;
michael@0 172
michael@0 173 virtual bool Initialize() MOZ_OVERRIDE;
michael@0 174
michael@0 175 virtual void Destroy() MOZ_OVERRIDE;
michael@0 176
michael@0 177 virtual TextureFactoryIdentifier GetTextureFactoryIdentifier() MOZ_OVERRIDE
michael@0 178 {
michael@0 179 return TextureFactoryIdentifier(LayersBackend::LAYERS_OPENGL,
michael@0 180 XRE_GetProcessType(),
michael@0 181 GetMaxTextureSize(),
michael@0 182 mFBOTextureTarget == LOCAL_GL_TEXTURE_2D,
michael@0 183 SupportsPartialTextureUpdate());
michael@0 184 }
michael@0 185
michael@0 186 virtual TemporaryRef<CompositingRenderTarget>
michael@0 187 CreateRenderTarget(const gfx::IntRect &aRect, SurfaceInitMode aInit) MOZ_OVERRIDE;
michael@0 188
michael@0 189 virtual TemporaryRef<CompositingRenderTarget>
michael@0 190 CreateRenderTargetFromSource(const gfx::IntRect &aRect,
michael@0 191 const CompositingRenderTarget *aSource,
michael@0 192 const gfx::IntPoint &aSourcePoint) MOZ_OVERRIDE;
michael@0 193
michael@0 194 virtual void SetRenderTarget(CompositingRenderTarget *aSurface) MOZ_OVERRIDE;
michael@0 195 virtual CompositingRenderTarget* GetCurrentRenderTarget() const MOZ_OVERRIDE;
michael@0 196
michael@0 197 virtual void DrawQuad(const gfx::Rect& aRect,
michael@0 198 const gfx::Rect& aClipRect,
michael@0 199 const EffectChain &aEffectChain,
michael@0 200 gfx::Float aOpacity,
michael@0 201 const gfx::Matrix4x4 &aTransform) MOZ_OVERRIDE
michael@0 202 {
michael@0 203 DrawQuadInternal(aRect, aClipRect, aEffectChain,
michael@0 204 aOpacity, aTransform, LOCAL_GL_TRIANGLE_STRIP);
michael@0 205 }
michael@0 206
michael@0 207 virtual void DrawLines(const std::vector<gfx::Point>& aLines,
michael@0 208 const gfx::Rect& aClipRect,
michael@0 209 const gfx::Color& aColor,
michael@0 210 gfx::Float aOpacity,
michael@0 211 const gfx::Matrix4x4 &aTransform) MOZ_OVERRIDE;
michael@0 212
michael@0 213
michael@0 214 virtual void EndFrame() MOZ_OVERRIDE;
michael@0 215 virtual void SetFBAcquireFence(Layer* aLayer) MOZ_OVERRIDE;
michael@0 216 virtual void EndFrameForExternalComposition(const gfx::Matrix& aTransform) MOZ_OVERRIDE;
michael@0 217 virtual void AbortFrame() MOZ_OVERRIDE;
michael@0 218
michael@0 219 virtual bool SupportsPartialTextureUpdate() MOZ_OVERRIDE;
michael@0 220
michael@0 221 virtual bool CanUseCanvasLayerForSize(const gfx::IntSize &aSize) MOZ_OVERRIDE
michael@0 222 {
michael@0 223 if (!mGLContext)
michael@0 224 return false;
michael@0 225 int32_t maxSize = GetMaxTextureSize();
michael@0 226 return aSize <= gfx::IntSize(maxSize, maxSize);
michael@0 227 }
michael@0 228
michael@0 229 virtual int32_t GetMaxTextureSize() const MOZ_OVERRIDE;
michael@0 230
michael@0 231 /**
michael@0 232 * Set the size of the EGL surface we're rendering to, if we're rendering to
michael@0 233 * an EGL surface.
michael@0 234 */
michael@0 235 virtual void SetDestinationSurfaceSize(const gfx::IntSize& aSize) MOZ_OVERRIDE;
michael@0 236
michael@0 237 virtual void SetScreenRenderOffset(const ScreenPoint& aOffset) MOZ_OVERRIDE {
michael@0 238 mRenderOffset = aOffset;
michael@0 239 }
michael@0 240
michael@0 241 virtual void MakeCurrent(MakeCurrentFlags aFlags = 0) MOZ_OVERRIDE;
michael@0 242
michael@0 243 virtual void SetTargetContext(gfx::DrawTarget* aTarget) MOZ_OVERRIDE
michael@0 244 {
michael@0 245 mTarget = aTarget;
michael@0 246 }
michael@0 247
michael@0 248 virtual void PrepareViewport(const gfx::IntSize& aSize,
michael@0 249 const gfx::Matrix& aWorldTransform) MOZ_OVERRIDE;
michael@0 250
michael@0 251
michael@0 252 #ifdef MOZ_DUMP_PAINTING
michael@0 253 virtual const char* Name() const MOZ_OVERRIDE { return "OGL"; }
michael@0 254 #endif // MOZ_DUMP_PAINTING
michael@0 255
michael@0 256 virtual LayersBackend GetBackendType() const MOZ_OVERRIDE {
michael@0 257 return LayersBackend::LAYERS_OPENGL;
michael@0 258 }
michael@0 259
michael@0 260 virtual void Pause() MOZ_OVERRIDE;
michael@0 261 virtual bool Resume() MOZ_OVERRIDE;
michael@0 262
michael@0 263 virtual nsIWidget* GetWidget() const MOZ_OVERRIDE { return mWidget; }
michael@0 264
michael@0 265 GLContext* gl() const { return mGLContext; }
michael@0 266 gfx::SurfaceFormat GetFBOFormat() const {
michael@0 267 return gfx::SurfaceFormat::R8G8B8A8;
michael@0 268 }
michael@0 269
michael@0 270 /**
michael@0 271 * The compositor provides with temporary textures for use with direct
michael@0 272 * textruing like gralloc texture.
michael@0 273 * Doing so lets us use gralloc the way it has been designed to be used
michael@0 274 * (see https://wiki.mozilla.org/Platform/GFX/Gralloc)
michael@0 275 */
michael@0 276 GLuint GetTemporaryTexture(GLenum aTarget, GLenum aUnit);
michael@0 277
michael@0 278 const gfx::Matrix4x4& GetProjMatrix() const {
michael@0 279 return mProjMatrix;
michael@0 280 }
michael@0 281 private:
michael@0 282 virtual void DrawQuadInternal(const gfx::Rect& aRect,
michael@0 283 const gfx::Rect& aClipRect,
michael@0 284 const EffectChain &aEffectChain,
michael@0 285 gfx::Float aOpacity,
michael@0 286 const gfx::Matrix4x4 &aTransformi,
michael@0 287 GLuint aDrawMode);
michael@0 288
michael@0 289 virtual gfx::IntSize GetWidgetSize() const MOZ_OVERRIDE
michael@0 290 {
michael@0 291 return gfx::ToIntSize(mWidgetSize);
michael@0 292 }
michael@0 293
michael@0 294 /**
michael@0 295 * Context target, nullptr when drawing directly to our swap chain.
michael@0 296 */
michael@0 297 RefPtr<gfx::DrawTarget> mTarget;
michael@0 298
michael@0 299 /** Widget associated with this compositor */
michael@0 300 nsIWidget *mWidget;
michael@0 301 nsIntSize mWidgetSize;
michael@0 302 nsRefPtr<GLContext> mGLContext;
michael@0 303 gfx::Matrix4x4 mProjMatrix;
michael@0 304
michael@0 305 /** The size of the surface we are rendering to */
michael@0 306 nsIntSize mSurfaceSize;
michael@0 307
michael@0 308 ScreenPoint mRenderOffset;
michael@0 309
michael@0 310 already_AddRefed<mozilla::gl::GLContext> CreateContext();
michael@0 311
michael@0 312 /** Texture target to use for FBOs */
michael@0 313 GLenum mFBOTextureTarget;
michael@0 314
michael@0 315 /** Currently bound render target */
michael@0 316 RefPtr<CompositingRenderTargetOGL> mCurrentRenderTarget;
michael@0 317 #ifdef DEBUG
michael@0 318 CompositingRenderTargetOGL* mWindowRenderTarget;
michael@0 319 #endif
michael@0 320
michael@0 321 /**
michael@0 322 * VBO that has some basics in it for a textured quad, including vertex
michael@0 323 * coords and texcoords.
michael@0 324 */
michael@0 325 GLuint mQuadVBO;
michael@0 326
michael@0 327 /**
michael@0 328 * When we can't use mQuadVBO, we allocate VBOs from this arena instead.
michael@0 329 */
michael@0 330 gl::VBOArena mVBOs;
michael@0 331
michael@0 332 bool mHasBGRA;
michael@0 333
michael@0 334 /**
michael@0 335 * When rendering to some EGL surfaces (e.g. on Android), we rely on being told
michael@0 336 * about size changes (via SetSurfaceSize) rather than pulling this information
michael@0 337 * from the widget.
michael@0 338 */
michael@0 339 bool mUseExternalSurfaceSize;
michael@0 340
michael@0 341 /**
michael@0 342 * Have we had DrawQuad calls since the last frame was rendered?
michael@0 343 */
michael@0 344 bool mFrameInProgress;
michael@0 345
michael@0 346 /*
michael@0 347 * Clear aRect on current render target.
michael@0 348 */
michael@0 349 virtual void ClearRect(const gfx::Rect& aRect) MOZ_OVERRIDE;
michael@0 350
michael@0 351 /* Start a new frame. If aClipRectIn is null and aClipRectOut is non-null,
michael@0 352 * sets *aClipRectOut to the screen dimensions.
michael@0 353 */
michael@0 354 virtual void BeginFrame(const nsIntRegion& aInvalidRegion,
michael@0 355 const gfx::Rect *aClipRectIn,
michael@0 356 const gfx::Matrix& aTransform,
michael@0 357 const gfx::Rect& aRenderBounds,
michael@0 358 gfx::Rect *aClipRectOut = nullptr,
michael@0 359 gfx::Rect *aRenderBoundsOut = nullptr) MOZ_OVERRIDE;
michael@0 360
michael@0 361 ShaderConfigOGL GetShaderConfigFor(Effect *aEffect, MaskType aMask = MaskNone) const;
michael@0 362 ShaderProgramOGL* GetShaderProgramFor(const ShaderConfigOGL &aConfig);
michael@0 363
michael@0 364 /**
michael@0 365 * Create a FBO backed by a texture.
michael@0 366 * Note that the texture target type will be
michael@0 367 * of the type returned by FBOTextureTarget; different
michael@0 368 * shaders are required to sample from the different
michael@0 369 * texture types.
michael@0 370 */
michael@0 371 void CreateFBOWithTexture(const gfx::IntRect& aRect, bool aCopyFromSource,
michael@0 372 GLuint aSourceFrameBuffer,
michael@0 373 GLuint *aFBO, GLuint *aTexture);
michael@0 374
michael@0 375 GLintptr QuadVBOVertexOffset() { return 0; }
michael@0 376 GLintptr QuadVBOTexCoordOffset() { return sizeof(float)*4*2; }
michael@0 377
michael@0 378 void BindQuadVBO();
michael@0 379 void QuadVBOVerticesAttrib(GLuint aAttribIndex);
michael@0 380 void QuadVBOTexCoordsAttrib(GLuint aAttribIndex);
michael@0 381 void BindAndDrawQuad(GLuint aVertAttribIndex,
michael@0 382 GLuint aTexCoordAttribIndex,
michael@0 383 GLuint aDrawMode = LOCAL_GL_TRIANGLE_STRIP);
michael@0 384 void BindAndDrawQuad(ShaderProgramOGL *aProg,
michael@0 385 GLuint aDrawMode = LOCAL_GL_TRIANGLE_STRIP);
michael@0 386 void BindAndDrawQuadWithTextureRect(ShaderProgramOGL *aProg,
michael@0 387 const gfx3DMatrix& aTextureTransform,
michael@0 388 const gfx::Rect& aTexCoordRect,
michael@0 389 TextureSource *aTexture);
michael@0 390
michael@0 391 void CleanupResources();
michael@0 392
michael@0 393 /**
michael@0 394 * Copies the content of our backbuffer to the set transaction target.
michael@0 395 * Does not restore the target FBO, so only call from EndFrame.
michael@0 396 */
michael@0 397 void CopyToTarget(gfx::DrawTarget* aTarget, const gfx::Matrix& aWorldMatrix);
michael@0 398
michael@0 399 /**
michael@0 400 * Implements the flipping of the y-axis to convert from layers/compositor
michael@0 401 * coordinates to OpenGL coordinates.
michael@0 402 *
michael@0 403 * Indeed, the only coordinate system that OpenGL knows has the y-axis
michael@0 404 * pointing upwards, but the layers/compositor coordinate system has the
michael@0 405 * y-axis pointing downwards, for good reason as Web pages are typically
michael@0 406 * scrolled downwards. So, some flipping has to take place; FlippedY does it.
michael@0 407 */
michael@0 408 GLint FlipY(GLint y) const { return mHeight - y; }
michael@0 409
michael@0 410 RefPtr<CompositorTexturePoolOGL> mTexturePool;
michael@0 411
michael@0 412 bool mDestroyed;
michael@0 413
michael@0 414 /**
michael@0 415 * Height of the OpenGL context's primary framebuffer in pixels. Used by
michael@0 416 * FlipY for the y-flipping calculation.
michael@0 417 */
michael@0 418 GLint mHeight;
michael@0 419 };
michael@0 420
michael@0 421 }
michael@0 422 }
michael@0 423
michael@0 424 #endif /* MOZILLA_GFX_COMPOSITOROGL_H */

mercurial