DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (56e7b9127e89)

VCS Links

Line Code
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
/* -*- 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/. */

#ifndef NS_CSS_RENDERING_BORDERS_H
#define NS_CSS_RENDERING_BORDERS_H

#include "gfxRect.h"
#include "mozilla/Attributes.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/BezierUtils.h"
#include "mozilla/gfx/PathHelpers.h"
#include "mozilla/RefPtr.h"
#include "nsColor.h"
#include "nsCOMPtr.h"
#include "nsIFrame.h"
#include "nsImageRenderer.h"
#include "nsStyleConsts.h"
#include "nsStyleStruct.h"
#include "nsPresContext.h"
#include "gfxUtils.h"

struct nsBorderColors;
class nsDisplayBorder;

namespace mozilla {
namespace gfx {
class GradientStops;
} // namespace gfx
namespace layers {
class StackingContextHelper;
} // namespace layers
} // namespace mozilla

// define this to enable a bunch of debug dump info
#undef DEBUG_NEW_BORDERS

/*
 * Helper class that handles border rendering.
 *
 * aDrawTarget -- the DrawTarget to which the border should be rendered
 * outsideRect -- the rectangle on the outer edge of the border
 *
 * For any parameter where an array of side values is passed in,
 * they are in top, right, bottom, left order.
 *
 * borderStyles -- one border style enum per side
 * borderWidths -- one border width per side
 * borderRadii -- a RectCornerRadii struct describing the w/h for each rounded corner.
 *    If the corner doesn't have a border radius, 0,0 should be given for it.
 * borderColors -- one nscolor per side
 *
 * skipSides -- a bit mask specifying which sides, if any, to skip
 * backgroundColor -- the background color of the element.
 *    Used in calculating colors for 2-tone borders, such as inset and outset
 * gapRect - a rectangle that should be clipped out to leave a gap in a border,
 *    or nullptr if none.
 */

typedef enum {
  BorderColorStyleNone,
  BorderColorStyleSolid,
  BorderColorStyleLight,
  BorderColorStyleDark
} BorderColorStyle;

class nsIDocument;
class nsPresContext;

class nsCSSBorderRenderer final
{
  typedef mozilla::gfx::Bezier Bezier;
  typedef mozilla::gfx::ColorPattern ColorPattern;
  typedef mozilla::gfx::DrawTarget DrawTarget;
  typedef mozilla::gfx::Float Float;
  typedef mozilla::gfx::Path Path;
  typedef mozilla::gfx::Point Point;
  typedef mozilla::gfx::Rect Rect;
  typedef mozilla::gfx::RectCornerRadii RectCornerRadii;
  typedef mozilla::gfx::StrokeOptions StrokeOptions;

  friend class nsDisplayBorder;
  friend class nsDisplayOutline;
  friend class nsDisplayButtonBorder;
  friend class nsDisplayButtonForeground;

public:

  nsCSSBorderRenderer(nsPresContext* aPresContext,
                      const nsIDocument* aDocument,
                      DrawTarget* aDrawTarget,
                      const Rect& aDirtyRect,
                      Rect& aOuterRect,
                      const uint8_t* aBorderStyles,
                      const Float* aBorderWidths,
                      RectCornerRadii& aBorderRadii,
                      const nscolor* aBorderColors,
                      nscolor aBackgroundColor,
                      bool aBackfaceIsVisible,
                      const mozilla::Maybe<Rect>& aClipRect);

  // draw the entire border
  void DrawBorders();

  void CreateWebRenderCommands(nsDisplayItem* aItem,
                               mozilla::wr::DisplayListBuilder& aBuilder,
                               mozilla::wr::IpcResourceUpdateQueue& aResources,
                               const mozilla::layers::StackingContextHelper& aSc);

  // utility function used for background painting as well as borders
  static void ComputeInnerRadii(const RectCornerRadii& aRadii,
                                const Float* aBorderSizes,
                                RectCornerRadii* aInnerRadiiRet);

  // Given aRadii as the border radii for a rectangle, compute the
  // appropriate radii for another rectangle *outside* that rectangle
  // by increasing the radii, except keeping sharp corners sharp.
  // Used for spread box-shadows
  static void ComputeOuterRadii(const RectCornerRadii& aRadii,
                                const Float* aBorderSizes,
                                RectCornerRadii* aOuterRadiiRet);

  static bool AllCornersZeroSize(const RectCornerRadii& corners);

private:

  RectCornerRadii mBorderCornerDimensions;

  // Target document to report warning
  nsPresContext* mPresContext;
  const nsIDocument* mDocument;

