summaryrefslogtreecommitdiffstats
path: root/gfx/layers/mlgpu/LayerMLGPU.h
blob: d11106688f673c7493c9684d2b82cc56e730816a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_gfx_layers_mlgpu_LayerMLGPU_h
#define mozilla_gfx_layers_mlgpu_LayerMLGPU_h

#include "Layers.h"
#include "mozilla/layers/LayerManagerComposite.h"

namespace mozilla {
namespace layers {

class CanvasLayerMLGPU;
class ColorLayerMLGPU;
class ContainerLayerMLGPU;
class FrameBuilder;
class ImageHost;
class ImageLayerMLGPU;
class LayerManagerMLGPU;
class MaskOperation;
class MLGRenderTarget;
class PaintedLayerMLGPU;
class RefLayerMLGPU;
class RenderViewMLGPU;
class TexturedLayerMLGPU;
class TextureSource;

class LayerMLGPU : public HostLayer {
 public:
  LayerMLGPU* AsLayerMLGPU() override { return this; }
  virtual PaintedLayerMLGPU* AsPaintedLayerMLGPU() { return nullptr; }
  virtual ImageLayerMLGPU* AsImageLayerMLGPU() { return nullptr; }
  virtual CanvasLayerMLGPU* AsCanvasLayerMLGPU() { return nullptr; }
  virtual ContainerLayerMLGPU* AsContainerLayerMLGPU() { return nullptr; }
  virtual RefLayerMLGPU* AsRefLayerMLGPU() { return nullptr; }
  virtual ColorLayerMLGPU* AsColorLayerMLGPU() { return nullptr; }
  virtual TexturedLayerMLGPU* AsTexturedLayerMLGPU() { return nullptr; }

  static void BeginFrame();

  // Ask the layer to acquire any resources or per-frame information needed
  // to render. If this returns false, the layer will be skipped entirely.
  bool PrepareToRender(FrameBuilder* aBuilder,
                       const RenderTargetIntRect& aClipRect);

  Layer::LayerType GetType() { return GetLayer()->GetType(); }
  const RenderTargetIntRect& GetComputedClipRect() const {
    return mComputedClipRect;
  }
  MaskOperation* GetMask() const { return mMask; }
  float GetComputedOpacity() const { return mComputedOpacity; }

  // Return the bounding box of this layer in render target space, clipped to
  // the computed clip rect, and rounded out to an integer rect.
  gfx::IntRect GetClippedBoundingBox(RenderViewMLGPU* aView,
                                     const Maybe<gfx::Polygon>& aGeometry);

  // If this layer has already been prepared for the current frame, return
  // true. This should only be used to guard against double-processing
  // container layers after 3d-sorting.
  bool IsPrepared() const { return mFrameKey == sFrameKey && mPrepared; }

  // Return true if the content in this layer is opaque (not factoring in
  // blend modes or opacity), false otherwise.
  virtual bool IsContentOpaque();

  // Returns the region that this layer will draw pixels to. If the layer and
  // its content are opaque, this is the layer's opaque region.
  const LayerIntRegion& GetRenderRegion() const { return mRenderRegion; }

  // Some layers have visible regions that extend beyond what is actually drawn.
  // When performing CPU-based occlusion culling we must clamp the visible
  // region to the actual area. Note that if a layer is opaque, it must not
  // expand its visible region such that it might include non-opaque pixels, as
  // may be the case for PaintedLayers with a restricted visible region.
  virtual void SetRenderRegion(LayerIntRegion&& aRegion);

  virtual void AssignToView(FrameBuilder* aBuilder, RenderViewMLGPU* aView,
                            Maybe<gfx::Polygon>&& aGeometry);

  // Callback for when PrepareToRender has finished successfully. If this
  // returns false, PrepareToRender will return false.
  virtual bool OnPrepareToRender(FrameBuilder* aBuilder) { return true; }

  virtual void ClearCachedResources() {}
  CompositableHost* GetCompositableHost() override { return nullptr; }

 protected:
  LayerMLGPU(LayerManagerMLGPU* aManager);
  ~LayerMLGPU();
  LayerManagerMLGPU* GetManager();

  void AddBoundsToView(FrameBuilder* aBuilder, RenderViewMLGPU* aView,
                       Maybe<gfx::Polygon>&& aGeometry);

  void MarkPrepared();

  // We don't want derivative layers overriding this directly - we provide a
  // callback instead.
  void SetLayerManager(HostLayerManager* aManager) override;
  virtual void OnLayerManagerChange(LayerManagerMLGPU* aManager) {}

 private:
  // This is a monotonic counter used to check whether a layer appears twice
  // when 3d sorting.
  static uint64_t sFrameKey;

 protected:
  // These are set during PrepareToRender.
  RenderTargetIntRect mComputedClipRect;
  RefPtr<MaskOperation> mMask;
  uint64_t mFrameKey;
  float mComputedOpacity;
  bool mPrepared;
  LayerIntRegion mRenderRegion;
};

class RefLayerMLGPU final : public RefLayer, public LayerMLGPU {
 public:
  explicit RefLayerMLGPU(LayerManagerMLGPU* aManager);
  virtual ~RefLayerMLGPU();

  // Layer
  HostLayer* AsHostLayer() override { return this; }
  RefLayerMLGPU* AsRefLayerMLGPU() override { return this; }
  Layer* GetLayer() override { return this; }

  // ContainerLayer
  void ComputeEffectiveTransforms(
      const gfx::Matrix4x4& aTransformToSurface) override {
    DefaultComputeEffectiveTransforms(aTransformToSurface);
  }

  const LayerIntRegion& GetShadowVisibleRegion() override;

  MOZ_LAYER_DECL_NAME("RefLayerMLGPU", TYPE_REF)
};

class ColorLayerMLGPU final : public ColorLayer, public LayerMLGPU {
 public:
  explicit ColorLayerMLGPU(LayerManagerMLGPU* aManager);
  virtual ~ColorLayerMLGPU();

  // LayerMLGPU
  bool IsContentOpaque() override { return mColor.a >= 1.0f; }

  // Layer
  HostLayer* AsHostLayer() override { return this; }
  ColorLayerMLGPU* AsColorLayerMLGPU() override { return this; }
  Layer* GetLayer() override { return this; }

  MOZ_LAYER_DECL_NAME("ColorLayerMLGPU", TYPE_COLOR)
};

}  // namespace layers
}  // namespace mozilla

#endif  // mozilla_gfx_layers_mlgpu_LayerMLGPU_h