OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkImageSource.h" | 10 #include "SkImageSource.h" |
11 #include "SkRefCnt.h" | 11 #include "SkRefCnt.h" |
12 #include "SkSurface.h" | 12 #include "SkSurface.h" |
13 | 13 |
14 namespace skiagm { | 14 namespace skiagm { |
15 | 15 |
16 class ResizeGM : public GM { | 16 class ResizeGM : public GM { |
17 public: | 17 public: |
18 ResizeGM() { | 18 ResizeGM() { |
19 this->setBGColor(0x00000000); | 19 this->setBGColor(0x00000000); |
20 } | 20 } |
21 | 21 |
22 protected: | 22 protected: |
23 virtual SkString onShortName() { | 23 SkString onShortName() override { |
24 return SkString("resizeimagefilter"); | 24 return SkString("resizeimagefilter"); |
25 } | 25 } |
26 | 26 |
27 void draw(SkCanvas* canvas, | 27 void draw(SkCanvas* canvas, |
28 const SkRect& rect, | 28 const SkRect& rect, |
29 const SkSize& deviceSize, | 29 const SkSize& deviceSize, |
30 SkFilterQuality filterQuality, | 30 SkFilterQuality filterQuality, |
31 SkImageFilter* input = nullptr) { | 31 sk_sp<SkImageFilter> input) { |
32 SkRect dstRect; | 32 SkRect dstRect; |
33 canvas->getTotalMatrix().mapRect(&dstRect, rect); | 33 canvas->getTotalMatrix().mapRect(&dstRect, rect); |
34 canvas->save(); | 34 canvas->save(); |
35 SkScalar deviceScaleX = deviceSize.width() / dstRect.width(); | 35 SkScalar deviceScaleX = deviceSize.width() / dstRect.width(); |
36 SkScalar deviceScaleY = deviceSize.height() / dstRect.height(); | 36 SkScalar deviceScaleY = deviceSize.height() / dstRect.height(); |
37 canvas->translate(rect.x(), rect.y()); | 37 canvas->translate(rect.x(), rect.y()); |
38 canvas->scale(deviceScaleX, deviceScaleY); | 38 canvas->scale(deviceScaleX, deviceScaleY); |
39 canvas->translate(-rect.x(), -rect.y()); | 39 canvas->translate(-rect.x(), -rect.y()); |
40 SkMatrix matrix; | 40 SkMatrix matrix; |
41 matrix.setScale(SkScalarInvert(deviceScaleX), | 41 matrix.setScale(SkScalarInvert(deviceScaleX), SkScalarInvert(deviceScale
Y)); |
42 SkScalarInvert(deviceScaleY)); | 42 sk_sp<SkImageFilter> filter(SkImageFilter::CreateMatrixFilter(matrix, |
43 SkAutoTUnref<SkImageFilter> imageFilter( | 43 filterQual
ity, |
44 SkImageFilter::CreateMatrixFilter(matrix, filterQuality, input)); | 44 input.get(
))); |
45 SkPaint filteredPaint; | 45 SkPaint filteredPaint; |
46 filteredPaint.setImageFilter(imageFilter.get()); | 46 filteredPaint.setImageFilter(std::move(filter)); |
47 canvas->saveLayer(&rect, &filteredPaint); | 47 canvas->saveLayer(&rect, &filteredPaint); |
48 SkPaint paint; | 48 SkPaint paint; |
49 paint.setColor(0xFF00FF00); | 49 paint.setColor(0xFF00FF00); |
50 SkRect ovalRect = rect; | 50 SkRect ovalRect = rect; |
51 ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4)); | 51 ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4)); |
52 canvas->drawOval(ovalRect, paint); | 52 canvas->drawOval(ovalRect, paint); |
53 canvas->restore(); // for saveLayer | 53 canvas->restore(); // for saveLayer |
54 canvas->restore(); | 54 canvas->restore(); |
55 } | 55 } |
56 | 56 |
57 virtual SkISize onISize() { | 57 SkISize onISize() override { |
58 return SkISize::Make(520, 100); | 58 return SkISize::Make(520, 100); |
59 } | 59 } |
60 | 60 |
61 virtual void onDraw(SkCanvas* canvas) { | 61 void onDraw(SkCanvas* canvas) override { |
62 canvas->clear(SK_ColorBLACK); | 62 canvas->clear(SK_ColorBLACK); |
63 | 63 |
64 SkRect srcRect = SkRect::MakeWH(96, 96); | 64 SkRect srcRect = SkRect::MakeWH(96, 96); |
65 | 65 |
66 SkSize deviceSize = SkSize::Make(16, 16); | 66 SkSize deviceSize = SkSize::Make(16, 16); |
67 draw(canvas, | 67 this->draw(canvas, |
68 srcRect, | 68 srcRect, |
69 deviceSize, | 69 deviceSize, |
70 kNone_SkFilterQuality); | 70 kNone_SkFilterQuality, |
| 71 nullptr); |
71 | 72 |
72 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); | 73 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); |
73 draw(canvas, | 74 this->draw(canvas, |
74 srcRect, | 75 srcRect, |
75 deviceSize, | 76 deviceSize, |
76 kLow_SkFilterQuality); | 77 kLow_SkFilterQuality, |
| 78 nullptr); |
77 | 79 |
78 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); | 80 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); |
79 draw(canvas, | 81 this->draw(canvas, |
80 srcRect, | 82 srcRect, |
81 deviceSize, | 83 deviceSize, |
82 kMedium_SkFilterQuality); | 84 kMedium_SkFilterQuality, |
| 85 nullptr); |
83 | 86 |
84 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); | 87 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); |
85 draw(canvas, | 88 this->draw(canvas, |
86 srcRect, | 89 srcRect, |
87 deviceSize, | 90 deviceSize, |
88 kHigh_SkFilterQuality); | 91 kHigh_SkFilterQuality, |
| 92 nullptr); |
89 | 93 |
90 auto surface(SkSurface::MakeRasterN32Premul(16, 16)); | 94 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(16, 16)); |
91 SkCanvas* surfaceCanvas = surface->getCanvas(); | 95 SkCanvas* surfaceCanvas = surface->getCanvas(); |
92 surfaceCanvas->clear(0x000000); | 96 surfaceCanvas->clear(0x000000); |
93 { | 97 { |
94 SkPaint paint; | 98 SkPaint paint; |
95 paint.setColor(0xFF00FF00); | 99 paint.setColor(0xFF00FF00); |
96 SkRect ovalRect = SkRect::MakeWH(16, 16); | 100 SkRect ovalRect = SkRect::MakeWH(16, 16); |
97 ovalRect.inset(SkIntToScalar(2)/3, SkIntToScalar(2)/3); | 101 ovalRect.inset(SkIntToScalar(2)/3, SkIntToScalar(2)/3); |
98 surfaceCanvas->drawOval(ovalRect, paint); | 102 surfaceCanvas->drawOval(ovalRect, paint); |
99 } | 103 } |
100 sk_sp<SkImage> image(surface->makeImageSnapshot()); | 104 sk_sp<SkImage> image(surface->makeImageSnapshot()); |
101 SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20); | 105 SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20); |
102 SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120); | 106 SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120); |
103 SkAutoTUnref<SkImageFilter> source( | 107 sk_sp<SkImageFilter> source( |
104 SkImageSource::Create(image.get(), inRect, outRect, kHigh_SkFilterQu
ality)); | 108 SkImageSource::Make(std::move(image), inRect, outRect, kHigh_SkFilte
rQuality)); |
105 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); | 109 canvas->translate(srcRect.width() + SkIntToScalar(10), 0); |
106 draw(canvas, | 110 this->draw(canvas, |
107 srcRect, | 111 srcRect, |
108 deviceSize, | 112 deviceSize, |
109 kHigh_SkFilterQuality, | 113 kHigh_SkFilterQuality, |
110 source.get()); | 114 std::move(source)); |
111 } | 115 } |
112 | 116 |
113 private: | 117 private: |
114 typedef GM INHERITED; | 118 typedef GM INHERITED; |
115 }; | 119 }; |
116 | 120 |
117 ////////////////////////////////////////////////////////////////////////////// | 121 ////////////////////////////////////////////////////////////////////////////// |
118 | 122 |
119 static GM* MyFactory(void*) { return new ResizeGM; } | 123 DEF_GM(return new ResizeGM; ) |
120 static GMRegistry reg(MyFactory); | |
121 | 124 |
122 } | 125 } |
OLD | NEW |