blob: 1863a7f597cbbf7cbed1d7fc7a9936d5f0088f95 [file] [log] [blame]
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +00009#include "SkColor.h"
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000010#include "SkMatrixConvolutionImageFilter.h"
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000011#include "SkGradientShader.h"
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000012
13namespace skiagm {
14
15class MatrixConvolutionGM : public GM {
16public:
17 MatrixConvolutionGM() : fInitialized(false) {
18 this->setBGColor(0x00000000);
19 }
20
21protected:
22 virtual SkString onShortName() {
23 return SkString("matrixconvolution");
24 }
25
26 void make_bitmap() {
27 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
28 fBitmap.allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000029 SkBitmapDevice device(fBitmap);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000030 SkCanvas canvas(&device);
31 canvas.clear(0x00000000);
32 SkPaint paint;
33 paint.setAntiAlias(true);
34 paint.setColor(0xFFFFFFFF);
35 paint.setTextSize(SkIntToScalar(180));
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000036 SkPoint pts[2] = { SkPoint::Make(0, 0),
37 SkPoint::Make(0, SkIntToScalar(80)) };
38 SkColor colors[2] = { 0xFFFFFFFF, 0x40404040 };
39 SkScalar pos[2] = { 0, SkIntToScalar(80) };
40 paint.setShader(SkGradientShader::CreateLinear(
41 pts, colors, pos, 2, SkShader::kClamp_TileMode))->unref();
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000042 const char* str = "e";
43 canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint);
44 }
45
46 virtual SkISize onISize() {
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000047 return make_isize(500, 300);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000048 }
49
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000050 void draw(SkCanvas* canvas, int x, int y, const SkIPoint& target,
51 SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
52 const SkImageFilter::CropRect* cropRect = NULL) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000053 SkScalar kernel[9] = {
54 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
55 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
56 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
57 };
58 SkISize kernelSize = SkISize::Make(3, 3);
59 SkScalar gain = SkFloatToScalar(0.3f), bias = SkIntToScalar(100);
60 SkPaint paint;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000061 SkAutoTUnref<SkImageFilter> filter(
62 SkNEW_ARGS(SkMatrixConvolutionImageFilter, (kernelSize,
63 kernel,
64 gain,
65 bias,
66 target,
67 tileMode,
68 convolveAlpha,
69 NULL,
70 cropRect)));
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000071 paint.setImageFilter(filter);
junov@chromium.orgff06af22013-01-14 16:27:50 +000072 canvas->save();
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000073 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
74 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()),
75 SkIntToScalar(fBitmap.height())));
76 canvas->drawBitmap(fBitmap, 0, 0, &paint);
junov@chromium.orgff06af22013-01-14 16:27:50 +000077 canvas->restore();
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000078 }
79
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000080 typedef SkMatrixConvolutionImageFilter MCIF;
81
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000082 virtual void onDraw(SkCanvas* canvas) {
83 if (!fInitialized) {
84 make_bitmap();
85 fInitialized = true;
86 }
87 canvas->clear(0x00000000);
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000088 SkIPoint target = SkIPoint::Make(1, 0);
89 for (int x = 10; x < 310; x += 100) {
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000090 this->draw(canvas, x, 10, target, MCIF::kClamp_TileMode, true);
91 this->draw(canvas, x, 110, target, MCIF::kClampToBlack_TileMode, true);
92 this->draw(canvas, x, 210, target, MCIF::kRepeat_TileMode, true);
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000093 target.fY++;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000094 }
senorblanco@chromium.org8640d502012-09-25 14:32:42 +000095 target.fY = 1;
senorblanco@chromium.org2a4223c2013-10-20 20:21:19 +000096 SkImageFilter::CropRect rect(SkRect::MakeXYWH(10, 5, 60, 60));
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000097 this->draw(canvas, 310, 10, target, MCIF::kClamp_TileMode, true, &rect);
98 this->draw(canvas, 310, 110, target, MCIF::kClampToBlack_TileMode, true, &rect);
99 this->draw(canvas, 310, 210, target, MCIF::kRepeat_TileMode, true, &rect);
100
101 this->draw(canvas, 410, 10, target, MCIF::kClamp_TileMode, false);
102 this->draw(canvas, 410, 110, target, MCIF::kClampToBlack_TileMode, false);
103 this->draw(canvas, 410, 210, target, MCIF::kRepeat_TileMode, false);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000104 }
105
106private:
107 typedef GM INHERITED;
108 SkBitmap fBitmap;
109 bool fInitialized;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114static GM* MyFactory(void*) { return new MatrixConvolutionGM; }
115static GMRegistry reg(MyFactory);
116
117}