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