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 "SkMorphologyImageFilter.h" 16 #include "SkColorFilterImageFilter.h" 17 #include "SkMergeImageFilter.h" 18 #include "SkOffsetImageFilter.h" 19 #include "SkTestImageFilters.h" 20 21 /////////////////////////////////////////////////////////////////////////////// 22 23 static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 24 SkPaint paint; 25 paint.setImageFilter(imf); 26 paint.setColor(SK_ColorBLACK); 27 canvas->save(); 28 canvas->clipRect(r); 29 canvas->drawPaint(paint); 30 canvas->restore(); 31 } 32 33 static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 34 SkPaint paint; 35 paint.setColor(SK_ColorMAGENTA); 36 paint.setImageFilter(imf); 37 paint.setAntiAlias(true); 38 canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint); 39 } 40 41 static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 42 SkPaint paint; 43 paint.setImageFilter(imf); 44 paint.setColor(SK_ColorGREEN); 45 paint.setAntiAlias(true); 46 sk_tool_utils::set_portable_typeface(&paint); 47 paint.setTextSize(r.height()/2); 48 paint.setTextAlign(SkPaint::kCenter_Align); 49 canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint); 50 } 51 52 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 53 SkPaint paint; 54 55 SkIRect bounds; 56 r.roundOut(&bounds); 57 58 SkBitmap bm; 59 bm.allocN32Pixels(bounds.width(), bounds.height()); 60 bm.eraseColor(SK_ColorTRANSPARENT); 61 SkCanvas c(bm); 62 draw_path(&c, r, nullptr); 63 64 paint.setImageFilter(imf); 65 canvas->drawBitmap(bm, 0, 0, &paint); 66 } 67 68 /////////////////////////////////////////////////////////////////////////////// 69 70 class ImageFiltersCroppedGM : public skiagm::GM { 71 public: 72 ImageFiltersCroppedGM () {} 73 74 protected: 75 SkString onShortName() override { 76 return SkString("imagefilterscropped"); 77 } 78 79 SkISize onISize() override { return SkISize::Make(400, 960); } 80 81 void make_checkerboard() { 82 fCheckerboard.allocN32Pixels(80, 80); 83 SkCanvas canvas(fCheckerboard); 84 canvas.clear(SK_ColorTRANSPARENT); 85 SkPaint darkPaint; 86 darkPaint.setColor(sk_tool_utils::color_to_565(0xFF404040)); 87 SkPaint lightPaint; 88 lightPaint.setColor(sk_tool_utils::color_to_565(0xFFA0A0A0)); 89 for (int y = 0; y < 80; y += 16) { 90 for (int x = 0; x < 80; x += 16) { 91 canvas.save(); 92 canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); 93 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); 94 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); 95 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); 96 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); 97 canvas.restore(); 98 } 99 } 100 } 101 102 void draw_frame(SkCanvas* canvas, const SkRect& r) { 103 SkPaint paint; 104 paint.setStyle(SkPaint::kStroke_Style); 105 paint.setColor(SK_ColorRED); 106 canvas->drawRect(r, paint); 107 } 108 109 virtual void onOnceBeforeDraw() override{ 110 make_checkerboard(); 111 } 112 113 virtual void onDraw(SkCanvas* canvas) override { 114 void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = { 115 draw_bitmap, draw_path, draw_paint, draw_text 116 }; 117 118 SkAutoTUnref<SkColorFilter> cf( 119 SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode)); 120 SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)), SkImageFilter::CropRect::kHasAll_CropEdge); 121 SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)), SkImageFilter::CropRect::kHasAll_CropEdge); 122 123 SkAutoTUnref<SkImageFilter> offset(SkOffsetImageFilter::Create( 124 SkIntToScalar(-10), SkIntToScalar(-10))); 125 126 SkAutoTUnref<SkImageFilter> cfOffset(SkColorFilterImageFilter::Create(cf.get(), offset.get())); 127 128 SkAutoTUnref<SkImageFilter> erodeX(SkErodeImageFilter::Create(8, 0, nullptr, &cropRect)); 129 SkAutoTUnref<SkImageFilter> erodeY(SkErodeImageFilter::Create(0, 8, nullptr, &cropRect)); 130 131 SkImageFilter* filters[] = { 132 nullptr, 133 SkColorFilterImageFilter::Create(cf.get(), nullptr, &cropRect), 134 SkBlurImageFilter::Create(0.0f, 0.0f, nullptr, &cropRect), 135 SkBlurImageFilter::Create(1.0f, 1.0f, nullptr, &cropRect), 136 SkBlurImageFilter::Create(8.0f, 0.0f, nullptr, &cropRect), 137 SkBlurImageFilter::Create(0.0f, 8.0f, nullptr, &cropRect), 138 SkBlurImageFilter::Create(8.0f, 8.0f, nullptr, &cropRect), 139 SkErodeImageFilter::Create(1, 1, nullptr, &cropRect), 140 SkErodeImageFilter::Create(8, 0, erodeY, &cropRect), 141 SkErodeImageFilter::Create(0, 8, erodeX, &cropRect), 142 SkErodeImageFilter::Create(8, 8, nullptr, &cropRect), 143 SkMergeImageFilter::Create(nullptr, cfOffset.get(), SkXfermode::kSrcOver_Mode, &cropRect), 144 SkBlurImageFilter::Create(8.0f, 8.0f, nullptr, &bogusRect), 145 SkColorFilterImageFilter::Create(cf.get(), nullptr, &bogusRect), 146 }; 147 148 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); 149 SkScalar MARGIN = SkIntToScalar(16); 150 SkScalar DX = r.width() + MARGIN; 151 SkScalar DY = r.height() + MARGIN; 152 153 canvas->translate(MARGIN, MARGIN); 154 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { 155 canvas->save(); 156 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { 157 SkPaint paint; 158 canvas->drawBitmap(fCheckerboard, 0, 0); 159 drawProc[j](canvas, r, filters[i]); 160 canvas->translate(0, DY); 161 } 162 canvas->restore(); 163 canvas->translate(DX, 0); 164 } 165 166 for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { 167 SkSafeUnref(filters[j]); 168 } 169 } 170 171 private: 172 SkBitmap fCheckerboard; 173 typedef GM INHERITED; 174 }; 175 176 /////////////////////////////////////////////////////////////////////////////// 177 178 DEF_GM( return new ImageFiltersCroppedGM; ) 179