blob: feda0398cd3a2db3676bd9beb8468c482f5b0b36 [file] [log] [blame]
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkClipOp.h"
11#include "include/core/SkColorSpace.h"
12#include "include/core/SkImageInfo.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkRegion.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTypes.h"
23#include "include/gpu/GrBackendSurface.h"
24#include "include/gpu/GrContext.h"
25#include "include/private/GrTextureProxy.h"
26#include "include/private/GrTypesPriv.h"
27#include "include/private/SkColorData.h"
28#include "src/core/SkClipOpPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/core/SkClipStack.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrAppliedClip.h"
31#include "src/gpu/GrCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040032#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040034#include "src/gpu/GrFixedClip.h"
35#include "src/gpu/GrFragmentProcessor.h"
36#include "src/gpu/GrPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrReducedClip.h"
38#include "src/gpu/GrRenderTargetContext.h"
39#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrStencilClip.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040041#include "src/gpu/GrUserStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/effects/GrTextureDomain.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040043#include "tools/ToolUtils.h"
44
45#include <utility>
46
47class GrRecordingContext;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070048
49constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070050constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070051
52namespace skiagm {
53
54////////////////////////////////////////////////////////////////////////////////////////////////////
55
56class WindowRectanglesBaseGM : public GM {
57protected:
Chris Dalton50e24d72019-02-07 16:20:09 -070058 virtual DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) = 0;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070059
60private:
61 SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
Chris Dalton50e24d72019-02-07 16:20:09 -070062 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070063};
64
Chris Dalton50e24d72019-02-07 16:20:09 -070065DrawResult WindowRectanglesBaseGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
Mike Kleinea3f0142019-03-20 11:12:10 -050066 ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070067
68 SkClipStack stack;
Brian Salomona3b45d42016-10-03 11:36:16 -040069 stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050070 kDifference_SkClipOp, false);
Brian Salomona3b45d42016-10-03 11:36:16 -040071 stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050072 kDifference_SkClipOp, true);
Brian Salomona3b45d42016-10-03 11:36:16 -040073 stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
Mike Reedc1f77742016-12-09 09:00:50 -050074 SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070075
76 SkRRect nine;
77 nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
Mike Reedc1f77742016-12-09 09:00:50 -050078 stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070079
80 SkRRect complx;
81 SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}};
82 complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii);
Mike Reedc1f77742016-12-09 09:00:50 -050083 stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070084
Chris Dalton50e24d72019-02-07 16:20:09 -070085 return this->onCoverClipStack(stack, canvas, errorMsg);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070086}
87
88////////////////////////////////////////////////////////////////////////////////////////////////////
89
90/**
91 * Draws a clip that will exercise window rectangles if they are supported.
92 */
93class WindowRectanglesGM : public WindowRectanglesBaseGM {
94private:
95 SkString onShortName() final { return SkString("windowrectangles"); }
Chris Dalton50e24d72019-02-07 16:20:09 -070096 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070097};
98
Chris Dalton50e24d72019-02-07 16:20:09 -070099DrawResult WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
100 SkString* errorMsg) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700101 SkPaint paint;
102 paint.setColor(0xff00aa80);
103
104 // Set up the canvas's clip to match our SkClipStack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700105 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
106 for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
Mike Reed598524d2017-03-08 13:13:44 -0500107 SkClipOp op = element->getOp();
108 bool isAA = element->isAA();
Brian Salomonf3b46e52017-08-30 11:37:57 -0400109 switch (element->getDeviceSpaceType()) {
110 case SkClipStack::Element::DeviceSpaceType::kPath:
111 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500112 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400113 case SkClipStack::Element::DeviceSpaceType::kRRect:
114 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500115 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400116 case SkClipStack::Element::DeviceSpaceType::kRect:
117 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500118 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400119 case SkClipStack::Element::DeviceSpaceType::kEmpty:
Mike Reed598524d2017-03-08 13:13:44 -0500120 canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
121 break;
122 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700123 }
124
125 canvas->drawRect(SkRect::Make(kCoverRect), paint);
Chris Dalton50e24d72019-02-07 16:20:09 -0700126 return DrawResult::kOk;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700127}
128
129DEF_GM( return new WindowRectanglesGM(); )
130
131////////////////////////////////////////////////////////////////////////////////////////////////////
132
mtklein0a441072016-09-06 11:45:31 -0700133constexpr static int kNumWindows = 8;
134
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700135/**
Chris Daltonc5348082018-03-30 15:59:38 +0000136 * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
137 * this test is to verify that window rectangles are being used during clip mask generation, and to
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700138 * visualize where the window rectangles are placed.
139 *
140 * We use window rectangles when generating the clip mask because there is no need to invest time
141 * defining those regions where window rectangles will be in effect during the actual draw anyway.
142 *
143 * This test works by filling the entire clip mask with a small checkerboard pattern before drawing
144 * it, and then covering the mask with a solid color once it has been generated. The regions inside
145 * window rectangles or outside the scissor should still have the initial checkerboard intact.
146 */
147class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
148private:
149 constexpr static int kMaskCheckerSize = 5;
150 SkString onShortName() final { return SkString("windowrectangles_mask"); }
Chris Dalton50e24d72019-02-07 16:20:09 -0700151 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
Chris Daltonc5348082018-03-30 15:59:38 +0000152 void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Salomon82f44312017-01-11 13:42:54 -0500153 void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Osman11052242016-10-27 14:47:55 -0400154 void stencilCheckerboard(GrRenderTargetContext*, bool flip);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700155};
156
157/**
158 * Base class for GrClips that visualize a clip mask.
159 */
160class MaskOnlyClipBase : public GrClip {
161private:
162 bool quickContains(const SkRect&) const final { return false; }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500163 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700164 void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
165 rect->set(0, 0, width, height);
166 if (iior) {
167 *iior = false;
168 }
169 }
170};
171
172/**
173 * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask.
174 */
175class AlphaOnlyClip final : public MaskOnlyClipBase {
176public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400177 AlphaOnlyClip(sk_sp<GrTextureProxy> mask, int x, int y) : fMask(mask), fX(x), fY(y) {}
178
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700179private:
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500180 bool apply(GrRecordingContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
Brian Salomon97180af2017-03-14 13:42:58 -0400181 SkRect* bounds) const override {
Brian Salomonaff329b2017-08-11 09:40:37 -0400182 int w = fMask->width();
183 int h = fMask->height();
184 out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
185 fMask, SkIRect::MakeWH(w, h), {fX, fY}));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700186 return true;
187 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400188 sk_sp<GrTextureProxy> fMask;
189 int fX;
190 int fY;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700191};
192
193/**
Chris Daltonbbfd5162017-11-07 13:35:22 -0700194 * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700195 */
Chris Daltonbbfd5162017-11-07 13:35:22 -0700196static GrStencilClip make_stencil_only_clip() {
197 return GrStencilClip(SkClipStack::kEmptyGenID);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700198};
199
Chris Dalton50e24d72019-02-07 16:20:09 -0700200DrawResult WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
201 SkString* errorMsg) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700202 GrContext* ctx = canvas->getGrContext();
Brian Osman11052242016-10-27 14:47:55 -0400203 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
Chris Dalton50e24d72019-02-07 16:20:09 -0700204 if (!ctx || !rtc) {
205 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
206 return DrawResult::kSkip;
207 }
208 if (rtc->priv().maxWindowRectangles() < kNumWindows) {
209 *errorMsg = "Requires at least 8 window rectangles. "
210 "(Are you off FBO 0? Use sRGB to force offscreen rendering.)";
211 return DrawResult::kSkip;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700212 }
213
Ethan Nicholaseace9352018-10-15 20:09:54 +0000214 const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700215
Chris Daltonc5348082018-03-30 15:59:38 +0000216 GrPaint paint;
Brian Salomon7c8460e2017-05-12 11:36:10 -0400217 if (GrFSAAType::kNone == rtc->fsaaType()) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400218 paint.setColor4f({ 0, 0.25f, 1, 1 });
Chris Daltonc5348082018-03-30 15:59:38 +0000219 this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700220 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400221 paint.setColor4f({ 1, 0.25f, 0.25f, 1 });
Chris Daltonc5348082018-03-30 15:59:38 +0000222 this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700223 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700224 return DrawResult::kOk;
Chris Daltonc5348082018-03-30 15:59:38 +0000225}
226
227void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
228 const GrReducedClip& reducedClip, GrPaint&& paint) {
229 const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
230 const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
Greg Daniel4065d452018-11-16 15:43:41 -0500231 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500232 ctx->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
Chris Daltonc5348082018-03-30 15:59:38 +0000233 sk_sp<GrRenderTargetContext> maskRTC(
Robert Phillips9da87e02019-02-04 13:26:26 -0500234 ctx->priv().makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500235 format, SkBackingFit::kExact,
Chris Daltonc5348082018-03-30 15:59:38 +0000236 kCoverRect.width() + padRight,
237 kCoverRect.height() + padBottom,
238 kAlpha_8_GrPixelConfig, nullptr));
239 if (!maskRTC) {
240 return;
241 }
242
243 // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
244 // the clip mask generation.
245 this->stencilCheckerboard(maskRTC.get(), true);
Brian Osman9a9baae2018-11-05 15:06:26 -0500246 maskRTC->clear(nullptr, SK_PMColor4fWHITE, GrRenderTargetContext::CanClearFullscreen::kYes);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400247 GrPaint stencilPaint;
248 stencilPaint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op, false);
249 maskRTC->priv().stencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
250 std::move(stencilPaint), GrAA::kNo, SkMatrix::I(),
251 SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
Chris Daltonc5348082018-03-30 15:59:38 +0000252 reducedClip.drawAlphaClipMask(maskRTC.get());
253
254 int x = kCoverRect.x() - kDeviceRect.x(),
255 y = kCoverRect.y() - kDeviceRect.y();
256
257 // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
258 // inside window rectangles or outside the scissor should still have the initial checkerboard
259 // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
260 AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
261 rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
262 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700263}
264
Brian Osman11052242016-10-27 14:47:55 -0400265void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700266 const GrReducedClip& reducedClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500267 GrPaint&& paint) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700268 // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
269 // by the clip mask generation.
Brian Osman11052242016-10-27 14:47:55 -0400270 this->stencilCheckerboard(rtc, false);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000271 reducedClip.drawStencilClipMask(ctx, rtc);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700272
273 // Now visualize the stencil mask by covering the entire render target. The regions inside
Robert Phillips806be2d2017-06-28 15:23:59 -0400274 // window rectangles or outside the scissor should still have the initial checkerboard intact.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700275 // (This verifies we didn't spend any time modifying those pixels in the mask.)
Chris Daltonbbfd5162017-11-07 13:35:22 -0700276 rtc->drawPaint(make_stencil_only_clip(), std::move(paint), SkMatrix::I());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700277}
278
Brian Osman11052242016-10-27 14:47:55 -0400279void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700280 constexpr static GrUserStencilSettings kSetClip(
281 GrUserStencilSettings::StaticInit<
282 0,
283 GrUserStencilTest::kAlways,
284 0,
285 GrUserStencilOp::kSetClipBit,
286 GrUserStencilOp::kKeep,
287 0>()
288 );
289
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000290 rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700291
Brian Salomon9a767722017-03-13 17:57:28 -0400292 for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700293 for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
Brian Salomon9a767722017-03-13 17:57:28 -0400294 x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700295 SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400296 GrPaint paint;
297 paint.setXPFactory(GrDisableColorXPFactory::Get());
298 rtc->priv().stencilRect(GrNoClip(), &kSetClip, std::move(paint), GrAA::kNo,
299 SkMatrix::I(), SkRect::Make(checker));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700300 }
301 }
302}
303
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700304DEF_GM( return new WindowRectanglesMaskGM(); )
305
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700306}