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     paint.setTextSize(r.height()/2);
     43     paint.setTextAlign(SkPaint::kCenter_Align);
     44     canvas->save();
     45     canvas->clipRect(r);
     46     canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint);
     47     canvas->restore();
     48 }
     49 
     50 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
     51     SkPaint paint;
     52 
     53     SkIRect bounds;
     54     r.roundOut(&bounds);
     55 
     56     SkBitmap bm;
     57     bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
     58     bm.allocPixels();
     59     bm.eraseColor(SK_ColorTRANSPARENT);
     60     SkCanvas c(bm);
     61     draw_path(&c, r, NULL);
     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 static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
     71     SkPaint paint;
     72 
     73     SkIRect bounds;
     74     r.roundOut(&bounds);
     75 
     76     SkBitmap bm;
     77     bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height());
     78     bm.allocPixels();
     79     bm.eraseColor(SK_ColorRED);
     80     SkCanvas c(bm);
     81 
     82     SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
     83     paint.setColor(SK_ColorGREEN);
     84     c.drawRect(SkRect::Make(cropRect), paint);
     85 
     86     paint.setImageFilter(imf);
     87     SkPoint loc = { r.fLeft, r.fTop };
     88     canvas->getTotalMatrix().mapPoints(&loc, 1);
     89     canvas->drawSprite(bm,
     90                        SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
     91                        &paint);
     92 }
     93 
     94 ///////////////////////////////////////////////////////////////////////////////
     95 
     96 class DropShadowImageFilterGM : public skiagm::GM {
     97 public:
     98     DropShadowImageFilterGM () {}
     99 
    100 protected:
    101 
    102     virtual SkString onShortName() {
    103         return SkString("dropshadowimagefilter");
    104     }
    105 
    106     virtual SkISize onISize() { return SkISize::Make(400, 700); }
    107 
    108     void draw_frame(SkCanvas* canvas, const SkRect& r) {
    109         SkPaint paint;
    110         paint.setStyle(SkPaint::kStroke_Style);
    111         paint.setColor(SK_ColorRED);
    112         canvas->drawRect(r, paint);
    113     }
    114 
    115     virtual uint32_t onGetFlags() const {
    116         // Because of the use of drawSprite, this test is excluded
    117         // from scaled replay tests because drawSprite ignores the
    118         // reciprocal scale that is applied at record time, which is
    119         // the intended behavior of drawSprite.
    120         return kSkipScaledReplay_Flag;
    121     }
    122 
    123     virtual void onDraw(SkCanvas* canvas) {
    124         void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
    125             draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text
    126         };
    127 
    128         SkAutoTUnref<SkColorFilter> cf(
    129             SkColorFilter::CreateModeFilter(SK_ColorMAGENTA, SkXfermode::kSrcIn_Mode));
    130         SkAutoTUnref<SkImageFilter> cfif(SkColorFilterImageFilter::Create(cf.get()));
    131         SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
    132                                          SkImageFilter::CropRect::kHasAll_CropEdge);
    133         SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)),
    134                                           SkImageFilter::CropRect::kHasAll_CropEdge);
    135 
    136         SkImageFilter* filters[] = {
    137             NULL,
    138             new SkDropShadowImageFilter(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE),
    139             new SkDropShadowImageFilter(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE),
    140             new SkDropShadowImageFilter(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE),
    141             new SkDropShadowImageFilter(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, cfif),
    142             new SkDropShadowImageFilter(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, NULL, &cropRect),
    143             new SkDropShadowImageFilter(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, NULL, &bogusRect),
    144         };
    145 
    146         SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
    147         SkScalar MARGIN = SkIntToScalar(16);
    148         SkScalar DX = r.width() + MARGIN;
    149         SkScalar DY = r.height() + MARGIN;
    150 
    151         canvas->translate(MARGIN, MARGIN);
    152         for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
    153             canvas->save();
    154             for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
    155                 drawProc[j](canvas, r, filters[i]);
    156                 canvas->translate(0, DY);
    157             }
    158             canvas->restore();
    159             canvas->translate(DX, 0);
    160         }
    161 
    162         for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
    163             SkSafeUnref(filters[j]);
    164         }
    165     }
    166 
    167 private:
    168     typedef GM INHERITED;
    169 };
    170 
    171 ///////////////////////////////////////////////////////////////////////////////
    172 
    173 static skiagm::GM* MyFactory(void*) { return new DropShadowImageFilterGM; }
    174 static skiagm::GMRegistry reg(MyFactory);
    175