2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-12-12 15:02:12 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkClipOp.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2020-10-27 22:07:29 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2020-09-09 00:47:09 +00:00
|
|
|
#include "include/core/SkPathBuilder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2020-06-02 21:27:07 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/core/SkClipOpPriv.h"
|
2020-03-10 18:02:56 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2011-03-10 19:20:15 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-03-10 19:20:15 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor gPathColor = SK_ColorBLACK;
|
|
|
|
constexpr SkColor gClipAColor = SK_ColorBLUE;
|
|
|
|
constexpr SkColor gClipBColor = SK_ColorRED;
|
2011-12-08 16:18:29 +00:00
|
|
|
|
2011-03-10 19:20:15 +00:00
|
|
|
class ComplexClipGM : public GM {
|
|
|
|
public:
|
2014-12-17 22:38:49 +00:00
|
|
|
ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw)
|
2012-07-12 13:48:46 +00:00
|
|
|
: fDoAAClip(aaclip)
|
2014-12-17 22:38:49 +00:00
|
|
|
, fDoSaveLayer(saveLayer)
|
|
|
|
, fInvertDraw(invertDraw) {
|
2015-06-12 17:00:11 +00:00
|
|
|
this->setBGColor(0xFFDEDFDE);
|
2011-03-10 19:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-17 02:49:55 +00:00
|
|
|
SkString onShortName() override {
|
2011-12-08 16:18:29 +00:00
|
|
|
SkString str;
|
2014-12-17 22:38:49 +00:00
|
|
|
str.printf("complexclip_%s%s%s",
|
2012-07-12 13:48:46 +00:00
|
|
|
fDoAAClip ? "aa" : "bw",
|
2014-12-17 22:38:49 +00:00
|
|
|
fDoSaveLayer ? "_layer" : "",
|
|
|
|
fInvertDraw ? "_invert" : "");
|
2011-12-08 16:18:29 +00:00
|
|
|
return str;
|
2011-03-10 19:20:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 14:56:42 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(388, 780); }
|
2011-03-10 19:20:15 +00:00
|
|
|
|
2018-08-17 02:49:55 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2020-09-09 00:47:09 +00:00
|
|
|
SkPath path = SkPathBuilder()
|
|
|
|
.moveTo(0, 50)
|
|
|
|
.quadTo(0, 0, 50, 0)
|
|
|
|
.lineTo(175, 0)
|
|
|
|
.quadTo(200, 0, 200, 25)
|
|
|
|
.lineTo(200, 150)
|
|
|
|
.quadTo(200, 200, 150, 200)
|
|
|
|
.lineTo(0, 200)
|
|
|
|
.close()
|
|
|
|
.moveTo(50, 50)
|
|
|
|
.lineTo(150, 50)
|
|
|
|
.lineTo(150, 125)
|
|
|
|
.quadTo(150, 150, 125, 150)
|
|
|
|
.lineTo(50, 150)
|
|
|
|
.close()
|
|
|
|
.detach();
|
2014-12-17 22:38:49 +00:00
|
|
|
if (fInvertDraw) {
|
2019-11-26 17:17:17 +00:00
|
|
|
path.setFillType(SkPathFillType::kInverseEvenOdd);
|
2014-12-17 22:38:49 +00:00
|
|
|
} else {
|
2019-11-26 17:17:17 +00:00
|
|
|
path.setFillType(SkPathFillType::kEvenOdd);
|
2014-12-17 22:38:49 +00:00
|
|
|
}
|
2011-03-10 19:20:15 +00:00
|
|
|
SkPaint pathPaint;
|
|
|
|
pathPaint.setAntiAlias(true);
|
2011-12-08 16:18:29 +00:00
|
|
|
pathPaint.setColor(gPathColor);
|
2011-03-10 19:20:15 +00:00
|
|
|
|
2020-09-09 00:47:09 +00:00
|
|
|
SkPath clipA = SkPath::Polygon({{10, 20}, {165, 22}, {70, 105}, {165, 177}, {-5, 180}}, true);
|
2011-03-10 19:20:15 +00:00
|
|
|
|
2020-09-09 00:47:09 +00:00
|
|
|
SkPath clipB = SkPath::Polygon({{40, 10}, {190, 15}, {195, 190}, {40, 185}, {155, 100}}, true);
|
2011-03-10 19:20:15 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 20);
|
2011-03-10 19:20:15 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr struct {
|
2016-12-09 14:00:50 +00:00
|
|
|
SkClipOp fOp;
|
2016-09-20 15:42:38 +00:00
|
|
|
const char* fName;
|
2011-03-10 19:20:15 +00:00
|
|
|
} gOps[] = { //extra spaces in names for measureText
|
2016-12-09 14:00:50 +00:00
|
|
|
{kIntersect_SkClipOp, "Isect "},
|
|
|
|
{kDifference_SkClipOp, "Diff " },
|
2011-03-10 19:20:15 +00:00
|
|
|
};
|
|
|
|
|
2018-08-17 02:49:55 +00:00
|
|
|
canvas->translate(20, 20);
|
2011-03-10 19:20:15 +00:00
|
|
|
canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
|
|
|
|
|
2012-07-13 14:55:25 +00:00
|
|
|
if (fDoSaveLayer) {
|
|
|
|
// We want the layer to appear symmetric relative to actual
|
|
|
|
// device boundaries so we need to "undo" the effect of the
|
|
|
|
// scale and translate
|
|
|
|
SkRect bounds = SkRect::MakeLTRB(
|
2013-11-25 19:44:07 +00:00
|
|
|
4.0f/3.0f * -20,
|
|
|
|
4.0f/3.0f * -20,
|
|
|
|
4.0f/3.0f * (this->getISize().fWidth - 20),
|
|
|
|
4.0f/3.0f * (this->getISize().fHeight - 20));
|
2012-07-13 14:55:25 +00:00
|
|
|
|
2018-08-17 02:49:55 +00:00
|
|
|
bounds.inset(100, 100);
|
2012-07-12 13:48:46 +00:00
|
|
|
SkPaint boundPaint;
|
|
|
|
boundPaint.setColor(SK_ColorRED);
|
|
|
|
boundPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(bounds, boundPaint);
|
2018-05-09 17:23:38 +00:00
|
|
|
canvas->clipRect(bounds);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&bounds, nullptr);
|
2012-07-12 13:48:46 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 16:18:29 +00:00
|
|
|
for (int invBits = 0; invBits < 4; ++invBits) {
|
|
|
|
canvas->save();
|
2011-03-10 19:20:15 +00:00
|
|
|
for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
|
2011-12-08 16:18:29 +00:00
|
|
|
this->drawHairlines(canvas, path, clipA, clipB);
|
|
|
|
|
|
|
|
bool doInvA = SkToBool(invBits & 1);
|
|
|
|
bool doInvB = SkToBool(invBits & 2);
|
2011-03-10 19:20:15 +00:00
|
|
|
canvas->save();
|
|
|
|
// set clip
|
2019-11-26 17:17:17 +00:00
|
|
|
clipA.setFillType(doInvA ? SkPathFillType::kInverseEvenOdd :
|
|
|
|
SkPathFillType::kEvenOdd);
|
|
|
|
clipB.setFillType(doInvB ? SkPathFillType::kInverseEvenOdd :
|
|
|
|
SkPathFillType::kEvenOdd);
|
2016-09-21 18:15:07 +00:00
|
|
|
canvas->clipPath(clipA, fDoAAClip);
|
2011-12-08 16:18:29 +00:00
|
|
|
canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
|
2011-03-10 19:20:15 +00:00
|
|
|
|
2014-12-17 22:38:49 +00:00
|
|
|
// In the inverse case we need to prevent the draw from covering the whole
|
|
|
|
// canvas.
|
|
|
|
if (fInvertDraw) {
|
|
|
|
SkRect rectClip = clipA.getBounds();
|
|
|
|
rectClip.join(path.getBounds());
|
|
|
|
rectClip.join(path.getBounds());
|
|
|
|
rectClip.outset(5, 5);
|
|
|
|
canvas->clipRect(rectClip);
|
|
|
|
}
|
|
|
|
|
2011-03-10 19:20:15 +00:00
|
|
|
// draw path clipped
|
|
|
|
canvas->drawPath(path, pathPaint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
|
2018-12-15 18:45:33 +00:00
|
|
|
SkPaint paint;
|
2018-08-17 02:49:55 +00:00
|
|
|
SkScalar txtX = 45;
|
2011-12-08 16:18:29 +00:00
|
|
|
paint.setColor(gClipAColor);
|
|
|
|
const char* aTxt = doInvA ? "InvA " : "A ";
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
|
|
|
|
txtX += font.measureText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8);
|
2011-03-10 19:20:15 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8, txtX, 220,
|
2018-12-15 18:45:33 +00:00
|
|
|
font, paint);
|
2019-05-07 19:38:46 +00:00
|
|
|
txtX += font.measureText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8);
|
2011-12-08 16:18:29 +00:00
|
|
|
paint.setColor(gClipBColor);
|
|
|
|
const char* bTxt = doInvB ? "InvB " : "B ";
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(bTxt, strlen(bTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
|
2011-03-10 19:20:15 +00:00
|
|
|
|
2018-08-17 02:49:55 +00:00
|
|
|
canvas->translate(250,0);
|
2011-03-10 19:20:15 +00:00
|
|
|
}
|
2011-12-08 16:18:29 +00:00
|
|
|
canvas->restore();
|
2018-08-17 02:49:55 +00:00
|
|
|
canvas->translate(0, 250);
|
2011-03-10 19:20:15 +00:00
|
|
|
}
|
2012-07-12 13:48:46 +00:00
|
|
|
|
2012-07-13 14:55:25 +00:00
|
|
|
if (fDoSaveLayer) {
|
2012-07-12 13:48:46 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
2011-03-10 19:20:15 +00:00
|
|
|
}
|
|
|
|
private:
|
2011-12-08 16:18:29 +00:00
|
|
|
void drawHairlines(SkCanvas* canvas, const SkPath& path,
|
|
|
|
const SkPath& clipA, const SkPath& clipB) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
const SkAlpha fade = 0x33;
|
|
|
|
|
|
|
|
// draw path in hairline
|
|
|
|
paint.setColor(gPathColor); paint.setAlpha(fade);
|
|
|
|
canvas->drawPath(path, paint);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-12-08 16:18:29 +00:00
|
|
|
// draw clips in hair line
|
|
|
|
paint.setColor(gClipAColor); paint.setAlpha(fade);
|
|
|
|
canvas->drawPath(clipA, paint);
|
|
|
|
paint.setColor(gClipBColor); paint.setAlpha(fade);
|
|
|
|
canvas->drawPath(clipB, paint);
|
|
|
|
}
|
|
|
|
|
2014-12-17 22:38:49 +00:00
|
|
|
bool fDoAAClip;
|
|
|
|
bool fDoSaveLayer;
|
|
|
|
bool fInvertDraw;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2011-03-10 19:20:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ComplexClipGM(false, false, false);)
|
|
|
|
DEF_GM(return new ComplexClipGM(false, false, true);)
|
|
|
|
DEF_GM(return new ComplexClipGM(false, true, false);)
|
|
|
|
DEF_GM(return new ComplexClipGM(false, true, true);)
|
|
|
|
DEF_GM(return new ComplexClipGM(true, false, false);)
|
|
|
|
DEF_GM(return new ComplexClipGM(true, false, true);)
|
|
|
|
DEF_GM(return new ComplexClipGM(true, true, false);)
|
|
|
|
DEF_GM(return new ComplexClipGM(true, true, true);)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|
2020-03-10 18:02:56 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(clip_shader, canvas, 840, 650) {
|
|
|
|
auto img = GetResourceAsImage("images/yellow_rose.png");
|
|
|
|
auto sh = img->makeShader();
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeIWH(img->width(), img->height());
|
|
|
|
SkPaint p;
|
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
canvas->drawImage(img, 0, 0, nullptr);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(img->width() + 10, 0);
|
|
|
|
canvas->clipShader(sh, SkClipOp::kIntersect);
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, img->height() + 10);
|
|
|
|
canvas->clipShader(sh, SkClipOp::kDifference);
|
|
|
|
p.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(img->width() + 10, img->height() + 10);
|
|
|
|
canvas->clipShader(sh, SkClipOp::kIntersect);
|
|
|
|
canvas->save();
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix lm = SkMatrix::Scale(1.0f/5, 1.0f/5);
|
2020-03-10 18:02:56 +00:00
|
|
|
canvas->clipShader(img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &lm));
|
|
|
|
canvas->drawImage(img, 0, 0, nullptr);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2020-03-12 17:55:44 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(clip_shader_layer, canvas, 430, 320) {
|
|
|
|
auto img = GetResourceAsImage("images/yellow_rose.png");
|
|
|
|
auto sh = img->makeShader();
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeIWH(img->width(), img->height());
|
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
// now add the cool clip
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->clipShader(sh);
|
|
|
|
// now draw a layer with the same image, and watch it get restored w/ the clip
|
|
|
|
canvas->saveLayer(&r, nullptr);
|
|
|
|
canvas->drawColor(0xFFFF0000);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2020-06-02 21:27:07 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(clip_shader_nested, canvas, 256, 256) {
|
|
|
|
float w = 64.f;
|
|
|
|
float h = 64.f;
|
|
|
|
|
|
|
|
const SkColor gradColors[] = {SK_ColorBLACK, SkColorSetARGB(128, 128, 128, 128)};
|
|
|
|
auto s = SkGradientShader::MakeRadial({0.5f * w, 0.5f * h}, 0.1f * w, gradColors, nullptr,
|
|
|
|
2, SkTileMode::kRepeat, 0, nullptr);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
|
|
|
|
// A large black rect affected by two gradient clips
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipShader(s);
|
|
|
|
canvas->scale(2.f, 2.f);
|
|
|
|
canvas->clipShader(s);
|
|
|
|
canvas->drawRect(SkRect::MakeWH(w, h), p);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(0.f, 2.f * h);
|
|
|
|
|
|
|
|
// A small red rect, with no clipping
|
|
|
|
canvas->save();
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(SkRect::MakeWH(w, h), p);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2020-06-03 14:22:49 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Where is canvas->concat(persp) called relative to the clipShader calls.
|
|
|
|
enum ConcatPerspective {
|
|
|
|
kConcatBeforeClips,
|
|
|
|
kConcatAfterClips,
|
|
|
|
kConcatBetweenClips
|
|
|
|
};
|
|
|
|
// Order in which clipShader(image) and clipShader(gradient) are specified; only meaningful
|
|
|
|
// when CanvasPerspective is kConcatBetweenClips.
|
|
|
|
enum ClipOrder {
|
|
|
|
kClipImageFirst,
|
|
|
|
kClipGradientFirst,
|
|
|
|
|
|
|
|
kDoesntMatter = kClipImageFirst
|
|
|
|
};
|
|
|
|
// Which shaders have perspective applied as a local matrix.
|
|
|
|
enum LocalMatrix {
|
|
|
|
kNoLocalMat,
|
|
|
|
kImageWithLocalMat,
|
|
|
|
kGradientWithLocalMat,
|
|
|
|
kBothWithLocalMat
|
|
|
|
};
|
|
|
|
struct Config {
|
|
|
|
ConcatPerspective fConcat;
|
|
|
|
ClipOrder fOrder;
|
|
|
|
LocalMatrix fLM;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void draw_banner(SkCanvas* canvas, Config config) {
|
|
|
|
SkString banner;
|
|
|
|
banner.append("Persp: ");
|
|
|
|
|
|
|
|
if (config.fConcat == kConcatBeforeClips || config.fLM == kBothWithLocalMat) {
|
|
|
|
banner.append("Both Clips");
|
|
|
|
} else {
|
|
|
|
SkASSERT((config.fConcat == kConcatBetweenClips && config.fLM == kNoLocalMat) ||
|
|
|
|
(config.fConcat == kConcatAfterClips && (config.fLM == kImageWithLocalMat ||
|
|
|
|
config.fLM == kGradientWithLocalMat)));
|
|
|
|
if ((config.fConcat == kConcatBetweenClips && config.fOrder == kClipImageFirst) ||
|
|
|
|
config.fLM == kGradientWithLocalMat) {
|
|
|
|
banner.append("Gradient");
|
|
|
|
} else {
|
|
|
|
SkASSERT(config.fOrder == kClipGradientFirst || config.fLM == kImageWithLocalMat);
|
|
|
|
banner.append("Image");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (config.fLM != kNoLocalMat) {
|
|
|
|
banner.append(" (w/ LM, should equal top row)");
|
|
|
|
}
|
|
|
|
|
|
|
|
static const SkFont kFont(ToolUtils::create_portable_typeface(), 12);
|
|
|
|
canvas->drawString(banner.c_str(), 20.f, -30.f, kFont, SkPaint());
|
|
|
|
};
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
2020-06-03 14:22:49 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(clip_shader_persp, canvas, 1370, 1030) {
|
|
|
|
// Each draw has a clipShader(image-shader), a clipShader(gradient-shader), a concat(persp-mat),
|
|
|
|
// and each shader may or may not be wrapped with a perspective local matrix.
|
|
|
|
|
|
|
|
// Pairs of configs that should match in appearance where first config doesn't use a local
|
|
|
|
// matrix (top row of GM) and the second does (bottom row of GM).
|
|
|
|
Config matches[][2] = {
|
|
|
|
// Everything has perspective
|
|
|
|
{{kConcatBeforeClips, kDoesntMatter, kNoLocalMat},
|
|
|
|
{kConcatAfterClips, kDoesntMatter, kBothWithLocalMat}},
|
|
|
|
// Image shader has perspective
|
|
|
|
{{kConcatBetweenClips, kClipGradientFirst, kNoLocalMat},
|
|
|
|
{kConcatAfterClips, kDoesntMatter, kImageWithLocalMat}},
|
|
|
|
// Gradient shader has perspective
|
|
|
|
{{kConcatBetweenClips, kClipImageFirst, kNoLocalMat},
|
|
|
|
{kConcatAfterClips, kDoesntMatter, kGradientWithLocalMat}}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The image that is drawn
|
|
|
|
auto img = GetResourceAsImage("images/yellow_rose.png");
|
|
|
|
// Scale factor always applied to the image shader so that it tiles
|
|
|
|
SkMatrix scale = SkMatrix::Scale(1.f / 4.f, 1.f / 4.f);
|
|
|
|
// The perspective matrix applied wherever needed
|
|
|
|
SkPoint src[4];
|
|
|
|
SkRect::Make(img->dimensions()).toQuad(src);
|
|
|
|
SkPoint dst[4] = {{0, 80.f},
|
|
|
|
{img->width() + 28.f, -100.f},
|
|
|
|
{img->width() - 28.f, img->height() + 100.f},
|
|
|
|
{0.f, img->height() - 80.f}};
|
|
|
|
SkMatrix persp;
|
|
|
|
SkAssertResult(persp.setPolyToPoly(src, dst, 4));
|
|
|
|
|
|
|
|
SkMatrix perspScale = SkMatrix::Concat(persp, scale);
|
|
|
|
|
|
|
|
auto drawConfig = [&](Config config) {
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
draw_banner(canvas, config);
|
|
|
|
|
|
|
|
// Make clipShaders (possibly with local matrices)
|
|
|
|
bool gradLM = config.fLM == kGradientWithLocalMat || config.fLM == kBothWithLocalMat;
|
|
|
|
const SkColor gradColors[] = {SK_ColorBLACK, SkColorSetARGB(128, 128, 128, 128)};
|
|
|
|
auto gradShader = SkGradientShader::MakeRadial({0.5f * img->width(), 0.5f * img->height()},
|
|
|
|
0.1f * img->width(), gradColors, nullptr, 2,
|
|
|
|
SkTileMode::kRepeat, 0,
|
|
|
|
gradLM ? &persp : nullptr);
|
|
|
|
bool imageLM = config.fLM == kImageWithLocalMat || config.fLM == kBothWithLocalMat;
|
|
|
|
auto imgShader = img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
|
|
|
imageLM ? perspScale : scale);
|
|
|
|
|
|
|
|
// Perspective before any clipShader
|
|
|
|
if (config.fConcat == kConcatBeforeClips) {
|
|
|
|
canvas->concat(persp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// First clipshader
|
|
|
|
canvas->clipShader(config.fOrder == kClipImageFirst ? imgShader : gradShader);
|
|
|
|
|
|
|
|
// Perspective between clipShader
|
|
|
|
if (config.fConcat == kConcatBetweenClips) {
|
|
|
|
canvas->concat(persp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second clipShader
|
|
|
|
canvas->clipShader(config.fOrder == kClipImageFirst ? gradShader : imgShader);
|
|
|
|
|
|
|
|
// Perspective after clipShader
|
|
|
|
if (config.fConcat == kConcatAfterClips) {
|
|
|
|
canvas->concat(persp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual draw and clip boundary are the same for all configs
|
|
|
|
canvas->clipRect(SkRect::MakeIWH(img->width(), img->height()));
|
|
|
|
canvas->clear(SK_ColorBLACK);
|
|
|
|
canvas->drawImage(img, 0, 0);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
};
|
|
|
|
|
|
|
|
SkIRect grid = persp.mapRect(SkRect::Make(img->dimensions())).roundOut();
|
|
|
|
grid.fLeft -= 20; // manual adjust to look nicer
|
|
|
|
|
|
|
|
canvas->translate(10.f, 10.f);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(matches); ++i) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(-grid.fLeft, -grid.fTop);
|
|
|
|
drawConfig(matches[i][0]);
|
|
|
|
canvas->translate(0.f, grid.height());
|
|
|
|
drawConfig(matches[i][1]);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(grid.width(), 0.f);
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 22:07:29 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(clip_shader_difference, canvas, 512, 512) {
|
|
|
|
auto image = GetResourceAsImage("images/yellow_rose.png");
|
|
|
|
canvas->clear(SK_ColorGRAY);
|
|
|
|
|
|
|
|
SkRect rect = SkRect::MakeWH(256, 256);
|
|
|
|
SkMatrix local = SkMatrix::MakeRectToRect(SkRect::MakeWH(image->width(), image->height()),
|
|
|
|
SkRect::MakeWH(64, 64), SkMatrix::kFill_ScaleToFit);
|
|
|
|
auto shader = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &local);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
// TL: A rectangle
|
|
|
|
{
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, 0);
|
|
|
|
canvas->clipShader(shader, SkClipOp::kDifference);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
// TR: A round rectangle
|
|
|
|
{
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(256, 0);
|
|
|
|
canvas->clipShader(shader, SkClipOp::kDifference);
|
|
|
|
canvas->drawRRect(SkRRect::MakeRectXY(rect, 64.f, 64.f), paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
// BL: A path
|
|
|
|
{
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, 256);
|
|
|
|
canvas->clipShader(shader, SkClipOp::kDifference);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(0.f, 128.f);
|
|
|
|
path.lineTo(128.f, 256.f);
|
|
|
|
path.lineTo(256.f, 128.f);
|
|
|
|
path.lineTo(128.f, 0.f);
|
|
|
|
|
|
|
|
SkScalar d = 64.f * SK_ScalarSqrt2;
|
|
|
|
path.moveTo(128.f - d, 128.f - d);
|
|
|
|
path.lineTo(128.f - d, 128.f + d);
|
|
|
|
path.lineTo(128.f + d, 128.f + d);
|
|
|
|
path.lineTo(128.f + d, 128.f - d);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
// BR: Text
|
|
|
|
{
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(256, 256);
|
|
|
|
canvas->clipShader(shader, SkClipOp::kDifference);
|
|
|
|
for (int y = 0; y < 4; ++y) {
|
|
|
|
canvas->drawString("Hello", 32.f, y * 64.f, SkFont(nullptr, 64.f), paint);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|