Bug 1687263: part 4) Defer and in some cases avoid removing spellchecking-ranges...
[gecko.git] / layout / painting / nsCSSRenderingBorders.h
blob5e29b7a73e99b8175d59ced5d64abf821c5abe2b
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 NS_CSS_RENDERING_BORDERS_H
8 #define NS_CSS_RENDERING_BORDERS_H
10 #include "gfxRect.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/gfx/2D.h"
13 #include "mozilla/gfx/BezierUtils.h"
14 #include "mozilla/gfx/PathHelpers.h"
15 #include "mozilla/RefPtr.h"
16 #include "nsColor.h"
17 #include "nsCOMPtr.h"
18 #include "nsIFrame.h"
19 #include "nsImageRenderer.h"
20 #include "gfxUtils.h"
22 struct nsBorderColors;
23 class nsDisplayBorder;
25 namespace mozilla {
27 enum class StyleBorderStyle : uint8_t;
28 enum class StyleBorderImageRepeat : uint8_t;
30 namespace gfx {
31 class GradientStops;
32 } // namespace gfx
33 namespace layers {
34 class StackingContextHelper;
35 } // namespace layers
36 } // namespace mozilla
38 // define this to enable a bunch of debug dump info
39 #undef DEBUG_NEW_BORDERS
42 * Helper class that handles border rendering.
44 * aDrawTarget -- the DrawTarget to which the border should be rendered
45 * outsideRect -- the rectangle on the outer edge of the border
47 * For any parameter where an array of side values is passed in,
48 * they are in top, right, bottom, left order.
50 * borderStyles -- one border style enum per side
51 * borderWidths -- one border width per side
52 * borderRadii -- a RectCornerRadii struct describing the w/h for each rounded
53 * corner. If the corner doesn't have a border radius, 0,0 should be given for
54 * it. borderColors -- one nscolor per side
56 * skipSides -- a bit mask specifying which sides, if any, to skip
57 * backgroundColor -- the background color of the element.
58 * Used in calculating colors for 2-tone borders, such as inset and outset
59 * gapRect - a rectangle that should be clipped out to leave a gap in a border,
60 * or nullptr if none.
63 typedef enum {
64 BorderColorStyleNone,
65 BorderColorStyleSolid,
66 BorderColorStyleLight,
67 BorderColorStyleDark
68 } BorderColorStyle;
70 class nsPresContext;
72 class nsCSSBorderRenderer final {
73 typedef mozilla::gfx::Bezier Bezier;
74 typedef mozilla::gfx::ColorPattern ColorPattern;
75 typedef mozilla::gfx::DrawTarget DrawTarget;
76 typedef mozilla::gfx::Float Float;
77 typedef mozilla::gfx::Path Path;
78 typedef mozilla::gfx::Point Point;
79 typedef mozilla::gfx::Rect Rect;
80 typedef mozilla::gfx::RectCornerRadii RectCornerRadii;
81 typedef mozilla::gfx::StrokeOptions StrokeOptions;
83 friend class nsDisplayBorder;
84 friend class nsDisplayOutline;
85 friend class nsDisplayButtonBorder;
86 friend class nsDisplayButtonForeground;
88 public:
89 nsCSSBorderRenderer(nsPresContext* aPresContext, DrawTarget* aDrawTarget,
90 const Rect& aDirtyRect, Rect& aOuterRect,
91 const mozilla::StyleBorderStyle* aBorderStyles,
92 const Float* aBorderWidths, RectCornerRadii& aBorderRadii,
93 const nscolor* aBorderColors, bool aBackfaceIsVisible,
94 const mozilla::Maybe<Rect>& aClipRect);
96 // draw the entire border
97 void DrawBorders();
99 void CreateWebRenderCommands(
100 nsDisplayItem* aItem, mozilla::wr::DisplayListBuilder& aBuilder,
101 mozilla::wr::IpcResourceUpdateQueue& aResources,
102 const mozilla::layers::StackingContextHelper& aSc);
104 // utility function used for background painting as well as borders
105 static void ComputeInnerRadii(const RectCornerRadii& aRadii,
106 const Float* aBorderSizes,
107 RectCornerRadii* aInnerRadiiRet);
109 // Given aRadii as the border radii for a rectangle, compute the
110 // appropriate radii for another rectangle *outside* that rectangle
111 // by increasing the radii, except keeping sharp corners sharp.
112 // Used for spread box-shadows
113 static void ComputeOuterRadii(const RectCornerRadii& aRadii,
114 const Float* aBorderSizes,
115 RectCornerRadii* aOuterRadiiRet);
117 static bool AllCornersZeroSize(const RectCornerRadii& corners);
119 private:
120 RectCornerRadii mBorderCornerDimensions;
122 nsPresContext* mPresContext;
124 // destination DrawTarget and dirty rect
125 DrawTarget* mDrawTarget;
126 Rect mDirtyRect;
128 // the rectangle of the outside and the inside of the border
129 Rect mOuterRect;
130 Rect mInnerRect;
132 // the style and size of the border
133 mozilla::StyleBorderStyle mBorderStyles[4];
134 Float mBorderWidths[4];
135 RectCornerRadii mBorderRadii;
137 // the colors for 'border-top-color' et. al.
138 nscolor mBorderColors[4];
140 // calculated values
141 bool mAllBordersSameStyle;
142 bool mAllBordersSameWidth;
143 bool mOneUnitBorder;
144 bool mNoBorderRadius;
145 bool mAvoidStroke;
146 bool mBackfaceIsVisible;
147 mozilla::Maybe<Rect> mLocalClip;
149 // For all the sides in the bitmask, would they be rendered
150 // in an identical color and style?
151 bool AreBorderSideFinalStylesSame(mozilla::SideBits aSides);
153 // For the given style, is the given corner a solid color?
154 bool IsSolidCornerStyle(mozilla::StyleBorderStyle aStyle,
155 mozilla::Corner aCorner);
157 // For the given corner, is the given corner mergeable into one dot?
158 bool IsCornerMergeable(mozilla::Corner aCorner);
160 // For the given solid corner, what color style should be used?
161 BorderColorStyle BorderColorStyleForSolidCorner(
162 mozilla::StyleBorderStyle aStyle, mozilla::Corner aCorner);
165 // Path generation functions
168 // Get the Rect for drawing the given corner
169 Rect GetCornerRect(mozilla::Corner aCorner);
170 // add the path for drawing the given side without any adjacent corners to the
171 // context
172 Rect GetSideClipWithoutCornersRect(mozilla::Side aSide);
174 // Create a clip path for the wedge that this side of
175 // the border should take up. This is only called
176 // when we're drawing separate border sides, so we know
177 // that ADD compositing is taking place.
179 // This code needs to make sure that the individual pieces
180 // don't ever (mathematically) overlap; the pixel overlap
181 // is taken care of by the ADD compositing.
182 already_AddRefed<Path> GetSideClipSubPath(mozilla::Side aSide);
184 // Return start or end point for dashed/dotted side
185 Point GetStraightBorderPoint(mozilla::Side aSide, mozilla::Corner aCorner,
186 bool* aIsUnfilled, Float aDotOffset = 0.0f);
188 // Return bezier control points for the outer and the inner curve for given
189 // corner
190 void GetOuterAndInnerBezier(Bezier* aOuterBezier, Bezier* aInnerBezier,
191 mozilla::Corner aCorner);
193 // Given a set of sides to fill and a color, do so in the fastest way.
195 // Stroke tends to be faster for smaller borders because it doesn't go
196 // through the tessellator, which has initialization overhead. If
197 // we're rendering all sides, we can use stroke at any thickness; we
198 // also do TL/BR pairs at 1px thickness using stroke.
200 // If we can't stroke, then if it's a TL/BR pair, we use the specific
201 // TL/BR paths. Otherwise, we do the full path and fill.
203 // Calling code is expected to only set up a clip as necessary; no
204 // clip is needed if we can render the entire border in 1 or 2 passes.
205 void FillSolidBorder(const Rect& aOuterRect, const Rect& aInnerRect,
206 const RectCornerRadii& aBorderRadii,
207 const Float* aBorderSizes, mozilla::SideBits aSides,
208 const ColorPattern& aColor);
211 // core rendering
214 // draw the border for the given sides, using the style of the first side
215 // present in the bitmask
216 void DrawBorderSides(mozilla::SideBits aSides);
218 // Setup the stroke options for the given dashed/dotted side
219 void SetupDashedOptions(StrokeOptions* aStrokeOptions, Float aDash[2],
220 mozilla::Side aSide, Float aBorderLength,
221 bool isCorner);
223 // Draw the given dashed/dotte side
224 void DrawDashedOrDottedSide(mozilla::Side aSide);
226 // Draw the given dotted side, each dot separately
227 void DrawDottedSideSlow(mozilla::Side aSide);
229 // Draw the given dashed/dotted corner
230 void DrawDashedOrDottedCorner(mozilla::Side aSide, mozilla::Corner aCorner);
232 // Draw the given dotted corner, each segment separately
233 void DrawDottedCornerSlow(mozilla::Side aSide, mozilla::Corner aCorner);
235 // Draw the given dashed corner, each dot separately
236 void DrawDashedCornerSlow(mozilla::Side aSide, mozilla::Corner aCorner);
238 // Draw the given dashed/dotted corner with solid style
239 void DrawFallbackSolidCorner(mozilla::Side aSide, mozilla::Corner aCorner);
241 // Analyze if all border sides have the same width.
242 bool AllBordersSameWidth();
244 // Analyze if all borders are 'solid' this also considers hidden or 'none'
245 // borders because they can be considered 'solid' borders of 0 width and
246 // with no color effect.
247 bool AllBordersSolid();
249 // Draw a solid color border that is uniformly the same width.
250 void DrawSingleWidthSolidBorder();
252 // Draw any border which is solid on all sides.
253 void DrawSolidBorder();
256 class nsCSSBorderImageRenderer final {
257 typedef mozilla::nsImageRenderer nsImageRenderer;
259 public:
260 static mozilla::Maybe<nsCSSBorderImageRenderer> CreateBorderImageRenderer(
261 nsPresContext* aPresContext, nsIFrame* aForFrame,
262 const nsRect& aBorderArea, const nsStyleBorder& aStyleBorder,
263 const nsRect& aDirtyRect, nsIFrame::Sides aSkipSides, uint32_t aFlags,
264 mozilla::image::ImgDrawResult* aDrawResult);
266 mozilla::image::ImgDrawResult DrawBorderImage(nsPresContext* aPresContext,
267 gfxContext& aRenderingContext,
268 nsIFrame* aForFrame,
269 const nsRect& aDirtyRect);
270 mozilla::image::ImgDrawResult CreateWebRenderCommands(
271 nsDisplayItem* aItem, nsIFrame* aForFrame,
272 mozilla::wr::DisplayListBuilder& aBuilder,
273 mozilla::wr::IpcResourceUpdateQueue& aResources,
274 const mozilla::layers::StackingContextHelper& aSc,
275 mozilla::layers::RenderRootStateManager* aManager,
276 nsDisplayListBuilder* aDisplayListBuilder);
278 nsCSSBorderImageRenderer(const nsCSSBorderImageRenderer& aRhs);
279 nsCSSBorderImageRenderer& operator=(const nsCSSBorderImageRenderer& aRhs);
281 private:
282 nsCSSBorderImageRenderer(nsIFrame* aForFrame, const nsRect& aBorderArea,
283 const nsStyleBorder& aStyleBorder,
284 nsIFrame::Sides aSkipSides,
285 const nsImageRenderer& aImageRenderer);
287 nsImageRenderer mImageRenderer;
288 nsSize mImageSize;
289 nsMargin mSlice;
290 nsMargin mWidths;
291 nsMargin mImageOutset;
292 nsRect mArea;
293 nsRect mClip;
294 mozilla::StyleBorderImageRepeat mRepeatModeHorizontal;
295 mozilla::StyleBorderImageRepeat mRepeatModeVertical;
296 bool mFill;
298 friend class nsDisplayBorder;
299 friend struct nsCSSRendering;
302 namespace mozilla {
303 #ifdef DEBUG_NEW_BORDERS
304 # include <stdarg.h>
306 static inline void PrintAsString(const mozilla::gfx::Point& p) {
307 fprintf(stderr, "[%f,%f]", p.x, p.y);
310 static inline void PrintAsString(const mozilla::gfx::Size& s) {
311 fprintf(stderr, "[%f %f]", s.width, s.height);
314 static inline void PrintAsString(const mozilla::gfx::Rect& r) {
315 fprintf(stderr, "[%f %f %f %f]", r.X(), r.Y(), r.Width(), r.Height());
318 static inline void PrintAsString(const mozilla::gfx::Float f) {
319 fprintf(stderr, "%f", f);
322 static inline void PrintAsString(const char* s) { fprintf(stderr, "%s", s); }
324 static inline void PrintAsStringNewline(const char* s = nullptr) {
325 if (s) fprintf(stderr, "%s", s);
326 fprintf(stderr, "\n");
327 fflush(stderr);
330 static inline MOZ_FORMAT_PRINTF(1, 2) void PrintAsFormatString(const char* fmt,
331 ...) {
332 va_list vl;
333 va_start(vl, fmt);
334 vfprintf(stderr, fmt, vl);
335 va_end(vl);
338 #else
339 static inline void PrintAsString(const mozilla::gfx::Point& p) {}
340 static inline void PrintAsString(const mozilla::gfx::Size& s) {}
341 static inline void PrintAsString(const mozilla::gfx::Rect& r) {}
342 static inline void PrintAsString(const mozilla::gfx::Float f) {}
343 static inline void PrintAsString(const char* s) {}
344 static inline void PrintAsStringNewline(const char* s = nullptr) {}
345 static inline MOZ_FORMAT_PRINTF(1, 2) void PrintAsFormatString(const char* fmt,
346 ...) {}
347 #endif
349 } // namespace mozilla
351 #endif /* NS_CSS_RENDERING_BORDERS_H */