blob: 49f1f1279d8bbfaf4ef7853acb100a7d0acf4023 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
12#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkLayerDrawLooper.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040014#include "SkMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000015#include "SkRandom.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#define WIDTH 200
18#define HEIGHT 200
19
reed@google.com076f4c92012-07-31 14:32:38 +000020class DrawLooperGM : public skiagm::GM {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021public:
halcanary96fcdcc2015-08-27 07:41:13 -070022 DrawLooperGM() : fLooper(nullptr) {
caryclark65cdba62015-06-15 06:51:08 -070023 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
reed@google.com076f4c92012-07-31 14:32:38 +000024 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
reed@google.com076f4c92012-07-31 14:32:38 +000026protected:
mtklein36352bf2015-03-25 18:17:31 -070027 virtual SkISize onISize() override {
reed@google.com076f4c92012-07-31 14:32:38 +000028 return SkISize::Make(520, 160);
29 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000030
mtklein36352bf2015-03-25 18:17:31 -070031 SkString onShortName() override {
reed@google.com076f4c92012-07-31 14:32:38 +000032 return SkString("drawlooper");
33 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 void onDraw(SkCanvas* canvas) override {
reed@google.com076f4c92012-07-31 14:32:38 +000036 this->init();
37
38 SkPaint paint;
reed@google.com48f31bd2014-02-27 14:27:44 +000039 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070040 sk_tool_utils::set_portable_typeface(&paint);
reed@google.com076f4c92012-07-31 14:32:38 +000041 paint.setTextSize(SkIntToScalar(72));
42 paint.setLooper(fLooper);
43
Mike Reed3661bc92017-02-22 13:21:42 -050044 canvas->drawCircle(50, 50, 30, paint);
45 canvas->drawRect({ 150, 50, 200, 100 }, paint);
Cary Clark2a475ea2017-04-28 15:35:12 -040046 canvas->drawString("Looper", 230, 100, paint);
reed@google.com076f4c92012-07-31 14:32:38 +000047 }
48
49private:
reed7b380d02016-03-21 13:25:16 -070050 sk_sp<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051
reed@google.com076f4c92012-07-31 14:32:38 +000052 void init() {
53 if (fLooper) return;
54
mtkleindbfd7ab2016-09-01 11:24:54 -070055 constexpr struct {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 SkColor fColor;
57 SkPaint::Style fStyle;
58 SkScalar fWidth;
59 SkScalar fOffset;
robertphillips@google.comb7061172013-09-06 14:16:12 +000060 SkScalar fBlur;
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 } gParams[] = {
62 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
63 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
64 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
robertphillips@google.comb7061172013-09-06 14:16:12 +000065 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000068 SkLayerDrawLooper::Builder looperBuilder;
rmistry@google.comd6176b02012-08-23 18:14:13 +000069
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000070 SkLayerDrawLooper::LayerInfo info;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000071 info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -040072 info.fColorMode = SkBlendMode::kSrc;
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000074 for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000075 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000076 SkPaint* paint = looperBuilder.addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 paint->setColor(gParams[i].fColor);
78 paint->setStyle(gParams[i].fStyle);
79 paint->setStrokeWidth(gParams[i].fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 if (gParams[i].fBlur > 0) {
Mike Reed1be1f8d2018-03-14 13:01:17 -040081 paint->setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070082 SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur)));
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 }
84 }
reed7b380d02016-03-21 13:25:16 -070085 fLooper = looperBuilder.detach();
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 }
reed@google.com82065d62011-02-07 15:30:46 +000087
reed@google.com076f4c92012-07-31 14:32:38 +000088 typedef GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000089};
90
91//////////////////////////////////////////////////////////////////////////////
92
scroggo96f16e82015-12-10 13:31:59 -080093DEF_GM( return new DrawLooperGM; )