1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_gfx_layers_mlgpu_LayerMLGPU_h
8 #define mozilla_gfx_layers_mlgpu_LayerMLGPU_h
9 
10 #include "Layers.h"
11 #include "mozilla/layers/LayerManagerComposite.h"
12 
13 namespace mozilla {
14 namespace layers {
15 
16 class CanvasLayerMLGPU;
17 class ColorLayerMLGPU;
18 class ContainerLayerMLGPU;
19 class FrameBuilder;
20 class ImageHost;
21 class ImageLayerMLGPU;
22 class LayerManagerMLGPU;
23 class MaskOperation;
24 class MLGRenderTarget;
25 class PaintedLayerMLGPU;
26 class RefLayerMLGPU;
27 class RenderViewMLGPU;
28 class TexturedLayerMLGPU;
29 class TextureSource;
30 
31 class LayerMLGPU : public HostLayer {
32  public:
AsLayerMLGPU()33   LayerMLGPU* AsLayerMLGPU() override { return this; }
AsPaintedLayerMLGPU()34   virtual PaintedLayerMLGPU* AsPaintedLayerMLGPU() { return nullptr; }
AsImageLayerMLGPU()35   virtual ImageLayerMLGPU* AsImageLayerMLGPU() { return nullptr; }
AsCanvasLayerMLGPU()36   virtual CanvasLayerMLGPU* AsCanvasLayerMLGPU() { return nullptr; }
AsContainerLayerMLGPU()37   virtual ContainerLayerMLGPU* AsContainerLayerMLGPU() { return nullptr; }
AsRefLayerMLGPU()38   virtual RefLayerMLGPU* AsRefLayerMLGPU() { return nullptr; }
AsColorLayerMLGPU()39   virtual ColorLayerMLGPU* AsColorLayerMLGPU() { return nullptr; }
AsTexturedLayerMLGPU()40   virtual TexturedLayerMLGPU* AsTexturedLayerMLGPU() { return nullptr; }
41 
42   static void BeginFrame();
43 
44   // Ask the layer to acquire any resources or per-frame information needed
45   // to render. If this returns false, the layer will be skipped entirely.
46   bool PrepareToRender(FrameBuilder* aBuilder,
47                        const RenderTargetIntRect& aClipRect);
48 
GetType()49   Layer::LayerType GetType() { return GetLayer()->GetType(); }
GetComputedClipRect()50   const RenderTargetIntRect& GetComputedClipRect() const {
51     return mComputedClipRect;
52   }
GetMask()53   MaskOperation* GetMask() const { return mMask; }
GetComputedOpacity()54   float GetComputedOpacity() const { return mComputedOpacity; }
55 
56   // Return the bounding box of this layer in render target space, clipped to
57   // the computed clip rect, and rounded out to an integer rect.
58   gfx::IntRect GetClippedBoundingBox(RenderViewMLGPU* aView,
59                                      const Maybe<gfx::Polygon>& aGeometry);
60 
61   // If this layer has already been prepared for the current frame, return
62   // true. This should only be used to guard against double-processing
63   // container layers after 3d-sorting.
IsPrepared()64   bool IsPrepared() const { return mFrameKey == sFrameKey && mPrepared; }
65 
66   // Return true if the content in this layer is opaque (not factoring in
67   // blend modes or opacity), false otherwise.
68   virtual bool IsContentOpaque();
69 
70   // Returns the region that this layer will draw pixels to. If the layer and
71   // its content are opaque, this is the layer's opaque region.
GetRenderRegion()72   const LayerIntRegion& GetRenderRegion() const { return mRenderRegion; }
73 
74   // Some layers have visible regions that extend beyond what is actually drawn.
75   // When performing CPU-based occlusion culling we must clamp the visible
76   // region to the actual area. Note that if a layer is opaque, it must not
77   // expand its visible region such that it might include non-opaque pixels, as
78   // may be the case for PaintedLayers with a restricted visible region.
79   virtual void SetRenderRegion(LayerIntRegion&& aRegion);
80 
81   virtual void AssignToView(FrameBuilder* aBuilder, RenderViewMLGPU* aView,
82                             Maybe<gfx::Polygon>&& aGeometry);
83 
84   // Callback for when PrepareToRender has finished successfully. If this
85   // returns false, PrepareToRender will return false.
OnPrepareToRender(FrameBuilder * aBuilder)86   virtual bool OnPrepareToRender(FrameBuilder* aBuilder) { return true; }
87 
ClearCachedResources()88   virtual void ClearCachedResources() {}
GetCompositableHost()89   CompositableHost* GetCompositableHost() override { return nullptr; }
90 
91  protected:
92   LayerMLGPU(LayerManagerMLGPU* aManager);
93   ~LayerMLGPU();
94   LayerManagerMLGPU* GetManager();
95 
96   void AddBoundsToView(FrameBuilder* aBuilder, RenderViewMLGPU* aView,
97                        Maybe<gfx::Polygon>&& aGeometry);
98 
99   void MarkPrepared();
100 
101   // We don't want derivative layers overriding this directly - we provide a
102   // callback instead.
103   void SetLayerManager(HostLayerManager* aManager) override;
OnLayerManagerChange(LayerManagerMLGPU * aManager)104   virtual void OnLayerManagerChange(LayerManagerMLGPU* aManager) {}
105 
106  private:
107   // This is a monotonic counter used to check whether a layer appears twice
108   // when 3d sorting.
109   static uint64_t sFrameKey;
110 
111  protected:
112   // These are set during PrepareToRender.
113   RenderTargetIntRect mComputedClipRect;
114   RefPtr<MaskOperation> mMask;
115   uint64_t mFrameKey;
116   float mComputedOpacity;
117   bool mPrepared;
118   LayerIntRegion mRenderRegion;
119 };
120 
121 class RefLayerMLGPU final : public RefLayer, public LayerMLGPU {
122  public:
123   explicit RefLayerMLGPU(LayerManagerMLGPU* aManager);
124   virtual ~RefLayerMLGPU();
125 
126   // Layer
AsHostLayer()127   HostLayer* AsHostLayer() override { return this; }
AsRefLayerMLGPU()128   RefLayerMLGPU* AsRefLayerMLGPU() override { return this; }
GetLayer()129   Layer* GetLayer() override { return this; }
130 
131   // ContainerLayer
ComputeEffectiveTransforms(const gfx::Matrix4x4 & aTransformToSurface)132   void ComputeEffectiveTransforms(
133       const gfx::Matrix4x4& aTransformToSurface) override {
134     DefaultComputeEffectiveTransforms(aTransformToSurface);
135   }
136 
137   const LayerIntRegion& GetShadowVisibleRegion() override;
138 
139   MOZ_LAYER_DECL_NAME("RefLayerMLGPU", TYPE_REF)
140 };
141 
142 class ColorLayerMLGPU final : public ColorLayer, public LayerMLGPU {
143  public:
144   explicit ColorLayerMLGPU(LayerManagerMLGPU* aManager);
145   virtual ~ColorLayerMLGPU();
146 
147   // LayerMLGPU
IsContentOpaque()148   bool IsContentOpaque() override { return mColor.a >= 1.0f; }
149 
150   // Layer
AsHostLayer()151   HostLayer* AsHostLayer() override { return this; }
AsColorLayerMLGPU()152   ColorLayerMLGPU* AsColorLayerMLGPU() override { return this; }
GetLayer()153   Layer* GetLayer() override { return this; }
154 
155   MOZ_LAYER_DECL_NAME("ColorLayerMLGPU", TYPE_COLOR)
156 };
157 
158 }  // namespace layers
159 }  // namespace mozilla
160 
161 #endif  // mozilla_gfx_layers_mlgpu_LayerMLGPU_h
162