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