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"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkAAClip.h"
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void buildRgn(SkAAClip* clip, SkRegion::Op op) {
|
|
|
|
clip->setPath(fBasePath, NULL, true);
|
|
|
|
|
|
|
|
SkAAClip clip2;
|
|
|
|
clip2.setPath(fRectPath, NULL, true);
|
|
|
|
clip->op(clip2, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-05-22 12:14:50 +00:00
|
|
|
void drawRgnOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) {
|
|
|
|
|
|
|
|
SkAAClip clip;
|
|
|
|
|
|
|
|
this->buildRgn(&clip, op);
|
|
|
|
this->drawOrig(canvas);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(color);
|
|
|
|
paint_rgn(canvas, clip, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawPathsOped(SkCanvas* canvas, SkRegion::Op op, SkColor color) {
|
|
|
|
|
|
|
|
this->drawOrig(canvas);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
// create the clip mask with the supplied boolean op
|
|
|
|
if (kPath_GeomType == fGeomType) {
|
|
|
|
// path-based case
|
|
|
|
canvas->clipPath(fBasePath, SkRegion::kReplace_Op, true);
|
|
|
|
canvas->clipPath(fRectPath, op, true);
|
|
|
|
} else {
|
|
|
|
// rect-based case
|
|
|
|
canvas->clipRect(fBase, SkRegion::kReplace_Op, true);
|
|
|
|
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
|
|
|
|
|
|
|
static const struct {
|
|
|
|
SkColor fColor;
|
|
|
|
const char* fName;
|
|
|
|
SkRegion::Op fOp;
|
|
|
|
} gOps[] = {
|
|
|
|
{ SK_ColorBLACK, "Difference", SkRegion::kDifference_Op },
|
|
|
|
{ SK_ColorRED, "Intersect", SkRegion::kIntersect_Op },
|
|
|
|
{ 0xFF008800, "Union", SkRegion::kUnion_Op },
|
|
|
|
{ SK_ColorGREEN, "Rev Diff", SkRegion::kReverseDifference_Op },
|
2012-07-17 15:37:15 +00:00
|
|
|
{ SK_ColorYELLOW, "Replace", SkRegion::kReplace_Op },
|
|
|
|
{ SK_ColorBLUE, "XOR", SkRegion::kXOR_Op },
|
2012-05-22 12:14:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint textPaint;
|
|
|
|
textPaint.setAntiAlias(true);
|
2015-06-15 13:51:08 +00:00
|
|
|
sk_tool_utils::set_portable_typeface_always(&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
|
|
|
}
|