2012-05-22 12:14:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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"
|
2015-08-05 01:44:56 +00:00
|
|
|
#include "SkAAClip.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPath.h"
|
2012-05-22 12:14:50 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
static void paint_rgn(SkCanvas* canvas, const SkAAClip& clip,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
SkMask mask;
|
|
|
|
SkBitmap bm;
|
|
|
|
|
|
|
|
clip.copyToMask(&mask);
|
|
|
|
|
|
|
|
SkAutoMaskFreeImage amfi(mask.fImage);
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
bm.installMaskPixels(mask);
|
2012-05-22 12:14:50 +00:00
|
|
|
|
|
|
|
// need to copy for deferred drawing test to work
|
|
|
|
SkBitmap bm2;
|
|
|
|
|
2014-02-20 19:08:07 +00:00
|
|
|
bm.deepCopyTo(&bm2);
|
2012-05-22 12:14:50 +00:00
|
|
|
|
|
|
|
canvas->drawBitmap(bm2,
|
|
|
|
SK_Scalar1 * mask.fBounds.fLeft,
|
|
|
|
SK_Scalar1 * mask.fBounds.fTop,
|
|
|
|
&paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/*
|
2012-08-23 18:14:13 +00:00
|
|
|
* This GM tests anti aliased single operation booleans with SkAAClips,
|
2012-05-22 12:14:50 +00:00
|
|
|
* SkRect and SkPaths.
|
|
|
|
*/
|
|
|
|
class SimpleClipGM : public GM {
|
|
|
|
public:
|
|
|
|
enum SkGeomTypes {
|
|
|
|
kRect_GeomType,
|
|
|
|
kPath_GeomType,
|
|
|
|
kAAClip_GeomType
|
|
|
|
};
|
|
|
|
|
|
|
|
SimpleClipGM(SkGeomTypes geomType)
|
|
|
|
: fGeomType(geomType) {
|
2015-02-25 17:04:04 +00:00
|
|
|
}
|
2012-05-22 12:14:50 +00:00
|
|
|
|
2015-02-25 17:04:04 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2012-05-22 12:14:50 +00:00
|
|
|
// offset the rects a bit so we get anti-aliasing in the rect case
|
2013-11-25 19:44:07 +00:00
|
|
|
fBase.set(100.65f,
|
|
|
|
100.65f,
|
|
|
|
150.65f,
|
|
|
|
150.65f);
|
2012-05-22 12:14:50 +00:00
|
|
|
fRect = fBase;
|
|
|
|
fRect.inset(5, 5);
|
|
|
|
fRect.offset(25, 25);
|
|
|
|
|
|
|
|
fBasePath.addRoundRect(fBase, SkIntToScalar(5), SkIntToScalar(5));
|
|
|
|
fRectPath.addRoundRect(fRect, SkIntToScalar(5), SkIntToScalar(5));
|
2015-06-15 13:51:08 +00:00
|
|
|
INHERITED::setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
|
2012-05-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 15:42:38 +00:00
|
|
|
void buildRgn(SkAAClip* clip, SkCanvas::ClipOp op) {
|
2015-08-27 14:41:13 +00:00
|
|
|
clip->setPath(fBasePath, nullptr, true);
|
2012-05-22 12:14:50 +00:00
|
|
|
|
|
|
|
SkAAClip clip2;
|
2015-08-27 14:41:13 +00:00
|
|
|
clip2.setPath(fRectPath, nullptr, true);
|
2016-09-20 15:42:38 +00:00
|
|
|
clip->op(clip2, (SkRegion::Op)op);
|
2012-05-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawOrig(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-22 12:14:50 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-22 12:14:50 +00:00
|
|
|
canvas->drawRect(fBase, paint);
|
|
|
|
canvas->drawRect(fRect, paint);
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2016-09-20 15:42:38 +00:00
|
|
|
void drawRgnOped(SkCanvas* canvas, SkCanvas::ClipOp op, SkColor color) {
|
2012-05-22 12:14:50 +00:00
|
|
|
|
|
|
|
SkAAClip clip;
|
|
|
|
|
|
|
|
this->buildRgn(&clip, op);
|
|
|
|
this->drawOrig(canvas);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(color);
|
|
|
|
paint_rgn(canvas, clip, paint);
|
|
|
|
}
|
|
|
|
|
2016-09-20 15:42:38 +00:00
|
|
|
void drawPathsOped(SkCanvas* canvas, SkCanvas::ClipOp op, SkColor color) {
|
2012-05-22 12:14:50 +00:00
|
|
|
|
|
|
|
this->drawOrig(canvas);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
// create the clip mask with the supplied boolean op
|
|
|
|
if (kPath_GeomType == fGeomType) {
|
|
|
|
// path-based case
|
2016-09-21 18:15:07 +00:00
|
|
|
canvas->clipPath(fBasePath, true);
|
2012-05-22 12:14:50 +00:00
|
|
|
canvas->clipPath(fRectPath, op, true);
|
|
|
|
} else {
|
|
|
|
// rect-based case
|
2016-09-21 18:15:07 +00:00
|
|
|
canvas->clipRect(fBase, true);
|
2012-05-22 12:14:50 +00:00
|
|
|
canvas->clipRect(fRect, op, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw a rect that will entirely cover the clip mask area
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(color);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeLTRB(SkIntToScalar(90), SkIntToScalar(90),
|
|
|
|
SkIntToScalar(180), SkIntToScalar(180));
|
|
|
|
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
SkString onShortName() override {
|
2012-05-22 12:14:50 +00:00
|
|
|
SkString str;
|
2012-08-23 18:14:13 +00:00
|
|
|
str.printf("simpleaaclip_%s",
|
2012-05-22 12:14:50 +00:00
|
|
|
kRect_GeomType == fGeomType ? "rect" :
|
2012-08-23 18:14:13 +00:00
|
|
|
(kPath_GeomType == fGeomType ? "path" :
|
2012-05-22 12:14:50 +00:00
|
|
|
"aaclip"));
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(640, 480);
|
2012-05-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-05-22 12:14:50 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const struct {
|
2012-05-22 12:14:50 +00:00
|
|
|
SkColor fColor;
|
|
|
|
const char* fName;
|
2016-09-20 15:42:38 +00:00
|
|
|
SkCanvas::ClipOp fOp;
|
2012-05-22 12:14:50 +00:00
|
|
|
} gOps[] = {
|
2016-09-20 15:42:38 +00:00
|
|
|
{ SK_ColorBLACK, "Difference", SkCanvas::kDifference_Op },
|
|
|
|
{ SK_ColorRED, "Intersect", SkCanvas::kIntersect_Op },
|
|
|
|
{ sk_tool_utils::color_to_565(0xFF008800), "Union", SkCanvas::kUnion_Op },
|
|
|
|
{ SK_ColorGREEN, "Rev Diff", SkCanvas::kReverseDifference_Op },
|
|
|
|
{ SK_ColorYELLOW, "Replace", SkCanvas::kReplace_Op },
|
|
|
|
{ SK_ColorBLUE, "XOR", SkCanvas::kXOR_Op },
|
2012-05-22 12:14:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint textPaint;
|
|
|
|
textPaint.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&textPaint);
|
2012-05-22 12:14:50 +00:00
|
|
|
textPaint.setTextSize(SK_Scalar1*24);
|
2012-07-17 15:37:15 +00:00
|
|
|
int xOff = 0;
|
2012-05-22 12:14:50 +00:00
|
|
|
|
|
|
|
for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); op++) {
|
|
|
|
canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
|
2012-08-23 18:14:13 +00:00
|
|
|
SkIntToScalar(75), SkIntToScalar(50),
|
2012-05-22 12:14:50 +00:00
|
|
|
textPaint);
|
|
|
|
|
|
|
|
if (kAAClip_GeomType == fGeomType) {
|
|
|
|
this->drawRgnOped(canvas, gOps[op].fOp, gOps[op].fColor);
|
|
|
|
} else {
|
|
|
|
this->drawPathsOped(canvas, gOps[op].fOp, gOps[op].fColor);
|
|
|
|
}
|
|
|
|
|
2012-07-17 15:37:15 +00:00
|
|
|
if (xOff >= 400) {
|
|
|
|
canvas->translate(SkIntToScalar(-400), SkIntToScalar(250));
|
|
|
|
xOff = 0;
|
2012-05-22 12:14:50 +00:00
|
|
|
} else {
|
|
|
|
canvas->translate(SkIntToScalar(200), 0);
|
2012-07-17 15:37:15 +00:00
|
|
|
xOff += 200;
|
2012-05-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
|
|
|
|
SkGeomTypes fGeomType;
|
|
|
|
|
|
|
|
SkRect fBase;
|
|
|
|
SkRect fRect;
|
|
|
|
|
|
|
|
SkPath fBasePath; // fBase as a round rect
|
|
|
|
SkPath fRectPath; // fRect as a round rect
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// rects
|
2012-12-14 12:59:07 +00:00
|
|
|
DEF_GM( return new SimpleClipGM(SimpleClipGM::kRect_GeomType); )
|
|
|
|
DEF_GM( return new SimpleClipGM(SimpleClipGM::kPath_GeomType); )
|
|
|
|
DEF_GM( return new SimpleClipGM(SimpleClipGM::kAAClip_GeomType); )
|
2012-12-15 02:03:10 +00:00
|
|
|
|
2012-05-22 12:14:50 +00:00
|
|
|
}
|