blob: 4af12e172d097560384b3e61751191aea9d2ff4e [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
34 SkBitmap n32Bitmap;
35 n32Bitmap.allocN32Pixels(80, 80);
36 SkCanvas canvas(n32Bitmap);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000037 canvas.clear(0x00000000);
38 SkPaint paint;
39 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070040 sk_tool_utils::set_portable_typeface(&paint);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000041 paint.setColor(0xFFFFFFFF);
42 paint.setTextSize(SkIntToScalar(180));
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000043 SkPoint pts[2] = { SkPoint::Make(0, 0),
44 SkPoint::Make(0, SkIntToScalar(80)) };
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +000045 SkScalar pos[2] = { 0, SkIntToScalar(80) };
reed1a9b9642016-03-13 14:13:58 -070046 paint.setShader(SkGradientShader::MakeLinear(
Brian Osman878df6d2016-12-27 11:04:12 -050047 pts, fColors, pos, 2, SkShader::kClamp_TileMode));
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000048 const char* str = "e";
Cary Clark2a475ea2017-04-28 15:35:12 -040049 canvas.drawString(str, SkIntToScalar(-10), SkIntToScalar(80), paint);
Brian Osman878df6d2016-12-27 11:04:12 -050050
51 // ... tag the data as sRGB, so color-aware devices do gamut adjustment, etc...
52 fBitmap.setInfo(SkImageInfo::MakeS32(80, 80, kPremul_SkAlphaType));
53 fBitmap.setPixelRef(sk_ref_sp(n32Bitmap.pixelRef()), 0, 0);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000054 }
55
robertphillips943a4622015-09-03 13:32:33 -070056 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070057 return SkISize::Make(500, 300);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000058 }
59
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +000060 void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset,
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000061 SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
halcanary96fcdcc2015-08-27 07:41:13 -070062 const SkImageFilter::CropRect* cropRect = nullptr) {
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000063 SkScalar kernel[9] = {
64 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
65 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
66 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
67 };
68 SkISize kernelSize = SkISize::Make(3, 3);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000069 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
Brian Osman878df6d2016-12-27 11:04:12 -050070 if (canvas->imageInfo().colorSpace()) {
71 // TODO: Gain and bias are poorly specified (in the feConvolveMatrix SVG documentation,
72 // there is obviously no mention of gamma or color spaces). Eventually, we need to
73 // decide what to do with these (they generally have an extreme brightening effect).
74 // For now, I'm modifying this GM to use values tuned to preserve luminance across the
75 // range of input values (compared to the legacy math and values).
76 //
77 // It's impossible to match the results exactly, because legacy math produces a flat
78 // response (when looking at sRGB encoded results), while gamma-correct math produces
79 // a curve.
80 gain = 0.25f;
81 bias = 16.5f;
82 }
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000083 SkPaint paint;
robertphillipsef6a47b2016-04-08 08:01:20 -070084 paint.setImageFilter(SkMatrixConvolutionImageFilter::Make(kernelSize,
85 kernel,
86 gain,
87 bias,
88 kernelOffset,
89 tileMode,
90 convolveAlpha,
91 nullptr,
92 cropRect));
junov@chromium.orgff06af22013-01-14 16:27:50 +000093 canvas->save();
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000094 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
95 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()),
96 SkIntToScalar(fBitmap.height())));
97 canvas->drawBitmap(fBitmap, 0, 0, &paint);
junov@chromium.orgff06af22013-01-14 16:27:50 +000098 canvas->restore();
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +000099 }
100
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000101 typedef SkMatrixConvolutionImageFilter MCIF;
102
robertphillips943a4622015-09-03 13:32:33 -0700103 void onOnceBeforeDraw() override {
104 this->makeBitmap();
105 }
106
107 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -0700108 canvas->clear(SK_ColorBLACK);
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +0000109 SkIPoint kernelOffset = SkIPoint::Make(1, 0);
senorblancoa544eda2015-12-07 07:48:34 -0800110 SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds()));
senorblanco@chromium.org3bc16c82012-10-04 17:18:20 +0000111 for (int x = 10; x < 310; x += 100) {
senorblancoa544eda2015-12-07 07:48:34 -0800112 this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
113 this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
114 this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +0000115 kernelOffset.fY++;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000116 }
commit-bot@chromium.org84cd0992014-03-12 16:36:08 +0000117 kernelOffset.fY = 1;
senorblancoa544eda2015-12-07 07:48:34 -0800118 SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60));
119 this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect);
120 this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect);
121 this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect);
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000122
senorblancoa544eda2015-12-07 07:48:34 -0800123 this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect);
124 this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect);
125 this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect);
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000126 }
127
128private:
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000129 SkBitmap fBitmap;
Brian Osman878df6d2016-12-27 11:04:12 -0500130 SkColor fColors[2];
131 const char* fNameSuffix;
robertphillips943a4622015-09-03 13:32:33 -0700132
133 typedef GM INHERITED;
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000134};
135
136//////////////////////////////////////////////////////////////////////////////
137
Brian Osman878df6d2016-12-27 11:04:12 -0500138DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");)
139DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");)
senorblanco@chromium.org5faa2dc2012-09-18 20:32:34 +0000140
141}