blob: da6628e9f4f22bda8d636b43002c2be1a3e1d50b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkDrawLooper.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkShader.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTileMode.h"
23#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkBlurDrawLooper.h"
25#include "src/core/SkBlurMask.h"
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000026
27namespace skiagm {
28
29///////////////////////////////////////////////////////////////////////////////
30
reed@google.come3605d02011-05-26 15:12:35 +000031static void setup(SkPaint* paint, SkColor c, SkScalar strokeWidth) {
32 paint->setColor(c);
33 if (strokeWidth < 0) {
34 paint->setStyle(SkPaint::kFill_Style);
35 } else {
36 paint->setStyle(SkPaint::kStroke_Style);
37 paint->setStrokeWidth(strokeWidth);
38 }
39}
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000040
reed@google.come3605d02011-05-26 15:12:35 +000041class ShadowsGM : public GM {
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000042public:
43 SkPath fCirclePath;
reed@google.come3605d02011-05-26 15:12:35 +000044 SkRect fRect;
caryclark500ab752015-09-04 05:20:32 -070045 SkBitmap fBitmap;
reed@google.come3605d02011-05-26 15:12:35 +000046
caryclark63c684a2015-02-25 09:04:04 -080047protected:
mtklein36352bf2015-03-25 18:17:31 -070048 void onOnceBeforeDraw() override {
Mike Kleind46dce32018-08-16 10:17:03 -040049 this->setBGColor(0xFFDDDDDD);
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000050 fCirclePath.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(10) );
reed@google.come3605d02011-05-26 15:12:35 +000051 fRect.set(SkIntToScalar(10), SkIntToScalar(10),
52 SkIntToScalar(30), SkIntToScalar(30));
caryclark500ab752015-09-04 05:20:32 -070053 fBitmap.allocPixels(SkImageInfo::Make(20, 20, SkColorType::kAlpha_8_SkColorType,
54 kPremul_SkAlphaType));
55 SkCanvas canvas(fBitmap);
56 canvas.clear(0x0);
57 SkPaint p;
58 canvas.drawRect(SkRect::MakeXYWH(10, 0, 10, 10), p);
59 canvas.drawRect(SkRect::MakeXYWH(0, 10, 10, 10), p);
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000060 }
61
mtkleinf0599002015-07-13 06:18:39 -070062 SkString onShortName() override {
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000063 return SkString("shadows");
64 }
65
mtkleinf0599002015-07-13 06:18:39 -070066 SkISize onISize() override {
caryclarkc8b350d2015-09-04 06:19:48 -070067 return SkISize::Make(200, 200);
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000068 }
69
mtkleinf0599002015-07-13 06:18:39 -070070 void onDraw(SkCanvas* canvas) override {
reed7b380d02016-03-21 13:25:16 -070071 sk_sp<SkDrawLooper> shadowLoopers[] = {
72 SkBlurDrawLooper::Make(SK_ColorBLUE,
mtkleinf0599002015-07-13 06:18:39 -070073 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040074 SkIntToScalar(5), SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070075 SkBlurDrawLooper::Make(SK_ColorBLUE,
mtkleinf0599002015-07-13 06:18:39 -070076 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
Matt Sarettf160ad42017-03-23 16:23:38 -040077 SkIntToScalar(5), SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070078 SkBlurDrawLooper::Make(SK_ColorBLACK,
mtkleinf0599002015-07-13 06:18:39 -070079 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
80 SkIntToScalar(5),
Matt Sarettf160ad42017-03-23 16:23:38 -040081 SkIntToScalar(10)),
reed7b380d02016-03-21 13:25:16 -070082 SkBlurDrawLooper::Make(0x7FFF0000,
mtkleinf0599002015-07-13 06:18:39 -070083 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
Matt Sarettf160ad42017-03-23 16:23:38 -040084 SkIntToScalar(-5), SkIntToScalar(-10)),
reed7b380d02016-03-21 13:25:16 -070085 SkBlurDrawLooper::Make(SK_ColorBLACK, SkIntToScalar(0),
Matt Sarettf160ad42017-03-23 16:23:38 -040086 SkIntToScalar(5), SkIntToScalar(5)),
reed7b380d02016-03-21 13:25:16 -070087 };
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +000088
mtkleindbfd7ab2016-09-01 11:24:54 -070089 constexpr struct {
mtkleinf0599002015-07-13 06:18:39 -070090 SkColor fColor;
91 SkScalar fStrokeWidth;
92 } gRec[] = {
93 { SK_ColorRED, -SK_Scalar1 },
94 { SK_ColorGREEN, SkIntToScalar(4) },
95 { SK_ColorBLUE, SkIntToScalar(0)},
96 };
reed@google.come3605d02011-05-26 15:12:35 +000097
mtkleinf0599002015-07-13 06:18:39 -070098 SkPaint paint;
99 paint.setAntiAlias(true);
100 for (size_t i = 0; i < SK_ARRAY_COUNT(shadowLoopers); ++i) {
101 SkAutoCanvasRestore acr(canvas, true);
reed@google.come3605d02011-05-26 15:12:35 +0000102
mtkleinf0599002015-07-13 06:18:39 -0700103 paint.setLooper(shadowLoopers[i]);
reed@google.come3605d02011-05-26 15:12:35 +0000104
mtkleinf0599002015-07-13 06:18:39 -0700105 canvas->translate(SkIntToScalar((unsigned int)i*40), SkIntToScalar(0));
106 setup(&paint, gRec[0].fColor, gRec[0].fStrokeWidth);
107 canvas->drawRect(fRect, paint);
reed@google.come3605d02011-05-26 15:12:35 +0000108
mtkleinf0599002015-07-13 06:18:39 -0700109 canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
110 setup(&paint, gRec[1].fColor, gRec[1].fStrokeWidth);
111 canvas->drawPath(fCirclePath, paint);
junov@chromium.org7c545bd2012-04-05 13:20:59 +0000112
mtkleinf0599002015-07-13 06:18:39 -0700113 canvas->translate(SkIntToScalar(0), SkIntToScalar(40));
114 setup(&paint, gRec[2].fColor, gRec[2].fStrokeWidth);
115 canvas->drawPath(fCirclePath, paint);
caryclark500ab752015-09-04 05:20:32 -0700116
117 // see bug.skia.org/562 (reference, draws correct)
118 canvas->translate(0, 40);
119 paint.setColor(SK_ColorBLACK);
120 canvas->drawBitmap(fBitmap, 10, 10, &paint);
121
122 canvas->translate(0, 40);
Mike Reed50acf8f2019-04-08 13:20:23 -0400123 paint.setShader(fBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
caryclark500ab752015-09-04 05:20:32 -0700124
125 // see bug.skia.org/562 (shows bug as reported)
caryclark500ab752015-09-04 05:20:32 -0700126 paint.setStyle(SkPaint::kFill_Style);
127 canvas->drawRect(SkRect::MakeXYWH(10, 10, 20, 20), paint);
128 paint.setShader(nullptr);
mtkleinf0599002015-07-13 06:18:39 -0700129 }
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +0000130 }
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +0000131
132private:
133 typedef GM INHERITED;
134};
135
136///////////////////////////////////////////////////////////////////////////////
137
Hal Canarye964c182019-01-23 10:22:01 -0500138DEF_GM( return new ShadowsGM; )
senorblanco@chromium.org4868e6b2011-02-18 19:03:01 +0000139
140}