blob: 813a1f9a8a3e1a0892b40524b6148ae897a80722 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000010#include "SkColor.h"
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000011#include "SkGradientShader.h"
Brian Osman878df6d2016-12-27 11:04:12 -050012#include "SkMatrixConvolutionImageFilter.h"
13#include "SkPixelRef.h"
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000014
15namespace skiagm {
16
17class MatrixConvolutionGM : public GM {
18public:
Brian Osman878df6d2016-12-27 11:04:12 -050019 MatrixConvolutionGM(SkColor colorOne, SkColor colorTwo, const char* nameSuffix)
20 : fNameSuffix(nameSuffix) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000021 this->setBGColor(0x00000000);
Brian Osman878df6d2016-12-27 11:04:12 -050022 fColors[0] = colorOne;
23 fColors[1] = colorTwo;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000024 }
25
26protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000027
robertphillips943a4622015-09-03 13:32:33 -070028 SkString onShortName() override {
Brian Osman878df6d2016-12-27 11:04:12 -050029 return SkStringPrintf("matrixconvolution%s", fNameSuffix);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000030 }
31
robertphillips943a4622015-09-03 13:32:33 -070032 void makeBitmap() {
Brian Osman878df6d2016-12-27 11:04:12 -050033 // Draw our bitmap in N32, so legacy devices get "premul" values they understand
Brian Osmanc69c4412018-07-10 15:50:11 -040034 fBitmap.allocN32Pixels(80, 80);
35 SkCanvas canvas(fBitmap);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000036 canvas.clear(0x00000000);
37 SkPaint paint;
38 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070039 sk_tool_utils::set_portable_typeface(&paint);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000040 paint.setColor(0xFFFFFFFF);
41 paint.setTextSize(SkIntToScalar(180));
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000042 SkPoint pts[2] = { SkPoint::Make(0, 0),
43 SkPoint::Make(0, SkIntToScalar(80)) };
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000044 SkScalar pos[2] = { 0, SkIntToScalar(80) };
reed1a9b9642016-03-13 14:13:58 -070045 paint.setShader(SkGradientShader::MakeLinear(
Brian Osman878df6d2016-12-27 11:04:12 -050046 pts, fColors, pos, 2, SkShader::kClamp_TileMode));
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000047 const char* str = "e";
Cary Clark2a475ea2017-04-28 15:35:12 -040048 canvas.drawString(str, SkIntToScalar(-10), SkIntToScalar(80), paint);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000049 }
50
robertphillips943a4622015-09-03 13:32:33 -070051 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070052 return SkISize::Make(500, 300);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000053 }
54
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000055 void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset,
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000056 SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
halcanary96fcdcc2015-08-27 07:41:13 -070057 const SkImageFilter::CropRect* cropRect = nullptr) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000058 SkScalar kernel[9] = {
59 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
60 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
61 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
62 };
63 SkISize kernelSize = SkISize::Make(3, 3);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000064 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000065 SkPaint paint;
robertphillipsef6a47b2016-04-08 08:01:20 -070066 paint.setImageFilter(SkMatrixConvolutionImageFilter::Make(kernelSize,
67 kernel,
68 gain,
69 bias,
70 kernelOffset,
71 tileMode,
72 convolveAlpha,
73 nullptr,
74 cropRect));
junov@chromium.orgff06af22013-01-14 16:27:50 +000075 canvas->save();
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000076 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
Robert Phillips12078432018-05-17 11:17:39 -040077 const SkRect layerBounds = SkRect::MakeIWH(fBitmap.width(), fBitmap.height());
78 canvas->clipRect(layerBounds);
79 // This GM is, in part, intended to display the wrapping behavior of the
80 // matrix image filter. The only (rational) way to achieve that for repeat mode
81 // is to create a tight layer.
82 canvas->saveLayer(layerBounds, &paint);
83 canvas->drawBitmap(fBitmap, 0, 0, nullptr);
84 canvas->restore();
junov@chromium.orgff06af22013-01-14 16:27:50 +000085 canvas->restore();
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000086 }
87
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000088 typedef SkMatrixConvolutionImageFilter MCIF;
89
robertphillips943a4622015-09-03 13:32:33 -070090 void onOnceBeforeDraw() override {
91 this->makeBitmap();
92 }
93
94 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -070095 canvas->clear(SK_ColorBLACK);
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000096 SkIPoint kernelOffset = SkIPoint::Make(1, 0);
senorblancoa544eda2015-12-07 07:48:34 -080097 SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds()));
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000098 for (int x = 10; x < 310; x += 100) {
senorblancoa544eda2015-12-07 07:48:34 -080099 this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
100 this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
101 this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +0000102 kernelOffset.fY++;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000103 }
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +0000104 kernelOffset.fY = 1;
senorblancoa544eda2015-12-07 07:48:34 -0800105 SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60));
106 this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect);
107 this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect);
108 this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect);
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000109
senorblancoa544eda2015-12-07 07:48:34 -0800110 this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect);
111 this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect);
112 this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000113 }
114
115private:
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000116 SkBitmap fBitmap;
Brian Osman878df6d2016-12-27 11:04:12 -0500117 SkColor fColors[2];
118 const char* fNameSuffix;
robertphillips943a4622015-09-03 13:32:33 -0700119
120 typedef GM INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000121};
122
123//////////////////////////////////////////////////////////////////////////////
124
Brian Osman878df6d2016-12-27 11:04:12 -0500125DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");)
126DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");)
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000127
128}