blob: 5dba270c66eff0ffbba1b3a7d97f589dbcf437d9 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reedfb8c1fc2015-08-04 18:44:56 -070010#include "SkBlurDrawLooper.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkBlurMask.h"
mtklein7a1f45f2016-08-04 06:19:33 -070012#include "SkColorFilter.h"
13#include "SkMaskFilter.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000015
16namespace skiagm {
17
18///////////////////////////////////////////////////////////////////////////////
19
reed@google.come3605d02011-05-26 15:12:35 +000020static void setup(SkPaint* paint, SkColor c, SkScalar strokeWidth) {
21 paint->setColor(c);
22 if (strokeWidth < 0) {
23 paint->setStyle(SkPaint::kFill_Style);
24 } else {
25 paint->setStyle(SkPaint::kStroke_Style);
26 paint->setStrokeWidth(strokeWidth);
27 }
28}
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000029
reed@google.come3605d02011-05-26 15:12:35 +000030class ShadowsGM : public GM {
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000031public:
32 SkPath fCirclePath;
reed@google.come3605d02011-05-26 15:12:35 +000033 SkRect fRect;
caryclark500ab752015-09-04 05:20:32 -070034 SkBitmap fBitmap;
reed@google.come3605d02011-05-26 15:12:35 +000035
caryclark63c684a2015-02-25 09:04:04 -080036protected:
mtklein36352bf2015-03-25 18:17:31 -070037 void onOnceBeforeDraw() override {
caryclark65cdba62015-06-15 06:51:08 -070038 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000039 fCirclePath.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(10) );
reed@google.come3605d02011-05-26 15:12:35 +000040 fRect.set(SkIntToScalar(10), SkIntToScalar(10),
41 SkIntToScalar(30), SkIntToScalar(30));
caryclark500ab752015-09-04 05:20:32 -070042 fBitmap.allocPixels(SkImageInfo::Make(20, 20, SkColorType::kAlpha_8_SkColorType,
43 kPremul_SkAlphaType));
44 SkCanvas canvas(fBitmap);
45 canvas.clear(0x0);
46 SkPaint p;
47 canvas.drawRect(SkRect::MakeXYWH(10, 0, 10, 10), p);
48 canvas.drawRect(SkRect::MakeXYWH(0, 10, 10, 10), p);
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000049 }
50
mtkleinf0599002015-07-13 06:18:39 -070051 SkString onShortName() override {
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000052 return SkString("shadows");
53 }
54
mtkleinf0599002015-07-13 06:18:39 -070055 SkISize onISize() override {
caryclarkc8b350d2015-09-04 06:19:48 -070056 return SkISize::Make(200, 200);
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000057 }
58
mtkleinf0599002015-07-13 06:18:39 -070059 void onDraw(SkCanvas* canvas) override {
reed7b380d02016-03-21 13:25:16 -070060 sk_sp<SkDrawLooper> shadowLoopers[] = {
61 SkBlurDrawLooper::Make(SK_ColorBLUE,
mtkleinf0599002015-07-13 06:18:39 -070062 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040063 SkIntToScalar(5), SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070064 SkBlurDrawLooper::Make(SK_ColorBLUE,
mtkleinf0599002015-07-13 06:18:39 -070065 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040066 SkIntToScalar(5), SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070067 SkBlurDrawLooper::Make(SK_ColorBLACK,
mtkleinf0599002015-07-13 06:18:39 -070068 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
69 SkIntToScalar(5),
Matt Sarettf160ad42017-03-23 16:23:38 -040070 SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070071 SkBlurDrawLooper::Make(0x7FFF0000,
mtkleinf0599002015-07-13 06:18:39 -070072 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
Matt Sarettf160ad42017-03-23 16:23:38 -040073 SkIntToScalar(-5), SkIntToScalar(-10)),
reed7b380d02016-03-21 13:25:16 -070074 SkBlurDrawLooper::Make(SK_ColorBLACK, SkIntToScalar(0),
Matt Sarettf160ad42017-03-23 16:23:38 -040075 SkIntToScalar(5), SkIntToScalar(5)),
reed7b380d02016-03-21 13:25:16 -070076 };
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000077
mtkleindbfd7ab2016-09-01 11:24:54 -070078 constexpr struct {
mtkleinf0599002015-07-13 06:18:39 -070079 SkColor fColor;
80 SkScalar fStrokeWidth;
81 } gRec[] = {
82 { SK_ColorRED, -SK_Scalar1 },
83 { SK_ColorGREEN, SkIntToScalar(4) },
84 { SK_ColorBLUE, SkIntToScalar(0)},
85 };
reed@google.come3605d02011-05-26 15:12:35 +000086
mtkleinf0599002015-07-13 06:18:39 -070087 SkPaint paint;
88 paint.setAntiAlias(true);
89 for (size_t i = 0; i < SK_ARRAY_COUNT(shadowLoopers); ++i) {
90 SkAutoCanvasRestore acr(canvas, true);
reed@google.come3605d02011-05-26 15:12:35 +000091
mtkleinf0599002015-07-13 06:18:39 -070092 paint.setLooper(shadowLoopers[i]);
reed@google.come3605d02011-05-26 15:12:35 +000093
mtkleinf0599002015-07-13 06:18:39 -070094 canvas->translate(SkIntToScalar((unsigned int)i*40), SkIntToScalar(0));
95 setup(&paint, gRec[0].fColor, gRec[0].fStrokeWidth);
96 canvas->drawRect(fRect, paint);
reed@google.come3605d02011-05-26 15:12:35 +000097
mtkleinf0599002015-07-13 06:18:39 -070098 canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
99 setup(&paint, gRec[1].fColor, gRec[1].fStrokeWidth);
100 canvas->drawPath(fCirclePath, paint);
junov@chromium.org7c545bd2012-04-05 13:20:59 +0000101
mtkleinf0599002015-07-13 06:18:39 -0700102 canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
103 setup(&paint, gRec[2].fColor, gRec[2].fStrokeWidth);
104 canvas->drawPath(fCirclePath, paint);
caryclark500ab752015-09-04 05:20:32 -0700105
106 // see bug.skia.org/562 (reference, draws correct)
107 canvas->translate(0, 40);
108 paint.setColor(SK_ColorBLACK);
109 canvas->drawBitmap(fBitmap, 10, 10, &paint);
110
111 canvas->translate(0, 40);
reed1a9b9642016-03-13 14:13:58 -0700112 paint.setShader(SkShader::MakeBitmapShader(
caryclark500ab752015-09-04 05:20:32 -0700113 fBitmap, SkShader::kRepeat_TileMode,
114 SkShader::kRepeat_TileMode));
115
116 // see bug.skia.org/562 (shows bug as reported)
caryclark500ab752015-09-04 05:20:32 -0700117 paint.setStyle(SkPaint::kFill_Style);
118 canvas->drawRect(SkRect::MakeXYWH(10, 10, 20, 20), paint);
119 paint.setShader(nullptr);
mtkleinf0599002015-07-13 06:18:39 -0700120 }
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +0000121 }
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +0000122
123private:
124 typedef GM INHERITED;
125};
126
127///////////////////////////////////////////////////////////////////////////////
128
129static GM* MyFactory(void*) { return new ShadowsGM; }
130static GMRegistry reg(MyFactory);
131
132}