forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsCSSRendering.cpp
4839 lines (4347 loc) · 182 KB
/
nsCSSRendering.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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// vim:cindent:ts=2:et:sw=2:
/* 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/. */
/* utility functions for drawing borders and backgrounds */
#include "nsStyleConsts.h"
#include "nsPresContext.h"
#include "nsIFrame.h"
#include "nsPoint.h"
#include "nsRect.h"
#include "nsIViewManager.h"
#include "nsIPresShell.h"
#include "nsFrameManager.h"
#include "nsStyleContext.h"
#include "nsGkAtoms.h"
#include "nsCSSAnonBoxes.h"
#include "nsTransform2D.h"
#include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIScrollableFrame.h"
#include "imgIRequest.h"
#include "imgIContainer.h"
#include "nsCSSRendering.h"
#include "nsCSSColorUtils.h"
#include "nsITheme.h"
#include "nsThemeConstants.h"
#include "nsIServiceManager.h"
#include "nsLayoutUtils.h"
#include "nsINameSpaceManager.h"
#include "nsBlockFrame.h"
#include "gfxContext.h"
#include "nsRenderingContext.h"
#include "nsIInterfaceRequestorUtils.h"
#include "gfxPlatform.h"
#include "gfxImageSurface.h"
#include "nsStyleStructInlines.h"
#include "nsCSSFrameConstructor.h"
#include "nsCSSProps.h"
#include "nsContentUtils.h"
#include "nsSVGEffects.h"
#include "nsSVGIntegrationUtils.h"
#include "gfxDrawable.h"
#include "sampler.h"
#include "nsCSSRenderingBorders.h"
#include "mozilla/css/ImageLoader.h"
#include "ImageContainer.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Types.h"
#include <ctime>
#include <cstdlib> // for std::abs(int/long)
#include <cmath> // for std::abs(float/double)
using namespace mozilla;
using namespace mozilla::css;
static int gFrameTreeLockCount = 0;
// To avoid storing this data on nsInlineFrame (bloat) and to avoid
// recalculating this for each frame in a continuation (perf), hold
// a cache of various coordinate information that we need in order
// to paint inline backgrounds.
struct InlineBackgroundData
{
InlineBackgroundData()
: mFrame(nullptr), mBlockFrame(nullptr)
{
}
~InlineBackgroundData()
{
}
void Reset()
{
mBoundingBox.SetRect(0,0,0,0);
mContinuationPoint = mLineContinuationPoint = mUnbrokenWidth = 0;
mFrame = mBlockFrame = nullptr;
}
nsRect GetContinuousRect(nsIFrame* aFrame)
{
SetFrame(aFrame);
nscoord x;
if (mBidiEnabled) {
x = mLineContinuationPoint;
// Scan continuations on the same line as aFrame and accumulate the widths
// of frames that are to the left (if this is an LTR block) or right
// (if it's RTL) of the current one.
bool isRtlBlock = (mBlockFrame->GetStyleVisibility()->mDirection ==
NS_STYLE_DIRECTION_RTL);
nscoord curOffset = aFrame->GetOffsetTo(mBlockFrame).x;
// No need to use our GetPrevContinuation/GetNextContinuation methods
// here, since ib special siblings are certainly not on the same line.
nsIFrame* inlineFrame = aFrame->GetPrevContinuation();
// If the continuation is fluid we know inlineFrame is not on the same line.
// If it's not fluid, we need to test further to be sure.
while (inlineFrame && !inlineFrame->GetNextInFlow() &&
AreOnSameLine(aFrame, inlineFrame)) {
nscoord frameXOffset = inlineFrame->GetOffsetTo(mBlockFrame).x;
if(isRtlBlock == (frameXOffset >= curOffset)) {
x += inlineFrame->GetSize().width;
}
inlineFrame = inlineFrame->GetPrevContinuation();
}
inlineFrame = aFrame->GetNextContinuation();
while (inlineFrame && !inlineFrame->GetPrevInFlow() &&
AreOnSameLine(aFrame, inlineFrame)) {
nscoord frameXOffset = inlineFrame->GetOffsetTo(mBlockFrame).x;
if(isRtlBlock == (frameXOffset >= curOffset)) {
x += inlineFrame->GetSize().width;
}
inlineFrame = inlineFrame->GetNextContinuation();
}
if (isRtlBlock) {
// aFrame itself is also to the right of its left edge, so add its width.
x += aFrame->GetSize().width;
// x is now the distance from the left edge of aFrame to the right edge
// of the unbroken content. Change it to indicate the distance from the
// left edge of the unbroken content to the left edge of aFrame.
x = mUnbrokenWidth - x;
}
} else {
x = mContinuationPoint;
}
// Assume background-origin: border and return a rect with offsets
// relative to (0,0). If we have a different background-origin,
// then our rect should be deflated appropriately by our caller.
return nsRect(-x, 0, mUnbrokenWidth, mFrame->GetSize().height);
}
nsRect GetBoundingRect(nsIFrame* aFrame)
{
SetFrame(aFrame);
// Move the offsets relative to (0,0) which puts the bounding box into
// our coordinate system rather than our parent's. We do this by
// moving it the back distance from us to the bounding box.
// This also assumes background-origin: border, so our caller will
// need to deflate us if needed.
nsRect boundingBox(mBoundingBox);
nsPoint point = mFrame->GetPosition();
boundingBox.MoveBy(-point.x, -point.y);
return boundingBox;
}
protected:
nsIFrame* mFrame;
nsBlockFrame* mBlockFrame;
nsRect mBoundingBox;
nscoord mContinuationPoint;
nscoord mUnbrokenWidth;
nscoord mLineContinuationPoint;
bool mBidiEnabled;
void SetFrame(nsIFrame* aFrame)
{
NS_PRECONDITION(aFrame, "Need a frame");
NS_ASSERTION(gFrameTreeLockCount > 0,
"Can't call this when frame tree is not locked");
if (aFrame == mFrame) {
return;
}
nsIFrame *prevContinuation = GetPrevContinuation(aFrame);
if (!prevContinuation || mFrame != prevContinuation) {
// Ok, we've got the wrong frame. We have to start from scratch.
Reset();
Init(aFrame);
return;
}
// Get our last frame's size and add its width to our continuation
// point before we cache the new frame.
mContinuationPoint += mFrame->GetSize().width;
// If this a new line, update mLineContinuationPoint.
if (mBidiEnabled &&
(aFrame->GetPrevInFlow() || !AreOnSameLine(mFrame, aFrame))) {
mLineContinuationPoint = mContinuationPoint;
}
mFrame = aFrame;
}
nsIFrame* GetPrevContinuation(nsIFrame* aFrame)
{
nsIFrame* prevCont = aFrame->GetPrevContinuation();
if (!prevCont && (aFrame->GetStateBits() & NS_FRAME_IS_SPECIAL)) {
nsIFrame* block = static_cast<nsIFrame*>
(aFrame->Properties().Get(nsIFrame::IBSplitSpecialPrevSibling()));
if (block) {
// The {ib} properties are only stored on first continuations
NS_ASSERTION(!block->GetPrevContinuation(),
"Incorrect value for IBSplitSpecialPrevSibling");
prevCont = static_cast<nsIFrame*>
(block->Properties().Get(nsIFrame::IBSplitSpecialPrevSibling()));
NS_ASSERTION(prevCont, "How did that happen?");
}
}
return prevCont;
}
nsIFrame* GetNextContinuation(nsIFrame* aFrame)
{
nsIFrame* nextCont = aFrame->GetNextContinuation();
if (!nextCont && (aFrame->GetStateBits() & NS_FRAME_IS_SPECIAL)) {
// The {ib} properties are only stored on first continuations
aFrame = aFrame->GetFirstContinuation();
nsIFrame* block = static_cast<nsIFrame*>
(aFrame->Properties().Get(nsIFrame::IBSplitSpecialSibling()));
if (block) {
nextCont = static_cast<nsIFrame*>
(block->Properties().Get(nsIFrame::IBSplitSpecialSibling()));
NS_ASSERTION(nextCont, "How did that happen?");
}
}
return nextCont;
}
void Init(nsIFrame* aFrame)
{
mBidiEnabled = aFrame->PresContext()->BidiEnabled();
if (mBidiEnabled) {
// Find the containing block frame
nsIFrame* frame = aFrame;
do {
frame = frame->GetParent();
mBlockFrame = do_QueryFrame(frame);
}
while (frame && frame->IsFrameOfType(nsIFrame::eLineParticipant));
NS_ASSERTION(mBlockFrame, "Cannot find containing block.");
}
// Start with the previous flow frame as our continuation point
// is the total of the widths of the previous frames.
nsIFrame* inlineFrame = GetPrevContinuation(aFrame);
while (inlineFrame) {
nsRect rect = inlineFrame->GetRect();
mContinuationPoint += rect.width;
if (mBidiEnabled && !AreOnSameLine(aFrame, inlineFrame)) {
mLineContinuationPoint += rect.width;
}
mUnbrokenWidth += rect.width;
mBoundingBox.UnionRect(mBoundingBox, rect);
inlineFrame = GetPrevContinuation(inlineFrame);
}
// Next add this frame and subsequent frames to the bounding box and
// unbroken width.
inlineFrame = aFrame;
while (inlineFrame) {
nsRect rect = inlineFrame->GetRect();
mUnbrokenWidth += rect.width;
mBoundingBox.UnionRect(mBoundingBox, rect);
inlineFrame = GetNextContinuation(inlineFrame);
}
mFrame = aFrame;
}
bool AreOnSameLine(nsIFrame* aFrame1, nsIFrame* aFrame2) {
bool isValid1, isValid2;
nsBlockInFlowLineIterator it1(mBlockFrame, aFrame1, &isValid1);
nsBlockInFlowLineIterator it2(mBlockFrame, aFrame2, &isValid2);
return isValid1 && isValid2 &&
// Make sure aFrame1 and aFrame2 are in the same continuation of
// mBlockFrame.
it1.GetContainer() == it2.GetContainer() &&
// And on the same line in it
it1.GetLine() == it2.GetLine();
}
};
struct GradientCacheKey : public PLDHashEntryHdr {
typedef const GradientCacheKey& KeyType;
typedef const GradientCacheKey* KeyTypePointer;
enum { ALLOW_MEMMOVE = true };
const nsRefPtr<nsStyleGradient> mGradient;
const gfxSize mGradientSize;
enum { SINGLE_CELL = 0x01 };
const uint32_t mFlags;
const gfx::BackendType mBackendType;
GradientCacheKey(nsStyleGradient* aGradient, const gfxSize& aGradientSize,
uint32_t aFlags, gfx::BackendType aBackendType)
: mGradient(aGradient), mGradientSize(aGradientSize), mFlags(aFlags),
mBackendType(aBackendType)
{ }
GradientCacheKey(const GradientCacheKey* aOther)
: mGradient(aOther->mGradient), mGradientSize(aOther->mGradientSize),
mFlags(aOther->mFlags), mBackendType(aOther->mBackendType)
{ }
static PLDHashNumber
HashKey(const KeyTypePointer aKey)
{
PLDHashNumber hash = 0;
hash = AddToHash(hash, aKey->mGradientSize.width);
hash = AddToHash(hash, aKey->mGradientSize.height);
hash = AddToHash(hash, aKey->mFlags);
hash = AddToHash(hash, aKey->mBackendType);
hash = aKey->mGradient->Hash(hash);
return hash;
}
bool KeyEquals(KeyTypePointer aKey) const
{
return (*aKey->mGradient == *mGradient) &&
(aKey->mGradientSize == mGradientSize) &&
(aKey->mBackendType == mBackendType) &&
(aKey->mFlags == mFlags);
}
static KeyTypePointer KeyToPointer(KeyType aKey)
{
return &aKey;
}
};
/**
* This class is what is cached. It need to be allocated in an object separated
* to the cache entry to be able to be tracked by the nsExpirationTracker.
* */
struct GradientCacheData {
GradientCacheData(gfxPattern* aPattern, bool aCoversTile,
const GradientCacheKey& aKey)
: mPattern(aPattern), mCoversTile(aCoversTile), mKey(aKey)
{}
GradientCacheData(const GradientCacheData& aOther)
: mPattern(aOther.mPattern),
mCoversTile(aOther.mCoversTile),
mKey(aOther.mKey)
{ }
nsExpirationState *GetExpirationState() {
return &mExpirationState;
}
nsExpirationState mExpirationState;
nsRefPtr<gfxPattern> mPattern;
bool mCoversTile;
GradientCacheKey mKey;
};
/**
* This class implements a cache with no maximum size, that retains the
* gfxPatterns used to draw the gradients.
*
* The key is the nsStyleGradient that defines the gradient, and the size of the
* gradient.
*
* The value is the gfxPattern, and whether or not we perform an optimization
* based on the actual gradient property.
*
* An entry stays in the cache as long as it is used often. As long as a cache
* entry is in the cache, all the references it has are guaranteed to be valid:
* the nsStyleRect for the key, the gfxPattern for the value.
*/
class GradientCache MOZ_FINAL : public nsExpirationTracker<GradientCacheData,4>
{
public:
GradientCache()
: nsExpirationTracker<GradientCacheData, 4>(MAX_GENERATION_MS)
{
mHashEntries.Init();
srand(time(nullptr));
mTimerPeriod = rand() % MAX_GENERATION_MS + 1;
Telemetry::Accumulate(Telemetry::GRADIENT_RETENTION_TIME, mTimerPeriod);
}
virtual void NotifyExpired(GradientCacheData* aObject)
{
// This will free the gfxPattern.
RemoveObject(aObject);
mHashEntries.Remove(aObject->mKey);
}
GradientCacheData* Lookup(nsStyleGradient* aKey, const gfxSize& aGradientSize,
uint32_t aFlags, gfx::BackendType aBackendType)
{
// We don't cache gradient that have Calc value, because the Calc object
// can be deallocated by the time we want to compute the hash, and thus we
// would have a dangling pointer in some nsStyleCoord in the
// nsStyleGradient that are in the hash table.
if (aKey->HasCalc()) {
return nullptr;
}
GradientCacheData* gradient =
mHashEntries.Get(GradientCacheKey(aKey, aGradientSize, aFlags, aBackendType));
if (gradient) {
MarkUsed(gradient);
}
return gradient;
}
// Returns true if we successfully register the gradient in the cache, false
// otherwise.
bool RegisterEntry(GradientCacheData* aValue)
{
// We don't cache gradient that have Calc values (see
// GradientCache::Lookup).
if (aValue->mKey.mGradient->HasCalc()) {
return false;
}
nsresult rv = AddObject(aValue);
if (NS_FAILED(rv)) {
// We are OOM, and we cannot track this object. We don't want stall
// entries in the hash table (since the expiration tracker is responsible
// for removing the cache entries), so we avoid putting that entry in the
// table, which is a good things considering we are short on memory
// anyway, we probably don't want to retain things.
return false;
}
mHashEntries.Put(aValue->mKey, aValue);
return true;
}
protected:
uint32_t mTimerPeriod;
static const uint32_t MAX_GENERATION_MS = 10000;
/**
* FIXME use nsTHashtable to avoid duplicating the GradientCacheKey.
* https://bugzilla.mozilla.org/show_bug.cgi?id=761393#c47
*/
nsClassHashtable<GradientCacheKey, GradientCacheData> mHashEntries;
};
/* Local functions */
static void DrawBorderImage(nsPresContext* aPresContext,
nsRenderingContext& aRenderingContext,
nsIFrame* aForFrame,
const nsRect& aBorderArea,
const nsStyleBorder& aStyleBorder,
const nsRect& aDirtyRect);
static void DrawBorderImageComponent(nsRenderingContext& aRenderingContext,
nsIFrame* aForFrame,
imgIContainer* aImage,
const nsRect& aDirtyRect,
const nsRect& aFill,
const nsIntRect& aSrc,
uint8_t aHFill,
uint8_t aVFill,
const nsSize& aUnitSize,
const nsStyleBorder& aStyleBorder,
uint8_t aIndex);
static nscolor MakeBevelColor(mozilla::css::Side whichSide, uint8_t style,
nscolor aBackgroundColor,
nscolor aBorderColor);
static InlineBackgroundData* gInlineBGData = nullptr;
static GradientCache* gGradientCache = nullptr;
// Initialize any static variables used by nsCSSRendering.
void nsCSSRendering::Init()
{
NS_ASSERTION(!gInlineBGData, "Init called twice");
gInlineBGData = new InlineBackgroundData();
gGradientCache = new GradientCache();
nsCSSBorderRenderer::Init();
}
// Clean up any global variables used by nsCSSRendering.
void nsCSSRendering::Shutdown()
{
delete gInlineBGData;
gInlineBGData = nullptr;
delete gGradientCache;
gGradientCache = nullptr;
nsCSSBorderRenderer::Shutdown();
}
/**
* Make a bevel color
*/
static nscolor
MakeBevelColor(mozilla::css::Side whichSide, uint8_t style,
nscolor aBackgroundColor, nscolor aBorderColor)
{
nscolor colors[2];
nscolor theColor;
// Given a background color and a border color
// calculate the color used for the shading
NS_GetSpecial3DColors(colors, aBackgroundColor, aBorderColor);
if ((style == NS_STYLE_BORDER_STYLE_OUTSET) ||
(style == NS_STYLE_BORDER_STYLE_RIDGE)) {
// Flip colors for these two border styles
switch (whichSide) {
case NS_SIDE_BOTTOM: whichSide = NS_SIDE_TOP; break;
case NS_SIDE_RIGHT: whichSide = NS_SIDE_LEFT; break;
case NS_SIDE_TOP: whichSide = NS_SIDE_BOTTOM; break;
case NS_SIDE_LEFT: whichSide = NS_SIDE_RIGHT; break;
}
}
switch (whichSide) {
case NS_SIDE_BOTTOM:
theColor = colors[1];
break;
case NS_SIDE_RIGHT:
theColor = colors[1];
break;
case NS_SIDE_TOP:
theColor = colors[0];
break;
case NS_SIDE_LEFT:
default:
theColor = colors[0];
break;
}
return theColor;
}
//----------------------------------------------------------------------
// Thebes Border Rendering Code Start
/*
* Compute the float-pixel radii that should be used for drawing
* this border/outline, given the various input bits.
*/
/* static */ void
nsCSSRendering::ComputePixelRadii(const nscoord *aAppUnitsRadii,
nscoord aAppUnitsPerPixel,
gfxCornerSizes *oBorderRadii)
{
gfxFloat radii[8];
NS_FOR_CSS_HALF_CORNERS(corner)
radii[corner] = gfxFloat(aAppUnitsRadii[corner]) / aAppUnitsPerPixel;
(*oBorderRadii)[C_TL] = gfxSize(radii[NS_CORNER_TOP_LEFT_X],
radii[NS_CORNER_TOP_LEFT_Y]);
(*oBorderRadii)[C_TR] = gfxSize(radii[NS_CORNER_TOP_RIGHT_X],
radii[NS_CORNER_TOP_RIGHT_Y]);
(*oBorderRadii)[C_BR] = gfxSize(radii[NS_CORNER_BOTTOM_RIGHT_X],
radii[NS_CORNER_BOTTOM_RIGHT_Y]);
(*oBorderRadii)[C_BL] = gfxSize(radii[NS_CORNER_BOTTOM_LEFT_X],
radii[NS_CORNER_BOTTOM_LEFT_Y]);
}
void
nsCSSRendering::PaintBorder(nsPresContext* aPresContext,
nsRenderingContext& aRenderingContext,
nsIFrame* aForFrame,
const nsRect& aDirtyRect,
const nsRect& aBorderArea,
nsStyleContext* aStyleContext,
int aSkipSides)
{
SAMPLE_LABEL("nsCSSRendering", "PaintBorder");
nsStyleContext *styleIfVisited = aStyleContext->GetStyleIfVisited();
const nsStyleBorder *styleBorder = aStyleContext->GetStyleBorder();
// Don't check RelevantLinkVisited here, since we want to take the
// same amount of time whether or not it's true.
if (!styleIfVisited) {
PaintBorderWithStyleBorder(aPresContext, aRenderingContext, aForFrame,
aDirtyRect, aBorderArea, *styleBorder,
aStyleContext, aSkipSides);
return;
}
nsStyleBorder newStyleBorder(*styleBorder);
// We're making an ephemeral stack copy here, so just copy this debug-only
// member to prevent assertions.
#ifdef DEBUG
newStyleBorder.mImageTracked = styleBorder->mImageTracked;
#endif
NS_FOR_CSS_SIDES(side) {
newStyleBorder.SetBorderColor(side,
aStyleContext->GetVisitedDependentColor(
nsCSSProps::SubpropertyEntryFor(eCSSProperty_border_color)[side]));
}
PaintBorderWithStyleBorder(aPresContext, aRenderingContext, aForFrame,
aDirtyRect, aBorderArea, newStyleBorder,
aStyleContext, aSkipSides);
#ifdef DEBUG
newStyleBorder.mImageTracked = false;
#endif
}
void
nsCSSRendering::PaintBorderWithStyleBorder(nsPresContext* aPresContext,
nsRenderingContext& aRenderingContext,
nsIFrame* aForFrame,
const nsRect& aDirtyRect,
const nsRect& aBorderArea,
const nsStyleBorder& aStyleBorder,
nsStyleContext* aStyleContext,
int aSkipSides)
{
nsMargin border;
nscoord twipsRadii[8];
nsCompatibility compatMode = aPresContext->CompatibilityMode();
SN("++ PaintBorder");
// Check to see if we have an appearance defined. If so, we let the theme
// renderer draw the border. DO not get the data from aForFrame, since the passed in style context
// may be different! Always use |aStyleContext|!
const nsStyleDisplay* displayData = aStyleContext->GetStyleDisplay();
if (displayData->mAppearance) {
nsITheme *theme = aPresContext->GetTheme();
if (theme && theme->ThemeSupportsWidget(aPresContext, aForFrame, displayData->mAppearance))
return; // Let the theme handle it.
}
if (aStyleBorder.IsBorderImageLoaded()) {
DrawBorderImage(aPresContext, aRenderingContext, aForFrame,
aBorderArea, aStyleBorder, aDirtyRect);
return;
}
// Get our style context's color struct.
const nsStyleColor* ourColor = aStyleContext->GetStyleColor();
// in NavQuirks mode we want to use the parent's context as a starting point
// for determining the background color
nsIFrame* bgFrame = nsCSSRendering::FindNonTransparentBackgroundFrame
(aForFrame, compatMode == eCompatibility_NavQuirks ? true : false);
nsStyleContext* bgContext = bgFrame->GetStyleContext();
nscolor bgColor =
bgContext->GetVisitedDependentColor(eCSSProperty_background_color);
border = aStyleBorder.GetComputedBorder();
if ((0 == border.left) && (0 == border.right) &&
(0 == border.top) && (0 == border.bottom)) {
// Empty border area
return;
}
nsSize frameSize = aForFrame->GetSize();
if (&aStyleBorder == aForFrame->GetStyleBorder() &&
frameSize == aBorderArea.Size()) {
aForFrame->GetBorderRadii(twipsRadii);
} else {
nsIFrame::ComputeBorderRadii(aStyleBorder.mBorderRadius, frameSize,
aBorderArea.Size(), aSkipSides, twipsRadii);
}
// Turn off rendering for all of the zero sized sides
if (aSkipSides & SIDE_BIT_TOP) border.top = 0;
if (aSkipSides & SIDE_BIT_RIGHT) border.right = 0;
if (aSkipSides & SIDE_BIT_BOTTOM) border.bottom = 0;
if (aSkipSides & SIDE_BIT_LEFT) border.left = 0;
// get the inside and outside parts of the border
nsRect outerRect(aBorderArea);
SF(" outerRect: %d %d %d %d\n", outerRect.x, outerRect.y, outerRect.width, outerRect.height);
// we can assume that we're already clipped to aDirtyRect -- I think? (!?)
// Get our conversion values
nscoord twipsPerPixel = aPresContext->DevPixelsToAppUnits(1);
// convert outer and inner rects
gfxRect oRect(nsLayoutUtils::RectToGfxRect(outerRect, twipsPerPixel));
// convert the border widths
gfxFloat borderWidths[4] = { gfxFloat(border.top / twipsPerPixel),
gfxFloat(border.right / twipsPerPixel),
gfxFloat(border.bottom / twipsPerPixel),
gfxFloat(border.left / twipsPerPixel) };
// convert the radii
gfxCornerSizes borderRadii;
ComputePixelRadii(twipsRadii, twipsPerPixel, &borderRadii);
uint8_t borderStyles[4];
nscolor borderColors[4];
nsBorderColors *compositeColors[4];
// pull out styles, colors, composite colors
NS_FOR_CSS_SIDES (i) {
bool foreground;
borderStyles[i] = aStyleBorder.GetBorderStyle(i);
aStyleBorder.GetBorderColor(i, borderColors[i], foreground);
aStyleBorder.GetCompositeColors(i, &compositeColors[i]);
if (foreground)
borderColors[i] = ourColor->mColor;
}
SF(" borderStyles: %d %d %d %d\n", borderStyles[0], borderStyles[1], borderStyles[2], borderStyles[3]);
// start drawing
gfxContext *ctx = aRenderingContext.ThebesContext();
ctx->Save();
#if 0
// this will draw a transparent red backround underneath the oRect area
ctx->Save();
ctx->Rectangle(oRect);
ctx->SetColor(gfxRGBA(1.0, 0.0, 0.0, 0.5));
ctx->Fill();
ctx->Restore();
#endif
//SF ("borderRadii: %f %f %f %f\n", borderRadii[0], borderRadii[1], borderRadii[2], borderRadii[3]);
nsCSSBorderRenderer br(twipsPerPixel,
ctx,
oRect,
borderStyles,
borderWidths,
borderRadii,
borderColors,
compositeColors,
aSkipSides,
bgColor);
br.DrawBorders();
ctx->Restore();
SN();
}
static nsRect
GetOutlineInnerRect(nsIFrame* aFrame)
{
nsRect* savedOutlineInnerRect = static_cast<nsRect*>
(aFrame->Properties().Get(nsIFrame::OutlineInnerRectProperty()));
if (savedOutlineInnerRect)
return *savedOutlineInnerRect;
// FIXME (bug 599652): We probably want something narrower than either
// overflow rect here, but for now use the visual overflow in order to
// be consistent with ComputeOutlineAndEffectsRect in nsFrame.cpp.
return aFrame->GetVisualOverflowRect();
}
void
nsCSSRendering::PaintOutline(nsPresContext* aPresContext,
nsRenderingContext& aRenderingContext,
nsIFrame* aForFrame,
const nsRect& aDirtyRect,
const nsRect& aBorderArea,
nsStyleContext* aStyleContext)
{
nscoord twipsRadii[8];
// Get our style context's color struct.
const nsStyleOutline* ourOutline = aStyleContext->GetStyleOutline();
nscoord width;
ourOutline->GetOutlineWidth(width);
if (width == 0) {
// Empty outline
return;
}
nsIFrame* bgFrame = nsCSSRendering::FindNonTransparentBackgroundFrame
(aForFrame, false);
nsStyleContext* bgContext = bgFrame->GetStyleContext();
nscolor bgColor =
bgContext->GetVisitedDependentColor(eCSSProperty_background_color);
// When the outline property is set on :-moz-anonymous-block or
// :-moz-anonyomus-positioned-block pseudo-elements, it inherited that
// outline from the inline that was broken because it contained a
// block. In that case, we don't want a really wide outline if the
// block inside the inline is narrow, so union the actual contents of
// the anonymous blocks.
nsIFrame *frameForArea = aForFrame;
do {
nsIAtom *pseudoType = frameForArea->GetStyleContext()->GetPseudo();
if (pseudoType != nsCSSAnonBoxes::mozAnonymousBlock &&
pseudoType != nsCSSAnonBoxes::mozAnonymousPositionedBlock)
break;
// If we're done, we really want it and all its later siblings.
frameForArea = frameForArea->GetFirstPrincipalChild();
NS_ASSERTION(frameForArea, "anonymous block with no children?");
} while (frameForArea);
nsRect innerRect; // relative to aBorderArea.TopLeft()
if (frameForArea == aForFrame) {
innerRect = GetOutlineInnerRect(aForFrame);
} else {
for (; frameForArea; frameForArea = frameForArea->GetNextSibling()) {
// The outline has already been included in aForFrame's overflow
// area, but not in those of its descendants, so we have to
// include it. Otherwise we'll end up drawing the outline inside
// the border.
nsRect r(GetOutlineInnerRect(frameForArea) +
frameForArea->GetOffsetTo(aForFrame));
innerRect.UnionRect(innerRect, r);
}
}
innerRect += aBorderArea.TopLeft();
nscoord offset = ourOutline->mOutlineOffset;
innerRect.Inflate(offset, offset);
// If the dirty rect is completely inside the border area (e.g., only the
// content is being painted), then we can skip out now
// XXX this isn't exactly true for rounded borders, where the inside curves may
// encroach into the content area. A safer calculation would be to
// shorten insideRect by the radius one each side before performing this test.
if (innerRect.Contains(aDirtyRect))
return;
nsRect outerRect = innerRect;
outerRect.Inflate(width, width);
// get the radius for our outline
nsIFrame::ComputeBorderRadii(ourOutline->mOutlineRadius, aBorderArea.Size(),
outerRect.Size(), 0, twipsRadii);
// Get our conversion values
nscoord twipsPerPixel = aPresContext->DevPixelsToAppUnits(1);
// get the outer rectangles
gfxRect oRect(nsLayoutUtils::RectToGfxRect(outerRect, twipsPerPixel));
// convert the radii
nsMargin outlineMargin(width, width, width, width);
gfxCornerSizes outlineRadii;
ComputePixelRadii(twipsRadii, twipsPerPixel, &outlineRadii);
uint8_t outlineStyle = ourOutline->GetOutlineStyle();
uint8_t outlineStyles[4] = { outlineStyle,
outlineStyle,
outlineStyle,
outlineStyle };
// This handles treating the initial color as 'currentColor'; if we
// ever want 'invert' back we'll need to do a bit of work here too.
nscolor outlineColor =
aStyleContext->GetVisitedDependentColor(eCSSProperty_outline_color);
nscolor outlineColors[4] = { outlineColor,
outlineColor,
outlineColor,
outlineColor };
// convert the border widths
gfxFloat outlineWidths[4] = { gfxFloat(width / twipsPerPixel),
gfxFloat(width / twipsPerPixel),
gfxFloat(width / twipsPerPixel),
gfxFloat(width / twipsPerPixel) };
// start drawing
gfxContext *ctx = aRenderingContext.ThebesContext();
ctx->Save();
nsCSSBorderRenderer br(twipsPerPixel,
ctx,
oRect,
outlineStyles,
outlineWidths,
outlineRadii,
outlineColors,
nullptr, 0,
bgColor);
br.DrawBorders();
ctx->Restore();
SN();
}
void
nsCSSRendering::PaintFocus(nsPresContext* aPresContext,
nsRenderingContext& aRenderingContext,
const nsRect& aFocusRect,
nscolor aColor)
{
nscoord oneCSSPixel = nsPresContext::CSSPixelsToAppUnits(1);
nscoord oneDevPixel = aPresContext->DevPixelsToAppUnits(1);
gfxRect focusRect(nsLayoutUtils::RectToGfxRect(aFocusRect, oneDevPixel));
gfxCornerSizes focusRadii;
{
nscoord twipsRadii[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
ComputePixelRadii(twipsRadii, oneDevPixel, &focusRadii);
}
gfxFloat focusWidths[4] = { gfxFloat(oneCSSPixel / oneDevPixel),
gfxFloat(oneCSSPixel / oneDevPixel),
gfxFloat(oneCSSPixel / oneDevPixel),
gfxFloat(oneCSSPixel / oneDevPixel) };
uint8_t focusStyles[4] = { NS_STYLE_BORDER_STYLE_DOTTED,
NS_STYLE_BORDER_STYLE_DOTTED,
NS_STYLE_BORDER_STYLE_DOTTED,
NS_STYLE_BORDER_STYLE_DOTTED };
nscolor focusColors[4] = { aColor, aColor, aColor, aColor };
gfxContext *ctx = aRenderingContext.ThebesContext();
ctx->Save();
// Because this renders a dotted border, the background color
// should not be used. Therefore, we provide a value that will
// be blatantly wrong if it ever does get used. (If this becomes
// something that CSS can style, this function will then have access
// to a style context and can use the same logic that PaintBorder
// and PaintOutline do.)
nsCSSBorderRenderer br(oneDevPixel,
ctx,
focusRect,
focusStyles,
focusWidths,
focusRadii,
focusColors,
nullptr, 0,
NS_RGB(255, 0, 0));
br.DrawBorders();
ctx->Restore();
SN();
}
// Thebes Border Rendering Code End
//----------------------------------------------------------------------
//----------------------------------------------------------------------
/**
* Computes the placement of a background image.
*
* @param aOriginBounds is the box to which the tiling position should be
* relative
* This should correspond to 'background-origin' for the frame,
* except when painting on the canvas, in which case the origin bounds
* should be the bounds of the root element's frame.
* @param aTopLeft the top-left corner where an image tile should be drawn
* @param aAnchorPoint a point which should be pixel-aligned by
* nsLayoutUtils::DrawImage. This is the same as aTopLeft, unless CSS
* specifies a percentage (including 'right' or 'bottom'), in which case
* it's that percentage within of aOriginBounds. So 'right' would set
* aAnchorPoint.x to aOriginBounds.XMost().
*
* Points are returned relative to aOriginBounds.
*/
static void
ComputeBackgroundAnchorPoint(const nsStyleBackground::Layer& aLayer,
const nsSize& aOriginBounds,
const nsSize& aImageSize,
nsPoint* aTopLeft,
nsPoint* aAnchorPoint)
{
double percentX = aLayer.mPosition.mXPosition.mPercent;
nscoord lengthX = aLayer.mPosition.mXPosition.mLength;
aAnchorPoint->x = lengthX + NSToCoordRound(percentX*aOriginBounds.width);
aTopLeft->x = lengthX +
NSToCoordRound(percentX*(aOriginBounds.width - aImageSize.width));
double percentY = aLayer.mPosition.mYPosition.mPercent;
nscoord lengthY = aLayer.mPosition.mYPosition.mLength;
aAnchorPoint->y = lengthY + NSToCoordRound(percentY*aOriginBounds.height);
aTopLeft->y = lengthY +
NSToCoordRound(percentY*(aOriginBounds.height - aImageSize.height));
}
nsIFrame*
nsCSSRendering::FindNonTransparentBackgroundFrame(nsIFrame* aFrame,
bool aStartAtParent /*= false*/)
{
NS_ASSERTION(aFrame, "Cannot find NonTransparentBackgroundFrame in a null frame");
nsIFrame* frame = nullptr;
if (aStartAtParent) {
frame = nsLayoutUtils::GetParentOrPlaceholderFor(aFrame);
}
if (!frame) {
frame = aFrame;
}
while (frame) {
// No need to call GetVisitedDependentColor because it always uses
// this alpha component anyway.
if (NS_GET_A(frame->GetStyleBackground()->mBackgroundColor) > 0)
break;