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"
|
|
|
|
|
#include "SkClipStack.h"
|
|
|
|
|
#include "SkRRect.h"
|
|
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
# include "GrAppliedClip.h"
|
2016-10-27 18:47:55 +00:00
|
|
|
|
# include "GrRenderTargetContext.h"
|
|
|
|
|
# include "GrRenderTargetContextPriv.h"
|
2016-09-06 17:01:06 +00:00
|
|
|
|
# include "GrFixedClip.h"
|
|
|
|
|
# include "GrReducedClip.h"
|
|
|
|
|
# include "GrRenderTargetPriv.h"
|
|
|
|
|
# include "GrResourceProvider.h"
|
|
|
|
|
# include "effects/GrTextureDomain.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
constexpr static SkIRect kDeviceRect = {0, 0, 600, 600};
|
|
|
|
|
constexpr static SkIRect kLayerRect = {25, 25, 575, 575};
|
|
|
|
|
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);
|
|
|
|
|
canvas->saveLayer(SkRect::Make(kLayerRect), nullptr);
|
|
|
|
|
|
|
|
|
|
SkClipStack stack;
|
2016-10-03 15:36:16 +00:00
|
|
|
|
stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(),
|
|
|
|
|
SkCanvas::kDifference_Op, false);
|
|
|
|
|
stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(),
|
|
|
|
|
SkCanvas::kDifference_Op, true);
|
|
|
|
|
stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45),
|
|
|
|
|
SkMatrix::I(), SkCanvas::kDifference_Op, 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-10-03 15:36:16 +00:00
|
|
|
|
stack.clipRRect(nine, SkMatrix::I(), SkCanvas::kDifference_Op, 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-10-03 15:36:16 +00:00
|
|
|
|
stack.clipRRect(complx, SkMatrix::I(), SkCanvas::kDifference_Op, false);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
|
|
this->onCoverClipStack(stack, canvas);
|
|
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is a simple helper class for resetting a canvas's clip to our test’s SkClipStack.
|
|
|
|
|
*/
|
|
|
|
|
class ReplayClipStackVisitor final : public SkCanvasClipVisitor {
|
|
|
|
|
public:
|
2016-09-20 15:42:38 +00:00
|
|
|
|
typedef SkCanvas::ClipOp Op;
|
2016-09-06 17:01:06 +00:00
|
|
|
|
ReplayClipStackVisitor(SkCanvas* canvas) : fCanvas(canvas) {}
|
|
|
|
|
void clipRect(const SkRect& r, Op op, bool aa) override { fCanvas->clipRect(r, op, aa); }
|
|
|
|
|
void clipRRect(const SkRRect& rr, Op op, bool aa) override { fCanvas->clipRRect(rr, op, aa); }
|
|
|
|
|
void clipPath(const SkPath&, Op, bool) override { SkFAIL("Not implemented"); }
|
|
|
|
|
private:
|
|
|
|
|
SkCanvas* const fCanvas;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void WindowRectanglesGM::onCoverClipStack(const SkClipStack& stack, SkCanvas* canvas) {
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
paint.setColor(0xff00aa80);
|
|
|
|
|
|
|
|
|
|
// Set up the canvas's clip to match our SkClipStack.
|
|
|
|
|
ReplayClipStackVisitor visitor(canvas);
|
|
|
|
|
SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
|
|
|
|
|
for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) {
|
|
|
|
|
element->replay(&visitor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2016-10-27 18:47:55 +00:00
|
|
|
|
void visualizeAlphaMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&,
|
|
|
|
|
const GrPaint&);
|
|
|
|
|
void visualizeStencilMask(GrContext*, GrRenderTargetContext*, const GrReducedClip&,
|
|
|
|
|
const GrPaint&);
|
|
|
|
|
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; }
|
|
|
|
|
bool isRRect(const SkRect& rtBounds, SkRRect* rr, bool* aa) const final { return false; }
|
|
|
|
|
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:
|
|
|
|
|
AlphaOnlyClip(GrTexture* mask, int x, int y) {
|
|
|
|
|
int w = mask->width(), h = mask->height();
|
2016-10-03 21:15:28 +00:00
|
|
|
|
fFP = GrDeviceSpaceTextureDecalFragmentProcessor::Make(mask, SkIRect::MakeWH(w, h), {x, y});
|
2016-09-06 17:01:06 +00:00
|
|
|
|
}
|
|
|
|
|
private:
|
2016-10-27 18:47:55 +00:00
|
|
|
|
bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out) const override {
|
2016-09-06 17:01:06 +00:00
|
|
|
|
out->addCoverageFP(fFP);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
sk_sp<GrFragmentProcessor> fFP;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class clips a cover by the stencil clip bit. We use it to visualize the stencil mask.
|
|
|
|
|
*/
|
|
|
|
|
class StencilOnlyClip final : public MaskOnlyClipBase {
|
|
|
|
|
private:
|
2016-10-27 18:47:55 +00:00
|
|
|
|
bool apply(GrContext*, GrRenderTargetContext*, bool, bool, GrAppliedClip* out) const override {
|
2016-09-06 17:01:06 +00:00
|
|
|
|
out->addStencilClip();
|
|
|
|
|
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-10-27 18:47:55 +00:00
|
|
|
|
if (!ctx || !rtc ||
|
|
|
|
|
rtc->accessRenderTarget()->renderTargetPriv().maxWindowRectangles() < kNumWindows) {
|
2016-09-06 17:01:06 +00:00
|
|
|
|
this->fail(canvas);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const GrReducedClip reducedClip(stack, SkRect::Make(kCoverRect), kNumWindows);
|
|
|
|
|
|
|
|
|
|
GrPaint paint;
|
|
|
|
|
paint.setAntiAlias(true);
|
2016-10-27 18:47:55 +00:00
|
|
|
|
if (!rtc->isStencilBufferMultisampled()) {
|
2016-09-06 17:01:06 +00:00
|
|
|
|
paint.setColor4f(GrColor4f(0, 0.25f, 1, 1));
|
2016-10-27 18:47:55 +00:00
|
|
|
|
this->visualizeAlphaMask(ctx, rtc, reducedClip, paint);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
} else {
|
|
|
|
|
paint.setColor4f(GrColor4f(1, 0.25f, 0.25f, 1));
|
2016-10-27 18:47:55 +00:00
|
|
|
|
this->visualizeStencilMask(ctx, rtc, reducedClip, paint);
|
2016-09-06 17:01:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
|
void WindowRectanglesMaskGM::visualizeAlphaMask(GrContext* ctx, GrRenderTargetContext* rtc,
|
2016-09-06 17:01:06 +00:00
|
|
|
|
const GrReducedClip& reducedClip,
|
|
|
|
|
const GrPaint& paint) {
|
2016-10-27 19:13:22 +00:00
|
|
|
|
sk_sp<GrRenderTargetContext> maskRTC(
|
2016-10-27 18:47:55 +00:00
|
|
|
|
ctx->makeRenderTargetContextWithFallback(SkBackingFit::kExact, kLayerRect.width(),
|
|
|
|
|
kLayerRect.height(), kAlpha_8_GrPixelConfig,
|
|
|
|
|
nullptr));
|
2016-10-27 19:13:22 +00:00
|
|
|
|
if (!maskRTC ||
|
|
|
|
|
!ctx->resourceProvider()->attachStencilAttachment(maskRTC->accessRenderTarget())) {
|
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,
|
|
|
|
|
SkRegion::kDifference_Op, false, false, SkMatrix::I(),
|
|
|
|
|
SkRect::MakeIWH(maskRTC->width(), maskRTC->height()));
|
|
|
|
|
reducedClip.drawAlphaClipMask(maskRTC.get());
|
|
|
|
|
sk_sp<GrTexture> mask(maskRTC->asTexture());
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
|
|
int x = kCoverRect.x() - kLayerRect.x(),
|
|
|
|
|
y = kCoverRect.y() - kLayerRect.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(mask.get(), x, y);
|
2016-10-27 18:47:55 +00:00
|
|
|
|
rtc->drawRect(clip, paint, SkMatrix::I(),
|
2016-09-06 17:01:06 +00:00
|
|
|
|
SkRect::Make(SkIRect::MakeXYWH(x, y, mask->width(), mask->height())));
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
const GrPaint& paint) {
|
2016-10-27 18:47:55 +00:00
|
|
|
|
if (!ctx->resourceProvider()->attachStencilAttachment(rtc->accessRenderTarget())) {
|
2016-09-06 17:01:06 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
reducedClip.drawStencilClipMask(ctx, rtc, {kLayerRect.x(), kLayerRect.y()});
|
2016-09-06 17:01:06 +00:00
|
|
|
|
|
|
|
|
|
// Now visualize the stencil mask by covering the entire render target. The regions inside
|
|
|
|
|
// window rectangless 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.)
|
2016-10-27 18:47:55 +00:00
|
|
|
|
rtc->drawPaint(StencilOnlyClip(), 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
|
|
|
|
|
|
|
|
|
for (int y = 0; y < kLayerRect.height(); y += kMaskCheckerSize) {
|
|
|
|
|
for (int x = (y & 1) == flip ? 0 : kMaskCheckerSize;
|
|
|
|
|
x < kLayerRect.width(); x += 2 * kMaskCheckerSize) {
|
|
|
|
|
SkIRect checker = SkIRect::MakeXYWH(x, y, kMaskCheckerSize, kMaskCheckerSize);
|
2016-10-27 19:13:22 +00:00
|
|
|
|
rtc->priv().stencilRect(GrNoClip(), &kSetClip, false, SkMatrix::I(),
|
|
|
|
|
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);
|
|
|
|
|
canvas->drawText(errorMsg.c_str(), errorMsg.size(), SkIntToScalar(kCoverRect.centerX()),
|
|
|
|
|
SkIntToScalar(kCoverRect.centerY() - 10), paint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEF_GM( return new WindowRectanglesMaskGM(); )
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
}
|