blob: df8256ff11103bb31316cc2dd2cf181bb38b78b7 [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"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.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"
robertphillips@google.comb7061172013-09-06 14:16:12 +000014#include "SkRandom.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#define WIDTH 200
17#define HEIGHT 200
18
reed@google.com076f4c92012-07-31 14:32:38 +000019class DrawLooperGM : public skiagm::GM {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020public:
halcanary96fcdcc2015-08-27 07:41:13 -070021 DrawLooperGM() : fLooper(nullptr) {
caryclark65cdba62015-06-15 06:51:08 -070022 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
reed@google.com076f4c92012-07-31 14:32:38 +000023 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
reed@google.com076f4c92012-07-31 14:32:38 +000025protected:
mtklein36352bf2015-03-25 18:17:31 -070026 virtual SkISize onISize() override {
reed@google.com076f4c92012-07-31 14:32:38 +000027 return SkISize::Make(520, 160);
28 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000029
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
reed@google.com076f4c92012-07-31 14:32:38 +000031 return SkString("drawlooper");
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 void onDraw(SkCanvas* canvas) override {
reed@google.com076f4c92012-07-31 14:32:38 +000035 this->init();
36
37 SkPaint paint;
reed@google.com48f31bd2014-02-27 14:27:44 +000038 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070039 sk_tool_utils::set_portable_typeface(&paint);
reed@google.com076f4c92012-07-31 14:32:38 +000040 paint.setTextSize(SkIntToScalar(72));
41 paint.setLooper(fLooper);
42
43 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
44 SkIntToScalar(30), paint);
45
46 canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
47 SkIntToScalar(200), SkIntToScalar(100), paint);
48
49 canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
50 paint);
51 }
52
53private:
reed7b380d02016-03-21 13:25:16 -070054 sk_sp<SkDrawLooper> fLooper;
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
reed@google.com076f4c92012-07-31 14:32:38 +000056 void init() {
57 if (fLooper) return;
58
mtkleindbfd7ab2016-09-01 11:24:54 -070059 constexpr struct {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 SkColor fColor;
61 SkPaint::Style fStyle;
62 SkScalar fWidth;
63 SkScalar fOffset;
robertphillips@google.comb7061172013-09-06 14:16:12 +000064 SkScalar fBlur;
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 } gParams[] = {
66 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
67 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
68 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
robertphillips@google.comb7061172013-09-06 14:16:12 +000069 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000071
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000072 SkLayerDrawLooper::Builder looperBuilder;
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000074 SkLayerDrawLooper::LayerInfo info;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000075 info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
76 info.fColorMode = SkXfermode::kSrc_Mode;
rmistry@google.comd6176b02012-08-23 18:14:13 +000077
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000078 for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000079 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000080 SkPaint* paint = looperBuilder.addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 paint->setColor(gParams[i].fColor);
82 paint->setStyle(gParams[i].fStyle);
83 paint->setStrokeWidth(gParams[i].fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 if (gParams[i].fBlur > 0) {
reedefdfd512016-04-04 10:02:58 -070085 paint->setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
86 SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur)));
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 }
88 }
reed7b380d02016-03-21 13:25:16 -070089 fLooper = looperBuilder.detach();
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 }
reed@google.com82065d62011-02-07 15:30:46 +000091
reed@google.com076f4c92012-07-31 14:32:38 +000092 typedef GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093};
94
95//////////////////////////////////////////////////////////////////////////////
96
scroggo96f16e82015-12-10 13:31:59 -080097DEF_GM( return new DrawLooperGM; )