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