Home | History | Annotate | Download | only in samplecode
      1 
      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  */
      8 #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 
     16 #define WIDTH   200
     17 #define HEIGHT  200
     18 
     19 class LooperView : public SampleView {
     20 public:
     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         };
     37 
     38         fLooper = new SkLayerDrawLooper;
     39 
     40         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 
     45         for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
     46             info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
     47             SkPaint* paint = fLooper->addLayer(info);
     48             paint->setAntiAlias(true);
     49             paint->setColor(gParams[i].fColor);
     50             paint->setStyle(gParams[i].fStyle);
     51             paint->setStrokeWidth(gParams[i].fWidth);
     52             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         }
     58 
     59         this->setBGColor(0xFFDDDDDD);
     60     }
     61 
     62     virtual ~LooperView() {
     63         SkSafeUnref(fLooper);
     64     }
     65 
     66 protected:
     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     }
     75 
     76     virtual void onDrawContent(SkCanvas* canvas) {
     77         SkPaint  paint;
     78         paint.setTextSize(SkIntToScalar(72));
     79         paint.setLooper(fLooper);
     80 
     81         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     }
     90 
     91 private:
     92     typedef SampleView INHERITED;
     93 };
     94 
     95 //////////////////////////////////////////////////////////////////////////////
     96 
     97 static SkView* MyFactory() { return new LooperView; }
     98 static SkViewRegister reg(MyFactory);
     99 
    100