2016-09-06 17:01:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2016-09-06 17:01:06 +00:00
|
|
|
#include "SkClipStack.h"
|
|
|
|
#include "SkRRect.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
# include "GrAppliedClip.h"
|
|
|
|
# include "GrFixedClip.h"
|
|
|
|
# include "GrReducedClip.h"
|
2017-06-15 16:07:18 +00:00
|
|
|
# include "GrRenderTargetContext.h"
|
|
|
|
# include "GrRenderTargetContextPriv.h"
|
2016-09-06 17:01:06 +00:00
|
|
|
# include "GrResourceProvider.h"
|
|
|
|
# include "effects/GrTextureDomain.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
|
|
|
|
constexpr static SkIRect kCoverRect = {50, 50, 550, 550};
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class WindowRectanglesBaseGM : public GM {
|
|
|
|
protected:
|
|
|
|
virtual void onCoverClipStack(const SkClipStack&, SkCanvas*) = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkISize onISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); }
|
|
|
|
void onDraw(SkCanvas*) final;
|
|
|
|
};
|
|
|
|
|
|
|
|
void WindowRectanglesBaseGM::onDraw(SkCanvas* canvas) {
|
|
|
|
sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25);
|
|
|
|
|
|
|
|
SkClipStack stack;
|
2016-10-03 15:36:16 +00:00
|
|
|
stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
|
2016-12-09 14:00:50 +00:00
|
|
|
kDifference_SkClipOp, false);
|
2016-10-03 15:36:16 +00:00
|
|
|
stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
|
2016-12-09 14:00:50 +00:00
|
|
|
kDifference_SkClipOp, true);
|
2016-10-03 15:36:16 +00:00
|
|
|
stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
|
2016-12-09 14:00:50 +00:00
|
|
|
SkMatrix::I(), kDifference_SkClipOp, true);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
SkRRect nine;
|
|
|
|
nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20);
|
2016-12-09 14:00:50 +00:00
|
|
|
stack.clipRRect(nine, SkMatrix::I(), kDifference_SkClipOp, true);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
SkRRect complx;
|
|
|
|
SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}};
|
|
|
|
complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii);
|
2016-12-09 14:00:50 +00:00
|
|
|
stack.clipRRect(complx, SkMatrix::I(), kDifference_SkClipOp, false);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
this->onCoverClipStack(stack, canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a clip that will exercise window rectangles if they are supported.
|
|
|
|
*/
|
|
|
|
class WindowRectanglesGM : public WindowRectanglesBaseGM {
|
|
|
|
private:
|
|
|
|
SkString onShortName() final { return SkString("windowrectangles"); }
|
|
|
|
void onCoverClipStack(const SkClipStack&, SkCanvas*) final;
|
|
|
|
};
|
|
|
|
|
|
|
|
void WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xff00aa80);
|
|
|
|
|
|
|
|
// Set up the canvas's clip to match our SkClipStack.
|
|
|
|
SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
|
|
|
|
for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
|
2017-03-08 18:13:44 +00:00
|
|
|
SkClipOp op = element->getOp();
|
|
|
|
bool isAA = element->isAA();
|
2017-08-30 15:37:57 +00:00
|
|
|
switch (element->getDeviceSpaceType()) {
|
|
|
|
case SkClipStack::Element::DeviceSpaceType::kPath:
|
|
|
|
canvas->clipPath(element->getDeviceSpacePath(), op, isAA);
|
2017-03-08 18:13:44 +00:00
|
|
|
break;
|
2017-08-30 15:37:57 +00:00
|
|
|
case SkClipStack::Element::DeviceSpaceType::kRRect:
|
|
|
|
canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA);
|
2017-03-08 18:13:44 +00:00
|
|
|
break;
|
2017-08-30 15:37:57 +00:00
|
|
|
case SkClipStack::Element::DeviceSpaceType::kRect:
|
|
|
|
canvas->clipRect(element->getDeviceSpaceRect(), op, isAA);
|
2017-03-08 18:13:44 +00:00
|
|
|
break;
|
2017-08-30 15:37:57 +00:00
|
|
|
case SkClipStack::Element::DeviceSpaceType::kEmpty:
|
2017-03-08 18:13:44 +00:00
|
|
|
canvas->clipRect({ 0, 0, 0, 0 }, kIntersect_SkClipOp, false);
|
|
|
|
break;
|
|
|
|
}
|
2016-09-06 17:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas->drawRect(SkRect::Make(kCoverRect), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GM( return new WindowRectanglesGM(); )
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2016-09-06 18:45:31 +00:00
|
|
|
constexpr static int kNumWindows = 8;
|
|
|
|
|
2016-09-06 17:01:06 +00:00
|
|
|
/**
|
|
|
|
* Visualizes the mask (alpha or stencil) for a clip with several window rectangles. The purpose of
|
|
|
|
* this test is to verify that window rectangles are being used during clip mask generation, and to
|
|
|
|
* visualize where the window rectangles are placed.
|
|
|
|
*
|
|
|
|
* We use window rectangles when generating the clip mask because there is no need to invest time
|
|
|
|
* defining those regions where window rectangles will be in effect during the actual draw anyway.
|
|
|
|
*
|
|
|
|
* This test works by filling the entire clip mask with a small checkerboard pattern before drawing
|
|
|
|
* it, and then covering the mask with a solid color once it has been generated. The regions inside
|
|
|
|
* window rectangles or outside the scissor should still have the initial checkerboard intact.
|
|
|
|
*/
|
|
|
|
class WindowRectanglesMaskGM : public WindowRectanglesBaseGM {
|
|
|
|
private:
|
|
|
|
constexpr static int kMaskCheckerSize = 5;
|
|
|
|
SkString onShortName() final { return SkString("windowrectangles_mask"); }
|
|
|
|
void onCoverClipStack(const SkClipStack&, SkCanvas*) final;
|
2017-01-11 18:42:54 +00:00
|
|
|
void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
|
|
|
|
void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
|
2016-10-27 18:47:55 +00:00
|
|
|
void stencilCheckerboard(GrRenderTargetContext*, bool flip);
|
2016-09-06 17:01:06 +00:00
|
|
|
void fail(SkCanvas*);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for GrClips that visualize a clip mask.
|
|
|
|
*/
|
|
|
|
class MaskOnlyClipBase : public GrClip {
|
|
|
|
private:
|
|
|
|
bool quickContains(const SkRect&) const final { return false; }
|
2016-12-09 20:10:07 +00:00
|
|
|
bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
|
2016-09-06 17:01:06 +00:00
|
|
|
void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
|
|
|
|
rect->set(0, 0, width, height);
|
|
|
|
if (iior) {
|
|
|
|
*iior = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class clips a cover by an alpha mask. We use it to visualize the alpha clip mask.
|
|
|
|
*/
|
|
|
|
class AlphaOnlyClip final : public MaskOnlyClipBase {
|
|
|
|
public:
|
2017-08-11 13:40:37 +00:00
|
|
|
AlphaOnlyClip(sk_sp<GrTextureProxy> mask, int x, int y) : fMask(mask), fX(x), fY(y) {}
|
|
|
|
|
2016-09-06 17:01:06 +00:00
|
|
|
private:
|
2017-03-14 17:42:58 +00:00
|
|
|
bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
|
|
|
|
SkRect* bounds) const override {
|
2017-08-11 13:40:37 +00:00
|
|
|
int w = fMask->width();
|
|
|
|
int h = fMask->height();
|
|
|
|
out->addCoverageFP(GrDeviceSpaceTextureDecalFragmentProcessor::Make(
|
|
|
|
fMask, SkIRect::MakeWH(w, h), {fX, fY}));
|
2016-09-06 17:01:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-11 13:40:37 +00:00
|
|
|
sk_sp<GrTextureProxy> fMask;
|
|
|
|
int fX;
|
|
|
|
int fY;
|
2016-09-06 17:01:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class clips a cover by the stencil clip bit. We use it to visualize the stencil mask.
|
|
|
|
*/
|
|
|
|
class StencilOnlyClip final : public MaskOnlyClipBase {
|
|
|
|
private:
|
2017-03-14 17:42:58 +00:00
|
|
|
bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out,
|
|
|
|
SkRect* bounds) const override {
|
2017-06-28 12:40:11 +00:00
|
|
|
out->addStencilClip(SkClipStack::kEmptyGenID);
|
2016-09-06 17:01:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void WindowRectanglesMaskGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) {
|
|
|
|
GrContext* ctx = canvas->getGrContext();
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
|
2016-09-06 17:01:06 +00:00
|
|
|
|
2016-11-09 13:54:35 +00:00
|
|
|
if (!ctx || !rtc || rtc->priv().maxWindowRectangles() < kNumWindows) {
|
2016-09-06 17:01:06 +00:00
|
|
|
this->fail(canvas);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), kNumWindows);
|
|
|
|
|
|
|
|
GrPaint paint;
|
2017-05-12 15:36:10 +00:00
|
|
|
if (GrFSAAType::kNone == rtc->fsaaType()) {
|
2016-09-06 17:01:06 +00:00
|
|
|
paint.setColor4f(GrColor4f(0, 0.25f, 1, 1));
|
2017-01-11 18:42:54 +00:00
|
|
|
this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
|
2016-09-06 17:01:06 +00:00
|
|
|
} else {
|
|
|
|
paint.setColor4f(GrColor4f(1, 0.25f, 0.25f, 1));
|
2017-01-11 18:42:54 +00:00
|
|
|
this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
|
2016-09-06 17:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
|
2017-01-11 18:42:54 +00:00
|
|
|
const GrReducedClip& reducedClip, GrPaint&& paint) {
|
2017-03-13 21:57:28 +00:00
|
|
|
const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
|
|
|
|
const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
|
2016-10-27 19:13:22 +00:00
|
|
|
sk_sp<GrRenderTargetContext> maskRTC(
|
2017-04-24 14:57:28 +00:00
|
|
|
ctx->makeDeferredRenderTargetContextWithFallback(SkBackingFit::kExact,
|
|
|
|
kCoverRect.width() + padRight,
|
|
|
|
kCoverRect.height() + padBottom,
|
|
|
|
kAlpha_8_GrPixelConfig, nullptr));
|
2017-09-21 16:00:26 +00:00
|
|
|
if (!maskRTC) {
|
2016-09-06 17:01:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
|
|
|
|
// the clip mask generation.
|
2016-10-27 19:13:22 +00:00
|
|
|
this->stencilCheckerboard(maskRTC.get(), true);
|
|
|
|
maskRTC->clear(nullptr, GrColorPackA4(0xff), true);
|
|
|
|
maskRTC->priv().drawAndStencilRect(StencilOnlyClip(), &GrUserStencilSettings::kUnused,
|
2016-12-09 20:10:07 +00:00
|
|
|
SkRegion::kDifference_Op, false, GrAA::kNo, SkMatrix::I(),
|
2016-10-27 19:13:22 +00:00
|
|
|
SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
|
|
|
|
reducedClip.drawAlphaClipMask(maskRTC.get());
|
2016-09-06 17:01:06 +00:00
|
|
|
|
2017-03-13 21:57:28 +00:00
|
|
|
int x = kCoverRect.x() - kDeviceRect.x(),
|
|
|
|
y = kCoverRect.y() - kDeviceRect.y();
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
// Now visualize the alpha mask by drawing a rect over the area where it is defined. The regions
|
|
|
|
// inside window rectangles or outside the scissor should still have the initial checkerboard
|
|
|
|
// intact. (This verifies we didn't spend any time modifying those pixels in the mask.)
|
2017-06-15 16:07:18 +00:00
|
|
|
AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
|
2017-01-11 18:42:54 +00:00
|
|
|
rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
|
2017-01-30 13:06:27 +00:00
|
|
|
SkRect::Make(SkIRect::MakeXYWH(x, y, maskRTC->width(), maskRTC->height())));
|
2016-09-06 17:01:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
void WindowRectanglesMaskGM::visualizeStencilMask(GrContext* ctx, GrRenderTargetContext* rtc,
|
2016-09-06 17:01:06 +00:00
|
|
|
const GrReducedClip& reducedClip,
|
2017-01-11 18:42:54 +00:00
|
|
|
GrPaint&& paint) {
|
2016-09-06 17:01:06 +00:00
|
|
|
// Draw a checker pattern into the stencil buffer so we can visualize the regions left untouched
|
|
|
|
// by the clip mask generation.
|
2016-10-27 18:47:55 +00:00
|
|
|
this->stencilCheckerboard(rtc, false);
|
2017-03-13 21:57:28 +00:00
|
|
|
reducedClip.drawStencilClipMask(ctx, rtc);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
// Now visualize the stencil mask by covering the entire render target. The regions inside
|
2017-06-28 19:23:59 +00:00
|
|
|
// window rectangles or outside the scissor should still have the initial checkerboard intact.
|
2016-09-06 17:01:06 +00:00
|
|
|
// (This verifies we didn't spend any time modifying those pixels in the mask.)
|
2017-01-11 18:42:54 +00:00
|
|
|
rtc->drawPaint(StencilOnlyClip(), std::move(paint), SkMatrix::I());
|
2016-09-06 17:01:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
void WindowRectanglesMaskGM::stencilCheckerboard(GrRenderTargetContext* rtc, bool flip) {
|
2016-09-06 17:01:06 +00:00
|
|
|
constexpr static GrUserStencilSettings kSetClip(
|
|
|
|
GrUserStencilSettings::StaticInit<
|
|
|
|
0,
|
|
|
|
GrUserStencilTest::kAlways,
|
|
|
|
0,
|
|
|
|
GrUserStencilOp::kSetClipBit,
|
|
|
|
GrUserStencilOp::kKeep,
|
|
|
|
0>()
|
|
|
|
);
|
|
|
|
|
2016-10-27 19:13:22 +00:00
|
|
|
rtc->priv().clearStencilClip(GrFixedClip::Disabled(), false);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
2017-03-13 21:57:28 +00:00
|
|
|
for (int y = 0; y < kDeviceRect.height(); y += kMaskCheckerSize) {
|
2016-09-06 17:01:06 +00:00
|
|
|
for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
|
2017-03-13 21:57:28 +00:00
|
|
|
x < kDeviceRect.width(); x += 2 * kMaskCheckerSize) {
|
2016-09-06 17:01:06 +00:00
|
|
|
SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
|
2016-12-09 20:10:07 +00:00
|
|
|
rtc->priv().stencilRect(GrNoClip(), &kSetClip, GrAAType::kNone, SkMatrix::I(),
|
2016-10-27 19:13:22 +00:00
|
|
|
SkRect::Make(checker));
|
2016-09-06 17:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowRectanglesMaskGM::fail(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
paint.setTextSize(20);
|
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
|
|
|
|
|
|
|
SkString errorMsg;
|
|
|
|
errorMsg.printf("Requires GPU with %i window rectangles", kNumWindows);
|
|
|
|
|
|
|
|
canvas->clipRect(SkRect::Make(kCoverRect));
|
|
|
|
canvas->clear(SK_ColorWHITE);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(errorMsg, SkIntToScalar(kCoverRect.centerX()),
|
2016-09-06 17:01:06 +00:00
|
|
|
SkIntToScalar(kCoverRect.centerY() - 10), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GM( return new WindowRectanglesMaskGM(); )
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|