gfx/layers/composite/LayerManagerComposite.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 GFX_LayerManagerComposite_H
michael@0 7 #define GFX_LayerManagerComposite_H
michael@0 8
michael@0 9 #include <stdint.h> // for int32_t, uint32_t
michael@0 10 #include "GLDefs.h" // for GLenum
michael@0 11 #include "Layers.h"
michael@0 12 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
michael@0 13 #include "mozilla/Attributes.h" // for MOZ_OVERRIDE
michael@0 14 #include "mozilla/RefPtr.h" // for RefPtr, TemporaryRef
michael@0 15 #include "mozilla/gfx/2D.h"
michael@0 16 #include "mozilla/gfx/Point.h" // for IntSize
michael@0 17 #include "mozilla/gfx/Rect.h" // for Rect
michael@0 18 #include "mozilla/gfx/Types.h" // for SurfaceFormat
michael@0 19 #include "mozilla/layers/CompositorTypes.h"
michael@0 20 #include "mozilla/layers/LayersTypes.h" // for LayersBackend, etc
michael@0 21 #include "mozilla/RefPtr.h"
michael@0 22 #include "nsAString.h"
michael@0 23 #include "nsAutoPtr.h" // for nsRefPtr
michael@0 24 #include "nsCOMPtr.h" // for already_AddRefed
michael@0 25 #include "nsDebug.h" // for NS_ASSERTION
michael@0 26 #include "nsISupportsImpl.h" // for Layer::AddRef, etc
michael@0 27 #include "nsRect.h" // for nsIntRect
michael@0 28 #include "nsRegion.h" // for nsIntRegion
michael@0 29 #include "nscore.h" // for nsAString, etc
michael@0 30 #include "LayerTreeInvalidation.h"
michael@0 31
michael@0 32 class gfxContext;
michael@0 33 struct nsIntPoint;
michael@0 34 struct nsIntSize;
michael@0 35
michael@0 36 #ifdef XP_WIN
michael@0 37 #include <windows.h>
michael@0 38 #endif
michael@0 39
michael@0 40 namespace mozilla {
michael@0 41 namespace gfx {
michael@0 42 class DrawTarget;
michael@0 43 }
michael@0 44
michael@0 45 namespace gl {
michael@0 46 class GLContext;
michael@0 47 class TextureImage;
michael@0 48 }
michael@0 49
michael@0 50 namespace layers {
michael@0 51
michael@0 52 class CanvasLayerComposite;
michael@0 53 class ColorLayerComposite;
michael@0 54 class CompositableHost;
michael@0 55 class Compositor;
michael@0 56 class ContainerLayerComposite;
michael@0 57 class EffectChain;
michael@0 58 class ImageLayer;
michael@0 59 class ImageLayerComposite;
michael@0 60 class LayerComposite;
michael@0 61 class RefLayerComposite;
michael@0 62 class SurfaceDescriptor;
michael@0 63 class ThebesLayerComposite;
michael@0 64 class TiledLayerComposer;
michael@0 65 class TextRenderer;
michael@0 66 struct FPSState;
michael@0 67
michael@0 68 class LayerManagerComposite : public LayerManager
michael@0 69 {
michael@0 70 typedef mozilla::gfx::DrawTarget DrawTarget;
michael@0 71 typedef mozilla::gfx::IntSize IntSize;
michael@0 72 typedef mozilla::gfx::SurfaceFormat SurfaceFormat;
michael@0 73
michael@0 74 public:
michael@0 75 LayerManagerComposite(Compositor* aCompositor);
michael@0 76 ~LayerManagerComposite();
michael@0 77
michael@0 78 virtual void Destroy() MOZ_OVERRIDE;
michael@0 79
michael@0 80 /**
michael@0 81 * return True if initialization was succesful, false when it was not.
michael@0 82 */
michael@0 83 bool Initialize();
michael@0 84
michael@0 85 /**
michael@0 86 * Sets the clipping region for this layer manager. This is important on
michael@0 87 * windows because using OGL we no longer have GDI's native clipping. Therefor
michael@0 88 * widget must tell us what part of the screen is being invalidated,
michael@0 89 * and we should clip to this.
michael@0 90 *
michael@0 91 * \param aClippingRegion Region to clip to. Setting an empty region
michael@0 92 * will disable clipping.
michael@0 93 */
michael@0 94 void SetClippingRegion(const nsIntRegion& aClippingRegion)
michael@0 95 {
michael@0 96 mClippingRegion = aClippingRegion;
michael@0 97 }
michael@0 98
michael@0 99 /**
michael@0 100 * LayerManager implementation.
michael@0 101 */
michael@0 102 virtual LayerManagerComposite* AsLayerManagerComposite() MOZ_OVERRIDE
michael@0 103 {
michael@0 104 return this;
michael@0 105 }
michael@0 106
michael@0 107 void UpdateRenderBounds(const nsIntRect& aRect);
michael@0 108
michael@0 109 virtual void BeginTransaction() MOZ_OVERRIDE;
michael@0 110 virtual void BeginTransactionWithTarget(gfxContext* aTarget) MOZ_OVERRIDE
michael@0 111 {
michael@0 112 MOZ_CRASH("Use BeginTransactionWithDrawTarget");
michael@0 113 }
michael@0 114 void BeginTransactionWithDrawTarget(gfx::DrawTarget* aTarget);
michael@0 115
michael@0 116 virtual bool EndEmptyTransaction(EndTransactionFlags aFlags = END_DEFAULT) MOZ_OVERRIDE;
michael@0 117 virtual void EndTransaction(DrawThebesLayerCallback aCallback,
michael@0 118 void* aCallbackData,
michael@0 119 EndTransactionFlags aFlags = END_DEFAULT) MOZ_OVERRIDE;
michael@0 120
michael@0 121 virtual void SetRoot(Layer* aLayer) MOZ_OVERRIDE { mRoot = aLayer; }
michael@0 122
michael@0 123 // XXX[nrc]: never called, we should move this logic to ClientLayerManager
michael@0 124 // (bug 946926).
michael@0 125 virtual bool CanUseCanvasLayerForSize(const gfx::IntSize &aSize) MOZ_OVERRIDE;
michael@0 126
michael@0 127 virtual int32_t GetMaxTextureSize() const MOZ_OVERRIDE
michael@0 128 {
michael@0 129 MOZ_CRASH("Call on compositor, not LayerManagerComposite");
michael@0 130 }
michael@0 131
michael@0 132 virtual void ClearCachedResources(Layer* aSubtree = nullptr) MOZ_OVERRIDE;
michael@0 133
michael@0 134 virtual already_AddRefed<ThebesLayer> CreateThebesLayer() MOZ_OVERRIDE;
michael@0 135 virtual already_AddRefed<ContainerLayer> CreateContainerLayer() MOZ_OVERRIDE;
michael@0 136 virtual already_AddRefed<ImageLayer> CreateImageLayer() MOZ_OVERRIDE;
michael@0 137 virtual already_AddRefed<ColorLayer> CreateColorLayer() MOZ_OVERRIDE;
michael@0 138 virtual already_AddRefed<CanvasLayer> CreateCanvasLayer() MOZ_OVERRIDE;
michael@0 139 already_AddRefed<ThebesLayerComposite> CreateThebesLayerComposite();
michael@0 140 already_AddRefed<ContainerLayerComposite> CreateContainerLayerComposite();
michael@0 141 already_AddRefed<ImageLayerComposite> CreateImageLayerComposite();
michael@0 142 already_AddRefed<ColorLayerComposite> CreateColorLayerComposite();
michael@0 143 already_AddRefed<CanvasLayerComposite> CreateCanvasLayerComposite();
michael@0 144 already_AddRefed<RefLayerComposite> CreateRefLayerComposite();
michael@0 145
michael@0 146 virtual LayersBackend GetBackendType() MOZ_OVERRIDE
michael@0 147 {
michael@0 148 MOZ_CRASH("Shouldn't be called for composited layer manager");
michael@0 149 }
michael@0 150 virtual void GetBackendName(nsAString& name) MOZ_OVERRIDE
michael@0 151 {
michael@0 152 MOZ_CRASH("Shouldn't be called for composited layer manager");
michael@0 153 }
michael@0 154
michael@0 155 virtual TemporaryRef<DrawTarget>
michael@0 156 CreateOptimalMaskDrawTarget(const IntSize &aSize) MOZ_OVERRIDE;
michael@0 157
michael@0 158 virtual const char* Name() const MOZ_OVERRIDE { return ""; }
michael@0 159
michael@0 160 enum WorldTransforPolicy {
michael@0 161 ApplyWorldTransform,
michael@0 162 DontApplyWorldTransform
michael@0 163 };
michael@0 164
michael@0 165 /**
michael@0 166 * Setup World transform matrix.
michael@0 167 * Transform will be ignored if it is not PreservesAxisAlignedRectangles
michael@0 168 * or has non integer scale
michael@0 169 */
michael@0 170 void SetWorldTransform(const gfx::Matrix& aMatrix);
michael@0 171 gfx::Matrix& GetWorldTransform(void);
michael@0 172
michael@0 173 /**
michael@0 174 * RAII helper class to add a mask effect with the compositable from aMaskLayer
michael@0 175 * to the EffectChain aEffect and notify the compositable when we are done.
michael@0 176 */
michael@0 177 class AutoAddMaskEffect
michael@0 178 {
michael@0 179 public:
michael@0 180 AutoAddMaskEffect(Layer* aMaskLayer,
michael@0 181 EffectChain& aEffect,
michael@0 182 bool aIs3D = false);
michael@0 183 ~AutoAddMaskEffect();
michael@0 184
michael@0 185 private:
michael@0 186 CompositableHost* mCompositable;
michael@0 187 };
michael@0 188
michael@0 189 /**
michael@0 190 * Creates a DrawTarget which is optimized for inter-operating with this
michael@0 191 * layermanager.
michael@0 192 */
michael@0 193 virtual TemporaryRef<mozilla::gfx::DrawTarget>
michael@0 194 CreateDrawTarget(const mozilla::gfx::IntSize& aSize,
michael@0 195 mozilla::gfx::SurfaceFormat aFormat) MOZ_OVERRIDE;
michael@0 196
michael@0 197 /**
michael@0 198 * Calculates the 'completeness' of the rendering that intersected with the
michael@0 199 * screen on the last render. This is only useful when progressive tile
michael@0 200 * drawing is enabled, otherwise this will always return 1.0.
michael@0 201 * This function's expense scales with the size of the layer tree and the
michael@0 202 * complexity of individual layers' valid regions.
michael@0 203 */
michael@0 204 float ComputeRenderIntegrity();
michael@0 205
michael@0 206 /**
michael@0 207 * returns true if PlatformAllocBuffer will return a buffer that supports
michael@0 208 * direct texturing
michael@0 209 */
michael@0 210 static bool SupportsDirectTexturing();
michael@0 211
michael@0 212 static void PlatformSyncBeforeReplyUpdate();
michael@0 213
michael@0 214 void AddInvalidRegion(const nsIntRegion& aRegion)
michael@0 215 {
michael@0 216 mInvalidRegion.Or(mInvalidRegion, aRegion);
michael@0 217 }
michael@0 218
michael@0 219 Compositor* GetCompositor() const
michael@0 220 {
michael@0 221 return mCompositor;
michael@0 222 }
michael@0 223
michael@0 224 /**
michael@0 225 * LayerManagerComposite provides sophisticated debug overlays
michael@0 226 * that can request a next frame.
michael@0 227 */
michael@0 228 bool DebugOverlayWantsNextFrame() { return mDebugOverlayWantsNextFrame; }
michael@0 229 void SetDebugOverlayWantsNextFrame(bool aVal)
michael@0 230 { mDebugOverlayWantsNextFrame = aVal; }
michael@0 231
michael@0 232 void NotifyShadowTreeTransaction();
michael@0 233
michael@0 234 TextRenderer* GetTextRenderer() { return mTextRenderer; }
michael@0 235
michael@0 236 private:
michael@0 237 /** Region we're clipping our current drawing to. */
michael@0 238 nsIntRegion mClippingRegion;
michael@0 239 nsIntRect mRenderBounds;
michael@0 240
michael@0 241 /** Current root layer. */
michael@0 242 LayerComposite* RootLayer() const;
michael@0 243
michael@0 244 /**
michael@0 245 * Recursive helper method for use by ComputeRenderIntegrity. Subtracts
michael@0 246 * any incomplete rendering on aLayer from aScreenRegion. Any low-precision
michael@0 247 * rendering is included in aLowPrecisionScreenRegion. aTransform is the
michael@0 248 * accumulated transform of intermediate surfaces beneath aLayer.
michael@0 249 */
michael@0 250 static void ComputeRenderIntegrityInternal(Layer* aLayer,
michael@0 251 nsIntRegion& aScreenRegion,
michael@0 252 nsIntRegion& aLowPrecisionScreenRegion,
michael@0 253 const gfx3DMatrix& aTransform);
michael@0 254
michael@0 255 /**
michael@0 256 * Render the current layer tree to the active target.
michael@0 257 */
michael@0 258 void Render();
michael@0 259
michael@0 260 /**
michael@0 261 * Render debug overlays such as the FPS/FrameCounter above the frame.
michael@0 262 */
michael@0 263 void RenderDebugOverlay(const gfx::Rect& aBounds);
michael@0 264
michael@0 265 void WorldTransformRect(nsIntRect& aRect);
michael@0 266
michael@0 267 RefPtr<Compositor> mCompositor;
michael@0 268 nsAutoPtr<LayerProperties> mClonedLayerTreeProperties;
michael@0 269
michael@0 270 /**
michael@0 271 * Context target, nullptr when drawing directly to our swap chain.
michael@0 272 */
michael@0 273 RefPtr<gfx::DrawTarget> mTarget;
michael@0 274
michael@0 275 gfx::Matrix mWorldMatrix;
michael@0 276 nsIntRegion mInvalidRegion;
michael@0 277 nsAutoPtr<FPSState> mFPS;
michael@0 278
michael@0 279 bool mInTransaction;
michael@0 280 bool mIsCompositorReady;
michael@0 281 bool mDebugOverlayWantsNextFrame;
michael@0 282
michael@0 283 RefPtr<TextRenderer> mTextRenderer;
michael@0 284 bool mGeometryChanged;
michael@0 285 };
michael@0 286
michael@0 287 /**
michael@0 288 * Composite layers are for use with OMTC on the compositor thread only. There
michael@0 289 * must be corresponding Basic layers on the content thread. For composite
michael@0 290 * layers, the layer manager only maintains the layer tree, all rendering is
michael@0 291 * done by a Compositor (see Compositor.h). As such, composite layers are
michael@0 292 * platform-independent and can be used on any platform for which there is a
michael@0 293 * Compositor implementation.
michael@0 294 *
michael@0 295 * The composite layer tree reflects exactly the basic layer tree. To
michael@0 296 * composite to screen, the layer manager walks the layer tree calling render
michael@0 297 * methods which in turn call into their CompositableHosts' Composite methods.
michael@0 298 * These call Compositor::DrawQuad to do the rendering.
michael@0 299 *
michael@0 300 * Mostly, layers are updated during the layers transaction. This is done from
michael@0 301 * CompositableClient to CompositableHost without interacting with the layer.
michael@0 302 *
michael@0 303 * A reference to the Compositor is stored in LayerManagerComposite.
michael@0 304 */
michael@0 305 class LayerComposite
michael@0 306 {
michael@0 307 public:
michael@0 308 LayerComposite(LayerManagerComposite* aManager);
michael@0 309
michael@0 310 virtual ~LayerComposite();
michael@0 311
michael@0 312 virtual LayerComposite* GetFirstChildComposite()
michael@0 313 {
michael@0 314 return nullptr;
michael@0 315 }
michael@0 316
michael@0 317 /* Do NOT call this from the generic LayerComposite destructor. Only from the
michael@0 318 * concrete class destructor
michael@0 319 */
michael@0 320 virtual void Destroy();
michael@0 321
michael@0 322 virtual Layer* GetLayer() = 0;
michael@0 323
michael@0 324 virtual void RenderLayer(const nsIntRect& aClipRect) = 0;
michael@0 325
michael@0 326 virtual bool SetCompositableHost(CompositableHost*)
michael@0 327 {
michael@0 328 // We must handle this gracefully, see bug 967824
michael@0 329 NS_WARNING("called SetCompositableHost for a layer type not accepting a compositable");
michael@0 330 return false;
michael@0 331 }
michael@0 332 virtual CompositableHost* GetCompositableHost() = 0;
michael@0 333
michael@0 334 virtual void CleanupResources() = 0;
michael@0 335
michael@0 336 virtual TiledLayerComposer* GetTiledLayerComposer() { return nullptr; }
michael@0 337
michael@0 338
michael@0 339 virtual void DestroyFrontBuffer() { }
michael@0 340
michael@0 341 /**
michael@0 342 * The following methods are
michael@0 343 *
michael@0 344 * CONSTRUCTION PHASE ONLY
michael@0 345 *
michael@0 346 * They are analogous to the Layer interface.
michael@0 347 */
michael@0 348 void SetShadowVisibleRegion(const nsIntRegion& aRegion)
michael@0 349 {
michael@0 350 mShadowVisibleRegion = aRegion;
michael@0 351 }
michael@0 352
michael@0 353 void SetShadowOpacity(float aOpacity)
michael@0 354 {
michael@0 355 mShadowOpacity = aOpacity;
michael@0 356 }
michael@0 357
michael@0 358 void SetShadowClipRect(const nsIntRect* aRect)
michael@0 359 {
michael@0 360 mUseShadowClipRect = aRect != nullptr;
michael@0 361 if (aRect) {
michael@0 362 mShadowClipRect = *aRect;
michael@0 363 }
michael@0 364 }
michael@0 365
michael@0 366 void SetShadowTransform(const gfx::Matrix4x4& aMatrix)
michael@0 367 {
michael@0 368 mShadowTransform = aMatrix;
michael@0 369 }
michael@0 370 void SetShadowTransformSetByAnimation(bool aSetByAnimation)
michael@0 371 {
michael@0 372 mShadowTransformSetByAnimation = aSetByAnimation;
michael@0 373 }
michael@0 374
michael@0 375 void SetLayerComposited(bool value)
michael@0 376 {
michael@0 377 mLayerComposited = value;
michael@0 378 }
michael@0 379
michael@0 380 void SetClearRect(const nsIntRect& aRect)
michael@0 381 {
michael@0 382 mClearRect = aRect;
michael@0 383 }
michael@0 384
michael@0 385 // These getters can be used anytime.
michael@0 386 float GetShadowOpacity() { return mShadowOpacity; }
michael@0 387 const nsIntRect* GetShadowClipRect() { return mUseShadowClipRect ? &mShadowClipRect : nullptr; }
michael@0 388 const nsIntRegion& GetShadowVisibleRegion() { return mShadowVisibleRegion; }
michael@0 389 const gfx::Matrix4x4& GetShadowTransform() { return mShadowTransform; }
michael@0 390 bool GetShadowTransformSetByAnimation() { return mShadowTransformSetByAnimation; }
michael@0 391 bool HasLayerBeenComposited() { return mLayerComposited; }
michael@0 392 nsIntRect GetClearRect() { return mClearRect; }
michael@0 393
michael@0 394 protected:
michael@0 395 gfx::Matrix4x4 mShadowTransform;
michael@0 396 nsIntRegion mShadowVisibleRegion;
michael@0 397 nsIntRect mShadowClipRect;
michael@0 398 LayerManagerComposite* mCompositeManager;
michael@0 399 RefPtr<Compositor> mCompositor;
michael@0 400 float mShadowOpacity;
michael@0 401 bool mUseShadowClipRect;
michael@0 402 bool mShadowTransformSetByAnimation;
michael@0 403 bool mDestroyed;
michael@0 404 bool mLayerComposited;
michael@0 405 nsIntRect mClearRect;
michael@0 406 };
michael@0 407
michael@0 408
michael@0 409 } /* layers */
michael@0 410 } /* mozilla */
michael@0 411
michael@0 412 #endif /* GFX_LayerManagerComposite_H */

mercurial