| 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/SkCanvas.h" |
| 9 |
#include "include/core/SkColor.h" |
| 10 |
#include "include/core/SkColorFilter.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkImageFilter.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkPaint.h" |
| 16 |
#include "include/core/SkPathEffect.h" |
| 17 |
#include "include/core/SkPoint.h" |
| 18 |
#include "include/core/SkRect.h" |
| 19 |
#include "include/core/SkSurface.h" |
| 20 |
#include "include/effects/SkDashPathEffect.h" |
| 21 |
#include "include/effects/SkGradient.h" |
| 22 |
#include "include/effects/SkImageFilters.h" |
| 23 |
#include "src/core/SkImageFilter_Base.h" |
| 24 |
#include "src/core/SkMatrixPriv.h" |
| 25 |
#include "src/core/SkSpecialImage.h" |
| 26 |
#include "tools/ToolUtils.h" |
| 27 |
#include "tools/fonts/FontToolUtils.h" |
| 28 |
#include "tools/viewer/Slide.h" |
| 29 |
|
| 30 |
namespace { |
| 31 |
|
| 32 |
struct FilterNode { |
| 33 |
// Pointer to the actual filter in the DAG, so it still contains its input filters and |
| 34 |
// may be used as an input in an earlier node. Null when this represents the "source" input |
| 35 |
sk_sp<SkImageFilter> fFilter; |
| 36 |
|
| 37 |
// FilterNodes wrapping each of fFilter's inputs. Leaf node when fInputNodes is empty. |
| 38 |
std::vector<FilterNode> fInputNodes; |
| 39 |
|
| 40 |
// Distance from root filter |
| 41 |
int fDepth; |
| 42 |
|
| 43 |
// The source content rect (this is the same for all nodes, but is stored here for convenience) |
| 44 |
skif::ParameterSpace<SkRect> fContent; |
| 45 |
// The mapping for the filter dag (same for all nodes, but stored here for convenience) |
| 46 |
skif::Mapping fMapping; |
| 47 |
|
| 48 |
// Cached reverse bounds using device-space clip bounds (e.g. no local bounds hint passed to |
| 49 |
// saveLayer). This represents the layer calculated in SkCanvas for the filtering. |
| 50 |
skif::LayerSpace<SkIRect> fUnhintedLayerBounds; |
| 51 |
|
| 52 |
// Cached input bounds using the local draw bounds (e.g. saveLayer with a bounds rect, or |
| 53 |
// an auto-layer for a draw with image filter). This represents the layer bounds up to this |
| 54 |
// point of the DAG. |
| 55 |
skif::LayerSpace<SkIRect> fHintedLayerBounds; |
| 56 |
|
| 57 |
// Cached output bounds based on local draw bounds. This represents the output up to this |
| 58 |
// point of the DAG. |
| 59 |
skif::LayerSpace<SkIRect> fOutputBounds; |
| 60 |
|
| 61 |
FilterNode(const SkImageFilter* filter, |
| 62 |
const skif::Mapping& mapping, |
| 63 |
const skif::ParameterSpace<SkRect>& content, |
| 64 |
int depth) |
| 65 |
: fFilter(sk_ref_sp(filter)) |
| 66 |
, fDepth(depth) |
| 67 |
, fContent(content) |
| 68 |
, fMapping(mapping) { |
| 69 |
this->computeInputBounds(); |
| 70 |
this->computeOutputBounds(); |
| 71 |
if (fFilter) { |
| 72 |
fInputNodes.reserve(fFilter->countInputs()); |
| 73 |
for (int i = 0; i < fFilter->countInputs(); ++i) { |
| 74 |
fInputNodes.emplace_back(fFilter->getInput(i), mapping, content, depth + 1); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private: |
| 80 |
void computeOutputBounds() { |
| 81 |
if (fFilter) { |
| 82 |
// For visualization purposes, we want the output bounds in layer space, before it's |
| 83 |
// been transformed to device space. To achieve that, we mock a new mapping with the |
| 84 |
// identity matrix transform. |
| 85 |
skif::Mapping layerOnly{fMapping.layerMatrix()}; |
| 86 |
std::optional<skif::DeviceSpace<SkIRect>> pseudoDeviceBounds = |
| 87 |
as_IFB(fFilter)->getOutputBounds(layerOnly, fContent); |
| 88 |
// Since layerOnly's device matrix is I, this is effectively a cast to layer space |
| 89 |
if (pseudoDeviceBounds) { |
| 90 |
fOutputBounds = layerOnly.deviceToLayer(*pseudoDeviceBounds); |
| 91 |
} else { |
| 92 |
// Skip drawing infinite output bounds |
| 93 |
fOutputBounds = skif::LayerSpace<SkIRect>::Empty(); |
| 94 |
} |
| 95 |
} else { |
| 96 |
fOutputBounds = fMapping.paramToLayer(fContent).roundOut(); |
| 97 |
} |
| 98 |
|
| 99 |
// Fill in children |
| 100 |
for (size_t i = 0; i < fInputNodes.size(); ++i) { |
| 101 |
fInputNodes[i].computeOutputBounds(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void computeInputBounds() { |
| 106 |
// As a proxy for what the base device had, use the content rect mapped to device space |
| 107 |
// (e.g. clipRect() was called with the same coords prior to the draw). |
| 108 |
skif::DeviceSpace<SkIRect> targetOutput(SkMatrixPriv::MapRect(fMapping.totalMatrix(), |
| 109 |
SkRect(fContent)) |
| 110 |
.roundOut()); |
| 111 |
|
| 112 |
if (fFilter) { |
| 113 |
fHintedLayerBounds = as_IFB(fFilter)->getInputBounds(fMapping, targetOutput, fContent); |
| 114 |
fUnhintedLayerBounds = as_IFB(fFilter)->getInputBounds(fMapping, targetOutput, {}); |
| 115 |
} else { |
| 116 |
fHintedLayerBounds = fMapping.paramToLayer(fContent).roundOut(); |
| 117 |
fUnhintedLayerBounds = fMapping.deviceToLayer(targetOutput); |
| 118 |
} |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
} // anonymous namespace |
| 123 |
|
| 124 |
static FilterNode build_dag(const SkM44& ctm, const SkRect& rect, |
| 125 |
const SkImageFilter* rootFilter) { |
| 126 |
// Emulate SkCanvas::internalSaveLayer's decomposition of the CTM. |
| 127 |
skif::ParameterSpace<SkRect> content(rect); |
| 128 |
skif::ParameterSpace<SkPoint> center({rect.centerX(), rect.centerY()}); |
| 129 |
skif::Mapping mapping; |
| 130 |
SkAssertResult(mapping.decomposeCTM(ctm, rootFilter, center)); |
| 131 |
return FilterNode(rootFilter, mapping, content, 0); |
| 132 |
} |
| 133 |
|
| 134 |
static void draw_node(SkCanvas* canvas, const FilterNode& node) { |
| 135 |
canvas->clear(SK_ColorTRANSPARENT); |
| 136 |
|
| 137 |
SkPaint filterPaint; |
| 138 |
filterPaint.setImageFilter(node.fFilter); |
| 139 |
|
| 140 |
SkRect content = SkRect(node.fContent); |
| 141 |
SkPaint paint; |
| 142 |
static const SkColor4f kColors[2] = {SkColors::kGreen, SkColors::kWhite}; |
| 143 |
SkPoint points[2] = { {content.fLeft + 15.f, content.fTop + 15.f}, |
| 144 |
{content.fRight - 15.f, content.fBottom - 15.f} }; |
| 145 |
paint.setShader(SkShaders::LinearGradient(points, {{kColors, {}, SkTileMode::kRepeat}, {}})); |
| 146 |
|
| 147 |
SkPaint line; |
| 148 |
line.setStrokeWidth(0.f); |
| 149 |
line.setStyle(SkPaint::kStroke_Style); |
| 150 |
|
| 151 |
canvas->save(); |
| 152 |
canvas->concat(node.fMapping.layerToDevice()); |
| 153 |
canvas->save(); |
| 154 |
canvas->concat(node.fMapping.layerMatrix()); |
| 155 |
|
| 156 |
canvas->saveLayer(&content, &filterPaint); |
| 157 |
canvas->drawRect(content, paint); |
| 158 |
canvas->restore(); // Completes the image filter |
| 159 |
|
| 160 |
// Draw content-rect bounds |
| 161 |
line.setColor(SK_ColorBLACK); |
| 162 |
canvas->drawRect(content, line); |
| 163 |
|
| 164 |
// Bounding boxes have all been mapped by the layer matrix from local to layer space, so undo |
| 165 |
// the layer matrix, leaving just the device matrix. |
| 166 |
canvas->restore(); |
| 167 |
|
| 168 |
// The hinted bounds of the layer saved for the filtering |
| 169 |
line.setColor(SK_ColorRED); |
| 170 |
canvas->drawRect(SkRect::Make(SkIRect(node.fHintedLayerBounds)).makeOutset(3.f, 3.f), line); |
| 171 |
// The bounds of the layer if there was no local content hint |
| 172 |
line.setColor(SK_ColorGREEN); |
| 173 |
canvas->drawRect(SkRect::Make(SkIRect(node.fUnhintedLayerBounds)).makeOutset(2.f, 2.f), line); |
| 174 |
|
| 175 |
// The output bounds in layer space |
| 176 |
line.setColor(SK_ColorBLUE); |
| 177 |
canvas->drawRect(SkRect::Make(SkIRect(node.fOutputBounds)).makeOutset(1.f, 1.f), line); |
| 178 |
// Device-space bounding box of the output bounds (e.g. what legacy DAG manipulation via |
| 179 |
// MatrixTransform would produce). |
| 180 |
static const SkScalar kDashParams[] = {6.f, 12.f}; |
| 181 |
line.setPathEffect(SkDashPathEffect::Make(kDashParams, 0.f)); |
| 182 |
SkRect devOutputBounds = SkRect::Make(SkIRect(node.fMapping.layerToDevice(node.fOutputBounds))); |
| 183 |
canvas->restore(); // undoes device matrix |
| 184 |
canvas->drawRect(devOutputBounds, line); |
| 185 |
} |
| 186 |
|
| 187 |
static constexpr float kLineHeight = 16.f; |
| 188 |
static constexpr float kLineInset = 8.f; |
| 189 |
|
| 190 |
static float print_matrix(SkCanvas* canvas, const char* prefix, const SkMatrix& matrix, |
| 191 |
float x, float y, const SkFont& font, const SkPaint& paint) { |
| 192 |
canvas->drawString(prefix, x, y, font, paint); |
| 193 |
y += kLineHeight; |
| 194 |
for (int i = 0; i < 3; ++i) { |
| 195 |
SkString row; |
| 196 |
row.appendf("[%.2f %.2f %.2f]", |
| 197 |
matrix.get(i * 3), matrix.get(i * 3 + 1), matrix.get(i * 3 + 2)); |
| 198 |
canvas->drawString(row, x, y, font, paint); |
| 199 |
y += kLineHeight; |
| 200 |
} |
| 201 |
return y; |
| 202 |
} |
| 203 |
|
| 204 |
static float print_size(SkCanvas* canvas, const char* prefix, const SkIRect& rect, |
| 205 |
float x, float y, const SkFont& font, const SkPaint& paint) { |
| 206 |
canvas->drawString(prefix, x, y, font, paint); |
| 207 |
y += kLineHeight; |
| 208 |
SkString sz; |
| 209 |
sz.appendf("%d x %d", rect.width(), rect.height()); |
| 210 |
canvas->drawString(sz, x, y, font, paint); |
| 211 |
return y + kLineHeight; |
| 212 |
} |
| 213 |
|
| 214 |
static float print_info(SkCanvas* canvas, const FilterNode& node) { |
| 215 |
SkFont font(ToolUtils::DefaultTypeface(), 12); |
| 216 |
SkPaint text; |
| 217 |
text.setAntiAlias(true); |
| 218 |
|
| 219 |
float y = kLineHeight; |
| 220 |
if (node.fFilter) { |
| 221 |
canvas->drawString(node.fFilter->getTypeName(), kLineInset, y, font, text); |
| 222 |
y += kLineHeight; |
| 223 |
if (node.fDepth == 0) { |
| 224 |
// The mapping is the same for all nodes, so only print at the root |
| 225 |
y = print_matrix(canvas, |
| 226 |
"Param->Layer", |
| 227 |
node.fMapping.layerMatrix().asM33(), |
| 228 |
kLineInset, |
| 229 |
y, |
| 230 |
font, |
| 231 |
text); |
| 232 |
y = print_matrix(canvas, |
| 233 |
"Layer->Device", |
| 234 |
node.fMapping.layerToDevice().asM33(), |
| 235 |
kLineInset, |
| 236 |
y, |
| 237 |
font, |
| 238 |
text); |
| 239 |
} |
| 240 |
|
| 241 |
y = print_size(canvas, "Layer Size", SkIRect(node.fUnhintedLayerBounds), |
| 242 |
kLineInset, y, font, text); |
| 243 |
y = print_size(canvas, "Layer Size (hinted)", SkIRect(node.fHintedLayerBounds), |
| 244 |
kLineInset, y, font, text); |
| 245 |
} else { |
| 246 |
canvas->drawString("Source Input", kLineInset, y, font, text); |
| 247 |
y += kLineHeight; |
| 248 |
} |
| 249 |
|
| 250 |
return y; |
| 251 |
} |
| 252 |
|
| 253 |
// Returns bottom edge in pixels that the subtree reached in canvas |
| 254 |
static float draw_dag(SkCanvas* canvas, SkSurface* nodeSurface, const FilterNode& node) { |
| 255 |
// First capture the results of the node, into nodeSurface |
| 256 |
draw_node(nodeSurface->getCanvas(), node); |
| 257 |
sk_sp<SkImage> nodeResults = nodeSurface->makeImageSnapshot(); |
| 258 |
|
| 259 |
// Fill in background of the filter node with a checkerboard |
| 260 |
canvas->save(); |
| 261 |
canvas->clipRect(SkRect::MakeWH(nodeResults->width(), nodeResults->height())); |
| 262 |
ToolUtils::draw_checkerboard(canvas, SK_ColorGRAY, SK_ColorLTGRAY, 10); |
| 263 |
canvas->restore(); |
| 264 |
|
| 265 |
// Display filtered results in current canvas' location (assumed CTM is set for this node) |
| 266 |
canvas->drawImage(nodeResults, 0, 0); |
| 267 |
|
| 268 |
SkPaint line; |
| 269 |
line.setAntiAlias(true); |
| 270 |
line.setStyle(SkPaint::kStroke_Style); |
| 271 |
line.setStrokeWidth(3.f); |
| 272 |
|
| 273 |
// Text info |
| 274 |
canvas->save(); |
| 275 |
canvas->translate(0, nodeResults->height()); |
| 276 |
float textHeight = print_info(canvas, node); |
| 277 |
canvas->restore(); |
| 278 |
|
| 279 |
// Border around filtered results + text info |
| 280 |
canvas->drawRect(SkRect::MakeWH(nodeResults->width(), nodeResults->height() + textHeight), |
| 281 |
line); |
| 282 |
|
| 283 |
static const float kPad = 20.f; |
| 284 |
float x = nodeResults->width() + kPad; |
| 285 |
float y = 0; |
| 286 |
for (size_t i = 0; i < node.fInputNodes.size(); ++i) { |
| 287 |
// Line connecting this node to its child |
| 288 |
canvas->drawLine(nodeResults->width(), 0.5f * nodeResults->height(), // right of node |
| 289 |
x, y + 0.5f * nodeResults->height(), line); // left of child |
| 290 |
canvas->save(); |
| 291 |
canvas->translate(x, y); |
| 292 |
y += draw_dag(canvas, nodeSurface, node.fInputNodes[i]); |
| 293 |
canvas->restore(); |
| 294 |
} |
| 295 |
return std::max(y, nodeResults->height() + textHeight + kPad); |
| 296 |
} |
| 297 |
|
| 298 |
static void draw_dag(SkCanvas* canvas, SkImageFilter* filter, |
| 299 |
const SkRect& rect, const SkISize& surfaceSize) { |
| 300 |
// Get the current CTM, which includes all the viewer's UI modifications, which we want to |
| 301 |
// pass into our mock canvases for each DAG node. |
| 302 |
SkM44 ctm = canvas->getLocalToDevice(); |
| 303 |
|
| 304 |
canvas->save(); |
| 305 |
// Reset the matrix so that the DAG layout and instructional text is fixed to the window. |
| 306 |
canvas->resetMatrix(); |
| 307 |
|
| 308 |
// Process the image filter DAG to display intermediate results later on, which will apply the |
| 309 |
// provided CTM during draw_node calls. |
| 310 |
FilterNode dag = build_dag(ctm, rect, filter); |
| 311 |
|
| 312 |
sk_sp<SkSurface> nodeSurface = |
| 313 |
canvas->makeSurface(canvas->imageInfo().makeDimensions(surfaceSize)); |
| 314 |
draw_dag(canvas, nodeSurface.get(), dag); |
| 315 |
|
| 316 |
canvas->restore(); |
| 317 |
} |
| 318 |
|
| 319 |
class ImageFilterDAGSlide : public Slide { |
| 320 |
public: |
| 321 |
ImageFilterDAGSlide() { fName = "ImageFilterDAG"; } |
| 322 |
|
| 323 |
void draw(SkCanvas* canvas) override { |
| 324 |
// Make a large DAG |
| 325 |
// /--- Color Filter <---- Blur <--- Offset |
| 326 |
// Merge < |
| 327 |
// \--- Blur <--- Drop Shadow |
| 328 |
sk_sp<SkImageFilter> drop2 = SkImageFilters::DropShadow( |
| 329 |
10.f, 5.f, 3.f, 3.f, SK_ColorBLACK, nullptr); |
| 330 |
sk_sp<SkImageFilter> blur1 = SkImageFilters::Blur(2.f, 2.f, std::move(drop2)); |
| 331 |
|
| 332 |
sk_sp<SkImageFilter> offset3 = SkImageFilters::Offset(-5.f, -5.f, nullptr); |
| 333 |
sk_sp<SkImageFilter> blur2 = SkImageFilters::Blur(4.f, 4.f, std::move(offset3)); |
| 334 |
sk_sp<SkImageFilter> cf1 = SkImageFilters::ColorFilter( |
| 335 |
SkColorFilters::Blend(SK_ColorGRAY, SkBlendMode::kModulate), std::move(blur2)); |
| 336 |
|
| 337 |
sk_sp<SkImageFilter> merge0 = SkImageFilters::Merge(std::move(blur1), std::move(cf1)); |
| 338 |
|
| 339 |
draw_dag(canvas, merge0.get(), kFilterRect, kFilterSurfaceSize); |
| 340 |
} |
| 341 |
|
| 342 |
// We want to use the viewer calculated CTM in the mini surfaces used per DAG node. The rotation |
| 343 |
// matrix viewer calculates is based on the slide content size. |
| 344 |
SkISize getDimensions() const override { return kFilterSurfaceSize; } |
| 345 |
|
| 346 |
private: |
| 347 |
static constexpr SkRect kFilterRect = SkRect::MakeXYWH(20.f, 20.f, 60.f, 60.f); |
| 348 |
static constexpr SkISize kFilterSurfaceSize = SkISize::Make( |
| 349 |
2 * (kFilterRect.fRight + kFilterRect.fLeft), |
| 350 |
2 * (kFilterRect.fBottom + kFilterRect.fTop)); |
| 351 |
|
| 352 |
}; |
| 353 |
|
| 354 |
DEF_SLIDE(return new ImageFilterDAGSlide();) |