| 1 |
/* |
| 2 |
* Copyright 2021 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 "gm/gm.h" |
| 9 |
|
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkImageFilter.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPathEffect.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkSurface.h" |
| 18 |
#include "include/effects/SkDashPathEffect.h" |
| 19 |
#include "include/effects/SkGradient.h" |
| 20 |
#include "include/effects/SkImageFilters.h" |
| 21 |
#include "tools/GpuToolUtils.h" |
| 22 |
#include "tools/Resources.h" |
| 23 |
#include "tools/ToolUtils.h" |
| 24 |
|
| 25 |
|
| 26 |
namespace { |
| 27 |
|
| 28 |
static constexpr SkColor kOutputBoundsColor = SK_ColorRED; |
| 29 |
static constexpr SkColor kCropRectColor = SK_ColorGREEN; |
| 30 |
static constexpr SkColor kContentBoundsColor = SK_ColorBLUE; |
| 31 |
|
| 32 |
static constexpr SkRect kExampleBounds = {0.f, 0.f, 100.f, 100.f}; |
| 33 |
|
| 34 |
// "Crop" refers to the rect passed to the crop image filter, "Rect" refers to some other rect |
| 35 |
// from context, likely the output bounds or the content bounds. |
| 36 |
enum class CropRelation { |
| 37 |
kCropOverlapsRect, // Intersect but doesn't fully contain one way or the other |
| 38 |
kCropContainsRect, |
| 39 |
kRectContainsCrop, |
| 40 |
kCropRectDisjoint, |
| 41 |
}; |
| 42 |
|
| 43 |
SkRect make_overlap(const SkRect& r, float amountX, float amountY) { |
| 44 |
return r.makeOffset(r.width() * amountX, r.height() * amountY); |
| 45 |
} |
| 46 |
|
| 47 |
SkRect make_inset(const SkRect& r, float amountX, float amountY) { |
| 48 |
return r.makeInset(r.width() * amountX, r.height() * amountY); |
| 49 |
} |
| 50 |
|
| 51 |
SkRect make_outset(const SkRect& r, float amountX, float amountY) { |
| 52 |
return r.makeOutset(r.width() * amountX, r.height() * amountY); |
| 53 |
} |
| 54 |
|
| 55 |
SkRect make_disjoint(const SkRect& r, float amountX, float amountY) { |
| 56 |
float xOffset = (amountX > 0.f ? (r.width() + r.width() * amountX) : |
| 57 |
(amountX < 0.f ? (-r.width() + r.width() * amountX) : 0.f)); |
| 58 |
float yOffset = (amountY > 0.f ? (r.height() + r.height() * amountY) : |
| 59 |
(amountY < 0.f ? (-r.height() + r.height() * amountY) : 0.f)); |
| 60 |
return r.makeOffset(xOffset, yOffset); |
| 61 |
} |
| 62 |
|
| 63 |
void get_example_rects(CropRelation outputRelation, CropRelation inputRelation, bool hintContent, |
| 64 |
SkRect* outputBounds, SkRect* cropRect, SkRect* contentBounds) { |
| 65 |
*outputBounds = kExampleBounds.makeInset(20.f, 20.f); |
| 66 |
switch(outputRelation) { |
| 67 |
case CropRelation::kCropOverlapsRect: |
| 68 |
*cropRect = make_overlap(*outputBounds, -0.15f, 0.15f); |
| 69 |
SkASSERT(cropRect->intersects(*outputBounds) && |
| 70 |
!cropRect->contains(*outputBounds) && |
| 71 |
!outputBounds->contains(*cropRect)); |
| 72 |
break; |
| 73 |
case CropRelation::kCropContainsRect: |
| 74 |
*cropRect = make_outset(*outputBounds, 0.15f, 0.15f); |
| 75 |
SkASSERT(cropRect->contains(*outputBounds)); |
| 76 |
break; |
| 77 |
case CropRelation::kRectContainsCrop: |
| 78 |
*cropRect = make_inset(*outputBounds, 0.15f, 0.15f); |
| 79 |
SkASSERT(outputBounds->contains(*cropRect)); |
| 80 |
break; |
| 81 |
case CropRelation::kCropRectDisjoint: |
| 82 |
*cropRect = make_disjoint(*outputBounds, 0.15f, 0.0f); |
| 83 |
SkASSERT(!cropRect->intersects(*outputBounds)); |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
SkAssertResult(cropRect->intersect(kExampleBounds)); |
| 88 |
|
| 89 |
// Determine content bounds for example based on computed crop rect and input relation |
| 90 |
if (hintContent) { |
| 91 |
switch(inputRelation) { |
| 92 |
case CropRelation::kCropOverlapsRect: |
| 93 |
*contentBounds = make_overlap(*cropRect, 0.075f, -0.75f); |
| 94 |
SkASSERT(contentBounds->intersects(*cropRect) && |
| 95 |
!contentBounds->contains(*cropRect) && |
| 96 |
!cropRect->contains(*contentBounds)); |
| 97 |
break; |
| 98 |
case CropRelation::kCropContainsRect: |
| 99 |
*contentBounds = make_inset(*cropRect, 0.075f, 0.075f); |
| 100 |
SkASSERT(cropRect->contains(*contentBounds)); |
| 101 |
break; |
| 102 |
case CropRelation::kRectContainsCrop: |
| 103 |
*contentBounds = make_outset(*cropRect, 0.1f, 0.1f); |
| 104 |
SkASSERT(contentBounds->contains(*cropRect)); |
| 105 |
break; |
| 106 |
case CropRelation::kCropRectDisjoint: |
| 107 |
*contentBounds = make_disjoint(*cropRect, 0.0f, 0.075f); |
| 108 |
SkASSERT(!contentBounds->intersects(*cropRect)); |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
SkAssertResult(contentBounds->intersect(kExampleBounds)); |
| 113 |
} else { |
| 114 |
*contentBounds = kExampleBounds; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// TODO(michaelludwig) - This is a useful test pattern for tile modes and filtering; should |
| 119 |
// consolidate it with the similar version in gpu_blur_utils if the GMs remain separate at the end. |
| 120 |
sk_sp<SkImage> make_image(SkCanvas* canvas, const SkRect* contentBounds) { |
| 121 |
const float w = kExampleBounds.width(); |
| 122 |
const float h = kExampleBounds.height(); |
| 123 |
|
| 124 |
const auto srcII = SkImageInfo::Make(SkISize::Make(SkScalarCeilToInt(w), SkScalarCeilToInt(h)), |
| 125 |
kN32_SkColorType, kPremul_SkAlphaType); |
| 126 |
auto surf = SkSurfaces::Raster(srcII); |
| 127 |
|
| 128 |
surf->getCanvas()->drawColor(SK_ColorDKGRAY); |
| 129 |
SkPaint paint; |
| 130 |
paint.setAntiAlias(true); |
| 131 |
paint.setStyle(SkPaint::kStroke_Style); |
| 132 |
// Draw four horizontal lines at 1/4, 3/8, 5/8, 3/4. |
| 133 |
paint.setStrokeWidth(h/16.f); |
| 134 |
paint.setColor(SK_ColorRED); |
| 135 |
surf->getCanvas()->drawLine({0.f, 1.f*h/4.f}, {w, 1.f*h/4.f}, paint); |
| 136 |
paint.setColor(/* sea foam */ 0xFF71EEB8); |
| 137 |
surf->getCanvas()->drawLine({0.f, 3.f*h/8.f}, {w, 3.f*h/8.f}, paint); |
| 138 |
paint.setColor(SK_ColorYELLOW); |
| 139 |
surf->getCanvas()->drawLine({0.f, 5.f*h/8.f}, {w, 5.f*h/8.f}, paint); |
| 140 |
paint.setColor(SK_ColorCYAN); |
| 141 |
surf->getCanvas()->drawLine({0.f, 3.f*h/4.f}, {w, 3.f*h/4.f}, paint); |
| 142 |
|
| 143 |
// Draw four vertical lines at 1/4, 3/8, 5/8, 3/4. |
| 144 |
paint.setStrokeWidth(w/16.f); |
| 145 |
paint.setColor(/* orange */ 0xFFFFA500); |
| 146 |
surf->getCanvas()->drawLine({1.f*w/4.f, 0.f}, {1.f*h/4.f, h}, paint); |
| 147 |
paint.setColor(SK_ColorBLUE); |
| 148 |
surf->getCanvas()->drawLine({3.f*w/8.f, 0.f}, {3.f*h/8.f, h}, paint); |
| 149 |
paint.setColor(SK_ColorMAGENTA); |
| 150 |
surf->getCanvas()->drawLine({5.f*w/8.f, 0.f}, {5.f*h/8.f, h}, paint); |
| 151 |
paint.setColor(SK_ColorGREEN); |
| 152 |
surf->getCanvas()->drawLine({3.f*w/4.f, 0.f}, {3.f*h/4.f, h}, paint); |
| 153 |
|
| 154 |
// Fill everything outside of the content bounds with red since it shouldn't be sampled from. |
| 155 |
if (contentBounds) { |
| 156 |
SkRect buffer = contentBounds->makeOutset(1.f, 1.f); |
| 157 |
surf->getCanvas()->clipRect(buffer, SkClipOp::kDifference); |
| 158 |
surf->getCanvas()->clear(SK_ColorRED); |
| 159 |
} |
| 160 |
|
| 161 |
return surf->makeImageSnapshot(); |
| 162 |
} |
| 163 |
|
| 164 |
// Subset 'image' to contentBounds, apply 'contentTile' mode to fill 'cropRect'-sized image. |
| 165 |
sk_sp<SkImage> make_cropped_image(sk_sp<SkImage> image, |
| 166 |
const SkRect& contentBounds, |
| 167 |
SkTileMode contentTile, |
| 168 |
const SkRect& cropRect) { |
| 169 |
auto surface = SkSurfaces::Raster( |
| 170 |
image->imageInfo().makeWH(SkScalarCeilToInt(cropRect.width()), |
| 171 |
SkScalarCeilToInt(cropRect.height()))); |
| 172 |
auto content = image->makeSubset( |
| 173 |
nullptr, |
| 174 |
contentTile == SkTileMode::kDecal ? contentBounds.roundOut() : contentBounds.roundIn(), |
| 175 |
{}); |
| 176 |
if (!content || !surface) { |
| 177 |
return nullptr; |
| 178 |
} |
| 179 |
SkPaint tiledContent; |
| 180 |
tiledContent.setShader(content->makeShader(contentTile, contentTile, |
| 181 |
SkFilterMode::kNearest, |
| 182 |
SkMatrix::Translate(contentBounds.left(), |
| 183 |
contentBounds.top()))); |
| 184 |
surface->getCanvas()->translate(-cropRect.left(), -cropRect.top()); |
| 185 |
surface->getCanvas()->drawPaint(tiledContent); |
| 186 |
return surface->makeImageSnapshot(); |
| 187 |
} |
| 188 |
|
| 189 |
void draw_example_tile( |
| 190 |
SkCanvas* canvas, |
| 191 |
SkTileMode inputMode, // the tile mode applied to content bounds |
| 192 |
CropRelation inputRelation, // how crop rect relates to content bounds |
| 193 |
bool hintContent, // whether or not contentBounds is hinted to saveLayer() |
| 194 |
SkTileMode outputMode, // the tile mode applied to the crop rect output |
| 195 |
CropRelation outputRelation) {// how crop rect relates to output bounds (clip pre-saveLayer) |
| 196 |
|
| 197 |
// Determine crop rect for example based on output relation |
| 198 |
SkRect outputBounds, cropRect, contentBounds; |
| 199 |
get_example_rects(outputRelation, inputRelation, hintContent, |
| 200 |
&outputBounds, &cropRect, &contentBounds); |
| 201 |
SkASSERT(kExampleBounds.contains(outputBounds) && |
| 202 |
kExampleBounds.contains(cropRect) && |
| 203 |
kExampleBounds.contains(contentBounds)); |
| 204 |
|
| 205 |
auto image = make_image(canvas, hintContent ? &contentBounds : nullptr); |
| 206 |
|
| 207 |
canvas->save(); |
| 208 |
// Visualize the image tiled on the content bounds (blue border) and then tiled on the crop |
| 209 |
// rect (green) border, semi-transparent |
| 210 |
{ |
| 211 |
auto cropImage = ToolUtils::MakeTextureImage( |
| 212 |
canvas, make_cropped_image(image, contentBounds, inputMode, cropRect)); |
| 213 |
if (cropImage) { |
| 214 |
SkPaint tiledPaint; |
| 215 |
tiledPaint.setShader(cropImage->makeShader(outputMode, outputMode, |
| 216 |
SkFilterMode::kNearest, |
| 217 |
SkMatrix::Translate(cropRect.left(), |
| 218 |
cropRect.top()))); |
| 219 |
tiledPaint.setAlphaf(0.25f); |
| 220 |
|
| 221 |
canvas->save(); |
| 222 |
canvas->clipRect(kExampleBounds); |
| 223 |
canvas->drawPaint(tiledPaint); |
| 224 |
canvas->restore(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
// Build filter, clip, save layer, draw, restore - the interesting part is in the tile modes |
| 229 |
// and how the various bounds intersect each other. |
| 230 |
{ |
| 231 |
sk_sp<SkImageFilter> filter = SkImageFilters::Crop(contentBounds, inputMode, nullptr); |
| 232 |
filter = SkImageFilters::Blur(4.f, 4.f, std::move(filter)); |
| 233 |
filter = SkImageFilters::Crop(cropRect, outputMode, std::move(filter)); |
| 234 |
SkPaint layerPaint; |
| 235 |
layerPaint.setImageFilter(std::move(filter)); |
| 236 |
|
| 237 |
canvas->save(); |
| 238 |
canvas->clipRect(outputBounds); |
| 239 |
canvas->saveLayer(hintContent ? &contentBounds : nullptr, &layerPaint); |
| 240 |
|
| 241 |
auto tmp = ToolUtils::MakeTextureImage(canvas, image); |
| 242 |
canvas->drawImageRect(tmp, contentBounds, contentBounds, |
| 243 |
SkSamplingOptions(SkFilterMode::kNearest), nullptr, |
| 244 |
SkCanvas::kStrict_SrcRectConstraint); |
| 245 |
canvas->restore(); |
| 246 |
canvas->restore(); |
| 247 |
} |
| 248 |
|
| 249 |
// Visualize bounds after the actual rendering. |
| 250 |
{ |
| 251 |
SkPaint border; |
| 252 |
border.setStyle(SkPaint::kStroke_Style); |
| 253 |
|
| 254 |
border.setColor(kOutputBoundsColor); |
| 255 |
canvas->drawRect(outputBounds, border); |
| 256 |
|
| 257 |
border.setColor(kCropRectColor); |
| 258 |
canvas->drawRect(cropRect, border); |
| 259 |
|
| 260 |
if (hintContent) { |
| 261 |
border.setColor(kContentBoundsColor); |
| 262 |
canvas->drawRect(contentBounds, border); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
canvas->restore(); |
| 267 |
} |
| 268 |
|
| 269 |
// Draw 5 example tiles in a column for 5 relationships between content bounds and crop rect: |
| 270 |
// no content hint, intersect, content contains crop, crop contains content, and no intersection |
| 271 |
void draw_example_column( |
| 272 |
SkCanvas* canvas, |
| 273 |
SkTileMode inputMode, |
| 274 |
SkTileMode outputMode, |
| 275 |
CropRelation outputRelation) { |
| 276 |
const std::pair<CropRelation, bool> inputRelations[5] = { |
| 277 |
{ CropRelation::kCropOverlapsRect, false }, |
| 278 |
{ CropRelation::kCropOverlapsRect, true }, |
| 279 |
{ CropRelation::kCropContainsRect, true }, |
| 280 |
{ CropRelation::kRectContainsCrop, true }, |
| 281 |
{ CropRelation::kCropRectDisjoint, true } |
| 282 |
}; |
| 283 |
|
| 284 |
canvas->save(); |
| 285 |
for (auto [inputRelation, hintContent] : inputRelations) { |
| 286 |
draw_example_tile(canvas, inputMode, inputRelation, hintContent, |
| 287 |
outputMode, outputRelation); |
| 288 |
canvas->translate(0.f, kExampleBounds.fBottom + 1.f); |
| 289 |
} |
| 290 |
|
| 291 |
canvas->restore(); |
| 292 |
} |
| 293 |
|
| 294 |
// Draw 5x4 grid of examples covering supported input tile modes and crop rect relations |
| 295 |
static constexpr int kNumRows = 5; |
| 296 |
static constexpr int kNumCols = 4; |
| 297 |
static constexpr float kGridWidth = kNumCols * (kExampleBounds.fRight+1.f) - 1.f; |
| 298 |
static constexpr float kGridHeight = kNumRows * (kExampleBounds.fBottom+1.f) - 1.f; |
| 299 |
|
| 300 |
void draw_example_grid( |
| 301 |
SkCanvas* canvas, |
| 302 |
SkTileMode inputMode, |
| 303 |
SkTileMode outputMode) { |
| 304 |
canvas->save(); |
| 305 |
for (auto outputRelation : { CropRelation::kCropOverlapsRect, |
| 306 |
CropRelation::kCropContainsRect, |
| 307 |
CropRelation::kRectContainsCrop, |
| 308 |
CropRelation::kCropRectDisjoint }) { |
| 309 |
draw_example_column(canvas, inputMode, outputMode, outputRelation); |
| 310 |
canvas->translate(kExampleBounds.fRight + 1.f, 0.f); |
| 311 |
} |
| 312 |
canvas->restore(); |
| 313 |
|
| 314 |
// Draw dashed lines between rows and columns |
| 315 |
SkPaint dashedLine; |
| 316 |
dashedLine.setColor(SK_ColorGRAY); |
| 317 |
dashedLine.setStyle(SkPaint::kStroke_Style); |
| 318 |
dashedLine.setStrokeCap(SkPaint::kSquare_Cap); |
| 319 |
static const float kDashes[2] = {5.f, 15.f}; |
| 320 |
dashedLine.setPathEffect(SkDashPathEffect::Make(kDashes, 0.f)); |
| 321 |
|
| 322 |
for (int y = 1; y < kNumRows; ++y) { |
| 323 |
canvas->drawLine({0.5f, y * (kExampleBounds.fBottom+1.f) - 0.5f}, |
| 324 |
{kGridWidth - 0.5f, y * (kExampleBounds.fBottom+1.f) - 0.5f}, |
| 325 |
dashedLine); |
| 326 |
} |
| 327 |
for (int x = 1; x < kNumCols; ++x) { |
| 328 |
canvas->drawLine({x * (kExampleBounds.fRight+1.f) - 0.5f, 0.5f}, |
| 329 |
{x * (kExampleBounds.fRight+1.f) - 0.5f, kGridHeight - 0.5f}, |
| 330 |
dashedLine); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
} // namespace |
| 335 |
|
| 336 |
namespace skiagm { |
| 337 |
|
| 338 |
class CropImageFilterGM : public GM { |
| 339 |
public: |
| 340 |
CropImageFilterGM(SkTileMode inputMode, SkTileMode outputMode) |
| 341 |
: fInputMode(inputMode) |
| 342 |
, fOutputMode(outputMode) {} |
| 343 |
|
| 344 |
protected: |
| 345 |
SkISize getISize() override { |
| 346 |
return {SkScalarRoundToInt(4.f * (kExampleBounds.fRight + 1.f) - 1.f), |
| 347 |
SkScalarRoundToInt(5.f * (kExampleBounds.fBottom + 1.f) - 1.f)}; |
| 348 |
} |
| 349 |
SkString getName() const override { |
| 350 |
SkString name("crop_imagefilter_"); |
| 351 |
switch(fInputMode) { |
| 352 |
case SkTileMode::kDecal: name.append("decal"); break; |
| 353 |
case SkTileMode::kClamp: name.append("clamp"); break; |
| 354 |
case SkTileMode::kRepeat: name.append("repeat"); break; |
| 355 |
case SkTileMode::kMirror: name.append("mirror"); break; |
| 356 |
} |
| 357 |
name.append("-in_"); |
| 358 |
|
| 359 |
switch (fOutputMode) { |
| 360 |
case SkTileMode::kDecal: name.append("decal"); break; |
| 361 |
case SkTileMode::kClamp: name.append("clamp"); break; |
| 362 |
case SkTileMode::kRepeat: name.append("repeat"); break; |
| 363 |
case SkTileMode::kMirror: name.append("mirror"); break; |
| 364 |
} |
| 365 |
name.append("-out"); |
| 366 |
return name; |
| 367 |
} |
| 368 |
|
| 369 |
void onDraw(SkCanvas* canvas) override { |
| 370 |
draw_example_grid(canvas, fInputMode, fOutputMode); |
| 371 |
} |
| 372 |
|
| 373 |
private: |
| 374 |
SkTileMode fInputMode; |
| 375 |
SkTileMode fOutputMode; |
| 376 |
}; |
| 377 |
|
| 378 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kDecal, SkTileMode::kDecal); ) |
| 379 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kDecal, SkTileMode::kClamp); ) |
| 380 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kDecal, SkTileMode::kRepeat); ) |
| 381 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kDecal, SkTileMode::kMirror); ) |
| 382 |
|
| 383 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kClamp, SkTileMode::kDecal); ) |
| 384 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kClamp, SkTileMode::kClamp); ) |
| 385 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kClamp, SkTileMode::kRepeat); ) |
| 386 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kClamp, SkTileMode::kMirror); ) |
| 387 |
|
| 388 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kRepeat, SkTileMode::kDecal); ) |
| 389 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kRepeat, SkTileMode::kClamp); ) |
| 390 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kRepeat, SkTileMode::kRepeat); ) |
| 391 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kRepeat, SkTileMode::kMirror); ) |
| 392 |
|
| 393 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kMirror, SkTileMode::kDecal); ) |
| 394 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kMirror, SkTileMode::kClamp); ) |
| 395 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kMirror, SkTileMode::kRepeat); ) |
| 396 |
DEF_GM( return new CropImageFilterGM(SkTileMode::kMirror, SkTileMode::kMirror); ) |
| 397 |
|
| 398 |
} // namespace skiagm |