| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkBlurImageFilter.h" | 10 #include "SkBlurImageFilter.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 dy += bm.height() + 20; | 80 dy += bm.height() + 20; |
| 81 draw_1_bitmap(canvas, bm, true, dx, dy); | 81 draw_1_bitmap(canvas, bm, true, dx, dy); |
| 82 dy += bm.height() + 20; | 82 dy += bm.height() + 20; |
| 83 draw_1_bitmap(canvas, bm, true, dx, dy, filter); | 83 draw_1_bitmap(canvas, bm, true, dx, dy, filter); |
| 84 } | 84 } |
| 85 | 85 |
| 86 private: | 86 private: |
| 87 typedef GM INHERITED; | 87 typedef GM INHERITED; |
| 88 }; | 88 }; |
| 89 DEF_GM( return new SpriteBitmapGM; ) | 89 DEF_GM( return new SpriteBitmapGM; ) |
| 90 | |
| 91 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
| 92 | |
| 93 #include "SkColorFilterImageFilter.h" | |
| 94 #include "SkModeColorFilter.h" | |
| 95 #include "SkMorphologyImageFilter.h" | |
| 96 #include "SkOffsetImageFilter.h" | |
| 97 | |
| 98 static SkImage* make_image(SkCanvas* rootCanvas) { | |
| 99 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); | |
| 100 SkAutoTUnref<SkSurface> surface(rootCanvas->newSurface(info)); | |
| 101 if (!surface) { | |
| 102 surface.reset(SkSurface::NewRaster(info)); | |
| 103 } | |
| 104 | |
| 105 SkPaint paint; | |
| 106 paint.setAntiAlias(true); | |
| 107 paint.setColor(SK_ColorRED); | |
| 108 surface->getCanvas()->drawCircle(50, 50, 50, paint); | |
| 109 return surface->newImageSnapshot(); | |
| 110 } | |
| 111 | |
| 112 static void show_image(SkCanvas* canvas, SkImage* image, const SkIPoint& offset)
{ | |
| 113 SkScalar x = SkIntToScalar(offset.x()); | |
| 114 SkScalar y = SkIntToScalar(offset.y()); | |
| 115 | |
| 116 SkPaint paint; | |
| 117 paint.setStyle(SkPaint::kStroke_Style); | |
| 118 | |
| 119 SkRect r = SkRect::MakeIWH(image->width(), image->height()); | |
| 120 r.offset(x, y); | |
| 121 // get on pixel-centers to make the hairline land on a numerical stable boun
dary | |
| 122 r.outset(SK_ScalarHalf, SK_ScalarHalf); | |
| 123 canvas->drawRect(r, paint); | |
| 124 | |
| 125 canvas->drawImage(image, x, y, nullptr); | |
| 126 } | |
| 127 | |
| 128 typedef SkImageFilter* (*ImageFilterFactory)(); | |
| 129 | |
| 130 // +[]{...} did not work on windows (VS) | |
| 131 // (ImageFilterFactory)[]{...} did not work on linux (gcc) | |
| 132 // hence this cast function | |
| 133 template <typename T> ImageFilterFactory IFCCast(T arg) { return arg; } | |
| 134 | |
| 135 // We expect that applying the filter will keep us in the same domain (raster or
gpu) | |
| 136 static void check_same_domain(SkImage* a, SkImage* b) { | |
| 137 SkASSERT(a->isTextureBacked() == b->isTextureBacked()); | |
| 138 } | |
| 139 | |
| 140 /** | |
| 141 * Compare output of drawSprite and drawBitmap (esp. clipping and imagefilters) | |
| 142 */ | |
| 143 class ApplyFilterGM : public skiagm::GM { | |
| 144 public: | |
| 145 ApplyFilterGM() {} | |
| 146 | |
| 147 protected: | |
| 148 SkString onShortName() override { | |
| 149 return SkString("apply-filter"); | |
| 150 } | |
| 151 | |
| 152 SkISize onISize() override { | |
| 153 return SkISize::Make(780, 780); | |
| 154 } | |
| 155 | |
| 156 void onDraw(SkCanvas* canvas) override { | |
| 157 SkAutoTUnref<SkImage> image0(make_image(canvas)); | |
| 158 | |
| 159 const ImageFilterFactory factories[] = { | |
| 160 IFCCast([]{ return SkBlurImageFilter::Create(8, 8); }), | |
| 161 IFCCast([]{ SkAutoTUnref<SkColorFilter> cf(SkModeColorFilter::Create
(SK_ColorBLUE, | |
| 162 SkXfermode:
:kSrcIn_Mode)); | |
| 163 return SkColorFilterImageFilter::Create(cf); | |
| 164 }), | |
| 165 IFCCast([]{ return SkDilateImageFilter::Create(8, 8); }), | |
| 166 IFCCast([]{ return SkErodeImageFilter::Create(8, 8); }), | |
| 167 IFCCast([]{ return SkOffsetImageFilter::Create(8, 8); }), | |
| 168 }; | |
| 169 | |
| 170 const SkScalar spacer = image0->width() * 3.0f / 2; | |
| 171 | |
| 172 for (auto&& factory : factories) { | |
| 173 SkAutoTUnref<SkImageFilter> filter(factory()); | |
| 174 | |
| 175 SkIPoint offset1, offset2; | |
| 176 SkAutoTUnref<SkImage> image1(image0->applyFilter(filter, &offset1, t
rue)); | |
| 177 SkAutoTUnref<SkImage> image2(image0->applyFilter(filter, &offset2, f
alse)); | |
| 178 | |
| 179 check_same_domain(image0, image1); | |
| 180 check_same_domain(image0, image2); | |
| 181 | |
| 182 canvas->save(); | |
| 183 canvas->translate(30, 30); | |
| 184 show_image(canvas, image0, SkIPoint::Make(0, 0)); // original | |
| 185 canvas->translate(spacer, 0); | |
| 186 show_image(canvas, image1, offset1); // snug | |
| 187 canvas->translate(spacer, 0); | |
| 188 show_image(canvas, image2, offset2); // not snug | |
| 189 | |
| 190 canvas->restore(); | |
| 191 | |
| 192 canvas->translate(0, spacer); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 private: | |
| 197 typedef GM INHERITED; | |
| 198 }; | |
| 199 DEF_GM( return new ApplyFilterGM; ) | |
| OLD | NEW |