windowrectangles.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright 2016 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. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkClipOp.h"
  10. #include "include/core/SkColorSpace.h"
  11. #include "include/core/SkImageInfo.h"
  12. #include "include/core/SkMatrix.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRRect.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkRegion.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkTypes.h"
  22. #include "include/gpu/GrBackendSurface.h"
  23. #include "include/gpu/GrContext.h"
  24. #include "include/private/GrTypesPriv.h"
  25. #include "include/private/SkColorData.h"
  26. #include "src/core/SkClipOpPriv.h"
  27. #include "src/core/SkClipStack.h"
  28. #include "src/gpu/GrAppliedClip.h"
  29. #include "src/gpu/GrCaps.h"
  30. #include "src/gpu/GrClip.h"
  31. #include "src/gpu/GrContextPriv.h"
  32. #include "src/gpu/GrFixedClip.h"
  33. #include "src/gpu/GrFragmentProcessor.h"
  34. #include "src/gpu/GrPaint.h"
  35. #include "src/gpu/GrReducedClip.h"
  36. #include "src/gpu/GrRenderTargetContext.h"
  37. #include "src/gpu/GrRenderTargetContextPriv.h"
  38. #include "src/gpu/GrStencilClip.h"
  39. #include "src/gpu/GrTextureProxy.h"
  40. #include "src/gpu/GrUserStencilSettings.h"
  41. #include "src/gpu/effects/GrTextureDomain.h"
  42. #include "tools/ToolUtils.h"
  43. #include <utility>
  44. class GrRecordingContext;
  45. constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
  46. constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
  47. namespace skiagm {
  48. ////////////////////////////////////////////////////////////////////////////////////////////////////
  49. class WindowRectanglesBaseGM : public GM {
  50. protected:
  51. virtual DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) = 0;
  52. private:
  53. SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
  54. DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
  55. };
  56. DrawResult WindowRectanglesBaseGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
  57. ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
  58. SkClipStack stack;
  59. stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
  60. kDifference_SkClipOp, false);
  61. stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
  62. kDifference_SkClipOp, true);
  63. stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
  64. SkMatrix::I(), kDifference_SkClipOp, true);
  65. SkRRect nine;
  66. nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
  67. stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
  68. SkRRect complx;
  69. SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}};
  70. complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii);
  71. stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
  72. return this->onCoverClipStack(stack, canvas, errorMsg);
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////
  75. /**
  76. * Draws a clip that will exercise window rectangles if they are supported.
  77. */
  78. class WindowRectanglesGM : public WindowRectanglesBaseGM {
  79. private:
  80. SkString onShortName() final { return SkString("windowrectangles"); }
  81. DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
  82. };
  83. DrawResult WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
  84. SkString* errorMsg) {
  85. SkPaint paint;
  86. paint.setColor(0xff00aa80);
  87. // Set up the canvas's clip to match our SkClipStack.
  88. SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
  89. for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
  90. SkClipOp op = element->getOp();
  91. bool isAA = element->isAA();
  92. switch (element->getDeviceSpaceType()) {
  93. case SkClipStack::Element::DeviceSpaceType::kPath:
  94. canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
  95. break;
  96. case SkClipStack::Element::DeviceSpaceType::kRRect:
  97. canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
  98. break;
  99. case SkClipStack::Element::DeviceSpaceType::kRect:
  100. canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
  101. break;
  102. case SkClipStack::Element::DeviceSpaceType::kEmpty:
  103. canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
  104. break;
  105. }
  106. }
  107. canvas->drawRect(SkRect::Make(kCoverRect), paint);
  108. return DrawResult::kOk;
  109. }
  110. DEF_GM( return new WindowRectanglesGM(); )
  111. ////////////////////////////////////////////////////////////////////////////////////////////////////
  112. constexpr static int kNumWindows = 8;
  113. /**
  114. * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
  115. * this test is to verify that window rectangles are being used during clip mask generation, and to
  116. * visualize where the window rectangles are placed.
  117. *
  118. * We use window rectangles when generating the clip mask because there is no need to invest time
  119. * defining those regions where window rectangles will be in effect during the actual draw anyway.
  120. *
  121. * This test works by filling the entire clip mask with a small checkerboard pattern before drawing
  122. * it, and then covering the mask with a solid color once it has been generated. The regions inside
  123. * window rectangles or outside the scissor should still have the initial checkerboard intact.
  124. */
  125. class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
  126. private:
  127. constexpr static int kMaskCheckerSize = 5;
  128. SkString onShortName() final { return SkString("windowrectangles_mask"); }
  129. DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
  130. void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
  131. void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
  132. void stencilCheckerboard(GrRenderTargetContext*, bool flip);
  133. };
  134. /**
  135. * Base class for GrClips that visualize a clip mask.
  136. */
  137. class MaskOnlyClipBase : public GrClip {
  138. private:
  139. bool quickContains(const SkRect&) const final { return false; }
  140. bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
  141. void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
  142. rect->set(0, 0, width, height);
  143. if (iior) {
  144. *iior = false;
  145. }
  146. }
  147. };
  148. /**
  149. * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask.
  150. */
  151. class AlphaOnlyClip final : public MaskOnlyClipBase {
  152. public:
  153. AlphaOnlyClip(sk_sp<GrTextureProxy> mask, int x, int y) : fMask(mask), fX(x), fY(y) {}
  154. private:
  155. bool apply(GrRecordingContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
  156. SkRect* bounds) const override {
  157. int w = fMask->width();
  158. int h = fMask->height();
  159. out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
  160. fMask, SkIRect::MakeWH(w, h), {fX, fY}));
  161. return true;
  162. }
  163. sk_sp<GrTextureProxy> fMask;
  164. int fX;
  165. int fY;
  166. };
  167. /**
  168. * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
  169. */
  170. static GrStencilClip make_stencil_only_clip() {
  171. return GrStencilClip(SkClipStack::kEmptyGenID);
  172. };
  173. DrawResult WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
  174. SkString* errorMsg) {
  175. GrContext* ctx = canvas->getGrContext();
  176. GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
  177. if (!ctx || !rtc) {
  178. *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
  179. return DrawResult::kSkip;
  180. }
  181. if (rtc->priv().maxWindowRectangles() < kNumWindows) {
  182. *errorMsg = "Requires at least 8 window rectangles. "
  183. "(Are you off FBO 0? Use sRGB to force offscreen rendering.)";
  184. return DrawResult::kSkip;
  185. }
  186. const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
  187. GrPaint paint;
  188. if (rtc->numSamples() <= 1) {
  189. paint.setColor4f({ 0, 0.25f, 1, 1 });
  190. this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
  191. } else {
  192. paint.setColor4f({ 1, 0.25f, 0.25f, 1 });
  193. this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
  194. }
  195. return DrawResult::kOk;
  196. }
  197. void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
  198. const GrReducedClip& reducedClip, GrPaint&& paint) {
  199. const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
  200. const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
  201. sk_sp<GrRenderTargetContext> maskRTC(ctx->priv().makeDeferredRenderTargetContextWithFallback(
  202. SkBackingFit::kExact, kCoverRect.width() + padRight, kCoverRect.height() + padBottom,
  203. GrColorType::kAlpha_8, nullptr));
  204. if (!maskRTC) {
  205. return;
  206. }
  207. // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
  208. // the clip mask generation.
  209. this->stencilCheckerboard(maskRTC.get(), true);
  210. maskRTC->clear(nullptr, SK_PMColor4fWHITE, GrRenderTargetContext::CanClearFullscreen::kYes);
  211. GrPaint stencilPaint;
  212. stencilPaint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op, false);
  213. maskRTC->priv().stencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
  214. std::move(stencilPaint), GrAA::kNo, SkMatrix::I(),
  215. SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
  216. reducedClip.drawAlphaClipMask(maskRTC.get());
  217. int x = kCoverRect.x() - kDeviceRect.x(),
  218. y = kCoverRect.y() - kDeviceRect.y();
  219. // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
  220. // inside window rectangles or outside the scissor should still have the initial checkerboard
  221. // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
  222. AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
  223. rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
  224. SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
  225. }
  226. void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
  227. const GrReducedClip& reducedClip,
  228. GrPaint&& paint) {
  229. // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
  230. // by the clip mask generation.
  231. this->stencilCheckerboard(rtc, false);
  232. reducedClip.drawStencilClipMask(ctx, rtc);
  233. // Now visualize the stencil mask by covering the entire render target. The regions inside
  234. // window rectangles or outside the scissor should still have the initial checkerboard intact.
  235. // (This verifies we didn't spend any time modifying those pixels in the mask.)
  236. rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
  237. }
  238. void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
  239. constexpr static GrUserStencilSettings kSetClip(
  240. GrUserStencilSettings::StaticInit<
  241. 0,
  242. GrUserStencilTest::kAlways,
  243. 0,
  244. GrUserStencilOp::kSetClipBit,
  245. GrUserStencilOp::kKeep,
  246. 0>()
  247. );
  248. rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
  249. for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
  250. for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
  251. x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
  252. SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
  253. GrPaint paint;
  254. paint.setXPFactory(GrDisableColorXPFactory::Get());
  255. rtc->priv().stencilRect(GrNoClip(), &kSetClip, std::move(paint), GrAA::kNo,
  256. SkMatrix::I(), SkRect::Make(checker));
  257. }
  258. }
  259. }
  260. DEF_GM( return new WindowRectanglesMaskGM(); )
  261. }