2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-08-05 20:57:49 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "samplecode/Sample.h"
|
2009-05-05 23:13:23 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class LayerMaskView : public Sample {
|
2009-05-05 23:13:23 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
LayerMaskView() {
|
2011-05-05 01:59:48 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-05-05 23:13:23 +00:00
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
virtual SkString name() { return SkString("LayerMask"); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-05-05 23:13:23 +00:00
|
|
|
void drawMask(SkCanvas* canvas, const SkRect& r) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
SkBitmap mask;
|
2013-12-17 19:22:07 +00:00
|
|
|
int w = SkScalarRoundToInt(r.width());
|
|
|
|
int h = SkScalarRoundToInt(r.height());
|
2014-02-17 02:55:57 +00:00
|
|
|
mask.allocN32Pixels(w, h);
|
2012-12-06 21:47:40 +00:00
|
|
|
mask.eraseColor(SK_ColorTRANSPARENT);
|
2009-05-05 23:13:23 +00:00
|
|
|
SkCanvas c(mask);
|
|
|
|
SkRect bounds = r;
|
|
|
|
bounds.offset(-bounds.fLeft, -bounds.fTop);
|
|
|
|
c.drawOval(bounds, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kDstIn);
|
2009-05-05 23:13:23 +00:00
|
|
|
canvas->drawBitmap(mask, r.fLeft, r.fTop, &paint);
|
|
|
|
} else {
|
|
|
|
SkPath p;
|
|
|
|
p.addOval(r);
|
2019-11-26 17:17:17 +00:00
|
|
|
p.setFillType(SkPathFillType::kInverseWinding);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kDstOut);
|
2009-05-05 23:13:23 +00:00
|
|
|
canvas->drawPath(p, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2009-05-05 23:13:23 +00:00
|
|
|
SkRect r;
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setLTRB(20, 20, 120, 120);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&r, nullptr);
|
2009-05-05 23:13:23 +00:00
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
drawMask(canvas, r);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-05-05 23:13:23 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2009-05-05 23:13:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new LayerMaskView(); )
|