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
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkMaskFilter.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
2022-04-25 11:54:10 +00:00
|
|
|
#include "include/utils/SkCamera.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
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,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp, 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
|
|
|
|
2013-11-18 16:03:59 +00:00
|
|
|
SkAutoCanvasRestore ar2(canvas, false);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
// create the layers
|
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(100, 100);
|
2008-12-17 15:59:43 +00:00
|
|
|
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
|
|
|
|
2022-02-04 21:09:04 +00:00
|
|
|
if ((true)) {
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(100, 100);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
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 {
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(100, 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);
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(100, 20);
|
2008-12-17 15:59:43 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class LayersView : public Sample {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
LayersView() {}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("Layers"); }
|
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
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
this->drawBG(canvas);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2022-02-04 21:09:04 +00:00
|
|
|
if ((true)) {
|
2009-05-22 14:39:03 +00:00
|
|
|
SkRect r;
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(220, 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
|
|
|
|
2022-02-04 21:09:04 +00:00
|
|
|
if ((false)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRect r;
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(220, 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);
|
|
|
|
}
|
|
|
|
|
2022-02-04 21:09:04 +00:00
|
|
|
if ((false)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(300), 0);
|
|
|
|
|
|
|
|
SkRect r;
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(220, 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
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setWH(220, 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
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2016-01-07 19:28:08 +00:00
|
|
|
DEF_SAMPLE( return new LayersView; )
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-08-01 20:38:24 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2016-01-07 19:28:08 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
2016-01-07 19:28:08 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class BackdropView : public Sample {
|
2016-01-07 19:28:08 +00:00
|
|
|
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;
|
2017-12-08 15:21:31 +00:00
|
|
|
fImage = GetResourceAsImage("images/mandrill_512.png");
|
2019-08-01 20:38:24 +00:00
|
|
|
fFilter = SkImageFilters::Dilate(8, 8, nullptr);
|
2016-01-07 19:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("Backdrop"); }
|
2016-01-07 19:28:08 +00:00
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2021-01-22 20:26:41 +00:00
|
|
|
canvas->drawImage(fImage.get(), 0, 0);
|
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);
|
|
|
|
|
2021-08-03 17:08:50 +00:00
|
|
|
canvas->clipPath(path, SkClipOp::kIntersect, true);
|
2016-01-07 19:28:08 +00:00
|
|
|
const SkRect bounds = path.getBounds();
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAlpha(0xCC);
|
2020-05-30 20:02:14 +00:00
|
|
|
canvas->saveLayer(SkCanvas::SaveLayerRec(&bounds, &paint, fFilter.get(), 0));
|
2016-01-07 19:28:08 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
fAngle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 5, 360));
|
2016-01-07 19:28:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
|
2019-07-12 15:32:43 +00:00
|
|
|
return new Click();
|
2016-01-07 19:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool onClick(Click* click) override {
|
|
|
|
fCenter = click->fCurr;
|
2019-07-12 15:32:43 +00:00
|
|
|
return true;
|
2016-01-07 19:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2016-01-07 19:28:08 +00:00
|
|
|
};
|
|
|
|
DEF_SAMPLE( return new BackdropView; )
|