blob: d7714ff0e37e2cafa227977895ed1b7d85be964d [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"
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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "src/gpu/GrFixedClip.h"
34#include "src/gpu/GrFragmentProcessor.h"
35#include "src/gpu/GrPaint.h"
Brian Salomon64227222020-02-26 13:28:42 -050036#include "src/gpu/GrRecordingContextPriv.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"
Greg Danielf91aeb22019-06-18 09:58:02 -040041#include "src/gpu/GrTextureProxy.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include "src/gpu/GrUserStencilSettings.h"
Brian Salomon64227222020-02-26 13:28:42 -050043#include "src/gpu/effects/generated/GrDeviceSpaceEffect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040044#include "tools/ToolUtils.h"
45
46#include <utility>
47
48class GrRecordingContext;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070049
50constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070051constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
csmartdaltonbf4a8f92016-09-06 10:01:06 -070052
53namespace skiagm {
54
55////////////////////////////////////////////////////////////////////////////////////////////////////
56
57class WindowRectanglesBaseGM : public GM {
58protected:
Chris Dalton50e24d72019-02-07 16:20:09 -070059 virtual DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) = 0;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070060
61private:
62 SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
Chris Dalton50e24d72019-02-07 16:20:09 -070063 DrawResult onDraw(SkCanvas*, SkString* errorMsg) final;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070064};
65
Chris Dalton50e24d72019-02-07 16:20:09 -070066DrawResult WindowRectanglesBaseGM::onDraw(SkCanvas* canvas, SkString* errorMsg) {
Mike Kleinea3f0142019-03-20 11:12:10 -050067 ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070068
69 SkClipStack stack;
Brian Salomona3b45d42016-10-03 11:36:16 -040070 stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050071 kDifference_SkClipOp, false);
Brian Salomona3b45d42016-10-03 11:36:16 -040072 stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
Mike Reedc1f77742016-12-09 09:00:50 -050073 kDifference_SkClipOp, true);
Brian Salomona3b45d42016-10-03 11:36:16 -040074 stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
Mike Reedc1f77742016-12-09 09:00:50 -050075 SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070076
77 SkRRect nine;
78 nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
Mike Reedc1f77742016-12-09 09:00:50 -050079 stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070080
81 SkRRect complx;
82 SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}};
83 complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii);
Mike Reedc1f77742016-12-09 09:00:50 -050084 stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070085
Chris Dalton50e24d72019-02-07 16:20:09 -070086 return this->onCoverClipStack(stack, canvas, errorMsg);
csmartdaltonbf4a8f92016-09-06 10:01:06 -070087}
88
89////////////////////////////////////////////////////////////////////////////////////////////////////
90
91/**
92 * Draws a clip that will exercise window rectangles if they are supported.
93 */
94class WindowRectanglesGM : public WindowRectanglesBaseGM {
95private:
96 SkString onShortName() final { return SkString("windowrectangles"); }
Chris Dalton50e24d72019-02-07 16:20:09 -070097 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
csmartdaltonbf4a8f92016-09-06 10:01:06 -070098};
99
Chris Dalton50e24d72019-02-07 16:20:09 -0700100DrawResult WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas,
101 SkString* errorMsg) {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700102 SkPaint paint;
103 paint.setColor(0xff00aa80);
104
105 // Set up the canvas's clip to match our SkClipStack.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700106 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
107 for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
Mike Reed598524d2017-03-08 13:13:44 -0500108 SkClipOp op = element->getOp();
109 bool isAA = element->isAA();
Brian Salomonf3b46e52017-08-30 11:37:57 -0400110 switch (element->getDeviceSpaceType()) {
111 case SkClipStack::Element::DeviceSpaceType::kPath:
112 canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500113 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400114 case SkClipStack::Element::DeviceSpaceType::kRRect:
115 canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500116 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400117 case SkClipStack::Element::DeviceSpaceType::kRect:
118 canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
Mike Reed598524d2017-03-08 13:13:44 -0500119 break;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400120 case SkClipStack::Element::DeviceSpaceType::kEmpty:
Mike Reed598524d2017-03-08 13:13:44 -0500121 canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
122 break;
123 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700124 }
125
126 canvas->drawRect(SkRect::Make(kCoverRect), paint);
Chris Dalton50e24d72019-02-07 16:20:09 -0700127 return DrawResult::kOk;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700128}
129
130DEF_GM( return new WindowRectanglesGM(); )
131
132////////////////////////////////////////////////////////////////////////////////////////////////////
133
mtklein0a441072016-09-06 11:45:31 -0700134constexpr static int kNumWindows = 8;
135
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700136/**
Chris Daltonc5348082018-03-30 15:59:38 +0000137 * Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
138 * this test is to verify that window rectangles are being used during clip mask generation, and to
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700139 * visualize where the window rectangles are placed.
140 *
141 * We use window rectangles when generating the clip mask because there is no need to invest time
142 * defining those regions where window rectangles will be in effect during the actual draw anyway.
143 *
144 * This test works by filling the entire clip mask with a small checkerboard pattern before drawing
145 * it, and then covering the mask with a solid color once it has been generated. The regions inside
146 * window rectangles or outside the scissor should still have the initial checkerboard intact.
147 */
148class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
149private:
150 constexpr static int kMaskCheckerSize = 5;
151 SkString onShortName() final { return SkString("windowrectangles_mask"); }
Chris Dalton50e24d72019-02-07 16:20:09 -0700152 DrawResult onCoverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg) final;
Chris Daltonc5348082018-03-30 15:59:38 +0000153 void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Salomon82f44312017-01-11 13:42:54 -0500154 void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
Brian Osman11052242016-10-27 14:47:55 -0400155 void stencilCheckerboard(GrRenderTargetContext*, bool flip);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700156};
157
158/**
159 * Base class for GrClips that visualize a clip mask.
160 */
161class MaskOnlyClipBase : public GrClip {
162private:
163 bool quickContains(const SkRect&) const final { return false; }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500164 bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700165};
166
167/**
168 * This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask.
169 */
170class AlphaOnlyClip final : public MaskOnlyClipBase {
171public:
Greg Daniele32506b2020-02-10 16:00:54 -0500172 AlphaOnlyClip(GrSurfaceProxyView mask, int x, int y) : fMask(std::move(mask)), fX(x), fY(y) {}
Brian Salomonaff329b2017-08-11 09:40:37 -0400173
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700174private:
Brian Salomon64227222020-02-26 13:28:42 -0500175 bool apply(GrRecordingContext* ctx, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
Brian Salomon97180af2017-03-14 13:42:58 -0400176 SkRect* bounds) const override {
Brian Salomon64227222020-02-26 13:28:42 -0500177 GrSamplerState samplerState(GrSamplerState::WrapMode::kClampToBorder,
178 GrSamplerState::Filter::kNearest);
179 auto m = SkMatrix::MakeTrans(-fX, -fY);
180 auto subset = SkRect::Make(fMask.dimensions());
181 auto domain = bounds->makeOffset(-fX, -fY).makeInset(0.5, 0.5);
182 auto fp = GrTextureEffect::MakeSubset(fMask, kPremul_SkAlphaType, m, samplerState, subset,
183 domain, *ctx->priv().caps());
184 fp = GrDeviceSpaceEffect::Make(std::move(fp));
185 out->addCoverageFP(std::move(fp));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700186 return true;
187 }
Greg Daniele32506b2020-02-10 16:00:54 -0500188 GrSurfaceProxyView fMask;
Brian Salomonaff329b2017-08-11 09:40:37 -0400189 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;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600217 if (rtc->numSamples() <= 1) {
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 Daniele20fcad2020-01-08 11:52:34 -0500231 auto maskRTC = GrRenderTargetContext::MakeWithFallback(
232 ctx, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact,
233 {kCoverRect.width() + padRight, kCoverRect.height() + padBottom});
Chris Daltonc5348082018-03-30 15:59:38 +0000234 if (!maskRTC) {
235 return;
236 }
237
238 // Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
239 // the clip mask generation.
240 this->stencilCheckerboard(maskRTC.get(), true);
Brian Osman9a9baae2018-11-05 15:06:26 -0500241 maskRTC->clear(nullptr, SK_PMColor4fWHITE, GrRenderTargetContext::CanClearFullscreen::kYes);
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400242 GrPaint stencilPaint;
243 stencilPaint.setCoverageSetOpXPFactory(SkRegion::kDifference_Op, false);
244 maskRTC->priv().stencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
245 std::move(stencilPaint), GrAA::kNo, SkMatrix::I(),
246 SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
Chris Daltonc5348082018-03-30 15:59:38 +0000247 reducedClip.drawAlphaClipMask(maskRTC.get());
248
249 int x = kCoverRect.x() - kDeviceRect.x(),
250 y = kCoverRect.y() - kDeviceRect.y();
251
252 // Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
253 // inside window rectangles or outside the scissor should still have the initial checkerboard
254 // intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
Greg Daniele32506b2020-02-10 16:00:54 -0500255 AlphaOnlyClip clip(maskRTC->readSurfaceView(), x, y);
Chris Daltonc5348082018-03-30 15:59:38 +0000256 rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
257 SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700258}
259
Brian Osman11052242016-10-27 14:47:55 -0400260void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700261 const GrReducedClip& reducedClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500262 GrPaint&& paint) {
Michael Ludwig828d3412020-05-12 13:15:35 -0400263 if (ctx->abandoned()) {
264 // GrReducedClip assumes the context hasn't been abandoned, which is reasonable since it is
265 // only ever used if a draw op is made. Since this GM calls it directly, it has to be taken
266 // into account.
267 return;
268 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700269 // Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
270 // by the clip mask generation.
Brian Osman11052242016-10-27 14:47:55 -0400271 this->stencilCheckerboard(rtc, false);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000272 reducedClip.drawStencilClipMask(ctx, rtc);
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700273
274 // Now visualize the stencil mask by covering the entire render target. The regions inside
Robert Phillips806be2d2017-06-28 15:23:59 -0400275 // window rectangles or outside the scissor should still have the initial checkerboard intact.
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700276 // (This verifies we didn't spend any time modifying those pixels in the mask.)
Chris Daltonbbfd5162017-11-07 13:35:22 -0700277 rtc->drawPaint(make_stencil_only_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
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000291 rtc->priv().clearStencilClip(GrFixedClip::Disabled(), 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());
299 rtc->priv().stencilRect(GrNoClip(), &kSetClip, std::move(paint), GrAA::kNo,
300 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}