  // destination DrawTarget and dirty rect
  DrawTarget* mDrawTarget;
  Rect mDirtyRect;

  // the rectangle of the outside and the inside of the border
  Rect mOuterRect;
  Rect mInnerRect;

  // the style and size of the border
  uint8_t mBorderStyles[4];
  Float mBorderWidths[4];
  RectCornerRadii mBorderRadii;

  // the colors for 'border-top-color' et. al.
  nscolor mBorderColors[4];

  // the background color
  nscolor mBackgroundColor;

  // calculated values
  bool mAllBordersSameStyle;
  bool mAllBordersSameWidth;
  bool mOneUnitBorder;
  bool mNoBorderRadius;
  bool mAvoidStroke;
  bool mBackfaceIsVisible;
  mozilla::Maybe<Rect> mLocalClip;

  // For all the sides in the bitmask, would they be rendered
  // in an identical color and style?
  bool AreBorderSideFinalStylesSame(uint8_t aSides);

  // For the given style, is the given corner a solid color?
  bool IsSolidCornerStyle(uint8_t aStyle, mozilla::Corner aCorner);

  // For the given corner, is the given corner mergeable into one dot?
  bool IsCornerMergeable(mozilla::Corner aCorner);

  // For the given solid corner, what color style should be used?
  BorderColorStyle BorderColorStyleForSolidCorner(uint8_t aStyle, mozilla::Corner aCorner);

  //
  // Path generation functions
  //

  // Get the Rect for drawing the given corner
  Rect GetCornerRect(mozilla::Corner aCorner);
  // add the path for drawing the given side without any adjacent corners to the context
  Rect GetSideClipWithoutCornersRect(mozilla::Side aSide);

  // Create a clip path for the wedge that this side of
  // the border should take up.  This is only called
  // when we're drawing separate border sides, so we know
  // that ADD compositing is taking place.
  //
  // This code needs to make sure that the individual pieces
  // don't ever (mathematically) overlap; the pixel overlap
  // is taken care of by the ADD compositing.
  already_AddRefed<Path> GetSideClipSubPath(mozilla::Side aSide);

  // Return start or end point for dashed/dotted side
  Point GetStraightBorderPoint(mozilla::Side aSide,
                               mozilla::Corner aCorner,
                               bool* aIsUnfilled,
                               Float aDotOffset = 0.0f);

  // Return bezier control points for the outer and the inner curve for given
  // corner
  void GetOuterAndInnerBezier(Bezier* aOuterBezier,
                              Bezier* aInnerBezier,
                              mozilla::Corner aCorner);

  // Given a set of sides to fill and a color, do so in the fastest way.
  //
  // Stroke tends to be faster for smaller borders because it doesn't go
  // through the tessellator, which has initialization overhead.  If
  // we're rendering all sides, we can use stroke at any thickness; we
  // also do TL/BR pairs at 1px thickness using stroke.
  //
  // If we can't stroke, then if it's a TL/BR pair, we use the specific
  // TL/BR paths.  Otherwise, we do the full path and fill.
  //
  // Calling code is expected to only set up a clip as necessary; no
  // clip is needed if we can render the entire border in 1 or 2 passes.
  void FillSolidBorder(const Rect& aOuterRect,
                       const Rect& aInnerRect,
                       const RectCornerRadii& aBorderRadii,
                       const Float* aBorderSizes,
                       int aSides,
                       const ColorPattern& aColor);

  //
  // core rendering
  //

  // draw the border for the given sides, using the style of the first side
  // present in the bitmask
  void DrawBorderSides (int aSides);

  // Setup the stroke options for the given dashed/dotted side
  void SetupDashedOptions(StrokeOptions* aStrokeOptions,
                          Float aDash[2], mozilla::Side aSide,
                          Float aBorderLength, bool isCorner);

  // Draw the given dashed/dotte side
  void DrawDashedOrDottedSide(mozilla::Side aSide);

  // Draw the given dotted side, each dot separately
  void DrawDottedSideSlow(mozilla::Side aSide);

  // Draw the given dashed/dotted corner
  void DrawDashedOrDottedCorner(mozilla::Side aSide,
                                mozilla::Corner aCorner);

  // Draw the given dotted corner, each segment separately
  void DrawDottedCornerSlow(mozilla::Side aSide,
                            mozilla::Corner aCorner);

  // Draw the given dashed corner, each dot separately
  void DrawDashedCornerSlow(mozilla::Side aSide,
                            mozilla::Corner aCorner);

  // Draw the given dashed/dotted corner with solid style
  void DrawFallbackSolidCorner(mozilla::Side aSide,
                               mozilla::Corner aCorner);

