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.
|
|
|
|
*/
|
2011-03-10 19:20:15 +00:00
|
|
|
#include "gm.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
//#include "SkParsePath.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
//#include "SkRandom.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2011-12-08 16:18:29 +00:00
|
|
|
static const SkColor gPathColor = SK_ColorBLACK;
|
|
|
|
static const SkColor gClipAColor = SK_ColorBLUE;
|
|
|
|
static const SkColor gClipBColor = SK_ColorRED;
|
|
|
|
|
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) {
|
2011-12-08 16:18:29 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2011-03-10 19:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2011-03-10 19:20:15 +00:00
|
|
|
|
|
|
|
SkString onShortName() {
|
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
|
|
|
}
|
|
|
|
|
2014-06-10 06:59:03 +00:00
|
|
|
SkISize onISize() { return SkISize::Make(970, 780); }
|
2011-03-10 19:20:15 +00:00
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(SkIntToScalar(0), SkIntToScalar(50));
|
|
|
|
path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0));
|
|
|
|
path.lineTo(SkIntToScalar(175), SkIntToScalar(0));
|
|
|
|
path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25));
|
|
|
|
path.lineTo(SkIntToScalar(200), SkIntToScalar(150));
|
|
|
|
path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200));
|
|
|
|
path.lineTo(SkIntToScalar(0), SkIntToScalar(200));
|
|
|
|
path.close();
|
|
|
|
path.moveTo(SkIntToScalar(50), SkIntToScalar(50));
|
|
|
|
path.lineTo(SkIntToScalar(150), SkIntToScalar(50));
|
|
|
|
path.lineTo(SkIntToScalar(150), SkIntToScalar(125));
|
|
|
|
path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150));
|
|
|
|
path.lineTo(SkIntToScalar(50), SkIntToScalar(150));
|
|
|
|
path.close();
|
2014-12-17 22:38:49 +00:00
|
|
|
if (fInvertDraw) {
|
|
|
|
path.setFillType(SkPath::kInverseEvenOdd_FillType);
|
|
|
|
} else {
|
|
|
|
path.setFillType(SkPath::kEvenOdd_FillType);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
SkPath clipA;
|
|
|
|
clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20));
|
|
|
|
clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22));
|
|
|
|
clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105));
|
|
|
|
clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177));
|
|
|
|
clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180));
|
|
|
|
clipA.close();
|
|
|
|
|
|
|
|
SkPath clipB;
|
|
|
|
clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10));
|
|
|
|
clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15));
|
|
|
|
clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190));
|
|
|
|
clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185));
|
|
|
|
clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100));
|
|
|
|
clipB.close();
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2014-06-23 18:25:00 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2011-12-08 16:18:29 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(20));
|
2011-03-10 19:20:15 +00:00
|
|
|
|
|
|
|
static const struct {
|
|
|
|
SkRegion::Op fOp;
|
|
|
|
const char* fName;
|
|
|
|
} gOps[] = { //extra spaces in names for measureText
|
|
|
|
{SkRegion::kIntersect_Op, "Isect "},
|
|
|
|
{SkRegion::kDifference_Op, "Diff " },
|
|
|
|
{SkRegion::kUnion_Op, "Union "},
|
|
|
|
{SkRegion::kXOR_Op, "Xor " },
|
|
|
|
{SkRegion::kReverseDifference_Op, "RDiff "}
|
|
|
|
};
|
|
|
|
|
2011-12-08 16:18:29 +00:00
|
|
|
canvas->translate(SkIntToScalar(20), SkIntToScalar(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
|
|
|
|
|
|
|
bounds.inset(SkIntToScalar(100), SkIntToScalar(100));
|
2012-07-12 13:48:46 +00:00
|
|
|
SkPaint boundPaint;
|
|
|
|
boundPaint.setColor(SK_ColorRED);
|
|
|
|
boundPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(bounds, boundPaint);
|
|
|
|
canvas->saveLayer(&bounds, NULL);
|
|
|
|
}
|
|
|
|
|
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
|
2011-12-08 16:18:29 +00:00
|
|
|
clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
|
|
|
|
SkPath::kEvenOdd_FillType);
|
|
|
|
clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
|
|
|
|
SkPath::kEvenOdd_FillType);
|
|
|
|
canvas->clipPath(clipA, SkRegion::kIntersect_Op, fDoAAClip);
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
2011-12-08 16:18:29 +00:00
|
|
|
SkScalar txtX = SkIntToScalar(45);
|
|
|
|
paint.setColor(gClipAColor);
|
|
|
|
const char* aTxt = doInvA ? "InvA " : "A ";
|
2011-03-10 19:20:15 +00:00
|
|
|
canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint);
|
|
|
|
txtX += paint.measureText(aTxt, strlen(aTxt));
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
|
|
|
|
txtX, SkIntToScalar(220), paint);
|
|
|
|
txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
|
2011-12-08 16:18:29 +00:00
|
|
|
paint.setColor(gClipBColor);
|
|
|
|
const char* bTxt = doInvB ? "InvB " : "B ";
|
|
|
|
canvas->drawText(bTxt, strlen(bTxt), txtX, SkIntToScalar(220), paint);
|
2011-03-10 19:20:15 +00:00
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(250),0);
|
|
|
|
}
|
2011-12-08 16:18:29 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, SkIntToScalar(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;
|
|
|
|
|
2011-03-10 19:20:15 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-12-17 22:38:49 +00:00
|
|
|
DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, false, false)); )
|
|
|
|
DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, false, true)); )
|
|
|
|
DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, true, false)); )
|
|
|
|
DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, true, true)); )
|
|
|
|
DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, false, false)); )
|
|
|
|
DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, false, true)); )
|
|
|
|
DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, true, false)); )
|
|
|
|
DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, true, true)); )
|
2011-03-10 19:20:15 +00:00
|
|
|
|
|
|
|
}
|