blob: c060813f5bdcd094d90a4b1c2d4bde4ee37b20d0 [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 */
reed@google.com076f4c92012-07-31 14:32:38 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkBlurTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkColor.h"
13#include "include/core/SkDrawLooper.h"
14#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkMaskFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTypeface.h"
23#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkLayerDrawLooper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkBlurMask.h"
26#include "tools/ToolUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#define WIDTH 200
29#define HEIGHT 200
30
reed@google.com076f4c92012-07-31 14:32:38 +000031class DrawLooperGM : public skiagm::GM {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032public:
halcanary96fcdcc2015-08-27 07:41:13 -070033 DrawLooperGM() : fLooper(nullptr) {
Mike Kleind46dce32018-08-16 10:17:03 -040034 this->setBGColor(0xFFDDDDDD);
reed@google.com076f4c92012-07-31 14:32:38 +000035 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000036
reed@google.com076f4c92012-07-31 14:32:38 +000037protected:
mtklein36352bf2015-03-25 18:17:31 -070038 virtual SkISize onISize() override {
reed@google.com076f4c92012-07-31 14:32:38 +000039 return SkISize::Make(520, 160);
40 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000041
mtklein36352bf2015-03-25 18:17:31 -070042 SkString onShortName() override {
reed@google.com076f4c92012-07-31 14:32:38 +000043 return SkString("drawlooper");
44 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 void onDraw(SkCanvas* canvas) override {
reed@google.com076f4c92012-07-31 14:32:38 +000047 this->init();
48
49 SkPaint paint;
reed@google.com48f31bd2014-02-27 14:27:44 +000050 paint.setAntiAlias(true);
reed@google.com076f4c92012-07-31 14:32:38 +000051 paint.setLooper(fLooper);
52
Mike Kleinea3f0142019-03-20 11:12:10 -050053 SkFont font(ToolUtils::create_portable_typeface(), 72);
Mike Reedc4745d62019-01-07 09:31:58 -050054
Mike Reed3661bc92017-02-22 13:21:42 -050055 canvas->drawCircle(50, 50, 30, paint);
56 canvas->drawRect({ 150, 50, 200, 100 }, paint);
Mike Reedc4745d62019-01-07 09:31:58 -050057 canvas->drawString("Looper", 230, 100, font, paint);
reed@google.com076f4c92012-07-31 14:32:38 +000058 }
59
60private:
reed7b380d02016-03-21 13:25:16 -070061 sk_sp<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000062
reed@google.com076f4c92012-07-31 14:32:38 +000063 void init() {
64 if (fLooper) return;
65
mtkleindbfd7ab2016-09-01 11:24:54 -070066 constexpr struct {
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 SkColor fColor;
68 SkPaint::Style fStyle;
69 SkScalar fWidth;
70 SkScalar fOffset;
robertphillips@google.comb7061172013-09-06 14:16:12 +000071 SkScalar fBlur;
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 } gParams[] = {
73 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
74 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
75 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
robertphillips@google.comb7061172013-09-06 14:16:12 +000076 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000078
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000079 SkLayerDrawLooper::Builder looperBuilder;
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000081 SkLayerDrawLooper::LayerInfo info;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000082 info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -040083 info.fColorMode = SkBlendMode::kSrc;
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000085 for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000086 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000087 SkPaint* paint = looperBuilder.addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 paint->setColor(gParams[i].fColor);
89 paint->setStyle(gParams[i].fStyle);
90 paint->setStrokeWidth(gParams[i].fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 if (gParams[i].fBlur > 0) {
Mike Reed1be1f8d2018-03-14 13:01:17 -040092 paint->setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070093 SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur)));
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 }
95 }
reed7b380d02016-03-21 13:25:16 -070096 fLooper = looperBuilder.detach();
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 }
reed@google.com82065d62011-02-07 15:30:46 +000098
reed@google.com076f4c92012-07-31 14:32:38 +000099 typedef GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100};
101
102//////////////////////////////////////////////////////////////////////////////
103
scroggo96f16e82015-12-10 13:31:59 -0800104DEF_GM( return new DrawLooperGM; )