-
Notifications
You must be signed in to change notification settings - Fork 54
/
RenderThread.cpp
1072 lines (894 loc) · 32.4 KB
/
RenderThread.cpp
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- 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/. */
#include "base/task.h"
#include "GeckoProfiler.h"
#include "GLContext.h"
#include "RenderThread.h"
#include "nsThreadUtils.h"
#include "mtransport/runnable_utils.h"
#include "mozilla/layers/AsyncImagePipelineManager.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/gfx/GPUParent.h"
#include "mozilla/layers/CompositorThread.h"
#include "mozilla/layers/CompositorBridgeParent.h"
#include "mozilla/layers/CompositorManagerParent.h"
#include "mozilla/layers/WebRenderBridgeParent.h"
#include "mozilla/layers/SharedSurfacesParent.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Telemetry.h"
#include "mozilla/webrender/RendererOGL.h"
#include "mozilla/webrender/RenderTextureHost.h"
#include "mozilla/widget/CompositorWidget.h"
#ifdef XP_WIN
# include "GLLibraryEGL.h"
# include "mozilla/widget/WinCompositorWindowThread.h"
#endif
#ifdef MOZ_WIDGET_ANDROID
# include "GLLibraryEGL.h"
# include "GeneratedJNIWrappers.h"
#endif
#ifdef MOZ_WIDGET_GTK
# include <gdk/gdkx.h>
#endif
#ifdef MOZ_WAYLAND
# include "GLLibraryEGL.h"
#endif
using namespace mozilla;
static already_AddRefed<gl::GLContext> CreateGLContext();
MOZ_DEFINE_MALLOC_SIZE_OF(WebRenderRendererMallocSizeOf)
namespace mozilla {
namespace wr {
static StaticRefPtr<RenderThread> sRenderThread;
RenderThread::RenderThread(base::Thread* aThread)
: mThread(aThread),
mThreadPool(false),
mThreadPoolLP(true),
mWindowInfos("RenderThread.mWindowInfos"),
mRenderTextureMapLock("RenderThread.mRenderTextureMapLock"),
mHasShutdown(false),
mHandlingDeviceReset(false),
mHandlingWebRenderError(false) {}
RenderThread::~RenderThread() {
MOZ_ASSERT(mRenderTexturesDeferred.empty());
delete mThread;
}
// static
RenderThread* RenderThread::Get() { return sRenderThread; }
// static
void RenderThread::Start() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!sRenderThread);
base::Thread* thread = new base::Thread("Renderer");
base::Thread::Options options;
// TODO(nical): The compositor thread has a bunch of specific options, see
// which ones make sense here.
if (!thread->StartWithOptions(options)) {
delete thread;
return;
}
sRenderThread = new RenderThread(thread);
#ifdef XP_WIN
widget::WinCompositorWindowThread::Start();
#endif
layers::SharedSurfacesParent::Initialize();
RefPtr<Runnable> runnable = WrapRunnable(
RefPtr<RenderThread>(sRenderThread.get()), &RenderThread::InitDeviceTask);
sRenderThread->Loop()->PostTask(runnable.forget());
}
// static
void RenderThread::ShutDown() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(sRenderThread);
{
MutexAutoLock lock(sRenderThread->mRenderTextureMapLock);
sRenderThread->mHasShutdown = true;
}
layers::SynchronousTask task("RenderThread");
RefPtr<Runnable> runnable =
WrapRunnable(RefPtr<RenderThread>(sRenderThread.get()),
&RenderThread::ShutDownTask, &task);
sRenderThread->Loop()->PostTask(runnable.forget());
task.Wait();
sRenderThread = nullptr;
#ifdef XP_WIN
if (widget::WinCompositorWindowThread::Get()) {
widget::WinCompositorWindowThread::ShutDown();
}
#endif
}
extern void ClearAllBlobImageResources();
void RenderThread::ShutDownTask(layers::SynchronousTask* aTask) {
layers::AutoCompleteTask complete(aTask);
MOZ_ASSERT(IsInRenderThread());
// Let go of our handle to the (internally ref-counted) thread pool.
mThreadPool.Release();
mThreadPoolLP.Release();
// Releasing on the render thread will allow us to avoid dispatching to remove
// remaining textures from the texture map.
layers::SharedSurfacesParent::Shutdown();
ClearAllBlobImageResources();
ClearSharedGL();
ClearSharedSurfacePool();
}
// static
MessageLoop* RenderThread::Loop() {
return sRenderThread ? sRenderThread->mThread->message_loop() : nullptr;
}
// static
bool RenderThread::IsInRenderThread() {
return sRenderThread &&
sRenderThread->mThread->thread_id() == PlatformThread::CurrentId();
}
void RenderThread::DoAccumulateMemoryReport(
MemoryReport aReport,
const RefPtr<MemoryReportPromise::Private>& aPromise) {
MOZ_ASSERT(IsInRenderThread());
for (auto& r : mRenderers) {
r.second->AccumulateMemoryReport(&aReport);
}
// Note memory used by the shader cache, which is shared across all WR
// instances.
MOZ_ASSERT(aReport.shader_cache == 0);
if (mProgramCache) {
aReport.shader_cache = wr_program_cache_report_memory(
mProgramCache->Raw(), &WebRenderRendererMallocSizeOf);
}
aPromise->Resolve(aReport, __func__);
}
// static
RefPtr<MemoryReportPromise> RenderThread::AccumulateMemoryReport(
MemoryReport aInitial) {
RefPtr<MemoryReportPromise::Private> p =
new MemoryReportPromise::Private(__func__);
MOZ_ASSERT(!IsInRenderThread());
if (!Get() || !Get()->Loop()) {
// This happens when the GPU process fails to start and we fall back to the
// basic compositor in the parent process. We could assert against this if
// we made the webrender detection code in gfxPlatform.cpp smarter. See bug
// 1494430 comment 12.
NS_WARNING("No render thread, returning empty memory report");
p->Resolve(aInitial, __func__);
return p;
}
Get()->Loop()->PostTask(
NewRunnableMethod<MemoryReport, RefPtr<MemoryReportPromise::Private>>(
"wr::RenderThread::DoAccumulateMemoryReport", Get(),
&RenderThread::DoAccumulateMemoryReport, aInitial, p));
return p;
}
void RenderThread::AddRenderer(wr::WindowId aWindowId,
UniquePtr<RendererOGL> aRenderer) {
MOZ_ASSERT(IsInRenderThread());
if (mHasShutdown) {
return;
}
mRenderers[aWindowId] = std::move(aRenderer);
auto windows = mWindowInfos.Lock();
windows->emplace(AsUint64(aWindowId), new WindowInfo());
}
void RenderThread::RemoveRenderer(wr::WindowId aWindowId) {
MOZ_ASSERT(IsInRenderThread());
if (mHasShutdown) {
return;
}
mRenderers.erase(aWindowId);
mCompositionRecorders.erase(aWindowId);
if (mRenderers.size() == 0 && mHandlingDeviceReset) {
mHandlingDeviceReset = false;
}
auto windows = mWindowInfos.Lock();
auto it = windows->find(AsUint64(aWindowId));
MOZ_ASSERT(it != windows->end());
WindowInfo* toDelete = it->second;
windows->erase(it);
delete toDelete;
}
RendererOGL* RenderThread::GetRenderer(wr::WindowId aWindowId) {
MOZ_ASSERT(IsInRenderThread());
auto it = mRenderers.find(aWindowId);
MOZ_ASSERT(it != mRenderers.end());
if (it == mRenderers.end()) {
return nullptr;
}
return it->second.get();
}
size_t RenderThread::RendererCount() {
MOZ_ASSERT(IsInRenderThread());
return mRenderers.size();
}
void RenderThread::SetCompositionRecorderForWindow(
wr::WindowId aWindowId,
UniquePtr<layers::WebRenderCompositionRecorder> aCompositionRecorder) {
MOZ_ASSERT(IsInRenderThread());
MOZ_ASSERT(GetRenderer(aWindowId));
MOZ_ASSERT(mCompositionRecorders.find(aWindowId) ==
mCompositionRecorders.end());
mCompositionRecorders[aWindowId] = std::move(aCompositionRecorder);
}
void RenderThread::WriteCollectedFramesForWindow(wr::WindowId aWindowId) {
MOZ_ASSERT(IsInRenderThread());
RendererOGL* renderer = GetRenderer(aWindowId);
MOZ_ASSERT(renderer);
auto it = mCompositionRecorders.find(aWindowId);
MOZ_DIAGNOSTIC_ASSERT(
it != mCompositionRecorders.end(),
"Attempted to write frames from a window that was not recording.");
if (it != mCompositionRecorders.end()) {
it->second->WriteCollectedFrames();
if (renderer) {
wr_renderer_release_composition_recorder_structures(
renderer->GetRenderer());
}
mCompositionRecorders.erase(it);
}
}
Maybe<layers::CollectedFrames> RenderThread::GetCollectedFramesForWindow(
wr::WindowId aWindowId) {
MOZ_ASSERT(IsInRenderThread());
RendererOGL* renderer = GetRenderer(aWindowId);
MOZ_ASSERT(renderer);
auto it = mCompositionRecorders.find(aWindowId);
MOZ_DIAGNOSTIC_ASSERT(
it != mCompositionRecorders.end(),
"Attempted to get frames from a window that was not recording.");
Maybe<layers::CollectedFrames> maybeFrames;
if (it != mCompositionRecorders.end()) {
maybeFrames.emplace(it->second->GetCollectedFrames());
if (renderer) {
wr_renderer_release_composition_recorder_structures(
renderer->GetRenderer());
}
mCompositionRecorders.erase(it);
}
return maybeFrames;
}
void RenderThread::HandleFrameOneDoc(wr::WindowId aWindowId, bool aRender) {
if (mHasShutdown) {
return;
}
if (!IsInRenderThread()) {
Loop()->PostTask(NewRunnableMethod<wr::WindowId, bool>(
"wr::RenderThread::HandleFrameOneDoc", this,
&RenderThread::HandleFrameOneDoc, aWindowId, aRender));
return;
}
if (IsDestroyed(aWindowId)) {
return;
}
if (mHandlingDeviceReset) {
return;
}
bool render = false;
PendingFrameInfo frame;
{ // scope lock
auto windows = mWindowInfos.Lock();
auto it = windows->find(AsUint64(aWindowId));
if (it == windows->end()) {
MOZ_ASSERT(false);
return;
}
WindowInfo* info = it->second;
PendingFrameInfo& frameInfo = info->mPendingFrames.front();
frameInfo.mDocFramesSeen++;
frameInfo.mFrameNeedsRender |= aRender;
if (frameInfo.mDocFramesSeen < frameInfo.mDocFramesTotal) {
return;
}
MOZ_ASSERT(frameInfo.mDocFramesSeen == frameInfo.mDocFramesTotal);
render = frameInfo.mFrameNeedsRender;
frame = frameInfo;
}
// It is for ensuring that PrepareForUse() is called before
// RenderTextureHost::Lock().
HandlePrepareForUse();
UpdateAndRender(aWindowId, frame.mStartId, frame.mStartTime, render,
/* aReadbackSize */ Nothing(),
/* aReadbackFormat */ Nothing(),
/* aReadbackBuffer */ Nothing());
{ // scope lock
auto windows = mWindowInfos.Lock();
auto it = windows->find(AsUint64(aWindowId));
if (it == windows->end()) {
MOZ_ASSERT(false);
return;
}
WindowInfo* info = it->second;
info->mPendingFrames.pop();
}
// The start time is from WebRenderBridgeParent::CompositeToTarget. From that
// point until now (when the frame is finally pushed to the screen) is
// equivalent to the COMPOSITE_TIME metric in the non-WR codepath.
mozilla::Telemetry::AccumulateTimeDelta(mozilla::Telemetry::COMPOSITE_TIME,
frame.mStartTime);
}
void RenderThread::WakeUp(wr::WindowId aWindowId) {
if (mHasShutdown) {
return;
}
if (!IsInRenderThread()) {
Loop()->PostTask(NewRunnableMethod<wr::WindowId>(
"wr::RenderThread::WakeUp", this, &RenderThread::WakeUp, aWindowId));
return;
}
if (IsDestroyed(aWindowId)) {
return;
}
if (mHandlingDeviceReset) {
return;
}
auto it = mRenderers.find(aWindowId);
MOZ_ASSERT(it != mRenderers.end());
if (it != mRenderers.end()) {
it->second->Update();
}
}
void RenderThread::RunEvent(wr::WindowId aWindowId,
UniquePtr<RendererEvent> aEvent) {
if (!IsInRenderThread()) {
Loop()->PostTask(
NewRunnableMethod<wr::WindowId, UniquePtr<RendererEvent>&&>(
"wr::RenderThread::RunEvent", this, &RenderThread::RunEvent,
aWindowId, std::move(aEvent)));
return;
}
aEvent->Run(*this, aWindowId);
aEvent = nullptr;
}
static void NotifyDidRender(layers::CompositorBridgeParent* aBridge,
RefPtr<const WebRenderPipelineInfo> aInfo,
VsyncId aCompositeStartId,
TimeStamp aCompositeStart, TimeStamp aRenderStart,
TimeStamp aEnd, bool aRender,
RendererStats aStats) {
if (aRender && aBridge->GetWrBridge()) {
// We call this here to mimic the behavior in LayerManagerComposite, as to
// not change what Talos measures. That is, we do not record an empty frame
// as a frame.
aBridge->GetWrBridge()->RecordFrame();
}
auto info = aInfo->Raw();
for (const auto& epoch : info.epochs) {
aBridge->NotifyPipelineRendered(epoch.pipeline_id, epoch.epoch,
aCompositeStartId, aCompositeStart,
aRenderStart, aEnd, &aStats);
}
if (aBridge->GetWrBridge()) {
aBridge->GetWrBridge()->CompositeIfNeeded();
}
}
static void NotifyDidStartRender(layers::CompositorBridgeParent* aBridge) {
// Starting a render will change mIsRendering, and potentially
// change whether we can allow the bridge to intiate another frame.
if (aBridge->GetWrBridge()) {
aBridge->GetWrBridge()->CompositeIfNeeded();
}
}
void RenderThread::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) {
AUTO_PROFILER_TRACING_MARKER("Paint", "Composite", GRAPHICS);
MOZ_ASSERT(IsInRenderThread());
MOZ_ASSERT(aRender || aReadbackBuffer.isNothing());
auto it = mRenderers.find(aWindowId);
MOZ_ASSERT(it != mRenderers.end());
if (it == mRenderers.end()) {
return;
}
TimeStamp start = TimeStamp::Now();
auto& renderer = it->second;
layers::CompositorThreadHolder::Loop()->PostTask(
NewRunnableFunction("NotifyDidStartRenderRunnable", &NotifyDidStartRender,
renderer->GetCompositorBridge()));
wr::RenderedFrameId latestFrameId;
RendererStats stats = {0};
if (aRender) {
latestFrameId = renderer->UpdateAndRender(aReadbackSize, aReadbackFormat,
aReadbackBuffer, &stats);
} else {
renderer->Update();
}
// Check graphics reset status even when rendering is skipped.
renderer->CheckGraphicsResetStatus();
TimeStamp end = TimeStamp::Now();
RefPtr<const WebRenderPipelineInfo> info = renderer->FlushPipelineInfo();
layers::CompositorThreadHolder::Loop()->PostTask(
NewRunnableFunction("NotifyDidRenderRunnable", &NotifyDidRender,
renderer->GetCompositorBridge(), info, aStartId,
aStartTime, start, end, aRender, stats));
if (latestFrameId.IsValid()) {
auto recorderIt = mCompositionRecorders.find(aWindowId);
if (recorderIt != mCompositionRecorders.end() &&
renderer->EnsureAsyncScreenshot()) {
recorderIt->second->MaybeRecordFrame(renderer->GetRenderer(), info.get());
}
}
if (latestFrameId.IsValid()) {
// Wait for GPU after posting NotifyDidRender, since the wait is not
// necessary for the NotifyDidRender.
// The wait is necessary for Textures recycling of AsyncImagePipelineManager
// and for avoiding GPU queue is filled with too much tasks.
// WaitForGPU's implementation is different for each platform.
renderer->WaitForGPU();
}
if (!aRender) {
// Update frame id for NotifyPipelinesUpdated() when rendering does not
// happen.
latestFrameId = renderer->UpdateFrameId();
}
RenderedFrameId lastCompletedFrameId = renderer->GetLastCompletedFrameId();
RefPtr<layers::AsyncImagePipelineManager> pipelineMgr =
renderer->GetCompositorBridge()->GetAsyncImagePipelineManager();
// pipelineMgr should always be non-null here because it is only nulled out
// after the WebRenderAPI instance for the CompositorBridgeParent is
// destroyed, and that destruction blocks until the renderer thread has
// removed the relevant renderer. And after that happens we should never reach
// this code at all; it would bail out at the mRenderers.find check above.
MOZ_ASSERT(pipelineMgr);
pipelineMgr->NotifyPipelinesUpdated(info, latestFrameId,
lastCompletedFrameId);
}
void RenderThread::Pause(wr::WindowId aWindowId) {
MOZ_ASSERT(IsInRenderThread());
auto it = mRenderers.find(aWindowId);
MOZ_ASSERT(it != mRenderers.end());
if (it == mRenderers.end()) {
return;
}
auto& renderer = it->second;
renderer->Pause();
}
bool RenderThread::Resume(wr::WindowId aWindowId) {
MOZ_ASSERT(IsInRenderThread());
auto it = mRenderers.find(aWindowId);
MOZ_ASSERT(it != mRenderers.end());
if (it == mRenderers.end()) {
return false;
}
auto& renderer = it->second;
return renderer->Resume();
}
bool RenderThread::TooManyPendingFrames(wr::WindowId aWindowId) {
const int64_t maxFrameCount = 1;
// Too many pending frames if pending frames exit more than maxFrameCount
// or if RenderBackend is still processing a frame.
auto windows = mWindowInfos.Lock();
auto it = windows->find(AsUint64(aWindowId));
if (it == windows->end()) {
MOZ_ASSERT(false);
return true;
}
WindowInfo* info = it->second;
if (info->PendingCount() > maxFrameCount) {
return true;
}
// If there is no ongoing frame build, we accept a new frame.
return info->mPendingFrameBuild > 0;
}
bool RenderThread::IsDestroyed(wr::WindowId aWindowId) {
auto windows = mWindowInfos.Lock();
auto it = windows->find(AsUint64(aWindowId));
if (it == windows->end()) {
return true;
}
return it->second->mIsDestroyed;
}
void RenderThread::SetDestroyed(wr::WindowId aWindowId) {
auto windows = mWindowInfos.Lock();
auto it = windows->find(AsUint64(aWindowId));
if (it == windows->end()) {
MOZ_ASSERT(false);
return;
}
it->second->mIsDestroyed = true;
}
void RenderThread::IncPendingFrameCount(wr::WindowId aWindowId,
const VsyncId& aStartId,
const TimeStamp& aStartTime,
uint8_t aDocFrameCount) {
auto windows = mWindowInfos.Lock();
auto it = windows->find(AsUint64(aWindowId));
if (it == windows->end()) {
MOZ_ASSERT(false);
return;
}
it->second->mPendingFrameBuild += aDocFrameCount;
it->second->mPendingFrames.push(
PendingFrameInfo{aStartTime, aStartId, 0, aDocFrameCount, false});
}
void RenderThread::DecPendingFrameBuildCount(wr::WindowId aWindowId) {
auto windows = mWindowInfos.Lock();
auto it = windows->find(AsUint64(aWindowId));
if (it == windows->end()) {
MOZ_ASSERT(false);
return;
}
WindowInfo* info = it->second;
MOZ_RELEASE_ASSERT(info->mPendingFrameBuild >= 1);
info->mPendingFrameBuild--;
}
void RenderThread::RegisterExternalImage(
uint64_t aExternalImageId, already_AddRefed<RenderTextureHost> aTexture) {
MutexAutoLock lock(mRenderTextureMapLock);
if (mHasShutdown) {
return;
}
MOZ_ASSERT(mRenderTextures.find(aExternalImageId) == mRenderTextures.end());
mRenderTextures.emplace(aExternalImageId, std::move(aTexture));
}
void RenderThread::UnregisterExternalImage(uint64_t aExternalImageId) {
MutexAutoLock lock(mRenderTextureMapLock);
if (mHasShutdown) {
return;
}
auto it = mRenderTextures.find(aExternalImageId);
MOZ_ASSERT(it != mRenderTextures.end());
if (it == mRenderTextures.end()) {
return;
}
if (!IsInRenderThread()) {
// The RenderTextureHost should be released in render thread. So, post the
// deletion task here.
// The shmem and raw buffer are owned by compositor ipc channel. It's
// possible that RenderTextureHost is still exist after the shmem/raw buffer
// deletion. Then the buffer in RenderTextureHost becomes invalid. It's fine
// for this situation. Gecko will only release the buffer if WR doesn't need
// it. So, no one will access the invalid buffer in RenderTextureHost.
RefPtr<RenderTextureHost> texture = it->second;
mRenderTextures.erase(it);
mRenderTexturesDeferred.emplace_back(std::move(texture));
Loop()->PostTask(NewRunnableMethod(
"RenderThread::DeferredRenderTextureHostDestroy", this,
&RenderThread::DeferredRenderTextureHostDestroy));
} else {
mRenderTextures.erase(it);
}
}
void RenderThread::PrepareForUse(uint64_t aExternalImageId) {
MOZ_ASSERT(!IsInRenderThread());
MutexAutoLock lock(mRenderTextureMapLock);
if (mHasShutdown) {
return;
}
auto it = mRenderTextures.find(aExternalImageId);
MOZ_ASSERT(it != mRenderTextures.end());
if (it == mRenderTextures.end()) {
return;
}
RefPtr<RenderTextureHost> texture = it->second;
mRenderTexturesPrepareForUse.emplace_back(std::move(texture));
Loop()->PostTask(NewRunnableMethod("RenderThread::HandlePrepareForUse", this,
&RenderThread::HandlePrepareForUse));
}
void RenderThread::NotifyNotUsed(uint64_t aExternalImageId) {
MOZ_ASSERT(!IsInRenderThread());
MutexAutoLock lock(mRenderTextureMapLock);
if (mHasShutdown) {
return;
}
auto it = mRenderTextures.find(aExternalImageId);
MOZ_ASSERT(it != mRenderTextures.end());
if (it == mRenderTextures.end()) {
return;
}
RefPtr<RenderTextureHost> texture = it->second;
RefPtr<Runnable> task =
NS_NewRunnableFunction("RenderThread::DoNotifyNotUsed",
[renderTexture = std::move(texture)]() -> void {
renderTexture->NotifyNotUsed();
});
Loop()->PostTask(task.forget());
}
void RenderThread::UnregisterExternalImageDuringShutdown(
uint64_t aExternalImageId) {
MOZ_ASSERT(IsInRenderThread());
MutexAutoLock lock(mRenderTextureMapLock);
MOZ_ASSERT(mHasShutdown);
MOZ_ASSERT(mRenderTextures.find(aExternalImageId) != mRenderTextures.end());
mRenderTextures.erase(aExternalImageId);
}
void RenderThread::HandlePrepareForUse() {
MOZ_ASSERT(IsInRenderThread());
MutexAutoLock lock(mRenderTextureMapLock);
for (auto& texture : mRenderTexturesPrepareForUse) {
texture->PrepareForUse();
}
mRenderTexturesPrepareForUse.clear();
}
void RenderThread::DeferredRenderTextureHostDestroy() {
MutexAutoLock lock(mRenderTextureMapLock);
mRenderTexturesDeferred.clear();
}
RenderTextureHost* RenderThread::GetRenderTexture(
wr::ExternalImageId aExternalImageId) {
MOZ_ASSERT(IsInRenderThread());
MutexAutoLock lock(mRenderTextureMapLock);
auto it = mRenderTextures.find(AsUint64(aExternalImageId));
MOZ_ASSERT(it != mRenderTextures.end());
if (it == mRenderTextures.end()) {
return nullptr;
}
return it->second;
}
void RenderThread::InitDeviceTask() {
MOZ_ASSERT(IsInRenderThread());
MOZ_ASSERT(!mSharedGL);
mSharedGL = CreateGLContext();
if (gfx::gfxVars::UseWebRenderProgramBinaryDisk() &&
!gfx::gfxVars::UseSoftwareWebRender()) {
mProgramCache = MakeUnique<WebRenderProgramCache>(ThreadPool().Raw());
}
// Query the shared GL context to force the
// lazy initialization to happen now.
SharedGL();
}
void RenderThread::HandleDeviceReset(const char* aWhere, bool aNotify) {
MOZ_ASSERT(IsInRenderThread());
if (mHandlingDeviceReset) {
return;
}
if (aNotify) {
gfxCriticalNote << "GFX: RenderThread detected a device reset in "
<< aWhere;
if (XRE_IsGPUProcess()) {
gfx::GPUParent::GetSingleton()->NotifyDeviceReset();
}
}
{
MutexAutoLock lock(mRenderTextureMapLock);
mRenderTexturesDeferred.clear();
for (const auto& entry : mRenderTextures) {
entry.second->ClearCachedResources();
}
}
mHandlingDeviceReset = true;
// All RenderCompositors will be destroyed by
// GPUChild::RecvNotifyDeviceReset()
}
bool RenderThread::IsHandlingDeviceReset() {
MOZ_ASSERT(IsInRenderThread());
return mHandlingDeviceReset;
}
void RenderThread::SimulateDeviceReset() {
if (!IsInRenderThread()) {
Loop()->PostTask(NewRunnableMethod("RenderThread::SimulateDeviceReset",
this,
&RenderThread::SimulateDeviceReset));
} else {
// When this function is called GPUProcessManager::SimulateDeviceReset()
// already triggers destroying all CompositorSessions before re-creating
// them.
HandleDeviceReset("SimulateDeviceReset", /* aNotify */ false);
}
}
static void DoNotifyWebRenderError(WebRenderError aError) {
layers::CompositorManagerParent::NotifyWebRenderError(aError);
}
void RenderThread::HandleWebRenderError(WebRenderError aError) {
if (mHandlingWebRenderError) {
return;
}
layers::CompositorThreadHolder::Loop()->PostTask(NewRunnableFunction(
"DoNotifyWebRenderErrorRunnable", &DoNotifyWebRenderError, aError));
{
MutexAutoLock lock(mRenderTextureMapLock);
mRenderTexturesDeferred.clear();
for (const auto& entry : mRenderTextures) {
entry.second->ClearCachedResources();
}
}
mHandlingWebRenderError = true;
// WebRender is going to be disabled by
// GPUProcessManager::NotifyWebRenderError()
}
bool RenderThread::IsHandlingWebRenderError() {
MOZ_ASSERT(IsInRenderThread());
return mHandlingWebRenderError;
}
gl::GLContext* RenderThread::SharedGL() {
MOZ_ASSERT(IsInRenderThread());
if (!mSharedGL) {
mSharedGL = CreateGLContext();
mShaders = nullptr;
}
if (mSharedGL && !mShaders && !gfx::gfxVars::UseSoftwareWebRender()) {
mShaders = MakeUnique<WebRenderShaders>(mSharedGL, mProgramCache.get());
}
return mSharedGL.get();
}
void RenderThread::ClearSharedGL() {
MOZ_ASSERT(IsInRenderThread());
if (mSurfacePool) {
mSurfacePool->DestroyGLResourcesForContext(mSharedGL);
}
mShaders = nullptr;
mSharedGL = nullptr;
}
RefPtr<layers::SurfacePool> RenderThread::SharedSurfacePool() {
#ifdef XP_MACOSX
if (!mSurfacePool) {
size_t poolSizeLimit =
StaticPrefs::gfx_webrender_compositor_surface_pool_size_AtStartup();
mSurfacePool = layers::SurfacePool::Create(poolSizeLimit);
}
#endif
return mSurfacePool;
}
void RenderThread::ClearSharedSurfacePool() { mSurfacePool = nullptr; }
WebRenderShaders::WebRenderShaders(gl::GLContext* gl,
WebRenderProgramCache* programCache) {
mGL = gl;
mShaders = wr_shaders_new(gl, programCache ? programCache->Raw() : nullptr);
}
WebRenderShaders::~WebRenderShaders() {
wr_shaders_delete(mShaders, mGL.get());
}
WebRenderThreadPool::WebRenderThreadPool(bool low_priority) {
mThreadPool = wr_thread_pool_new(low_priority);
}
WebRenderThreadPool::~WebRenderThreadPool() { Release(); }
void WebRenderThreadPool::Release() {
if (mThreadPool) {
wr_thread_pool_delete(mThreadPool);
mThreadPool = nullptr;
}
}
WebRenderProgramCache::WebRenderProgramCache(wr::WrThreadPool* aThreadPool) {
MOZ_ASSERT(aThreadPool);
nsAutoString path;
if (gfx::gfxVars::UseWebRenderProgramBinaryDisk()) {
path.Append(gfx::gfxVars::ProfDirectory());
}
mProgramCache = wr_program_cache_new(&path, aThreadPool);
if (gfx::gfxVars::UseWebRenderProgramBinaryDisk()) {
wr_try_load_startup_shaders_from_disk(mProgramCache);
}
}
WebRenderProgramCache::~WebRenderProgramCache() {
wr_program_cache_delete(mProgramCache);
}
} // namespace wr
} // namespace mozilla
#ifdef XP_WIN
static already_AddRefed<gl::GLContext> CreateGLContextANGLE() {
nsCString discardFailureId;
if (!gl::GLLibraryEGL::EnsureInitialized(/* forceAccel */ true,
&discardFailureId)) {
gfxCriticalNote << "Failed to load EGL library: " << discardFailureId.get();
return nullptr;
}
auto* egl = gl::GLLibraryEGL::Get();
gl::CreateContextFlags flags = gl::CreateContextFlags::PREFER_ES3 |
gl::CreateContextFlags::PREFER_ROBUSTNESS;
if (egl->IsExtensionSupported(
gl::GLLibraryEGL::MOZ_create_context_provoking_vertex_dont_care)) {
flags |= gl::CreateContextFlags::PROVOKING_VERTEX_DONT_CARE;
}
// Create GLContext with dummy EGLSurface, the EGLSurface is not used.
// Instread we override it with EGLSurface of SwapChain's back buffer.
RefPtr<gl::GLContext> gl =
gl::GLContextProviderEGL::CreateHeadless(flags, &discardFailureId);
if (!gl || !gl->IsANGLE()) {
gfxCriticalNote << "Failed ANGLE GL context creation for WebRender: "
<< gfx::hexa(gl.get());
return nullptr;
}
if (!gl->MakeCurrent()) {
gfxCriticalNote << "Failed GL context creation for WebRender: "
<< gfx::hexa(gl.get());
return nullptr;
}
return gl.forget();
}
#endif
#if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WAYLAND)
static already_AddRefed<gl::GLContext> CreateGLContextEGL() {
nsCString discardFailureId;
if (!gl::GLLibraryEGL::EnsureInitialized(/* forceAccel */ true,
&discardFailureId)) {
gfxCriticalNote << "Failed to load EGL library: " << discardFailureId.get();
return nullptr;
}
// Create GLContext with dummy EGLSurface.
RefPtr<gl::GLContext> gl =
gl::GLContextProviderEGL::CreateForCompositorWidget(
nullptr, /* aWebRender */ true, /* aForceAccelerated */ true);
if (!gl || !gl->MakeCurrent()) {
gfxCriticalNote << "Failed GL context creation for WebRender: "
<< gfx::hexa(gl.get());
return nullptr;
}
return gl.forget();
}
#endif
#ifdef XP_MACOSX
static already_AddRefed<gl::GLContext> CreateGLContextCGL() {
nsCString failureUnused;
return gl::GLContextProvider::CreateHeadless(
gl::CreateContextFlags::ALLOW_OFFLINE_RENDERER |
gl::CreateContextFlags::FORCE_ENABLE_HARDWARE,
&failureUnused);
}
#endif
static already_AddRefed<gl::GLContext> CreateGLContext() {
#ifdef XP_WIN
if (gfx::gfxVars::UseWebRenderANGLE()) {
return CreateGLContextANGLE();
}
#endif
#if defined(MOZ_WIDGET_ANDROID)
return CreateGLContextEGL();
#elif defined(MOZ_WAYLAND)
if (gdk_display_get_default() &&
!GDK_IS_X11_DISPLAY(gdk_display_get_default())) {
return CreateGLContextEGL();
}
#endif
#ifdef XP_MACOSX