2014-03-14 17:44:41 +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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageFilter.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"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.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"
|
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"
|
2014-03-14 17:44:41 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2017-01-12 22:15:50 +00:00
|
|
|
namespace {
|
2014-03-14 17:44:41 +00:00
|
|
|
|
2021-01-24 13:57:23 +00:00
|
|
|
sk_sp<SkImage> make_checkerboard();
|
2017-01-12 22:15:50 +00:00
|
|
|
sk_sp<SkImage> make_gradient_circle(int width, int height);
|
2021-01-24 13:57:23 +00:00
|
|
|
void draw(SkCanvas*, sk_sp<SkImage>, const SkIRect&, sk_sp<SkImageFilter>);
|
2014-03-14 17:44:41 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|
2014-03-14 17:44:41 +00:00
|
|
|
|
2017-01-12 22:15:50 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-09-15 18:26:13 +00:00
|
|
|
|
2017-01-12 22:15:50 +00:00
|
|
|
DEF_SIMPLE_GM(imagefilterscropexpand, canvas, 730, 650) {
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
|
2017-01-12 22:15:50 +00:00
|
|
|
|
|
|
|
sk_sp<SkImage> gradientCircle(make_gradient_circle(64, 64));
|
2021-01-24 13:57:23 +00:00
|
|
|
auto checkerboard = make_checkerboard();
|
2017-01-12 22:15:50 +00:00
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> gradientCircleSource(SkImageFilters::Image(std::move(gradientCircle)));
|
|
|
|
sk_sp<SkImageFilter> noopCropped(SkImageFilters::Offset(0, 0, nullptr, &cropRect));
|
2017-01-12 22:15:50 +00:00
|
|
|
// This color matrix saturates the green component but only partly increases the opacity.
|
|
|
|
// For the opaque checkerboard, the opacity boost doesn't matter but it does impact the
|
|
|
|
// area outside the checkerboard.
|
2019-04-30 16:18:54 +00:00
|
|
|
float matrix[20] = { 1, 0, 0, 0, 0,
|
|
|
|
0, 1, 0, 0, 1,
|
|
|
|
0, 0, 1, 0, 0,
|
|
|
|
0, 0, 0, 1, 32.0f/255 };
|
|
|
|
sk_sp<SkColorFilter> cfAlphaTrans(SkColorFilters::Matrix(matrix));
|
2017-01-12 22:15:50 +00:00
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
|
|
|
|
SkScalar MARGIN = SkIntToScalar(12);
|
|
|
|
|
|
|
|
SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
|
|
|
|
SkScalar kd = SkIntToScalar(2);
|
|
|
|
SkScalar surfaceScale = SkIntToScalar(1);
|
|
|
|
SkIRect bounds;
|
|
|
|
r.roundOut(&bounds);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->translate(MARGIN, MARGIN);
|
|
|
|
for (int outset = -15; outset <= 20; outset += 5) {
|
|
|
|
canvas->save();
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect bigRect = cropRect;
|
|
|
|
bigRect.outset(outset, outset);
|
2017-01-12 22:15:50 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
|
|
|
SkImageFilters::ColorFilter(cfAlphaTrans, noopCropped, &bigRect));
|
2017-01-12 22:15:50 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
|
|
|
SkImageFilters::Blur(0.3f, 0.3f, noopCropped, &bigRect));
|
2017-01-12 22:15:50 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
|
|
|
SkImageFilters::Blur(8.0f, 8.0f, noopCropped, &bigRect));
|
2017-01-12 22:15:50 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
|
|
|
SkImageFilters::Dilate(2, 2, noopCropped, &bigRect));
|
2017-01-12 22:15:50 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
|
|
|
SkImageFilters::Erode(2, 2, noopCropped, &bigRect));
|
2017-01-12 22:15:50 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::DropShadow(10, 10, 3, 3, SK_ColorBLUE, noopCropped, &bigRect));
|
2017-01-12 22:15:50 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kR, 12,
|
|
|
|
gradientCircleSource, noopCropped, &bigRect));
|
2016-02-16 21:26:56 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::Offset(SkIntToScalar(-8), SkIntToScalar(16), noopCropped, &bigRect));
|
2015-09-15 18:26:13 +00:00
|
|
|
|
2019-08-05 14:24:49 +00:00
|
|
|
draw(canvas, checkerboard, bigRect,
|
2019-08-02 19:21:23 +00:00
|
|
|
SkImageFilters::PointLitDiffuse(pointLocation, SK_ColorWHITE, surfaceScale, kd,
|
|
|
|
noopCropped, &bigRect));
|
2017-01-12 22:15:50 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, SkIntToScalar(80));
|
2015-09-15 18:26:13 +00:00
|
|
|
}
|
2017-01-12 22:15:50 +00:00
|
|
|
}
|
2015-09-15 18:26:13 +00:00
|
|
|
|
2017-01-12 22:15:50 +00:00
|
|
|
namespace {
|
2021-01-24 13:57:23 +00:00
|
|
|
sk_sp<SkImage> make_checkerboard() {
|
|
|
|
auto surf = SkSurface::MakeRasterN32Premul(64, 64);
|
|
|
|
auto canvas = surf->getCanvas();
|
|
|
|
canvas->clear(0xFFFF0000);
|
2014-03-14 17:44:41 +00:00
|
|
|
SkPaint darkPaint;
|
2018-08-16 14:17:03 +00:00
|
|
|
darkPaint.setColor(0xFF404040);
|
2014-03-14 17:44:41 +00:00
|
|
|
SkPaint lightPaint;
|
2018-08-16 14:17:03 +00:00
|
|
|
lightPaint.setColor(0xFFA0A0A0);
|
2014-03-14 17:44:41 +00:00
|
|
|
for (int y = 8; y < 48; y += 16) {
|
|
|
|
for (int x = 8; x < 48; x += 16) {
|
2021-01-24 13:57:23 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
|
|
|
|
canvas->restore();
|
2014-03-14 17:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-24 13:57:23 +00:00
|
|
|
return surf->makeImageSnapshot();
|
2014-03-14 17:44:41 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 22:15:50 +00:00
|
|
|
sk_sp<SkImage> make_gradient_circle(int width, int height) {
|
2014-03-14 17:44:41 +00:00
|
|
|
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-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRasterN32Premul(width, height));
|
2015-09-15 18:26:13 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->clear(0x00000000);
|
2014-03-14 17:44:41 +00:00
|
|
|
SkColor colors[2];
|
|
|
|
colors[0] = SK_ColorWHITE;
|
|
|
|
colors[1] = SK_ColorBLACK;
|
|
|
|
SkPaint paint;
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr,
|
2019-04-03 14:27:45 +00:00
|
|
|
2, SkTileMode::kClamp));
|
2015-09-15 18:26:13 +00:00
|
|
|
canvas->drawCircle(x, y, radius, paint);
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2014-03-14 17:44:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 13:57:23 +00:00
|
|
|
void draw(SkCanvas* canvas, sk_sp<SkImage> image, const SkIRect& layerRect,
|
2016-03-31 16:05:49 +00:00
|
|
|
sk_sp<SkImageFilter> filter) {
|
2019-08-05 14:24:49 +00:00
|
|
|
// Convert SkIRect to SkRect since that's what saveLayer and drawRect need
|
|
|
|
SkRect rect = SkRect::Make(layerRect);
|
|
|
|
|
2014-03-14 17:44:41 +00:00
|
|
|
SkPaint paint;
|
2016-03-31 16:05:49 +00:00
|
|
|
paint.setImageFilter(std::move(filter));
|
2014-03-14 17:44:41 +00:00
|
|
|
canvas->saveLayer(&rect, &paint);
|
2021-01-24 13:57:23 +00:00
|
|
|
canvas->drawImage(image, 0, 0);
|
2014-03-14 17:44:41 +00:00
|
|
|
canvas->restore();
|
2014-03-15 03:02:32 +00:00
|
|
|
|
2014-03-14 17:44:41 +00:00
|
|
|
SkPaint strokePaint;
|
|
|
|
strokePaint.setColor(0xFFFF0000);
|
|
|
|
strokePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(rect, strokePaint);
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(80), 0);
|
|
|
|
}
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace
|