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.
|
|
|
|
*/
|
2016-03-09 02:50:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
#include "SkCamera.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkGradientShader.h"
|
2016-03-18 22:48:49 +00:00
|
|
|
#include "SkImage.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkInterpolator.h"
|
|
|
|
#include "SkMaskFilter.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkKey.h"
|
|
|
|
#include "SkDrawFilter.h"
|
|
|
|
|
2014-04-28 14:55:39 +00:00
|
|
|
static void make_paint(SkPaint* paint, const SkMatrix& localMatrix) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColor colors[] = { 0, SK_ColorWHITE };
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint pts[] = { { 0, 0 }, { 0, SK_Scalar1*20 } };
|
2016-03-09 02:50:00 +00:00
|
|
|
paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
|
|
|
|
SkShader::kClamp_TileMode, 0, &localMatrix));
|
2016-10-06 00:33:02 +00:00
|
|
|
paint->setBlendMode(SkBlendMode::kDstIn);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test drawing with strips of fading gradient above and below
|
|
|
|
static void test_fade(SkCanvas* canvas) {
|
|
|
|
SkAutoCanvasRestore ar(canvas, true);
|
|
|
|
|
|
|
|
SkRect r;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
|
2013-11-18 16:03:59 +00:00
|
|
|
SkAutoCanvasRestore ar2(canvas, false);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
// create the layers
|
|
|
|
|
|
|
|
r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
canvas->clipRect(r);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
r.fBottom = SkIntToScalar(20);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&r, nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
r.fTop = SkIntToScalar(80);
|
|
|
|
r.fBottom = SkIntToScalar(100);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&r, nullptr);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
// now draw the "content"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
if (true) {
|
|
|
|
r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
|
|
|
|
canvas->saveLayerAlpha(&r, 0x80);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
canvas->drawOval(r, p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
} else {
|
|
|
|
r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// return;
|
|
|
|
|
|
|
|
// now apply an effect
|
2014-04-28 14:55:39 +00:00
|
|
|
SkMatrix m;
|
|
|
|
m.setScale(SK_Scalar1, -SK_Scalar1);
|
|
|
|
m.postTranslate(0, SkIntToScalar(100));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2014-04-28 14:55:39 +00:00
|
|
|
make_paint(&paint, m);
|
2008-12-17 15:59:43 +00:00
|
|
|
r.set(0, 0, SkIntToScalar(100), SkIntToScalar(20));
|
|
|
|
// SkDebugf("--------- draw top grad\n");
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
r.fTop = SkIntToScalar(80);
|
|
|
|
r.fBottom = SkIntToScalar(100);
|
|
|
|
// SkDebugf("--------- draw bot grad\n");
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RedFilter : public SkDrawFilter {
|
|
|
|
public:
|
2015-03-26 01:17:31 +00:00
|
|
|
bool filter(SkPaint* p, SkDrawFilter::Type) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
fColor = p->getColor();
|
|
|
|
if (fColor == SK_ColorRED) {
|
|
|
|
p->setColor(SK_ColorGREEN);
|
|
|
|
}
|
2012-11-26 20:26:54 +00:00
|
|
|
return true;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkColor fColor;
|
|
|
|
};
|
|
|
|
|
|
|
|
class LayersView : public SkView {
|
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
LayersView() {}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2015-07-13 13:18:39 +00:00
|
|
|
bool onQuery(SkEvent* evt) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Layers");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void drawBG(SkCanvas* canvas) {
|
2009-05-22 14:39:03 +00:00
|
|
|
canvas->drawColor(SK_ColorGRAY);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
this->drawBG(canvas);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-05-22 14:39:03 +00:00
|
|
|
if (true) {
|
|
|
|
SkRect r;
|
2012-08-23 18:19:56 +00:00
|
|
|
r.set(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(120));
|
2009-05-22 14:39:03 +00:00
|
|
|
SkPaint p;
|
|
|
|
canvas->saveLayer(&r, &p);
|
|
|
|
canvas->drawColor(0xFFFF0000);
|
2010-12-20 18:26:13 +00:00
|
|
|
p.setAlpha(0); // or 0
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(SkBlendMode::kSrc);
|
2009-05-22 14:39:03 +00:00
|
|
|
canvas->drawOval(r, p);
|
|
|
|
canvas->restore();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (false) {
|
|
|
|
SkRect r;
|
2012-08-23 18:19:56 +00:00
|
|
|
r.set(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(120));
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
p.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (true) {
|
|
|
|
canvas->saveLayer(&r, &p);
|
|
|
|
p.setColor(0xFFFF0000);
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
p.setColor(0xFF0000FF);
|
|
|
|
r.offset(SkIntToScalar(20), SkIntToScalar(50));
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(300), 0);
|
|
|
|
|
|
|
|
SkRect r;
|
2012-08-23 18:19:56 +00:00
|
|
|
r.set(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(60));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-04-18 14:19:31 +00:00
|
|
|
canvas->saveLayer(&r, &p);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
r.set(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(120));
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
canvas->restore();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
test_fade(canvas);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-01-08 16:17:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
bool onClick(Click* click) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->INHERITED::onClick(click);
|
|
|
|
}
|
|
|
|
|
2013-03-12 14:36:57 +00:00
|
|
|
virtual bool handleKey(SkKey) {
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
2016-01-07 19:28:08 +00:00
|
|
|
DEF_SAMPLE( return new LayersView; )
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-01-07 19:28:08 +00:00
|
|
|
#include "SkBlurImageFilter.h"
|
|
|
|
#include "SkMatrixConvolutionImageFilter.h"
|
|
|
|
#include "SkMorphologyImageFilter.h"
|
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkAnimTimer.h"
|
|
|
|
|
|
|
|
class BackdropView : public SampleView {
|
|
|
|
SkPoint fCenter;
|
|
|
|
SkScalar fAngle;
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
2016-04-05 16:09:36 +00:00
|
|
|
sk_sp<SkImageFilter> fFilter;
|
2016-01-07 19:28:08 +00:00
|
|
|
public:
|
|
|
|
BackdropView() {
|
|
|
|
fCenter.set(200, 150);
|
|
|
|
fAngle = 0;
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = GetResourceAsImage("mandrill_512.png");
|
2016-04-05 16:09:36 +00:00
|
|
|
fFilter = SkDilateImageFilter::Make(8, 8, nullptr);
|
2016-01-07 19:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
bool onQuery(SkEvent* evt) override {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Backdrop");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawImage(fImage.get(), 0, 0, nullptr);
|
2016-01-07 19:28:08 +00:00
|
|
|
|
|
|
|
const SkScalar w = 250;
|
|
|
|
const SkScalar h = 150;
|
|
|
|
SkPath path;
|
|
|
|
path.addOval(SkRect::MakeXYWH(-w/2, -h/2, w, h));
|
|
|
|
SkMatrix m;
|
|
|
|
m.setRotate(fAngle);
|
|
|
|
m.postTranslate(fCenter.x(), fCenter.y());
|
|
|
|
path.transform(m);
|
|
|
|
|
2016-09-20 15:42:38 +00:00
|
|
|
canvas->clipPath(path, SkCanvas::kIntersect_Op, true);
|
2016-01-07 19:28:08 +00:00
|
|
|
const SkRect bounds = path.getBounds();
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAlpha(0xCC);
|
2016-04-05 16:09:36 +00:00
|
|
|
canvas->saveLayer({ &bounds, &paint, fFilter.get(), 0 });
|
2016-01-07 19:28:08 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
|
|
|
fAngle = SkDoubleToScalar(fmod(timer.secs() * 360 / 5, 360));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
|
|
|
this->inval(nullptr);
|
|
|
|
return new Click(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onClick(Click* click) override {
|
|
|
|
this->inval(nullptr);
|
|
|
|
fCenter = click->fCurr;
|
|
|
|
return this->INHERITED::onClick(click);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
DEF_SAMPLE( return new BackdropView; )
|