2014-02-05 17:51:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkFilterQuality.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPoint3.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkScalar.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkPerlinNoiseShader.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2014-02-05 17:51:22 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2014-02-19 22:10:12 +00:00
|
|
|
#define RESIZE_FACTOR_X SkIntToScalar(2)
|
|
|
|
#define RESIZE_FACTOR_Y SkIntToScalar(5)
|
|
|
|
|
2016-04-01 16:28:51 +00:00
|
|
|
static sk_sp<SkImage> make_gradient_circle(int width, int height) {
|
|
|
|
SkScalar x = SkIntToScalar(width / 2);
|
|
|
|
SkScalar y = SkIntToScalar(height / 2);
|
2020-02-05 18:34:09 +00:00
|
|
|
SkScalar radius = std::min(x, y) * 0.8f;
|
2016-04-01 16:28:51 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(width, height));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->clear(0x00000000);
|
|
|
|
SkColor colors[2];
|
|
|
|
colors[0] = SK_ColorWHITE;
|
|
|
|
colors[1] = SK_ColorBLACK;
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr,
|
2019-04-03 14:27:45 +00:00
|
|
|
2, SkTileMode::kClamp));
|
2016-04-01 16:28:51 +00:00
|
|
|
canvas->drawCircle(x, y, radius, paint);
|
|
|
|
return surface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
2016-04-08 23:28:09 +00:00
|
|
|
static void draw_clipped_filter(SkCanvas* canvas, sk_sp<SkImageFilter> filter, size_t i,
|
2016-04-01 16:28:51 +00:00
|
|
|
const SkRect& primBounds, const SkRect& clipBounds) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
2016-04-08 23:28:09 +00:00
|
|
|
paint.setImageFilter(std::move(filter));
|
2016-04-01 16:28:51 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clipBounds);
|
|
|
|
if (5 == i) {
|
|
|
|
canvas->translate(SkIntToScalar(16), SkIntToScalar(-32));
|
|
|
|
} else if (6 == i) {
|
|
|
|
canvas->scale(SkScalarInvert(RESIZE_FACTOR_X), SkScalarInvert(RESIZE_FACTOR_Y));
|
|
|
|
}
|
|
|
|
canvas->drawCircle(primBounds.centerX(), primBounds.centerY(),
|
|
|
|
primBounds.width() * 2 / 5, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2014-02-05 17:51:22 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class ImageFiltersClippedGM : public GM {
|
|
|
|
public:
|
2015-09-03 20:32:33 +00:00
|
|
|
ImageFiltersClippedGM() {
|
2014-02-05 17:51:22 +00:00
|
|
|
this->setBGColor(0x00000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-02-05 17:51:22 +00:00
|
|
|
return SkString("imagefiltersclipped");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(860, 500);
|
2014-02-05 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2020-12-23 15:11:33 +00:00
|
|
|
fCheckerboard =
|
|
|
|
ToolUtils::create_checkerboard_image(64, 64, 0xFFA0A0A0, 0xFF404040, 8);
|
2016-04-01 16:28:51 +00:00
|
|
|
fGradientCircle = make_gradient_circle(64, 64);
|
2015-09-03 20:32:33 +00:00
|
|
|
}
|
2015-01-26 19:24:32 +00:00
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-04-09 18:13:24 +00:00
|
|
|
canvas->clear(SK_ColorBLACK);
|
2014-02-05 17:51:22 +00:00
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> gradient(SkImageFilters::Image(fGradientCircle));
|
|
|
|
sk_sp<SkImageFilter> checkerboard(SkImageFilters::Image(fCheckerboard));
|
2014-03-25 17:35:10 +00:00
|
|
|
SkMatrix resizeMatrix;
|
|
|
|
resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y);
|
2015-08-26 21:26:40 +00:00
|
|
|
SkPoint3 pointLocation = SkPoint3::Make(32, 32, SkIntToScalar(10));
|
2014-02-05 17:51:22 +00:00
|
|
|
|
2016-04-08 23:28:09 +00:00
|
|
|
sk_sp<SkImageFilter> filters[] = {
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::Blur(SkIntToScalar(12), SkIntToScalar(12), nullptr),
|
|
|
|
SkImageFilters::DropShadow(SkIntToScalar(10), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN, nullptr),
|
|
|
|
SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kR,
|
|
|
|
SkIntToScalar(12), std::move(gradient), checkerboard),
|
|
|
|
SkImageFilters::Dilate(2, 2, checkerboard),
|
|
|
|
SkImageFilters::Erode(2, 2, checkerboard),
|
|
|
|
SkImageFilters::Offset(SkIntToScalar(-16), SkIntToScalar(32), nullptr),
|
|
|
|
SkImageFilters::MatrixTransform(resizeMatrix, kNone_SkFilterQuality, nullptr),
|
|
|
|
SkImageFilters::PointLitDiffuse(pointLocation, SK_ColorWHITE, SK_Scalar1,
|
|
|
|
SkIntToScalar(2), checkerboard),
|
2015-08-26 21:26:40 +00:00
|
|
|
|
2014-02-05 17:51:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
|
|
|
|
SkScalar margin = SkIntToScalar(16);
|
|
|
|
SkRect bounds = r;
|
|
|
|
bounds.outset(margin, margin);
|
|
|
|
|
2015-07-21 15:38:06 +00:00
|
|
|
canvas->save();
|
2014-02-05 17:51:22 +00:00
|
|
|
for (int xOffset = 0; xOffset < 80; xOffset += 16) {
|
|
|
|
canvas->save();
|
|
|
|
bounds.fLeft = SkIntToScalar(xOffset);
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
|
2015-07-21 15:38:06 +00:00
|
|
|
draw_clipped_filter(canvas, filters[i], i, r, bounds);
|
2014-02-05 17:51:22 +00:00
|
|
|
canvas->translate(r.width() + margin, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() + margin);
|
|
|
|
}
|
2015-07-21 15:38:06 +00:00
|
|
|
canvas->restore();
|
2014-02-06 00:36:01 +00:00
|
|
|
|
Add SkImageFilters::Shader in place of Paint factory
SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.
Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.
SkImageFilters::Paint still exists, but is deprecated to be removed
once I've confirmed clients aren't depending on it.
Bug: skia:9310
Change-Id: I11a82bda1a5d440726cf4e2b5bfaae4929568679
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/323680
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-10-07 19:27:20 +00:00
|
|
|
sk_sp<SkImageFilter> rectFilter(SkImageFilters::Shader(
|
|
|
|
SkPerlinNoiseShader::MakeFractalNoise(0.1f, 0.05f, 1, 0)));
|
2015-07-21 15:38:06 +00:00
|
|
|
canvas->translate(SK_ARRAY_COUNT(filters)*(r.width() + margin), 0);
|
|
|
|
for (int xOffset = 0; xOffset < 80; xOffset += 16) {
|
|
|
|
bounds.fLeft = SkIntToScalar(xOffset);
|
2016-04-08 23:28:09 +00:00
|
|
|
draw_clipped_filter(canvas, rectFilter, 0, r, bounds);
|
2015-07-21 15:38:06 +00:00
|
|
|
canvas->translate(0, r.height() + margin);
|
|
|
|
}
|
2014-02-05 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fCheckerboard, fGradientCircle;
|
2015-09-03 20:32:33 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2014-02-05 17:51:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
DEF_GM(return new ImageFiltersClippedGM;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|