blob: 678d2ad35f2a8a226053af1d709b0a87829d893b [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) {
reed@google.com076f4c92012-07-31 14:32:38 +000022 this->setBGColor(0xFFDDDDDD);
23 }
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:
30 virtual SkISize onISize() {
31 return SkISize::Make(520, 160);
32 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000033
reed@google.com076f4c92012-07-31 14:32:38 +000034 virtual SkString onShortName() SK_OVERRIDE {
35 return SkString("drawlooper");
36 }
37
38 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
39 this->init();
40
41 SkPaint paint;
reed@google.com48f31bd2014-02-27 14:27:44 +000042 paint.setAntiAlias(true);
reed@google.com076f4c92012-07-31 14:32:38 +000043 paint.setTextSize(SkIntToScalar(72));
44 paint.setLooper(fLooper);
45
46 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
47 SkIntToScalar(30), paint);
48
49 canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
50 SkIntToScalar(200), SkIntToScalar(100), paint);
51
52 canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
53 paint);
54 }
55
56private:
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 SkLayerDrawLooper* fLooper;
58
reed@google.com076f4c92012-07-31 14:32:38 +000059 void init() {
60 if (fLooper) return;
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 static const struct {
63 SkColor fColor;
64 SkPaint::Style fStyle;
65 SkScalar fWidth;
66 SkScalar fOffset;
robertphillips@google.comb7061172013-09-06 14:16:12 +000067 SkScalar fBlur;
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 } gParams[] = {
69 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
70 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
71 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
robertphillips@google.comb7061172013-09-06 14:16:12 +000072 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 fLooper = new SkLayerDrawLooper;
rmistry@google.comd6176b02012-08-23 18:14:13 +000076
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000077 SkLayerDrawLooper::LayerInfo info;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000078 info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
79 info.fColorMode = SkXfermode::kSrc_Mode;
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000081 for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000082 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
83 SkPaint* paint = fLooper->addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 paint->setColor(gParams[i].fColor);
85 paint->setStyle(gParams[i].fStyle);
86 paint->setStrokeWidth(gParams[i].fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 if (gParams[i].fBlur > 0) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000088 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
89 SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur));
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 paint->setMaskFilter(mf)->unref();
91 }
92 }
93 }
reed@google.com82065d62011-02-07 15:30:46 +000094
reed@google.com076f4c92012-07-31 14:32:38 +000095 typedef GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000096};
97
98//////////////////////////////////////////////////////////////////////////////
99
reed@google.com076f4c92012-07-31 14:32:38 +0000100static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; }
101static skiagm::GMRegistry reg(MyFactory);