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 "sk_tool_utils.h"
     10 #include "SkBlurMask.h"
     11 #include "SkBlurMaskFilter.h"
     12 #include "SkCanvas.h"
     13 #include "SkGraphics.h"
     14 #include "SkLayerDrawLooper.h"
     15 #include "SkRandom.h"
     16 
     17 #define WIDTH   200
     18 #define HEIGHT  200
     19 
     20 class DrawLooperGM : public skiagm::GM {
     21 public:
     22     DrawLooperGM() : fLooper(nullptr) {
     23         this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
     24     }
     25 
     26 protected:
     27     virtual SkISize onISize() override {
     28         return SkISize::Make(520, 160);
     29     }
     30 
     31     SkString onShortName() override {
     32         return SkString("drawlooper");
     33     }
     34 
     35     void onDraw(SkCanvas* canvas) override {
     36         this->init();
     37 
     38         SkPaint  paint;
     39         paint.setAntiAlias(true);
     40         sk_tool_utils::set_portable_typeface(&paint);
     41         paint.setTextSize(SkIntToScalar(72));
     42         paint.setLooper(fLooper);
     43 
     44         canvas->drawCircle(50, 50, 30, paint);
     45         canvas->drawRect({ 150, 50, 200, 100 }, paint);
     46         canvas->drawString("Looper", 230, 100, paint);
     47     }
     48 
     49 private:
     50     sk_sp<SkDrawLooper> fLooper;
     51 
     52     void init() {
     53         if (fLooper) return;
     54 
     55         constexpr struct {
     56             SkColor         fColor;
     57             SkPaint::Style  fStyle;
     58             SkScalar        fWidth;
     59             SkScalar        fOffset;
     60             SkScalar        fBlur;
     61         } gParams[] = {
     62             { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
     63             { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
     64             { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
     65             { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
     66         };
     67 
     68         SkLayerDrawLooper::Builder looperBuilder;
     69 
     70         SkLayerDrawLooper::LayerInfo info;
     71         info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
     72         info.fColorMode = SkBlendMode::kSrc;
     73 
     74         for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
     75             info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
     76             SkPaint* paint = looperBuilder.addLayer(info);
     77             paint->setColor(gParams[i].fColor);
     78             paint->setStyle(gParams[i].fStyle);
     79             paint->setStrokeWidth(gParams[i].fWidth);
     80             if (gParams[i].fBlur > 0) {
     81                 paint->setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
     82                                          SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur)));
     83             }
     84         }
     85         fLooper = looperBuilder.detach();
     86     }
     87 
     88     typedef GM INHERITED;
     89 };
     90 
     91 //////////////////////////////////////////////////////////////////////////////
     92 
     93 DEF_GM( return new DrawLooperGM; )
     94