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.
|
|
|
|
*/
|
|
|
|
|
2015-01-26 20:49:00 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2014-02-05 17:51:22 +00:00
|
|
|
#include "SkBlurImageFilter.h"
|
2015-01-26 19:24:32 +00:00
|
|
|
#include "SkColor.h"
|
2014-02-05 17:51:22 +00:00
|
|
|
#include "SkDisplacementMapEffect.h"
|
|
|
|
#include "SkDropShadowImageFilter.h"
|
|
|
|
#include "SkGradientShader.h"
|
2015-09-15 18:26:13 +00:00
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkImageSource.h"
|
2015-08-26 21:26:40 +00:00
|
|
|
#include "SkLightingImageFilter.h"
|
2014-02-05 17:51:22 +00:00
|
|
|
#include "SkMorphologyImageFilter.h"
|
|
|
|
#include "SkOffsetImageFilter.h"
|
2014-03-03 14:58:09 +00:00
|
|
|
#include "SkPerlinNoiseShader.h"
|
2015-08-26 21:26:40 +00:00
|
|
|
#include "SkPoint3.h"
|
2014-03-03 14:58:09 +00:00
|
|
|
#include "SkRectShaderImageFilter.h"
|
2014-02-05 17:51:22 +00:00
|
|
|
#include "SkScalar.h"
|
2015-09-15 18:26:13 +00:00
|
|
|
#include "SkSurface.h"
|
2015-01-26 19:24:32 +00:00
|
|
|
#include "gm.h"
|
2014-02-05 17:51:22 +00:00
|
|
|
|
2014-02-19 22:10:12 +00:00
|
|
|
#define RESIZE_FACTOR_X SkIntToScalar(2)
|
|
|
|
#define RESIZE_FACTOR_Y SkIntToScalar(5)
|
|
|
|
|
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 makeGradientCircle(int width, int height) {
|
2014-02-05 17:51:22 +00:00
|
|
|
SkScalar x = SkIntToScalar(width / 2);
|
|
|
|
SkScalar y = SkIntToScalar(height / 2);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkScalar radius = SkMinScalar(x, y) * 0.8f;
|
2015-09-15 18:26:13 +00:00
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(width, height));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->clear(0x00000000);
|
2014-02-05 17:51:22 +00:00
|
|
|
SkColor colors[2];
|
|
|
|
colors[0] = SK_ColorWHITE;
|
|
|
|
colors[1] = SK_ColorBLACK;
|
|
|
|
SkAutoTUnref<SkShader> shader(
|
2015-08-27 14:41:13 +00:00
|
|
|
SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, nullptr, 2,
|
2014-02-05 17:51:22 +00:00
|
|
|
SkShader::kClamp_TileMode)
|
|
|
|
);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(shader);
|
2015-09-15 18:26:13 +00:00
|
|
|
canvas->drawCircle(x, y, radius, paint);
|
|
|
|
fGradientCircle.reset(surface->newImageSnapshot());
|
2014-02-05 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:38:06 +00:00
|
|
|
static void draw_clipped_filter(SkCanvas* canvas, SkImageFilter* filter, size_t i,
|
|
|
|
const SkRect& primBounds, const SkRect& clipBounds) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
paint.setImageFilter(filter);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2015-09-15 18:26:13 +00:00
|
|
|
fCheckerboard.reset(SkImage::NewFromBitmap
|
|
|
|
(sk_tool_utils::create_checkerboard_bitmap(64, 64, 0xFFA0A0A0, 0xFF404040, 8)));
|
2015-09-03 20:32:33 +00:00
|
|
|
this->makeGradientCircle(64, 64);
|
|
|
|
}
|
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
|
|
|
|
2015-09-15 18:26:13 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> gradient(SkImageSource::Create(fGradientCircle));
|
|
|
|
SkAutoTUnref<SkImageFilter> checkerboard(SkImageSource::Create(fCheckerboard));
|
2014-03-03 14:58:09 +00:00
|
|
|
SkAutoTUnref<SkShader> noise(SkPerlinNoiseShader::CreateFractalNoise(
|
|
|
|
SkDoubleToScalar(0.1), SkDoubleToScalar(0.05), 1, 0));
|
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
|
|
|
|
|
|
|
SkImageFilter* filters[] = {
|
2014-03-10 10:51:58 +00:00
|
|
|
SkBlurImageFilter::Create(SkIntToScalar(12), SkIntToScalar(12)),
|
2014-07-22 19:49:25 +00:00
|
|
|
SkDropShadowImageFilter::Create(SkIntToScalar(10), SkIntToScalar(10),
|
2014-10-23 20:59:52 +00:00
|
|
|
SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN,
|
|
|
|
SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkIntToScalar(12),
|
|
|
|
gradient.get(),
|
|
|
|
checkerboard.get()),
|
|
|
|
SkDilateImageFilter::Create(2, 2, checkerboard.get()),
|
|
|
|
SkErodeImageFilter::Create(2, 2, checkerboard.get()),
|
|
|
|
SkOffsetImageFilter::Create(SkIntToScalar(-16), SkIntToScalar(32)),
|
2015-03-20 13:38:17 +00:00
|
|
|
SkImageFilter::CreateMatrixFilter(resizeMatrix, kNone_SkFilterQuality),
|
2015-08-26 21:26:40 +00:00
|
|
|
SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, SK_ColorWHITE, SK_Scalar1, SkIntToScalar(2), checkerboard.get()),
|
|
|
|
|
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
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
|
|
|
|
SkSafeUnref(filters[i]);
|
|
|
|
}
|
2015-07-21 15:38:06 +00:00
|
|
|
|
2015-09-15 18:26:13 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> rectFilter(SkRectShaderImageFilter::Create(noise));
|
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);
|
|
|
|
draw_clipped_filter(canvas, rectFilter, 0, r, bounds);
|
|
|
|
canvas->translate(0, r.height() + margin);
|
|
|
|
}
|
2014-02-05 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-15 18:26:13 +00:00
|
|
|
SkAutoTUnref<SkImage> fCheckerboard, fGradientCircle;
|
2015-09-03 20:32:33 +00:00
|
|
|
|
2014-02-05 17:51:22 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
DEF_GM(return new ImageFiltersClippedGM;)
|
2014-02-05 17:51:22 +00:00
|
|
|
}
|