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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkClipOp.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRegion.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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkAAClip.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/core/SkClipOpPriv.h"
|
|
|
|
#include "src/core/SkMask.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.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;
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::copy_to(&bm2, bm.colorType(), bm);
|
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
|
2019-08-24 23:39:13 +00:00
|
|
|
fBase.setLTRB(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));
|
2018-08-16 14:17:03 +00:00
|
|
|
INHERITED::setBGColor(0xFFDDDDDD);
|
2012-05-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 14:00:50 +00:00
|
|
|
void buildRgn(SkAAClip* clip, SkClipOp 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-12-09 14:00:50 +00:00
|
|
|
void drawRgnOped(SkCanvas* canvas, SkClipOp 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-12-09 14:00:50 +00:00
|
|
|
void drawPathsOped(SkCanvas* canvas, SkClipOp 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 {
|
2020-05-05 14:56:42 +00:00
|
|
|
return SkISize::Make(500, 240);
|
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-12-09 14:00:50 +00:00
|
|
|
SkClipOp fOp;
|
2012-05-22 12:14:50 +00:00
|
|
|
} gOps[] = {
|
2019-03-20 16:12:10 +00:00
|
|
|
{SK_ColorBLACK, "Difference", kDifference_SkClipOp},
|
|
|
|
{SK_ColorRED, "Intersect", kIntersect_SkClipOp},
|
2012-05-22 12:14:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint textPaint;
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 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++) {
|
2019-01-08 14:38:02 +00:00
|
|
|
canvas->drawString(gOps[op].fName, 75.0f, 50.0f, font, textPaint);
|
2012-05-22 12:14:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|