blob: 4cc6e8e3693e021572775807573448ea89e74d6a [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;
42 paint.setTextSize(SkIntToScalar(72));
43 paint.setLooper(fLooper);
44
45 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
46 SkIntToScalar(30), paint);
47
48 canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
49 SkIntToScalar(200), SkIntToScalar(100), paint);
50
51 canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
52 paint);
53 }
54
55private:
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 SkLayerDrawLooper* fLooper;
57
reed@google.com076f4c92012-07-31 14:32:38 +000058 void init() {
59 if (fLooper) return;
60
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 static const struct {
62 SkColor fColor;
63 SkPaint::Style fStyle;
64 SkScalar fWidth;
65 SkScalar fOffset;
robertphillips@google.comb7061172013-09-06 14:16:12 +000066 SkScalar fBlur;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 } gParams[] = {
68 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
69 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
70 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
robertphillips@google.comb7061172013-09-06 14:16:12 +000071 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 fLooper = new SkLayerDrawLooper;
rmistry@google.comd6176b02012-08-23 18:14:13 +000075
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000076 SkLayerDrawLooper::LayerInfo info;
77 info.fFlagsMask = SkPaint::kAntiAlias_Flag;
78 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->setAntiAlias(true);
85 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) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000089 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
90 SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur));
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 paint->setMaskFilter(mf)->unref();
92 }
93 }
94 }
reed@google.com82065d62011-02-07 15:30:46 +000095
reed@google.com076f4c92012-07-31 14:32:38 +000096 typedef GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097};
98
99//////////////////////////////////////////////////////////////////////////////
100
reed@google.com076f4c92012-07-31 14:32:38 +0000101static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; }
102static skiagm::GMRegistry reg(MyFactory);