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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkBitmapSource.h"
|
|
|
|
#include "SkBlurImageFilter.h"
|
|
|
|
#include "SkDisplacementMapEffect.h"
|
|
|
|
#include "SkDropShadowImageFilter.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkMorphologyImageFilter.h"
|
|
|
|
#include "SkOffsetImageFilter.h"
|
2014-03-03 14:58:09 +00:00
|
|
|
#include "SkPerlinNoiseShader.h"
|
|
|
|
#include "SkRectShaderImageFilter.h"
|
2014-03-25 17:35:10 +00:00
|
|
|
#include "SkMatrixImageFilter.h"
|
2014-02-05 17:51:22 +00:00
|
|
|
#include "SkScalar.h"
|
|
|
|
|
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:
|
|
|
|
ImageFiltersClippedGM() : fInitialized(false) {
|
|
|
|
this->setBGColor(0x00000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
return kSkipTiled_Flag;
|
|
|
|
}
|
|
|
|
|
2014-02-05 17:51:22 +00:00
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("imagefiltersclipped");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
|
|
|
return make_isize(860, 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_checkerboard() {
|
|
|
|
fCheckerboard.allocN32Pixels(64, 64);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(fCheckerboard);
|
2014-02-05 17:51:22 +00:00
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint darkPaint;
|
|
|
|
darkPaint.setColor(0xFF404040);
|
|
|
|
SkPaint lightPaint;
|
|
|
|
lightPaint.setColor(0xFFA0A0A0);
|
|
|
|
for (int y = 0; y < 64; y += 16) {
|
|
|
|
for (int x = 0; x < 64; x += 16) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_gradient_circle(int width, int height) {
|
|
|
|
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;
|
2014-02-05 17:51:22 +00:00
|
|
|
fGradientCircle.allocN32Pixels(width, height);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(fGradientCircle);
|
2014-02-05 17:51:22 +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) {
|
|
|
|
this->make_checkerboard();
|
|
|
|
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-03-25 17:35:10 +00:00
|
|
|
SkMatrix resizeMatrix;
|
|
|
|
resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y);
|
2014-02-05 17:51:22 +00:00
|
|
|
|
|
|
|
SkImageFilter* filters[] = {
|
2014-03-10 10:51:58 +00:00
|
|
|
SkBlurImageFilter::Create(SkIntToScalar(12), SkIntToScalar(12)),
|
|
|
|
SkDropShadowImageFilter::Create(SkIntToScalar(10), SkIntToScalar(10), SkIntToScalar(3),
|
|
|
|
SK_ColorGREEN),
|
|
|
|
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)),
|
2014-03-25 17:35:10 +00:00
|
|
|
SkMatrixImageFilter::Create(resizeMatrix, SkPaint::kNone_FilterLevel),
|
2014-03-03 14:58:09 +00:00
|
|
|
SkRectShaderImageFilter::Create(noise),
|
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);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
paint.setImageFilter(filters[i]);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(bounds);
|
2014-02-19 22:10:12 +00:00
|
|
|
if (5 == i) {
|
2014-02-05 17:51:22 +00:00
|
|
|
canvas->translate(SkIntToScalar(16), SkIntToScalar(-32));
|
2014-02-19 22:10:12 +00:00
|
|
|
} else if (6 == i) {
|
|
|
|
canvas->scale(SkScalarInvert(RESIZE_FACTOR_X),
|
|
|
|
SkScalarInvert(RESIZE_FACTOR_Y));
|
2014-02-05 17:51:22 +00:00
|
|
|
}
|
|
|
|
canvas->drawCircle(r.centerX(), r.centerY(),
|
|
|
|
SkScalarDiv(r.width()*2, SkIntToScalar(5)), paint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(r.width() + margin, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() + margin);
|
|
|
|
}
|
2014-02-06 00:36:01 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
|
|
|
|
SkSafeUnref(filters[i]);
|
|
|
|
}
|
2014-02-05 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool fInitialized;
|
|
|
|
SkBitmap fCheckerboard;
|
|
|
|
SkBitmap fGradientCircle;
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new ImageFiltersClippedGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|