blob: a04955b4a80a8202d009529301ad500c93106504 [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:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000030 virtual uint32_t onGetFlags() const SK_OVERRIDE {
31 return kSkipTiled_Flag;
32 }
33
reed@google.com076f4c92012-07-31 14:32:38 +000034 virtual SkISize onISize() {
35 return SkISize::Make(520, 160);
36 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000037
reed@google.com076f4c92012-07-31 14:32:38 +000038 virtual SkString onShortName() SK_OVERRIDE {
39 return SkString("drawlooper");
40 }
41
42 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
43 this->init();
44
45 SkPaint paint;
reed@google.com48f31bd2014-02-27 14:27:44 +000046 paint.setAntiAlias(true);
reed@google.com076f4c92012-07-31 14:32:38 +000047 paint.setTextSize(SkIntToScalar(72));
48 paint.setLooper(fLooper);
49
50 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
51 SkIntToScalar(30), paint);
52
53 canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
54 SkIntToScalar(200), SkIntToScalar(100), paint);
55
56 canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
57 paint);
58 }
59
60private:
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 SkLayerDrawLooper* fLooper;
62
reed@google.com076f4c92012-07-31 14:32:38 +000063 void init() {
64 if (fLooper) return;
65
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 static const struct {
67 SkColor fColor;
68 SkPaint::Style fStyle;
69 SkScalar fWidth;
70 SkScalar fOffset;
robertphillips@google.comb7061172013-09-06 14:16:12 +000071 SkScalar fBlur;
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 } gParams[] = {
73 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
74 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
75 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
robertphillips@google.comb7061172013-09-06 14:16:12 +000076 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000078
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000079 SkLayerDrawLooper::Builder looperBuilder;
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000081 SkLayerDrawLooper::LayerInfo info;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000082 info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
83 info.fColorMode = SkXfermode::kSrc_Mode;
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000085 for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000086 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000087 SkPaint* paint = looperBuilder.addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 paint->setColor(gParams[i].fColor);
89 paint->setStyle(gParams[i].fStyle);
90 paint->setStrokeWidth(gParams[i].fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 if (gParams[i].fBlur > 0) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000092 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000093 SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur));
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 paint->setMaskFilter(mf)->unref();
95 }
96 }
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000097 fLooper = looperBuilder.detachLooper();
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 }
reed@google.com82065d62011-02-07 15:30:46 +000099
reed@google.com076f4c92012-07-31 14:32:38 +0000100 typedef GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101};
102
103//////////////////////////////////////////////////////////////////////////////
104
reed@google.com076f4c92012-07-31 14:32:38 +0000105static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; }
106static skiagm::GMRegistry reg(MyFactory);