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 "sk_tool_utils.h" 10 #include "SkCanvas.h" 11 #include "SkColorFilter.h" 12 #include "SkColorPriv.h" 13 #include "SkShader.h" 14 15 #include "SkBlurImageFilter.h" 16 #include "SkMorphologyImageFilter.h" 17 #include "SkColorFilterImageFilter.h" 18 #include "SkMergeImageFilter.h" 19 #include "SkOffsetImageFilter.h" 20 21 /////////////////////////////////////////////////////////////////////////////// 22 23 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { 24 SkPaint paint; 25 paint.setImageFilter(std::move(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, sk_sp<SkImageFilter> imf) { 34 SkPaint paint; 35 paint.setColor(SK_ColorMAGENTA); 36 paint.setImageFilter(std::move(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, sk_sp<SkImageFilter> imf) { 42 SkPaint paint; 43 paint.setImageFilter(std::move(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, sk_sp<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(std::move(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 void onOnceBeforeDraw() override{ 110 make_checkerboard(); 111 } 112 113 void onDraw(SkCanvas* canvas) override { 114 void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = { 115 draw_bitmap, draw_path, draw_paint, draw_text 116 }; 117 118 sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorBLUE, 119 SkBlendMode::kSrcIn)); 120 SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)), 121 SkImageFilter::CropRect::kHasAll_CropEdge); 122 SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)), 123 SkImageFilter::CropRect::kHasAll_CropEdge); 124 125 sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(SkIntToScalar(-10), 126 SkIntToScalar(-10), 127 nullptr)); 128 129 sk_sp<SkImageFilter> cfOffset(SkColorFilterImageFilter::Make(cf, std::move(offset))); 130 131 sk_sp<SkImageFilter> erodeX(SkErodeImageFilter::Make(8, 0, nullptr, &cropRect)); 132 sk_sp<SkImageFilter> erodeY(SkErodeImageFilter::Make(0, 8, nullptr, &cropRect)); 133 134 sk_sp<SkImageFilter> filters[] = { 135 nullptr, 136 SkColorFilterImageFilter::Make(cf, nullptr, &cropRect), 137 SkBlurImageFilter::Make(0.0f, 0.0f, nullptr, &cropRect), 138 SkBlurImageFilter::Make(1.0f, 1.0f, nullptr, &cropRect), 139 SkBlurImageFilter::Make(8.0f, 0.0f, nullptr, &cropRect), 140 SkBlurImageFilter::Make(0.0f, 8.0f, nullptr, &cropRect), 141 SkBlurImageFilter::Make(8.0f, 8.0f, nullptr, &cropRect), 142 SkErodeImageFilter::Make(1, 1, nullptr, &cropRect), 143 SkErodeImageFilter::Make(8, 0, std::move(erodeY), &cropRect), 144 SkErodeImageFilter::Make(0, 8, std::move(erodeX), &cropRect), 145 SkErodeImageFilter::Make(8, 8, nullptr, &cropRect), 146 SkMergeImageFilter::Make(nullptr, 147 std::move(cfOffset), 148 SkBlendMode::kSrcOver, 149 &cropRect), 150 SkBlurImageFilter::Make(8.0f, 8.0f, nullptr, &bogusRect), 151 SkColorFilterImageFilter::Make(cf, nullptr, &bogusRect), 152 }; 153 154 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); 155 SkScalar MARGIN = SkIntToScalar(16); 156 SkScalar DX = r.width() + MARGIN; 157 SkScalar DY = r.height() + MARGIN; 158 159 canvas->translate(MARGIN, MARGIN); 160 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { 161 canvas->save(); 162 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { 163 SkPaint paint; 164 canvas->drawBitmap(fCheckerboard, 0, 0); 165 drawProc[j](canvas, r, filters[i]); 166 canvas->translate(0, DY); 167 } 168 canvas->restore(); 169 canvas->translate(DX, 0); 170 } 171 } 172 173 private: 174 SkBitmap fCheckerboard; 175 typedef GM INHERITED; 176 }; 177 178 /////////////////////////////////////////////////////////////////////////////// 179 180 DEF_GM( return new ImageFiltersCroppedGM; ) 181