forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nsBulletFrame.cpp
1343 lines (1144 loc) · 46.5 KB
/
nsBulletFrame.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/. */
/* rendering object for list-item bullets */
#include "nsBulletFrame.h"
#include "gfx2DGlue.h"
#include "gfxContext.h"
#include "gfxUtils.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/PathHelpers.h"
#include "mozilla/layers/LayersMessages.h"
#include "mozilla/layers/StackingContextHelper.h"
#include "mozilla/layers/RenderRootStateManager.h"
#include "mozilla/layers/WebRenderMessages.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Move.h"
#include "mozilla/PresShell.h"
#include "nsCOMPtr.h"
#include "nsCSSFrameConstructor.h"
#include "nsFontMetrics.h"
#include "nsGkAtoms.h"
#include "nsGenericHTMLElement.h"
#include "nsAttrValueInlines.h"
#include "nsPresContext.h"
#include "mozilla/dom/Document.h"
#include "nsDisplayList.h"
#include "nsCounterManager.h"
#include "nsBidiUtils.h"
#include "CounterStyleManager.h"
#include "UnitTransforms.h"
#include "imgIContainer.h"
#include "ImageLayers.h"
#include "imgRequestProxy.h"
#include "nsIURI.h"
#include "SVGImageContext.h"
#include "TextDrawTarget.h"
#include "mozilla/layers/WebRenderBridgeChild.h"
#include <algorithm>
#ifdef ACCESSIBILITY
# include "nsAccessibilityService.h"
#endif
using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::image;
using namespace mozilla::layout;
using mozilla::dom::Document;
nsIFrame* NS_NewBulletFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
return new (aPresShell) nsBulletFrame(aStyle, aPresShell->GetPresContext());
}
NS_DECLARE_FRAME_PROPERTY_SMALL_VALUE(FontSizeInflationProperty, float)
NS_IMPL_FRAMEARENA_HELPERS(nsBulletFrame)
#ifdef DEBUG
NS_QUERYFRAME_HEAD(nsBulletFrame)
NS_QUERYFRAME_ENTRY(nsBulletFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsFrame)
#endif
nsBulletFrame::~nsBulletFrame() {}
CounterStyle* nsBulletFrame::ResolveCounterStyle() {
return PresContext()->CounterStyleManager()->ResolveCounterStyle(
StyleList()->mCounterStyle);
}
void nsBulletFrame::DestroyFrom(nsIFrame* aDestructRoot,
PostDestroyData& aPostDestroyData) {
// Stop image loading first.
DeregisterAndCancelImageRequest();
if (mListener) {
mListener->SetFrame(nullptr);
}
// Let base class do the rest
nsFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
}
#ifdef DEBUG_FRAME_DUMP
nsresult nsBulletFrame::GetFrameName(nsAString& aResult) const {
return MakeFrameName(NS_LITERAL_STRING("Bullet"), aResult);
}
#endif
bool nsBulletFrame::IsEmpty() { return IsSelfEmpty(); }
bool nsBulletFrame::IsSelfEmpty() {
return StyleList()->mCounterStyle.IsNone();
}
/* virtual */
void nsBulletFrame::DidSetComputedStyle(ComputedStyle* aOldComputedStyle) {
nsFrame::DidSetComputedStyle(aOldComputedStyle);
imgRequestProxy* newRequest = StyleList()->GetListStyleImage();
if (newRequest) {
if (!mListener) {
mListener = new nsBulletListener();
mListener->SetFrame(this);
}
bool needNewRequest = true;
if (mImageRequest) {
// Reload the image, maybe...
nsCOMPtr<nsIURI> oldURI;
mImageRequest->GetURI(getter_AddRefs(oldURI));
nsCOMPtr<nsIURI> newURI;
newRequest->GetURI(getter_AddRefs(newURI));
if (oldURI && newURI) {
bool same;
newURI->Equals(oldURI, &same);
if (same) {
needNewRequest = false;
}
}
}
if (needNewRequest) {
RefPtr<imgRequestProxy> newRequestClone;
newRequest->SyncClone(mListener, PresContext()->Document(),
getter_AddRefs(newRequestClone));
// Deregister the old request. We wait until after Clone is done in case
// the old request and the new request are the same underlying image
// accessed via different URLs.
DeregisterAndCancelImageRequest();
// Register the new request.
mImageRequest = std::move(newRequestClone);
RegisterImageRequest(/* aKnownToBeAnimated = */ false);
// Image bullets can affect the layout of the page, so boost the image
// load priority.
mImageRequest->BoostPriority(imgIRequest::CATEGORY_SIZE_QUERY);
}
} else {
// No image request on the new ComputedStyle.
DeregisterAndCancelImageRequest();
}
#ifdef ACCESSIBILITY
// Update the list bullet accessible. If old style list isn't available then
// no need to update the accessible tree because it's not created yet.
if (aOldComputedStyle) {
if (nsAccessibilityService* accService =
PresShell::GetAccessibilityService()) {
const nsStyleList* oldStyleList = aOldComputedStyle->StyleList();
bool hadBullet = oldStyleList->GetListStyleImage() ||
!oldStyleList->mCounterStyle.IsNone();
const nsStyleList* newStyleList = StyleList();
bool hasBullet = newStyleList->GetListStyleImage() ||
!newStyleList->mCounterStyle.IsNone();
if (hadBullet != hasBullet) {
nsIContent* listItem = mContent->GetParent();
accService->UpdateListBullet(PresContext()->GetPresShell(), listItem,
hasBullet);
}
}
}
#endif // #ifdef ACCESSIBILITY
}
class nsDisplayBulletGeometry
: public nsDisplayItemGenericGeometry,
public nsImageGeometryMixin<nsDisplayBulletGeometry> {
public:
nsDisplayBulletGeometry(nsDisplayItem* aItem, nsDisplayListBuilder* aBuilder)
: nsDisplayItemGenericGeometry(aItem, aBuilder),
nsImageGeometryMixin(aItem, aBuilder) {
nsBulletFrame* f = static_cast<nsBulletFrame*>(aItem->Frame());
mOrdinal = f->Ordinal();
}
virtual bool InvalidateForSyncDecodeImages() const override {
return ShouldInvalidateToSyncDecodeImages();
}
int32_t mOrdinal;
};
class BulletRenderer final {
public:
BulletRenderer(imgIContainer* image, const nsRect& dest)
: mImage(image),
mDest(dest),
mColor(NS_RGBA(0, 0, 0, 0)),
mListStyleType(NS_STYLE_LIST_STYLE_NONE) {
MOZ_ASSERT(IsImageType());
}
BulletRenderer(Path* path, nscolor color, int32_t listStyleType)
: mColor(color), mPath(path), mListStyleType(listStyleType) {
MOZ_ASSERT(IsPathType());
}
BulletRenderer(const LayoutDeviceRect& aPathRect, nscolor color,
int32_t listStyleType)
: mPathRect(aPathRect), mColor(color), mListStyleType(listStyleType) {
MOZ_ASSERT(IsPathType());
}
BulletRenderer(const nsString& text, nsFontMetrics* fm, nscolor color,
const nsPoint& point, int32_t listStyleType)
: mColor(color),
mText(text),
mFontMetrics(fm),
mPoint(point),
mListStyleType(listStyleType) {
MOZ_ASSERT(IsTextType());
}
ImgDrawResult CreateWebRenderCommands(
nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
wr::IpcResourceUpdateQueue& aResources,
const layers::StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder);
ImgDrawResult Paint(gfxContext& aRenderingContext, nsPoint aPt,
const nsRect& aDirtyRect, uint32_t aFlags,
bool aDisableSubpixelAA, nsIFrame* aFrame);
bool IsImageType() const {
return mListStyleType == NS_STYLE_LIST_STYLE_NONE && mImage;
}
bool IsPathType() const {
return mListStyleType == NS_STYLE_LIST_STYLE_DISC ||
mListStyleType == NS_STYLE_LIST_STYLE_CIRCLE ||
mListStyleType == NS_STYLE_LIST_STYLE_SQUARE ||
mListStyleType == NS_STYLE_LIST_STYLE_DISCLOSURE_OPEN ||
mListStyleType == NS_STYLE_LIST_STYLE_DISCLOSURE_CLOSED;
}
bool IsTextType() const {
return mListStyleType != NS_STYLE_LIST_STYLE_NONE &&
mListStyleType != NS_STYLE_LIST_STYLE_DISC &&
mListStyleType != NS_STYLE_LIST_STYLE_CIRCLE &&
mListStyleType != NS_STYLE_LIST_STYLE_SQUARE &&
mListStyleType != NS_STYLE_LIST_STYLE_DISCLOSURE_OPEN &&
mListStyleType != NS_STYLE_LIST_STYLE_DISCLOSURE_CLOSED &&
!mText.IsEmpty();
}
void PaintTextToContext(nsIFrame* aFrame, gfxContext* aCtx,
bool aDisableSubpixelAA);
bool IsImageContainerAvailable(layers::LayerManager* aManager,
uint32_t aFlags);
private:
ImgDrawResult CreateWebRenderCommandsForImage(
nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
wr::IpcResourceUpdateQueue& aResources,
const layers::StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder);
bool CreateWebRenderCommandsForPath(
nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
wr::IpcResourceUpdateQueue& aResources,
const layers::StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder);
bool CreateWebRenderCommandsForText(
nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
wr::IpcResourceUpdateQueue& aResources,
const layers::StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder);
private:
// mImage and mDest are the properties for list-style-image.
// mImage is the image content and mDest is the image position.
RefPtr<imgIContainer> mImage;
nsRect mDest;
// Some bullet types are stored as a rect (in device pixels) instead of a Path
// to allow generating proper WebRender commands. When webrender is disabled
// the Path is lazily created for these items before painting.
// TODO: The size of this structure doesn't seem to be an issue since it has
// so many fields that are specific to a bullet style or another, but if it
// becomes one we can easily store mDest and mPathRect into the same memory
// location since they are never used by the same bullet types.
LayoutDeviceRect mPathRect;
// mColor indicate the color of list-style. Both text and path type would use
// this member.
nscolor mColor;
// mPath record the path of the list-style for later drawing.
// Included following types: square, circle, disc, disclosure open and
// disclosure closed.
RefPtr<Path> mPath;
// mText, mFontMertrics, mPoint, mFont and mGlyphs are for other
// list-style-type which can be drawed by text.
nsString mText;
RefPtr<nsFontMetrics> mFontMetrics;
nsPoint mPoint;
RefPtr<ScaledFont> mFont;
nsTArray<layers::GlyphArray> mGlyphs;
// Store the type of list-style-type.
int32_t mListStyleType;
};
ImgDrawResult BulletRenderer::CreateWebRenderCommands(
nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
wr::IpcResourceUpdateQueue& aResources,
const layers::StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) {
if (IsImageType()) {
return CreateWebRenderCommandsForImage(aItem, aBuilder, aResources, aSc,
aManager, aDisplayListBuilder);
}
bool success;
if (IsPathType()) {
success = CreateWebRenderCommandsForPath(aItem, aBuilder, aResources, aSc,
aManager, aDisplayListBuilder);
} else {
MOZ_ASSERT(IsTextType());
success = CreateWebRenderCommandsForText(aItem, aBuilder, aResources, aSc,
aManager, aDisplayListBuilder);
}
return success ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_SUPPORTED;
}
ImgDrawResult BulletRenderer::Paint(gfxContext& aRenderingContext, nsPoint aPt,
const nsRect& aDirtyRect, uint32_t aFlags,
bool aDisableSubpixelAA, nsIFrame* aFrame) {
if (IsImageType()) {
SamplingFilter filter = nsLayoutUtils::GetSamplingFilterForFrame(aFrame);
return nsLayoutUtils::DrawSingleImage(
aRenderingContext, aFrame->PresContext(), mImage, filter, mDest,
aDirtyRect,
/* no SVGImageContext */ Nothing(), aFlags);
}
if (IsPathType()) {
DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
if (!mPath) {
RefPtr<PathBuilder> builder = drawTarget->CreatePathBuilder();
switch (mListStyleType) {
case NS_STYLE_LIST_STYLE_CIRCLE:
case NS_STYLE_LIST_STYLE_DISC:
AppendEllipseToPath(builder, mPathRect.Center().ToUnknownPoint(),
mPathRect.Size().ToUnknownSize());
break;
case NS_STYLE_LIST_STYLE_SQUARE:
AppendRectToPath(builder, mPathRect.ToUnknownRect());
break;
default:
MOZ_ASSERT(false, "Should have a parth.");
}
mPath = builder->Finish();
}
switch (mListStyleType) {
case NS_STYLE_LIST_STYLE_CIRCLE:
drawTarget->Stroke(mPath, ColorPattern(ToDeviceColor(mColor)));
break;
case NS_STYLE_LIST_STYLE_DISC:
case NS_STYLE_LIST_STYLE_SQUARE:
case NS_STYLE_LIST_STYLE_DISCLOSURE_CLOSED:
case NS_STYLE_LIST_STYLE_DISCLOSURE_OPEN:
drawTarget->Fill(mPath, ColorPattern(ToDeviceColor(mColor)));
break;
default:
MOZ_CRASH("unreachable");
}
}
if (IsTextType()) {
PaintTextToContext(aFrame, &aRenderingContext, aDisableSubpixelAA);
}
return ImgDrawResult::SUCCESS;
}
void BulletRenderer::PaintTextToContext(nsIFrame* aFrame, gfxContext* aCtx,
bool aDisableSubpixelAA) {
MOZ_ASSERT(IsTextType());
DrawTarget* drawTarget = aCtx->GetDrawTarget();
DrawTargetAutoDisableSubpixelAntialiasing disable(drawTarget,
aDisableSubpixelAA);
aCtx->SetColor(Color::FromABGR(mColor));
nsPresContext* presContext = aFrame->PresContext();
if (!presContext->BidiEnabled() && HasRTLChars(mText)) {
presContext->SetBidiEnabled();
}
nsLayoutUtils::DrawString(aFrame, *mFontMetrics, aCtx, mText.get(),
mText.Length(), mPoint);
}
bool BulletRenderer::IsImageContainerAvailable(layers::LayerManager* aManager,
uint32_t aFlags) {
MOZ_ASSERT(IsImageType());
return mImage->IsImageContainerAvailable(aManager, aFlags);
}
ImgDrawResult BulletRenderer::CreateWebRenderCommandsForImage(
nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
wr::IpcResourceUpdateQueue& aResources,
const layers::StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) {
MOZ_RELEASE_ASSERT(IsImageType());
MOZ_RELEASE_ASSERT(mImage);
uint32_t flags = imgIContainer::FLAG_ASYNC_NOTIFY;
if (aDisplayListBuilder->IsPaintingToWindow()) {
flags |= imgIContainer::FLAG_HIGH_QUALITY_SCALING;
}
if (aDisplayListBuilder->ShouldSyncDecodeImages()) {
flags |= imgIContainer::FLAG_SYNC_DECODE;
}
const int32_t appUnitsPerDevPixel =
aItem->Frame()->PresContext()->AppUnitsPerDevPixel();
LayoutDeviceRect destRect =
LayoutDeviceRect::FromAppUnits(mDest, appUnitsPerDevPixel);
Maybe<SVGImageContext> svgContext;
gfx::IntSize decodeSize =
nsLayoutUtils::ComputeImageContainerDrawingParameters(
mImage, aItem->Frame(), destRect, aSc, flags, svgContext);
RefPtr<layers::ImageContainer> container;
ImgDrawResult drawResult = mImage->GetImageContainerAtSize(
aManager->LayerManager(), decodeSize, svgContext, flags,
getter_AddRefs(container));
if (!container) {
return drawResult;
}
mozilla::wr::ImageRendering rendering = wr::ToImageRendering(
nsLayoutUtils::GetSamplingFilterForFrame(aItem->Frame()));
gfx::IntSize size;
Maybe<wr::ImageKey> key = aManager->CommandBuilder().CreateImageKey(
aItem, container, aBuilder, aResources, rendering, aSc, size, Nothing());
if (key.isNothing()) {
return drawResult;
}
wr::LayoutRect dest = wr::ToLayoutRect(destRect);
aBuilder.PushImage(dest, dest, !aItem->BackfaceIsHidden(), rendering,
key.value());
return drawResult;
}
bool BulletRenderer::CreateWebRenderCommandsForPath(
nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
wr::IpcResourceUpdateQueue& aResources,
const layers::StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) {
MOZ_ASSERT(IsPathType());
wr::LayoutRect dest = wr::ToLayoutRect(mPathRect);
auto color = wr::ToColorF(ToDeviceColor(mColor));
bool isBackfaceVisible = !aItem->BackfaceIsHidden();
switch (mListStyleType) {
case NS_STYLE_LIST_STYLE_CIRCLE: {
LayoutDeviceSize radii = mPathRect.Size() / 2.0;
auto borderWidths = wr::ToBorderWidths(1.0, 1.0, 1.0, 1.0);
wr::BorderSide side = {color, wr::BorderStyle::Solid};
wr::BorderSide sides[4] = {side, side, side, side};
Range<const wr::BorderSide> sidesRange(sides, 4);
aBuilder.PushBorder(dest, dest, isBackfaceVisible, borderWidths,
sidesRange,
wr::ToBorderRadius(radii, radii, radii, radii));
return true;
}
case NS_STYLE_LIST_STYLE_DISC: {
aBuilder.PushRoundedRect(dest, dest, isBackfaceVisible, color);
return true;
}
case NS_STYLE_LIST_STYLE_SQUARE: {
aBuilder.PushRect(dest, dest, isBackfaceVisible, color);
return true;
}
default:
if (!aManager->CommandBuilder().PushItemAsImage(
aItem, aBuilder, aResources, aSc, aDisplayListBuilder)) {
NS_WARNING("Fail to create WebRender commands for Bullet path.");
return false;
}
}
return true;
}
bool BulletRenderer::CreateWebRenderCommandsForText(
nsDisplayItem* aItem, wr::DisplayListBuilder& aBuilder,
wr::IpcResourceUpdateQueue& aResources,
const layers::StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) {
MOZ_ASSERT(IsTextType());
bool dummy;
nsRect bounds = aItem->GetBounds(aDisplayListBuilder, &dummy);
if (bounds.IsEmpty()) {
return true;
}
RefPtr<TextDrawTarget> textDrawer =
new TextDrawTarget(aBuilder, aResources, aSc, aManager, aItem, bounds);
RefPtr<gfxContext> captureCtx = gfxContext::CreateOrNull(textDrawer);
PaintTextToContext(aItem->Frame(), captureCtx, aItem->IsSubpixelAADisabled());
textDrawer->TerminateShadows();
return textDrawer->Finish();
}
class nsDisplayBullet final : public nsPaintedDisplayItem {
public:
nsDisplayBullet(nsDisplayListBuilder* aBuilder, nsBulletFrame* aFrame)
: nsPaintedDisplayItem(aBuilder, aFrame) {
MOZ_COUNT_CTOR(nsDisplayBullet);
}
#ifdef NS_BUILD_REFCNT_LOGGING
virtual ~nsDisplayBullet() { MOZ_COUNT_DTOR(nsDisplayBullet); }
#endif
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder,
bool* aSnap) const override {
*aSnap = false;
return mFrame->GetVisualOverflowRectRelativeToSelf() + ToReferenceFrame();
}
virtual bool CreateWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder,
mozilla::wr::IpcResourceUpdateQueue&, const StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) override;
virtual void HitTest(nsDisplayListBuilder* aBuilder, const nsRect& aRect,
HitTestState* aState,
nsTArray<nsIFrame*>* aOutFrames) override {
aOutFrames->AppendElement(mFrame);
}
virtual void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override;
NS_DISPLAY_DECL_NAME("Bullet", TYPE_BULLET)
virtual nsRect GetComponentAlphaBounds(
nsDisplayListBuilder* aBuilder) const override {
bool snap;
return GetBounds(aBuilder, &snap);
}
virtual nsDisplayItemGeometry* AllocateGeometry(
nsDisplayListBuilder* aBuilder) override {
return new nsDisplayBulletGeometry(this, aBuilder);
}
virtual void ComputeInvalidationRegion(
nsDisplayListBuilder* aBuilder, const nsDisplayItemGeometry* aGeometry,
nsRegion* aInvalidRegion) const override {
const nsDisplayBulletGeometry* geometry =
static_cast<const nsDisplayBulletGeometry*>(aGeometry);
nsBulletFrame* f = static_cast<nsBulletFrame*>(mFrame);
if (f->Ordinal() != geometry->mOrdinal) {
bool snap;
aInvalidRegion->Or(geometry->mBounds, GetBounds(aBuilder, &snap));
return;
}
nsCOMPtr<imgIContainer> image = f->GetImage();
if (aBuilder->ShouldSyncDecodeImages() && image &&
geometry->ShouldInvalidateToSyncDecodeImages()) {
bool snap;
aInvalidRegion->Or(*aInvalidRegion, GetBounds(aBuilder, &snap));
}
return nsPaintedDisplayItem::ComputeInvalidationRegion(aBuilder, aGeometry,
aInvalidRegion);
}
protected:
Maybe<BulletRenderer> mBulletRenderer;
};
bool nsDisplayBullet::CreateWebRenderCommands(
wr::DisplayListBuilder& aBuilder, wr::IpcResourceUpdateQueue& aResources,
const StackingContextHelper& aSc,
mozilla::layers::RenderRootStateManager* aManager,
nsDisplayListBuilder* aDisplayListBuilder) {
// FIXME: avoid needing to make this target if we're drawing text
// (non-trivial refactor of all this code)
RefPtr<gfxContext> screenRefCtx = gfxContext::CreateOrNull(
gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget().get());
Maybe<BulletRenderer> br =
static_cast<nsBulletFrame*>(mFrame)->CreateBulletRenderer(
*screenRefCtx, ToReferenceFrame());
if (!br) {
return false;
}
ImgDrawResult drawResult = br->CreateWebRenderCommands(
this, aBuilder, aResources, aSc, aManager, aDisplayListBuilder);
if (drawResult == ImgDrawResult::NOT_SUPPORTED) {
return false;
}
nsDisplayBulletGeometry::UpdateDrawResult(this, drawResult);
return true;
}
void nsDisplayBullet::Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) {
uint32_t flags = imgIContainer::FLAG_NONE;
if (aBuilder->ShouldSyncDecodeImages()) {
flags |= imgIContainer::FLAG_SYNC_DECODE;
}
ImgDrawResult result = static_cast<nsBulletFrame*>(mFrame)->PaintBullet(
*aCtx, ToReferenceFrame(), GetPaintRect(), flags, IsSubpixelAADisabled());
nsDisplayBulletGeometry::UpdateDrawResult(this, result);
}
void nsBulletFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsDisplayListSet& aLists) {
if (!IsVisibleForPainting()) return;
DO_GLOBAL_REFLOW_COUNT_DSP("nsBulletFrame");
aLists.Content()->AppendNewToTop<nsDisplayBullet>(aBuilder, this);
}
Maybe<BulletRenderer> nsBulletFrame::CreateBulletRenderer(
gfxContext& aRenderingContext, nsPoint aPt) {
const nsStyleList* myList = StyleList();
CounterStyle* listStyleType = ResolveCounterStyle();
nsMargin padding = mPadding.GetPhysicalMargin(GetWritingMode());
if (myList->GetListStyleImage() && mImageRequest) {
uint32_t status;
mImageRequest->GetImageStatus(&status);
if (!(status & imgIRequest::STATUS_ERROR)) {
if (status & imgIRequest::STATUS_LOAD_COMPLETE) {
nsCOMPtr<imgIContainer> imageCon;
mImageRequest->GetImage(getter_AddRefs(imageCon));
if (imageCon) {
nsRect dest(padding.left, padding.top,
mRect.width - (padding.left + padding.right),
mRect.height - (padding.top + padding.bottom));
BulletRenderer br(imageCon, dest + aPt);
return Some(br);
}
} else {
// Boost the load priority further now that we know we want to display
// the bullet image.
mImageRequest->BoostPriority(imgIRequest::CATEGORY_DISPLAY);
}
}
}
nscolor color = nsLayoutUtils::GetColor(this, &nsStyleText::mColor);
DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
switch (listStyleType->GetStyle()) {
case NS_STYLE_LIST_STYLE_NONE:
return Nothing();
case NS_STYLE_LIST_STYLE_DISC:
case NS_STYLE_LIST_STYLE_CIRCLE: {
nsRect rect(padding.left + aPt.x, padding.top + aPt.y,
mRect.width - (padding.left + padding.right),
mRect.height - (padding.top + padding.bottom));
auto devPxRect =
LayoutDeviceRect::FromAppUnits(rect, appUnitsPerDevPixel);
return Some(BulletRenderer(devPxRect, color, listStyleType->GetStyle()));
}
case NS_STYLE_LIST_STYLE_SQUARE: {
nsRect rect(aPt, mRect.Size());
rect.Deflate(padding);
// Snap the height and the width of the rectangle to device pixels,
// and then center the result within the original rectangle, so that
// all square bullets at the same font size have the same visual
// size (bug 376690).
// FIXME: We should really only do this if we're not transformed
// (like gfxContext::UserToDevicePixelSnapped does).
nsPresContext* pc = PresContext();
nsRect snapRect(rect.x, rect.y,
pc->RoundAppUnitsToNearestDevPixels(rect.width),
pc->RoundAppUnitsToNearestDevPixels(rect.height));
snapRect.MoveBy((rect.width - snapRect.width) / 2,
(rect.height - snapRect.height) / 2);
auto devPxRect =
LayoutDeviceRect::FromAppUnits(snapRect, appUnitsPerDevPixel);
return Some(BulletRenderer(devPxRect, color, listStyleType->GetStyle()));
}
case NS_STYLE_LIST_STYLE_DISCLOSURE_CLOSED:
case NS_STYLE_LIST_STYLE_DISCLOSURE_OPEN: {
nsRect rect(aPt, mRect.Size());
rect.Deflate(padding);
WritingMode wm = GetWritingMode();
bool isVertical = wm.IsVertical();
bool isClosed =
listStyleType->GetStyle() == NS_STYLE_LIST_STYLE_DISCLOSURE_CLOSED;
bool isDown = (!isVertical && !isClosed) || (isVertical && isClosed);
nscoord diff = NSToCoordRound(0.1f * rect.height);
if (isDown) {
rect.y += diff * 2;
rect.height -= diff * 2;
} else {
rect.Deflate(diff, 0);
}
nsPresContext* pc = PresContext();
rect.x = pc->RoundAppUnitsToNearestDevPixels(rect.x);
rect.y = pc->RoundAppUnitsToNearestDevPixels(rect.y);
RefPtr<PathBuilder> builder = drawTarget->CreatePathBuilder();
if (isDown) {
// to bottom
builder->MoveTo(NSPointToPoint(rect.TopLeft(), appUnitsPerDevPixel));
builder->LineTo(NSPointToPoint(rect.TopRight(), appUnitsPerDevPixel));
builder->LineTo(NSPointToPoint(
(rect.BottomLeft() + rect.BottomRight()) / 2, appUnitsPerDevPixel));
} else {
bool isLR = isVertical ? wm.IsVerticalLR() : wm.IsBidiLTR();
if (isLR) {
// to right
builder->MoveTo(NSPointToPoint(rect.TopLeft(), appUnitsPerDevPixel));
builder->LineTo(NSPointToPoint(
(rect.TopRight() + rect.BottomRight()) / 2, appUnitsPerDevPixel));
builder->LineTo(
NSPointToPoint(rect.BottomLeft(), appUnitsPerDevPixel));
} else {
// to left
builder->MoveTo(NSPointToPoint(rect.TopRight(), appUnitsPerDevPixel));
builder->LineTo(
NSPointToPoint(rect.BottomRight(), appUnitsPerDevPixel));
builder->LineTo(NSPointToPoint(
(rect.TopLeft() + rect.BottomLeft()) / 2, appUnitsPerDevPixel));
}
}
RefPtr<Path> path = builder->Finish();
BulletRenderer br(path, color, listStyleType->GetStyle());
return Some(br);
}
default: {
RefPtr<nsFontMetrics> fm =
nsLayoutUtils::GetFontMetricsForFrame(this, GetFontSizeInflation());
nsAutoString text;
WritingMode wm = GetWritingMode();
GetListItemText(listStyleType, wm, Ordinal(), text);
nscoord ascent = wm.IsLineInverted() ? fm->MaxDescent() : fm->MaxAscent();
aPt.MoveBy(padding.left, padding.top);
if (wm.IsVertical()) {
if (wm.IsVerticalLR()) {
aPt.x = NSToCoordRound(nsLayoutUtils::GetSnappedBaselineX(
this, &aRenderingContext, aPt.x, ascent));
} else {
aPt.x = NSToCoordRound(nsLayoutUtils::GetSnappedBaselineX(
this, &aRenderingContext, aPt.x + mRect.width, -ascent));
}
} else {
aPt.y = NSToCoordRound(nsLayoutUtils::GetSnappedBaselineY(
this, &aRenderingContext, aPt.y, ascent));
}
BulletRenderer br(text, fm, color, aPt, listStyleType->GetStyle());
return Some(br);
}
}
MOZ_CRASH("unreachable");
return Nothing();
}
ImgDrawResult nsBulletFrame::PaintBullet(gfxContext& aRenderingContext,
nsPoint aPt, const nsRect& aDirtyRect,
uint32_t aFlags,
bool aDisableSubpixelAA) {
Maybe<BulletRenderer> br = CreateBulletRenderer(aRenderingContext, aPt);
if (!br) {
return ImgDrawResult::SUCCESS;
}
return br->Paint(aRenderingContext, aPt, aDirtyRect, aFlags,
aDisableSubpixelAA, this);
}
void nsBulletFrame::GetListItemText(CounterStyle* aStyle,
mozilla::WritingMode aWritingMode,
int32_t aOrdinal, nsAString& aResult) {
NS_ASSERTION(
aStyle->GetStyle() != NS_STYLE_LIST_STYLE_NONE &&
aStyle->GetStyle() != NS_STYLE_LIST_STYLE_DISC &&
aStyle->GetStyle() != NS_STYLE_LIST_STYLE_CIRCLE &&
aStyle->GetStyle() != NS_STYLE_LIST_STYLE_SQUARE &&
aStyle->GetStyle() != NS_STYLE_LIST_STYLE_DISCLOSURE_CLOSED &&
aStyle->GetStyle() != NS_STYLE_LIST_STYLE_DISCLOSURE_OPEN,
"we should be using specialized code for these types");
bool isRTL;
nsAutoString counter, prefix, suffix;
aStyle->GetPrefix(prefix);
aStyle->GetSuffix(suffix);
aStyle->GetCounterText(aOrdinal, aWritingMode, counter, isRTL);
aResult.Truncate();
aResult.Append(prefix);
if (aWritingMode.IsBidiLTR() != isRTL) {
aResult.Append(counter);
} else {
// RLM = 0x200f, LRM = 0x200e
char16_t mark = isRTL ? 0x200f : 0x200e;
aResult.Append(mark);
aResult.Append(counter);
aResult.Append(mark);
}
aResult.Append(suffix);
}
#define MIN_BULLET_SIZE 1
void nsBulletFrame::AppendSpacingToPadding(nsFontMetrics* aFontMetrics,
LogicalMargin* aPadding) {
aPadding->IEnd(GetWritingMode()) += aFontMetrics->EmHeight() / 2;
}
void nsBulletFrame::GetDesiredSize(nsPresContext* aCX,
gfxContext* aRenderingContext,
ReflowOutput& aMetrics,
float aFontSizeInflation,
LogicalMargin* aPadding) {
// Reset our padding. If we need it, we'll set it below.
WritingMode wm = GetWritingMode();
aPadding->SizeTo(wm, 0, 0, 0, 0);
LogicalSize finalSize(wm);
const nsStyleList* myList = StyleList();
nscoord ascent;
RefPtr<nsFontMetrics> fm =
nsLayoutUtils::GetFontMetricsForFrame(this, aFontSizeInflation);
RemoveStateBits(BULLET_FRAME_IMAGE_LOADING);
if (myList->GetListStyleImage() && mImageRequest) {
uint32_t status;
mImageRequest->GetImageStatus(&status);
if (status & imgIRequest::STATUS_SIZE_AVAILABLE &&
!(status & imgIRequest::STATUS_ERROR)) {
// auto size the image
finalSize.ISize(wm) = mIntrinsicSize.ISize(wm);
aMetrics.SetBlockStartAscent(finalSize.BSize(wm) =
mIntrinsicSize.BSize(wm));
aMetrics.SetSize(wm, finalSize);
AppendSpacingToPadding(fm, aPadding);
AddStateBits(BULLET_FRAME_IMAGE_LOADING);
return;
}
}
// If we're getting our desired size and don't have an image, reset
// mIntrinsicSize to (0,0). Otherwise, if we used to have an image, it
// changed, and the new one is coming in, but we're reflowing before it's
// fully there, we'll end up with mIntrinsicSize not matching our size, but
// won't trigger a reflow in OnStartContainer (because mIntrinsicSize will
// match the image size).
mIntrinsicSize.SizeTo(wm, 0, 0);
nscoord bulletSize;
nsAutoString text;
CounterStyle* style = ResolveCounterStyle();
switch (style->GetStyle()) {
case NS_STYLE_LIST_STYLE_NONE:
finalSize.ISize(wm) = finalSize.BSize(wm) = 0;
aMetrics.SetBlockStartAscent(0);
break;
case NS_STYLE_LIST_STYLE_DISC:
case NS_STYLE_LIST_STYLE_CIRCLE:
case NS_STYLE_LIST_STYLE_SQUARE: {
ascent = fm->MaxAscent();
bulletSize = std::max(nsPresContext::CSSPixelsToAppUnits(MIN_BULLET_SIZE),
NSToCoordRound(0.8f * (float(ascent) / 2.0f)));
aPadding->BEnd(wm) = NSToCoordRound(float(ascent) / 8.0f);
finalSize.ISize(wm) = finalSize.BSize(wm) = bulletSize;
aMetrics.SetBlockStartAscent(bulletSize + aPadding->BEnd(wm));
AppendSpacingToPadding(fm, aPadding);
break;
}
case NS_STYLE_LIST_STYLE_DISCLOSURE_CLOSED:
case NS_STYLE_LIST_STYLE_DISCLOSURE_OPEN:
ascent = fm->EmAscent();
bulletSize = std::max(nsPresContext::CSSPixelsToAppUnits(MIN_BULLET_SIZE),
NSToCoordRound(0.75f * ascent));
aPadding->BEnd(wm) = NSToCoordRound(0.125f * ascent);
finalSize.ISize(wm) = finalSize.BSize(wm) = bulletSize;
if (!wm.IsVertical()) {
aMetrics.SetBlockStartAscent(bulletSize + aPadding->BEnd(wm));
}
AppendSpacingToPadding(fm, aPadding);
break;
default:
GetListItemText(style, wm, Ordinal(), text);
finalSize.BSize(wm) = fm->MaxHeight();
finalSize.ISize(wm) = nsLayoutUtils::AppUnitWidthOfStringBidi(
text, this, *fm, *aRenderingContext);
aMetrics.SetBlockStartAscent(wm.IsLineInverted() ? fm->MaxDescent()
: fm->MaxAscent());
break;
}
aMetrics.SetSize(wm, finalSize);
}
void nsBulletFrame::Reflow(nsPresContext* aPresContext, ReflowOutput& aMetrics,
const ReflowInput& aReflowInput,
nsReflowStatus& aStatus) {
MarkInReflow();
DO_GLOBAL_REFLOW_COUNT("nsBulletFrame");
DISPLAY_REFLOW(aPresContext, this, aReflowInput, aMetrics, aStatus);
MOZ_ASSERT(aStatus.IsEmpty(), "The reflow status should be empty!");
float inflation = nsLayoutUtils::FontSizeInflationFor(this);
SetFontSizeInflation(inflation);
// Get the base size
GetDesiredSize(aPresContext, aReflowInput.mRenderingContext, aMetrics,
inflation, &mPadding);
// Add in the border and padding; split the top/bottom between the
// ascent and descent to make things look nice
WritingMode wm = aReflowInput.GetWritingMode();
const LogicalMargin& bp = aReflowInput.ComputedLogicalBorderPadding();
mPadding.BStart(wm) += NSToCoordRound(bp.BStart(wm) * inflation);
mPadding.IEnd(wm) += NSToCoordRound(bp.IEnd(wm) * inflation);
mPadding.BEnd(wm) += NSToCoordRound(bp.BEnd(wm) * inflation);
mPadding.IStart(wm) += NSToCoordRound(bp.IStart(wm) * inflation);
WritingMode lineWM = aMetrics.GetWritingMode();
LogicalMargin linePadding = mPadding.ConvertTo(lineWM, wm);
aMetrics.ISize(lineWM) += linePadding.IStartEnd(lineWM);
aMetrics.BSize(lineWM) += linePadding.BStartEnd(lineWM);
aMetrics.SetBlockStartAscent(aMetrics.BlockStartAscent() +
linePadding.BStart(lineWM));
// XXX this is a bit of a hack, we're assuming that no glyphs used for bullets
// overflow their font-boxes. It'll do for now; to fix it for real, we really
// should rewrite all the text-handling code here to use gfxTextRun (bug
// 397294).
aMetrics.SetOverflowAreasToDesiredBounds();
NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aMetrics);
}
/* virtual */
nscoord nsBulletFrame::GetMinISize(gfxContext* aRenderingContext) {
WritingMode wm = GetWritingMode();
ReflowOutput reflowOutput(wm);