Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2013 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 "SkBlurDrawLooper.h"
     10 #include "SkBlurMask.h"
     11 #include "SkBlurMaskFilter.h"
     12 #include "SkColorFilter.h"
     13 #include "SkGradientShader.h"
     14 #include "SkMatrix.h"
     15 #include "SkTArray.h"
     16 
     17 namespace skiagm {
     18 
     19 class RectsGM : public GM {
     20 public:
     21     RectsGM() {
     22         this->setBGColor(0xFF000000);
     23         this->makePaints();
     24         this->makeMatrices();
     25         this->makeRects();
     26     }
     27 
     28 protected:
     29 
     30     SkString onShortName() override {
     31         return SkString("rects");
     32     }
     33 
     34     SkISize onISize() override {
     35         return SkISize::Make(1200, 900);
     36     }
     37 
     38     void makePaints() {
     39         {
     40             // no AA
     41             SkPaint p;
     42             p.setColor(SK_ColorWHITE);
     43             fPaints.push_back(p);
     44         }
     45 
     46         {
     47             // AA
     48             SkPaint p;
     49             p.setColor(SK_ColorWHITE);
     50             p.setAntiAlias(true);
     51             fPaints.push_back(p);
     52         }
     53 
     54         {
     55             // AA with translucent
     56             SkPaint p;
     57             p.setColor(SK_ColorWHITE);
     58             p.setAntiAlias(true);
     59             p.setAlpha(0x66);
     60             fPaints.push_back(p);
     61         }
     62 
     63         {
     64             // AA with mask filter
     65             SkPaint p;
     66             p.setColor(SK_ColorWHITE);
     67             p.setAntiAlias(true);
     68             p.setMaskFilter(SkBlurMaskFilter::Make(
     69                                    kNormal_SkBlurStyle,
     70                                    SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
     71                                    SkBlurMaskFilter::kHighQuality_BlurFlag));
     72             fPaints.push_back(p);
     73         }
     74 
     75         {
     76             // AA with radial shader
     77             SkPaint p;
     78             p.setColor(SK_ColorWHITE);
     79             p.setAntiAlias(true);
     80             SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30));
     81             SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
     82             SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
     83             p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos,
     84                                                      SK_ARRAY_COUNT(colors),
     85                                                      SkShader::kClamp_TileMode));
     86             fPaints.push_back(p);
     87         }
     88 
     89         {
     90             // AA with blur
     91             SkPaint p;
     92             p.setColor(SK_ColorWHITE);
     93             p.setAntiAlias(true);
     94             p.setLooper(SkBlurDrawLooper::Make(SK_ColorWHITE,
     95                                          SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
     96                                          SkIntToScalar(5), SkIntToScalar(10)));
     97             fPaints.push_back(p);
     98         }
     99 
    100         {
    101             // AA with stroke style
    102             SkPaint p;
    103             p.setColor(SK_ColorWHITE);
    104             p.setAntiAlias(true);
    105             p.setStyle(SkPaint::kStroke_Style);
    106             p.setStrokeWidth(SkIntToScalar(3));
    107             fPaints.push_back(p);
    108         }
    109 
    110         {
    111             // AA with bevel-stroke style
    112             SkPaint p;
    113             p.setColor(SK_ColorWHITE);
    114             p.setAntiAlias(true);
    115             p.setStyle(SkPaint::kStroke_Style);
    116             p.setStrokeJoin(SkPaint::kBevel_Join);
    117             p.setStrokeWidth(SkIntToScalar(3));
    118             fPaints.push_back(p);
    119         }
    120 
    121         {
    122             // AA with round-stroke style
    123             SkPaint p;
    124             p.setColor(SK_ColorWHITE);
    125             p.setAntiAlias(true);
    126             p.setStyle(SkPaint::kStroke_Style);
    127             p.setStrokeJoin(SkPaint::kRound_Join);
    128             p.setStrokeWidth(SkIntToScalar(3));
    129             fPaints.push_back(p);
    130         }
    131 
    132         {
    133             // AA with stroke style, width = 0
    134             SkPaint p;
    135             p.setColor(SK_ColorWHITE);
    136             p.setAntiAlias(true);
    137             p.setStyle(SkPaint::kStroke_Style);
    138             fPaints.push_back(p);
    139         }
    140 
    141         {
    142             // AA with stroke style, width wider than rect width and/or height
    143             SkPaint p;
    144             p.setColor(SK_ColorWHITE);
    145             p.setAntiAlias(true);
    146             p.setStyle(SkPaint::kStroke_Style);
    147             p.setStrokeWidth(SkIntToScalar(40));
    148             fPaints.push_back(p);
    149         }
    150 
    151         {
    152             // AA with stroke and fill style
    153             SkPaint p;
    154             p.setColor(SK_ColorWHITE);
    155             p.setAntiAlias(true);
    156             p.setStyle(SkPaint::kStrokeAndFill_Style);
    157             p.setStrokeWidth(SkIntToScalar(2));
    158             fPaints.push_back(p);
    159         }
    160     }
    161 
    162     void makeMatrices() {
    163         {
    164             // 1x1.5 scale
    165             SkMatrix m;
    166             m.setScale(1, 1.5f);
    167             fMatrices.push_back(m);
    168         }
    169 
    170         {
    171             // 1.5x1.5 scale
    172             SkMatrix m;
    173             m.setScale(1.5f, 1.5f);
    174             fMatrices.push_back(m);
    175         }
    176 
    177         {
    178             // 1x1.5 skew
    179             SkMatrix m;
    180             m.setSkew(1, 1.5f);
    181             fMatrices.push_back(m);
    182         }
    183 
    184         {
    185             // 1.5x1.5 skew
    186             SkMatrix m;
    187             m.setSkew(1.5f, 1.5f);
    188             fMatrices.push_back(m);
    189         }
    190 
    191         {
    192             // 30 degree rotation
    193             SkMatrix m;
    194             m.setRotate(SkIntToScalar(30));
    195             fMatrices.push_back(m);
    196         }
    197 
    198         {
    199             // 90 degree rotation
    200             SkMatrix m;
    201             m.setRotate(SkIntToScalar(90));
    202             fMatrices.push_back(m);
    203         }
    204     }
    205 
    206     void makeRects() {
    207         {
    208             // small square
    209             SkRect r = SkRect::MakeLTRB(0, 0, 30, 30);
    210             fRects.push_back(r);
    211         }
    212 
    213         {
    214             // thin vertical
    215             SkRect r = SkRect::MakeLTRB(0, 0, 2, 40);
    216             fRects.push_back(r);
    217         }
    218 
    219         {
    220             // thin horizontal
    221             SkRect r = SkRect::MakeLTRB(0, 0, 40, 2);
    222             fRects.push_back(r);
    223         }
    224 
    225         {
    226             // very thin
    227             SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10);
    228             fRects.push_back(r);
    229         }
    230 
    231         {
    232             // zaftig
    233             SkRect r = SkRect::MakeLTRB(0, 0, 60, 60);
    234             fRects.push_back(r);
    235         }
    236     }
    237 
    238     // position the current test on the canvas
    239     static void position(SkCanvas* canvas, int testCount) {
    240         canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4,
    241                           SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4);
    242     }
    243 
    244     void onDraw(SkCanvas* canvas) override {
    245         canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
    246 
    247         int testCount = 0;
    248 
    249         for (int i = 0; i < fPaints.count(); ++i) {
    250             for (int j = 0; j < fRects.count(); ++j, ++testCount) {
    251                 canvas->save();
    252                 this->position(canvas, testCount);
    253                 canvas->drawRect(fRects[j], fPaints[i]);
    254                 canvas->restore();
    255             }
    256         }
    257 
    258         SkPaint paint;
    259         paint.setColor(SK_ColorWHITE);
    260         paint.setAntiAlias(true);
    261 
    262         for (int i = 0; i < fMatrices.count(); ++i) {
    263             for (int j = 0; j < fRects.count(); ++j, ++testCount) {
    264                 canvas->save();
    265                 this->position(canvas, testCount);
    266                 canvas->concat(fMatrices[i]);
    267                 canvas->drawRect(fRects[j], paint);
    268                 canvas->restore();
    269             }
    270         }
    271     }
    272 
    273 private:
    274     SkTArray<SkPaint>  fPaints;
    275     SkTArray<SkMatrix> fMatrices;
    276     SkTArray<SkRect>   fRects;
    277 
    278     typedef GM INHERITED;
    279 };
    280 
    281 //////////////////////////////////////////////////////////////////////////////
    282 
    283 static GM* MyFactory(void*) { return new RectsGM; }
    284 static GMRegistry reg(MyFactory);
    285 
    286 }
    287