1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/layers/composite/LayerManagerComposite.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,412 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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 GFX_LayerManagerComposite_H 1.10 +#define GFX_LayerManagerComposite_H 1.11 + 1.12 +#include <stdint.h> // for int32_t, uint32_t 1.13 +#include "GLDefs.h" // for GLenum 1.14 +#include "Layers.h" 1.15 +#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc 1.16 +#include "mozilla/Attributes.h" // for MOZ_OVERRIDE 1.17 +#include "mozilla/RefPtr.h" // for RefPtr, TemporaryRef 1.18 +#include "mozilla/gfx/2D.h" 1.19 +#include "mozilla/gfx/Point.h" // for IntSize 1.20 +#include "mozilla/gfx/Rect.h" // for Rect 1.21 +#include "mozilla/gfx/Types.h" // for SurfaceFormat 1.22 +#include "mozilla/layers/CompositorTypes.h" 1.23 +#include "mozilla/layers/LayersTypes.h" // for LayersBackend, etc 1.24 +#include "mozilla/RefPtr.h" 1.25 +#include "nsAString.h" 1.26 +#include "nsAutoPtr.h" // for nsRefPtr 1.27 +#include "nsCOMPtr.h" // for already_AddRefed 1.28 +#include "nsDebug.h" // for NS_ASSERTION 1.29 +#include "nsISupportsImpl.h" // for Layer::AddRef, etc 1.30 +#include "nsRect.h" // for nsIntRect 1.31 +#include "nsRegion.h" // for nsIntRegion 1.32 +#include "nscore.h" // for nsAString, etc 1.33 +#include "LayerTreeInvalidation.h" 1.34 + 1.35 +class gfxContext; 1.36 +struct nsIntPoint; 1.37 +struct nsIntSize; 1.38 + 1.39 +#ifdef XP_WIN 1.40 +#include <windows.h> 1.41 +#endif 1.42 + 1.43 +namespace mozilla { 1.44 +namespace gfx { 1.45 +class DrawTarget; 1.46 +} 1.47 + 1.48 +namespace gl { 1.49 +class GLContext; 1.50 +class TextureImage; 1.51 +} 1.52 + 1.53 +namespace layers { 1.54 + 1.55 +class CanvasLayerComposite; 1.56 +class ColorLayerComposite; 1.57 +class CompositableHost; 1.58 +class Compositor; 1.59 +class ContainerLayerComposite; 1.60 +class EffectChain; 1.61 +class ImageLayer; 1.62 +class ImageLayerComposite; 1.63 +class LayerComposite; 1.64 +class RefLayerComposite; 1.65 +class SurfaceDescriptor; 1.66 +class ThebesLayerComposite; 1.67 +class TiledLayerComposer; 1.68 +class TextRenderer; 1.69 +struct FPSState; 1.70 + 1.71 +class LayerManagerComposite : public LayerManager 1.72 +{ 1.73 + typedef mozilla::gfx::DrawTarget DrawTarget; 1.74 + typedef mozilla::gfx::IntSize IntSize; 1.75 + typedef mozilla::gfx::SurfaceFormat SurfaceFormat; 1.76 + 1.77 +public: 1.78 + LayerManagerComposite(Compositor* aCompositor); 1.79 + ~LayerManagerComposite(); 1.80 + 1.81 + virtual void Destroy() MOZ_OVERRIDE; 1.82 + 1.83 + /** 1.84 + * return True if initialization was succesful, false when it was not. 1.85 + */ 1.86 + bool Initialize(); 1.87 + 1.88 + /** 1.89 + * Sets the clipping region for this layer manager. This is important on 1.90 + * windows because using OGL we no longer have GDI's native clipping. Therefor 1.91 + * widget must tell us what part of the screen is being invalidated, 1.92 + * and we should clip to this. 1.93 + * 1.94 + * \param aClippingRegion Region to clip to. Setting an empty region 1.95 + * will disable clipping. 1.96 + */ 1.97 + void SetClippingRegion(const nsIntRegion& aClippingRegion) 1.98 + { 1.99 + mClippingRegion = aClippingRegion; 1.100 + } 1.101 + 1.102 + /** 1.103 + * LayerManager implementation. 1.104 + */ 1.105 + virtual LayerManagerComposite* AsLayerManagerComposite() MOZ_OVERRIDE 1.106 + { 1.107 + return this; 1.108 + } 1.109 + 1.110 + void UpdateRenderBounds(const nsIntRect& aRect); 1.111 + 1.112 + virtual void BeginTransaction() MOZ_OVERRIDE; 1.113 + virtual void BeginTransactionWithTarget(gfxContext* aTarget) MOZ_OVERRIDE 1.114 + { 1.115 + MOZ_CRASH("Use BeginTransactionWithDrawTarget"); 1.116 + } 1.117 + void BeginTransactionWithDrawTarget(gfx::DrawTarget* aTarget); 1.118 + 1.119 + virtual bool EndEmptyTransaction(EndTransactionFlags aFlags = END_DEFAULT) MOZ_OVERRIDE; 1.120 + virtual void EndTransaction(DrawThebesLayerCallback aCallback, 1.121 + void* aCallbackData, 1.122 + EndTransactionFlags aFlags = END_DEFAULT) MOZ_OVERRIDE; 1.123 + 1.124 + virtual void SetRoot(Layer* aLayer) MOZ_OVERRIDE { mRoot = aLayer; } 1.125 + 1.126 + // XXX[nrc]: never called, we should move this logic to ClientLayerManager 1.127 + // (bug 946926). 1.128 + virtual bool CanUseCanvasLayerForSize(const gfx::IntSize &aSize) MOZ_OVERRIDE; 1.129 + 1.130 + virtual int32_t GetMaxTextureSize() const MOZ_OVERRIDE 1.131 + { 1.132 + MOZ_CRASH("Call on compositor, not LayerManagerComposite"); 1.133 + } 1.134 + 1.135 + virtual void ClearCachedResources(Layer* aSubtree = nullptr) MOZ_OVERRIDE; 1.136 + 1.137 + virtual already_AddRefed<ThebesLayer> CreateThebesLayer() MOZ_OVERRIDE; 1.138 + virtual already_AddRefed<ContainerLayer> CreateContainerLayer() MOZ_OVERRIDE; 1.139 + virtual already_AddRefed<ImageLayer> CreateImageLayer() MOZ_OVERRIDE; 1.140 + virtual already_AddRefed<ColorLayer> CreateColorLayer() MOZ_OVERRIDE; 1.141 + virtual already_AddRefed<CanvasLayer> CreateCanvasLayer() MOZ_OVERRIDE; 1.142 + already_AddRefed<ThebesLayerComposite> CreateThebesLayerComposite(); 1.143 + already_AddRefed<ContainerLayerComposite> CreateContainerLayerComposite(); 1.144 + already_AddRefed<ImageLayerComposite> CreateImageLayerComposite(); 1.145 + already_AddRefed<ColorLayerComposite> CreateColorLayerComposite(); 1.146 + already_AddRefed<CanvasLayerComposite> CreateCanvasLayerComposite(); 1.147 + already_AddRefed<RefLayerComposite> CreateRefLayerComposite(); 1.148 + 1.149 + virtual LayersBackend GetBackendType() MOZ_OVERRIDE 1.150 + { 1.151 + MOZ_CRASH("Shouldn't be called for composited layer manager"); 1.152 + } 1.153 + virtual void GetBackendName(nsAString& name) MOZ_OVERRIDE 1.154 + { 1.155 + MOZ_CRASH("Shouldn't be called for composited layer manager"); 1.156 + } 1.157 + 1.158 + virtual TemporaryRef<DrawTarget> 1.159 + CreateOptimalMaskDrawTarget(const IntSize &aSize) MOZ_OVERRIDE; 1.160 + 1.161 + virtual const char* Name() const MOZ_OVERRIDE { return ""; } 1.162 + 1.163 + enum WorldTransforPolicy { 1.164 + ApplyWorldTransform, 1.165 + DontApplyWorldTransform 1.166 + }; 1.167 + 1.168 + /** 1.169 + * Setup World transform matrix. 1.170 + * Transform will be ignored if it is not PreservesAxisAlignedRectangles 1.171 + * or has non integer scale 1.172 + */ 1.173 + void SetWorldTransform(const gfx::Matrix& aMatrix); 1.174 + gfx::Matrix& GetWorldTransform(void); 1.175 + 1.176 + /** 1.177 + * RAII helper class to add a mask effect with the compositable from aMaskLayer 1.178 + * to the EffectChain aEffect and notify the compositable when we are done. 1.179 + */ 1.180 + class AutoAddMaskEffect 1.181 + { 1.182 + public: 1.183 + AutoAddMaskEffect(Layer* aMaskLayer, 1.184 + EffectChain& aEffect, 1.185 + bool aIs3D = false); 1.186 + ~AutoAddMaskEffect(); 1.187 + 1.188 + private: 1.189 + CompositableHost* mCompositable; 1.190 + }; 1.191 + 1.192 + /** 1.193 + * Creates a DrawTarget which is optimized for inter-operating with this 1.194 + * layermanager. 1.195 + */ 1.196 + virtual TemporaryRef<mozilla::gfx::DrawTarget> 1.197 + CreateDrawTarget(const mozilla::gfx::IntSize& aSize, 1.198 + mozilla::gfx::SurfaceFormat aFormat) MOZ_OVERRIDE; 1.199 + 1.200 + /** 1.201 + * Calculates the 'completeness' of the rendering that intersected with the 1.202 + * screen on the last render. This is only useful when progressive tile 1.203 + * drawing is enabled, otherwise this will always return 1.0. 1.204 + * This function's expense scales with the size of the layer tree and the 1.205 + * complexity of individual layers' valid regions. 1.206 + */ 1.207 + float ComputeRenderIntegrity(); 1.208 + 1.209 + /** 1.210 + * returns true if PlatformAllocBuffer will return a buffer that supports 1.211 + * direct texturing 1.212 + */ 1.213 + static bool SupportsDirectTexturing(); 1.214 + 1.215 + static void PlatformSyncBeforeReplyUpdate(); 1.216 + 1.217 + void AddInvalidRegion(const nsIntRegion& aRegion) 1.218 + { 1.219 + mInvalidRegion.Or(mInvalidRegion, aRegion); 1.220 + } 1.221 + 1.222 + Compositor* GetCompositor() const 1.223 + { 1.224 + return mCompositor; 1.225 + } 1.226 + 1.227 + /** 1.228 + * LayerManagerComposite provides sophisticated debug overlays 1.229 + * that can request a next frame. 1.230 + */ 1.231 + bool DebugOverlayWantsNextFrame() { return mDebugOverlayWantsNextFrame; } 1.232 + void SetDebugOverlayWantsNextFrame(bool aVal) 1.233 + { mDebugOverlayWantsNextFrame = aVal; } 1.234 + 1.235 + void NotifyShadowTreeTransaction(); 1.236 + 1.237 + TextRenderer* GetTextRenderer() { return mTextRenderer; } 1.238 + 1.239 +private: 1.240 + /** Region we're clipping our current drawing to. */ 1.241 + nsIntRegion mClippingRegion; 1.242 + nsIntRect mRenderBounds; 1.243 + 1.244 + /** Current root layer. */ 1.245 + LayerComposite* RootLayer() const; 1.246 + 1.247 + /** 1.248 + * Recursive helper method for use by ComputeRenderIntegrity. Subtracts 1.249 + * any incomplete rendering on aLayer from aScreenRegion. Any low-precision 1.250 + * rendering is included in aLowPrecisionScreenRegion. aTransform is the 1.251 + * accumulated transform of intermediate surfaces beneath aLayer. 1.252 + */ 1.253 + static void ComputeRenderIntegrityInternal(Layer* aLayer, 1.254 + nsIntRegion& aScreenRegion, 1.255 + nsIntRegion& aLowPrecisionScreenRegion, 1.256 + const gfx3DMatrix& aTransform); 1.257 + 1.258 + /** 1.259 + * Render the current layer tree to the active target. 1.260 + */ 1.261 + void Render(); 1.262 + 1.263 + /** 1.264 + * Render debug overlays such as the FPS/FrameCounter above the frame. 1.265 + */ 1.266 + void RenderDebugOverlay(const gfx::Rect& aBounds); 1.267 + 1.268 + void WorldTransformRect(nsIntRect& aRect); 1.269 + 1.270 + RefPtr<Compositor> mCompositor; 1.271 + nsAutoPtr<LayerProperties> mClonedLayerTreeProperties; 1.272 + 1.273 + /** 1.274 + * Context target, nullptr when drawing directly to our swap chain. 1.275 + */ 1.276 + RefPtr<gfx::DrawTarget> mTarget; 1.277 + 1.278 + gfx::Matrix mWorldMatrix; 1.279 + nsIntRegion mInvalidRegion; 1.280 + nsAutoPtr<FPSState> mFPS; 1.281 + 1.282 + bool mInTransaction; 1.283 + bool mIsCompositorReady; 1.284 + bool mDebugOverlayWantsNextFrame; 1.285 + 1.286 + RefPtr<TextRenderer> mTextRenderer; 1.287 + bool mGeometryChanged; 1.288 +}; 1.289 + 1.290 +/** 1.291 + * Composite layers are for use with OMTC on the compositor thread only. There 1.292 + * must be corresponding Basic layers on the content thread. For composite 1.293 + * layers, the layer manager only maintains the layer tree, all rendering is 1.294 + * done by a Compositor (see Compositor.h). As such, composite layers are 1.295 + * platform-independent and can be used on any platform for which there is a 1.296 + * Compositor implementation. 1.297 + * 1.298 + * The composite layer tree reflects exactly the basic layer tree. To 1.299 + * composite to screen, the layer manager walks the layer tree calling render 1.300 + * methods which in turn call into their CompositableHosts' Composite methods. 1.301 + * These call Compositor::DrawQuad to do the rendering. 1.302 + * 1.303 + * Mostly, layers are updated during the layers transaction. This is done from 1.304 + * CompositableClient to CompositableHost without interacting with the layer. 1.305 + * 1.306 + * A reference to the Compositor is stored in LayerManagerComposite. 1.307 + */ 1.308 +class LayerComposite 1.309 +{ 1.310 +public: 1.311 + LayerComposite(LayerManagerComposite* aManager); 1.312 + 1.313 + virtual ~LayerComposite(); 1.314 + 1.315 + virtual LayerComposite* GetFirstChildComposite() 1.316 + { 1.317 + return nullptr; 1.318 + } 1.319 + 1.320 + /* Do NOT call this from the generic LayerComposite destructor. Only from the 1.321 + * concrete class destructor 1.322 + */ 1.323 + virtual void Destroy(); 1.324 + 1.325 + virtual Layer* GetLayer() = 0; 1.326 + 1.327 + virtual void RenderLayer(const nsIntRect& aClipRect) = 0; 1.328 + 1.329 + virtual bool SetCompositableHost(CompositableHost*) 1.330 + { 1.331 + // We must handle this gracefully, see bug 967824 1.332 + NS_WARNING("called SetCompositableHost for a layer type not accepting a compositable"); 1.333 + return false; 1.334 + } 1.335 + virtual CompositableHost* GetCompositableHost() = 0; 1.336 + 1.337 + virtual void CleanupResources() = 0; 1.338 + 1.339 + virtual TiledLayerComposer* GetTiledLayerComposer() { return nullptr; } 1.340 + 1.341 + 1.342 + virtual void DestroyFrontBuffer() { } 1.343 + 1.344 + /** 1.345 + * The following methods are 1.346 + * 1.347 + * CONSTRUCTION PHASE ONLY 1.348 + * 1.349 + * They are analogous to the Layer interface. 1.350 + */ 1.351 + void SetShadowVisibleRegion(const nsIntRegion& aRegion) 1.352 + { 1.353 + mShadowVisibleRegion = aRegion; 1.354 + } 1.355 + 1.356 + void SetShadowOpacity(float aOpacity) 1.357 + { 1.358 + mShadowOpacity = aOpacity; 1.359 + } 1.360 + 1.361 + void SetShadowClipRect(const nsIntRect* aRect) 1.362 + { 1.363 + mUseShadowClipRect = aRect != nullptr; 1.364 + if (aRect) { 1.365 + mShadowClipRect = *aRect; 1.366 + } 1.367 + } 1.368 + 1.369 + void SetShadowTransform(const gfx::Matrix4x4& aMatrix) 1.370 + { 1.371 + mShadowTransform = aMatrix; 1.372 + } 1.373 + void SetShadowTransformSetByAnimation(bool aSetByAnimation) 1.374 + { 1.375 + mShadowTransformSetByAnimation = aSetByAnimation; 1.376 + } 1.377 + 1.378 + void SetLayerComposited(bool value) 1.379 + { 1.380 + mLayerComposited = value; 1.381 + } 1.382 + 1.383 + void SetClearRect(const nsIntRect& aRect) 1.384 + { 1.385 + mClearRect = aRect; 1.386 + } 1.387 + 1.388 + // These getters can be used anytime. 1.389 + float GetShadowOpacity() { return mShadowOpacity; } 1.390 + const nsIntRect* GetShadowClipRect() { return mUseShadowClipRect ? &mShadowClipRect : nullptr; } 1.391 + const nsIntRegion& GetShadowVisibleRegion() { return mShadowVisibleRegion; } 1.392 + const gfx::Matrix4x4& GetShadowTransform() { return mShadowTransform; } 1.393 + bool GetShadowTransformSetByAnimation() { return mShadowTransformSetByAnimation; } 1.394 + bool HasLayerBeenComposited() { return mLayerComposited; } 1.395 + nsIntRect GetClearRect() { return mClearRect; } 1.396 + 1.397 +protected: 1.398 + gfx::Matrix4x4 mShadowTransform; 1.399 + nsIntRegion mShadowVisibleRegion; 1.400 + nsIntRect mShadowClipRect; 1.401 + LayerManagerComposite* mCompositeManager; 1.402 + RefPtr<Compositor> mCompositor; 1.403 + float mShadowOpacity; 1.404 + bool mUseShadowClipRect; 1.405 + bool mShadowTransformSetByAnimation; 1.406 + bool mDestroyed; 1.407 + bool mLayerComposited; 1.408 + nsIntRect mClearRect; 1.409 +}; 1.410 + 1.411 + 1.412 +} /* layers */ 1.413 +} /* mozilla */ 1.414 + 1.415 +#endif /* GFX_LayerManagerComposite_H */