blob: a3f442b5876622d9a2977b82f7a0f032af26575c [file] [log] [blame]
Michael Ludwig7d2ad0f2019-05-03 17:09:47 -04001/*
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 Ludwig898bbfa2019-08-02 15:21:23 -040010#include "include/core/SkColorFilter.h"
Mike Kleinad44dd52019-05-14 14:01:39 -050011#include "include/core/SkPaint.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040012#include "include/effects/SkImageFilters.h"
Michael Ludwig7d2ad0f2019-05-03 17:09:47 -040013
Michael Ludwig898bbfa2019-08-02 15:21:23 -040014typedef sk_sp<SkImageFilter> (*FilterFactory)(const SkIRect* crop);
Michael Ludwig7d2ad0f2019-05-03 17:09:47 -040015
Michael Ludwig08b260c2019-05-17 11:21:53 -040016static void draw_backdrop_filter_gm(SkCanvas* canvas, float outsetX, float outsetY,
17 FilterFactory factory) {
Michael Ludwig7d2ad0f2019-05-03 17:09:47 -040018 // 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 Ludwig898bbfa2019-08-02 15:21:23 -040028 SkIRect cropRect = cropInLocal.makeOutset(outsetX, outsetY).roundOut();
Michael Ludwig08b260c2019-05-17 11:21:53 -040029 sk_sp<SkImageFilter> imageFilter = factory(&cropRect);
Michael Ludwig7d2ad0f2019-05-03 17:09:47 -040030
31 SkPaint p;
Michael Ludwig7d2ad0f2019-05-03 17:09:47 -040032 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.
57 canvas->saveLayer({nullptr, nullptr, imageFilter.get(), nullptr, nullptr,
58 SkCanvas::kInitWithPrevious_SaveLayerFlag});
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 Ludwig08b260c2019-05-17 11:21:53 -040070
Michael Ludwig898bbfa2019-08-02 15:21:23 -040071static sk_sp<SkImageFilter> make_invert_filter(const SkIRect* crop) {
Michael Ludwig08b260c2019-05-17 11:21:53 -040072 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 Ludwig898bbfa2019-08-02 15:21:23 -040076 return SkImageFilters::ColorFilter(SkColorFilters::Matrix(matrix), nullptr, crop);
Michael Ludwig08b260c2019-05-17 11:21:53 -040077}
78
Michael Ludwig898bbfa2019-08-02 15:21:23 -040079static sk_sp<SkImageFilter> make_blur_filter(const SkIRect* crop) {
Michael Ludwig08b260c2019-05-17 11:21:53 -040080 // Use different sigmas for x and y so rotated CTM is apparent
Michael Ludwig898bbfa2019-08-02 15:21:23 -040081 return SkImageFilters::Blur(16.f, 4.f, nullptr, crop);
Michael Ludwig08b260c2019-05-17 11:21:53 -040082}
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.
86DEF_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.
93DEF_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
103DEF_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.
114DEF_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}