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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* -*- 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_LAYERS_RENDERTHREAD_H
#define MOZILLA_LAYERS_RENDERTHREAD_H
#include "base/basictypes.h" // for DISALLOW_EVIL_CONSTRUCTORS
#include "base/platform_thread.h" // for PlatformThreadId
#include "base/thread.h" // for Thread
#include "base/message_loop.h"
#include "GLTypes.h" // for GLenum
#include "nsISupportsImpl.h"
#include "mozilla/gfx/Point.h"
#include "mozilla/MozPromise.h"
#include "mozilla/DataMutex.h"
#include "mozilla/Maybe.h"
#include "mozilla/webrender/webrender_ffi.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/webrender/WebRenderTypes.h"
#include "mozilla/layers/CompositionRecorder.h"
#include "mozilla/layers/SynchronousTask.h"
#include "mozilla/VsyncDispatcher.h"
#include <list>
#include <queue>
#include <unordered_map>
namespace mozilla {
namespace gl {
class GLContext;
} // namespace gl
namespace layers {
class CompositorBridgeParent;
class SurfacePool;
} // namespace layers
namespace wr {
typedef MozPromise<MemoryReport, bool, true> MemoryReportPromise;
class RendererOGL;
class RenderTextureHost;
class RenderThread;
/// A rayon thread pool that is shared by all WebRender instances within a
/// process.
class WebRenderThreadPool {
public:
explicit WebRenderThreadPool(bool low_priority);
~WebRenderThreadPool();
wr::WrThreadPool* Raw() {
// If this pointer is null we are likely at some late shutdown stage,
// when threads are no longer safe to interact with.
MOZ_RELEASE_ASSERT(mThreadPool);
return mThreadPool;
}
/// Prematurely destroys this handle to the thread pool.
/// After calling this the object is useless.
void Release();
protected:
wr::WrThreadPool* mThreadPool;
};
class WebRenderProgramCache final {
public:
explicit WebRenderProgramCache(wr::WrThreadPool* aThreadPool);
~WebRenderProgramCache();
wr::WrProgramCache* Raw() { return mProgramCache; }
protected:
wr::WrProgramCache* mProgramCache;
};
class WebRenderShaders final {
public:
WebRenderShaders(gl::GLContext* gl, WebRenderProgramCache* programCache);
~WebRenderShaders();
wr::WrShaders* RawShaders() { return mShaders; }
protected:
RefPtr<gl::GLContext> mGL;
wr::WrShaders* mShaders;
};
class WebRenderPipelineInfo final {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WebRenderPipelineInfo);
const wr::WrPipelineInfo& Raw() const { return mPipelineInfo; }
wr::WrPipelineInfo& Raw() { return mPipelineInfo; }
protected:
~WebRenderPipelineInfo() = default;
wr::WrPipelineInfo mPipelineInfo;
};
/// Base class for an event that can be scheduled to run on the render thread.
///
/// The event can be passed through the same channels as regular WebRender
/// messages to preserve ordering.
class RendererEvent {
public:
virtual ~RendererEvent() = default;
virtual void Run(RenderThread& aRenderThread, wr::WindowId aWindow) = 0;
};
/// The render thread is where WebRender issues all of its GPU work, and as much
/// as possible this thread should only serve this purpose.
///
/// The render thread owns the different RendererOGLs (one per window) and
/// implements the RenderNotifier api exposed by the WebRender bindings.
///
/// We should generally avoid posting tasks to the render thread's event loop
/// directly and instead use the RendererEvent mechanism which avoids races
/// between the events and WebRender's own messages.
///
/// The GL context(s) should be created and used on this thread only.
/// XXX - I've tried to organize code so that we can potentially avoid making
/// this a singleton since this bad habit has a tendency to bite us later, but
/// I haven't gotten all the way there either, in order to focus on the more
/// important pieces first. So we are a bit in-between (this is totally a
/// singleton but in some places we pretend it's not). Hopefully we can evolve
/// this in a way that keeps the door open to removing the singleton bits.
class RenderThread final {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_MAIN_THREAD(RenderThread)
public:
/// Can be called from any thread.
static RenderThread* Get();
/// Can only be called from the main thread.
static void Start();
/// Can only be called from the main thread.
static void ShutDown();
/// Can be called from any thread.
/// In most cases it is best to post RendererEvents through WebRenderAPI
/// instead of scheduling directly to this message loop (so as to preserve the
/// ordering of the messages).
static MessageLoop* Loop();
/// Can be called from any thread.
static bool IsInRenderThread();
// Can be called from any thread. Dispatches an event to the Renderer thread
// to iterate over all Renderers, accumulates memory statistics, and resolves
// the return promise.
static RefPtr<MemoryReportPromise> AccumulateMemoryReport(
MemoryReport aInitial);
/// Can only be called from the render thread.
void AddRenderer(wr::WindowId aWindowId, UniquePtr<RendererOGL> aRenderer);
/// Can only be called from the render thread.
void RemoveRenderer(wr::WindowId aWindowId);
/// Can only be called from the render thread.
RendererOGL* GetRenderer(wr::WindowId aWindowId);
// RenderNotifier implementation
/// Automatically forwarded to the render thread. Will trigger a render for
/// the current pending frame once one call per document in that pending
/// frame has been received.
void HandleFrameOneDoc(wr::WindowId aWindowId, bool aRender);
/// Automatically forwarded to the render thread.
void SetClearColor(wr::WindowId aWindowId, wr::ColorF aColor);
/// Automatically forwarded to the render thread.
void SetProfilerUI(wr::WindowId aWindowId, nsCString aUI);
/// Automatically forwarded to the render thread.
void PipelineSizeChanged(wr::WindowId aWindowId, uint64_t aPipelineId,
float aWidth, float aHeight);
/// Automatically forwarded to the render thread.
void RunEvent(wr::WindowId aWindowId, UniquePtr<RendererEvent> aCallBack);
/// Can only be called from the render thread.
void UpdateAndRender(wr::WindowId aWindowId, const VsyncId& aStartId,
const TimeStamp& aStartTime, bool aRender,
const Maybe<gfx::IntSize>& aReadbackSize,
const Maybe<wr::ImageFormat>& aReadbackFormat,
const Maybe<Range<uint8_t>>& aReadbackBuffer,
bool* aNeedsYFlip = nullptr);
void Pause(wr::WindowId aWindowId);
bool Resume(wr::WindowId aWindowId);
/// Can be called from any thread.
void RegisterExternalImage(uint64_t aExternalImageId,
already_AddRefed<RenderTextureHost> aTexture);
/// Can be called from any thread.
void UnregisterExternalImage(uint64_t aExternalImageId);
/// Can be called from any thread.
void PrepareForUse(uint64_t aExternalImageId);
/// Can be called from any thread.
void NotifyNotUsed(uint64_t aExternalImageId);
/// Can be called from any thread.
void NotifyForUse(uint64_t aExternalImageId);
void HandleRenderTextureOps();
/// Can only be called from the render thread.
void UnregisterExternalImageDuringShutdown(uint64_t aExternalImageId);
/// Can only be called from the render thread.
RenderTextureHost* GetRenderTexture(ExternalImageId aExternalImageId);
/// Can be called from any thread.
bool IsDestroyed(wr::WindowId aWindowId);
/// Can be called from any thread.
void SetDestroyed(wr::WindowId aWindowId);
/// Can be called from any thread.
bool TooManyPendingFrames(wr::WindowId aWindowId);
/// Can be called from any thread.
void IncPendingFrameCount(wr::WindowId aWindowId, const VsyncId& aStartId,
const TimeStamp& aStartTime);
/// Can be called from any thread.
void DecPendingFrameBuildCount(wr::WindowId aWindowId);
/// Can be called from any thread.
WebRenderThreadPool& ThreadPool() { return mThreadPool; }
/// Thread pool for low priority scene building
/// Can be called from any thread.
WebRenderThreadPool& ThreadPoolLP() { return mThreadPoolLP; }
/// Returns the cache used to serialize shader programs to disk, if enabled.
///
/// Can only be called from the render thread.
WebRenderProgramCache* GetProgramCache() {
MOZ_ASSERT(IsInRenderThread());
return mProgramCache.get();
}
/// Can only be called from the render thread.
WebRenderShaders* GetShaders() {
MOZ_ASSERT(IsInRenderThread());
return mShaders.get();
}
/// Can only be called from the render thread.
gl::GLContext* SharedGL(nsACString& aError);
gl::GLContext* SharedGL();
void ClearSharedGL();
RefPtr<layers::SurfacePool> SharedSurfacePool();
void ClearSharedSurfacePool();
/// Can only be called from the render thread.
void HandleDeviceReset(const char* aWhere,
layers::CompositorBridgeParent* aBridge,
GLenum aReason);
/// Can only be called from the render thread.
bool IsHandlingDeviceReset();
/// Can be called from any thread.
void SimulateDeviceReset();
/// Can only be called from the render thread.
void NotifyWebRenderError(WebRenderError aError);
/// Can only be called from the render thread.
void HandleWebRenderError(WebRenderError aError);
/// Can only be called from the render thread.
bool IsHandlingWebRenderError();
/// Can only be called from the render thread.
bool SyncObjectNeeded();
size_t RendererCount();
void BeginRecordingForWindow(wr::WindowId aWindowId,
const TimeStamp& aRecordingStart,
wr::PipelineId aRootPipelineId);
void WriteCollectedFramesForWindow(wr::WindowId aWindowId);
Maybe<layers::CollectedFrames> GetCollectedFramesForWindow(
wr::WindowId aWindowId);
static void MaybeEnableGLDebugMessage(gl::GLContext* aGLContext);
private:
enum class RenderTextureOp {
PrepareForUse,
NotifyForUse,
NotifyNotUsed,
};
explicit RenderThread(base::Thread* aThread);
void DeferredRenderTextureHostDestroy();
void ShutDownTask(layers::SynchronousTask* aTask);
void InitDeviceTask();
void DoAccumulateMemoryReport(MemoryReport,
const RefPtr<MemoryReportPromise::Private>&);
void AddRenderTextureOp(RenderTextureOp aOp, uint64_t aExternalImageId);
~RenderThread();
base::Thread* const mThread;
WebRenderThreadPool mThreadPool;
WebRenderThreadPool mThreadPoolLP;
UniquePtr<WebRenderProgramCache> mProgramCache;
UniquePtr<WebRenderShaders> mShaders;
// An optional shared GLContext to be used for all
// windows.
RefPtr<gl::GLContext> mSharedGL;
RefPtr<layers::SurfacePool> mSurfacePool;
std::map<wr::WindowId, UniquePtr<RendererOGL>> mRenderers;
struct PendingFrameInfo {
TimeStamp mStartTime;
VsyncId mStartId;
bool mFrameNeedsRender = false;
};
struct WindowInfo {
int64_t PendingCount() { return mPendingFrames.size(); }
// If mIsRendering is true, mPendingFrames.front() is currently being
// rendered.
std::queue<PendingFrameInfo> mPendingFrames;
uint8_t mPendingFrameBuild = 0;
bool mIsDestroyed = false;
};
DataMutex<std::unordered_map<uint64_t, WindowInfo*>> mWindowInfos;
Mutex mRenderTextureMapLock;
std::unordered_map<uint64_t, RefPtr<RenderTextureHost>> mRenderTextures;
std::unordered_map<uint64_t, RefPtr<RenderTextureHost>>
mSyncObjectNeededRenderTextures;
std::list<std::pair<RenderTextureOp, RefPtr<RenderTextureHost>>>
mRenderTextureOps;
// Used to remove all RenderTextureHost that are going to be removed by
// a deferred callback and remove them right away without waiting for the
// callback. On device reset we have to remove all GL related resources right
// away.
std::list<RefPtr<RenderTextureHost>> mRenderTexturesDeferred;
bool mHasShutdown;
bool mHandlingDeviceReset;
bool mHandlingWebRenderError;
};
} // namespace wr
} // namespace mozilla
#endif
|