Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef mozilla_gfx_layers_mlgpu_FrameBuilder_h
7 : #define mozilla_gfx_layers_mlgpu_FrameBuilder_h
8 :
9 : #include "mozilla/RefPtr.h"
10 : #include "mozilla/gfx/Point.h"
11 : #include "mozilla/gfx/Types.h"
12 : #include "MaskOperation.h"
13 : #include "nsDataHashtable.h"
14 : #include "nsRefPtrHashtable.h"
15 : #include "ShaderDefinitionsMLGPU.h"
16 : #include "SharedBufferMLGPU.h"
17 : #include "Units.h"
18 : #include <map>
19 : #include <vector>
20 :
21 : namespace mozilla {
22 : namespace layers {
23 :
24 : class ContainerLayer;
25 : class ContainerLayerMLGPU;
26 : class Layer;
27 : class LayerMLGPU;
28 : class LayerManagerMLGPU;
29 : class MLGDevice;
30 : class MLGRenderTarget;
31 : class MLGSwapChain;
32 : class RenderViewMLGPU;
33 : struct ItemInfo;
34 :
35 : class FrameBuilder final
36 : {
37 : public:
38 : FrameBuilder(LayerManagerMLGPU* aManager, MLGSwapChain* aSwapChain);
39 : ~FrameBuilder();
40 :
41 : bool Build();
42 : void Render();
43 :
44 : bool AddLayerToConstantBuffer(ItemInfo& aItem);
45 :
46 0 : LayerManagerMLGPU* GetManager() const {
47 0 : return mManager;
48 : }
49 0 : MLGDevice* GetDevice() const {
50 0 : return mDevice;
51 : }
52 0 : const ConstantBufferSection& GetDefaultMaskInfo() const {
53 0 : return mDefaultMaskInfo;
54 : }
55 :
56 : // Called during tile construction. Finds or adds a mask layer chain to the
57 : // cache, that will be flattened as a dependency to rendering batches.
58 : MaskOperation* AddMaskOperation(LayerMLGPU* aLayer);
59 :
60 : // Note: These should only be called during batch construction.
61 : size_t CurrentLayerBufferIndex() const;
62 : size_t CurrentMaskRectBufferIndex() const;
63 :
64 : // These are called during rendering, and may return null if a buffer
65 : // couldn't be allocated.
66 : ConstantBufferSection GetLayerBufferByIndex(size_t aIndex) const;
67 : ConstantBufferSection GetMaskRectBufferByIndex(size_t aIndex) const;
68 :
69 : // Hold a layer alive until the frame ends.
70 : void RetainTemporaryLayer(LayerMLGPU* aLayer);
71 :
72 : private:
73 : void AssignLayer(Layer* aLayer,
74 : RenderViewMLGPU* aView,
75 : const RenderTargetIntRect& aClipRect,
76 : Maybe<gfx::Polygon>&& aGeometry);
77 :
78 : void ProcessChildList(ContainerLayer* aContainer,
79 : RenderViewMLGPU* aView,
80 : const RenderTargetIntRect& aParentClipRect,
81 : const Maybe<gfx::Polygon>& aParentGeometry);
82 :
83 : mlg::LayerConstants* AllocateLayerInfo(ItemInfo& aItem);
84 : bool AddMaskRect(const gfx::Rect& aRect, uint32_t* aOutIndex);
85 : void FinishCurrentLayerBuffer();
86 : void FinishCurrentMaskRectBuffer();
87 :
88 : // Returns true to continue, false to stop - false does not indicate
89 : // failure.
90 : bool ProcessContainerLayer(ContainerLayer* aLayer,
91 : RenderViewMLGPU* aView,
92 : const RenderTargetIntRect& aClipRect,
93 : Maybe<gfx::Polygon>& aGeometry);
94 :
95 : private:
96 : RefPtr<LayerManagerMLGPU> mManager;
97 : RefPtr<MLGDevice> mDevice;
98 : RefPtr<MLGSwapChain> mSwapChain;
99 : RefPtr<RenderViewMLGPU> mWidgetRenderView;
100 : LayerMLGPU* mRoot;
101 :
102 : // Each time we consume a layer in a tile, we make sure a constant buffer
103 : // exists that contains information about the layer. The mapping is valid
104 : // for the most recent buffer, and once the buffer fills, we begin a new
105 : // one and clear the map.
106 : nsTArray<ConstantBufferSection> mLayerBuffers;
107 : nsTArray<mlg::LayerConstants> mCurrentLayerBuffer;
108 : nsDataHashtable<nsPtrHashKey<LayerMLGPU>, uint32_t> mLayerBufferMap;
109 :
110 : // We keep mask rects in a separate buffer since they're rare.
111 : nsTArray<ConstantBufferSection> mMaskRectBuffers;
112 : nsTArray<gfx::Rect> mCurrentMaskRectList;
113 :
114 : // For values that *can* change every render pass, but almost certainly do
115 : // not, we pre-fill and cache some buffers.
116 : ConstantBufferSection mDefaultMaskInfo;
117 :
118 : // Cache for MaskOperations.
119 : nsRefPtrHashtable<nsRefPtrHashKey<TextureSource>, MaskOperation> mSingleTextureMasks;
120 : std::map<MaskTextureList, RefPtr<MaskCombineOperation>> mCombinedTextureMasks;
121 :
122 : // This list of temporary layers is wiped out when the frame is completed.
123 : std::vector<RefPtr<Layer>> mTemporaryLayers;
124 : };
125 :
126 : } // namespace layers
127 : } // namespace mozilla
128 :
129 : #endif // mozilla_gfx_layers_mlgpu_FrameBuilder_h
|