blob: 3b159f455bd084df5255f3b4dcff62ad01d46030 [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:
robertphillips943a4622015-09-03 13:32:33 -070017 MatrixConvolutionGM() {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000018 this->setBGColor(0x00000000);
19 }
20
21protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000022
robertphillips943a4622015-09-03 13:32:33 -070023 SkString onShortName() override {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000024 return SkString("matrixconvolution");
25 }
26
robertphillips943a4622015-09-03 13:32:33 -070027 void makeBitmap() {
reed@google.comeb9a46c2014-01-25 16:46:20 +000028 fBitmap.allocN32Pixels(80, 80);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000029 SkCanvas canvas(fBitmap);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000030 canvas.clear(0x00000000);
31 SkPaint paint;
32 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070033 sk_tool_utils::set_portable_typeface(&paint);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000034 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
robertphillips943a4622015-09-03 13:32:33 -070046 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070047 return SkISize::Make(500, 300);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000048 }
49
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000050 void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset,
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000051 SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
halcanary96fcdcc2015-08-27 07:41:13 -070052 const SkImageFilter::CropRect* cropRect = nullptr) {
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);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000059 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000060 SkPaint paint;
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000061 SkAutoTUnref<SkImageFilter> filter(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000062 SkMatrixConvolutionImageFilter::Create(kernelSize,
63 kernel,
64 gain,
65 bias,
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000066 kernelOffset,
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000067 tileMode,
68 convolveAlpha,
halcanary96fcdcc2015-08-27 07:41:13 -070069 nullptr,
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000070 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
robertphillips943a4622015-09-03 13:32:33 -070082 void onOnceBeforeDraw() override {
83 this->makeBitmap();
84 }
85
86 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -070087 canvas->clear(SK_ColorBLACK);
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000088 SkIPoint kernelOffset = SkIPoint::Make(1, 0);
senorblancoa544eda2015-12-07 07:48:34 -080089 SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds()));
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000090 for (int x = 10; x < 310; x += 100) {
senorblancoa544eda2015-12-07 07:48:34 -080091 this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
92 this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
93 this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000094 kernelOffset.fY++;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000095 }
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000096 kernelOffset.fY = 1;
senorblancoa544eda2015-12-07 07:48:34 -080097 SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60));
98 this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect);
99 this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect);
100 this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect);
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000101
senorblancoa544eda2015-12-07 07:48:34 -0800102 this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect);
103 this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect);
104 this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000105 }
106
107private:
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000108 SkBitmap fBitmap;
robertphillips943a4622015-09-03 13:32:33 -0700109
110 typedef GM INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000111};
112
113//////////////////////////////////////////////////////////////////////////////
114
robertphillips943a4622015-09-03 13:32:33 -0700115DEF_GM(return new MatrixConvolutionGM;)
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000116
117}