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"
|
2018-10-25 16:36:06 +00:00
|
|
|
#include "SkTextUtils.h"
|
2016-09-06 17:01:06 +00:00
|
|
|
|
2018-05-11 14:14:21 +00:00
|
|
|
#include "GrAppliedClip.h"
|
|
|
|
#include "GrCaps.h"
|
|
|
|
#include "GrContextPriv.h"
|
|
|
|
#include "GrReducedClip.h"
|
|
|
|
#include "GrRenderTargetContext.h"
|
|
|
|
#include "GrRenderTargetContextPriv.h"
|
|
|
|
#include "GrResourceProvider.h"
|
|
|
|
#include "GrStencilClip.h"
|
|
|
|
#include "effects/GrTextureDomain.h"
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
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(); )
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-06 18:45:31 +00:00
|
|
|
constexpr static int kNumWindows = 8;
|
|
|
|
|
2016-09-06 17:01:06 +00:00
|
|
|
/**
|
2018-03-30 15:59:38 +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
|
2016-09-06 17:01:06 +00:00
|
|
|
* 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;
|
2018-03-30 15:59:38 +00:00
|
|
|
void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&, GrPaint&&);
|
2017-01-11 18:42:54 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-11-07 20:35:22 +00:00
|
|
|
* Makes a clip object that enforces the stencil clip bit. Used to visualize the stencil mask.
|
2016-09-06 17:01:06 +00:00
|
|
|
*/
|
2017-11-07 20:35:22 +00:00
|
|
|
static GrStencilClip make_stencil_only_clip() {
|
|
|
|
return GrStencilClip(SkClipStack::kEmptyGenID);
|
2016-09-06 17:01:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:09:54 +00:00
|
|
|
const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), rtc->caps(), kNumWindows);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
2018-03-30 15:59:38 +00:00
|
|
|
GrPaint paint;
|
2017-05-12 15:36:10 +00:00
|
|
|
if (GrFSAAType::kNone == rtc->fsaaType()) {
|
2018-10-16 19:19:28 +00:00
|
|
|
paint.setColor4f({ 0, 0.25f, 1, 1 });
|
2018-03-30 15:59:38 +00:00
|
|
|
this->visualizeAlphaMask(ctx, rtc, reducedClip, std::move(paint));
|
2016-09-06 17:01:06 +00:00
|
|
|
} else {
|
2018-10-16 19:19:28 +00:00
|
|
|
paint.setColor4f({ 1, 0.25f, 0.25f, 1 });
|
2018-03-30 15:59:38 +00:00
|
|
|
this->visualizeStencilMask(ctx, rtc, reducedClip, std::move(paint));
|
2016-09-06 17:01:06 +00:00
|
|
|
}
|
2018-03-30 15:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
|
|
|
|
const GrReducedClip& reducedClip, GrPaint&& paint) {
|
|
|
|
const int padRight = (kDeviceRect.right() - kCoverRect.right()) / 2;
|
|
|
|
const int padBottom = (kDeviceRect.bottom() - kCoverRect.bottom()) / 2;
|
|
|
|
sk_sp<GrRenderTargetContext> maskRTC(
|
|
|
|
ctx->contextPriv().makeDeferredRenderTargetContextWithFallback(
|
2018-11-14 15:28:10 +00:00
|
|
|
SkBackingFit::kExact,
|
2018-03-30 15:59:38 +00:00
|
|
|
kCoverRect.width() + padRight,
|
|
|
|
kCoverRect.height() + padBottom,
|
|
|
|
kAlpha_8_GrPixelConfig, nullptr));
|
|
|
|
if (!maskRTC) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw a checker pattern into the alpha mask so we can visualize the regions left untouched by
|
|
|
|
// the clip mask generation.
|
|
|
|
this->stencilCheckerboard(maskRTC.get(), true);
|
2018-11-05 20:06:26 +00:00
|
|
|
maskRTC->clear(nullptr, SK_PMColor4fWHITE, GrRenderTargetContext::CanClearFullscreen::kYes);
|
2018-03-30 15:59:38 +00:00
|
|
|
maskRTC->priv().drawAndStencilRect(make_stencil_only_clip(), &GrUserStencilSettings::kUnused,
|
|
|
|
SkRegion::kDifference_Op, false, GrAA::kNo, SkMatrix::I(),
|
|
|
|
SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
|
|
|
|
reducedClip.drawAlphaClipMask(maskRTC.get());
|
|
|
|
|
|
|
|
int x = kCoverRect.x() - kDeviceRect.x(),
|
|
|
|
y = kCoverRect.y() - kDeviceRect.y();
|
|
|
|
|
|
|
|
// 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.)
|
|
|
|
AlphaOnlyClip clip(maskRTC->asTextureProxyRef(), x, y);
|
|
|
|
rtc->drawRect(clip, std::move(paint), GrAA::kYes, SkMatrix::I(),
|
|
|
|
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);
|
2018-10-15 20:09:54 +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-11-07 20:35:22 +00:00
|
|
|
rtc->drawPaint(make_stencil_only_clip(), 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>()
|
|
|
|
);
|
|
|
|
|
2017-11-02 16:56:09 +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.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);
|
2018-04-11 18:30:17 +00:00
|
|
|
|
2018-10-25 16:36:06 +00:00
|
|
|
SkTextUtils::DrawString(canvas, errorMsg, SkIntToScalar((kCoverRect.left() + kCoverRect.right())/2),
|
|
|
|
SkIntToScalar((kCoverRect.top() + kCoverRect.bottom())/2 - 10), paint,
|
2018-10-30 16:53:21 +00:00
|
|
|
SkTextUtils::kCenter_Align);
|
2016-09-06 17:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GM( return new WindowRectanglesMaskGM(); )
|
|
|
|
|
|
|
|
}
|