blob: e5fc14431bd120ce33f0caa78cf120e805effb67 [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"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040024#include "include/gpu/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/private/GrTypesPriv.h"
26#include "include/private/SkColorData.h"
27#include "src/core/SkClipOpPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkClipStack.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrAppliedClip.h"
30#include "src/gpu/GrCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include "src/gpu/GrClip.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040032#include "src/gpu/GrFragmentProcessor.h"
33#include "src/gpu/GrPaint.h"
Brian Salomon64227222020-02-26 13:28:42 -050034#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrReducedClip.h"
36#include "src/gpu/GrRenderTargetContext.h"
37#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrStencilClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040039#include "src/gpu/GrTextureProxy.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040040#include "src/gpu/GrUserStencilSettings.h"
Brian Salomon64227222020-02-26 13:28:42 -050041#include "src/gpu/effects/generated/GrDeviceSpaceEffect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include "tools/ToolUtils.h"
43
44#include <utility>
45
46class GrRecordingContext;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070047
48constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070049constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070050
51namespace skiagm {
52
53////////////////////////////////////////////////////////////////////////////////////////////////////
54
55class WindowRectanglesBaseGM : public GM {
56protected:
Chris Dalton50e24d72019-02-07 16:20:09 -070057 virtual DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) = 0;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070058
59private:
60 SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
Chris Dalton50e24d72019-02-07 16:20:09 -070061 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070062};
63
Chris Dalton50e24d72019-02-07 16:20:09 -070064DrawResult WindowRectanglesBaseGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
Mike Kleinea3f0142019-03-20 11:12:10 -050065 ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070066
67 SkClipStack stack;
Brian Salomona3b45d42016-10-03 11:36:16 -040068 stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050069 kDifference_SkClipOp, false);
Brian Salomona3b45d42016-10-03 11:36:16 -040070 stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050071 kDifference_SkClipOp, true);
Brian Salomona3b45d42016-10-03 11:36:16 -040072 stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
Mike Reedc1f77742016-12-09 09:00:50 -050073 SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070074
75 SkRRect nine;
76 nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
Mike Reedc1f77742016-12-09 09:00:50 -050077 stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070078
79 SkRRect complx;
80 SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}};
81 complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii);
Mike Reedc1f77742016-12-09 09:00:50 -050082 stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070083
Chris Dalton50e24d72019-02-07 16:20:09 -070084 return this->onCoverClipStack(stack, canvas, errorMsg);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070085}
86
87////////////////////////////////////////////////////////////////////////////////////////////////////
88
89/**
90 * Draws a clip that will exercise window rectangles if they are supported.
91 */
92class WindowRectanglesGM : public WindowRectanglesBaseGM {
93private:
94 SkString onShortName() final { return SkString("windowrectangles"); }
Chris Dalton50e24d72019-02-07 16:20:09 -070095 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070096};
97
Chris Dalton50e24d72019-02-07 16:20:09 -070098DrawResult WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
99 SkString* errorMsg) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700100 SkPaint paint;
101 paint.setColor(0xff00aa80);
102
103 // Set up the canvas's clip to match our SkClipStack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700104 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
105 for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
Mike Reed598524d2017-03-08 13:13:44 -0500106 SkClipOp op = element->getOp();
107 bool isAA = element->isAA();
Brian Salomonf3b46e52017-08-30 11:37:57 -0400108 switch (element->getDeviceSpaceType()) {
Michael Ludwig4e221bd2020-06-05 11:29:36 -0400109 case SkClipStack::Element::DeviceSpaceType::kShader:
110 canvas->clipShader(element->refShader(), op);
111 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400112 case SkClipStack::Element::DeviceSpaceType::kPath:
113 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500114 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400115 case SkClipStack::Element::DeviceSpaceType::kRRect:
116 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500117 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400118 case SkClipStack::Element::DeviceSpaceType::kRect:
119 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500120 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400121 case SkClipStack::Element::DeviceSpaceType::kEmpty:
Mike Reed598524d2017-03-08 13:13:44 -0500122 canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
123 break;
124 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700125 }
126
127 canvas->drawRect(SkRect::Make(kCoverRect), paint);
Chris Dalton50e24d72019-02-07 16:20:09 -0700128 return DrawResult::kOk;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700129}
130
131DEF_GM( return new WindowRectanglesGM(); )
132
133////////////////////////////////////////////////////////////////////////////////////////////////////
134
mtklein0a441072016-09-06 11:45:31 -0700135constexpr static int kNumWindows = 8;
136
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700137/**
Chris Daltonc5348082018-03-30 15:59:38 +0000138 * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
139 * this test is to verify that window rectangles are being used during clip mask generation, and to
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700140 * visualize where the window rectangles are placed.
141 *
142 * We use window rectangles when generating the clip mask because there is no need to invest time
143 * defining those regions where window rectangles will be in effect during the actual draw anyway.
144 *
145 * This test works by filling the entire clip mask with a small checkerboard pattern before drawing
146 * it, and then covering the mask with a solid color once it has been generated. The regions inside
147 * window rectangles or outside the scissor should still have the initial checkerboard intact.
148 */
149class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
150private:
151 constexpr static int kMaskCheckerSize = 5;
152 SkString onShortName() final { return SkString("windowrectangles_mask"); }
Chris Dalton50e24d72019-02-07 16:20:09 -0700153 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
Robert Phillips95c250c2020-06-29 15:36:12 -0400154 void visualizeAlphaMask(GrRecordingContext*, GrRenderTargetContext*,
155 const GrReducedClip&, GrPaint&&);
156 void visualizeStencilMask(GrRecordingContext*, GrRenderTargetContext*,
157 const GrReducedClip&, GrPaint&&);
Brian Osman11052242016-10-27 14:47:55 -0400158 void stencilCheckerboard(GrRenderTargetContext*, bool flip);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700159};
160
161/**
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700162 * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask.
163 */
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400164class AlphaOnlyClip final : public GrClip {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700165public:
Greg Daniele32506b2020-02-10 16:00:54 -0500166 AlphaOnlyClip(GrSurfaceProxyView mask, int x, int y) : fMask(std::move(mask)), fX(x), fY(y) {}
Brian Salomonaff329b2017-08-11 09:40:37 -0400167
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700168private:
Michael Ludwige06a8972020-06-11 10:29:00 -0400169 SkIRect getConservativeBounds() const final {
170 return SkIRect::MakeXYWH(fX, fY, fMask.width(), fMask.height());
171 }
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400172 Effect apply(GrRecordingContext* ctx, GrRenderTargetContext*, bool, bool,
173 GrAppliedClip* out, SkRect* bounds) const override {
Brian Salomon64227222020-02-26 13:28:42 -0500174 GrSamplerState samplerState(GrSamplerState::WrapMode::kClampToBorder,
175 GrSamplerState::Filter::kNearest);
Mike Reed1f607332020-05-21 12:11:27 -0400176 auto m = SkMatrix::Translate(-fX, -fY);
Brian Salomon64227222020-02-26 13:28:42 -0500177 auto subset = SkRect::Make(fMask.dimensions());
178 auto domain = bounds->makeOffset(-fX, -fY).makeInset(0.5, 0.5);
179 auto fp = GrTextureEffect::MakeSubset(fMask, kPremul_SkAlphaType, m, samplerState, subset,
180 domain, *ctx->priv().caps());
181 fp = GrDeviceSpaceEffect::Make(std::move(fp));
182 out->addCoverageFP(std::move(fp));
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400183 return Effect::kClipped;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700184 }
Greg Daniele32506b2020-02-10 16:00:54 -0500185 GrSurfaceProxyView fMask;
Brian Salomonaff329b2017-08-11 09:40:37 -0400186 int fX;
187 int fY;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700188};
189
190/**
Chris Daltonbbfd5162017-11-07 13:35:22 -0700191 * Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700192 */
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400193static GrStencilClip make_stencil_only_clip(GrRenderTargetContext* rtc) {
194 return GrStencilClip(rtc->dimensions(), SkClipStack::kEmptyGenID);
195};
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700196
Chris Dalton50e24d72019-02-07 16:20:09 -0700197DrawResult WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400198 SkString* errorMsg) {
Robert Phillips95c250c2020-06-29 15:36:12 -0400199 auto ctx = canvas->recordingContext();
Brian Osman11052242016-10-27 14:47:55 -0400200 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
Chris Dalton50e24d72019-02-07 16:20:09 -0700201 if (!ctx || !rtc) {
202 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
203 return DrawResult::kSkip;
204 }
205 if (rtc->priv().maxWindowRectangles() < kNumWindows) {
206 *errorMsg = "Requires at least 8 window rectangles. "
207 "(Are you off FBO 0? Use sRGB to force offscreen rendering.)";
208 return DrawResult::kSkip;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700209 }
210
Ethan Nicholaseace9352018-10-15 20:09:54 +0000211 const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700212
Chris Daltonc5348082018-03-30 15:59:38 +0000213 GrPaint paint;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600214 if (rtc->numSamples() <= 1) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400215 paint.setColor4f({ 0, 0.25f, 1, 1 });
Chris Daltonc5348082018-03-30 15:59:38 +0000216 this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700217 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400218 paint.setColor4f({ 1, 0.25f, 0.25f, 1 });
Chris Daltonc5348082018-03-30 15:59:38 +0000219 this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700220 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700221 return DrawResult::kOk;
Chris Daltonc5348082018-03-30 15:59:38 +0000222}
223
Robert Phillips95c250c2020-06-29 15:36:12 -0400224void WindowRectanglesMaskGM::visualizeAlphaMask(GrRecordingContext* ctx, GrRenderTargetContext* rtc,
Chris Daltonc5348082018-03-30 15:59:38 +0000225 const GrReducedClip& reducedClip, GrPaint&& paint) {
226 const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
227 const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
Greg Daniele20fcad2020-01-08 11:52:34 -0500228 auto maskRTC = GrRenderTargetContext::MakeWithFallback(
229 ctx, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact,
230 {kCoverRect.width() + padRight, kCoverRect.height() + padBottom});
Chris Daltonc5348082018-03-30 15:59:38 +0000231 if (!maskRTC) {
232 return;
233 }
234
235 // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
236 // the clip mask generation.
237 this->stencilCheckerboard(maskRTC.get(), true);
Michael Ludwig81d41722020-05-26 16:57:38 -0400238 maskRTC->clear(SK_PMColor4fWHITE);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400239 GrPaint stencilPaint;
240 stencilPaint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op, false);
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400241 GrStencilClip stencilClip = make_stencil_only_clip(maskRTC.get());
242 maskRTC->priv().stencilRect(&stencilClip, &GrUserStencilSettings::kUnused,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400243 std::move(stencilPaint), GrAA::kNo, SkMatrix::I(),
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400244 SkRect::Make(maskRTC->dimensions()));
Chris Daltonc5348082018-03-30 15:59:38 +0000245 reducedClip.drawAlphaClipMask(maskRTC.get());
246
247 int x = kCoverRect.x() - kDeviceRect.x(),
248 y = kCoverRect.y() - kDeviceRect.y();
249
250 // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
251 // inside window rectangles or outside the scissor should still have the initial checkerboard
252 // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400253 AlphaOnlyClip alphaClip(maskRTC->readSurfaceView(), x, y);
254 rtc->drawRect(&alphaClip, std::move(paint), GrAA::kYes, SkMatrix::I(),
Chris Daltonc5348082018-03-30 15:59:38 +0000255 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700256}
257
Robert Phillips95c250c2020-06-29 15:36:12 -0400258void WindowRectanglesMaskGM::visualizeStencilMask(GrRecordingContext* ctx,
259 GrRenderTargetContext* rtc,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700260 const GrReducedClip& reducedClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500261 GrPaint&& paint) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400262 if (ctx->abandoned()) {
Michael Ludwig828d3412020-05-12 13:15:35 -0400263 // GrReducedClip assumes the context hasn't been abandoned, which is reasonable since it is
264 // only ever used if a draw op is made. Since this GM calls it directly, it has to be taken
265 // into account.
266 return;
267 }
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.)
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400276 GrStencilClip clip = make_stencil_only_clip(rtc);
277 rtc->drawPaint(&clip, std::move(paint), SkMatrix::I());
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700278}
279
Brian Osman11052242016-10-27 14:47:55 -0400280void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700281 constexpr static GrUserStencilSettings kSetClip(
282 GrUserStencilSettings::StaticInit<
283 0,
284 GrUserStencilTest::kAlways,
285 0,
286 GrUserStencilOp::kSetClipBit,
287 GrUserStencilOp::kKeep,
288 0>()
289 );
290
Michael Ludwig81d41722020-05-26 16:57:38 -0400291 rtc->priv().clearStencilClip(SkIRect::MakeSize(rtc->dimensions()), false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700292
Brian Salomon9a767722017-03-13 17:57:28 -0400293 for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700294 for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
Brian Salomon9a767722017-03-13 17:57:28 -0400295 x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700296 SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400297 GrPaint paint;
298 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwig7c12e282020-05-29 09:54:07 -0400299 rtc->priv().stencilRect(nullptr, &kSetClip, std::move(paint), GrAA::kNo,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400300 SkMatrix::I(), SkRect::Make(checker));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700301 }
302 }
303}
304
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700305DEF_GM( return new WindowRectanglesMaskGM(); )
306
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700307}