skia2/gm/complexclip.cpp
Mike Reed 121c2afb99 clipShader -- raster implementation
1. pass shader+op down to device
2. bitmapdevice pass it down to rasterclip
3. rasterclip only ever stores at most one shader
   - if there is a ctm, fold that into (another) shader
   - if the op is difference, invert the sense of alpha
   - if there was a previous shader, compose with it
4. pass through to rasterpipelineblitter
5. it prepends the colorPipeline with the clipShader, and stashes its
   results in a buffer (fClipShaderBuffer)
6. in each blit, scale/lerp from the buffer before storing the result

Change-Id: I07c7a8a20b9ae95cdcc9954237d115e63819f7c1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/275798
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
2020-03-10 18:43:21 +00:00

249 lines
8.5 KiB
C++

/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkClipOp.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontTypes.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#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"
#include "src/core/SkClipOpPriv.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include <string.h>
namespace skiagm {
constexpr SkColor gPathColor = SK_ColorBLACK;
constexpr SkColor gClipAColor = SK_ColorBLUE;
constexpr SkColor gClipBColor = SK_ColorRED;
class ComplexClipGM : public GM {
public:
ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw)
: fDoAAClip(aaclip)
, fDoSaveLayer(saveLayer)
, fInvertDraw(invertDraw) {
this->setBGColor(0xFFDEDFDE);
}
protected:
SkString onShortName() override {
SkString str;
str.printf("complexclip_%s%s%s",
fDoAAClip ? "aa" : "bw",
fDoSaveLayer ? "_layer" : "",
fInvertDraw ? "_invert" : "");
return str;
}
SkISize onISize() override { return SkISize::Make(970, 780); }
void onDraw(SkCanvas* canvas) override {
SkPath path;
path.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();
if (fInvertDraw) {
path.setFillType(SkPathFillType::kInverseEvenOdd);
} else {
path.setFillType(SkPathFillType::kEvenOdd);
}
SkPaint pathPaint;
pathPaint.setAntiAlias(true);
pathPaint.setColor(gPathColor);
SkPath clipA;
clipA.addPoly({{10, 20}, {165, 22}, {70, 105}, {165, 177}, {-5, 180}}, false).close();
SkPath clipB;
clipB.addPoly({{40, 10}, {190, 15}, {195, 190}, {40, 185}, {155, 100}}, false).close();
SkFont font(ToolUtils::create_portable_typeface(), 20);
constexpr struct {
SkClipOp fOp;
const char* fName;
} gOps[] = { //extra spaces in names for measureText
{kIntersect_SkClipOp, "Isect "},
{kDifference_SkClipOp, "Diff " },
{kUnion_SkClipOp, "Union "},
{kXOR_SkClipOp, "Xor " },
{kReverseDifference_SkClipOp, "RDiff "}
};
canvas->translate(20, 20);
canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
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(
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));
bounds.inset(100, 100);
SkPaint boundPaint;
boundPaint.setColor(SK_ColorRED);
boundPaint.setStyle(SkPaint::kStroke_Style);
canvas->drawRect(bounds, boundPaint);
canvas->clipRect(bounds);
canvas->saveLayer(&bounds, nullptr);
}
for (int invBits = 0; invBits < 4; ++invBits) {
canvas->save();
for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
this->drawHairlines(canvas, path, clipA, clipB);
bool doInvA = SkToBool(invBits & 1);
bool doInvB = SkToBool(invBits & 2);
canvas->save();
// set clip
clipA.setFillType(doInvA ? SkPathFillType::kInverseEvenOdd :
SkPathFillType::kEvenOdd);
clipB.setFillType(doInvB ? SkPathFillType::kInverseEvenOdd :
SkPathFillType::kEvenOdd);
canvas->clipPath(clipA, fDoAAClip);
canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
// 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);
}
// draw path clipped
canvas->drawPath(path, pathPaint);
canvas->restore();
SkPaint paint;
SkScalar txtX = 45;
paint.setColor(gClipAColor);
const char* aTxt = doInvA ? "InvA " : "A ";
canvas->drawSimpleText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
txtX += font.measureText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8);
paint.setColor(SK_ColorBLACK);
canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8, txtX, 220,
font, paint);
txtX += font.measureText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8);
paint.setColor(gClipBColor);
const char* bTxt = doInvB ? "InvB " : "B ";
canvas->drawSimpleText(bTxt, strlen(bTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
canvas->translate(250,0);
}
canvas->restore();
canvas->translate(0, 250);
}
if (fDoSaveLayer) {
canvas->restore();
}
}
private:
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);
// 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);
}
bool fDoAAClip;
bool fDoSaveLayer;
bool fInvertDraw;
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
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);)
}
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();
SkMatrix lm = SkMatrix::MakeScale(1.0f / 5);
canvas->clipShader(img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &lm));
canvas->drawImage(img, 0, 0, nullptr);
canvas->restore();
canvas->restore();
}