Debian Codegraph @

tools/viewer/FilterBoundsSlide.cpp

raw
1 /*
2 * Copyright 2019 Google LLC
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 "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPathBuilder.h"
15 #include "include/core/SkPathEffect.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/effects/SkDashPathEffect.h"
19 #include "include/effects/SkGradient.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "src/core/SkImageFilterTypes.h"
22 #include "src/core/SkImageFilter_Base.h"
23 #include "src/core/SkMatrixPriv.h"
24 #include "src/core/SkRectPriv.h"
25 #include "tools/ToolUtils.h"
26 #include "tools/fonts/FontToolUtils.h"
27 #include "tools/viewer/Slide.h"
28
29 static constexpr float kLineHeight = 16.f;
30 static constexpr float kLineInset = 8.f;
31
32 static float print_size(SkCanvas* canvas, const char* prefix,
33 std::optional<SkIRect> rect,
34 float x, float y, const SkFont& font, const SkPaint& paint) {
35 canvas->drawString(prefix, x, y, font, paint);
36 y += kLineHeight;
37 SkString sz;
38 if (rect) {
39 sz.appendf("%d x %d", rect->width(), rect->height());
40 } else {
41 sz.appendf("infinite");
42 }
43 canvas->drawString(sz, x, y, font, paint);
44 return y + kLineHeight;
45 }
46
47 static float print_info(SkCanvas* canvas,
48 const skif::LayerSpace<SkIRect>& layerContentBounds,
49 const skif::DeviceSpace<SkIRect>& outputBounds,
50 std::optional<skif::DeviceSpace<SkIRect>> hintedOutputBounds,
51 const skif::LayerSpace<SkIRect>& unhintedLayerBounds) {
52 SkFont font(ToolUtils::DefaultTypeface(), 12);
53 SkPaint text;
54 text.setAntiAlias(true);
55
56 float y = kLineHeight;
57
58 text.setColor(SK_ColorRED);
59 y = print_size(canvas, "Content (in layer)", SkIRect(layerContentBounds),
60 kLineInset, y, font, text);
61 text.setColor(SK_ColorDKGRAY);
62 y = print_size(canvas, "Target (in device)", SkIRect(outputBounds),
63 kLineInset, y, font, text);
64 text.setColor(SK_ColorBLUE);
65 y = print_size(canvas, "Output (w/ hint)",
66 hintedOutputBounds ? SkIRect(*hintedOutputBounds) : std::optional<SkIRect>{},
67 kLineInset, y, font, text);
68 text.setColor(SK_ColorGREEN);
69 y = print_size(canvas, "Input (w/ no hint)", SkIRect(unhintedLayerBounds),
70 kLineInset, y, font, text);
71
72 return y;
73 }
74
75 static void print_label(SkCanvas* canvas, float x, float y, float value) {
76 SkFont font(ToolUtils::DefaultTypeface(), 12);
77 SkPaint text;
78 text.setAntiAlias(true);
79
80 SkString label;
81 label.printf("%.3f", value);
82
83 canvas->drawString(label, x, y + kLineHeight / 2.f, font, text);
84 }
85
86 static SkPaint line_paint(SkColor color, bool dashed = false) {
87 SkPaint paint;
88 paint.setColor(color);
89 paint.setStrokeWidth(0.f);
90 paint.setStyle(SkPaint::kStroke_Style);
91 paint.setAntiAlias(true);
92 if (dashed) {
93 SkScalar dash[2] = {10.f, 10.f};
94 paint.setPathEffect(SkDashPathEffect::Make(dash, 0.f));
95 }
96 return paint;
97 }
98
99 static SkPath create_axis_path(const SkRect& rect, float axisSpace) {
100 SkPathBuilder localSpace;
101 for (float y = rect.fTop + axisSpace; y <= rect.fBottom; y += axisSpace) {
102 localSpace.moveTo(rect.fLeft, y);
103 localSpace.lineTo(rect.fRight, y);
104 }
105 for (float x = rect.fLeft + axisSpace; x <= rect.fRight; x += axisSpace) {
106 localSpace.moveTo(x, rect.fTop);
107 localSpace.lineTo(x, rect.fBottom);
108 }
109 return localSpace.detach();
110 }
111
112 static const SkColor4f kScaleGradientColors[] =
113 { { 0.05f, 0.0f, 6.f, 1.f }, // Severe downscaling, s < 1/8, log(s) < -3
114 { 0.6f, 0.6f, 0.8f, 0.6f }, // Okay downscaling, s < 1/2, log(s) < -1
115 { 1.f, 1.f, 1.f, 0.2f }, // No scaling, s = 1, log(s) = 0
116 { 0.95f, 0.6f, 0.5f, 0.6f }, // Okay upscaling, s > 2, log(s) > 1
117 { 0.8f, 0.1f, 0.f, 1.f } }; // Severe upscaling, s > 8, log(s) > 3
118 static const SkScalar kLogScaleFactors[] = { -3.f, -1.f, 0.f, 1.f, 3.f };
119 static const SkScalar kGradientStops[] = { 0.f, 0.33333f, 0.5f, 0.66667f, 1.f };
120 static const int kStopCount = (int) std::size(kScaleGradientColors);
121
122 static void draw_scale_key(SkCanvas* canvas, float y) {
123 SkRect key = SkRect::MakeXYWH(15.f, y + 30.f, 15.f, 100.f);
124 SkPoint pts[] = {{key.centerX(), key.fTop}, {key.centerX(), key.fBottom}};
125 sk_sp<SkShader> gradient = SkShaders::LinearGradient(
126 pts, {{kScaleGradientColors, kGradientStops, SkTileMode::kClamp},
127 {SkGradient::Interpolation::InPremul::kYes}});
128 SkPaint keyPaint;
129 keyPaint.setShader(gradient);
130 canvas->drawRect(key, keyPaint);
131 for (int i = 0; i < kStopCount; ++i) {
132 print_label(canvas, key.fRight + 5.f, key.fTop + kGradientStops[i] * key.height(),
133 SkScalarPow(2.f, kLogScaleFactors[i]));
134 }
135 }
136
137 static void draw_scale_factors(SkCanvas* canvas, const skif::Mapping& mapping, const SkRect& rect) {
138 SkPoint testPoints[5];
139 testPoints[0] = {rect.centerX(), rect.centerY()};
140 rect.copyToQuad({&testPoints[1], 4});
141 for (int i = 0; i < 5; ++i) {
142 float scale = SkMatrixPriv::DifferentialAreaScale(
143 mapping.layerToDevice().asM33(),
144 SkPoint(mapping.paramToLayer(skif::ParameterSpace<SkPoint>(testPoints[i]))));
145 SkColor4f color = {0.f, 0.f, 0.f, 1.f};
146
147 if (SkIsFinite(scale)) {
148 float logScale = SkScalarLog2(scale);
149 for (int j = 0; j <= kStopCount; ++j) {
150 if (j == kStopCount) {
151 color = kScaleGradientColors[j - 1];
152 break;
153 } else if (kLogScaleFactors[j] >= logScale) {
154 if (j == 0) {
155 color = kScaleGradientColors[0];
156 } else {
157 SkScalar t = (logScale - kLogScaleFactors[j - 1]) /
158 (kLogScaleFactors[j] - kLogScaleFactors[j - 1]);
159
160 SkColor4f a = kScaleGradientColors[j - 1] * (1.f - t);
161 SkColor4f b = kScaleGradientColors[j] * t;
162 color = {a.fR + b.fR, a.fG + b.fG, a.fB + b.fB, a.fA + b.fA};
163 }
164 break;
165 }
166 }
167 }
168
169 SkPaint p;
170 p.setAntiAlias(true);
171 p.setColor4f(color, nullptr);
172 canvas->drawRect(SkRect::MakeLTRB(testPoints[i].fX - 4.f, testPoints[i].fY - 4.f,
173 testPoints[i].fX + 4.f, testPoints[i].fY + 4.f), p);
174 }
175 }
176
177 class FilterBoundsSample : public Slide {
178 public:
179 FilterBoundsSample() { fName = "FilterBounds"; }
180
181 void load(SkScalar w, SkScalar h) override {
182 fBlur = SkImageFilters::Blur(8.f, 8.f, nullptr);
183 fImage = ToolUtils::create_checkerboard_image(
184 300, 300, SK_ColorMAGENTA, SK_ColorLTGRAY, 50);
185 }
186
187 void draw(SkCanvas* canvas) override {
188 // The local content, e.g. what would be submitted to drawRect or the bounds to saveLayer
189 const SkRect localContentRect = SkRect::MakeLTRB(100.f, 20.f, 180.f, 140.f);
190 SkM44 ctm = canvas->getLocalToDevice();
191
192 // Base rendering of a filter
193 SkPaint blurPaint;
194 blurPaint.setImageFilter(fBlur);
195 canvas->saveLayer(&localContentRect, &blurPaint);
196 canvas->drawImageRect(fImage.get(), localContentRect, localContentRect,
197 SkSamplingOptions(SkFilterMode::kLinear),
198 nullptr, SkCanvas::kFast_SrcRectConstraint);
199 canvas->restore();
200
201 // Now visualize the underlying bounds calculations used to determine the layer for the blur
202 SkIRect target = SkMatrixPriv::MapRect(ctm, localContentRect).roundOut();
203 if (!target.intersect(SkIRect::MakeWH(canvas->imageInfo().width(),
204 canvas->imageInfo().height()))) {
205 return;
206 }
207 skif::DeviceSpace<SkIRect> targetOutput(target);
208 skif::ParameterSpace<SkRect> contentBounds(localContentRect);
209 skif::ParameterSpace<SkPoint> contentCenter({localContentRect.centerX(),
210 localContentRect.centerY()});
211 skif::Mapping mapping;
212 SkAssertResult(mapping.decomposeCTM(ctm, fBlur.get(), contentCenter));
213
214 // Add axis lines, to show perspective distortion
215 canvas->save();
216 canvas->setMatrix(mapping.layerToDevice());
217 canvas->drawPath(create_axis_path(SkRect(mapping.paramToLayer(contentBounds)), 20.f),
218 line_paint(SK_ColorGRAY));
219 canvas->restore();
220
221 // Visualize scale factors at the four corners and center of the local rect
222 draw_scale_factors(canvas, mapping, localContentRect);
223
224 // The device content rect, e.g. the clip bounds if 'localContentRect' were used as a clip
225 // before the draw or saveLayer, representing what the filter must cover if it affects
226 // transparent black or doesn't have a local content hint.
227 canvas->setMatrix(SkMatrix::I());
228 canvas->drawRect(SkMatrixPriv::MapRect(ctm, localContentRect), line_paint(SK_ColorDKGRAY));
229
230 // Layer bounds for the filter, in the layer space compatible with the filter's matrix
231 // type requirements.
232 skif::LayerSpace<SkIRect> targetOutputInLayer = mapping.deviceToLayer(targetOutput);
233 skif::LayerSpace<SkIRect> hintedLayerBounds = as_IFB(fBlur)->getInputBounds(
234 mapping, targetOutput, contentBounds);
235 skif::LayerSpace<SkIRect> unhintedLayerBounds = as_IFB(fBlur)->getInputBounds(
236 mapping, targetOutput, {});
237
238 canvas->setMatrix(mapping.layerToDevice());
239 canvas->drawRect(SkRect::Make(SkIRect(targetOutputInLayer)),
240 line_paint(SK_ColorDKGRAY, true));
241 canvas->drawRect(SkRect::Make(SkIRect(hintedLayerBounds)), line_paint(SK_ColorRED));
242 canvas->drawRect(SkRect::Make(SkIRect(unhintedLayerBounds)), line_paint(SK_ColorGREEN));
243
244 // For visualization purposes, we want to show the layer-space output, this is what we get
245 // when contentBounds is provided as a hint in local/parameter space.
246 skif::Mapping layerOnly{mapping.layerMatrix()};
247 std::optional<skif::DeviceSpace<SkIRect>> hintedOutputBounds =
248 as_IFB(fBlur)->getOutputBounds(layerOnly, contentBounds);
249 if (hintedOutputBounds) {
250 canvas->drawRect(SkRect::Make(SkIRect(*hintedOutputBounds)), line_paint(SK_ColorBLUE));
251 }
252
253 canvas->resetMatrix();
254 float y = print_info(canvas,
255 mapping.paramToLayer(contentBounds).roundOut(),
256 targetOutput,
257 hintedOutputBounds,
258 unhintedLayerBounds);
259
260 // Draw color key for layer visualization
261 draw_scale_key(canvas, y);
262 }
263
264 private:
265 sk_sp<SkImageFilter> fBlur;
266 sk_sp<SkImage> fImage;
267 };
268
269 DEF_SLIDE(return new FilterBoundsSample();)