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 "SkCanvas.h"
     10 #include "SkColorFilter.h"
     11 #include "SkColorPriv.h"
     12 #include "SkShader.h"
     13 
     14 #include "SkBlurImageFilter.h"
     15 #include "SkColorFilterImageFilter.h"
     16 #include "SkComposeImageFilter.h"
     17 #include "SkMergeImageFilter.h"
     18 #include "SkOffsetImageFilter.h"
     19 #include "SkTestImageFilters.h"
     20 
     21 #define FILTER_WIDTH    SkIntToScalar(150)
     22 #define FILTER_HEIGHT   SkIntToScalar(200)
     23 
     24 static SkImageFilter* make0() { return new SkDownSampleImageFilter(SK_Scalar1 / 5); }
     25 static SkImageFilter* make1() { return new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16)); }
     26 static SkImageFilter* make2() {
     27     SkColorFilter* cf = SkColorFilter::CreateModeFilter(SK_ColorBLUE,
     28                                                         SkXfermode::kSrcIn_Mode);
     29     SkAutoUnref aur(cf);
     30     return SkColorFilterImageFilter::Create(cf);
     31 }
     32 static SkImageFilter* make3() {
     33     return new SkBlurImageFilter(8, 0);
     34 }
     35 
     36 static SkImageFilter* make4() {
     37     SkImageFilter* outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
     38     SkImageFilter* inner = new SkDownSampleImageFilter(SK_Scalar1 / 5);
     39     SkAutoUnref aur0(outer);
     40     SkAutoUnref aur1(inner);
     41     return new SkComposeImageFilter(outer, inner);
     42 }
     43 static SkImageFilter* make5() {
     44     SkImageFilter* first = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
     45     SkImageFilter* second = new SkDownSampleImageFilter(SK_Scalar1 / 5);
     46     SkAutoUnref aur0(first);
     47     SkAutoUnref aur1(second);
     48     return new SkMergeImageFilter(first, second);
     49 }
     50 
     51 static SkImageFilter* make6() {
     52     SkImageFilter* outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
     53     SkImageFilter* inner = new SkDownSampleImageFilter(SK_Scalar1 / 5);
     54     SkAutoUnref aur0(outer);
     55     SkAutoUnref aur1(inner);
     56     SkImageFilter* compose = new SkComposeImageFilter(outer, inner);
     57     SkAutoUnref aur2(compose);
     58 
     59     SkColorFilter* cf = SkColorFilter::CreateModeFilter(0x880000FF,
     60                                                         SkXfermode::kSrcIn_Mode);
     61     SkAutoUnref aur3(cf);
     62     SkImageFilter* blue = SkColorFilterImageFilter::Create(cf);
     63     SkAutoUnref aur4(blue);
     64 
     65     return new SkMergeImageFilter(compose, blue);
     66 }
     67 
     68 static SkImageFilter* make7() {
     69     SkImageFilter* outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
     70     SkImageFilter* inner = make3();
     71     SkAutoUnref aur0(outer);
     72     SkAutoUnref aur1(inner);
     73     SkImageFilter* compose = new SkComposeImageFilter(outer, inner);
     74     SkAutoUnref aur2(compose);
     75 
     76     SkColorFilter* cf = SkColorFilter::CreateModeFilter(0x880000FF,
     77                                                         SkXfermode::kSrcIn_Mode);
     78     SkAutoUnref aur3(cf);
     79     SkImageFilter* blue = SkColorFilterImageFilter::Create(cf);
     80     SkAutoUnref aur4(blue);
     81 
     82     return new SkMergeImageFilter(compose, blue);
     83 }
     84 
     85 static void draw0(SkCanvas* canvas) {
     86     SkPaint p;
     87     p.setAntiAlias(true);
     88     SkRect r = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
     89     r.inset(SK_Scalar1 * 12, SK_Scalar1 * 12);
     90     p.setColor(SK_ColorRED);
     91     canvas->drawOval(r, p);
     92 }
     93 
     94 class TestImageFiltersGM : public skiagm::GM {
     95 public:
     96     TestImageFiltersGM () {}
     97 
     98 protected:
     99 
    100     virtual SkString onShortName() {
    101         return SkString("testimagefilters");
    102     }
    103 
    104     virtual SkISize onISize() { return SkISize::Make(700, 460); }
    105 
    106     virtual void onDraw(SkCanvas* canvas) {
    107 //        this->drawSizeBounds(canvas, 0xFFCCCCCC);
    108 
    109         static SkImageFilter* (*gFilterProc[])() = {
    110             make0, make1, make2, make3, make4, make5, make6, make7
    111         };
    112 
    113         const SkRect bounds = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
    114 
    115         const SkScalar dx = bounds.width() * 8 / 7;
    116         const SkScalar dy = bounds.height() * 8 / 7;
    117 
    118         canvas->translate(SkIntToScalar(8), SkIntToScalar(8));
    119 
    120         for (size_t i = 0; i < SK_ARRAY_COUNT(gFilterProc); ++i) {
    121             int ix = i % 4;
    122             int iy = i / 4;
    123 
    124             SkAutoCanvasRestore acr(canvas, true);
    125             canvas->translate(ix * dx, iy * dy);
    126 
    127             SkPaint p;
    128             p.setStyle(SkPaint::kStroke_Style);
    129             canvas->drawRect(bounds, p);
    130 
    131             SkPaint paint;
    132             paint.setImageFilter(gFilterProc[i]())->unref();
    133             canvas->saveLayer(&bounds, &paint);
    134             draw0(canvas);
    135         }
    136     }
    137 
    138 private:
    139     typedef GM INHERITED;
    140 };
    141 
    142 //////////////////////////////////////////////////////////////////////////////
    143 
    144 static skiagm::GM* MyFactory(void*) { return new TestImageFiltersGM; }
    145 static skiagm::GMRegistry reg(MyFactory);
    146