Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: gm/colorfilterimagefilter.cpp

Issue 1827433002: Reland of [2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gm/coloremoji.cpp ('k') | gm/colorfilters.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkColorMatrixFilter.h" 10 #include "SkColorMatrixFilter.h"
11 #include "SkColorPriv.h" 11 #include "SkColorPriv.h"
12 #include "SkShader.h" 12 #include "SkShader.h"
13 13
14 #include "SkBlurImageFilter.h" 14 #include "SkBlurImageFilter.h"
15 #include "SkColorFilterImageFilter.h" 15 #include "SkColorFilterImageFilter.h"
16 16
17 #define FILTER_WIDTH SkIntToScalar(30) 17 #define FILTER_WIDTH SkIntToScalar(30)
18 #define FILTER_HEIGHT SkIntToScalar(30) 18 #define FILTER_HEIGHT SkIntToScalar(30)
19 #define MARGIN SkIntToScalar(10) 19 #define MARGIN SkIntToScalar(10)
20 20
21 static SkColorFilter* cf_make_brightness(float brightness) { 21 static sk_sp<SkColorFilter> cf_make_brightness(float brightness) {
22 SkScalar amount255 = SkScalarMul(brightness, SkIntToScalar(255)); 22 SkScalar amount255 = SkScalarMul(brightness, SkIntToScalar(255));
23 SkScalar matrix[20] = { 23 SkScalar matrix[20] = {
24 1, 0, 0, 0, amount255, 24 1, 0, 0, 0, amount255,
25 0, 1, 0, 0, amount255, 25 0, 1, 0, 0, amount255,
26 0, 0, 1, 0, amount255, 26 0, 0, 1, 0, amount255,
27 0, 0, 0, 1, 0 }; 27 0, 0, 0, 1, 0 };
28 return SkColorMatrixFilter::Create(matrix); 28 return SkColorFilter::MakeMatrixFilterRowMajor255(matrix);
29 } 29 }
30 30
31 static SkColorFilter* cf_make_grayscale() { 31 static sk_sp<SkColorFilter> cf_make_grayscale() {
32 SkScalar matrix[20]; 32 SkScalar matrix[20];
33 memset(matrix, 0, 20 * sizeof(SkScalar)); 33 memset(matrix, 0, 20 * sizeof(SkScalar));
34 matrix[0] = matrix[5] = matrix[10] = 0.2126f; 34 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
35 matrix[1] = matrix[6] = matrix[11] = 0.7152f; 35 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
36 matrix[2] = matrix[7] = matrix[12] = 0.0722f; 36 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
37 matrix[18] = 1.0f; 37 matrix[18] = 1.0f;
38 return SkColorMatrixFilter::Create(matrix); 38 return SkColorFilter::MakeMatrixFilterRowMajor255(matrix);
39 } 39 }
40 40
41 static SkColorFilter* cf_make_colorize(SkColor color) { 41 static sk_sp<SkColorFilter> cf_make_colorize(SkColor color) {
42 return SkColorFilter::CreateModeFilter(color, SkXfermode::kSrc_Mode); 42 return SkColorFilter::MakeModeFilter(color, SkXfermode::kSrc_Mode);
43 } 43 }
44 44
45 static void sk_gm_get_colorfilters(SkTDArray<SkColorFilter*>* array) { 45 static void sk_gm_get_colorfilters(SkTArray<sk_sp<SkColorFilter>>* array) {
46 *array->append() = cf_make_brightness(0.5f); 46 array->push_back(cf_make_brightness(0.5f));
47 *array->append() = cf_make_grayscale(); 47 array->push_back(cf_make_grayscale());
48 *array->append() = cf_make_colorize(SK_ColorBLUE); 48 array->push_back(cf_make_colorize(SK_ColorBLUE));
49 } 49 }
50 50
51 //////////////////////////////////////////////////////////////////////////////// /////////////////// 51 //////////////////////////////////////////////////////////////////////////////// ///////////////////
52 #include "SkGradientShader.h" 52 #include "SkGradientShader.h"
53 #include "SkImage.h" 53 #include "SkImage.h"
54 #include "Resources.h" 54 #include "Resources.h"
55 55
56 static sk_sp<SkShader> sh_make_lineargradient0() { 56 static sk_sp<SkShader> sh_make_lineargradient0() {
57 const SkPoint pts[] = { { 0, 0 }, { 100, 100 } }; 57 const SkPoint pts[] = { { 0, 0 }, { 100, 100 } };
58 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; 58 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
(...skipping 26 matching lines...) Expand all
85 } 85 }
86 } 86 }
87 87
88 //////////////////////////////////////////////////////////////////////////////// /////////////////// 88 //////////////////////////////////////////////////////////////////////////////// ///////////////////
89 89
90 static SkImageFilter* make_blur(float amount, SkImageFilter* input = nullptr) { 90 static SkImageFilter* make_blur(float amount, SkImageFilter* input = nullptr) {
91 return SkBlurImageFilter::Create(amount, amount, input); 91 return SkBlurImageFilter::Create(amount, amount, input);
92 } 92 }
93 93
94 static SkImageFilter* make_brightness(float amount, SkImageFilter* input = nullp tr) { 94 static SkImageFilter* make_brightness(float amount, SkImageFilter* input = nullp tr) {
95 SkAutoTUnref<SkColorFilter> filter(cf_make_brightness(amount)); 95 return SkColorFilterImageFilter::Create(cf_make_brightness(amount).get(), in put);
96 return SkColorFilterImageFilter::Create(filter, input);
97 } 96 }
98 97
99 static SkImageFilter* make_grayscale(SkImageFilter* input = nullptr) { 98 static SkImageFilter* make_grayscale(SkImageFilter* input = nullptr) {
100 SkAutoTUnref<SkColorFilter> filter(cf_make_grayscale()); 99 return SkColorFilterImageFilter::Create(cf_make_grayscale().get(), input);
101 return SkColorFilterImageFilter::Create(filter, input);
102 } 100 }
103 101
104 static SkImageFilter* make_mode_blue(SkImageFilter* input = nullptr) { 102 static SkImageFilter* make_mode_blue(SkImageFilter* input = nullptr) {
105 SkAutoTUnref<SkColorFilter> filter(cf_make_colorize(SK_ColorBLUE)); 103 return SkColorFilterImageFilter::Create(cf_make_colorize(SK_ColorBLUE).get() , input);
106 return SkColorFilterImageFilter::Create(filter, input);
107 } 104 }
108 105
109 static void drawClippedRect(SkCanvas* canvas, 106 static void drawClippedRect(SkCanvas* canvas,
110 const SkRect& r, 107 const SkRect& r,
111 const SkPaint& paint, 108 const SkPaint& paint,
112 float outset = 0.0f) { 109 float outset = 0.0f) {
113 canvas->save(); 110 canvas->save();
114 SkRect clip(r); 111 SkRect clip(r);
115 clip.outset(outset, outset); 112 clip.outset(outset, outset);
116 canvas->clipRect(clip); 113 canvas->clipRect(clip);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 paint.setImageFilter(blue.get()); 169 paint.setImageFilter(blue.get());
173 drawClippedRect(canvas, r, paint, 5); 170 drawClippedRect(canvas, r, paint, 5);
174 canvas->translate(FILTER_WIDTH + MARGIN, 0); 171 canvas->translate(FILTER_WIDTH + MARGIN, 0);
175 } 172 }
176 } 173 }
177 174
178 DEF_SIMPLE_GM(colorfilterimagefilter_layer, canvas, 32, 32) { 175 DEF_SIMPLE_GM(colorfilterimagefilter_layer, canvas, 32, 32) {
179 SkAutoCanvasRestore autoCanvasRestore(canvas, false); 176 SkAutoCanvasRestore autoCanvasRestore(canvas, false);
180 SkColorMatrix cm; 177 SkColorMatrix cm;
181 cm.setSaturation(0.0f); 178 cm.setSaturation(0.0f);
182 SkAutoTUnref<SkColorFilter> cf(SkColorMatrixFilter::Create(cm)); 179 auto cf(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
183 SkAutoTUnref<SkImageFilter> imf(SkColorFilterImageFilter::Create(cf)); 180 SkAutoTUnref<SkImageFilter> imf(SkColorFilterImageFilter::Create(cf.get()));
184 SkPaint p; 181 SkPaint p;
185 p.setImageFilter(imf); 182 p.setImageFilter(imf);
186 canvas->saveLayer(NULL, &p); 183 canvas->saveLayer(NULL, &p);
187 canvas->clear(SK_ColorRED); 184 canvas->clear(SK_ColorRED);
188 } 185 }
189 186
190 //////////////////////////////////////////////////////////////////////////////// /////////////////// 187 //////////////////////////////////////////////////////////////////////////////// ///////////////////
191 188
192 template <typename T> class SkTRefArray : public SkTDArray<T> { 189 template <typename T> class SkTRefArray : public SkTDArray<T> {
193 public: 190 public:
194 ~SkTRefArray() { this->unrefAll(); } 191 ~SkTRefArray() { this->unrefAll(); }
195 }; 192 };
196 193
197 DEF_SIMPLE_GM(colorfiltershader, canvas, 800, 800) { 194 DEF_SIMPLE_GM(colorfiltershader, canvas, 800, 800) {
198 SkTRefArray<SkColorFilter*> filters; 195 SkTArray<sk_sp<SkColorFilter>> filters;
199 sk_gm_get_colorfilters(&filters); 196 sk_gm_get_colorfilters(&filters);
200 197
201 SkTRefArray<SkShader*> shaders; 198 SkTRefArray<SkShader*> shaders;
202 sk_gm_get_shaders(&shaders); 199 sk_gm_get_shaders(&shaders);
203 200
204 SkPaint paint; 201 SkPaint paint;
205 SkRect r = SkRect::MakeWH(120, 120); 202 SkRect r = SkRect::MakeWH(120, 120);
206 203
207 canvas->translate(20, 20); 204 canvas->translate(20, 20);
208 for (int y = 0; y < shaders.count(); ++y) { 205 for (int y = 0; y < shaders.count(); ++y) {
209 SkShader* shader = shaders[y]; 206 SkShader* shader = shaders[y];
210 207
211 canvas->save(); 208 canvas->save();
212 for (int x = -1; x < filters.count(); ++x) { 209 for (int x = -1; x < filters.count(); ++x) {
213 SkColorFilter* filter = x >= 0 ? filters[x] : nullptr; 210 sk_sp<SkColorFilter> filter = x >= 0 ? filters[x] : nullptr;
214 211
215 paint.setShader(shader->makeWithColorFilter(filter)); 212 paint.setShader(shader->makeWithColorFilter(filter));
216 canvas->drawRect(r, paint); 213 canvas->drawRect(r, paint);
217 canvas->translate(150, 0); 214 canvas->translate(150, 0);
218 } 215 }
219 canvas->restore(); 216 canvas->restore();
220 canvas->translate(0, 150); 217 canvas->translate(0, 150);
221 } 218 }
222 } 219 }
OLDNEW
« no previous file with comments | « gm/coloremoji.cpp ('k') | gm/colorfilters.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698