blob: 74e7e91fe6b2a377fca763bad42127cc91227fd9 [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:
rmistry@google.comd6176b02012-08-23 18:14:13 +000021 DrawLooperGM() : fLooper(NULL) {
caryclarkbe7f7682015-06-15 05:09:59 -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 +000025 virtual ~DrawLooperGM() {
26 SkSafeUnref(fLooper);
27 }
28
29protected:
mtklein36352bf2015-03-25 18:17:31 -070030 virtual SkISize onISize() override {
reed@google.com076f4c92012-07-31 14:32:38 +000031 return SkISize::Make(520, 160);
32 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000033
mtklein36352bf2015-03-25 18:17:31 -070034 SkString onShortName() override {
reed@google.com076f4c92012-07-31 14:32:38 +000035 return SkString("drawlooper");
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 void onDraw(SkCanvas* canvas) override {
reed@google.com076f4c92012-07-31 14:32:38 +000039 this->init();
40
41 SkPaint paint;
reed@google.com48f31bd2014-02-27 14:27:44 +000042 paint.setAntiAlias(true);
caryclark82fecd42015-06-12 13:41:02 -070043 sk_tool_utils::set_portable_typeface_always(&paint);
reed@google.com076f4c92012-07-31 14:32:38 +000044 paint.setTextSize(SkIntToScalar(72));
45 paint.setLooper(fLooper);
46
47 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
48 SkIntToScalar(30), paint);
49
50 canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
51 SkIntToScalar(200), SkIntToScalar(100), paint);
52
53 canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
54 paint);
55 }
56
57private:
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SkLayerDrawLooper* fLooper;
59
reed@google.com076f4c92012-07-31 14:32:38 +000060 void init() {
61 if (fLooper) return;
62
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 static const struct {
64 SkColor fColor;
65 SkPaint::Style fStyle;
66 SkScalar fWidth;
67 SkScalar fOffset;
robertphillips@google.comb7061172013-09-06 14:16:12 +000068 SkScalar fBlur;
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 } gParams[] = {
70 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
71 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
72 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
robertphillips@google.comb7061172013-09-06 14:16:12 +000073 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000075
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000076 SkLayerDrawLooper::Builder looperBuilder;
rmistry@google.comd6176b02012-08-23 18:14:13 +000077
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000078 SkLayerDrawLooper::LayerInfo info;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000079 info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
80 info.fColorMode = SkXfermode::kSrc_Mode;
rmistry@google.comd6176b02012-08-23 18:14:13 +000081
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000082 for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000083 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000084 SkPaint* paint = looperBuilder.addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 paint->setColor(gParams[i].fColor);
86 paint->setStyle(gParams[i].fStyle);
87 paint->setStrokeWidth(gParams[i].fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 if (gParams[i].fBlur > 0) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000089 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000090 SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur));
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 paint->setMaskFilter(mf)->unref();
92 }
93 }
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000094 fLooper = looperBuilder.detachLooper();
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 }
reed@google.com82065d62011-02-07 15:30:46 +000096
reed@google.com076f4c92012-07-31 14:32:38 +000097 typedef GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098};
99
100//////////////////////////////////////////////////////////////////////////////
101
reed@google.com076f4c92012-07-31 14:32:38 +0000102static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; }
103static skiagm::GMRegistry reg(MyFactory);