  // Analyze if all border sides have the same width.
  bool AllBordersSameWidth();

  // Analyze if all borders are 'solid' this also considers hidden or 'none'
  // borders because they can be considered 'solid' borders of 0 width and
  // with no color effect.
  bool AllBordersSolid();

  // Draw a solid color border that is uniformly the same width.
  void DrawSingleWidthSolidBorder();

  // Draw any border which is solid on all sides.
  void DrawSolidBorder();
};

class nsCSSBorderImageRenderer final
{
  typedef mozilla::nsImageRenderer nsImageRenderer;
public:
  static mozilla::Maybe<nsCSSBorderImageRenderer>
  CreateBorderImageRenderer(nsPresContext* aPresContext,
                            nsIFrame* aForFrame,
                            const nsRect& aBorderArea,
                            const nsStyleBorder& aStyleBorder,
                            const nsRect& aDirtyRect,
                            nsIFrame::Sides aSkipSides,
                            uint32_t aFlags,
                            mozilla::image::ImgDrawResult* aDrawResult);

  mozilla::image::ImgDrawResult
  DrawBorderImage(nsPresContext* aPresContext,
                  gfxContext& aRenderingContext,
                  nsIFrame* aForFrame,
                  const nsRect& aDirtyRect);
  void
  CreateWebRenderCommands(nsDisplayItem* aItem,
                          nsIFrame* aForFrame,
                          mozilla::wr::DisplayListBuilder& aBuilder,
                          mozilla::wr::IpcResourceUpdateQueue& aResources,
                          const mozilla::layers::StackingContextHelper& aSc,
                          mozilla::layers::WebRenderLayerManager* aManager,
                          nsDisplayListBuilder* aDisplayListBuilder);

  nsCSSBorderImageRenderer(const nsCSSBorderImageRenderer& aRhs);
  nsCSSBorderImageRenderer& operator=(const nsCSSBorderImageRenderer& aRhs);

private:
  nsCSSBorderImageRenderer(nsIFrame* aForFrame,
                           const nsRect& aBorderArea,
                           const nsStyleBorder& aStyleBorder,
                           nsIFrame::Sides aSkipSides,
                           const nsImageRenderer& aImageRenderer);

  nsImageRenderer mImageRenderer;
  nsSize mImageSize;
  nsMargin mSlice;
  nsMargin mWidths;
  nsMargin mImageOutset;
  nsRect mArea;
  nsRect mClip;
  mozilla::StyleBorderImageRepeat mRepeatModeHorizontal;
  mozilla::StyleBorderImageRepeat mRepeatModeVertical;
  uint8_t mFill;

  friend class nsDisplayBorder;
  friend struct nsCSSRendering;
};

namespace mozilla {
#ifdef DEBUG_NEW_BORDERS
#include <stdarg.h>

static inline void PrintAsString(const mozilla::gfx::Point& p) {
  fprintf (stderr, "[%f,%f]", p.x, p.y);
}

static inline void PrintAsString(const mozilla::gfx::Size& s) {
  fprintf (stderr, "[%f %f]", s.width, s.height);
}

static inline void PrintAsString(const mozilla::gfx::Rect& r) {
  fprintf (stderr, "[%f %f %f %f]", r.X(), r.Y(), r.Width(), r.Height());
}

static inline void PrintAsString(const mozilla::gfx::Float f) {
  fprintf (stderr, "%f", f);
}

static inline void PrintAsString(const char *s) {
  fprintf (stderr, "%s", s);
}

static inline void PrintAsStringNewline(const char *s = nullptr) {
  if (s)
    fprintf (stderr, "%s", s);
  fprintf (stderr, "\n");
  fflush (stderr);
}

static inline MOZ_FORMAT_PRINTF(1, 2) void PrintAsFormatString(const char *fmt, ...) {
  va_list vl;
  va_start(vl, fmt);
  vfprintf (stderr, fmt, vl);
  va_end(vl);
}

#else
static inline void PrintAsString(const mozilla::gfx::Point& p) {}
static inline void PrintAsString(const mozilla::gfx::Size& s) {}
static inline void PrintAsString(const mozilla::gfx::Rect& r) {}
static inline void PrintAsString(const mozilla::gfx::Float f) {}
static inline void PrintAsString(const char *s) {}
static inline void PrintAsStringNewline(const char *s = nullptr) {}
static inline MOZ_FORMAT_PRINTF(1, 2) void PrintAsFormatString(const char *fmt, ...) {}
#endif

} // namespace mozilla

#endif /* NS_CSS_RENDERING_BORDERS_H */