OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkColor.h" | 9 #include "SkColor.h" |
10 #include "SkMatrixConvolutionImageFilter.h" | 10 #include "SkMatrixConvolutionImageFilter.h" |
11 #include "SkGradientShader.h" | 11 #include "SkGradientShader.h" |
12 | 12 |
13 namespace skiagm { | 13 namespace skiagm { |
14 | 14 |
15 class MatrixConvolutionGM : public GM { | 15 class MatrixConvolutionGM : public GM { |
16 public: | 16 public: |
17 MatrixConvolutionGM() : fInitialized(false) { | 17 MatrixConvolutionGM() { |
18 this->setBGColor(0x00000000); | 18 this->setBGColor(0x00000000); |
19 } | 19 } |
20 | 20 |
21 protected: | 21 protected: |
22 | 22 |
23 virtual SkString onShortName() { | 23 SkString onShortName() override { |
24 return SkString("matrixconvolution"); | 24 return SkString("matrixconvolution"); |
25 } | 25 } |
26 | 26 |
27 void make_bitmap() { | 27 void makeBitmap() { |
28 fBitmap.allocN32Pixels(80, 80); | 28 fBitmap.allocN32Pixels(80, 80); |
29 SkCanvas canvas(fBitmap); | 29 SkCanvas canvas(fBitmap); |
30 canvas.clear(0x00000000); | 30 canvas.clear(0x00000000); |
31 SkPaint paint; | 31 SkPaint paint; |
32 paint.setAntiAlias(true); | 32 paint.setAntiAlias(true); |
33 sk_tool_utils::set_portable_typeface(&paint); | 33 sk_tool_utils::set_portable_typeface(&paint); |
34 paint.setColor(0xFFFFFFFF); | 34 paint.setColor(0xFFFFFFFF); |
35 paint.setTextSize(SkIntToScalar(180)); | 35 paint.setTextSize(SkIntToScalar(180)); |
36 SkPoint pts[2] = { SkPoint::Make(0, 0), | 36 SkPoint pts[2] = { SkPoint::Make(0, 0), |
37 SkPoint::Make(0, SkIntToScalar(80)) }; | 37 SkPoint::Make(0, SkIntToScalar(80)) }; |
38 SkColor colors[2] = { 0xFFFFFFFF, 0x40404040 }; | 38 SkColor colors[2] = { 0xFFFFFFFF, 0x40404040 }; |
39 SkScalar pos[2] = { 0, SkIntToScalar(80) }; | 39 SkScalar pos[2] = { 0, SkIntToScalar(80) }; |
40 paint.setShader(SkGradientShader::CreateLinear( | 40 paint.setShader(SkGradientShader::CreateLinear( |
41 pts, colors, pos, 2, SkShader::kClamp_TileMode))->unref(); | 41 pts, colors, pos, 2, SkShader::kClamp_TileMode))->unref(); |
42 const char* str = "e"; | 42 const char* str = "e"; |
43 canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80),
paint); | 43 canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80),
paint); |
44 } | 44 } |
45 | 45 |
46 virtual SkISize onISize() { | 46 SkISize onISize() override { |
47 return SkISize::Make(500, 300); | 47 return SkISize::Make(500, 300); |
48 } | 48 } |
49 | 49 |
50 void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset, | 50 void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset, |
51 SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAl
pha, | 51 SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAl
pha, |
52 const SkImageFilter::CropRect* cropRect = nullptr) { | 52 const SkImageFilter::CropRect* cropRect = nullptr) { |
53 SkScalar kernel[9] = { | 53 SkScalar kernel[9] = { |
54 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), | 54 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
55 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), | 55 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
56 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), | 56 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
(...skipping 15 matching lines...) Expand all Loading... |
72 canvas->save(); | 72 canvas->save(); |
73 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); | 73 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
74 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), | 74 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), |
75 SkIntToScalar(fBitmap.height()))); | 75 SkIntToScalar(fBitmap.height()))); |
76 canvas->drawBitmap(fBitmap, 0, 0, &paint); | 76 canvas->drawBitmap(fBitmap, 0, 0, &paint); |
77 canvas->restore(); | 77 canvas->restore(); |
78 } | 78 } |
79 | 79 |
80 typedef SkMatrixConvolutionImageFilter MCIF; | 80 typedef SkMatrixConvolutionImageFilter MCIF; |
81 | 81 |
82 virtual void onDraw(SkCanvas* canvas) { | 82 void onOnceBeforeDraw() override { |
83 if (!fInitialized) { | 83 this->makeBitmap(); |
84 make_bitmap(); | 84 } |
85 fInitialized = true; | 85 |
86 } | 86 void onDraw(SkCanvas* canvas) override { |
87 canvas->clear(SK_ColorBLACK); | 87 canvas->clear(SK_ColorBLACK); |
88 SkIPoint kernelOffset = SkIPoint::Make(1, 0); | 88 SkIPoint kernelOffset = SkIPoint::Make(1, 0); |
89 for (int x = 10; x < 310; x += 100) { | 89 for (int x = 10; x < 310; x += 100) { |
90 this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true)
; | 90 this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true)
; |
91 this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMod
e, true); | 91 this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMod
e, true); |
92 this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, tru
e); | 92 this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, tru
e); |
93 kernelOffset.fY++; | 93 kernelOffset.fY++; |
94 } | 94 } |
95 kernelOffset.fY = 1; | 95 kernelOffset.fY = 1; |
96 SkImageFilter::CropRect rect(SkRect::MakeXYWH(10, 5, 60, 60)); | 96 SkImageFilter::CropRect rect(SkRect::MakeXYWH(10, 5, 60, 60)); |
97 this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &
rect); | 97 this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &
rect); |
98 this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode,
true, &rect); | 98 this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode,
true, &rect); |
99 this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true,
&rect); | 99 this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true,
&rect); |
100 | 100 |
101 this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false); | 101 this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false); |
102 this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode,
false); | 102 this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode,
false); |
103 this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false
); | 103 this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false
); |
104 } | 104 } |
105 | 105 |
106 private: | 106 private: |
| 107 SkBitmap fBitmap; |
| 108 |
107 typedef GM INHERITED; | 109 typedef GM INHERITED; |
108 SkBitmap fBitmap; | |
109 bool fInitialized; | |
110 }; | 110 }; |
111 | 111 |
112 ////////////////////////////////////////////////////////////////////////////// | 112 ////////////////////////////////////////////////////////////////////////////// |
113 | 113 |
114 static GM* MyFactory(void*) { return new MatrixConvolutionGM; } | 114 DEF_GM(return new MatrixConvolutionGM;) |
115 static GMRegistry reg(MyFactory); | |
116 | 115 |
117 } | 116 } |
OLD | NEW |