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 "SkColorFilter.h"
     10 
     11 #include "SkColorFilterImageFilter.h"
     12 #include "SkDropShadowImageFilter.h"
     13 
     14 ///////////////////////////////////////////////////////////////////////////////
     15 
     16 static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
     17     SkPaint paint;
     18     paint.setImageFilter(imf);
     19     paint.setColor(SK_ColorBLACK);
     20     canvas->save();
     21     canvas->clipRect(r);
     22     canvas->drawPaint(paint);
     23     canvas->restore();
     24 }
     25 
     26 static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
     27     SkPaint paint;
     28     paint.setColor(SK_ColorGREEN);
     29     paint.setImageFilter(imf);
     30     paint.setAntiAlias(true);
     31     canvas->save();
     32     canvas->clipRect(r);
     33     canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint);
     34     canvas->restore();
     35 }
     36 
     37 static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
     38     SkPaint paint;
     39     paint.setImageFilter(imf);
     40     paint.setColor(SK_ColorGREEN);
     41     paint.setAntiAlias(true);
     42     sk_tool_utils::set_portable_typeface(&paint);
     43     paint.setTextSize(r.height()/2);
     44     paint.setTextAlign(SkPaint::kCenter_Align);
     45     canvas->save();
     46     canvas->clipRect(r);
     47     canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint);
     48     canvas->restore();
     49 }
     50 
     51 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
     52     SkPaint paint;
     53 
     54     SkIRect bounds;
     55     r.roundOut(&bounds);
     56 
     57     SkBitmap bm;
     58     bm.allocN32Pixels(bounds.width(), bounds.height());
     59     bm.eraseColor(SK_ColorTRANSPARENT);
     60     SkCanvas c(bm);
     61     draw_path(&c, r, nullptr);
     62 
     63     paint.setImageFilter(imf);
     64     canvas->save();
     65     canvas->clipRect(r);
     66     canvas->drawBitmap(bm, 0, 0, &paint);
     67     canvas->restore();
     68 }
     69 
     70 ///////////////////////////////////////////////////////////////////////////////
     71 
     72 class DropShadowImageFilterGM : public skiagm::GM {
     73 public:
     74     DropShadowImageFilterGM () {}
     75 
     76 protected:
     77 
     78     virtual SkString onShortName() {
     79         return SkString("dropshadowimagefilter");
     80     }
     81 
     82     virtual SkISize onISize() { return SkISize::Make(400, 656); }
     83 
     84     void draw_frame(SkCanvas* canvas, const SkRect& r) {
     85         SkPaint paint;
     86         paint.setStyle(SkPaint::kStroke_Style);
     87         paint.setColor(SK_ColorRED);
     88         canvas->drawRect(r, paint);
     89     }
     90 
     91     virtual void onDraw(SkCanvas* canvas) {
     92         void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
     93             draw_bitmap, draw_path, draw_paint, draw_text
     94         };
     95 
     96         SkAutoTUnref<SkColorFilter> cf(
     97             SkColorFilter::CreateModeFilter(SK_ColorMAGENTA, SkXfermode::kSrcIn_Mode));
     98         SkAutoTUnref<SkImageFilter> cfif(SkColorFilterImageFilter::Create(cf.get()));
     99         SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
    100                                          SkImageFilter::CropRect::kHasAll_CropEdge);
    101         SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)),
    102                                           SkImageFilter::CropRect::kHasAll_CropEdge);
    103 
    104         SkImageFilter* filters[] = {
    105             nullptr,
    106             SkDropShadowImageFilter::Create(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE,
    107                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
    108             SkDropShadowImageFilter::Create(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE,
    109                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
    110             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
    111                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
    112             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
    113                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, cfif, nullptr),
    114             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
    115                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr, &cropRect),
    116             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
    117                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr, &bogusRect),
    118             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
    119                 SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode),
    120         };
    121 
    122         SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
    123         SkScalar MARGIN = SkIntToScalar(16);
    124         SkScalar DX = r.width() + MARGIN;
    125         SkScalar DY = r.height() + MARGIN;
    126 
    127         canvas->translate(MARGIN, MARGIN);
    128         for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
    129             canvas->save();
    130             for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
    131                 drawProc[j](canvas, r, filters[i]);
    132                 canvas->translate(0, DY);
    133             }
    134             canvas->restore();
    135             canvas->translate(DX, 0);
    136         }
    137 
    138         for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
    139             SkSafeUnref(filters[j]);
    140         }
    141     }
    142 
    143 private:
    144     typedef GM INHERITED;
    145 };
    146 
    147 ///////////////////////////////////////////////////////////////////////////////
    148 
    149 DEF_GM( return new DropShadowImageFilterGM; )
    150