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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkPathOps.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class PathOpsInverseGM : public GM {
|
|
|
|
public:
|
|
|
|
PathOpsInverseGM() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-01-09 18:06:39 +00:00
|
|
|
void onOnceBeforeDraw() SK_OVERRIDE {
|
2013-04-22 14:37:05 +00:00
|
|
|
const unsigned oneColor = 0xFF8080FF;
|
|
|
|
const unsigned twoColor = 0x807F1f1f;
|
|
|
|
SkColor blendColor = blend(oneColor, twoColor);
|
2013-04-22 20:12:47 +00:00
|
|
|
makePaint(&fOnePaint, oneColor);
|
|
|
|
makePaint(&fTwoPaint, twoColor);
|
|
|
|
makePaint(&fOpPaint[kDifference_PathOp], oneColor);
|
|
|
|
makePaint(&fOpPaint[kIntersect_PathOp], blendColor);
|
|
|
|
makePaint(&fOpPaint[kUnion_PathOp], 0xFFc0FFc0);
|
|
|
|
makePaint(&fOpPaint[kReverseDifference_PathOp], twoColor);
|
|
|
|
makePaint(&fOpPaint[kXOR_PathOp], 0xFFa0FFe0);
|
|
|
|
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-01-09 18:06:39 +00:00
|
|
|
SkString onShortName() SK_OVERRIDE {
|
2013-04-22 14:37:05 +00:00
|
|
|
return SkString("pathopsinverse");
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
SkISize onISize() SK_OVERRIDE {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(1200, 900);
|
2013-04-22 14:37:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2013-04-22 14:37:05 +00:00
|
|
|
SkPath one, two;
|
|
|
|
int yPos = 0;
|
|
|
|
for (int oneFill = 0; oneFill <= 1; ++oneFill) {
|
|
|
|
SkPath::FillType oneF = oneFill ? SkPath::kInverseEvenOdd_FillType
|
|
|
|
: SkPath::kEvenOdd_FillType;
|
|
|
|
for (int twoFill = 0; twoFill <= 1; ++twoFill) {
|
|
|
|
SkPath::FillType twoF = twoFill ? SkPath::kInverseEvenOdd_FillType
|
|
|
|
: SkPath::kEvenOdd_FillType;
|
|
|
|
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));
|
|
|
|
canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, 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;
|
|
|
|
for (int op = kDifference_PathOp; op <= kReverseDifference_PathOp; ++op) {
|
|
|
|
SkPath result;
|
|
|
|
Op(one, two, (SkPathOp) op, &result);
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
|
|
|
|
canvas->clipRect(SkRect::MakeWH(110, 110), SkRegion::kIntersect_Op, 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;
|
|
|
|
SkPaint fOpPaint[kReverseDifference_PathOp - kDifference_PathOp + 1];
|
2013-04-22 14:37:05 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new PathOpsInverseGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|