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.
|
|
|
|
*/
|
|
|
|
|
2015-01-26 20:49:00 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2014-01-20 19:58:28 +00:00
|
|
|
#include "SkBitmapSource.h"
|
|
|
|
#include "SkBlurImageFilter.h"
|
2015-01-26 19:24:32 +00:00
|
|
|
#include "SkColor.h"
|
2014-01-20 19:58:28 +00:00
|
|
|
#include "SkDisplacementMapEffect.h"
|
|
|
|
#include "SkDropShadowImageFilter.h"
|
|
|
|
#include "SkGradientShader.h"
|
2014-03-13 19:45:41 +00:00
|
|
|
#include "SkLightingImageFilter.h"
|
2015-01-26 19:24:32 +00:00
|
|
|
#include "SkMatrixImageFilter.h"
|
2014-01-20 19:58:28 +00:00
|
|
|
#include "SkMorphologyImageFilter.h"
|
2014-02-05 17:51:22 +00:00
|
|
|
#include "SkOffsetImageFilter.h"
|
2014-03-03 14:58:09 +00:00
|
|
|
#include "SkPerlinNoiseShader.h"
|
|
|
|
#include "SkRectShaderImageFilter.h"
|
2014-01-20 19:58:28 +00:00
|
|
|
#include "SkScalar.h"
|
2015-01-26 19:24:32 +00:00
|
|
|
#include "gm.h"
|
2014-01-20 19:58:28 +00:00
|
|
|
|
2014-02-19 22:10:12 +00:00
|
|
|
#define RESIZE_FACTOR SkIntToScalar(4)
|
|
|
|
|
2014-01-20 19:58:28 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class ImageFiltersScaledGM : public GM {
|
|
|
|
public:
|
|
|
|
ImageFiltersScaledGM() : fInitialized(false) {
|
|
|
|
this->setBGColor(0x00000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2014-01-20 19:58:28 +00:00
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("imagefiltersscaled");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(1428, 500);
|
2014-01-20 19:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void make_gradient_circle(int width, int height) {
|
|
|
|
SkScalar x = SkIntToScalar(width / 2);
|
|
|
|
SkScalar y = SkIntToScalar(height / 2);
|
|
|
|
SkScalar radius = SkScalarMul(SkMinScalar(x, y), SkIntToScalar(4) / SkIntToScalar(5));
|
2014-01-25 16:46:20 +00:00
|
|
|
fGradientCircle.allocN32Pixels(width, height);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(fGradientCircle);
|
2014-01-20 19:58:28 +00:00
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkColor colors[2];
|
|
|
|
colors[0] = SK_ColorWHITE;
|
|
|
|
colors[1] = SK_ColorBLACK;
|
|
|
|
SkAutoTUnref<SkShader> shader(
|
|
|
|
SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2,
|
|
|
|
SkShader::kClamp_TileMode)
|
|
|
|
);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(shader);
|
|
|
|
canvas.drawCircle(x, y, radius, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
if (!fInitialized) {
|
2015-01-26 19:24:32 +00:00
|
|
|
fCheckerboard.allocN32Pixels(64, 64);
|
|
|
|
SkCanvas checkerboardCanvas(fCheckerboard);
|
2015-01-26 20:49:00 +00:00
|
|
|
sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0xFF404040, 8);
|
2015-01-26 19:24:32 +00:00
|
|
|
|
2014-01-20 19:58:28 +00:00
|
|
|
this->make_gradient_circle(64, 64);
|
|
|
|
fInitialized = true;
|
|
|
|
}
|
|
|
|
canvas->clear(0x00000000);
|
|
|
|
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCircle));
|
|
|
|
SkAutoTUnref<SkImageFilter> checkerboard(SkBitmapSource::Create(fCheckerboard));
|
2014-03-03 14:58:09 +00:00
|
|
|
SkAutoTUnref<SkShader> noise(SkPerlinNoiseShader::CreateFractalNoise(
|
|
|
|
SkDoubleToScalar(0.1), SkDoubleToScalar(0.05), 1, 0));
|
2014-01-20 19:58:28 +00:00
|
|
|
|
2014-03-13 19:45:41 +00:00
|
|
|
SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
|
|
|
|
SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
|
|
|
|
SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
|
|
|
|
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
|
|
|
|
2014-01-20 19:58:28 +00:00
|
|
|
SkImageFilter* filters[] = {
|
2014-03-10 10:51:58 +00:00
|
|
|
SkBlurImageFilter::Create(SkIntToScalar(4), SkIntToScalar(4)),
|
2014-07-22 19:49:25 +00:00
|
|
|
SkDropShadowImageFilter::Create(SkIntToScalar(5), SkIntToScalar(10),
|
2014-10-23 20:59:52 +00:00
|
|
|
SkIntToScalar(3), SkIntToScalar(3), SK_ColorYELLOW,
|
|
|
|
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(1, 1, checkerboard.get()),
|
|
|
|
SkErodeImageFilter::Create(1, 1, checkerboard.get()),
|
|
|
|
SkOffsetImageFilter::Create(SkIntToScalar(32), 0),
|
2015-03-16 17:08:34 +00:00
|
|
|
SkMatrixImageFilter::Create(resizeMatrix, kNone_SkFilterQuality),
|
2014-03-03 14:58:09 +00:00
|
|
|
SkRectShaderImageFilter::Create(noise),
|
2014-03-13 19:45:41 +00:00
|
|
|
SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd),
|
|
|
|
SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent,
|
|
|
|
cutoffAngle, white, surfaceScale, kd),
|
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);
|
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
|
|
|
}
|
2014-01-20 19:58:28 +00:00
|
|
|
canvas->drawCircle(r.centerX(), r.centerY(),
|
|
|
|
SkScalarDiv(r.width()*2, SkIntToScalar(5)), paint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(r.width() * scales[j].fX + margin, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() * scales[j].fY + margin);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
|
|
|
|
filters[i]->unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool fInitialized;
|
|
|
|
SkBitmap fCheckerboard;
|
|
|
|
SkBitmap fGradientCircle;
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new ImageFiltersScaledGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|