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 | 9 |
10 #include "SkArithmeticMode.h" | 10 #include "SkArithmeticMode.h" |
11 #include "SkBlurImageFilter.h" | 11 #include "SkBlurImageFilter.h" |
12 #include "SkColorFilter.h" | 12 #include "SkColorFilter.h" |
13 #include "SkColorFilterImageFilter.h" | 13 #include "SkColorFilterImageFilter.h" |
14 #include "SkColorMatrixFilter.h" | 14 #include "SkColorMatrixFilter.h" |
15 #include "SkImage.h" | 15 #include "SkImage.h" |
16 #include "SkImageSource.h" | 16 #include "SkImageSource.h" |
17 #include "SkMatrixConvolutionImageFilter.h" | 17 #include "SkMatrixConvolutionImageFilter.h" |
| 18 #include "SkOffsetImageFilter.h" |
18 #include "SkReadBuffer.h" | 19 #include "SkReadBuffer.h" |
19 #include "SkSpecialImage.h" | 20 #include "SkSpecialImage.h" |
20 #include "SkSpecialSurface.h" | 21 #include "SkSpecialSurface.h" |
21 #include "SkWriteBuffer.h" | 22 #include "SkWriteBuffer.h" |
22 #include "SkMergeImageFilter.h" | 23 #include "SkMergeImageFilter.h" |
23 #include "SkMorphologyImageFilter.h" | 24 #include "SkMorphologyImageFilter.h" |
24 #include "SkTestImageFilters.h" | 25 #include "SkTestImageFilters.h" |
25 #include "SkXfermodeImageFilter.h" | 26 #include "SkXfermodeImageFilter.h" |
26 | 27 |
27 // More closely models how Blink's OffsetFilter works as of 10/23/13. SkOffsetIm
ageFilter doesn't | |
28 // perform a draw and this one does. | |
29 class SimpleOffsetFilter : public SkImageFilter { | |
30 public: | |
31 class Registrar { | |
32 public: | |
33 Registrar() { | |
34 SkFlattenable::Register("SimpleOffsetFilter", | |
35 SimpleOffsetFilter::CreateProc, | |
36 SimpleOffsetFilter::GetFlattenableType()); | |
37 } | |
38 }; | |
39 static sk_sp<SkImageFilter> Make(SkScalar dx, SkScalar dy, sk_sp<SkImageFilt
er> input) { | |
40 return sk_sp<SkImageFilter>(new SimpleOffsetFilter(dx, dy, std::move(inp
ut))); | |
41 } | |
42 | |
43 SK_TO_STRING_OVERRIDE() | |
44 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SimpleOffsetFilter); | |
45 | |
46 protected: | |
47 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context& c
tx, | |
48 SkIPoint* offset) const override { | |
49 SkIPoint inputOffset = SkIPoint::Make(0, 0); | |
50 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffs
et)); | |
51 if (!input) { | |
52 return nullptr; | |
53 } | |
54 | |
55 SkIRect bounds; | |
56 input = this->applyCropRect(ctx, input.get(), &inputOffset, &bounds); | |
57 if (!input) { | |
58 return nullptr; | |
59 } | |
60 | |
61 SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.hei
ght()); | |
62 | |
63 sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); | |
64 if (!surf) { | |
65 return nullptr; | |
66 } | |
67 | |
68 SkCanvas* canvas = surf->getCanvas(); | |
69 SkASSERT(canvas); | |
70 | |
71 SkPaint paint; | |
72 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
73 | |
74 input->draw(canvas, fDX - bounds.left(), fDY - bounds.top(), &paint); | |
75 | |
76 offset->fX += bounds.left(); | |
77 offset->fY += bounds.top(); | |
78 return surf->makeImageSnapshot(); | |
79 } | |
80 | |
81 void flatten(SkWriteBuffer& buffer) const override { | |
82 this->INHERITED::flatten(buffer); | |
83 buffer.writeScalar(fDX); | |
84 buffer.writeScalar(fDY); | |
85 } | |
86 | |
87 private: | |
88 SimpleOffsetFilter(SkScalar dx, SkScalar dy, sk_sp<SkImageFilter> input) | |
89 : SkImageFilter(&input, 1, nullptr) | |
90 , fDX(dx) | |
91 , fDY(dy) { | |
92 } | |
93 | |
94 SkScalar fDX, fDY; | |
95 | |
96 typedef SkImageFilter INHERITED; | |
97 }; | |
98 | |
99 static SimpleOffsetFilter::Registrar gReg; | |
100 | |
101 sk_sp<SkFlattenable> SimpleOffsetFilter::CreateProc(SkReadBuffer& buffer) { | |
102 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); | |
103 SkScalar dx = buffer.readScalar(); | |
104 SkScalar dy = buffer.readScalar(); | |
105 return Make(dx, dy, common.getInput(0)); | |
106 } | |
107 | |
108 #ifndef SK_IGNORE_TO_STRING | |
109 void SimpleOffsetFilter::toString(SkString* str) const { | |
110 str->appendf("SimpleOffsetFilter: ("); | |
111 str->append(")"); | |
112 } | |
113 #endif | |
114 | |
115 class ImageFiltersGraphGM : public skiagm::GM { | 28 class ImageFiltersGraphGM : public skiagm::GM { |
116 public: | 29 public: |
117 ImageFiltersGraphGM() {} | 30 ImageFiltersGraphGM() {} |
118 | 31 |
119 protected: | 32 protected: |
120 | 33 |
121 SkString onShortName() override { | 34 SkString onShortName() override { |
122 return SkString("imagefiltersgraph"); | 35 return SkString("imagefiltersgraph"); |
123 } | 36 } |
124 | 37 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 canvas->translate(SkIntToScalar(100), 0); | 79 canvas->translate(SkIntToScalar(100), 0); |
167 } | 80 } |
168 { | 81 { |
169 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, | 82 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, |
170 0, SK_Scalar1, 0, 0, 0, | 83 0, SK_Scalar1, 0, 0, 0, |
171 0, 0, SK_Scalar1, 0, 0, | 84 0, 0, SK_Scalar1, 0, 0, |
172 0, 0, 0, 0.5f, 0 }; | 85 0, 0, 0, 0.5f, 0 }; |
173 sk_sp<SkColorFilter> matrixCF(SkColorFilter::MakeMatrixFilterRowMajo
r255(matrix)); | 86 sk_sp<SkColorFilter> matrixCF(SkColorFilter::MakeMatrixFilterRowMajo
r255(matrix)); |
174 sk_sp<SkImageFilter> matrixFilter(SkColorFilterImageFilter::Make(std
::move(matrixCF), | 87 sk_sp<SkImageFilter> matrixFilter(SkColorFilterImageFilter::Make(std
::move(matrixCF), |
175 nul
lptr)); | 88 nul
lptr)); |
176 sk_sp<SkImageFilter> offsetFilter(SimpleOffsetFilter::Make(10.0f, 10
.f, matrixFilter)); | 89 sk_sp<SkImageFilter> offsetFilter(SkOffsetImageFilter::Make(10.0f, 1
0.f, |
| 90 matrixFi
lter)); |
177 | 91 |
178 SkPaint paint; | 92 SkPaint paint; |
179 paint.setImageFilter( | 93 paint.setImageFilter( |
180 SkXfermodeImageFilter::Make(SkArithmeticMode::Make(0, SK_Scalar1
, SK_Scalar1, 0), | 94 SkXfermodeImageFilter::Make(SkArithmeticMode::Make(0, SK_Scalar1
, SK_Scalar1, 0), |
181 std::move(matrixFilter), | 95 std::move(matrixFilter), |
182 std::move(offsetFilter), | 96 std::move(offsetFilter), |
183 nullptr)); | 97 nullptr)); |
184 | 98 |
185 DrawClippedImage(canvas, fImage.get(), paint); | 99 DrawClippedImage(canvas, fImage.get(), paint); |
186 canvas->translate(SkIntToScalar(100), 0); | 100 canvas->translate(SkIntToScalar(100), 0); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 } | 178 } |
265 | 179 |
266 sk_sp<SkImage> fImage; | 180 sk_sp<SkImage> fImage; |
267 | 181 |
268 typedef GM INHERITED; | 182 typedef GM INHERITED; |
269 }; | 183 }; |
270 | 184 |
271 /////////////////////////////////////////////////////////////////////////////// | 185 /////////////////////////////////////////////////////////////////////////////// |
272 | 186 |
273 DEF_GM(return new ImageFiltersGraphGM;) | 187 DEF_GM(return new ImageFiltersGraphGM;) |
OLD | NEW |