Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrClip.h
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrClip_DEFINED
9 #define GrClip_DEFINED
10 
11 #include "SkRect.h"
12 #include "SkRRect.h"
13 
14 class GrAppliedClip;
15 class GrContext;
16 class GrDrawContext;
17 
22 class GrClip {
23 public:
24  virtual bool quickContains(const SkRect&) const = 0;
25  virtual bool quickContains(const SkRRect& rrect) const {
26  return this->quickContains(rrect.getBounds());
27  }
28  virtual void getConservativeBounds(int width, int height, SkIRect* devResult,
29  bool* isIntersectionOfRects = nullptr) const = 0;
30  virtual bool apply(GrContext*, GrDrawContext*, bool useHWAA, bool hasUserStencilSettings,
31  GrAppliedClip* out) const = 0;
32 
33  virtual ~GrClip() {}
34 
42  constexpr static SkScalar kBoundsTolerance = 1e-3f;
43 
50  template<typename TRect> constexpr static bool IsInsideClip(const TRect& innerClipBounds,
51  const SkRect& queryBounds) {
52  return innerClipBounds.fRight - innerClipBounds.fLeft > kBoundsTolerance &&
53  innerClipBounds.fBottom - innerClipBounds.fTop > kBoundsTolerance &&
54  innerClipBounds.fLeft < queryBounds.fLeft + kBoundsTolerance &&
55  innerClipBounds.fTop < queryBounds.fTop + kBoundsTolerance &&
56  innerClipBounds.fRight > queryBounds.fRight - kBoundsTolerance &&
57  innerClipBounds.fBottom > queryBounds.fBottom - kBoundsTolerance;
58  }
59 
66  template<typename TRect> constexpr static bool IsOutsideClip(const TRect& outerClipBounds,
67  const SkRect& queryBounds) {
68  return outerClipBounds.fRight - outerClipBounds.fLeft <= kBoundsTolerance ||
69  outerClipBounds.fBottom - outerClipBounds.fTop <= kBoundsTolerance ||
70  outerClipBounds.fLeft >= queryBounds.fRight - kBoundsTolerance ||
71  outerClipBounds.fTop >= queryBounds.fBottom - kBoundsTolerance ||
72  outerClipBounds.fRight <= queryBounds.fLeft + kBoundsTolerance ||
73  outerClipBounds.fBottom <= queryBounds.fTop + kBoundsTolerance;
74  }
75 
79  static SkIRect GetPixelIBounds(const SkRect& bounds) {
80  return SkIRect::MakeLTRB(SkScalarFloorToInt(bounds.fLeft + kBoundsTolerance),
81  SkScalarFloorToInt(bounds.fTop + kBoundsTolerance),
82  SkScalarCeilToInt(bounds.fRight - kBoundsTolerance),
83  SkScalarCeilToInt(bounds.fBottom - kBoundsTolerance));
84  }
85 
89  static SkRect GetPixelBounds(const SkRect& bounds) {
90  return SkRect::MakeLTRB(SkScalarFloorToScalar(bounds.fLeft + kBoundsTolerance),
91  SkScalarFloorToScalar(bounds.fTop + kBoundsTolerance),
92  SkScalarCeilToScalar(bounds.fRight - kBoundsTolerance),
93  SkScalarCeilToScalar(bounds.fBottom - kBoundsTolerance));
94  }
95 
99  static bool IsPixelAligned(const SkRect& rect) {
100  return SkScalarAbs(SkScalarRoundToScalar(rect.fLeft) - rect.fLeft) <= kBoundsTolerance &&
101  SkScalarAbs(SkScalarRoundToScalar(rect.fTop) - rect.fTop) <= kBoundsTolerance &&
102  SkScalarAbs(SkScalarRoundToScalar(rect.fRight) - rect.fRight) <= kBoundsTolerance &&
103  SkScalarAbs(SkScalarRoundToScalar(rect.fBottom) - rect.fBottom) <= kBoundsTolerance;
104  }
105 };
106 
110 class GrNoClip final : public GrClip {
111 private:
112  bool quickContains(const SkRect&) const final {
113  return true;
114  }
115  void getConservativeBounds(int width, int height, SkIRect* devResult,
116  bool* isIntersectionOfRects) const final {
117  devResult->setXYWH(0, 0, width, height);
118  if (isIntersectionOfRects) {
119  *isIntersectionOfRects = true;
120  }
121  }
122  bool apply(GrContext*, GrDrawContext*, bool, bool, GrAppliedClip*) const final {
123  return true;
124  }
125 };
126 
127 #endif
static SkRect GetPixelBounds(const SkRect &bounds)
Returns the minimal pixel-aligned rect that counts as containing a given set of bounds.
Definition: GrClip.h:89
The SkRRect class represents a rounded rect with a potentially different radii for each corner...
Definition: SkRRect.h:48
static constexpr bool IsInsideClip(const TRect &innerClipBounds, const SkRect &queryBounds)
Returns true if the given query bounds count as entirely inside the clip.
Definition: GrClip.h:50
GrClip is an abstract base class for applying a clip.
Definition: GrClip.h:22
static constexpr SkScalar kBoundsTolerance
This is the maximum distance that a draw may extend beyond a clip's boundary and still count count as...
Definition: GrClip.h:42
Definition: GrDrawContext.h:51
Specialized implementation for no clip.
Definition: GrClip.h:110
Definition: GrContext.h:48
static SkIRect GetPixelIBounds(const SkRect &bounds)
Returns the minimal integer rect that counts as containing a given set of bounds. ...
Definition: GrClip.h:79
static constexpr bool IsOutsideClip(const TRect &outerClipBounds, const SkRect &queryBounds)
Returns true if the given query bounds count as entirely outside the clip.
Definition: GrClip.h:66
Definition: SkRect.h:390
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
static bool IsPixelAligned(const SkRect &rect)
Returns true if the given rect counts as aligned with pixel boundaries.
Definition: GrClip.h:99