Home | History | Annotate | Download | only in gm
      1 
      2 /*
      3  * Copyright 2012 Intel 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 "gm.h"
      9 #include "SkTArray.h"
     10 #include "SkRandom.h"
     11 #include "SkMatrix.h"
     12 #include "SkBlurMaskFilter.h"
     13 #include "SkGradientShader.h"
     14 #include "SkBlurDrawLooper.h"
     15 
     16 namespace skiagm {
     17 
     18 class CircleGM : public GM {
     19 public:
     20     CircleGM() {
     21         this->setBGColor(0xFF000000);
     22         this->makePaints();
     23         this->makeMatrices();
     24     }
     25 
     26 protected:
     27     virtual SkString onShortName() SK_OVERRIDE {
     28         return SkString("circles");
     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         fPaints.push_back(p);
     40         }
     41 
     42         {
     43         // AA
     44         SkPaint p;
     45         p.setAntiAlias(true);
     46         fPaints.push_back(p);
     47         }
     48 
     49         {
     50         // AA with mask filter
     51         SkPaint p;
     52         p.setAntiAlias(true);
     53         SkMaskFilter* mf = SkBlurMaskFilter::Create(SkIntToScalar(5),
     54                                SkBlurMaskFilter::kNormal_BlurStyle,
     55                                SkBlurMaskFilter::kHighQuality_BlurFlag);
     56         p.setMaskFilter(mf)->unref();
     57         fPaints.push_back(p);
     58         }
     59 
     60         {
     61         // AA with radial shader
     62         SkPaint p;
     63         p.setAntiAlias(true);
     64         SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
     65         SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
     66         SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
     67         SkShader* s = SkGradientShader::CreateRadial(center,
     68                                                      SkIntToScalar(20),
     69                                                      colors,
     70                                                      pos,
     71                                                      SK_ARRAY_COUNT(colors),
     72                                                      SkShader::kClamp_TileMode);
     73         p.setShader(s)->unref();
     74         fPaints.push_back(p);
     75         }
     76 
     77         {
     78         // AA with blur
     79         SkPaint p;
     80         p.setAntiAlias(true);
     81         SkBlurDrawLooper* shadowLooper =
     82             new SkBlurDrawLooper (SkIntToScalar(10), SkIntToScalar(5),
     83                                   SkIntToScalar(10), 0xFF0000FF,
     84                                   SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
     85                                   SkBlurDrawLooper::kOverrideColor_BlurFlag |
     86                                   SkBlurDrawLooper::kHighQuality_BlurFlag );
     87         SkAutoUnref aurL0(shadowLooper);
     88         p.setLooper(shadowLooper);
     89         fPaints.push_back(p);
     90         }
     91 
     92         {
     93         // AA with stroke style
     94         SkPaint p;
     95         p.setAntiAlias(true);
     96         p.setStyle(SkPaint::kStroke_Style);
     97         p.setStrokeWidth(SkIntToScalar(3));
     98         fPaints.push_back(p);
     99         }
    100 
    101         {
    102         // AA with stroke style, width = 0
    103         SkPaint p;
    104         p.setAntiAlias(true);
    105         p.setStyle(SkPaint::kStroke_Style);
    106         fPaints.push_back(p);
    107         }
    108 
    109         {
    110         // AA with stroke and fill style
    111         SkPaint p;
    112         p.setAntiAlias(true);
    113         p.setStyle(SkPaint::kStrokeAndFill_Style);
    114         p.setStrokeWidth(SkIntToScalar(2));
    115         fPaints.push_back(p);
    116         }
    117     }
    118 
    119     void makeMatrices() {
    120         {
    121         SkMatrix m;
    122         m.setScale(SkIntToScalar(2), SkIntToScalar(3));
    123         fMatrices.push_back(m);
    124         }
    125 
    126         {
    127         SkMatrix m;
    128         m.setScale(SkIntToScalar(2), SkIntToScalar(2));
    129         fMatrices.push_back(m);
    130         }
    131 
    132         {
    133         SkMatrix m;
    134         m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
    135         fMatrices.push_back(m);
    136         }
    137 
    138         {
    139         SkMatrix m;
    140         m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
    141         fMatrices.push_back(m);
    142         }
    143 
    144         {
    145         SkMatrix m;
    146         m.setRotate(SkIntToScalar(30));
    147         fMatrices.push_back(m);
    148         }
    149     }
    150 
    151     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
    152         SkRandom rand;
    153         canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
    154 
    155         int i;
    156         for (i = 0; i < fPaints.count(); ++i) {
    157             canvas->save();
    158             // position the path, and make it at off-integer coords.
    159             canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
    160                               SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
    161             SkColor color = rand.nextU();
    162             color |= 0xff000000;
    163             fPaints[i].setColor(color);
    164 
    165             canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
    166                                SkIntToScalar(20),
    167                                fPaints[i]);
    168             canvas->restore();
    169         }
    170 
    171         for (int j = 0; j < fMatrices.count(); ++j, ++i) {
    172             canvas->save();
    173 
    174             canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
    175                               SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
    176 
    177             canvas->concat(fMatrices[j]);
    178 
    179             SkPaint paint;
    180             paint.setAntiAlias(true);
    181 
    182             SkColor color = rand.nextU();
    183             color |= 0xff000000;
    184             paint.setColor(color);
    185 
    186             canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
    187                                SkIntToScalar(20),
    188                                paint);
    189 
    190             canvas->restore();
    191         }
    192     }
    193 
    194 private:
    195     typedef GM INHERITED;
    196     SkTArray<SkPaint> fPaints;
    197     SkTArray<SkMatrix> fMatrices;
    198 };
    199 
    200 //////////////////////////////////////////////////////////////////////////////
    201 
    202 static GM* MyFactory(void*) { return new CircleGM; }
    203 static GMRegistry reg(MyFactory);
    204 
    205 }
    206