GrClip.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifndef GrClip_DEFINED
  8. #define GrClip_DEFINED
  9. #include "include/core/SkRRect.h"
  10. #include "include/core/SkRect.h"
  11. #include "src/gpu/GrAppliedClip.h"
  12. #include "src/gpu/GrRenderTargetContext.h"
  13. class GrContext;
  14. /**
  15. * GrClip is an abstract base class for applying a clip. It constructs a clip mask if necessary, and
  16. * fills out a GrAppliedClip instructing the caller on how to set up the draw state.
  17. */
  18. class GrClip {
  19. public:
  20. virtual bool quickContains(const SkRect&) const = 0;
  21. virtual bool quickContains(const SkRRect& rrect) const {
  22. return this->quickContains(rrect.getBounds());
  23. }
  24. virtual void getConservativeBounds(int width, int height, SkIRect* devResult,
  25. bool* isIntersectionOfRects = nullptr) const = 0;
  26. /**
  27. * This computes a GrAppliedClip from the clip which in turn can be used to build a GrPipeline.
  28. * To determine the appropriate clipping implementation the GrClip subclass must know whether
  29. * the draw will enable HW AA or uses the stencil buffer. On input 'bounds' is a conservative
  30. * bounds of the draw that is to be clipped. After return 'bounds' has been intersected with a
  31. * conservative bounds of the clip. A return value of false indicates that the draw can be
  32. * skipped as it is fully clipped out.
  33. */
  34. virtual bool apply(GrRecordingContext*, GrRenderTargetContext*, bool useHWAA,
  35. bool hasUserStencilSettings, GrAppliedClip*, SkRect* bounds) const = 0;
  36. virtual ~GrClip() {}
  37. /**
  38. * This method quickly and conservatively determines whether the entire clip is equivalent to
  39. * intersection with a rrect. This will only return true if the rrect does not fully contain
  40. * the render target bounds. Moreover, the returned rrect need not be contained by the render
  41. * target bounds. We assume all draws will be implicitly clipped by the render target bounds.
  42. *
  43. * @param rtBounds The bounds of the render target that the clip will be applied to.
  44. * @param rrect If return is true rrect will contain the rrect equivalent to the clip within
  45. * rtBounds.
  46. * @param aa If return is true aa will indicate whether the rrect clip is antialiased.
  47. * @return true if the clip is equivalent to a single rrect, false otherwise.
  48. *
  49. */
  50. virtual bool isRRect(const SkRect& rtBounds, SkRRect* rrect, GrAA* aa) const = 0;
  51. /**
  52. * This is the maximum distance that a draw may extend beyond a clip's boundary and still count
  53. * count as "on the other side". We leave some slack because floating point rounding error is
  54. * likely to blame. The rationale for 1e-3 is that in the coverage case (and barring unexpected
  55. * rounding), as long as coverage stays within 0.5 * 1/256 of its intended value it shouldn't
  56. * have any effect on the final pixel values.
  57. */
  58. constexpr static SkScalar kBoundsTolerance = 1e-3f;
  59. /**
  60. * Returns true if the given query bounds count as entirely inside the clip.
  61. *
  62. * @param innerClipBounds device-space rect contained by the clip (SkRect or SkIRect).
  63. * @param queryBounds device-space bounds of the query region.
  64. */
  65. template <typename TRect>
  66. constexpr static bool IsInsideClip(const TRect& innerClipBounds, const SkRect& queryBounds) {
  67. return innerClipBounds.fRight > innerClipBounds.fLeft + kBoundsTolerance &&
  68. innerClipBounds.fBottom > innerClipBounds.fTop + kBoundsTolerance &&
  69. innerClipBounds.fLeft < queryBounds.fLeft + kBoundsTolerance &&
  70. innerClipBounds.fTop < queryBounds.fTop + kBoundsTolerance &&
  71. innerClipBounds.fRight > queryBounds.fRight - kBoundsTolerance &&
  72. innerClipBounds.fBottom > queryBounds.fBottom - kBoundsTolerance;
  73. }
  74. /**
  75. * Returns true if the given query bounds count as entirely outside the clip.
  76. *
  77. * @param outerClipBounds device-space rect that contains the clip (SkRect or SkIRect).
  78. * @param queryBounds device-space bounds of the query region.
  79. */
  80. template <typename TRect>
  81. constexpr static bool IsOutsideClip(const TRect& outerClipBounds, const SkRect& queryBounds) {
  82. return
  83. // Is the clip so small that it is effectively empty?
  84. outerClipBounds.fRight - outerClipBounds.fLeft <= kBoundsTolerance ||
  85. outerClipBounds.fBottom - outerClipBounds.fTop <= kBoundsTolerance ||
  86. // Are the query bounds effectively outside the clip?
  87. outerClipBounds.fLeft >= queryBounds.fRight - kBoundsTolerance ||
  88. outerClipBounds.fTop >= queryBounds.fBottom - kBoundsTolerance ||
  89. outerClipBounds.fRight <= queryBounds.fLeft + kBoundsTolerance ||
  90. outerClipBounds.fBottom <= queryBounds.fTop + kBoundsTolerance;
  91. }
  92. /**
  93. * Returns the minimal integer rect that counts as containing a given set of bounds.
  94. */
  95. static SkIRect GetPixelIBounds(const SkRect& bounds) {
  96. return SkIRect::MakeLTRB(SkScalarFloorToInt(bounds.fLeft + kBoundsTolerance),
  97. SkScalarFloorToInt(bounds.fTop + kBoundsTolerance),
  98. SkScalarCeilToInt(bounds.fRight - kBoundsTolerance),
  99. SkScalarCeilToInt(bounds.fBottom - kBoundsTolerance));
  100. }
  101. /**
  102. * Returns the minimal pixel-aligned rect that counts as containing a given set of bounds.
  103. */
  104. static SkRect GetPixelBounds(const SkRect& bounds) {
  105. return SkRect::MakeLTRB(SkScalarFloorToScalar(bounds.fLeft + kBoundsTolerance),
  106. SkScalarFloorToScalar(bounds.fTop + kBoundsTolerance),
  107. SkScalarCeilToScalar(bounds.fRight - kBoundsTolerance),
  108. SkScalarCeilToScalar(bounds.fBottom - kBoundsTolerance));
  109. }
  110. /**
  111. * Returns true if the given rect counts as aligned with pixel boundaries.
  112. */
  113. static bool IsPixelAligned(const SkRect& rect) {
  114. return SkScalarAbs(SkScalarRoundToScalar(rect.fLeft) - rect.fLeft) <= kBoundsTolerance &&
  115. SkScalarAbs(SkScalarRoundToScalar(rect.fTop) - rect.fTop) <= kBoundsTolerance &&
  116. SkScalarAbs(SkScalarRoundToScalar(rect.fRight) - rect.fRight) <= kBoundsTolerance &&
  117. SkScalarAbs(SkScalarRoundToScalar(rect.fBottom) - rect.fBottom) <= kBoundsTolerance;
  118. }
  119. };
  120. /**
  121. * GrHardClip never uses coverage FPs. It can only enforce the clip using the already-existing
  122. * stencil buffer contents and/or fixed-function state like scissor. Always aliased if MSAA is off.
  123. */
  124. class GrHardClip : public GrClip {
  125. public:
  126. /**
  127. * Sets the appropriate hardware state modifications on GrAppliedHardClip that will implement
  128. * the clip. On input 'bounds' is a conservative bounds of the draw that is to be clipped. After
  129. * return 'bounds' has been intersected with a conservative bounds of the clip. A return value
  130. * of false indicates that the draw can be skipped as it is fully clipped out.
  131. */
  132. virtual bool apply(int rtWidth, int rtHeight, GrAppliedHardClip* out, SkRect* bounds) const = 0;
  133. private:
  134. bool apply(GrRecordingContext*, GrRenderTargetContext* rtc, bool useHWAA,
  135. bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const final {
  136. return this->apply(rtc->width(), rtc->height(), &out->hardClip(), bounds);
  137. }
  138. };
  139. /**
  140. * Specialized implementation for no clip.
  141. */
  142. class GrNoClip final : public GrHardClip {
  143. private:
  144. bool quickContains(const SkRect&) const final { return true; }
  145. bool quickContains(const SkRRect&) const final { return true; }
  146. void getConservativeBounds(int width, int height, SkIRect* devResult,
  147. bool* isIntersectionOfRects) const final {
  148. devResult->setXYWH(0, 0, width, height);
  149. if (isIntersectionOfRects) {
  150. *isIntersectionOfRects = true;
  151. }
  152. }
  153. bool apply(int rtWidth, int rtHeight, GrAppliedHardClip*, SkRect*) const final { return true; }
  154. bool isRRect(const SkRect&, SkRRect*, GrAA*) const override { return false; }
  155. };
  156. #endif