blob: 7e317d764e93dde1c58da09a9726e349d9eda1ef [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGraphics.h"
12#include "SkRandom.h"
13#include "SkLayerDrawLooper.h"
14#include "SkBlurMaskFilter.h"
15
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#define WIDTH 200
17#define HEIGHT 200
18
reed@google.com961ddb02011-05-05 14:03:48 +000019class LooperView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020public:
21
22 SkLayerDrawLooper* fLooper;
23
24 LooperView() {
25 static const struct {
26 SkColor fColor;
27 SkPaint::Style fStyle;
28 SkScalar fWidth;
29 SkScalar fOffset;
30 int fBlur;
31 } gParams[] = {
32 { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
33 { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
34 { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
35 { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), 3 }
36 };
reed@google.com82065d62011-02-07 15:30:46 +000037
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 fLooper = new SkLayerDrawLooper;
reed@google.com82065d62011-02-07 15:30:46 +000039
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000040 SkLayerDrawLooper::LayerInfo info;
41 info.fFlagsMask = SkPaint::kAntiAlias_Flag;
42 info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
43 info.fColorMode = SkXfermode::kSrc_Mode;
44
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000045 for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000046 info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
47 SkPaint* paint = fLooper->addLayer(info);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 paint->setAntiAlias(true);
49 paint->setColor(gParams[i].fColor);
50 paint->setStyle(gParams[i].fStyle);
51 paint->setStrokeWidth(gParams[i].fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 if (gParams[i].fBlur > 0) {
53 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkIntToScalar(gParams[i].fBlur),
54 SkBlurMaskFilter::kNormal_BlurStyle);
55 paint->setMaskFilter(mf)->unref();
56 }
57 }
reed@google.com961ddb02011-05-05 14:03:48 +000058
59 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 }
reed@google.com82065d62011-02-07 15:30:46 +000061
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 virtual ~LooperView() {
reed@google.com82065d62011-02-07 15:30:46 +000063 SkSafeUnref(fLooper);
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 }
reed@google.com82065d62011-02-07 15:30:46 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066protected:
67 // overrides from SkEventSink
68 virtual bool onQuery(SkEvent* evt) {
69 if (SampleCode::TitleQ(*evt)) {
70 SampleCode::TitleR(evt, "DrawLooper");
71 return true;
72 }
73 return this->INHERITED::onQuery(evt);
74 }
reed@google.com82065d62011-02-07 15:30:46 +000075
reed@google.com961ddb02011-05-05 14:03:48 +000076 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 SkPaint paint;
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000078 paint.setTextSize(SkIntToScalar(72));
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 paint.setLooper(fLooper);
reed@google.com82065d62011-02-07 15:30:46 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
82 SkIntToScalar(30), paint);
83
84 canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
85 SkIntToScalar(200), SkIntToScalar(100), paint);
86
87 canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
88 paint);
89 }
reed@google.com82065d62011-02-07 15:30:46 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091private:
reed@google.com961ddb02011-05-05 14:03:48 +000092 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093};
94
95//////////////////////////////////////////////////////////////////////////////
96
97static SkView* MyFactory() { return new LooperView; }
98static SkViewRegister reg(MyFactory);
99