2014-01-20 19:58:28 +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/SkBlurImageFilter.h"
|
|
|
|
#include "include/effects/SkDisplacementMapEffect.h"
|
|
|
|
#include "include/effects/SkDropShadowImageFilter.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/effects/SkImageSource.h"
|
|
|
|
#include "include/effects/SkLightingImageFilter.h"
|
|
|
|
#include "include/effects/SkMorphologyImageFilter.h"
|
|
|
|
#include "include/effects/SkOffsetImageFilter.h"
|
|
|
|
#include "include/effects/SkPaintImageFilter.h"
|
|
|
|
#include "include/effects/SkPerlinNoiseShader.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2014-01-20 19:58:28 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2014-02-19 22:10:12 +00:00
|
|
|
#define RESIZE_FACTOR SkIntToScalar(4)
|
|
|
|
|
2016-04-08 23:28:09 +00:00
|
|
|
static sk_sp<SkImage> make_gradient_circle(int width, int height) {
|
|
|
|
SkScalar x = SkIntToScalar(width / 2);
|
|
|
|
SkScalar y = SkIntToScalar(height / 2);
|
2017-02-14 15:59:19 +00:00
|
|
|
SkScalar radius = SkMinScalar(x, y) * 4 / 5;
|
2016-04-08 23:28:09 +00:00
|
|
|
sk_sp<SkSurface> 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-08 23:28:09 +00:00
|
|
|
canvas->drawCircle(x, y, radius, paint);
|
|
|
|
|
|
|
|
return surface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-20 19:58:28 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class ImageFiltersScaledGM : public GM {
|
|
|
|
public:
|
2015-09-03 20:32:33 +00:00
|
|
|
ImageFiltersScaledGM() {
|
2014-01-20 19:58:28 +00:00
|
|
|
this->setBGColor(0x00000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkString onShortName() override {
|
2014-01-20 19:58:28 +00:00
|
|
|
return SkString("imagefiltersscaled");
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(1428, 500);
|
2014-01-20 19:58:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2016-03-17 17:51:11 +00:00
|
|
|
fCheckerboard = SkImage::MakeFromBitmap(
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::create_checkerboard_bitmap(64, 64, 0xFFA0A0A0, 0xFF404040, 8));
|
2016-04-08 23:28:09 +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-01-20 19:58:28 +00:00
|
|
|
|
2016-04-01 16:28:51 +00:00
|
|
|
sk_sp<SkImageFilter> gradient(SkImageSource::Make(fGradientCircle));
|
|
|
|
sk_sp<SkImageFilter> checkerboard(SkImageSource::Make(fCheckerboard));
|
2016-04-08 23:28:09 +00:00
|
|
|
|
2016-01-08 22:58:35 +00:00
|
|
|
SkPaint noisePaint;
|
2016-04-08 23:28:09 +00:00
|
|
|
noisePaint.setShader(SkPerlinNoiseShader::MakeFractalNoise(SkDoubleToScalar(0.1),
|
|
|
|
SkDoubleToScalar(0.05), 1, 0));
|
2014-01-20 19:58:28 +00:00
|
|
|
|
2015-07-13 20:16:44 +00:00
|
|
|
SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
|
2016-03-29 16:03:52 +00:00
|
|
|
SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10),
|
|
|
|
SkIntToScalar(-10),
|
2015-07-13 20:16:44 +00:00
|
|
|
SkIntToScalar(20));
|
|
|
|
SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
|
2014-03-13 19:45:41 +00:00
|
|
|
SkScalar spotExponent = SK_Scalar1;
|
|
|
|
SkScalar cutoffAngle = SkIntToScalar(15);
|
|
|
|
SkScalar kd = SkIntToScalar(2);
|
|
|
|
SkScalar surfaceScale = SkIntToScalar(1);
|
|
|
|
SkColor white(0xFFFFFFFF);
|
2014-03-25 17:35:10 +00:00
|
|
|
SkMatrix resizeMatrix;
|
|
|
|
resizeMatrix.setScale(RESIZE_FACTOR, RESIZE_FACTOR);
|
2014-03-13 19:45:41 +00:00
|
|
|
|
2016-04-08 23:28:09 +00:00
|
|
|
sk_sp<SkImageFilter> filters[] = {
|
|
|
|
SkBlurImageFilter::Make(SkIntToScalar(4), SkIntToScalar(4), nullptr),
|
|
|
|
SkDropShadowImageFilter::Make(
|
|
|
|
SkIntToScalar(5), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(3), SkIntToScalar(3), SK_ColorYELLOW,
|
|
|
|
SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
|
|
|
|
nullptr),
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::Make(SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkIntToScalar(12),
|
|
|
|
std::move(gradient),
|
|
|
|
checkerboard),
|
2016-04-08 23:28:09 +00:00
|
|
|
SkDilateImageFilter::Make(1, 1, checkerboard),
|
|
|
|
SkErodeImageFilter::Make(1, 1, checkerboard),
|
|
|
|
SkOffsetImageFilter::Make(SkIntToScalar(32), 0, nullptr),
|
|
|
|
SkImageFilter::MakeMatrixFilter(resizeMatrix, kNone_SkFilterQuality, nullptr),
|
|
|
|
SkPaintImageFilter::Make(noisePaint),
|
|
|
|
SkLightingImageFilter::MakePointLitDiffuse(pointLocation, white, surfaceScale, kd,
|
|
|
|
nullptr),
|
|
|
|
SkLightingImageFilter::MakeSpotLitDiffuse(spotLocation, spotTarget, spotExponent,
|
|
|
|
cutoffAngle, white, surfaceScale, kd,
|
|
|
|
nullptr),
|
2014-01-20 19:58:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkVector scales[] = {
|
|
|
|
SkVector::Make(SkScalarInvert(2), SkScalarInvert(2)),
|
|
|
|
SkVector::Make(SkIntToScalar(1), SkIntToScalar(1)),
|
|
|
|
SkVector::Make(SkIntToScalar(1), SkIntToScalar(2)),
|
|
|
|
SkVector::Make(SkIntToScalar(2), SkIntToScalar(1)),
|
|
|
|
SkVector::Make(SkIntToScalar(2), SkIntToScalar(2)),
|
|
|
|
};
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
|
|
|
|
SkScalar margin = SkIntToScalar(16);
|
|
|
|
SkRect bounds = r;
|
|
|
|
bounds.outset(margin, margin);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(scales); ++j) {
|
|
|
|
canvas->save();
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setImageFilter(filters[i]);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(scales[j].fX, scales[j].fY);
|
2015-08-20 18:10:41 +00:00
|
|
|
canvas->clipRect(r);
|
2014-02-05 17:51:22 +00:00
|
|
|
if (5 == i) {
|
|
|
|
canvas->translate(SkIntToScalar(-32), 0);
|
2014-02-19 22:10:12 +00:00
|
|
|
} else if (6 == i) {
|
|
|
|
canvas->scale(SkScalarInvert(RESIZE_FACTOR),
|
|
|
|
SkScalarInvert(RESIZE_FACTOR));
|
2014-02-05 17:51:22 +00:00
|
|
|
}
|
2015-05-12 17:37:34 +00:00
|
|
|
canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
|
2014-01-20 19:58:28 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(r.width() * scales[j].fX + margin, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() * scales[j].fY + margin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fCheckerboard, fGradientCircle;
|
2015-09-03 20:32:33 +00:00
|
|
|
|
2014-01-20 19:58:28 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
DEF_GM(return new ImageFiltersScaledGM;)
|
2014-01-20 19:58:28 +00:00
|
|
|
}
|