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 "SkFlattenableBuffers.h"
     10 #include "SkLayerRasterizer.h"
     11 #include "SkBlurMaskFilter.h"
     12 
     13 static void r0(SkLayerRasterizer* rast, SkPaint& p) {
     14     p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
     15                                              SkBlurMaskFilter::kNormal_BlurStyle))->unref();
     16     rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
     17 
     18     p.setMaskFilter(NULL);
     19     p.setStyle(SkPaint::kStroke_Style);
     20     p.setStrokeWidth(SK_Scalar1);
     21     rast->addLayer(p);
     22 
     23     p.setAlpha(0x11);
     24     p.setStyle(SkPaint::kFill_Style);
     25     p.setXfermodeMode(SkXfermode::kSrc_Mode);
     26     rast->addLayer(p);
     27 }
     28 
     29 static void r1(SkLayerRasterizer* rast, SkPaint& p) {
     30     rast->addLayer(p);
     31 
     32     p.setAlpha(0x40);
     33     p.setXfermodeMode(SkXfermode::kSrc_Mode);
     34     p.setStyle(SkPaint::kStroke_Style);
     35     p.setStrokeWidth(SK_Scalar1*2);
     36     rast->addLayer(p);
     37 }
     38 
     39 static void r2(SkLayerRasterizer* rast, SkPaint& p) {
     40     p.setStyle(SkPaint::kStrokeAndFill_Style);
     41     p.setStrokeWidth(SK_Scalar1*4);
     42     rast->addLayer(p);
     43 
     44     p.setStyle(SkPaint::kStroke_Style);
     45     p.setStrokeWidth(SK_Scalar1*3/2);
     46     p.setXfermodeMode(SkXfermode::kClear_Mode);
     47     rast->addLayer(p);
     48 }
     49 
     50 static void r3(SkLayerRasterizer* rast, SkPaint& p) {
     51     p.setStyle(SkPaint::kStroke_Style);
     52     p.setStrokeWidth(SK_Scalar1*3);
     53     rast->addLayer(p);
     54 
     55     p.setAlpha(0x20);
     56     p.setStyle(SkPaint::kFill_Style);
     57     p.setXfermodeMode(SkXfermode::kSrc_Mode);
     58     rast->addLayer(p);
     59 }
     60 
     61 static void r4(SkLayerRasterizer* rast, SkPaint& p) {
     62     p.setAlpha(0x60);
     63     rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
     64 
     65     p.setAlpha(0xFF);
     66     p.setXfermodeMode(SkXfermode::kClear_Mode);
     67     rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
     68 
     69     p.setXfermode(NULL);
     70     rast->addLayer(p);
     71 }
     72 
     73 #include "SkDiscretePathEffect.h"
     74 
     75 static void r5(SkLayerRasterizer* rast, SkPaint& p) {
     76     rast->addLayer(p);
     77 
     78     p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
     79     p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
     80     rast->addLayer(p);
     81 }
     82 
     83 static void r6(SkLayerRasterizer* rast, SkPaint& p) {
     84     rast->addLayer(p);
     85 
     86     p.setAntiAlias(false);
     87     SkLayerRasterizer* rast2 = new SkLayerRasterizer;
     88     r5(rast2, p);
     89     p.setRasterizer(rast2)->unref();
     90     p.setXfermodeMode(SkXfermode::kClear_Mode);
     91     rast->addLayer(p);
     92 }
     93 
     94 #include "Sk2DPathEffect.h"
     95 
     96 static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
     97     SkPath path;
     98     path.addCircle(0, 0, radius);
     99     return new SkPath2DPathEffect(matrix, path);
    100 }
    101 
    102 static void r7(SkLayerRasterizer* rast, SkPaint& p) {
    103     SkMatrix    lattice;
    104     lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
    105     lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
    106     p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
    107     rast->addLayer(p);
    108 }
    109 
    110 static void r8(SkLayerRasterizer* rast, SkPaint& p) {
    111     rast->addLayer(p);
    112 
    113     SkMatrix    lattice;
    114     lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
    115     lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
    116     p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
    117     p.setXfermodeMode(SkXfermode::kClear_Mode);
    118     rast->addLayer(p);
    119 
    120     p.setPathEffect(NULL);
    121     p.setXfermode(NULL);
    122     p.setStyle(SkPaint::kStroke_Style);
    123     p.setStrokeWidth(SK_Scalar1);
    124     rast->addLayer(p);
    125 }
    126 
    127 static void r9(SkLayerRasterizer* rast, SkPaint& p) {
    128     rast->addLayer(p);
    129 
    130     SkMatrix    lattice;
    131     lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
    132     lattice.postRotate(SkIntToScalar(30), 0, 0);
    133     p.setPathEffect(new SkLine2DPathEffect(SK_Scalar1*2, lattice))->unref();
    134     p.setXfermodeMode(SkXfermode::kClear_Mode);
    135     rast->addLayer(p);
    136 
    137     p.setPathEffect(NULL);
    138     p.setXfermode(NULL);
    139     p.setStyle(SkPaint::kStroke_Style);
    140     p.setStrokeWidth(SK_Scalar1);
    141     rast->addLayer(p);
    142 }
    143 
    144 typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
    145 
    146 static const raster_proc gRastProcs[] = {
    147     r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
    148 };
    149 
    150 #include "SkXfermode.h"
    151 
    152 static void apply_shader(SkPaint* paint, int index) {
    153     raster_proc proc = gRastProcs[index];
    154     if (proc)
    155     {
    156         SkPaint p;
    157         SkLayerRasterizer*  rast = new SkLayerRasterizer;
    158 
    159         p.setAntiAlias(true);
    160         proc(rast, p);
    161         paint->setRasterizer(rast)->unref();
    162     }
    163 
    164 #if 0
    165     SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
    166     paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
    167 #endif
    168     paint->setColor(SK_ColorBLUE);
    169 }
    170 
    171 class TextEffectsGM : public skiagm::GM {
    172 public:
    173     TextEffectsGM() {}
    174 
    175 protected:
    176     virtual SkString onShortName() SK_OVERRIDE {
    177         return SkString("texteffects");
    178     }
    179 
    180     virtual SkISize onISize() SK_OVERRIDE {
    181         return SkISize::Make(460, 680);
    182     }
    183 
    184     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
    185         canvas->save();
    186 
    187         SkPaint     paint;
    188         paint.setAntiAlias(true);
    189         paint.setTextSize(SkIntToScalar(56));
    190 
    191         SkScalar    x = SkIntToScalar(20);
    192         SkScalar    y = paint.getTextSize();
    193 
    194         SkString str("Hamburgefons");
    195 
    196         for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
    197             apply_shader(&paint, i);
    198 
    199             //  paint.setMaskFilter(NULL);
    200             //  paint.setColor(SK_ColorBLACK);
    201 
    202             canvas->drawText(str.c_str(), str.size(), x, y, paint);
    203 
    204             y += paint.getFontSpacing();
    205         }
    206 
    207         canvas->restore();
    208     }
    209 
    210 private:
    211     typedef skiagm::GM INHERITED;
    212 };
    213 
    214 //////////////////////////////////////////////////////////////////////////////
    215 
    216 static skiagm::GM* MyFactory(void*) { return new TextEffectsGM; }
    217 static skiagm::GMRegistry reg(MyFactory);
    218