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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/* -*- 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 GFX_WEBRENDERUSERDATA_H
#define GFX_WEBRENDERUSERDATA_H
#include <vector>
#include "mozilla/webrender/WebRenderAPI.h"
#include "mozilla/layers/AnimationInfo.h"
#include "mozilla/dom/RemoteBrowser.h"
#include "mozilla/UniquePtr.h"
#include "nsIFrame.h"
#include "nsRefPtrHashtable.h"
#include "ImageTypes.h"
class nsDisplayItemGeometry;
namespace mozilla {
namespace webgpu {
class WebGPUChild;
}
namespace wr {
class IpcResourceUpdateQueue;
}
namespace gfx {
class SourceSurface;
}
namespace layers {
class BasicLayerManager;
class CanvasLayer;
class ImageClient;
class ImageContainer;
class WebRenderBridgeChild;
class WebRenderCanvasData;
class WebRenderCanvasRenderer;
class WebRenderCanvasRendererAsync;
class WebRenderImageData;
class WebRenderFallbackData;
class WebRenderLocalCanvasData;
class RenderRootStateManager;
class WebRenderGroupData;
class WebRenderBackgroundData {
public:
WebRenderBackgroundData(wr::LayoutRect aBounds, wr::ColorF aColor)
: mBounds(aBounds), mColor(aColor) {}
void AddWebRenderCommands(wr::DisplayListBuilder& aBuilder);
protected:
wr::LayoutRect mBounds;
wr::ColorF mColor;
};
/// Parent class for arbitrary WebRender-specific data that can be associated
/// to an nsFrame.
class WebRenderUserData {
public:
typedef nsTHashtable<nsRefPtrHashKey<WebRenderUserData>>
WebRenderUserDataRefTable;
static bool SupportsAsyncUpdate(nsIFrame* aFrame);
static bool ProcessInvalidateForImage(nsIFrame* aFrame, DisplayItemType aType,
ContainerProducerID aProducerId);
NS_INLINE_DECL_REFCOUNTING(WebRenderUserData)
WebRenderUserData(RenderRootStateManager* aManager, nsDisplayItem* aItem);
WebRenderUserData(RenderRootStateManager* aManager, uint32_t mDisplayItemKey,
nsIFrame* aFrame);
virtual WebRenderImageData* AsImageData() { return nullptr; }
virtual WebRenderFallbackData* AsFallbackData() { return nullptr; }
virtual WebRenderCanvasData* AsCanvasData() { return nullptr; }
virtual WebRenderLocalCanvasData* AsLocalCanvasData() { return nullptr; }
virtual WebRenderGroupData* AsGroupData() { return nullptr; }
enum class UserDataType {
eImage,
eFallback,
eAPZAnimation,
eAnimation,
eCanvas,
eLocalCanvas,
eRemote,
eGroup,
eMask,
};
virtual UserDataType GetType() = 0;
bool IsUsed() { return mUsed; }
void SetUsed(bool aUsed) { mUsed = aUsed; }
nsIFrame* GetFrame() { return mFrame; }
uint32_t GetDisplayItemKey() { return mDisplayItemKey; }
void RemoveFromTable();
virtual nsDisplayItemGeometry* GetGeometry() { return nullptr; }
protected:
virtual ~WebRenderUserData();
WebRenderBridgeChild* WrBridge() const;
RefPtr<RenderRootStateManager> mManager;
nsIFrame* mFrame;
uint32_t mDisplayItemKey;
WebRenderUserDataRefTable* mTable;
bool mUsed;
};
struct WebRenderUserDataKey {
WebRenderUserDataKey(uint32_t aFrameKey,
WebRenderUserData::UserDataType aType)
: mFrameKey(aFrameKey), mType(aType) {}
bool operator==(const WebRenderUserDataKey& other) const {
return mFrameKey == other.mFrameKey && mType == other.mType;
}
PLDHashNumber Hash() const {
return HashGeneric(
mFrameKey,
static_cast<std::underlying_type<decltype(mType)>::type>(mType));
}
uint32_t mFrameKey;
WebRenderUserData::UserDataType mType;
};
typedef nsRefPtrHashtable<
nsGenericHashKey<mozilla::layers::WebRenderUserDataKey>, WebRenderUserData>
WebRenderUserDataTable;
/// Holds some data used to share TextureClient/ImageClient with the parent
/// process.
class WebRenderImageData : public WebRenderUserData {
public:
WebRenderImageData(RenderRootStateManager* aManager, nsDisplayItem* aItem);
WebRenderImageData(RenderRootStateManager* aManager, uint32_t aDisplayItemKey,
nsIFrame* aFrame);
virtual ~WebRenderImageData();
WebRenderImageData* AsImageData() override { return this; }
UserDataType GetType() override { return UserDataType::eImage; }
static UserDataType Type() { return UserDataType::eImage; }
Maybe<wr::ImageKey> GetImageKey() { return mKey; }
void SetImageKey(const wr::ImageKey& aKey);
already_AddRefed<ImageClient> GetImageClient();
Maybe<wr::ImageKey> UpdateImageKey(ImageContainer* aContainer,
wr::IpcResourceUpdateQueue& aResources,
bool aFallback = false);
void CreateAsyncImageWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder, ImageContainer* aContainer,
const StackingContextHelper& aSc, const LayoutDeviceRect& aBounds,
const LayoutDeviceRect& aSCBounds, VideoInfo::Rotation aRotation,
const wr::ImageRendering& aFilter, const wr::MixBlendMode& aMixBlendMode,
bool aIsBackfaceVisible);
void CreateImageClientIfNeeded();
bool IsAsync() { return mPipelineId.isSome(); }
bool UsingSharedSurface(ContainerProducerID aProducerId) const;
void ClearImageKey();
protected:
Maybe<wr::ImageKey> mKey;
RefPtr<TextureClient> mTextureOfImage;
RefPtr<ImageClient> mImageClient;
Maybe<wr::PipelineId> mPipelineId;
RefPtr<ImageContainer> mContainer;
// The key can be owned by a shared surface that is used by several elements.
// when this is the case the shared surface is responsible for managing the
// destruction of the key.
// TODO: we surely can come up with a simpler/safer way to model this.
bool mOwnsKey;
};
/// Used for fallback rendering.
///
/// In most cases this uses blob images but it can also render on the content
/// side directly into a texture.
class WebRenderFallbackData : public WebRenderUserData {
public:
WebRenderFallbackData(RenderRootStateManager* aManager, nsDisplayItem* aItem);
virtual ~WebRenderFallbackData();
WebRenderFallbackData* AsFallbackData() override { return this; }
UserDataType GetType() override { return UserDataType::eFallback; }
static UserDataType Type() { return UserDataType::eFallback; }
void SetInvalid(bool aInvalid) { mInvalid = aInvalid; }
bool IsInvalid() { return mInvalid; }
void SetFonts(const std::vector<RefPtr<gfx::ScaledFont>>& aFonts) {
mFonts = aFonts;
}
Maybe<wr::BlobImageKey> GetBlobImageKey() { return mBlobKey; }
void SetBlobImageKey(const wr::BlobImageKey& aKey);
Maybe<wr::ImageKey> GetImageKey();
/// Create a WebRenderImageData to manage the image we are about to render
/// into.
WebRenderImageData* PaintIntoImage();
std::vector<RefPtr<gfx::SourceSurface>> mExternalSurfaces;
RefPtr<BasicLayerManager> mBasicLayerManager;
UniquePtr<nsDisplayItemGeometry> mGeometry;
nsRect mBounds;
nsRect mBuildingRect;
gfx::Size mScale;
protected:
void ClearImageKey();
std::vector<RefPtr<gfx::ScaledFont>> mFonts;
Maybe<wr::BlobImageKey> mBlobKey;
// When rendering into a blob image, mImageData is null. It is non-null only
// when we render directly into a texture on the content side.
RefPtr<WebRenderImageData> mImageData;
bool mInvalid;
};
class WebRenderAPZAnimationData : public WebRenderUserData {
public:
WebRenderAPZAnimationData(RenderRootStateManager* aManager,
nsDisplayItem* aItem);
virtual ~WebRenderAPZAnimationData() = default;
UserDataType GetType() override { return UserDataType::eAPZAnimation; }
static UserDataType Type() { return UserDataType::eAPZAnimation; }
uint64_t GetAnimationId() { return mAnimationId; }
private:
uint64_t mAnimationId;
};
class WebRenderAnimationData : public WebRenderUserData {
public:
WebRenderAnimationData(RenderRootStateManager* aManager,
nsDisplayItem* aItem);
virtual ~WebRenderAnimationData();
UserDataType GetType() override { return UserDataType::eAnimation; }
static UserDataType Type() { return UserDataType::eAnimation; }
AnimationInfo& GetAnimationInfo() { return mAnimationInfo; }
protected:
AnimationInfo mAnimationInfo;
};
class WebRenderCanvasData : public WebRenderUserData {
public:
WebRenderCanvasData(RenderRootStateManager* aManager, nsDisplayItem* aItem);
virtual ~WebRenderCanvasData();
WebRenderCanvasData* AsCanvasData() override { return this; }
UserDataType GetType() override { return UserDataType::eCanvas; }
static UserDataType Type() { return UserDataType::eCanvas; }
void ClearCanvasRenderer();
WebRenderCanvasRendererAsync* GetCanvasRenderer();
WebRenderCanvasRendererAsync* CreateCanvasRenderer();
void SetImageContainer(ImageContainer* aImageContainer);
ImageContainer* GetImageContainer();
void ClearImageContainer();
protected:
RefPtr<WebRenderCanvasRendererAsync> mCanvasRenderer;
RefPtr<ImageContainer> mContainer;
};
// WebRender data assocatiated with canvases that don't need to
// synchronize across content-GPU process barrier.
class WebRenderLocalCanvasData : public WebRenderUserData {
public:
WebRenderLocalCanvasData(RenderRootStateManager* aManager,
nsDisplayItem* aItem);
virtual ~WebRenderLocalCanvasData();
WebRenderLocalCanvasData* AsLocalCanvasData() override { return this; }
UserDataType GetType() override { return UserDataType::eLocalCanvas; }
static UserDataType Type() { return UserDataType::eLocalCanvas; }
void RequestFrameReadback();
void RefreshExternalImage();
// TODO: introduce a CanvasRenderer derivative to store here?
WeakPtr<webgpu::WebGPUChild> mGpuBridge;
uint64_t mGpuTextureId = 0;
wr::ExternalImageId mExternalImageId = {0};
wr::ImageKey mImageKey = {};
wr::ImageDescriptor mDescriptor;
gfx::SurfaceFormat mFormat = gfx::SurfaceFormat::UNKNOWN;
bool mDirty = false;
};
class WebRenderRemoteData : public WebRenderUserData {
public:
WebRenderRemoteData(RenderRootStateManager* aManager, nsDisplayItem* aItem);
virtual ~WebRenderRemoteData();
UserDataType GetType() override { return UserDataType::eRemote; }
static UserDataType Type() { return UserDataType::eRemote; }
void SetRemoteBrowser(dom::RemoteBrowser* aBrowser) {
mRemoteBrowser = aBrowser;
}
protected:
RefPtr<dom::RemoteBrowser> mRemoteBrowser;
};
extern void DestroyWebRenderUserDataTable(WebRenderUserDataTable* aTable);
struct WebRenderUserDataProperty {
NS_DECLARE_FRAME_PROPERTY_WITH_DTOR(Key, WebRenderUserDataTable,
DestroyWebRenderUserDataTable)
};
template <class T>
already_AddRefed<T> GetWebRenderUserData(const nsIFrame* aFrame,
uint32_t aPerFrameKey) {
MOZ_ASSERT(aFrame);
WebRenderUserDataTable* userDataTable =
aFrame->GetProperty(WebRenderUserDataProperty::Key());
if (!userDataTable) {
return nullptr;
}
WebRenderUserData* data =
userDataTable->GetWeak(WebRenderUserDataKey(aPerFrameKey, T::Type()));
if (data) {
RefPtr<T> result = static_cast<T*>(data);
return result.forget();
}
return nullptr;
}
} // namespace layers
} // namespace mozilla
#endif /* GFX_WEBRENDERUSERDATA_H */
|