summaryrefslogtreecommitdiffstats
path: root/gfx/layers/mlgpu/FrameBuilder.h
blob: 5cf5714767caa32b7afb81a8f38d85a1706b3716 (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
/* -*- 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_FrameBuilder_h
#define mozilla_gfx_layers_mlgpu_FrameBuilder_h

#include "mozilla/RefPtr.h"
#include "mozilla/gfx/Point.h"
#include "mozilla/gfx/Types.h"
#include "MaskOperation.h"
#include "MLGDevice.h"
#include "nsDataHashtable.h"
#include "nsRefPtrHashtable.h"
#include "ShaderDefinitionsMLGPU.h"
#include "SharedBufferMLGPU.h"
#include "Units.h"
#include <map>
#include <vector>

namespace mozilla {
namespace layers {

class ContainerLayer;
class ContainerLayerMLGPU;
class Layer;
class LayerMLGPU;
class LayerManagerMLGPU;
class MLGDevice;
class MLGRenderTarget;
class MLGSwapChain;
class RenderViewMLGPU;
struct ItemInfo;

class FrameBuilder final {
 public:
  FrameBuilder(LayerManagerMLGPU* aManager, MLGSwapChain* aSwapChain);
  ~FrameBuilder();

  bool Build();
  void Render();

  bool AddLayerToConstantBuffer(ItemInfo& aItem);

  LayerManagerMLGPU* GetManager() const { return mManager; }
  MLGDevice* GetDevice() const { return mDevice; }
  const ConstantBufferSection& GetDefaultMaskInfo() const {
    return mDefaultMaskInfo;
  }

  // Called during tile construction. Finds or adds a mask layer chain to the
  // cache, that will be flattened as a dependency to rendering batches.
  MaskOperation* AddMaskOperation(LayerMLGPU* aLayer);

  // Note: These should only be called during batch construction.
  size_t CurrentLayerBufferIndex() const;
  size_t CurrentMaskRectBufferIndex() const;

  // These are called during rendering, and may return null if a buffer
  // couldn't be allocated.
  ConstantBufferSection GetLayerBufferByIndex(size_t aIndex) const;
  ConstantBufferSection GetMaskRectBufferByIndex(size_t aIndex) const;

  // Hold a layer alive until the frame ends.
  void RetainTemporaryLayer(LayerMLGPU* aLayer);

  MLGRenderTarget* GetWidgetRT();

 private:
  void AssignLayer(Layer* aLayer, RenderViewMLGPU* aView,
                   const RenderTargetIntRect& aClipRect,
                   Maybe<gfx::Polygon>&& aGeometry);

  void ProcessChildList(ContainerLayer* aContainer, RenderViewMLGPU* aView,
                        const RenderTargetIntRect& aParentClipRect,
                        const Maybe<gfx::Polygon>& aParentGeometry);

  mlg::LayerConstants* AllocateLayerInfo(ItemInfo& aItem);
  bool AddMaskRect(const gfx::Rect& aRect, uint32_t* aOutIndex);
  void FinishCurrentLayerBuffer();
  void FinishCurrentMaskRectBuffer();

  // Returns true to continue, false to stop - false does not indicate
  // failure.
  bool ProcessContainerLayer(ContainerLayer* aLayer, RenderViewMLGPU* aView,
                             const RenderTargetIntRect& aClipRect,
                             Maybe<gfx::Polygon>& aGeometry);

 private:
  RefPtr<LayerManagerMLGPU> mManager;
  RefPtr<MLGDevice> mDevice;
  RefPtr<MLGSwapChain> mSwapChain;
  RefPtr<RenderViewMLGPU> mWidgetRenderView;
  LayerMLGPU* mRoot;

  // Each time we consume a layer in a tile, we make sure a constant buffer
  // exists that contains information about the layer. The mapping is valid
  // for the most recent buffer, and once the buffer fills, we begin a new
  // one and clear the map.
  nsTArray<ConstantBufferSection> mLayerBuffers;
  nsTArray<mlg::LayerConstants> mCurrentLayerBuffer;
  nsDataHashtable<nsPtrHashKey<LayerMLGPU>, uint32_t> mLayerBufferMap;

  // We keep mask rects in a separate buffer since they're rare.
  nsTArray<ConstantBufferSection> mMaskRectBuffers;
  nsTArray<gfx::Rect> mCurrentMaskRectList;

  // For values that *can* change every render pass, but almost certainly do
  // not, we pre-fill and cache some buffers.
  ConstantBufferSection mDefaultMaskInfo;

  // Cache for MaskOperations.
  nsRefPtrHashtable<nsRefPtrHashKey<TextureSource>, MaskOperation>
      mSingleTextureMasks;
  std::map<MaskTextureList, RefPtr<MaskCombineOperation>> mCombinedTextureMasks;

  // This list of temporary layers is wiped out when the frame is completed.
  std::vector<RefPtr<Layer>> mTemporaryLayers;
};

}  // namespace layers
}  // namespace mozilla

#endif  // mozilla_gfx_layers_mlgpu_FrameBuilder_h