| 1 |
/* |
| 2 |
* Copyright 2015 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/gm.h" |
| 9 |
#include "include/core/SkBlendMode.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorFilter.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkImageFilter.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkMaskFilter.h" |
| 17 |
#include "include/core/SkMatrix.h" |
| 18 |
#include "include/core/SkPaint.h" |
| 19 |
#include "include/core/SkRRect.h" |
| 20 |
#include "include/core/SkRect.h" |
| 21 |
#include "include/core/SkRefCnt.h" |
| 22 |
#include "include/core/SkScalar.h" |
| 23 |
#include "include/core/SkSurface.h" |
| 24 |
#include "include/core/SkTypes.h" |
| 25 |
#include "include/effects/SkColorMatrix.h" |
| 26 |
#include "include/effects/SkGradient.h" |
| 27 |
#include "include/effects/SkHighContrastFilter.h" |
| 28 |
#include "include/effects/SkImageFilters.h" |
| 29 |
#include "include/effects/SkShaderMaskFilter.h" |
| 30 |
#include "src/core/SkCanvasPriv.h" |
| 31 |
#include "tools/DecodeUtils.h" |
| 32 |
#include "tools/Resources.h" |
| 33 |
#include "tools/ToolUtils.h" |
| 34 |
|
| 35 |
#if defined(SK_GANESH) |
| 36 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 37 |
#include "include/gpu/ganesh/SkImageGanesh.h" |
| 38 |
#endif |
| 39 |
|
| 40 |
#include <utility> |
| 41 |
|
| 42 |
/** |
| 43 |
* Test drawing a primitive w/ an imagefilter (in this case, just matrix w/ identity) to see |
| 44 |
* that we apply the xfermode *after* the image has been created and filtered, and not during |
| 45 |
* the creation step (i.e. before it is filtered). |
| 46 |
* |
| 47 |
* see skbug.com/40034872 |
| 48 |
*/ |
| 49 |
static void do_draw(SkCanvas* canvas, SkBlendMode mode, sk_sp<SkImageFilter> imf) { |
| 50 |
SkAutoCanvasRestore acr(canvas, true); |
| 51 |
canvas->clipRect(SkRect::MakeWH(220, 220)); |
| 52 |
|
| 53 |
// want to force a layer, so modes like DstIn can combine meaningfully, but the final |
| 54 |
// image can still be shown against our default (opaque) background. non-opaque GMs |
| 55 |
// are a lot more trouble to compare/triage. |
| 56 |
canvas->saveLayer(nullptr, nullptr); |
| 57 |
canvas->drawColor(SK_ColorGREEN); |
| 58 |
|
| 59 |
SkPaint paint; |
| 60 |
paint.setAntiAlias(true); |
| 61 |
|
| 62 |
SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100); |
| 63 |
SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200); |
| 64 |
|
| 65 |
paint.setColor(SK_ColorRED); |
| 66 |
canvas->drawOval(r0, paint); |
| 67 |
|
| 68 |
paint.setColor(0x660000FF); |
| 69 |
paint.setImageFilter(std::move(imf)); |
| 70 |
paint.setBlendMode(mode); |
| 71 |
canvas->drawOval(r1, paint); |
| 72 |
} |
| 73 |
|
| 74 |
DEF_SIMPLE_GM(imagefilters_xfermodes, canvas, 480, 480) { |
| 75 |
canvas->translate(10, 10); |
| 76 |
|
| 77 |
// just need an imagefilter to trigger the code-path (which creates a tmp layer) |
| 78 |
sk_sp<SkImageFilter> imf(SkImageFilters::MatrixTransform(SkMatrix::I(), |
| 79 |
SkSamplingOptions(), |
| 80 |
nullptr)); |
| 81 |
|
| 82 |
const SkBlendMode modes[] = { |
| 83 |
SkBlendMode::kSrcATop, SkBlendMode::kDstIn |
| 84 |
}; |
| 85 |
|
| 86 |
for (size_t i = 0; i < std::size(modes); ++i) { |
| 87 |
canvas->save(); |
| 88 |
do_draw(canvas, modes[i], nullptr); |
| 89 |
canvas->translate(240, 0); |
| 90 |
do_draw(canvas, modes[i], imf); |
| 91 |
canvas->restore(); |
| 92 |
|
| 93 |
canvas->translate(0, 240); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static sk_sp<SkImage> make_image(SkCanvas* canvas) { |
| 98 |
const SkImageInfo info = SkImageInfo::MakeS32(100, 100, kPremul_SkAlphaType); |
| 99 |
auto surface(ToolUtils::makeSurface(canvas, info)); |
| 100 |
surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), SkPaint()); |
| 101 |
return surface->makeImageSnapshot(); |
| 102 |
} |
| 103 |
|
| 104 |
// Compare blurs when we're tightly clipped (fast) and not as tightly (slower) |
| 105 |
// |
| 106 |
// Expect the two to draw the same (modulo the extra border of pixels when the clip is larger) |
| 107 |
// |
| 108 |
DEF_SIMPLE_GM(fast_slow_blurimagefilter, canvas, 620, 260) { |
| 109 |
sk_sp<SkImage> image(make_image(canvas)); |
| 110 |
const SkRect r = SkRect::MakeIWH(image->width(), image->height()); |
| 111 |
|
| 112 |
canvas->translate(10, 10); |
| 113 |
for (SkScalar sigma = 8; sigma <= 128; sigma *= 2) { |
| 114 |
SkPaint paint; |
| 115 |
paint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr)); |
| 116 |
|
| 117 |
canvas->save(); |
| 118 |
// we outset the clip by 1, to fall out of the fast-case in drawImage |
| 119 |
// i.e. the clip is larger than the image |
| 120 |
for (SkScalar outset = 0; outset <= 1; ++outset) { |
| 121 |
canvas->save(); |
| 122 |
canvas->clipRect(r.makeOutset(outset, outset)); |
| 123 |
canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint); |
| 124 |
canvas->restore(); |
| 125 |
canvas->translate(0, r.height() + 20); |
| 126 |
} |
| 127 |
canvas->restore(); |
| 128 |
canvas->translate(r.width() + 20, 0); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 133 |
|
| 134 |
static void draw_set(SkCanvas* canvas, sk_sp<SkImageFilter> filters[], int count) { |
| 135 |
const SkRect r = SkRect::MakeXYWH(30, 30, 200, 200); |
| 136 |
const SkScalar offset = 250; |
| 137 |
SkScalar dx = 0, dy = 0; |
| 138 |
|
| 139 |
for (int i = 0; i < count; ++i) { |
| 140 |
canvas->save(); |
| 141 |
SkRRect rr = SkRRect::MakeRectXY(r.makeOffset(dx, dy), 20, 20); |
| 142 |
canvas->clipRRect(rr, true); |
| 143 |
canvas->saveLayer(SkCanvas::SaveLayerRec(&rr.getBounds(), nullptr, filters[i].get(), 0)); |
| 144 |
canvas->drawColor(0x40FFFFFF); |
| 145 |
canvas->restore(); |
| 146 |
canvas->restore(); |
| 147 |
|
| 148 |
if (0 == dx) { |
| 149 |
dx = offset; |
| 150 |
} else { |
| 151 |
dx = 0; |
| 152 |
dy = offset; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
class SaveLayerWithBackdropGM : public skiagm::GM { |
| 158 |
protected: |
| 159 |
bool runAsBench() const override { return true; } |
| 160 |
SkString getName() const override { return SkString("savelayer_with_backdrop"); } |
| 161 |
SkISize getISize() override { return SkISize::Make(830, 550); } |
| 162 |
|
| 163 |
void onDraw(SkCanvas* canvas) override { |
| 164 |
SkColorMatrix cm; |
| 165 |
cm.setSaturation(10); |
| 166 |
sk_sp<SkColorFilter> cf(SkColorFilters::Matrix(cm)); |
| 167 |
const SkScalar kernel[] = { 4, 0, 4, 0, -15, 0, 4, 0, 4 }; |
| 168 |
sk_sp<SkImageFilter> filters[] = { |
| 169 |
SkImageFilters::Blur(10, 10, nullptr), |
| 170 |
SkImageFilters::Dilate(8, 8, nullptr), |
| 171 |
SkImageFilters::MatrixConvolution({ 3, 3 }, kernel, 1, 0, { 0, 0 }, |
| 172 |
SkTileMode::kDecal, true, nullptr), |
| 173 |
SkImageFilters::ColorFilter(std::move(cf), nullptr), |
| 174 |
}; |
| 175 |
|
| 176 |
const struct { |
| 177 |
SkScalar fSx, fSy, fTx, fTy; |
| 178 |
} xforms[] = { |
| 179 |
{ 1, 1, 0, 0 }, |
| 180 |
{ 0.5f, 0.5f, 530, 0 }, |
| 181 |
{ 0.25f, 0.25f, 530, 275 }, |
| 182 |
{ 0.125f, 0.125f, 530, 420 }, |
| 183 |
}; |
| 184 |
|
| 185 |
SkSamplingOptions sampling(SkFilterMode::kLinear, |
| 186 |
SkMipmapMode::kLinear); |
| 187 |
sk_sp<SkImage> image(ToolUtils::GetResourceAsImage("images/mandrill_512.png")); |
| 188 |
|
| 189 |
canvas->translate(20, 20); |
| 190 |
for (const auto& xform : xforms) { |
| 191 |
canvas->save(); |
| 192 |
canvas->translate(xform.fTx, xform.fTy); |
| 193 |
canvas->scale(xform.fSx, xform.fSy); |
| 194 |
canvas->drawImage(image, 0, 0, sampling, nullptr); |
| 195 |
draw_set(canvas, filters, std::size(filters)); |
| 196 |
canvas->restore(); |
| 197 |
} |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
DEF_GM(return new SaveLayerWithBackdropGM();) |
| 202 |
|
| 203 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 204 |
|
| 205 |
// Test that color filters and mask filters are applied before the image filter, even if it would |
| 206 |
// normally be a sprite draw that could avoid an auto-saveLayer. |
| 207 |
DEF_SIMPLE_GM(imagefilters_effect_order, canvas, 512, 512) { |
| 208 |
sk_sp<SkImage> image(ToolUtils::GetResourceAsImage("images/mandrill_256.png")); |
| 209 |
#if defined(SK_GANESH) |
| 210 |
if (auto direct = GrAsDirectContext(canvas->recordingContext())) { |
| 211 |
if (sk_sp<SkImage> gpuImage = SkImages::TextureFromImage(direct, image)) { |
| 212 |
image = std::move(gpuImage); |
| 213 |
} |
| 214 |
} |
| 215 |
#endif |
| 216 |
|
| 217 |
SkISize kernelSize = SkISize::Make(3, 3); |
| 218 |
SkIPoint kernelOffset = SkIPoint::Make(1, 1); |
| 219 |
// A Laplacian edge detector, ie https://en.wikipedia.org/wiki/Kernel_(image_processing) |
| 220 |
SkScalar kernel[9] = {-1.f, -1.f, -1.f, |
| 221 |
-1.f, 8.f, -1.f, |
| 222 |
-1.f, -1.f, -1.f}; |
| 223 |
auto edgeDetector = SkImageFilters::MatrixConvolution( |
| 224 |
kernelSize, kernel, 1.f, 0.f, kernelOffset, SkTileMode::kClamp, false, nullptr); |
| 225 |
// This uses the high contrast filter because it resembles a pre-processing step you may perform |
| 226 |
// prior to edge detection. The specifics of the high contrast algorithm don't matter for the GM |
| 227 |
auto edgeAmplify = SkHighContrastFilter::Make( |
| 228 |
{false, SkHighContrastConfig::InvertStyle::kNoInvert, 0.5f}); |
| 229 |
|
| 230 |
SkPaint testCFPaint; |
| 231 |
testCFPaint.setColorFilter(edgeAmplify); |
| 232 |
testCFPaint.setImageFilter(edgeDetector); |
| 233 |
|
| 234 |
// The expected result is color filter then image filter, so represent this explicitly in the |
| 235 |
// image filter graph. |
| 236 |
SkPaint expectedCFPaint; |
| 237 |
expectedCFPaint.setImageFilter(SkImageFilters::Compose(edgeDetector, |
| 238 |
SkImageFilters::ColorFilter(edgeAmplify, nullptr))); |
| 239 |
|
| 240 |
// Draw the image twice (expected on the left, test on the right that should match) |
| 241 |
SkRect crop = SkRect::Make(image->bounds()); |
| 242 |
canvas->save(); |
| 243 |
canvas->clipRect(crop); |
| 244 |
canvas->drawImage(image, 0, 0, SkSamplingOptions(), &expectedCFPaint); // Filter applied by draw's SkPaint |
| 245 |
canvas->restore(); |
| 246 |
|
| 247 |
canvas->save(); |
| 248 |
canvas->translate(image->width(), 0); |
| 249 |
canvas->clipRect(crop); |
| 250 |
canvas->drawImage(image, 0, 0, SkSamplingOptions(), &testCFPaint); |
| 251 |
canvas->restore(); |
| 252 |
|
| 253 |
// Now test mask filters. These should be run before the image filter, and thus have the same |
| 254 |
// effect as multiplying by an alpha mask. |
| 255 |
|
| 256 |
// This mask filter pokes a hole in the center of the image |
| 257 |
static constexpr SkColor4f kAlphas[] = { SkColors::kBlack, SkColors::kTransparent }; |
| 258 |
static constexpr SkScalar kPos[] = { 0.4f, 0.9f }; |
| 259 |
sk_sp<SkShader> alphaMaskShader = SkShaders::RadialGradient( |
| 260 |
{128.f, 128.f}, 128.f, {{kAlphas, kPos, SkTileMode::kClamp}, {}}); |
| 261 |
sk_sp<SkMaskFilter> maskFilter = SkShaderMaskFilter::Make(alphaMaskShader); |
| 262 |
|
| 263 |
// If edge detector sees the mask filter, it'll have alpha and then blend with the original |
| 264 |
// image; otherwise the mask filter will apply late (incorrectly) and none of the original |
| 265 |
// image will be visible. |
| 266 |
sk_sp<SkImageFilter> edgeBlend = SkImageFilters::Blend(SkBlendMode::kSrcOver, |
| 267 |
SkImageFilters::Image(image, SkFilterMode::kNearest), edgeDetector); |
| 268 |
|
| 269 |
SkPaint testMaskPaint; |
| 270 |
testMaskPaint.setMaskFilter(maskFilter); |
| 271 |
testMaskPaint.setImageFilter(edgeBlend); |
| 272 |
|
| 273 |
SkPaint expectedMaskPaint; |
| 274 |
expectedMaskPaint.setImageFilter(SkImageFilters::Compose(edgeBlend, |
| 275 |
SkImageFilters::Blend(SkBlendMode::kSrcIn, |
| 276 |
SkImageFilters::Shader(alphaMaskShader)))); |
| 277 |
|
| 278 |
canvas->save(); |
| 279 |
canvas->translate(0, image->height()); |
| 280 |
canvas->clipRect(crop); |
| 281 |
canvas->drawImage(image, 0, 0, SkSamplingOptions(), &expectedMaskPaint); |
| 282 |
canvas->restore(); |
| 283 |
|
| 284 |
canvas->save(); |
| 285 |
canvas->translate(image->width(), image->height()); |
| 286 |
canvas->clipRect(crop); |
| 287 |
canvas->drawImage(image, 0, 0, SkSamplingOptions(), &testMaskPaint); |
| 288 |
canvas->restore(); |
| 289 |
} |
| 290 |
|
| 291 |
DEF_SIMPLE_GM(multiple_filters, canvas, 415, 210) { |
| 292 |
ToolUtils::draw_checkerboard(canvas); |
| 293 |
canvas->translate(5, 5); |
| 294 |
|
| 295 |
auto drawFilteredLayer = [=](SkCanvas::FilterSpan filters) { |
| 296 |
SkPaint restorePaint; |
| 297 |
restorePaint.setAlphaf(0.5f); |
| 298 |
SkCanvas::SaveLayerRec rec = SkCanvasPriv::ScaledBackdropLayer( |
| 299 |
/*bounds=*/nullptr, |
| 300 |
&restorePaint, |
| 301 |
/*backdrop=*/nullptr, |
| 302 |
/*backdropScale=*/1, |
| 303 |
/*saveLayerFlags=*/0, |
| 304 |
filters); |
| 305 |
canvas->save(); |
| 306 |
canvas->clipRect({0, 0, 200, 200}); |
| 307 |
canvas->saveLayer(rec); |
| 308 |
|
| 309 |
SkPaint paint; |
| 310 |
paint.setStyle(SkPaint::kStroke_Style); |
| 311 |
paint.setStrokeWidth(20); |
| 312 |
paint.setColor(SK_ColorGREEN); |
| 313 |
canvas->drawCircle(100, 100, 70, paint); |
| 314 |
|
| 315 |
canvas->restore(); |
| 316 |
canvas->restore(); |
| 317 |
canvas->translate(205, 0); |
| 318 |
}; |
| 319 |
|
| 320 |
{ |
| 321 |
// Test with two non-null filters that each change bounds in a different way: |
| 322 |
sk_sp<SkImageFilter> filters[2] = {SkImageFilters::Dilate(5, 5, nullptr), |
| 323 |
SkImageFilters::Erode(5, 5, nullptr)}; |
| 324 |
drawFilteredLayer(filters); |
| 325 |
} |
| 326 |
|
| 327 |
{ |
| 328 |
// Test with one null filter, to more closely mimic the canvas2D layers use-case: |
| 329 |
sk_sp<SkImageFilter> filters[2] = { |
| 330 |
SkImageFilters::DropShadowOnly(7, 7, 5, 5, SK_ColorBLUE, nullptr), nullptr}; |
| 331 |
drawFilteredLayer(filters); |
| 332 |
} |
| 333 |
} |