blob: 58c691c311bbba9641710c1716119395a582907e [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkCanvas.h"
10#include "SkGraphics.h"
11#include "SkRandom.h"
12#include "SkLayerDrawLooper.h"
13#include "SkBlurMaskFilter.h"
14
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#define WIDTH 200
16#define HEIGHT 200
17
reed@google.com076f4c92012-07-31 14:32:38 +000018class DrawLooperGM : public skiagm::GM {
reed@android.com8a1c16f2008-12-17 15:59:43 +000019public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000020 DrawLooperGM() : fLooper(NULL) {
reed@google.com076f4c92012-07-31 14:32:38 +000021 this->setBGColor(0xFFDDDDDD);
22 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
reed@google.com076f4c92012-07-31 14:32:38 +000024 virtual ~DrawLooperGM() {
25 SkSafeUnref(fLooper);
26 }
27
28protected:
29 virtual SkISize onISize() {
30 return SkISize::Make(520, 160);
31 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000032
reed@google.com076f4c92012-07-31 14:32:38 +000033 virtual SkString onShortName() SK_OVERRIDE {
34 return SkString("drawlooper");
35 }
36
37 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
38 this->init();
39
40 SkPaint paint;
41 paint.setTextSize(SkIntToScalar(72));
42 paint.setLooper(fLooper);
43
44 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
45 SkIntToScalar(30), paint);
46
47 canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
48 SkIntToScalar(200), SkIntToScalar(100), paint);
49
50 canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
51 paint);
52 }
53
54private:
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 SkLayerDrawLooper* fLooper;
56
reed@google.com076f4c92012-07-31 14:32:38 +000057 void init() {
58 if (fLooper) return;
59
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 static const struct {
61 SkColor fColor;
62 SkPaint::Style fStyle;
63 SkScalar fWidth;
64 SkScalar fOffset;
65 int fBlur;
66 } gParams[] = {
67 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
68 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
69 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
70 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), 3 }
71 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 fLooper = new SkLayerDrawLooper;
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000075 SkLayerDrawLooper::LayerInfo info;
76 info.fFlagsMask = SkPaint::kAntiAlias_Flag;
77 info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
78 info.fColorMode = SkXfermode::kSrc_Mode;
rmistry@google.comd6176b02012-08-23 18:14:13 +000079
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000080 for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000081 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
82 SkPaint* paint = fLooper->addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 paint->setAntiAlias(true);
84 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) {
88 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkIntToScalar(gParams[i].fBlur),
89 SkBlurMaskFilter::kNormal_BlurStyle);
90 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);