• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
9 #include "sk_tool_utils.h"
10 #include "SkColor.h"
11 #include "SkGradientShader.h"
12 #include "SkMatrixConvolutionImageFilter.h"
13 
14 namespace skiagm {
15 
16 class MatrixConvolutionGM : public GM {
17 public:
MatrixConvolutionGM(SkColor colorOne,SkColor colorTwo,const char * nameSuffix)18     MatrixConvolutionGM(SkColor colorOne, SkColor colorTwo, const char* nameSuffix)
19             : fNameSuffix(nameSuffix) {
20         this->setBGColor(0x00000000);
21         fColors[0] = colorOne;
22         fColors[1] = colorTwo;
23     }
24 
25 protected:
26 
onShortName()27     SkString onShortName() override {
28         return SkStringPrintf("matrixconvolution%s", fNameSuffix);
29     }
30 
makeBitmap()31     void makeBitmap() {
32         // Draw our bitmap in N32, so legacy devices get "premul" values they understand
33         fBitmap.allocN32Pixels(80, 80);
34         SkCanvas canvas(fBitmap);
35         canvas.clear(0x00000000);
36         SkPaint paint;
37         paint.setColor(0xFFFFFFFF);
38         SkPoint pts[2] = { {0, 0},
39                            {0, 80.0f} };
40         SkScalar pos[2] = { 0, 80.0f };
41         paint.setShader(SkGradientShader::MakeLinear(
42             pts, fColors, pos, 2, SkShader::kClamp_TileMode));
43         SkFont font(sk_tool_utils::create_portable_typeface(), 180.0f);
44         canvas.drawString("e", -10.0f, 80.0f, font, paint);
45     }
46 
onISize()47     SkISize onISize() override {
48         return SkISize::Make(500, 300);
49     }
50 
draw(SkCanvas * canvas,int x,int y,const SkIPoint & kernelOffset,SkMatrixConvolutionImageFilter::TileMode tileMode,bool convolveAlpha,const SkImageFilter::CropRect * cropRect=nullptr)51     void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset,
52               SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
53               const SkImageFilter::CropRect* cropRect = nullptr) {
54         SkScalar kernel[9] = {
55             SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
56             SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
57             SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
58         };
59         SkISize kernelSize = SkISize::Make(3, 3);
60         SkScalar gain = 0.3f, bias = SkIntToScalar(100);
61         SkPaint paint;
62         paint.setImageFilter(SkMatrixConvolutionImageFilter::Make(kernelSize,
63                                                                   kernel,
64                                                                   gain,
65                                                                   bias,
66                                                                   kernelOffset,
67                                                                   tileMode,
68                                                                   convolveAlpha,
69                                                                   nullptr,
70                                                                   cropRect));
71         canvas->save();
72         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
73         const SkRect layerBounds = SkRect::MakeIWH(fBitmap.width(), fBitmap.height());
74         canvas->clipRect(layerBounds);
75         // This GM is, in part, intended to display the wrapping behavior of the
76         // matrix image filter. The only (rational) way to achieve that for repeat mode
77         // is to create a tight layer.
78         canvas->saveLayer(layerBounds, &paint);
79             canvas->drawBitmap(fBitmap, 0, 0, nullptr);
80         canvas->restore();
81         canvas->restore();
82     }
83 
84     typedef SkMatrixConvolutionImageFilter MCIF;
85 
onOnceBeforeDraw()86     void onOnceBeforeDraw() override {
87         this->makeBitmap();
88     }
89 
onDraw(SkCanvas * canvas)90     void onDraw(SkCanvas* canvas) override {
91         canvas->clear(SK_ColorBLACK);
92         SkIPoint kernelOffset = SkIPoint::Make(1, 0);
93         SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds()));
94         for (int x = 10; x < 310; x += 100) {
95             this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
96             this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
97             this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
98             kernelOffset.fY++;
99         }
100         kernelOffset.fY = 1;
101         SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60));
102         this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect);
103         this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect);
104         this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect);
105 
106         this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect);
107         this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect);
108         this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect);
109     }
110 
111 private:
112     SkBitmap fBitmap;
113     SkColor fColors[2];
114     const char* fNameSuffix;
115 
116     typedef GM INHERITED;
117 };
118 
119 //////////////////////////////////////////////////////////////////////////////
120 
121 DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");)
122 DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");)
123 
124 }
125