2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageFilter.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkPoint3.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkRefCnt.h"
22 #include "include/core/SkRegion.h"
23 #include "include/core/SkScalar.h"
24 #include "include/core/SkSize.h"
25 #include "include/core/SkString.h"
26 #include "include/core/SkSurface.h"
27 #include "include/core/SkTypes.h"
29 #include "include/effects/SkImageFilters.h"
31 #include "include/gpu/GrDirectContext.h"
33 #include "tools/Resources.h"
34 #include "tools/ToolUtils.h"
38 ///////////////////////////////////////////////////////////////////////////////
40 static void show_bounds(SkCanvas* canvas, const SkIRect* clip, const SkIRect* inSubset,
41 const SkIRect* outSubset) {
42 const SkIRect* rects[] { clip, inSubset, outSubset };
43 SkColor colors[] { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorRED };
46 paint.setStyle(SkPaint::kStroke_Style);
48 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
49 // Skip null bounds rects, since not all methods have subsets
51 paint.setColor(colors[i]);
52 canvas->drawRect(SkRect::Make(*(rects[i])), paint);
57 // Factories for creating image filters, either with or without a cropRect
58 // (this could go away if there was a SkImageFilter::makeWithCropRect() function, but that seems
59 // less generally useful).
60 typedef sk_sp<SkImageFilter> (*FilterFactory)(sk_sp<SkImage> auxImage, const SkIRect* cropRect);
62 static sk_sp<SkImageFilter> color_filter_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
63 // The color filter uses kSrcIn so that it respects the transparency introduced by clamping;
64 // using kSrc would just turn the entire out rect to green regardless.
65 auto cf = SkColorFilters::Blend(SK_ColorGREEN, SkBlendMode::kSrcIn);
66 return SkImageFilters::ColorFilter(std::move(cf), nullptr, cropRect);
69 static sk_sp<SkImageFilter> blur_filter_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
70 return SkImageFilters::Blur(2.0f, 2.0f, nullptr, cropRect);
73 static sk_sp<SkImageFilter> drop_shadow_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
74 return SkImageFilters::DropShadow(10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr, cropRect);
77 static sk_sp<SkImageFilter> offset_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
78 return SkImageFilters::Offset(10.f, 5.f, nullptr, cropRect);
81 static sk_sp<SkImageFilter> dilate_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
82 return SkImageFilters::Dilate(10.f, 5.f, nullptr, cropRect);
85 static sk_sp<SkImageFilter> erode_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
86 return SkImageFilters::Erode(10.f, 5.f, nullptr, cropRect);
89 static sk_sp<SkImageFilter> displacement_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
90 sk_sp<SkImageFilter> displacement = SkImageFilters::Image(std::move(auxImage));
91 return SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kG, 40.f,
92 std::move(displacement), nullptr, cropRect);
95 static sk_sp<SkImageFilter> arithmetic_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
96 sk_sp<SkImageFilter> background = SkImageFilters::Image(std::move(auxImage));
97 return SkImageFilters::Arithmetic(0.0f, .6f, 1.f, 0.f, false, std::move(background),
101 static sk_sp<SkImageFilter> blend_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
102 sk_sp<SkImageFilter> background = SkImageFilters::Image(std::move(auxImage));
103 return SkImageFilters::Blend(
104 SkBlendMode::kModulate, std::move(background), nullptr, cropRect);
107 static sk_sp<SkImageFilter> convolution_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
108 SkISize kernelSize = SkISize::Make(3, 3);
109 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
110 // A Laplacian edge detector, ee https://en.wikipedia.org/wiki/Kernel_(image_processing)
111 SkScalar kernel[9] = {-1.f, -1.f, -1.f,
114 return SkImageFilters::MatrixConvolution(kernelSize, kernel, 1.f, 0.f, kernelOffset,
115 SkTileMode::kClamp, false, nullptr, cropRect);
118 static sk_sp<SkImageFilter> matrix_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
119 SkMatrix matrix = SkMatrix::I();
120 matrix.setRotate(45.f, 50.f, 50.f);
122 // This doesn't support a cropRect
123 return SkImageFilters::MatrixTransform(matrix, SkSamplingOptions(SkFilterMode::kLinear), nullptr);
126 static sk_sp<SkImageFilter> alpha_threshold_factory(sk_sp<SkImage> auxImage,
127 const SkIRect* cropRect) {
128 // Centered cross with higher opacity
129 SkRegion region(SkIRect::MakeLTRB(30, 45, 70, 55));
130 region.op(SkIRect::MakeLTRB(45, 30, 55, 70), SkRegion::kUnion_Op);
132 return SkImageFilters::AlphaThreshold(region, 1.f, .2f, nullptr, cropRect);
135 static sk_sp<SkImageFilter> lighting_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
136 // Must convert the RGB values of the source to alpha, since that is what the lighting filters
137 // use to estimate their normals. This color matrix changes the color to white and the alpha
138 // to be equal to the approx. luminance of the original color.
139 static const float kMatrix[20] = {
140 0.f, 0.f, 0.f, 0.f, 1.f,
141 0.f, 0.f, 0.f, 0.f, 1.f,
142 0.f, 0.f, 0.f, 0.f, 1.f,
143 0.2126f, 0.7152f, 0.0722f, 0.f, 0.f
145 sk_sp<SkImageFilter> srcToAlpha = SkImageFilters::ColorFilter(
146 SkColorFilters::Matrix(kMatrix), nullptr);
148 // Combine both specular and diffuse into a single DAG since they use separate internal filter
150 SkScalar sinAzimuth = SkScalarSin(SkDegreesToRadians(225.f)),
151 cosAzimuth = SkScalarCos(SkDegreesToRadians(225.f));
153 SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
154 SkPoint3 diffLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth,
155 spotTarget.fY + 50 * sinAzimuth,
157 SkPoint3 specLocation = SkPoint3::Make(spotTarget.fX - 50 * sinAzimuth,
158 spotTarget.fY + 50 * cosAzimuth,
160 sk_sp<SkImageFilter> diffuse = SkImageFilters::PointLitDiffuse(
161 diffLocation, SK_ColorWHITE, /* scale */ 1.f, /* kd */ 2.f, srcToAlpha, cropRect);
162 sk_sp<SkImageFilter> specular = SkImageFilters::PointLitSpecular(
163 specLocation, SK_ColorRED, /* scale */ 1.f, /* ks */ 1.f, /* shine */ 8.f,
164 srcToAlpha, cropRect);
165 return SkImageFilters::Merge(std::move(diffuse), std::move(specular), cropRect);
168 static sk_sp<SkImageFilter> tile_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
169 // Tile the subset over a large region
170 return SkImageFilters::Tile(SkRect::MakeLTRB(25, 25, 75, 75), SkRect::MakeWH(100, 100),
175 enum class Strategy {
176 // Uses makeWithFilter, passing in subset and clip directly
178 // Uses saveLayer after clipRect() to filter on the restore (i.e. reference image)
183 // In this GM, we're going to feed the inner portion of a 100x100 mandrill (i.e., strip off a
184 // 25-wide border) through the makeWithFilter method. We'll then draw the appropriate subset of the
185 // result to the screen at the given offset. Some filters rely on a secondary image, which will be a
186 // 100x100 checkerboard. The original image is drawn in the background so that alignment is clear
187 // when drawing the result at its reported offset.
188 class ImageMakeWithFilterGM : public skiagm::GM {
190 ImageMakeWithFilterGM (Strategy strategy, bool filterWithCropRect = false)
191 : fStrategy(strategy)
192 , fFilterWithCropRect(filterWithCropRect)
193 , fMainImage(nullptr)
194 , fAuxImage(nullptr) {}
197 SkString onShortName() override {
198 SkString name = SkString("imagemakewithfilter");
200 if (fFilterWithCropRect) {
201 name.append("_crop");
203 if (fStrategy == Strategy::kSaveLayer) {
209 SkISize onISize() override { return SkISize::Make(1980, 860); }
211 void onOnceBeforeDraw() override {
212 SkImageInfo info = SkImageInfo::MakeN32(100, 100, kUnpremul_SkAlphaType);
213 auto surface = SkSurface::MakeRaster(info, nullptr);
215 sk_sp<SkImage> colorImage = GetResourceAsImage("images/mandrill_128.png");
217 surface->getCanvas()->drawImageRect(
218 colorImage, SkRect::MakeWH(colorImage->width(), colorImage->height()),
219 SkRect::MakeWH(info.width(), info.height()), SkSamplingOptions(), nullptr,
220 SkCanvas::kStrict_SrcRectConstraint);
221 fMainImage = surface->makeImageSnapshot();
223 ToolUtils::draw_checkerboard(surface->getCanvas());
224 fAuxImage = surface->makeImageSnapshot();
227 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
228 FilterFactory filters[] = {
229 color_filter_factory,
235 displacement_factory,
240 alpha_threshold_factory,
244 const char* filterNames[] = {
253 "Xfer Mode", // "blend"
260 static_assert(SK_ARRAY_COUNT(filters) == SK_ARRAY_COUNT(filterNames), "filter name length");
262 SkIRect clipBounds[] {
263 { -20, -20, 100, 100 },
265 { 20, 20, 100, 100 },
266 { -20, -20, 50, 50 },
271 auto rContext = canvas->recordingContext();
272 // In a DDL context, we can't use the GPU code paths and we will drop the work – skip.
273 auto dContext = GrAsDirectContext(rContext);
276 *errorMsg = "Requires a direct context.";
277 return DrawResult::kSkip;
279 if (dContext->abandoned()) {
280 *errorMsg = "Direct context abandoned.";
281 return DrawResult::kSkip;
285 // These need to be GPU-backed when on the GPU to ensure that the image filters use the GPU
286 // code paths (otherwise they may choose to do CPU filtering then upload)
287 sk_sp<SkImage> mainImage = ToolUtils::MakeTextureImage(canvas, fMainImage);
288 sk_sp<SkImage> auxImage = ToolUtils::MakeTextureImage(canvas, fAuxImage);
289 if (!mainImage || !auxImage) {
290 return DrawResult::kFail;
292 SkASSERT(mainImage && (mainImage->isTextureBacked() || !dContext));
293 SkASSERT(auxImage && (auxImage->isTextureBacked() || !dContext));
295 SkScalar MARGIN = SkIntToScalar(40);
296 SkScalar DX = mainImage->width() + MARGIN;
297 SkScalar DY = auxImage->height() + MARGIN;
299 // Header hinting at what the filters do
301 textPaint.setAntiAlias(true);
302 SkFont font(nullptr, 12);
303 for (size_t i = 0; i < SK_ARRAY_COUNT(filterNames); ++i) {
304 canvas->drawString(filterNames[i], DX * i + MARGIN, 15, font, textPaint);
307 canvas->translate(MARGIN, MARGIN);
309 for (auto clipBound : clipBounds) {
311 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
312 SkIRect subset = SkIRect::MakeXYWH(25, 25, 50, 50);
315 // Draw the original image faintly so that it aids in checking alignment of the
318 alpha.setAlphaf(0.3f);
319 canvas->drawImage(mainImage, 0, 0, SkSamplingOptions(), &alpha);
321 this->drawImageWithFilter(canvas, mainImage, auxImage, filters[i], clipBound,
324 // Draw outlines to highlight what was subset, what was cropped, and what was output
325 // (no output subset is displayed for kSaveLayer since that information isn't avail)
326 SkIRect* outSubsetBounds = nullptr;
327 if (fStrategy != Strategy::kSaveLayer) {
328 outSubsetBounds = &outSubset;
330 show_bounds(canvas, &clipBound, &subset, outSubsetBounds);
332 canvas->translate(DX, 0);
335 canvas->translate(0, DY);
337 return DrawResult::kOk;
342 bool fFilterWithCropRect;
343 sk_sp<SkImage> fMainImage;
344 sk_sp<SkImage> fAuxImage;
346 void drawImageWithFilter(SkCanvas* canvas, sk_sp<SkImage> mainImage, sk_sp<SkImage> auxImage,
347 FilterFactory filterFactory, const SkIRect& clip,
348 const SkIRect& subset, SkIRect* dstRect) {
349 // When creating the filter with a crop rect equal to the clip, we should expect to see no
350 // difference from a filter without a crop rect. However, if the CTM isn't managed properly
351 // by makeWithFilter, then the final result will be the incorrect intersection of the clip
352 // and the transformed crop rect.
353 sk_sp<SkImageFilter> filter = filterFactory(auxImage,
354 fFilterWithCropRect ? &clip : nullptr);
356 if (fStrategy == Strategy::kSaveLayer) {
357 SkAutoCanvasRestore acr(canvas, true);
359 // Clip before the saveLayer with the filter
360 canvas->clipRect(SkRect::Make(clip));
362 // Put the image filter on the layer
364 paint.setImageFilter(filter);
365 canvas->saveLayer(nullptr, &paint);
367 // Draw the original subset of the image
368 SkRect r = SkRect::Make(subset);
369 canvas->drawImageRect(mainImage, r, r, SkSamplingOptions(),
370 nullptr, SkCanvas::kStrict_SrcRectConstraint);
374 sk_sp<SkImage> result;
378 auto rContext = canvas->recordingContext();
379 result = mainImage->makeWithFilter(rContext, filter.get(), subset, clip,
380 &outSubset, &offset);
385 SkASSERT(mainImage->isTextureBacked() == result->isTextureBacked());
387 *dstRect = SkIRect::MakeXYWH(offset.x(), offset.y(),
388 outSubset.width(), outSubset.height());
389 canvas->drawImageRect(result, SkRect::Make(outSubset), SkRect::Make(*dstRect),
390 SkSamplingOptions(), nullptr,
391 SkCanvas::kStrict_SrcRectConstraint);
395 using INHERITED = GM;
397 // The different strategies should all look the same, with the exception of filters that affect
398 // transparent black (i.e. the lighting filter). In the save layer case, the filter affects the
399 // transparent pixels outside of the drawn subset, whereas the makeWithFilter is restricted. This
400 // works as intended.
401 DEF_GM( return new ImageMakeWithFilterGM(Strategy::kMakeWithFilter); )
402 DEF_GM( return new ImageMakeWithFilterGM(Strategy::kSaveLayer); )
403 // Test with crop rects on the image filters; should look identical to above if working correctly
404 DEF_GM( return new ImageMakeWithFilterGM(Strategy::kMakeWithFilter, true); )
405 DEF_GM( return new ImageMakeWithFilterGM(Strategy::kSaveLayer, true); )