2013-04-22 14:37:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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"
|
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRect.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/pathops/SkPathOps.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2013-04-22 14:37:05 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class PathOpsInverseGM : public GM {
|
|
|
|
public:
|
|
|
|
PathOpsInverseGM() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
const unsigned oneColor = ToolUtils::color_to_565(0xFF8080FF);
|
2013-04-22 14:37:05 +00:00
|
|
|
const unsigned twoColor = 0x807F1f1f;
|
|
|
|
SkColor blendColor = blend(oneColor, twoColor);
|
2013-04-22 20:12:47 +00:00
|
|
|
makePaint(&fOnePaint, oneColor);
|
|
|
|
makePaint(&fTwoPaint, twoColor);
|
2015-03-26 14:52:43 +00:00
|
|
|
makePaint(&fOpPaint[kDifference_SkPathOp], oneColor);
|
|
|
|
makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor);
|
2019-03-20 16:12:10 +00:00
|
|
|
makePaint(&fOpPaint[kUnion_SkPathOp], ToolUtils::color_to_565(0xFFc0FFc0));
|
2015-03-26 14:52:43 +00:00
|
|
|
makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor);
|
2019-03-20 16:12:10 +00:00
|
|
|
makePaint(&fOpPaint[kXOR_SkPathOp], ToolUtils::color_to_565(0xFFa0FFe0));
|
2013-04-22 20:12:47 +00:00
|
|
|
makePaint(&fOutlinePaint, 0xFF000000);
|
|
|
|
fOutlinePaint.setStyle(SkPaint::kStroke_Style);
|
2013-04-22 14:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkColor blend(SkColor one, SkColor two) {
|
|
|
|
SkBitmap temp;
|
2014-01-25 16:46:20 +00:00
|
|
|
temp.allocN32Pixels(1, 1);
|
2013-04-22 14:37:05 +00:00
|
|
|
SkCanvas canvas(temp);
|
|
|
|
canvas.drawColor(one);
|
|
|
|
canvas.drawColor(two);
|
|
|
|
void* pixels = temp.getPixels();
|
|
|
|
return *(SkColor*) pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void makePaint(SkPaint* paint, SkColor color) {
|
|
|
|
paint->setAntiAlias(true);
|
|
|
|
paint->setStyle(SkPaint::kFill_Style);
|
|
|
|
paint->setColor(color);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-04-22 14:37:05 +00:00
|
|
|
return SkString("pathopsinverse");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(1200, 900);
|
2013-04-22 14:37:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-04-22 14:37:05 +00:00
|
|
|
SkPath one, two;
|
|
|
|
int yPos = 0;
|
|
|
|
for (int oneFill = 0; oneFill <= 1; ++oneFill) {
|
2019-11-26 17:17:17 +00:00
|
|
|
SkPathFillType oneF = oneFill ? SkPathFillType::kInverseEvenOdd
|
|
|
|
: SkPathFillType::kEvenOdd;
|
2013-04-22 14:37:05 +00:00
|
|
|
for (int twoFill = 0; twoFill <= 1; ++twoFill) {
|
2019-11-26 17:17:17 +00:00
|
|
|
SkPathFillType twoF = twoFill ? SkPathFillType::kInverseEvenOdd
|
|
|
|
: SkPathFillType::kEvenOdd;
|
2013-04-22 14:37:05 +00:00
|
|
|
one.reset();
|
|
|
|
one.setFillType(oneF);
|
|
|
|
one.addRect(10, 10, 70, 70);
|
|
|
|
two.reset();
|
|
|
|
two.setFillType(twoF);
|
|
|
|
two.addRect(40, 40, 100, 100);
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, SkIntToScalar(yPos));
|
2016-09-21 18:15:07 +00:00
|
|
|
canvas->clipRect(SkRect::MakeWH(110, 110), true);
|
2013-04-22 20:12:47 +00:00
|
|
|
canvas->drawPath(one, fOnePaint);
|
|
|
|
canvas->drawPath(one, fOutlinePaint);
|
|
|
|
canvas->drawPath(two, fTwoPaint);
|
|
|
|
canvas->drawPath(two, fOutlinePaint);
|
2013-04-22 14:37:05 +00:00
|
|
|
canvas->restore();
|
|
|
|
int xPos = 150;
|
2015-03-26 14:52:43 +00:00
|
|
|
for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) {
|
2013-04-22 14:37:05 +00:00
|
|
|
SkPath result;
|
|
|
|
Op(one, two, (SkPathOp) op, &result);
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
|
2016-09-21 18:15:07 +00:00
|
|
|
canvas->clipRect(SkRect::MakeWH(110, 110), true);
|
2013-04-22 20:12:47 +00:00
|
|
|
canvas->drawPath(result, fOpPaint[op]);
|
|
|
|
canvas->drawPath(result, fOutlinePaint);
|
2013-04-22 14:37:05 +00:00
|
|
|
canvas->restore();
|
|
|
|
xPos += 150;
|
|
|
|
}
|
|
|
|
yPos += 150;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-04-22 20:12:47 +00:00
|
|
|
SkPaint fOnePaint;
|
|
|
|
SkPaint fTwoPaint;
|
|
|
|
SkPaint fOutlinePaint;
|
2015-03-26 14:52:43 +00:00
|
|
|
SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1];
|
2013-04-22 14:37:05 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new PathOpsInverseGM; )
|
2013-04-22 14:37:05 +00:00
|
|
|
|
|
|
|
}
|