blob: 0319984f7c4a91c383bd3c76867a2ace518821d9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bungemand3ebb482015-08-05 13:57:49 -07007
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +00008#include "gm.h"
reedfb8c1fc2015-08-04 18:44:56 -07009#include "SkBlurDrawLooper.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkBlurMask.h"
mtklein7a1f45f2016-08-04 06:19:33 -070011#include "SkColorFilter.h"
12#include "SkMaskFilter.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000014
15namespace skiagm {
16
17///////////////////////////////////////////////////////////////////////////////
18
reed@google.come3605d02011-05-26 15:12:35 +000019static void setup(SkPaint* paint, SkColor c, SkScalar strokeWidth) {
20 paint->setColor(c);
21 if (strokeWidth < 0) {
22 paint->setStyle(SkPaint::kFill_Style);
23 } else {
24 paint->setStyle(SkPaint::kStroke_Style);
25 paint->setStrokeWidth(strokeWidth);
26 }
27}
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000028
reed@google.come3605d02011-05-26 15:12:35 +000029class ShadowsGM : public GM {
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000030public:
31 SkPath fCirclePath;
reed@google.come3605d02011-05-26 15:12:35 +000032 SkRect fRect;
caryclark500ab752015-09-04 05:20:32 -070033 SkBitmap fBitmap;
reed@google.come3605d02011-05-26 15:12:35 +000034
caryclark63c684a2015-02-25 09:04:04 -080035protected:
mtklein36352bf2015-03-25 18:17:31 -070036 void onOnceBeforeDraw() override {
Mike Kleind46dce32018-08-16 10:17:03 -040037 this->setBGColor(0xFFDDDDDD);
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000038 fCirclePath.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(10) );
reed@google.come3605d02011-05-26 15:12:35 +000039 fRect.set(SkIntToScalar(10), SkIntToScalar(10),
40 SkIntToScalar(30), SkIntToScalar(30));
caryclark500ab752015-09-04 05:20:32 -070041 fBitmap.allocPixels(SkImageInfo::Make(20, 20, SkColorType::kAlpha_8_SkColorType,
42 kPremul_SkAlphaType));
43 SkCanvas canvas(fBitmap);
44 canvas.clear(0x0);
45 SkPaint p;
46 canvas.drawRect(SkRect::MakeXYWH(10, 0, 10, 10), p);
47 canvas.drawRect(SkRect::MakeXYWH(0, 10, 10, 10), p);
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000048 }
49
mtkleinf0599002015-07-13 06:18:39 -070050 SkString onShortName() override {
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000051 return SkString("shadows");
52 }
53
mtkleinf0599002015-07-13 06:18:39 -070054 SkISize onISize() override {
caryclarkc8b350d2015-09-04 06:19:48 -070055 return SkISize::Make(200, 200);
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000056 }
57
mtkleinf0599002015-07-13 06:18:39 -070058 void onDraw(SkCanvas* canvas) override {
reed7b380d02016-03-21 13:25:16 -070059 sk_sp<SkDrawLooper> shadowLoopers[] = {
60 SkBlurDrawLooper::Make(SK_ColorBLUE,
mtkleinf0599002015-07-13 06:18:39 -070061 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040062 SkIntToScalar(5), SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070063 SkBlurDrawLooper::Make(SK_ColorBLUE,
mtkleinf0599002015-07-13 06:18:39 -070064 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040065 SkIntToScalar(5), SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070066 SkBlurDrawLooper::Make(SK_ColorBLACK,
mtkleinf0599002015-07-13 06:18:39 -070067 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
68 SkIntToScalar(5),
Matt Sarettf160ad42017-03-23 16:23:38 -040069 SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070070 SkBlurDrawLooper::Make(0x7FFF0000,
mtkleinf0599002015-07-13 06:18:39 -070071 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
Matt Sarettf160ad42017-03-23 16:23:38 -040072 SkIntToScalar(-5), SkIntToScalar(-10)),
reed7b380d02016-03-21 13:25:16 -070073 SkBlurDrawLooper::Make(SK_ColorBLACK, SkIntToScalar(0),
Matt Sarettf160ad42017-03-23 16:23:38 -040074 SkIntToScalar(5), SkIntToScalar(5)),
reed7b380d02016-03-21 13:25:16 -070075 };
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000076
mtkleindbfd7ab2016-09-01 11:24:54 -070077 constexpr struct {
mtkleinf0599002015-07-13 06:18:39 -070078 SkColor fColor;
79 SkScalar fStrokeWidth;
80 } gRec[] = {
81 { SK_ColorRED, -SK_Scalar1 },
82 { SK_ColorGREEN, SkIntToScalar(4) },
83 { SK_ColorBLUE, SkIntToScalar(0)},
84 };
reed@google.come3605d02011-05-26 15:12:35 +000085
mtkleinf0599002015-07-13 06:18:39 -070086 SkPaint paint;
87 paint.setAntiAlias(true);
88 for (size_t i = 0; i < SK_ARRAY_COUNT(shadowLoopers); ++i) {
89 SkAutoCanvasRestore acr(canvas, true);
reed@google.come3605d02011-05-26 15:12:35 +000090
mtkleinf0599002015-07-13 06:18:39 -070091 paint.setLooper(shadowLoopers[i]);
reed@google.come3605d02011-05-26 15:12:35 +000092
mtkleinf0599002015-07-13 06:18:39 -070093 canvas->translate(SkIntToScalar((unsigned int)i*40), SkIntToScalar(0));
94 setup(&paint, gRec[0].fColor, gRec[0].fStrokeWidth);
95 canvas->drawRect(fRect, paint);
reed@google.come3605d02011-05-26 15:12:35 +000096
mtkleinf0599002015-07-13 06:18:39 -070097 canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
98 setup(&paint, gRec[1].fColor, gRec[1].fStrokeWidth);
99 canvas->drawPath(fCirclePath, paint);
junov@chromium.org7c545bd2012-04-05 13:20:59 +0000100
mtkleinf0599002015-07-13 06:18:39 -0700101 canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
102 setup(&paint, gRec[2].fColor, gRec[2].fStrokeWidth);
103 canvas->drawPath(fCirclePath, paint);
caryclark500ab752015-09-04 05:20:32 -0700104
105 // see bug.skia.org/562 (reference, draws correct)
106 canvas->translate(0, 40);
107 paint.setColor(SK_ColorBLACK);
108 canvas->drawBitmap(fBitmap, 10, 10, &paint);
109
110 canvas->translate(0, 40);
reed1a9b9642016-03-13 14:13:58 -0700111 paint.setShader(SkShader::MakeBitmapShader(
caryclark500ab752015-09-04 05:20:32 -0700112 fBitmap, SkShader::kRepeat_TileMode,
113 SkShader::kRepeat_TileMode));
114
115 // see bug.skia.org/562 (shows bug as reported)
caryclark500ab752015-09-04 05:20:32 -0700116 paint.setStyle(SkPaint::kFill_Style);
117 canvas->drawRect(SkRect::MakeXYWH(10, 10, 20, 20), paint);
118 paint.setShader(nullptr);
mtkleinf0599002015-07-13 06:18:39 -0700119 }
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +0000120 }
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +0000121
122private:
123 typedef GM INHERITED;
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
128static GM* MyFactory(void*) { return new ShadowsGM; }
129static GMRegistry reg(MyFactory);
130
131}