1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsCSSRenderingGradients_h__
8 #define nsCSSRenderingGradients_h__
10 #include "nsLayoutUtils.h"
11 #include "nsStyleStruct.h"
13 #include "mozilla/Maybe.h"
14 #include "mozilla/gfx/2D.h"
19 class StackingContextHelper
;
23 class DisplayListBuilder
;
26 // A resolved color stop, with a specific position along the gradient line and
29 ColorStop() : mPosition(0), mIsMidpoint(false) {}
30 ColorStop(double aPosition
, bool aIsMidPoint
, const gfx::Color
& aColor
)
31 : mPosition(aPosition
), mIsMidpoint(aIsMidPoint
), mColor(aColor
) {}
32 double mPosition
; // along the gradient line; 0=start, 1=end
37 class nsCSSGradientRenderer final
{
40 * Prepare a nsCSSGradientRenderer for a gradient for an element.
41 * aIntrinsicSize - the size of the source gradient.
43 static nsCSSGradientRenderer
Create(nsPresContext
* aPresContext
,
44 ComputedStyle
* aComputedStyle
,
45 const StyleGradient
& aGradient
,
46 const nsSize
& aIntrinsiceSize
);
49 * Draw the gradient to aContext
50 * aDest - where the first tile of gradient is
51 * aFill - the area to be filled with tiles of aDest
52 * aSrc - the area of the gradient that will fill aDest
53 * aRepeatSize - the distance from the origin of a tile
54 * to the next origin of a tile
55 * aDirtyRect - pixels outside of this area may be skipped
57 void Paint(gfxContext
& aContext
, const nsRect
& aDest
, const nsRect
& aFill
,
58 const nsSize
& aRepeatSize
, const mozilla::CSSIntRect
& aSrc
,
59 const nsRect
& aDirtyRect
, float aOpacity
= 1.0);
62 * Collect the gradient parameters
64 void BuildWebRenderParameters(float aOpacity
, wr::ExtendMode
& aMode
,
65 nsTArray
<wr::GradientStop
>& aStops
,
66 LayoutDevicePoint
& aLineStart
,
67 LayoutDevicePoint
& aLineEnd
,
68 LayoutDeviceSize
& aGradientRadius
);
71 * Build display items for the gradient
72 * aLayer - the layer to make this display item relative to
73 * aDest - where the first tile of gradient is
74 * aFill - the area to be filled with tiles of aDest
75 * aRepeatSize - the distance from the origin of a tile
76 * to the next origin of a tile
77 * aSrc - the area of the gradient that will fill aDest
79 void BuildWebRenderDisplayItems(wr::DisplayListBuilder
& aBuilder
,
80 const layers::StackingContextHelper
& aSc
,
81 const nsRect
& aDest
, const nsRect
& aFill
,
82 const nsSize
& aRepeatSize
,
83 const mozilla::CSSIntRect
& aSrc
,
84 bool aIsBackfaceVisible
,
85 float aOpacity
= 1.0);
88 nsCSSGradientRenderer()
89 : mPresContext(nullptr),
95 * Attempts to paint the tiles for a gradient by painting it once to an
96 * offscreen surface and then painting that offscreen surface with
97 * ExtendMode::Repeat to cover all tiles.
99 * Returns false if the optimization wasn't able to be used, in which case
100 * a fallback should be used.
102 bool TryPaintTilesWithExtendMode(
103 gfxContext
& aContext
, gfxPattern
* aGradientPattern
, nscoord aXStart
,
104 nscoord aYStart
, const gfxRect
& aDirtyAreaToFill
, const nsRect
& aDest
,
105 const nsSize
& aRepeatSize
, bool aForceRepeatToCoverTiles
);
107 nsPresContext
* mPresContext
;
108 const StyleGradient
* mGradient
;
109 nsTArray
<ColorStop
> mStops
;
110 gfxPoint mLineStart
, mLineEnd
;
111 double mRadiusX
, mRadiusY
;
114 } // namespace mozilla
116 #endif /* nsCSSRenderingGradients_h__ */