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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
|
/* -*- 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 "mozilla/layers/CompositorBridgeChild.h"
#include "mozilla/layers/CompositorBridgeParent.h"
#include "mozilla/layers/CompositorThread.h"
#include <stddef.h> // for size_t
#include "ClientLayerManager.h" // for ClientLayerManager
#include "base/task.h" // for NewRunnableMethod, etc
#include "mozilla/StaticPrefs_layers.h"
#include "mozilla/layers/CompositorManagerChild.h"
#include "mozilla/layers/ImageBridgeChild.h"
#include "mozilla/layers/APZChild.h"
#include "mozilla/layers/IAPZCTreeManager.h"
#include "mozilla/layers/APZCTreeManagerChild.h"
#include "mozilla/layers/CanvasChild.h"
#include "mozilla/layers/LayerTransactionChild.h"
#include "mozilla/layers/PaintThread.h"
#include "mozilla/layers/PLayerTransactionChild.h"
#include "mozilla/layers/PTextureChild.h"
#include "mozilla/layers/TextureClient.h" // for TextureClient
#include "mozilla/layers/TextureClientPool.h" // for TextureClientPool
#include "mozilla/layers/WebRenderBridgeChild.h"
#include "mozilla/layers/SyncObject.h" // for SyncObjectClient
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/gfx/GPUProcessManager.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/ipc/Endpoint.h"
#include "mozilla/webgpu/WebGPUChild.h"
#include "mozilla/mozalloc.h" // for operator new, etc
#include "mozilla/Telemetry.h"
#include "gfxConfig.h"
#include "nsDebug.h" // for NS_WARNING
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
#include "nsTArray.h" // for nsTArray, nsTArray_Impl
#include "FrameLayerBuilder.h"
#include "mozilla/dom/BrowserChild.h"
#include "mozilla/dom/BrowserParent.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/Unused.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsThreadUtils.h"
#if defined(XP_WIN)
# include "WinUtils.h"
#endif
#include "mozilla/widget/CompositorWidget.h"
#ifdef MOZ_WIDGET_SUPPORTS_OOP_COMPOSITING
# include "mozilla/widget/CompositorWidgetChild.h"
#endif
#include "VsyncSource.h"
#ifdef MOZ_WIDGET_ANDROID
# include "mozilla/layers/AndroidHardwareBuffer.h"
#endif
using mozilla::Unused;
using mozilla::gfx::GPUProcessManager;
using mozilla::layers::LayerTransactionChild;
namespace mozilla {
namespace layers {
static int sShmemCreationCounter = 0;
static void ResetShmemCounter() { sShmemCreationCounter = 0; }
static void ShmemAllocated(CompositorBridgeChild* aProtocol) {
sShmemCreationCounter++;
if (sShmemCreationCounter > 256) {
aProtocol->SendSyncWithCompositor();
ResetShmemCounter();
MOZ_PERFORMANCE_WARNING(
"gfx", "The number of shmem allocations is too damn high!");
}
}
static StaticRefPtr<CompositorBridgeChild> sCompositorBridge;
Atomic<int32_t> KnowsCompositor::sSerialCounter(0);
CompositorBridgeChild::CompositorBridgeChild(CompositorManagerChild* aManager)
: mCompositorManager(aManager),
mIdNamespace(0),
mResourceId(0),
mCanSend(false),
mActorDestroyed(false),
mFwdTransactionId(0),
mThread(NS_GetCurrentThread()),
mProcessToken(0),
mSectionAllocator(nullptr),
mPaintLock("CompositorBridgeChild.mPaintLock"),
mTotalAsyncPaints(0),
mOutstandingAsyncPaints(0),
mOutstandingAsyncEndTransaction(false),
mIsDelayingForAsyncPaints(false),
mSlowFlushCount(0),
mTotalFlushCount(0) {
MOZ_ASSERT(NS_IsMainThread());
}
CompositorBridgeChild::~CompositorBridgeChild() {
if (mCanSend) {
gfxCriticalError() << "CompositorBridgeChild was not deinitialized";
}
}
bool CompositorBridgeChild::IsSameProcess() const {
return OtherPid() == base::GetCurrentProcId();
}
void CompositorBridgeChild::PrepareFinalDestroy() {
// Because of medium high priority DidComposite, we need to repost to
// medium high priority queue to ensure the actor is destroyed after possible
// pending DidComposite message.
nsCOMPtr<nsIRunnable> runnable =
NewRunnableMethod("CompositorBridgeChild::AfterDestroy", this,
&CompositorBridgeChild::AfterDestroy);
NS_DispatchToCurrentThreadQueue(runnable.forget(),
EventQueuePriority::MediumHigh);
}
void CompositorBridgeChild::AfterDestroy() {
// Note that we cannot rely upon mCanSend here because we already set that to
// false to prevent normal IPDL calls from being made after SendWillClose.
// The only time we should not issue Send__delete__ is if the actor is already
// destroyed, e.g. the compositor process crashed.
if (!mActorDestroyed) {
Send__delete__(this);
mActorDestroyed = true;
}
if (mCanvasChild) {
mCanvasChild->Destroy();
}
if (sCompositorBridge == this) {
sCompositorBridge = nullptr;
}
}
void CompositorBridgeChild::Destroy() {
// This must not be called from the destructor!
mTexturesWaitingNotifyNotUsed.clear();
// Destroying the layer manager may cause all sorts of things to happen, so
// let's make sure there is still a reference to keep this alive whatever
// happens.
RefPtr<CompositorBridgeChild> selfRef = this;
for (size_t i = 0; i < mTexturePools.Length(); i++) {
mTexturePools[i]->Destroy();
}
if (mSectionAllocator) {
delete mSectionAllocator;
mSectionAllocator = nullptr;
}
if (mLayerManager) {
mLayerManager->Destroy();
mLayerManager = nullptr;
}
// Flush async paints before we destroy texture data.
FlushAsyncPaints();
if (!mCanSend) {
// We may have already called destroy but still have lingering references
// or CompositorBridgeChild::ActorDestroy was called. Ensure that we do our
// post destroy clean up no matter what. It is safe to call multiple times.
NS_GetCurrentThread()->Dispatch(
NewRunnableMethod("CompositorBridgeChild::PrepareFinalDestroy", selfRef,
&CompositorBridgeChild::PrepareFinalDestroy));
return;
}
AutoTArray<PLayerTransactionChild*, 16> transactions;
ManagedPLayerTransactionChild(transactions);
for (int i = transactions.Length() - 1; i >= 0; --i) {
RefPtr<LayerTransactionChild> layers =
static_cast<LayerTransactionChild*>(transactions[i]);
layers->Destroy();
}
AutoTArray<PWebRenderBridgeChild*, 16> wrBridges;
ManagedPWebRenderBridgeChild(wrBridges);
for (int i = wrBridges.Length() - 1; i >= 0; --i) {
RefPtr<WebRenderBridgeChild> wrBridge =
static_cast<WebRenderBridgeChild*>(wrBridges[i]);
wrBridge->Destroy(/* aIsSync */ false);
}
AutoTArray<PAPZChild*, 16> apzChildren;
ManagedPAPZChild(apzChildren);
for (PAPZChild* child : apzChildren) {
Unused << child->SendDestroy();
}
AutoTArray<PWebGPUChild*, 16> webGPUChildren;
ManagedPWebGPUChild(webGPUChildren);
for (PWebGPUChild* child : webGPUChildren) {
Unused << child->SendShutdown();
}
const ManagedContainer<PTextureChild>& textures = ManagedPTextureChild();
for (auto iter = textures.ConstIter(); !iter.Done(); iter.Next()) {
RefPtr<TextureClient> texture =
TextureClient::AsTextureClient(iter.Get()->GetKey());
if (texture) {
texture->Destroy();
}
}
// The WillClose message is synchronous, so we know that after it returns
// any messages sent by the above code will have been processed on the
// other side.
SendWillClose();
mCanSend = false;
// We no longer care about unexpected shutdowns, in the remote process case.
mProcessToken = 0;
// The call just made to SendWillClose can result in IPC from the
// CompositorBridgeParent to the CompositorBridgeChild (e.g. caused by the
// destruction of shared memory). We need to ensure this gets processed by the
// CompositorBridgeChild before it gets destroyed. It suffices to ensure that
// events already in the thread get processed before the
// CompositorBridgeChild is destroyed, so we add a task to the thread to
// handle compositor destruction.
// From now on we can't send any message message.
NS_GetCurrentThread()->Dispatch(
NewRunnableMethod("CompositorBridgeChild::PrepareFinalDestroy", selfRef,
&CompositorBridgeChild::PrepareFinalDestroy));
}
// static
void CompositorBridgeChild::ShutDown() {
if (sCompositorBridge) {
sCompositorBridge->Destroy();
SpinEventLoopUntil([&]() { return !sCompositorBridge; });
}
}
bool CompositorBridgeChild::LookupCompositorFrameMetrics(
const ScrollableLayerGuid::ViewID aId, FrameMetrics& aFrame) {
SharedFrameMetricsData* data = mFrameMetricsTable.Get(aId);
if (data) {
data->CopyFrameMetrics(&aFrame);
return true;
}
return false;
}
void CompositorBridgeChild::InitForContent(uint32_t aNamespace) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aNamespace);
if (RefPtr<CompositorBridgeChild> old = sCompositorBridge.forget()) {
// Note that at this point, ActorDestroy may not have been called yet,
// meaning mCanSend is still true. In this case we will try to send a
// synchronous WillClose message to the parent, and will certainly get
// a false result and a MsgDropped processing error. This is okay.
old->Destroy();
}
mCanSend = true;
mIdNamespace = aNamespace;
sCompositorBridge = this;
}
void CompositorBridgeChild::InitForWidget(uint64_t aProcessToken,
LayerManager* aLayerManager,
uint32_t aNamespace) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aProcessToken);
MOZ_ASSERT(aLayerManager);
MOZ_ASSERT(aNamespace);
mCanSend = true;
mProcessToken = aProcessToken;
mLayerManager = aLayerManager;
mIdNamespace = aNamespace;
}
/*static*/
CompositorBridgeChild* CompositorBridgeChild::Get() {
// This is only expected to be used in child processes. While the parent
// process does have CompositorBridgeChild instances, it has _multiple_ (one
// per window), and therefore there is no global singleton available.
MOZ_ASSERT(!XRE_IsParentProcess());
return sCompositorBridge;
}
// static
bool CompositorBridgeChild::ChildProcessHasCompositorBridge() {
return sCompositorBridge != nullptr;
}
/* static */
bool CompositorBridgeChild::CompositorIsInGPUProcess() {
MOZ_ASSERT(NS_IsMainThread());
if (XRE_IsParentProcess()) {
return !!GPUProcessManager::Get()->GetGPUChild();
}
MOZ_ASSERT(XRE_IsContentProcess());
CompositorBridgeChild* bridge = CompositorBridgeChild::Get();
if (!bridge) {
return false;
}
return bridge->OtherPid() != dom::ContentChild::GetSingleton()->OtherPid();
}
PLayerTransactionChild* CompositorBridgeChild::AllocPLayerTransactionChild(
const nsTArray<LayersBackend>& aBackendHints, const LayersId& aId) {
LayerTransactionChild* c = new LayerTransactionChild(aId);
c->AddIPDLReference();
return c;
}
bool CompositorBridgeChild::DeallocPLayerTransactionChild(
PLayerTransactionChild* actor) {
LayersId childId = static_cast<LayerTransactionChild*>(actor)->GetId();
ClearSharedFrameMetricsData(childId);
static_cast<LayerTransactionChild*>(actor)->ReleaseIPDLReference();
return true;
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvInvalidateLayers(
const LayersId& aLayersId) {
if (mLayerManager) {
MOZ_ASSERT(!aLayersId.IsValid());
FrameLayerBuilder::InvalidateAllLayers(mLayerManager);
} else if (aLayersId.IsValid()) {
if (dom::BrowserChild* child = dom::BrowserChild::GetFrom(aLayersId)) {
child->InvalidateLayers();
}
}
return IPC_OK();
}
#if defined(XP_WIN) || defined(MOZ_WIDGET_GTK)
static void CalculatePluginClip(
const LayoutDeviceIntRect& aBounds,
const nsTArray<LayoutDeviceIntRect>& aPluginClipRects,
const LayoutDeviceIntPoint& aContentOffset,
const LayoutDeviceIntRegion& aParentLayerVisibleRegion,
nsTArray<LayoutDeviceIntRect>& aResult, LayoutDeviceIntRect& aVisibleBounds,
bool& aPluginIsVisible) {
aPluginIsVisible = true;
LayoutDeviceIntRegion contentVisibleRegion;
// aPluginClipRects (plugin widget origin) - contains *visible* rects
for (uint32_t idx = 0; idx < aPluginClipRects.Length(); idx++) {
LayoutDeviceIntRect rect = aPluginClipRects[idx];
// shift to content origin
rect.MoveBy(aBounds.X(), aBounds.Y());
// accumulate visible rects
contentVisibleRegion.OrWith(rect);
}
// apply layers clip (window origin)
LayoutDeviceIntRegion region = aParentLayerVisibleRegion;
region.MoveBy(-aContentOffset.x, -aContentOffset.y);
contentVisibleRegion.AndWith(region);
if (contentVisibleRegion.IsEmpty()) {
aPluginIsVisible = false;
return;
}
// shift to plugin widget origin
contentVisibleRegion.MoveBy(-aBounds.X(), -aBounds.Y());
for (auto iter = contentVisibleRegion.RectIter(); !iter.Done(); iter.Next()) {
const LayoutDeviceIntRect& rect = iter.Get();
aResult.AppendElement(rect);
aVisibleBounds.UnionRect(aVisibleBounds, rect);
}
}
#endif
mozilla::ipc::IPCResult CompositorBridgeChild::RecvUpdatePluginConfigurations(
const LayoutDeviceIntPoint& aContentOffset,
const LayoutDeviceIntRegion& aParentLayerVisibleRegion,
nsTArray<PluginWindowData>&& aPlugins) {
#if !defined(XP_WIN) && !defined(MOZ_WIDGET_GTK)
MOZ_ASSERT_UNREACHABLE(
"CompositorBridgeChild::RecvUpdatePluginConfigurations"
" calls unexpected on this platform.");
return IPC_FAIL_NO_REASON(this);
#else
// Now that we are on the main thread, update plugin widget config.
// This should happen a little before we paint to the screen assuming
// the main thread is running freely.
DebugOnly<nsresult> rv;
MOZ_ASSERT(NS_IsMainThread());
// Tracks visible plugins we update, so we can hide any plugins we don't.
nsTArray<uintptr_t> visiblePluginIds;
nsIWidget* parent = nullptr;
for (uint32_t pluginsIdx = 0; pluginsIdx < aPlugins.Length(); pluginsIdx++) {
nsIWidget* widget = nsIWidget::LookupRegisteredPluginWindow(
aPlugins[pluginsIdx].windowId());
if (!widget) {
NS_WARNING("Unexpected, plugin id not found!");
continue;
}
if (!parent) {
parent = widget->GetParent();
}
bool isVisible = aPlugins[pluginsIdx].visible();
if (widget && !widget->Destroyed()) {
LayoutDeviceIntRect bounds;
LayoutDeviceIntRect visibleBounds;
// If the plugin is visible update it's geometry.
if (isVisible) {
// Set bounds (content origin)
bounds = aPlugins[pluginsIdx].bounds();
nsTArray<LayoutDeviceIntRect> rectsOut;
// This call may change the value of isVisible
CalculatePluginClip(bounds, aPlugins[pluginsIdx].clip(), aContentOffset,
aParentLayerVisibleRegion, rectsOut, visibleBounds,
isVisible);
// content clipping region (widget origin)
rv = widget->SetWindowClipRegion(rectsOut, false);
NS_ASSERTION(NS_SUCCEEDED(rv), "widget call failure");
// This will trigger a browser window paint event for areas uncovered
// by a child window move, and will call invalidate on the plugin
// parent window which the browser owns. The latter gets picked up in
// our OnPaint handler and forwarded over to the plugin process async.
widget->Resize(aContentOffset.x + bounds.X(),
aContentOffset.y + bounds.Y(), bounds.Width(),
bounds.Height(), true);
}
widget->Enable(isVisible);
// visible state - updated after clipping, prior to invalidating
widget->Show(isVisible);
// Handle invalidation, this can be costly, avoid if it is not needed.
if (isVisible) {
// invalidate region (widget origin)
# if defined(XP_WIN)
// Work around for flash's crummy sandbox. See bug 762948. This call
// digs down into the window hirearchy, invalidating regions on
// windows owned by other processes.
mozilla::widget::WinUtils::InvalidatePluginAsWorkaround(widget,
visibleBounds);
# else
widget->Invalidate(visibleBounds);
# endif
visiblePluginIds.AppendElement(aPlugins[pluginsIdx].windowId());
}
}
}
// Any plugins we didn't update need to be hidden, as they are
// not associated with visible content.
nsIWidget::UpdateRegisteredPluginWindowVisibility((uintptr_t)parent,
visiblePluginIds);
if (!mCanSend) {
return IPC_OK();
}
SendRemotePluginsReady();
return IPC_OK();
#endif // !defined(XP_WIN) && !defined(MOZ_WIDGET_GTK)
}
#if defined(XP_WIN)
static void ScheduleSendAllPluginsCaptured(CompositorBridgeChild* aThis,
nsISerialEventTarget* aThread) {
aThread->Dispatch(NewNonOwningRunnableMethod(
"CompositorBridgeChild::SendAllPluginsCaptured", aThis,
&CompositorBridgeChild::SendAllPluginsCaptured));
}
#endif
mozilla::ipc::IPCResult CompositorBridgeChild::RecvCaptureAllPlugins(
const uintptr_t& aParentWidget) {
#if defined(XP_WIN)
MOZ_ASSERT(NS_IsMainThread());
nsIWidget::CaptureRegisteredPlugins(aParentWidget);
// Bounce the call to SendAllPluginsCaptured off the ImageBridgeChild thread,
// to make sure that the image updates on that thread have been processed.
ImageBridgeChild::GetSingleton()->GetThread()->Dispatch(NewRunnableFunction(
"ScheduleSendAllPluginsCapturedRunnable", &ScheduleSendAllPluginsCaptured,
this, NS_GetCurrentThread()));
return IPC_OK();
#else
MOZ_ASSERT_UNREACHABLE(
"CompositorBridgeChild::RecvCaptureAllPlugins calls unexpected.");
return IPC_FAIL_NO_REASON(this);
#endif
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvHideAllPlugins(
const uintptr_t& aParentWidget) {
#if !defined(XP_WIN) && !defined(MOZ_WIDGET_GTK)
MOZ_ASSERT_UNREACHABLE(
"CompositorBridgeChild::RecvHideAllPlugins calls "
"unexpected on this platform.");
return IPC_FAIL_NO_REASON(this);
#else
MOZ_ASSERT(NS_IsMainThread());
nsTArray<uintptr_t> list;
nsIWidget::UpdateRegisteredPluginWindowVisibility(aParentWidget, list);
if (!mCanSend) {
return IPC_OK();
}
SendRemotePluginsReady();
return IPC_OK();
#endif // !defined(XP_WIN) && !defined(MOZ_WIDGET_GTK)
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvDidComposite(
const LayersId& aId, const TransactionId& aTransactionId,
const TimeStamp& aCompositeStart, const TimeStamp& aCompositeEnd) {
// Hold a reference to keep texture pools alive. See bug 1387799
const auto texturePools = mTexturePools.Clone();
if (mLayerManager) {
MOZ_ASSERT(!aId.IsValid());
MOZ_ASSERT(mLayerManager->GetBackendType() ==
LayersBackend::LAYERS_CLIENT ||
mLayerManager->GetBackendType() == LayersBackend::LAYERS_WR);
// Hold a reference to keep LayerManager alive. See Bug 1242668.
RefPtr<LayerManager> m = mLayerManager;
m->DidComposite(aTransactionId, aCompositeStart, aCompositeEnd);
} else if (aId.IsValid()) {
RefPtr<dom::BrowserChild> child = dom::BrowserChild::GetFrom(aId);
if (child) {
child->DidComposite(aTransactionId, aCompositeStart, aCompositeEnd);
}
}
for (size_t i = 0; i < texturePools.Length(); i++) {
texturePools[i]->ReturnDeferredClients();
}
return IPC_OK();
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvNotifyFrameStats(
nsTArray<FrameStats>&& aFrameStats) {
gfxPlatform::GetPlatform()->NotifyFrameStats(std::move(aFrameStats));
return IPC_OK();
}
void CompositorBridgeChild::ActorDestroy(ActorDestroyReason aWhy) {
if (aWhy == AbnormalShutdown) {
// If the parent side runs into a problem then the actor will be destroyed.
// There is nothing we can do in the child side, here sets mCanSend as
// false.
gfxCriticalNote << "Receive IPC close with reason=AbnormalShutdown";
}
{
// We take the lock to update these fields, since they are read from the
// paint thread. We don't need the lock to init them, since that happens
// on the main thread before the paint thread can ever grab a reference
// to the CompositorBridge object.
//
// Note that it is useful to take this lock for one other reason: It also
// tells us whether GetIPCChannel is safe to call. If we access the IPC
// channel within this lock, when mCanSend is true, then we know it has not
// been zapped by IPDL.
MonitorAutoLock lock(mPaintLock);
mCanSend = false;
mActorDestroyed = true;
}
if (mProcessToken && XRE_IsParentProcess()) {
GPUProcessManager::Get()->NotifyRemoteActorDestroyed(mProcessToken);
}
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvSharedCompositorFrameMetrics(
const mozilla::ipc::SharedMemoryBasic::Handle& metrics,
const CrossProcessMutexHandle& handle, const LayersId& aLayersId,
const uint32_t& aAPZCId) {
SharedFrameMetricsData* data =
new SharedFrameMetricsData(metrics, handle, aLayersId, aAPZCId);
mFrameMetricsTable.Put(data->GetViewID(), data);
return IPC_OK();
}
mozilla::ipc::IPCResult
CompositorBridgeChild::RecvReleaseSharedCompositorFrameMetrics(
const ViewID& aId, const uint32_t& aAPZCId) {
if (auto entry = mFrameMetricsTable.Lookup(aId)) {
// The SharedFrameMetricsData may have been removed previously if
// a SharedFrameMetricsData with the same ViewID but later APZCId had
// been store and over wrote it.
if (entry.Data()->GetAPZCId() == aAPZCId) {
entry.Remove();
}
}
return IPC_OK();
}
CompositorBridgeChild::SharedFrameMetricsData::SharedFrameMetricsData(
const ipc::SharedMemoryBasic::Handle& metrics,
const CrossProcessMutexHandle& handle, const LayersId& aLayersId,
const uint32_t& aAPZCId)
: mMutex(nullptr), mLayersId(aLayersId), mAPZCId(aAPZCId) {
mBuffer = new ipc::SharedMemoryBasic;
mBuffer->SetHandle(metrics, ipc::SharedMemory::RightsReadOnly);
mBuffer->Map(sizeof(FrameMetrics));
mMutex = new CrossProcessMutex(handle);
MOZ_COUNT_CTOR(SharedFrameMetricsData);
}
CompositorBridgeChild::SharedFrameMetricsData::~SharedFrameMetricsData() {
// When the hash table deletes the class, delete
// the shared memory and mutex.
delete mMutex;
mBuffer = nullptr;
MOZ_COUNT_DTOR(SharedFrameMetricsData);
}
void CompositorBridgeChild::SharedFrameMetricsData::CopyFrameMetrics(
FrameMetrics* aFrame) {
const FrameMetrics* frame =
static_cast<const FrameMetrics*>(mBuffer->memory());
MOZ_ASSERT(frame);
mMutex->Lock();
*aFrame = *frame;
mMutex->Unlock();
}
ScrollableLayerGuid::ViewID
CompositorBridgeChild::SharedFrameMetricsData::GetViewID() {
const FrameMetrics* frame =
static_cast<const FrameMetrics*>(mBuffer->memory());
MOZ_ASSERT(frame);
// Not locking to read of mScrollId since it should not change after being
// initially set.
return frame->GetScrollId();
}
LayersId CompositorBridgeChild::SharedFrameMetricsData::GetLayersId() const {
return mLayersId;
}
uint32_t CompositorBridgeChild::SharedFrameMetricsData::GetAPZCId() {
return mAPZCId;
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvRemotePaintIsReady() {
// Used on the content thread, this bounces the message to the
// BrowserParent (via the BrowserChild) if the notification was previously
// requested. XPCOM gives a soup of compiler errors when trying to
// do_QueryReference so I'm using static_cast<>
MOZ_LAYERS_LOG(
("[RemoteGfx] CompositorBridgeChild received RemotePaintIsReady"));
RefPtr<nsIBrowserChild> iBrowserChild(do_QueryReferent(mWeakBrowserChild));
if (!iBrowserChild) {
MOZ_LAYERS_LOG(
("[RemoteGfx] Note: BrowserChild was released before "
"RemotePaintIsReady. "
"MozAfterRemotePaint will not be sent to listener."));
return IPC_OK();
}
BrowserChild* browserChild = static_cast<BrowserChild*>(iBrowserChild.get());
MOZ_ASSERT(browserChild);
Unused << browserChild->SendRemotePaintIsReady();
mWeakBrowserChild = nullptr;
return IPC_OK();
}
void CompositorBridgeChild::RequestNotifyAfterRemotePaint(
BrowserChild* aBrowserChild) {
MOZ_ASSERT(aBrowserChild,
"NULL BrowserChild not allowed in "
"CompositorBridgeChild::RequestNotifyAfterRemotePaint");
mWeakBrowserChild =
do_GetWeakReference(static_cast<dom::BrowserChild*>(aBrowserChild));
if (!mCanSend) {
return;
}
Unused << SendRequestNotifyAfterRemotePaint();
}
void CompositorBridgeChild::CancelNotifyAfterRemotePaint(
BrowserChild* aBrowserChild) {
RefPtr<nsIBrowserChild> iBrowserChild(do_QueryReferent(mWeakBrowserChild));
if (!iBrowserChild) {
return;
}
BrowserChild* browserChild = static_cast<BrowserChild*>(iBrowserChild.get());
if (browserChild == aBrowserChild) {
mWeakBrowserChild = nullptr;
}
}
bool CompositorBridgeChild::SendWillClose() {
MOZ_RELEASE_ASSERT(mCanSend);
return PCompositorBridgeChild::SendWillClose();
}
bool CompositorBridgeChild::SendPause() {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendPause();
}
bool CompositorBridgeChild::SendResume() {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendResume();
}
bool CompositorBridgeChild::SendResumeAsync() {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendResumeAsync();
}
bool CompositorBridgeChild::SendNotifyChildCreated(
const LayersId& id, CompositorOptions* aOptions) {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendNotifyChildCreated(id, aOptions);
}
bool CompositorBridgeChild::SendAdoptChild(const LayersId& id) {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendAdoptChild(id);
}
bool CompositorBridgeChild::SendMakeSnapshot(
const SurfaceDescriptor& inSnapshot, const gfx::IntRect& dirtyRect) {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendMakeSnapshot(inSnapshot, dirtyRect);
}
bool CompositorBridgeChild::SendFlushRendering() {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendFlushRendering();
}
bool CompositorBridgeChild::SendStartFrameTimeRecording(
const int32_t& bufferSize, uint32_t* startIndex) {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendStartFrameTimeRecording(bufferSize,
startIndex);
}
bool CompositorBridgeChild::SendStopFrameTimeRecording(
const uint32_t& startIndex, nsTArray<float>* intervals) {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendStopFrameTimeRecording(startIndex,
intervals);
}
bool CompositorBridgeChild::SendNotifyRegionInvalidated(
const nsIntRegion& region) {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendNotifyRegionInvalidated(region);
}
bool CompositorBridgeChild::SendRequestNotifyAfterRemotePaint() {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendRequestNotifyAfterRemotePaint();
}
bool CompositorBridgeChild::SendAllPluginsCaptured() {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::SendAllPluginsCaptured();
}
PTextureChild* CompositorBridgeChild::AllocPTextureChild(
const SurfaceDescriptor&, const ReadLockDescriptor&, const LayersBackend&,
const TextureFlags&, const LayersId&, const uint64_t& aSerial,
const wr::MaybeExternalImageId& aExternalImageId) {
return TextureClient::CreateIPDLActor();
}
bool CompositorBridgeChild::DeallocPTextureChild(PTextureChild* actor) {
return TextureClient::DestroyIPDLActor(actor);
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvParentAsyncMessages(
nsTArray<AsyncParentMessageData>&& aMessages) {
for (AsyncParentMessageArray::index_type i = 0; i < aMessages.Length(); ++i) {
const AsyncParentMessageData& message = aMessages[i];
switch (message.type()) {
case AsyncParentMessageData::TOpNotifyNotUsed: {
const OpNotifyNotUsed& op = message.get_OpNotifyNotUsed();
NotifyNotUsed(op.TextureId(), op.fwdTransactionId());
break;
}
case AsyncParentMessageData::TOpDeliverReleaseFence: {
// Release fences are delivered via ImageBridge.
// Since some TextureClients are recycled without recycle callback.
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
break;
}
default:
NS_ERROR("unknown AsyncParentMessageData type");
return IPC_FAIL_NO_REASON(this);
}
}
return IPC_OK();
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvObserveLayersUpdate(
const LayersId& aLayersId, const LayersObserverEpoch& aEpoch,
const bool& aActive) {
// This message is sent via the window compositor, not the tab compositor -
// however it still has a layers id.
MOZ_ASSERT(aLayersId.IsValid());
MOZ_ASSERT(XRE_IsParentProcess());
if (RefPtr<dom::BrowserParent> tab =
dom::BrowserParent::GetBrowserParentFromLayersId(aLayersId)) {
tab->LayerTreeUpdate(aEpoch, aActive);
}
return IPC_OK();
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvCompositorOptionsChanged(
const LayersId& aLayersId, const CompositorOptions& aNewOptions) {
MOZ_ASSERT(aLayersId.IsValid());
MOZ_ASSERT(XRE_IsParentProcess());
if (RefPtr<dom::BrowserParent> tab =
dom::BrowserParent::GetBrowserParentFromLayersId(aLayersId)) {
Unused << tab->SendCompositorOptionsChanged(aNewOptions);
}
return IPC_OK();
}
mozilla::ipc::IPCResult CompositorBridgeChild::RecvNotifyJankedAnimations(
const LayersId& aLayersId, nsTArray<uint64_t>&& aJankedAnimations) {
if (mLayerManager) {
MOZ_ASSERT(!aLayersId.IsValid());
mLayerManager->UpdatePartialPrerenderedAnimations(aJankedAnimations);
} else if (aLayersId.IsValid()) {
RefPtr<dom::BrowserChild> child = dom::BrowserChild::GetFrom(aLayersId);
if (child) {
child->NotifyJankedAnimations(aJankedAnimations);
}
}
return IPC_OK();
}
void CompositorBridgeChild::HoldUntilCompositableRefReleasedIfNecessary(
TextureClient* aClient) {
if (!aClient) {
return;
}
#ifdef MOZ_WIDGET_ANDROID
auto bufferId = aClient->GetInternalData()->GetBufferId();
if (bufferId.isSome()) {
MOZ_ASSERT(aClient->GetFlags() & TextureFlags::WAIT_HOST_USAGE_END);
AndroidHardwareBufferManager::Get()->HoldUntilNotifyNotUsed(
bufferId.ref(), GetFwdTransactionId(), /* aUsesImageBridge */ false);
}
#endif
bool waitNotifyNotUsed =
aClient->GetFlags() & TextureFlags::RECYCLE ||
aClient->GetFlags() & TextureFlags::WAIT_HOST_USAGE_END;
if (!waitNotifyNotUsed) {
return;
}
aClient->SetLastFwdTransactionId(GetFwdTransactionId());
mTexturesWaitingNotifyNotUsed.emplace(aClient->GetSerial(), aClient);
}
void CompositorBridgeChild::NotifyNotUsed(uint64_t aTextureId,
uint64_t aFwdTransactionId) {
auto it = mTexturesWaitingNotifyNotUsed.find(aTextureId);
if (it != mTexturesWaitingNotifyNotUsed.end()) {
if (aFwdTransactionId < it->second->GetLastFwdTransactionId()) {
// Released on host side, but client already requested newer use texture.
return;
}
mTexturesWaitingNotifyNotUsed.erase(it);
}
}
void CompositorBridgeChild::CancelWaitForNotifyNotUsed(uint64_t aTextureId) {
mTexturesWaitingNotifyNotUsed.erase(aTextureId);
}
TextureClientPool* CompositorBridgeChild::GetTexturePool(
KnowsCompositor* aAllocator, SurfaceFormat aFormat, TextureFlags aFlags) {
for (size_t i = 0; i < mTexturePools.Length(); i++) {
if (mTexturePools[i]->GetBackend() ==
aAllocator->GetCompositorBackendType() &&
mTexturePools[i]->GetMaxTextureSize() ==
aAllocator->GetMaxTextureSize() &&
mTexturePools[i]->GetFormat() == aFormat &&
mTexturePools[i]->GetFlags() == aFlags) {
return mTexturePools[i];
}
}
mTexturePools.AppendElement(new TextureClientPool(
aAllocator, aFormat, gfx::gfxVars::TileSize(), aFlags,
StaticPrefs::layers_tile_pool_shrink_timeout_AtStartup(),
StaticPrefs::layers_tile_pool_clear_timeout_AtStartup(),
StaticPrefs::layers_tile_initial_pool_size_AtStartup(),
StaticPrefs::layers_tile_pool_unused_size_AtStartup(), this));
return mTexturePools.LastElement();
}
void CompositorBridgeChild::HandleMemoryPressure() {
for (size_t i = 0; i < mTexturePools.Length(); i++) {
mTexturePools[i]->Clear();
}
}
void CompositorBridgeChild::ClearTexturePool() {
for (size_t i = 0; i < mTexturePools.Length(); i++) {
mTexturePools[i]->Clear();
}
}
FixedSizeSmallShmemSectionAllocator*
CompositorBridgeChild::GetTileLockAllocator() {
if (!IPCOpen()) {
return nullptr;
}
if (!mSectionAllocator) {
mSectionAllocator = new FixedSizeSmallShmemSectionAllocator(this);
}
return mSectionAllocator;
}
PTextureChild* CompositorBridgeChild::CreateTexture(
const SurfaceDescriptor& aSharedData, const ReadLockDescriptor& aReadLock,
LayersBackend aLayersBackend, TextureFlags aFlags, uint64_t aSerial,
wr::MaybeExternalImageId& aExternalImageId, nsISerialEventTarget* aTarget) {
PTextureChild* textureChild =
AllocPTextureChild(aSharedData, aReadLock, aLayersBackend, aFlags,
LayersId{0} /* FIXME */, aSerial, aExternalImageId);
// Do the DOM labeling.
if (aTarget) {
SetEventTargetForActor(textureChild, aTarget);
}
return SendPTextureConstructor(
textureChild, aSharedData, aReadLock, aLayersBackend, aFlags,
LayersId{0} /* FIXME? */, aSerial, aExternalImageId);
}
already_AddRefed<CanvasChild> CompositorBridgeChild::GetCanvasChild() {
MOZ_ASSERT(gfx::gfxVars::RemoteCanvasEnabled());
if (CanvasChild::Deactivated()) {
return nullptr;
}
if (!mCanvasChild) {
ipc::Endpoint<PCanvasParent> parentEndpoint;
ipc::Endpoint<PCanvasChild> childEndpoint;
nsresult rv = PCanvas::CreateEndpoints(OtherPid(), base::GetCurrentProcId(),
&parentEndpoint, &childEndpoint);
if (NS_SUCCEEDED(rv)) {
Unused << SendInitPCanvasParent(std::move(parentEndpoint));
mCanvasChild = new CanvasChild(std::move(childEndpoint));
}
}
return do_AddRef(mCanvasChild);
}
void CompositorBridgeChild::EndCanvasTransaction() {
if (mCanvasChild) {
mCanvasChild->EndTransaction();
if (mCanvasChild->ShouldBeCleanedUp()) {
mCanvasChild->Destroy();
Unused << SendReleasePCanvasParent();
mCanvasChild = nullptr;
}
}
}
RefPtr<webgpu::WebGPUChild> CompositorBridgeChild::GetWebGPUChild() {
MOZ_ASSERT(gfx::gfxConfig::IsEnabled(gfx::Feature::WEBGPU));
if (!mWebGPUChild) {
webgpu::PWebGPUChild* bridge = SendPWebGPUConstructor();
mWebGPUChild = static_cast<webgpu::WebGPUChild*>(bridge);
}
return mWebGPUChild;
}
bool CompositorBridgeChild::AllocUnsafeShmem(
size_t aSize, ipc::SharedMemory::SharedMemoryType aType,
ipc::Shmem* aShmem) {
ShmemAllocated(this);
return PCompositorBridgeChild::AllocUnsafeShmem(aSize, aType, aShmem);
}
bool CompositorBridgeChild::AllocShmem(
size_t aSize, ipc::SharedMemory::SharedMemoryType aType,
ipc::Shmem* aShmem) {
ShmemAllocated(this);
return PCompositorBridgeChild::AllocShmem(aSize, aType, aShmem);
}
bool CompositorBridgeChild::DeallocShmem(ipc::Shmem& aShmem) {
if (!mCanSend) {
return false;
}
return PCompositorBridgeChild::DeallocShmem(aShmem);
}
widget::PCompositorWidgetChild*
CompositorBridgeChild::AllocPCompositorWidgetChild(
const CompositorWidgetInitData& aInitData) {
// We send the constructor manually.
MOZ_CRASH("Should not be called");
return nullptr;
}
bool CompositorBridgeChild::DeallocPCompositorWidgetChild(
PCompositorWidgetChild* aActor) {
#ifdef MOZ_WIDGET_SUPPORTS_OOP_COMPOSITING
delete aActor;
return true;
#else
return false;
#endif
}
PAPZCTreeManagerChild* CompositorBridgeChild::AllocPAPZCTreeManagerChild(
const LayersId& aLayersId) {
APZCTreeManagerChild* child = new APZCTreeManagerChild();
child->AddIPDLReference();
return child;
}
PAPZChild* CompositorBridgeChild::AllocPAPZChild(const LayersId& aLayersId) {
// We send the constructor manually.
MOZ_CRASH("Should not be called");
return nullptr;
}
bool CompositorBridgeChild::DeallocPAPZChild(PAPZChild* aActor) {
delete aActor;
return true;
}
bool CompositorBridgeChild::DeallocPAPZCTreeManagerChild(
PAPZCTreeManagerChild* aActor) {
APZCTreeManagerChild* child = static_cast<APZCTreeManagerChild*>(aActor);
child->ReleaseIPDLReference();
return true;
}
// -
void CompositorBridgeChild::WillEndTransaction() { ResetShmemCounter(); }
PWebRenderBridgeChild* CompositorBridgeChild::AllocPWebRenderBridgeChild(
const wr::PipelineId& aPipelineId, const LayoutDeviceIntSize&,
const WindowKind&) {
WebRenderBridgeChild* child = new WebRenderBridgeChild(aPipelineId);
child->AddIPDLReference();
return child;
}
bool CompositorBridgeChild::DeallocPWebRenderBridgeChild(
PWebRenderBridgeChild* aActor) {
WebRenderBridgeChild* child = static_cast<WebRenderBridgeChild*>(aActor);
ClearSharedFrameMetricsData(wr::AsLayersId(child->GetPipeline()));
child->ReleaseIPDLReference();
return true;
}
webgpu::PWebGPUChild* CompositorBridgeChild::AllocPWebGPUChild() {
webgpu::WebGPUChild* child = new webgpu::WebGPUChild();
child->AddIPDLReference();
return child;
}
bool CompositorBridgeChild::DeallocPWebGPUChild(webgpu::PWebGPUChild* aActor) {
webgpu::WebGPUChild* child = static_cast<webgpu::WebGPUChild*>(aActor);
child->ReleaseIPDLReference();
return true;
}
void CompositorBridgeChild::ClearSharedFrameMetricsData(LayersId aLayersId) {
for (auto iter = mFrameMetricsTable.Iter(); !iter.Done(); iter.Next()) {
auto data = iter.UserData();
if (data->GetLayersId() == aLayersId) {
iter.Remove();
}
}
}
uint64_t CompositorBridgeChild::GetNextResourceId() {
++mResourceId;
MOZ_RELEASE_ASSERT(mResourceId != UINT32_MAX);
uint64_t id = mIdNamespace;
id = (id << 32) | mResourceId;
return id;
}
wr::MaybeExternalImageId CompositorBridgeChild::GetNextExternalImageId() {
return Some(wr::ToExternalImageId(GetNextResourceId()));
}
wr::PipelineId CompositorBridgeChild::GetNextPipelineId() {
return wr::AsPipelineId(GetNextResourceId());
}
void CompositorBridgeChild::FlushAsyncPaints() {
MOZ_ASSERT(NS_IsMainThread());
Maybe<TimeStamp> start;
if (XRE_IsContentProcess() && gfx::gfxVars::UseOMTP()) {
start = Some(TimeStamp::Now());
}
{
MonitorAutoLock lock(mPaintLock);
while (mOutstandingAsyncPaints > 0 || mOutstandingAsyncEndTransaction) {
lock.Wait();
}
// It's now safe to free any TextureClients that were used during painting.
mTextureClientsForAsyncPaint.Clear();
}
if (start) {
float ms = (TimeStamp::Now() - start.value()).ToMilliseconds();
// Anything above 200us gets recorded.
if (ms >= 0.2) {
mSlowFlushCount++;
Telemetry::Accumulate(Telemetry::GFX_OMTP_PAINT_WAIT_TIME, int32_t(ms));
}
mTotalFlushCount++;
double ratio = double(mSlowFlushCount) / double(mTotalFlushCount);
Telemetry::ScalarSet(Telemetry::ScalarID::GFX_OMTP_PAINT_WAIT_RATIO,
uint32_t(ratio * 100 * 100));
}
}
void CompositorBridgeChild::NotifyBeginAsyncPaint(PaintTask* aTask) {
MOZ_ASSERT(NS_IsMainThread());
MonitorAutoLock lock(mPaintLock);
if (mTotalAsyncPaints == 0) {
mAsyncTransactionBegin = TimeStamp::Now();
}
mTotalAsyncPaints += 1;
// We must not be waiting for paints or buffer copying to complete yet. This
// would imply we started a new paint without waiting for a previous one,
// which could lead to incorrect rendering or IPDL deadlocks.
MOZ_ASSERT(!mIsDelayingForAsyncPaints);
mOutstandingAsyncPaints++;
// Mark texture clients that they are being used for async painting, and
// make sure we hold them alive on the main thread.
for (auto& client : aTask->mClients) {
client->AddPaintThreadRef();
mTextureClientsForAsyncPaint.AppendElement(client);
};
}
// Must only be called from the paint thread. Notifies the CompositorBridge
// that the paint thread has finished an asynchronous paint request.
bool CompositorBridgeChild::NotifyFinishedAsyncWorkerPaint(PaintTask* aTask) {
MOZ_ASSERT(PaintThread::Get()->IsOnPaintWorkerThread());
MonitorAutoLock lock(mPaintLock);
mOutstandingAsyncPaints--;
for (auto& client : aTask->mClients) {
client->DropPaintThreadRef();
};
aTask->DropTextureClients();
// If the main thread has completed queuing work and this was the
// last paint, then it is time to end the layer transaction and sync
return mOutstandingAsyncEndTransaction && mOutstandingAsyncPaints == 0;
}
bool CompositorBridgeChild::NotifyBeginAsyncEndLayerTransaction(
SyncObjectClient* aSyncObject) {
MOZ_ASSERT(NS_IsMainThread());
MonitorAutoLock lock(mPaintLock);
MOZ_ASSERT(!mOutstandingAsyncEndTransaction);
mOutstandingAsyncEndTransaction = true;
mOutstandingAsyncSyncObject = aSyncObject;
return mOutstandingAsyncPaints == 0;
}
void CompositorBridgeChild::NotifyFinishedAsyncEndLayerTransaction() {
MOZ_ASSERT(PaintThread::Get()->IsOnPaintWorkerThread());
if (mOutstandingAsyncSyncObject) {
mOutstandingAsyncSyncObject->Synchronize();
mOutstandingAsyncSyncObject = nullptr;
}
MonitorAutoLock lock(mPaintLock);
if (mTotalAsyncPaints > 0) {
float tenthMs =
(TimeStamp::Now() - mAsyncTransactionBegin).ToMilliseconds() * 10;
Telemetry::Accumulate(Telemetry::GFX_OMTP_PAINT_TASK_COUNT,
int32_t(mTotalAsyncPaints));
Telemetry::Accumulate(Telemetry::GFX_OMTP_PAINT_TIME, int32_t(tenthMs));
mTotalAsyncPaints = 0;
}
// Since this should happen after ALL paints are done and
// at the end of a transaction, this should always be true.
MOZ_RELEASE_ASSERT(mOutstandingAsyncPaints == 0);
MOZ_ASSERT(mOutstandingAsyncEndTransaction);
mOutstandingAsyncEndTransaction = false;
// It's possible that we painted so fast that the main thread never reached
// the code that starts delaying messages. If so, mIsDelayingForAsyncPaints
// will be false, and we can safely return.
if (mIsDelayingForAsyncPaints) {
ResumeIPCAfterAsyncPaint();
}
// Notify the main thread in case it's blocking. We do this unconditionally
// to avoid deadlocking.
lock.Notify();
}
void CompositorBridgeChild::ResumeIPCAfterAsyncPaint() {
// Note: the caller is responsible for holding the lock.
mPaintLock.AssertCurrentThreadOwns();
MOZ_ASSERT(PaintThread::Get()->IsOnPaintWorkerThread());
MOZ_ASSERT(mOutstandingAsyncPaints == 0);
MOZ_ASSERT(!mOutstandingAsyncEndTransaction);
MOZ_ASSERT(mIsDelayingForAsyncPaints);
mIsDelayingForAsyncPaints = false;
// It's also possible that the channel has shut down already.
if (!mCanSend || mActorDestroyed) {
return;
}
GetIPCChannel()->StopPostponingSends();
}
void CompositorBridgeChild::PostponeMessagesIfAsyncPainting() {
MOZ_ASSERT(NS_IsMainThread());
MonitorAutoLock lock(mPaintLock);
MOZ_ASSERT(!mIsDelayingForAsyncPaints);
// We need to wait for async paints and the async end transaction as
// it will do texture synchronization
if (mOutstandingAsyncPaints > 0 || mOutstandingAsyncEndTransaction) {
mIsDelayingForAsyncPaints = true;
GetIPCChannel()->BeginPostponingSends();
}
}
} // namespace layers
} // namespace mozilla
|