Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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/SkCanvas.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 10 | #include "include/core/SkColorFilter.h" |
Mike Klein | ad44dd5 | 2019-05-14 14:01:39 -0500 | [diff] [blame] | 11 | #include "include/core/SkPaint.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 12 | #include "include/effects/SkImageFilters.h" |
Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 13 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 14 | typedef sk_sp<SkImageFilter> (*FilterFactory)(const SkIRect* crop); |
Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 15 | |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 16 | static void draw_backdrop_filter_gm(SkCanvas* canvas, float outsetX, float outsetY, |
| 17 | FilterFactory factory) { |
Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 18 | // CTM translates to (150, 150) |
| 19 | SkPoint origin = SkPoint::Make(150.f, 150.f); |
| 20 | // The save layer specified after the CTM has negative coordinates, but |
| 21 | // means that (100, 100) to (500, 250) in device-space will be saved |
| 22 | SkRect clip = SkRect::MakeXYWH(-50.f, -50.f, 400.f, 150.f); |
| 23 | // The image-filter crop relative to the CTM, which will map to |
| 24 | // (200, 160) to (400, 190) in device space, or (100, 600) to (300, 90) in |
| 25 | // the layer's image space. |
| 26 | SkRect cropInLocal = SkRect::MakeLTRB(50.f, 10.f, 250.f, 40.f); |
| 27 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 28 | SkIRect cropRect = cropInLocal.makeOutset(outsetX, outsetY).roundOut(); |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 29 | sk_sp<SkImageFilter> imageFilter = factory(&cropRect); |
Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 30 | |
| 31 | SkPaint p; |
Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 32 | for (int i = 0; i < 2; ++i) { |
| 33 | canvas->save(); |
| 34 | canvas->translate(origin.fX, origin.fY); |
| 35 | |
| 36 | canvas->clipRect(clip); |
| 37 | |
| 38 | if (i == 0) { |
| 39 | // Primary save layer mode, so save layer before drawing the content |
| 40 | SkPaint imfPaint; |
| 41 | imfPaint.setImageFilter(imageFilter); |
| 42 | canvas->saveLayer(nullptr, &imfPaint); |
| 43 | } // else backdrop mode, so the content is drawn first |
| 44 | |
| 45 | // Fill the clip with one color (cyan for i == 0 (inverse = red), and |
| 46 | // magenta for i == 1 (inverse = green)) |
| 47 | p.setColor(i == 0 ? SK_ColorCYAN : SK_ColorMAGENTA); |
| 48 | canvas->drawPaint(p); |
| 49 | |
| 50 | // Then an inner rectangle with a color meant to be inverted by the image filter |
| 51 | p.setColor(i == 0 ? SK_ColorRED : SK_ColorGREEN); |
| 52 | canvas->drawRect(cropInLocal, p); |
| 53 | |
| 54 | if (i == 1) { |
| 55 | // Backdrop mode, so save a layer using the image filter as the backdrop to filter |
| 56 | // content on initialization. |
Mike Reed | b42a327 | 2020-05-30 16:02:14 -0400 | [diff] [blame] | 57 | canvas->saveLayer(SkCanvas::SaveLayerRec(nullptr, nullptr, imageFilter.get(), |
| 58 | SkCanvas::kInitWithPrevious_SaveLayerFlag)); |
Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Restore the saved layer (either a main layer that was just drawn into and needs to be |
| 62 | // filtered, or an "empty" layer initialized with the previously filtered backdrop) |
| 63 | canvas->restore(); |
| 64 | |
| 65 | // Move down |
| 66 | canvas->restore(); |
| 67 | origin.fY += 150.f; |
| 68 | } |
| 69 | } |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 70 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 71 | static sk_sp<SkImageFilter> make_invert_filter(const SkIRect* crop) { |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 72 | static const float matrix[20] = {-1.f, 0.f, 0.f, 0.f, 1.f, |
| 73 | 0.f, -1.f, 0.f, 0.f, 1.f, |
| 74 | 0.f, 0.f, -1.f, 0.f, 1.f, |
| 75 | 0.f, 0.f, 0.f, 1.f, 0.f}; |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 76 | return SkImageFilters::ColorFilter(SkColorFilters::Matrix(matrix), nullptr, crop); |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 79 | static sk_sp<SkImageFilter> make_blur_filter(const SkIRect* crop) { |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 80 | // Use different sigmas for x and y so rotated CTM is apparent |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 81 | return SkImageFilters::Blur(16.f, 4.f, nullptr, crop); |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // This draws correctly if there's a small cyan rectangle above a much larger magenta rectangle. |
| 85 | // There should be no red around the cyan rectangle and no green within the magenta rectangle. |
| 86 | DEF_SIMPLE_GM(backdrop_imagefilter_croprect, canvas, 600, 500) { |
| 87 | draw_backdrop_filter_gm(canvas, 0.f, 0.f, make_invert_filter); |
| 88 | } |
| 89 | |
| 90 | // This draws correctly if there's a blurred red rectangle inside a cyan rectangle, above a blurred |
| 91 | // green rectangle inside a larger magenta rectangle. All rectangles and the blur direction are |
| 92 | // consistently rotated. |
| 93 | DEF_SIMPLE_GM(backdrop_imagefilter_croprect_rotated, canvas, 600, 500) { |
| 94 | canvas->translate(140.f, -180.f); |
| 95 | canvas->rotate(30.f); |
| 96 | draw_backdrop_filter_gm(canvas, 32.f, 32.f, make_blur_filter); |
| 97 | } |
| 98 | |
| 99 | // This draws correctly if there's a blurred red rectangle inside a cyan rectangle, above a blurred |
| 100 | // green rectangle inside a larger magenta rectangle. All rectangles and the blur direction are |
| 101 | // under consistent perspective. |
| 102 | // NOTE: Currently renders incorrectly, see skbug.com/9074 |
| 103 | DEF_SIMPLE_GM(backdrop_imagefilter_croprect_persp, canvas, 600, 500) { |
| 104 | SkMatrix persp = SkMatrix::I(); |
| 105 | persp.setPerspY(0.001f); |
| 106 | persp.setSkewX(8.f / 25.f); |
| 107 | canvas->concat(persp); |
| 108 | draw_backdrop_filter_gm(canvas, 32.f, 32.f, make_blur_filter); |
| 109 | } |
| 110 | |
| 111 | // This draws correctly if there's a small cyan rectangle above a much larger magenta rectangle. |
| 112 | // There should be no red around the cyan rectangle and no green within the magenta rectangle, and |
| 113 | // everything should be 50% transparent. |
| 114 | DEF_SIMPLE_GM(backdrop_imagefilter_croprect_nested, canvas, 600, 500) { |
| 115 | SkPaint p; |
| 116 | p.setAlphaf(0.5f); |
| 117 | // This ensures there is a non-root device on the stack with a non-zero origin. |
| 118 | canvas->translate(15.f, 10.f); |
| 119 | canvas->clipRect(SkRect::MakeWH(600.f, 500.f)); |
| 120 | |
| 121 | canvas->saveLayer(nullptr, &p); |
| 122 | draw_backdrop_filter_gm(canvas, 0.f, 0.f, make_invert_filter); |
| 123 | canvas->restore(); |
| 124 | } |