2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-04-29 15:20:16 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkBlurTypes.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkDrawLooper.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMaskFilter.h"
|
|
|
|
#include "include/core/SkMath.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRRect.h"
|
2019-05-06 21:17:19 +00:00
|
|
|
#include "include/core/SkRect.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/effects/SkBlurDrawLooper.h"
|
|
|
|
#include "include/effects/SkLayerDrawLooper.h"
|
|
|
|
#include "include/effects/SkPerlinNoiseShader.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkFloatBits.h"
|
2020-10-16 16:04:18 +00:00
|
|
|
#include "include/private/SkTPin.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkBlurMask.h"
|
2020-10-21 17:01:34 +00:00
|
|
|
#include "src/core/SkGpuBlurUtils.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkMask.h"
|
|
|
|
#include "src/core/SkMaskFilterBase.h"
|
|
|
|
#include "src/core/SkMathPriv.h"
|
|
|
|
#include "src/effects/SkEmbossMaskFilter.h"
|
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
#include "tools/gpu/GrContextFactory.h"
|
2013-09-05 16:39:03 +00:00
|
|
|
|
2018-03-09 19:41:36 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2019-05-06 21:17:19 +00:00
|
|
|
#include <initializer_list>
|
2018-03-09 19:41:36 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2013-09-05 16:39:03 +00:00
|
|
|
#define WRITE_CSV 0
|
2011-07-07 13:43:38 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
|
|
|
|
|
2011-09-02 15:06:44 +00:00
|
|
|
static const int outset = 100;
|
|
|
|
static const SkColor bgColor = SK_ColorWHITE;
|
|
|
|
static const int strokeWidth = 4;
|
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
static void create(SkBitmap* bm, const SkIRect& bound) {
|
|
|
|
bm->allocN32Pixels(bound.width(), bound.height());
|
2011-09-02 15:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void drawBG(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(bgColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct BlurTest {
|
|
|
|
void (*addPath)(SkPath*);
|
|
|
|
int viewLen;
|
|
|
|
SkIRect views[9];
|
|
|
|
};
|
|
|
|
|
|
|
|
//Path Draw Procs
|
|
|
|
//Beware that paths themselves my draw differently depending on the clip.
|
|
|
|
static void draw50x50Rect(SkPath* path) {
|
|
|
|
path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Tests
|
|
|
|
static BlurTest tests[] = {
|
|
|
|
{ draw50x50Rect, 3, {
|
|
|
|
//inner half of blur
|
|
|
|
{ 0, 0, 50, 50 },
|
|
|
|
//blur, but no path.
|
|
|
|
{ 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
|
|
|
|
//just an edge
|
|
|
|
{ 40, strokeWidth, 60, 50 - strokeWidth },
|
|
|
|
}},
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Assumes that the ref draw was completely inside ref canvas --
|
|
|
|
implies that everything outside is "bgColor".
|
|
|
|
Checks that all overlap is the same and that all non-overlap on the
|
|
|
|
ref is "bgColor".
|
|
|
|
*/
|
|
|
|
static bool compare(const SkBitmap& ref, const SkIRect& iref,
|
|
|
|
const SkBitmap& test, const SkIRect& itest)
|
|
|
|
{
|
|
|
|
const int xOff = itest.fLeft - iref.fLeft;
|
|
|
|
const int yOff = itest.fTop - iref.fTop;
|
|
|
|
|
|
|
|
for (int y = 0; y < test.height(); ++y) {
|
|
|
|
for (int x = 0; x < test.width(); ++x) {
|
|
|
|
SkColor testColor = test.getColor(x, y);
|
|
|
|
int refX = x + xOff;
|
|
|
|
int refY = y + yOff;
|
|
|
|
SkColor refColor;
|
|
|
|
if (refX >= 0 && refX < ref.width() &&
|
|
|
|
refY >= 0 && refY < ref.height())
|
|
|
|
{
|
|
|
|
refColor = ref.getColor(refX, refY);
|
|
|
|
} else {
|
|
|
|
refColor = bgColor;
|
|
|
|
}
|
|
|
|
if (refColor != testColor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
DEF_TEST(BlurDrawing, reporter) {
|
2011-09-02 15:06:44 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGRAY);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(strokeWidth));
|
|
|
|
|
2013-09-06 14:16:12 +00:00
|
|
|
SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
|
2014-04-28 16:25:35 +00:00
|
|
|
for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) {
|
|
|
|
SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style);
|
2011-09-02 15:06:44 +00:00
|
|
|
|
2018-03-12 18:03:47 +00:00
|
|
|
for (bool respectCTM : { false, true }) {
|
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM));
|
2011-09-02 15:06:44 +00:00
|
|
|
|
2011-10-27 15:27:51 +00:00
|
|
|
for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
|
2011-09-02 15:06:44 +00:00
|
|
|
SkPath path;
|
|
|
|
tests[test].addPath(&path);
|
2011-09-02 15:46:17 +00:00
|
|
|
SkPath strokedPath;
|
|
|
|
paint.getFillPath(path, &strokedPath);
|
|
|
|
SkRect refBound = strokedPath.getBounds();
|
2011-09-02 15:06:44 +00:00
|
|
|
SkIRect iref;
|
|
|
|
refBound.roundOut(&iref);
|
2011-09-02 15:46:17 +00:00
|
|
|
iref.inset(-outset, -outset);
|
2011-09-02 15:06:44 +00:00
|
|
|
SkBitmap refBitmap;
|
2014-02-13 22:00:04 +00:00
|
|
|
create(&refBitmap, iref);
|
2011-07-07 13:43:38 +00:00
|
|
|
|
2011-09-02 15:06:44 +00:00
|
|
|
SkCanvas refCanvas(refBitmap);
|
|
|
|
refCanvas.translate(SkIntToScalar(-iref.fLeft),
|
|
|
|
SkIntToScalar(-iref.fTop));
|
|
|
|
drawBG(&refCanvas);
|
|
|
|
refCanvas.drawPath(path, paint);
|
2011-07-07 13:43:38 +00:00
|
|
|
|
2011-09-02 15:06:44 +00:00
|
|
|
for (int view = 0; view < tests[test].viewLen; ++view) {
|
|
|
|
SkIRect itest = tests[test].views[view];
|
|
|
|
SkBitmap testBitmap;
|
2014-02-13 22:00:04 +00:00
|
|
|
create(&testBitmap, itest);
|
2011-07-07 13:43:38 +00:00
|
|
|
|
2011-09-02 15:06:44 +00:00
|
|
|
SkCanvas testCanvas(testBitmap);
|
|
|
|
testCanvas.translate(SkIntToScalar(-itest.fLeft),
|
|
|
|
SkIntToScalar(-itest.fTop));
|
|
|
|
drawBG(&testCanvas);
|
|
|
|
testCanvas.drawPath(path, paint);
|
2011-07-07 13:43:38 +00:00
|
|
|
|
2011-09-02 15:06:44 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
compare(refBitmap, iref, testBitmap, itest));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-07 13:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:39:03 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
|
|
|
|
// white rect. Return the right half of the middle row in 'result'.
|
2013-09-06 07:01:45 +00:00
|
|
|
static void ground_truth_2d(int width, int height,
|
2013-09-05 16:39:03 +00:00
|
|
|
SkScalar sigma,
|
|
|
|
int* result, int resultCount) {
|
|
|
|
SkMask src, dst;
|
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
src.fBounds.setWH(width, height);
|
2013-09-05 16:39:03 +00:00
|
|
|
src.fFormat = SkMask::kA8_Format;
|
|
|
|
src.fRowBytes = src.fBounds.width();
|
|
|
|
src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
|
|
|
|
|
|
|
|
memset(src.fImage, 0xff, src.computeTotalImageSize());
|
|
|
|
|
2015-12-17 19:33:12 +00:00
|
|
|
if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-05 16:39:03 +00:00
|
|
|
|
2018-04-11 18:30:17 +00:00
|
|
|
int midX = dst.fBounds.x() + dst.fBounds.width()/2;
|
|
|
|
int midY = dst.fBounds.y() + dst.fBounds.height()/2;
|
2013-09-05 16:39:03 +00:00
|
|
|
uint8_t* bytes = dst.getAddr8(midX, midY);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) {
|
|
|
|
if (i < resultCount) {
|
|
|
|
result[i] = bytes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ( ; i < resultCount; ++i) {
|
|
|
|
result[i] = 0;
|
|
|
|
}
|
2013-10-11 14:38:46 +00:00
|
|
|
|
|
|
|
SkMask::FreeImage(src.fImage);
|
|
|
|
SkMask::FreeImage(dst.fImage);
|
2013-09-05 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implement a step function that is 255 between min and max; 0 elsewhere.
|
|
|
|
static int step(int x, SkScalar min, SkScalar max) {
|
|
|
|
if (min < x && x < max) {
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
|
|
|
|
static float gaussian(int x, SkScalar sigma) {
|
2013-09-05 17:53:34 +00:00
|
|
|
float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
|
2013-09-05 16:39:03 +00:00
|
|
|
float exponent = -(x * x) / (2 * sigma * sigma);
|
2013-09-05 17:53:34 +00:00
|
|
|
return k * expf(exponent);
|
2013-09-05 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a brute force convolution of a step function with a Gaussian.
|
|
|
|
// Return the right half in 'result'
|
2013-09-06 07:01:45 +00:00
|
|
|
static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
|
2013-09-05 16:39:03 +00:00
|
|
|
SkScalar gaussianSigma,
|
|
|
|
int* result, int resultCount) {
|
|
|
|
|
|
|
|
int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
|
|
|
|
|
|
|
|
for (int i = 0; i < resultCount; ++i) {
|
|
|
|
SkScalar sum = 0.0f;
|
|
|
|
for (int j = -gaussianRange; j < gaussianRange; ++j) {
|
|
|
|
sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
|
|
|
|
}
|
|
|
|
|
2020-02-06 19:15:43 +00:00
|
|
|
result[i] = SkTPin(SkClampPos(int(sum + 0.5f)), 0, 255);
|
2013-09-05 16:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 07:01:45 +00:00
|
|
|
static void blur_path(SkCanvas* canvas, const SkPath& path,
|
2013-09-05 16:39:03 +00:00
|
|
|
SkScalar gaussianSigma) {
|
|
|
|
|
|
|
|
SkScalar midX = path.getBounds().centerX();
|
|
|
|
SkScalar midY = path.getBounds().centerY();
|
|
|
|
|
|
|
|
canvas->translate(-midX, -midY);
|
|
|
|
|
|
|
|
SkPaint blurPaint;
|
|
|
|
blurPaint.setColor(SK_ColorWHITE);
|
2018-03-12 18:03:47 +00:00
|
|
|
blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, gaussianSigma));
|
2013-09-05 16:39:03 +00:00
|
|
|
|
|
|
|
canvas->drawColor(SK_ColorBLACK);
|
|
|
|
canvas->drawPath(path, blurPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readback the blurred draw results from the canvas
|
2017-07-21 18:24:29 +00:00
|
|
|
static void readback(const SkBitmap& src, int* result, int resultCount) {
|
2013-09-05 16:39:03 +00:00
|
|
|
SkBitmap readback;
|
2014-02-13 22:00:04 +00:00
|
|
|
readback.allocN32Pixels(resultCount, 30);
|
2017-07-21 18:24:29 +00:00
|
|
|
SkPixmap pm;
|
|
|
|
readback.peekPixels(&pm);
|
|
|
|
src.readPixels(pm, 0, 0);
|
2013-09-05 16:39:03 +00:00
|
|
|
|
2017-07-21 18:24:29 +00:00
|
|
|
const SkPMColor* pixels = pm.addr32(0, 15);
|
2013-09-05 16:39:03 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < resultCount; ++i) {
|
|
|
|
result[i] = SkColorGetR(pixels[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 07:01:45 +00:00
|
|
|
// Draw a blurred version of the provided path.
|
2013-09-05 16:39:03 +00:00
|
|
|
// Return the right half of the middle row in 'result'.
|
|
|
|
static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
|
|
|
|
int* result, int resultCount) {
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
2014-02-13 22:00:04 +00:00
|
|
|
bitmap.allocN32Pixels(resultCount, 30);
|
2013-09-05 16:39:03 +00:00
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
|
|
|
blur_path(&canvas, path, gaussianSigma);
|
2017-07-21 18:24:29 +00:00
|
|
|
readback(bitmap, result, resultCount);
|
2013-09-05 16:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if WRITE_CSV
|
|
|
|
static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
|
|
|
|
SkDebugf("%s_%.2f,", label, scale);
|
|
|
|
for (int i = 0; i < count-1; ++i) {
|
|
|
|
SkDebugf("%d,", data[i]);
|
|
|
|
}
|
|
|
|
SkDebugf("%d\n", data[count-1]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool match(int* first, int* second, int count, int tol) {
|
|
|
|
int delta;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
delta = first[i] - second[i];
|
|
|
|
if (delta > tol || delta < -tol) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test out the normal blur style with a wide range of sigmas
|
2016-04-05 19:59:06 +00:00
|
|
|
DEF_TEST(BlurSigmaRange, reporter) {
|
2013-09-05 16:39:03 +00:00
|
|
|
static const int kSize = 100;
|
|
|
|
|
|
|
|
// The geometry is offset a smidge to trigger:
|
|
|
|
// https://code.google.com/p/chromium/issues/detail?id=282418
|
|
|
|
SkPath rectPath;
|
|
|
|
rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
|
|
|
|
|
|
|
|
SkPoint polyPts[] = {
|
|
|
|
{ 0.3f, 0.3f },
|
|
|
|
{ 100.3f, 0.3f },
|
|
|
|
{ 100.3f, 100.3f },
|
|
|
|
{ 0.3f, 100.3f },
|
|
|
|
{ 2.3f, 50.3f } // a little divet to throw off the rect special case
|
|
|
|
};
|
|
|
|
SkPath polyPath;
|
|
|
|
polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
|
|
|
|
|
|
|
|
int rectSpecialCaseResult[kSize];
|
|
|
|
int generalCaseResult[kSize];
|
|
|
|
int groundTruthResult[kSize];
|
|
|
|
int bruteForce1DResult[kSize];
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
SkScalar sigma = 10.0f;
|
2013-09-05 16:39:03 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i, sigma /= 10) {
|
|
|
|
|
|
|
|
cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
|
|
|
|
cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
|
2014-06-17 20:39:29 +00:00
|
|
|
|
2013-09-05 16:39:03 +00:00
|
|
|
ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
|
|
|
|
brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
|
|
|
|
REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
|
|
|
|
REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
|
|
|
|
|
|
|
|
#if WRITE_CSV
|
|
|
|
write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
|
|
|
|
write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
|
|
|
|
write_as_csv("GPU", sigma, gpuResult, kSize);
|
|
|
|
write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
|
|
|
|
write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:20:16 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-03-12 18:03:47 +00:00
|
|
|
static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma, SkBlurStyle style) {
|
2014-04-29 15:20:16 +00:00
|
|
|
if (kNormal_SkBlurStyle != style) {
|
|
|
|
return; // blurdrawlooper only supports normal
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkColor color = 0xFF335577;
|
|
|
|
const SkScalar dx = 10;
|
|
|
|
const SkScalar dy = -5;
|
2017-03-23 20:23:38 +00:00
|
|
|
sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy));
|
|
|
|
const bool expectSuccess = sigma > 0;
|
2014-04-29 15:20:16 +00:00
|
|
|
|
2016-03-21 20:25:16 +00:00
|
|
|
if (nullptr == lp) {
|
2014-04-29 15:20:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, sigma <= 0);
|
|
|
|
} else {
|
|
|
|
SkDrawLooper::BlurShadowRec rec;
|
|
|
|
bool success = lp->asABlurShadow(&rec);
|
|
|
|
REPORTER_ASSERT(reporter, success == expectSuccess);
|
|
|
|
if (success) {
|
|
|
|
REPORTER_ASSERT(reporter, rec.fSigma == sigma);
|
|
|
|
REPORTER_ASSERT(reporter, rec.fOffset.x() == dx);
|
|
|
|
REPORTER_ASSERT(reporter, rec.fOffset.y() == dy);
|
|
|
|
REPORTER_ASSERT(reporter, rec.fColor == color);
|
|
|
|
REPORTER_ASSERT(reporter, rec.fStyle == style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 20:25:16 +00:00
|
|
|
static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma,
|
2018-03-12 18:03:47 +00:00
|
|
|
SkBlurStyle style, bool expectSuccess) {
|
2014-04-29 15:20:16 +00:00
|
|
|
SkDrawLooper::BlurShadowRec rec;
|
|
|
|
bool success = lp->asABlurShadow(&rec);
|
|
|
|
REPORTER_ASSERT(reporter, success == expectSuccess);
|
|
|
|
if (success != expectSuccess) {
|
|
|
|
lp->asABlurShadow(&rec);
|
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
REPORTER_ASSERT(reporter, rec.fSigma == sigma);
|
|
|
|
REPORTER_ASSERT(reporter, rec.fStyle == style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
|
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
|
|
|
|
|
|
|
info.fPaintBits = 0;
|
2016-11-03 18:45:31 +00:00
|
|
|
info.fColorMode = SkBlendMode::kDst;
|
2014-04-29 15:20:16 +00:00
|
|
|
builder->addLayer(info);
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:02:58 +00:00
|
|
|
static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) {
|
2014-04-29 15:20:16 +00:00
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
|
|
|
|
|
|
|
info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
|
2016-11-03 18:45:31 +00:00
|
|
|
info.fColorMode = SkBlendMode::kSrc;
|
2014-04-29 15:20:16 +00:00
|
|
|
SkPaint* paint = builder->addLayer(info);
|
2016-04-04 17:02:58 +00:00
|
|
|
paint->setMaskFilter(std::move(mf));
|
2014-04-29 15:20:16 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 17:02:58 +00:00
|
|
|
static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf,
|
2018-03-12 18:03:47 +00:00
|
|
|
SkScalar sigma, SkBlurStyle style, bool expectSuccess) {
|
2014-04-29 15:20:16 +00:00
|
|
|
|
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
|
|
|
SkLayerDrawLooper::Builder builder;
|
|
|
|
|
|
|
|
// 1 layer is too few
|
|
|
|
make_noop_layer(&builder);
|
2018-03-12 18:03:47 +00:00
|
|
|
test_looper(reporter, builder.detach(), sigma, style, false);
|
2014-04-29 15:20:16 +00:00
|
|
|
|
|
|
|
// 2 layers is good, but need blur
|
|
|
|
make_noop_layer(&builder);
|
|
|
|
make_noop_layer(&builder);
|
2018-03-12 18:03:47 +00:00
|
|
|
test_looper(reporter, builder.detach(), sigma, style, false);
|
2014-04-29 15:20:16 +00:00
|
|
|
|
|
|
|
// 2 layers is just right
|
|
|
|
make_noop_layer(&builder);
|
|
|
|
make_blur_layer(&builder, mf);
|
2018-03-12 18:03:47 +00:00
|
|
|
test_looper(reporter, builder.detach(), sigma, style, expectSuccess);
|
2014-04-29 15:20:16 +00:00
|
|
|
|
|
|
|
// 3 layers is too many
|
|
|
|
make_noop_layer(&builder);
|
|
|
|
make_blur_layer(&builder, mf);
|
|
|
|
make_noop_layer(&builder);
|
2018-03-12 18:03:47 +00:00
|
|
|
test_looper(reporter, builder.detach(), sigma, style, false);
|
2014-04-29 15:20:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
DEF_TEST(BlurAsABlur, reporter) {
|
2014-04-29 15:20:16 +00:00
|
|
|
const SkBlurStyle styles[] = {
|
|
|
|
kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle
|
|
|
|
};
|
|
|
|
const SkScalar sigmas[] = {
|
|
|
|
// values <= 0 should not success for a blur
|
|
|
|
-1, 0, 0.5f, 2
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test asABlur for SkBlurMaskFilter
|
|
|
|
//
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) {
|
2016-12-19 16:37:37 +00:00
|
|
|
const SkBlurStyle style = styles[i];
|
2014-04-29 15:20:16 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
|
|
|
|
const SkScalar sigma = sigmas[j];
|
2018-03-12 18:03:47 +00:00
|
|
|
for (bool respectCTM : { false, true }) {
|
|
|
|
sk_sp<SkMaskFilter> mf(SkMaskFilter::MakeBlur(style, sigma, respectCTM));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == mf.get()) {
|
2014-04-29 15:20:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, sigma <= 0);
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, sigma > 0);
|
2018-01-23 20:29:32 +00:00
|
|
|
SkMaskFilterBase::BlurRec rec;
|
|
|
|
bool success = as_MFB(mf)->asABlur(&rec);
|
2018-03-12 18:03:47 +00:00
|
|
|
if (respectCTM) {
|
2014-04-29 15:20:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, success);
|
|
|
|
REPORTER_ASSERT(reporter, rec.fSigma == sigma);
|
|
|
|
REPORTER_ASSERT(reporter, rec.fStyle == style);
|
2018-03-12 18:03:47 +00:00
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, !success);
|
2014-04-29 15:20:16 +00:00
|
|
|
}
|
2018-03-12 18:03:47 +00:00
|
|
|
test_layerDrawLooper(reporter, std::move(mf), sigma, style, success);
|
2014-04-29 15:20:16 +00:00
|
|
|
}
|
2018-03-12 18:03:47 +00:00
|
|
|
test_blurDrawLooper(reporter, sigma, style);
|
2014-04-29 15:20:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test asABlur for SkEmbossMaskFilter -- should never succeed
|
|
|
|
//
|
|
|
|
{
|
|
|
|
SkEmbossMaskFilter::Light light = {
|
|
|
|
{ 1, 1, 1 }, 0, 127, 127
|
|
|
|
};
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
|
|
|
|
const SkScalar sigma = sigmas[j];
|
2016-04-04 17:02:58 +00:00
|
|
|
auto mf(SkEmbossMaskFilter::Make(sigma, light));
|
|
|
|
if (mf) {
|
2018-01-23 20:29:32 +00:00
|
|
|
SkMaskFilterBase::BlurRec rec;
|
|
|
|
bool success = as_MFB(mf)->asABlur(&rec);
|
2014-04-29 15:20:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, !success);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 16:03:52 +00:00
|
|
|
// This exercises the problem discovered in crbug.com/570232. The return value from
|
2016-01-04 19:20:25 +00:00
|
|
|
// SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create
|
2016-06-28 15:07:26 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) {
|
2016-01-04 19:20:25 +00:00
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
|
2020-07-06 14:56:46 +00:00
|
|
|
auto surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
|
2016-01-04 19:20:25 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
|
|
|
|
SkRRect rr = SkRRect::MakeRectXY(r, 10, 10);
|
|
|
|
|
|
|
|
SkPaint p;
|
2018-03-12 18:03:47 +00:00
|
|
|
p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 0.01f));
|
2016-01-04 19:20:25 +00:00
|
|
|
|
|
|
|
canvas->drawRRect(rr, p);
|
|
|
|
}
|
|
|
|
|
2016-08-10 19:00:09 +00:00
|
|
|
DEF_TEST(BlurredRRectNinePatchComputation, reporter) {
|
|
|
|
const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
|
2016-08-16 16:30:03 +00:00
|
|
|
static const SkScalar kBlurRad = 3.0f;
|
2016-08-10 19:00:09 +00:00
|
|
|
|
|
|
|
bool ninePatchable;
|
|
|
|
SkRRect rrectToDraw;
|
|
|
|
SkISize size;
|
2020-10-21 17:01:34 +00:00
|
|
|
SkScalar rectXs[SkGpuBlurUtils::kBlurRRectMaxDivisions],
|
|
|
|
rectYs[SkGpuBlurUtils::kBlurRRectMaxDivisions];
|
|
|
|
SkScalar texXs[SkGpuBlurUtils::kBlurRRectMaxDivisions],
|
|
|
|
texYs[SkGpuBlurUtils::kBlurRRectMaxDivisions];
|
2016-08-10 19:00:09 +00:00
|
|
|
|
|
|
|
// not nine-patchable
|
|
|
|
{
|
|
|
|
SkVector radii[4] = { { 100, 100 }, { 0, 0 }, { 100, 100 }, { 0, 0 } };
|
|
|
|
|
|
|
|
SkRRect rr;
|
|
|
|
rr.setRectRadii(r, radii);
|
|
|
|
|
2020-10-21 17:01:34 +00:00
|
|
|
ninePatchable = SkGpuBlurUtils::ComputeBlurredRRectParams(rr, rr, kBlurRad, kBlurRad,
|
|
|
|
&rrectToDraw, &size,
|
|
|
|
rectXs, rectYs, texXs, texYs);
|
2016-08-10 19:00:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, !ninePatchable);
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple circular
|
|
|
|
{
|
2016-08-16 16:30:03 +00:00
|
|
|
static const SkScalar kCornerRad = 10.0f;
|
2016-08-10 19:00:09 +00:00
|
|
|
SkRRect rr;
|
2016-08-16 16:30:03 +00:00
|
|
|
rr.setRectXY(r, kCornerRad, kCornerRad);
|
2016-08-10 19:00:09 +00:00
|
|
|
|
2020-10-21 17:01:34 +00:00
|
|
|
ninePatchable = SkGpuBlurUtils::ComputeBlurredRRectParams(rr, rr, kBlurRad, kBlurRad,
|
|
|
|
&rrectToDraw, &size,
|
|
|
|
rectXs, rectYs, texXs, texYs);
|
2016-08-16 16:30:03 +00:00
|
|
|
|
|
|
|
static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
|
2016-08-10 19:00:09 +00:00
|
|
|
REPORTER_ASSERT(reporter, ninePatchable);
|
2016-08-16 16:30:03 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
|
2016-08-10 19:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// simple elliptical
|
|
|
|
{
|
2016-08-16 16:30:03 +00:00
|
|
|
static const SkScalar kXCornerRad = 2.0f;
|
|
|
|
static const SkScalar kYCornerRad = 10.0f;
|
2016-08-10 19:00:09 +00:00
|
|
|
SkRRect rr;
|
2016-08-16 16:30:03 +00:00
|
|
|
rr.setRectXY(r, kXCornerRad, kYCornerRad);
|
|
|
|
|
2020-10-21 17:01:34 +00:00
|
|
|
ninePatchable = SkGpuBlurUtils::ComputeBlurredRRectParams(rr, rr, kBlurRad, kBlurRad,
|
|
|
|
&rrectToDraw, &size,
|
|
|
|
rectXs, rectYs, texXs, texYs);
|
2016-08-16 16:30:03 +00:00
|
|
|
|
|
|
|
static const SkScalar kXAns = 12.0f * kBlurRad + 2.0f * kXCornerRad + 1.0f;
|
|
|
|
static const SkScalar kYAns = 12.0f * kBlurRad + 2.0f * kYCornerRad + 1.0f;
|
2016-08-10 19:00:09 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, ninePatchable);
|
2016-08-16 16:30:03 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kXAns));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kYAns));
|
|
|
|
}
|
2016-08-10 19:00:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 22:54:23 +00:00
|
|
|
// https://crbugs.com/787712
|
|
|
|
DEF_TEST(EmbossPerlinCrash, reporter) {
|
|
|
|
SkPaint p;
|
|
|
|
|
|
|
|
static constexpr SkEmbossMaskFilter::Light light = {
|
|
|
|
{ 1, 1, 1 }, 0, 127, 127
|
|
|
|
};
|
|
|
|
p.setMaskFilter(SkEmbossMaskFilter::Make(1, light));
|
|
|
|
p.setShader(SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f));
|
|
|
|
|
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
|
|
|
|
surface->getCanvas()->drawPaint(p);
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:20:16 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
2018-02-15 16:38:20 +00:00
|
|
|
|
|
|
|
DEF_TEST(BlurZeroSigma, reporter) {
|
|
|
|
auto surf = SkSurface::MakeRasterN32Premul(20, 20);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
const SkIRect ir = { 5, 5, 15, 15 };
|
|
|
|
const SkRect r = SkRect::Make(ir);
|
|
|
|
|
|
|
|
const SkScalar sigmas[] = { 0, SkBits2Float(1) };
|
|
|
|
// if sigma is zero (or nearly so), we need to draw correctly (unblurred) and not crash
|
|
|
|
// or assert.
|
|
|
|
for (auto sigma : sigmas) {
|
2018-03-12 18:03:47 +00:00
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma));
|
2018-02-15 16:38:20 +00:00
|
|
|
surf->getCanvas()->drawRect(r, paint);
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::PixelIter iter(surf.get());
|
2018-02-15 16:38:20 +00:00
|
|
|
SkIPoint loc;
|
|
|
|
while (const SkPMColor* p = (const SkPMColor*)iter.next(&loc)) {
|
|
|
|
if (ir.contains(loc.fX, loc.fY)) {
|
|
|
|
// inside the rect we draw (opaque black)
|
|
|
|
REPORTER_ASSERT(reporter, *p == SkPackARGB32(0xFF, 0, 0, 0));
|
|
|
|
} else {
|
|
|
|
// outside the rect we didn't draw at all, no blurred edges
|
|
|
|
REPORTER_ASSERT(reporter, *p == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 15:12:00 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BlurMaskBiggerThanDest, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
auto context = ctxInfo.directContext();
|
2018-04-23 15:12:00 +00:00
|
|
|
|
|
|
|
SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
|
|
|
sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
|
|
|
|
if (!dst) {
|
|
|
|
ERRORF(reporter, "Could not create surface for test.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 3));
|
|
|
|
|
|
|
|
SkCanvas* canvas = dst->getCanvas();
|
|
|
|
|
|
|
|
canvas->clear(SK_ColorBLACK);
|
|
|
|
canvas->drawCircle(SkPoint::Make(16, 16), 8, p);
|
|
|
|
|
|
|
|
SkBitmap readback;
|
|
|
|
SkAssertResult(readback.tryAllocPixels(ii));
|
|
|
|
|
|
|
|
canvas->readPixels(readback, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, SkColorGetR(readback.getColor(15, 15)) > 128);
|
|
|
|
REPORTER_ASSERT(reporter, SkColorGetG(readback.getColor(15, 15)) == 0);
|
|
|
|
REPORTER_ASSERT(reporter, SkColorGetB(readback.getColor(15, 15)) == 0);
|
|
|
|
REPORTER_ASSERT(reporter, readback.getColor(31, 31) == SK_ColorBLACK);
|
|
|
|
}
|
2018-09-15 16:15:27 +00:00
|
|
|
|
|
|
|
DEF_TEST(zero_blur, reporter) {
|
|
|
|
SkBitmap alpha, bitmap;
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3));
|
|
|
|
SkIPoint offset;
|
|
|
|
bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
|
|
|
|
}
|