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 "SkCanvas.h"
     10 #include "SkGradientShader.h"
     11 #include "SkLumaColorFilter.h"
     12 
     13 static SkScalar kSize   = 80;
     14 static SkScalar kInset  = 10;
     15 static SkColor  kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
     16 static SkColor  kColor2 = SkColorSetARGB(0xff, 0x80, 0xff, 0);
     17 
     18 static void draw_label(SkCanvas* canvas, const char* label,
     19                        const SkPoint& offset) {
     20     SkPaint paint;
     21     size_t len = strlen(label);
     22 
     23     SkScalar width = paint.measureText(label, len);
     24     canvas->drawText(label, len, offset.x() - width / 2, offset.y(),
     25                      paint);
     26 }
     27 
     28 static void draw_scene(SkCanvas* canvas, SkColorFilter* filter,
     29                        SkXfermode::Mode mode, SkShader* s1,
     30                        SkShader* s2) {
     31     SkPaint paint;
     32     paint.setAntiAlias(true);
     33     SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize);
     34 
     35     c = bounds;
     36     c.fRight = bounds.centerX();
     37     paint.setARGB(0x20, 0, 0, 0xff);
     38     canvas->drawRect(bounds, paint);
     39 
     40     canvas->saveLayer(&bounds, NULL);
     41 
     42     r = bounds;
     43     r.inset(kInset, 0);
     44     paint.setShader(s1);
     45     paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80));
     46     canvas->drawOval(r, paint);
     47     if (!s1) {
     48         canvas->save();
     49         canvas->clipRect(c);
     50         paint.setColor(kColor1);
     51         canvas->drawOval(r, paint);
     52         canvas->restore();
     53     }
     54 
     55     SkPaint xferPaint;
     56     xferPaint.setXfermodeMode(mode);
     57     canvas->saveLayer(&bounds, &xferPaint);
     58 
     59     r = bounds;
     60     r.inset(0, kInset);
     61     paint.setShader(s2);
     62     paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80));
     63     paint.setColorFilter(filter);
     64     canvas->drawOval(r, paint);
     65     if (!s2) {
     66         canvas->save();
     67         canvas->clipRect(c);
     68         paint.setColor(kColor2);
     69         canvas->drawOval(r, paint);
     70         canvas->restore();
     71     }
     72 
     73     canvas->restore();
     74     canvas->restore();
     75 }
     76 
     77 class LumaFilterGM : public skiagm::GM {
     78 public:
     79     LumaFilterGM() {
     80         SkColor  g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
     81         SkColor  g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
     82         SkPoint  g1Points[] = { { 0, 0 }, { 0,     100 } };
     83         SkPoint  g2Points[] = { { 0, 0 }, { kSize, 0   } };
     84         SkScalar pos[] = { 0.2f, 1.0f };
     85 
     86         fFilter.reset(SkLumaColorFilter::Create());
     87         fGr1.reset(SkGradientShader::CreateLinear(g1Points,
     88                                                   g1Colors,
     89                                                   pos,
     90                                                   SK_ARRAY_COUNT(g1Colors),
     91                                                   SkShader::kClamp_TileMode));
     92         fGr2.reset(SkGradientShader::CreateLinear(g2Points,
     93                                                   g2Colors,
     94                                                   pos,
     95                                                   SK_ARRAY_COUNT(g2Colors),
     96                                                   SkShader::kClamp_TileMode));
     97     }
     98 
     99 protected:
    100     virtual SkString onShortName() SK_OVERRIDE {
    101         return SkString("lumafilter");
    102     }
    103 
    104     virtual SkISize onISize() SK_OVERRIDE {
    105         return SkISize::Make(600, 420);
    106     }
    107 
    108     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
    109         SkXfermode::Mode modes[] = { SkXfermode::kSrcOver_Mode,
    110                                      SkXfermode::kDstOver_Mode,
    111                                      SkXfermode::kSrcATop_Mode,
    112                                      SkXfermode::kDstATop_Mode,
    113                                      SkXfermode::kSrcIn_Mode,
    114                                      SkXfermode::kDstIn_Mode,
    115                                    };
    116         struct {
    117             SkShader*   fShader1;
    118             SkShader*   fShader2;
    119         } shaders[] = {
    120             { NULL, NULL },
    121             { NULL, fGr2 },
    122             { fGr1, NULL },
    123             { fGr1, fGr2 },
    124         };
    125 
    126         SkScalar gridStep = kSize + 2 * kInset;
    127         for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
    128             draw_label(canvas, SkXfermode::ModeName(modes[i]),
    129                        SkPoint::Make(gridStep * (0.5f + i), 20));
    130         }
    131 
    132         for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) {
    133             canvas->save();
    134             canvas->translate(kInset, gridStep * i + 30);
    135             for (size_t m = 0; m < SK_ARRAY_COUNT(modes); ++m) {
    136                 draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1,
    137                            shaders[i].fShader2);
    138                 canvas->translate(gridStep, 0);
    139             }
    140             canvas->restore();
    141         }
    142     }
    143 
    144 private:
    145     SkAutoTUnref<SkColorFilter> fFilter;
    146     SkAutoTUnref<SkShader>      fGr1, fGr2;
    147 
    148     typedef skiagm::GM INHERITED;
    149 };
    150 
    151 //////////////////////////////////////////////////////////////////////////////
    152 
    153 DEF_GM( return SkNEW(LumaFilterGM); )
    154