| 1 |
/* |
| 2 |
* Copyright 2012 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 <cmath> |
| 9 |
#include "gm/gm.h" |
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkBlurTypes.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkColor.h" |
| 14 |
#include "include/core/SkColorFilter.h" |
| 15 |
#include "include/core/SkImage.h" |
| 16 |
#include "include/core/SkMaskFilter.h" |
| 17 |
#include "include/core/SkMatrix.h" |
| 18 |
#include "include/core/SkPaint.h" |
| 19 |
#include "include/core/SkPathBuilder.h" |
| 20 |
#include "include/core/SkPoint.h" |
| 21 |
#include "include/core/SkRect.h" |
| 22 |
#include "include/core/SkRefCnt.h" |
| 23 |
#include "include/core/SkScalar.h" |
| 24 |
#include "include/core/SkShader.h" |
| 25 |
#include "include/core/SkSize.h" |
| 26 |
#include "include/core/SkString.h" |
| 27 |
#include "include/core/SkSurface.h" |
| 28 |
#include "include/core/SkTileMode.h" |
| 29 |
#include "include/core/SkTypes.h" |
| 30 |
#include "include/effects/SkGradient.h" |
| 31 |
#include "include/private/base/SkFloatingPoint.h" |
| 32 |
#include "include/private/base/SkTo.h" |
| 33 |
#include "src/core/SkBlurMask.h" |
| 34 |
#include "src/core/SkMask.h" |
| 35 |
#include "tools/timer/TimeUtils.h" |
| 36 |
|
| 37 |
#if defined(SK_GANESH) |
| 38 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 39 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
#include <vector> |
| 43 |
|
| 44 |
#define STROKE_WIDTH SkIntToScalar(10) |
| 45 |
|
| 46 |
typedef void (*Proc)(SkCanvas*, const SkRect&, const SkPaint&); |
| 47 |
|
| 48 |
static void fill_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 49 |
canvas->drawRect(r, p); |
| 50 |
} |
| 51 |
|
| 52 |
static void draw_donut(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 53 |
SkRect rect; |
| 54 |
SkPathBuilder path; |
| 55 |
|
| 56 |
rect = r; |
| 57 |
rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 58 |
path.addRect(rect); |
| 59 |
rect = r; |
| 60 |
rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 61 |
|
| 62 |
path.addRect(rect); |
| 63 |
path.setFillType(SkPathFillType::kEvenOdd); |
| 64 |
|
| 65 |
canvas->drawPath(path.detach(), p); |
| 66 |
} |
| 67 |
|
| 68 |
static void draw_donut_skewed(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 69 |
SkRect rect; |
| 70 |
SkPathBuilder path; |
| 71 |
|
| 72 |
rect = r; |
| 73 |
rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 74 |
path.addRect(rect); |
| 75 |
rect = r; |
| 76 |
rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 77 |
|
| 78 |
rect.offset(7, -7); |
| 79 |
|
| 80 |
path.addRect(rect); |
| 81 |
path.setFillType(SkPathFillType::kEvenOdd); |
| 82 |
|
| 83 |
canvas->drawPath(path.detach(), p); |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* Spits out an arbitrary gradient to test blur with shader on paint |
| 88 |
*/ |
| 89 |
static sk_sp<SkShader> make_radial() { |
| 90 |
SkPoint pts[2] = { |
| 91 |
{ 0, 0 }, |
| 92 |
{ SkIntToScalar(100), SkIntToScalar(100) } |
| 93 |
}; |
| 94 |
SkTileMode tm = SkTileMode::kClamp; |
| 95 |
const SkColor4f colors[] = { SkColors::kRed, SkColors::kGreen, }; |
| 96 |
const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 97 |
SkMatrix scale; |
| 98 |
scale.setScale(0.5f, 0.5f); |
| 99 |
scale.postTranslate(25.f, 25.f); |
| 100 |
SkPoint center0, center1; |
| 101 |
center0.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 102 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 103 |
center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 104 |
SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 105 |
return SkShaders::TwoPointConicalGradient(center1, (pts[1].fX - pts[0].fX) / 7, |
| 106 |
center0, (pts[1].fX - pts[0].fX) / 2, |
| 107 |
{{colors, pos, tm}, {}}, &scale); |
| 108 |
} |
| 109 |
|
| 110 |
typedef void (*PaintProc)(SkPaint*, SkScalar width); |
| 111 |
|
| 112 |
class BlurRectGM : public skiagm::GM { |
| 113 |
public: |
| 114 |
BlurRectGM(const char name[], U8CPU alpha) : fName(name), fAlpha(SkToU8(alpha)) {} |
| 115 |
|
| 116 |
private: |
| 117 |
sk_sp<SkMaskFilter> fMaskFilters[kLastEnum_SkBlurStyle + 1]; |
| 118 |
const char* fName; |
| 119 |
SkAlpha fAlpha; |
| 120 |
|
| 121 |
void onOnceBeforeDraw() override { |
| 122 |
for (int i = 0; i <= kLastEnum_SkBlurStyle; ++i) { |
| 123 |
fMaskFilters[i] = SkMaskFilter::MakeBlur((SkBlurStyle)i, |
| 124 |
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(STROKE_WIDTH/2))); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
SkString getName() const override { return SkString(fName); } |
| 129 |
|
| 130 |
SkISize getISize() override { return {860, 820}; } |
| 131 |
|
| 132 |
void onDraw(SkCanvas* canvas) override { |
| 133 |
canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2); |
| 134 |
|
| 135 |
SkRect r = { 0, 0, 100, 50 }; |
| 136 |
SkScalar scales[] = { SK_Scalar1, 0.6f }; |
| 137 |
|
| 138 |
for (size_t s = 0; s < std::size(scales); ++s) { |
| 139 |
canvas->save(); |
| 140 |
for (size_t f = 0; f < std::size(fMaskFilters); ++f) { |
| 141 |
SkPaint paint; |
| 142 |
paint.setMaskFilter(fMaskFilters[f]); |
| 143 |
paint.setAlpha(fAlpha); |
| 144 |
|
| 145 |
SkPaint paintWithRadial = paint; |
| 146 |
paintWithRadial.setShader(make_radial()); |
| 147 |
|
| 148 |
constexpr Proc procs[] = { |
| 149 |
fill_rect, draw_donut, draw_donut_skewed |
| 150 |
}; |
| 151 |
|
| 152 |
canvas->save(); |
| 153 |
canvas->scale(scales[s], scales[s]); |
| 154 |
this->drawProcs(canvas, r, paint, false, procs, std::size(procs)); |
| 155 |
canvas->translate(r.width() * 4/3, 0); |
| 156 |
this->drawProcs(canvas, r, paintWithRadial, false, procs, std::size(procs)); |
| 157 |
canvas->translate(r.width() * 4/3, 0); |
| 158 |
this->drawProcs(canvas, r, paint, true, procs, std::size(procs)); |
| 159 |
canvas->translate(r.width() * 4/3, 0); |
| 160 |
this->drawProcs(canvas, r, paintWithRadial, true, procs, std::size(procs)); |
| 161 |
canvas->restore(); |
| 162 |
|
| 163 |
canvas->translate(0, std::size(procs) * r.height() * 4/3 * scales[s]); |
| 164 |
} |
| 165 |
canvas->restore(); |
| 166 |
canvas->translate(4 * r.width() * 4/3 * scales[s], 0); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
void drawProcs(SkCanvas* canvas, const SkRect& r, const SkPaint& paint, |
| 171 |
bool doClip, const Proc procs[], size_t procsCount) { |
| 172 |
SkAutoCanvasRestore acr(canvas, true); |
| 173 |
for (size_t i = 0; i < procsCount; ++i) { |
| 174 |
if (doClip) { |
| 175 |
SkRect clipRect(r); |
| 176 |
clipRect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 177 |
canvas->save(); |
| 178 |
canvas->clipRect(r); |
| 179 |
} |
| 180 |
procs[i](canvas, r, paint); |
| 181 |
if (doClip) { |
| 182 |
canvas->restore(); |
| 183 |
} |
| 184 |
canvas->translate(0, r.height() * 4/3); |
| 185 |
} |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
DEF_SIMPLE_GM(blurrect_gallery, canvas, 1200, 1024) { |
| 190 |
const int fGMWidth = 1200; |
| 191 |
const int fPadding = 10; |
| 192 |
const int fMargin = 100; |
| 193 |
|
| 194 |
const int widths[] = {25, 5, 5, 100, 150, 25}; |
| 195 |
const int heights[] = {100, 100, 5, 25, 150, 25}; |
| 196 |
const SkBlurStyle styles[] = {kNormal_SkBlurStyle, kInner_SkBlurStyle, kOuter_SkBlurStyle}; |
| 197 |
const float radii[] = {20, 5, 10}; |
| 198 |
|
| 199 |
canvas->translate(50,20); |
| 200 |
|
| 201 |
int cur_x = 0; |
| 202 |
int cur_y = 0; |
| 203 |
|
| 204 |
int max_height = 0; |
| 205 |
|
| 206 |
for (size_t i = 0 ; i < std::size(widths) ; i++) { |
| 207 |
int width = widths[i]; |
| 208 |
int height = heights[i]; |
| 209 |
SkRect r; |
| 210 |
r.setWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 211 |
SkAutoCanvasRestore autoRestore(canvas, true); |
| 212 |
|
| 213 |
for (size_t j = 0 ; j < std::size(radii) ; j++) { |
| 214 |
float radius = radii[j]; |
| 215 |
for (size_t k = 0 ; k < std::size(styles) ; k++) { |
| 216 |
SkBlurStyle style = styles[k]; |
| 217 |
|
| 218 |
SkMaskBuilder mask; |
| 219 |
if (!SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(radius), |
| 220 |
&mask, r, style)) { |
| 221 |
continue; |
| 222 |
} |
| 223 |
|
| 224 |
SkAutoMaskFreeImage amfi(mask.image()); |
| 225 |
|
| 226 |
SkBitmap bm; |
| 227 |
SkAssertResult(bm.installPixels( |
| 228 |
SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()), |
| 229 |
mask.image(), |
| 230 |
mask.fRowBytes)); |
| 231 |
|
| 232 |
if (cur_x + bm.width() >= fGMWidth - fMargin) { |
| 233 |
cur_x = 0; |
| 234 |
cur_y += max_height + fPadding; |
| 235 |
max_height = 0; |
| 236 |
} |
| 237 |
|
| 238 |
canvas->save(); |
| 239 |
canvas->translate((SkScalar)cur_x, (SkScalar)cur_y); |
| 240 |
canvas->translate(-(bm.width() - r.width())/2, -(bm.height()-r.height())/2); |
| 241 |
canvas->drawImage(bm.asImage(), 0.f, 0.f); |
| 242 |
canvas->restore(); |
| 243 |
|
| 244 |
cur_x += bm.width() + fPadding; |
| 245 |
if (bm.height() > max_height) |
| 246 |
max_height = bm.height(); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
namespace skiagm { |
| 253 |
|
| 254 |
// Compares actual blur rects with reference masks created by the GM. Animates sigma in viewer. |
| 255 |
class BlurRectCompareGM : public GM { |
| 256 |
protected: |
| 257 |
SkString getName() const override { return SkString("blurrect_compare"); } |
| 258 |
|
| 259 |
SkISize getISize() override { return {900, 1220}; } |
| 260 |
|
| 261 |
void onOnceBeforeDraw() override { this->prepareReferenceMasks(); } |
| 262 |
|
| 263 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 264 |
if (canvas->imageInfo().colorType() == kUnknown_SkColorType) { |
| 265 |
*errorMsg = "Not supported when recording, relies on canvas->makeSurface()"; |
| 266 |
return DrawResult::kSkip; |
| 267 |
} |
| 268 |
|
| 269 |
int32_t ctxID = 0; |
| 270 |
#if defined(SK_GANESH) |
| 271 |
if (auto rc = canvas->recordingContext()) { |
| 272 |
if (!rc->asDirectContext()) { |
| 273 |
*errorMsg = "Not supported when recording, relies on canvas->makeSurface()"; |
| 274 |
return DrawResult::kSkip; |
| 275 |
} |
| 276 |
ctxID = rc->priv().contextID(); |
| 277 |
} |
| 278 |
#endif |
| 279 |
|
| 280 |
if (fRecalcMasksForAnimation || !fActualMasks[0][0][0] || ctxID != fLastContextUniqueID) { |
| 281 |
if (fRecalcMasksForAnimation) { |
| 282 |
// Sigma is changing so references must also be recalculated. |
| 283 |
this->prepareReferenceMasks(); |
| 284 |
} |
| 285 |
this->prepareActualMasks(canvas); |
| 286 |
this->prepareMaskDifferences(canvas); |
| 287 |
fLastContextUniqueID = ctxID; |
| 288 |
fRecalcMasksForAnimation = false; |
| 289 |
} |
| 290 |
canvas->clear(SK_ColorBLACK); |
| 291 |
static constexpr float kMargin = 30; |
| 292 |
float totalW = 0; |
| 293 |
for (auto w : kSizes) { |
| 294 |
totalW += w + kMargin; |
| 295 |
} |
| 296 |
canvas->translate(kMargin, kMargin); |
| 297 |
for (int mode = 0; mode < 3; ++mode) { |
| 298 |
canvas->save(); |
| 299 |
for (size_t sigmaIdx = 0; sigmaIdx < kNumSigmas; ++sigmaIdx) { |
| 300 |
auto sigma = kSigmas[sigmaIdx] + fSigmaAnimationBoost; |
| 301 |
for (size_t heightIdx = 0; heightIdx < kNumSizes; ++heightIdx) { |
| 302 |
auto h = kSizes[heightIdx]; |
| 303 |
canvas->save(); |
| 304 |
for (size_t widthIdx = 0; widthIdx < kNumSizes; ++widthIdx) { |
| 305 |
auto w = kSizes[widthIdx]; |
| 306 |
SkPaint paint; |
| 307 |
paint.setColor(SK_ColorWHITE); |
| 308 |
SkImage* img; |
| 309 |
switch (mode) { |
| 310 |
case 0: |
| 311 |
img = fReferenceMasks[sigmaIdx][heightIdx][widthIdx].get(); |
| 312 |
break; |
| 313 |
case 1: |
| 314 |
img = fActualMasks[sigmaIdx][heightIdx][widthIdx].get(); |
| 315 |
break; |
| 316 |
case 2: |
| 317 |
img = fMaskDifferences[sigmaIdx][heightIdx][widthIdx].get(); |
| 318 |
// The error images are opaque, use kPlus so they are additive if |
| 319 |
// the overlap between test cases. |
| 320 |
paint.setBlendMode(SkBlendMode::kPlus); |
| 321 |
break; |
| 322 |
} |
| 323 |
auto pad = PadForSigma(sigma); |
| 324 |
canvas->drawImage(img, -pad, -pad, SkSamplingOptions(), &paint); |
| 325 |
#if 0 // Uncomment to hairline stroke around blurred rect in red on top of the blur result. |
| 326 |
// The rect is defined at integer coords. We inset by 1/2 pixel so our stroke lies on top |
| 327 |
// of the edge pixels. |
| 328 |
SkPaint stroke; |
| 329 |
stroke.setColor(SK_ColorRED); |
| 330 |
stroke.setStrokeWidth(0.f); |
| 331 |
stroke.setStyle(SkPaint::kStroke_Style); |
| 332 |
canvas->drawRect(SkRect::MakeWH(w, h).makeInset(0.5, 0.5), stroke); |
| 333 |
#endif |
| 334 |
canvas->translate(w + kMargin, 0.f); |
| 335 |
} |
| 336 |
canvas->restore(); |
| 337 |
canvas->translate(0, h + kMargin); |
| 338 |
} |
| 339 |
} |
| 340 |
canvas->restore(); |
| 341 |
canvas->translate(totalW + 2 * kMargin, 0); |
| 342 |
} |
| 343 |
return DrawResult::kOk; |
| 344 |
} |
| 345 |
bool onAnimate(double nanos) override { |
| 346 |
fSigmaAnimationBoost = TimeUtils::SineWave(nanos, 5, 2.5f, 0.f, 2.f); |
| 347 |
fRecalcMasksForAnimation = true; |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
private: |
| 352 |
void prepareReferenceMasks() { |
| 353 |
auto create_reference_mask = [](int w, int h, float sigma, int numSubpixels) { |
| 354 |
int pad = PadForSigma(sigma); |
| 355 |
int maskW = w + 2 * pad; |
| 356 |
int maskH = h + 2 * pad; |
| 357 |
// We'll do all our calculations at subpixel resolution, so adjust params |
| 358 |
w *= numSubpixels; |
| 359 |
h *= numSubpixels; |
| 360 |
sigma *= numSubpixels; |
| 361 |
auto scale = SK_ScalarRoot2Over2 / sigma; |
| 362 |
auto def_integral_approx = [scale](float a, float b) { |
| 363 |
return 0.5f * (std::erf(b * scale) - std::erf(a * scale)); |
| 364 |
}; |
| 365 |
// Do the x-pass. Above/below rect are rows of zero. All rows that intersect the rect |
| 366 |
// are the same. The row is calculated and stored at subpixel resolution. |
| 367 |
SkASSERT(!(numSubpixels & 0b1)); |
| 368 |
std::unique_ptr<float[]> row(new float[maskW * numSubpixels]); |
| 369 |
for (int col = 0; col < maskW * numSubpixels; ++col) { |
| 370 |
// Compute distance to rect left in subpixel units |
| 371 |
float ldiff = numSubpixels * pad - (col + 0.5f); |
| 372 |
float rdiff = ldiff + w; |
| 373 |
row[col] = def_integral_approx(ldiff, rdiff); |
| 374 |
} |
| 375 |
// y-pass |
| 376 |
SkBitmap bmp; |
| 377 |
bmp.allocPixels(SkImageInfo::MakeA8(maskW, maskH)); |
| 378 |
std::unique_ptr<float[]> accums(new float[maskW]); |
| 379 |
const float accumScale = 1.f / (numSubpixels * numSubpixels); |
| 380 |
for (int y = 0; y < maskH; ++y) { |
| 381 |
// Initialize subpixel accumulation buffer for this row. |
| 382 |
std::fill_n(accums.get(), maskW, 0); |
| 383 |
for (int ys = 0; ys < numSubpixels; ++ys) { |
| 384 |
// At each subpixel we want to integrate over the kernel centered at the |
| 385 |
// subpixel multiplied by the x-pass. The x-pass is zero above and below the |
| 386 |
// rect and constant valued from rect top to rect bottom. So we can get the |
| 387 |
// integral of just the kernel from rect top to rect bottom and multiply by |
| 388 |
// the single x-pass value from our precomputed row. |
| 389 |
float tdiff = numSubpixels * pad - (y * numSubpixels + ys + 0.5f); |
| 390 |
float bdiff = tdiff + h; |
| 391 |
auto integral = def_integral_approx(tdiff, bdiff); |
| 392 |
for (int x = 0; x < maskW; ++x) { |
| 393 |
for (int xs = 0; xs < numSubpixels; ++xs) { |
| 394 |
int rowIdx = x * numSubpixels + xs; |
| 395 |
accums[x] += integral * row[rowIdx]; |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
for (int x = 0; x < maskW; ++x) { |
| 400 |
auto result = accums[x] * accumScale; |
| 401 |
*bmp.getAddr8(x, y) = SkToU8(sk_float_round2int(255.f * result)); |
| 402 |
} |
| 403 |
} |
| 404 |
return bmp.asImage(); |
| 405 |
}; |
| 406 |
|
| 407 |
// Number of times to subsample (in both X and Y). If fRecalcMasksForAnimation is true |
| 408 |
// then we're animating, don't subsample as much to keep fps higher. |
| 409 |
const int numSubpixels = fRecalcMasksForAnimation ? 2 : 8; |
| 410 |
|
| 411 |
for (size_t sigmaIdx = 0; sigmaIdx < kNumSigmas; ++sigmaIdx) { |
| 412 |
auto sigma = kSigmas[sigmaIdx] + fSigmaAnimationBoost; |
| 413 |
for (size_t heightIdx = 0; heightIdx < kNumSizes; ++heightIdx) { |
| 414 |
auto h = kSizes[heightIdx]; |
| 415 |
for (size_t widthIdx = 0; widthIdx < kNumSizes; ++widthIdx) { |
| 416 |
auto w = kSizes[widthIdx]; |
| 417 |
fReferenceMasks[sigmaIdx][heightIdx][widthIdx] = |
| 418 |
create_reference_mask(w, h, sigma, numSubpixels); |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
void prepareActualMasks(SkCanvas* canvas) { |
| 425 |
for (size_t sigmaIdx = 0; sigmaIdx < kNumSigmas; ++sigmaIdx) { |
| 426 |
auto sigma = kSigmas[sigmaIdx] + fSigmaAnimationBoost; |
| 427 |
for (size_t heightIdx = 0; heightIdx < kNumSizes; ++heightIdx) { |
| 428 |
auto h = kSizes[heightIdx]; |
| 429 |
for (size_t widthIdx = 0; widthIdx < kNumSizes; ++widthIdx) { |
| 430 |
auto w = kSizes[widthIdx]; |
| 431 |
auto pad = PadForSigma(sigma); |
| 432 |
auto ii = SkImageInfo::MakeA8(w + 2 * pad, h + 2 * pad); |
| 433 |
auto surf = canvas->makeSurface(ii); |
| 434 |
if (!surf) { |
| 435 |
// Some GPUs don't have renderable A8 :( |
| 436 |
surf = canvas->makeSurface(ii.makeColorType(kRGBA_8888_SkColorType)); |
| 437 |
if (!surf) { |
| 438 |
return; |
| 439 |
} |
| 440 |
} |
| 441 |
auto rect = SkRect::MakeXYWH(pad, pad, w, h); |
| 442 |
SkPaint paint; |
| 443 |
// Color doesn't matter if we're rendering to A8 but does if we promoted to |
| 444 |
// RGBA above. |
| 445 |
paint.setColor(SK_ColorWHITE); |
| 446 |
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma)); |
| 447 |
surf->getCanvas()->drawRect(rect, paint); |
| 448 |
fActualMasks[sigmaIdx][heightIdx][widthIdx] = surf->makeImageSnapshot(); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
void prepareMaskDifferences(SkCanvas* canvas) { |
| 455 |
for (size_t sigmaIdx = 0; sigmaIdx < kNumSigmas; ++sigmaIdx) { |
| 456 |
for (size_t heightIdx = 0; heightIdx < kNumSizes; ++heightIdx) { |
| 457 |
for (size_t widthIdx = 0; widthIdx < kNumSizes; ++widthIdx) { |
| 458 |
const auto& r = fReferenceMasks[sigmaIdx][heightIdx][widthIdx]; |
| 459 |
const auto& a = fActualMasks[sigmaIdx][heightIdx][widthIdx]; |
| 460 |
auto& d = fMaskDifferences[sigmaIdx][heightIdx][widthIdx]; |
| 461 |
// The actual image might not be present if we're on an abandoned GrContext. |
| 462 |
if (!a) { |
| 463 |
d.reset(); |
| 464 |
continue; |
| 465 |
} |
| 466 |
SkASSERT(r->width() == a->width()); |
| 467 |
SkASSERT(r->height() == a->height()); |
| 468 |
auto ii = SkImageInfo::Make(r->width(), r->height(), |
| 469 |
kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 470 |
auto surf = canvas->makeSurface(ii); |
| 471 |
if (!surf) { |
| 472 |
return; |
| 473 |
} |
| 474 |
// We visualize the difference by turning both the alpha masks into opaque green |
| 475 |
// images (where alpha becomes the green channel) and then perform a |
| 476 |
// SkBlendMode::kDifference between them. |
| 477 |
SkPaint filterPaint; |
| 478 |
filterPaint.setColor(SK_ColorWHITE); |
| 479 |
// Actually 8 * alpha becomes green to really highlight differences. |
| 480 |
static constexpr float kGreenifyM[] = {0, 0, 0, 0, 0, |
| 481 |
0, 0, 0, 8, 0, |
| 482 |
0, 0, 0, 0, 0, |
| 483 |
0, 0, 0, 0, 1}; |
| 484 |
auto greenifyCF = SkColorFilters::Matrix(kGreenifyM); |
| 485 |
SkPaint paint; |
| 486 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 487 |
paint.setColorFilter(std::move(greenifyCF)); |
| 488 |
surf->getCanvas()->drawImage(a, 0, 0, SkSamplingOptions(), &paint); |
| 489 |
paint.setBlendMode(SkBlendMode::kDifference); |
| 490 |
surf->getCanvas()->drawImage(r, 0, 0, SkSamplingOptions(), &paint); |
| 491 |
d = surf->makeImageSnapshot(); |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
// Per side padding around mask images for a sigma. Make this overly generous to ensure bugs |
| 498 |
// related to big blurs are fully visible. |
| 499 |
static int PadForSigma(float sigma) { return sk_float_ceil2int(4 * sigma); } |
| 500 |
|
| 501 |
inline static constexpr int kSizes[] = {1, 2, 4, 8, 16, 32}; |
| 502 |
inline static constexpr float kSigmas[] = {0.5f, 1.2f, 2.3f, 3.9f, 7.4f}; |
| 503 |
inline static constexpr size_t kNumSizes = std::size(kSizes); |
| 504 |
inline static constexpr size_t kNumSigmas = std::size(kSigmas); |
| 505 |
|
| 506 |
sk_sp<SkImage> fReferenceMasks[kNumSigmas][kNumSizes][kNumSizes]; |
| 507 |
sk_sp<SkImage> fActualMasks[kNumSigmas][kNumSizes][kNumSizes]; |
| 508 |
sk_sp<SkImage> fMaskDifferences[kNumSigmas][kNumSizes][kNumSizes]; |
| 509 |
int32_t fLastContextUniqueID; |
| 510 |
// These are used only when animating. |
| 511 |
float fSigmaAnimationBoost = 0; |
| 512 |
bool fRecalcMasksForAnimation = false; |
| 513 |
}; |
| 514 |
|
| 515 |
} // namespace skiagm |
| 516 |
|
| 517 |
////////////////////////////////////////////////////////////////////////////// |
| 518 |
|
| 519 |
DEF_GM(return new BlurRectGM("blurrects", 0xFF);) |
| 520 |
DEF_GM(return new skiagm::BlurRectCompareGM();) |
| 521 |
|
| 522 |
////////////////////////////////////////////////////////////////////////////// |
| 523 |
|
| 524 |
DEF_SIMPLE_GM(blur_matrix_rect, canvas, 650, 685) { |
| 525 |
static constexpr auto kRect = SkRect::MakeWH(14, 60); |
| 526 |
static constexpr float kSigmas[] = {0.5f, 1.2f, 2.3f, 3.9f, 7.4f}; |
| 527 |
static constexpr size_t kNumSigmas = std::size(kSigmas); |
| 528 |
|
| 529 |
const SkPoint c = {kRect.centerX(), kRect.centerY()}; |
| 530 |
|
| 531 |
std::vector<SkMatrix> matrices; |
| 532 |
|
| 533 |
matrices.push_back(SkMatrix::RotateDeg(4.f, c)); |
| 534 |
|
| 535 |
matrices.push_back(SkMatrix::RotateDeg(63.f, c)); |
| 536 |
|
| 537 |
matrices.push_back(SkMatrix::RotateDeg(30.f, c)); |
| 538 |
matrices.back().preScale(1.1f, .5f); |
| 539 |
|
| 540 |
matrices.push_back(SkMatrix::RotateDeg(147.f, c)); |
| 541 |
matrices.back().preScale(3.f, .1f); |
| 542 |
|
| 543 |
SkMatrix mirror; |
| 544 |
mirror.setAll(0, 1, 0, |
| 545 |
1, 0, 0, |
| 546 |
0, 0, 1); |
| 547 |
matrices.push_back(SkMatrix::Concat(mirror, matrices.back())); |
| 548 |
|
| 549 |
matrices.push_back(SkMatrix::RotateDeg(197.f, c)); |
| 550 |
matrices.back().preSkew(.3f, -.5f); |
| 551 |
|
| 552 |
auto bounds = SkRect::MakeEmpty(); |
| 553 |
for (const auto& m : matrices) { |
| 554 |
SkRect mapped; |
| 555 |
m.mapRect(&mapped, kRect); |
| 556 |
bounds.joinNonEmptyArg(mapped.makeSorted()); |
| 557 |
} |
| 558 |
float blurPad = 2.f*kSigmas[kNumSigmas - 1]; |
| 559 |
bounds.outset(blurPad, blurPad); |
| 560 |
canvas->translate(-bounds.left(), -bounds.top()); |
| 561 |
for (auto sigma : kSigmas) { |
| 562 |
SkPaint paint; |
| 563 |
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma)); |
| 564 |
canvas->save(); |
| 565 |
for (const auto& m : matrices) { |
| 566 |
canvas->save(); |
| 567 |
canvas->concat(m); |
| 568 |
canvas->drawRect(kRect, paint); |
| 569 |
canvas->restore(); |
| 570 |
canvas->translate(0, bounds.height()); |
| 571 |
} |
| 572 |
canvas->restore(); |
| 573 |
canvas->translate(bounds.width(), 0); |
| 574 |
} |
| 575 |
} |