Home | History | Annotate | Download | only in gm
      1 /*
      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  */
      7 
      8 #include "gm.h"
      9 #include "SkBlurMask.h"
     10 #include "SkBlurMaskFilter.h"
     11 #include "SkCanvas.h"
     12 #include "SkGraphics.h"
     13 #include "SkLayerDrawLooper.h"
     14 #include "SkRandom.h"
     15 
     16 #define WIDTH   200
     17 #define HEIGHT  200
     18 
     19 class DrawLooperGM : public skiagm::GM {
     20 public:
     21     DrawLooperGM() : fLooper(NULL) {
     22         this->setBGColor(0xFFDDDDDD);
     23     }
     24 
     25     virtual ~DrawLooperGM() {
     26         SkSafeUnref(fLooper);
     27     }
     28 
     29 protected:
     30     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     31         return kSkipTiled_Flag;
     32     }
     33 
     34     virtual SkISize onISize() {
     35         return SkISize::Make(520, 160);
     36     }
     37 
     38     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;
     46         paint.setAntiAlias(true);
     47         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 
     60 private:
     61     SkLayerDrawLooper*   fLooper;
     62 
     63     void init() {
     64         if (fLooper) return;
     65 
     66         static const struct {
     67             SkColor         fColor;
     68             SkPaint::Style  fStyle;
     69             SkScalar        fWidth;
     70             SkScalar        fOffset;
     71             SkScalar        fBlur;
     72         } 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 },
     76             { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
     77         };
     78 
     79         SkLayerDrawLooper::Builder looperBuilder;
     80 
     81         SkLayerDrawLooper::LayerInfo info;
     82         info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
     83         info.fColorMode = SkXfermode::kSrc_Mode;
     84 
     85         for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
     86             info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
     87             SkPaint* paint = looperBuilder.addLayer(info);
     88             paint->setColor(gParams[i].fColor);
     89             paint->setStyle(gParams[i].fStyle);
     90             paint->setStrokeWidth(gParams[i].fWidth);
     91             if (gParams[i].fBlur > 0) {
     92                 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
     93                                          SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur));
     94                 paint->setMaskFilter(mf)->unref();
     95             }
     96         }
     97         fLooper = looperBuilder.detachLooper();
     98     }
     99 
    100     typedef GM INHERITED;
    101 };
    102 
    103 //////////////////////////////////////////////////////////////////////////////
    104 
    105 static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; }
    106 static skiagm::GMRegistry reg(MyFactory);
    107