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

Side by Side Diff: gm/xfermodeimagefilter.cpp

Issue 1343123002: Convert unit tests, GMs from SkBitmapSource to SkImagesource (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: review comments Created 5 years, 3 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/tileimagefilter.cpp ('k') | samplecode/SampleFilterFuzz.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 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 "sk_tool_utils.h" 9 #include "sk_tool_utils.h"
10 #include "SkArithmeticMode.h" 10 #include "SkArithmeticMode.h"
11 #include "SkImage.h"
12 #include "SkImageSource.h"
11 #include "SkOffsetImageFilter.h" 13 #include "SkOffsetImageFilter.h"
12 #include "SkXfermodeImageFilter.h" 14 #include "SkXfermodeImageFilter.h"
13 #include "SkBitmapSource.h"
14 15
15 #define WIDTH 600 16 #define WIDTH 600
16 #define HEIGHT 600 17 #define HEIGHT 600
17 #define MARGIN 12 18 #define MARGIN 12
18 19
19 namespace skiagm { 20 namespace skiagm {
20 21
21 class XfermodeImageFilterGM : public GM { 22 class XfermodeImageFilterGM : public GM {
22 public: 23 public:
23 XfermodeImageFilterGM(){ 24 XfermodeImageFilterGM(){
24 this->setBGColor(0xFF000000); 25 this->setBGColor(0xFF000000);
25 } 26 }
26 27
27 protected: 28 protected:
28 SkString onShortName() override { 29 SkString onShortName() override {
29 return SkString("xfermodeimagefilter"); 30 return SkString("xfermodeimagefilter");
30 } 31 }
31 32
32 SkISize onISize() override { 33 SkISize onISize() override {
33 return SkISize::Make(WIDTH, HEIGHT); 34 return SkISize::Make(WIDTH, HEIGHT);
34 } 35 }
35 36
36 static void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, cons t SkPaint& paint,
37 int x, int y) {
38 canvas->save();
39 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
40 canvas->clipRect(SkRect::MakeWH(
41 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())));
42 canvas->drawBitmap(bitmap, 0, 0, &paint);
43 canvas->restore();
44 }
45
46 static void drawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkP aint& paint,
47 int x, int y) {
48 canvas->save();
49 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
50 canvas->clipRect(rect);
51 canvas->drawPaint(paint);
52 canvas->restore();
53 }
54
55 void onOnceBeforeDraw() override { 37 void onOnceBeforeDraw() override {
56 fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65 , 96, "e"); 38 fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65 , 96, "e");
57 39
58 fCheckerboard = sk_tool_utils::create_checkerboard_bitmap( 40 fCheckerboard.reset(SkImage::NewFromBitmap(
59 80, 80, 41 sk_tool_utils::create_checkerboard_bitmap(80, 80,
60 sk_tool_utils::color_to_ 565(0xFFA0A0A0), 42 sk_tool_utils::color_to_56 5(0xFFA0A0A0),
61 sk_tool_utils::color_to_ 565(0xFF404040), 43 sk_tool_utils::color_to_56 5(0xFF404040),
62 8); 44 8)));
63 } 45 }
64 46
65 void onDraw(SkCanvas* canvas) override { 47 void onDraw(SkCanvas* canvas) override {
66 canvas->clear(SK_ColorBLACK); 48 canvas->clear(SK_ColorBLACK);
67 SkPaint paint; 49 SkPaint paint;
68 50
69 const struct { 51 const struct {
70 SkXfermode::Mode fMode; 52 SkXfermode::Mode fMode;
71 const char* fLabel; 53 const char* fLabel;
72 } gModes[] = { 54 } gModes[] = {
(...skipping 23 matching lines...) Expand all
96 { SkXfermode::kDifference_Mode, "Difference" }, 78 { SkXfermode::kDifference_Mode, "Difference" },
97 { SkXfermode::kExclusion_Mode, "Exclusion" }, 79 { SkXfermode::kExclusion_Mode, "Exclusion" },
98 { SkXfermode::kMultiply_Mode, "Multiply" }, 80 { SkXfermode::kMultiply_Mode, "Multiply" },
99 { SkXfermode::kHue_Mode, "Hue" }, 81 { SkXfermode::kHue_Mode, "Hue" },
100 { SkXfermode::kSaturation_Mode, "Saturation" }, 82 { SkXfermode::kSaturation_Mode, "Saturation" },
101 { SkXfermode::kColor_Mode, "Color" }, 83 { SkXfermode::kColor_Mode, "Color" },
102 { SkXfermode::kLuminosity_Mode, "Luminosity" }, 84 { SkXfermode::kLuminosity_Mode, "Luminosity" },
103 }; 85 };
104 86
105 int x = 0, y = 0; 87 int x = 0, y = 0;
106 SkAutoTUnref<SkImageFilter> background(SkBitmapSource::Create(fCheckerbo ard)); 88 SkAutoTUnref<SkImageFilter> background(SkImageSource::Create(fCheckerboa rd));
107 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { 89 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
108 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode)); 90 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode));
109 SkAutoTUnref<SkImageFilter> filter(SkXfermodeImageFilter::Create(mod e, background)); 91 SkAutoTUnref<SkImageFilter> filter(SkXfermodeImageFilter::Create(mod e, background));
110 paint.setImageFilter(filter); 92 paint.setImageFilter(filter);
111 drawClippedBitmap(canvas, fBitmap, paint, x, y); 93 DrawClippedBitmap(canvas, fBitmap, paint, x, y);
112 x += fBitmap.width() + MARGIN; 94 x += fBitmap.width() + MARGIN;
113 if (x + fBitmap.width() > WIDTH) { 95 if (x + fBitmap.width() > WIDTH) {
114 x = 0; 96 x = 0;
115 y += fBitmap.height() + MARGIN; 97 y += fBitmap.height() + MARGIN;
116 } 98 }
117 } 99 }
118 // Test arithmetic mode as image filter 100 // Test arithmetic mode as image filter
119 SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK _Scalar1, 0)); 101 SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK _Scalar1, 0));
120 SkAutoTUnref<SkImageFilter> filter(SkXfermodeImageFilter::Create(mode, b ackground)); 102 SkAutoTUnref<SkImageFilter> filter(SkXfermodeImageFilter::Create(mode, b ackground));
121 paint.setImageFilter(filter); 103 paint.setImageFilter(filter);
122 drawClippedBitmap(canvas, fBitmap, paint, x, y); 104 DrawClippedBitmap(canvas, fBitmap, paint, x, y);
123 x += fBitmap.width() + MARGIN; 105 x += fBitmap.width() + MARGIN;
124 if (x + fBitmap.width() > WIDTH) { 106 if (x + fBitmap.width() > WIDTH) {
125 x = 0; 107 x = 0;
126 y += fBitmap.height() + MARGIN; 108 y += fBitmap.height() + MARGIN;
127 } 109 }
128 // Test nullptr mode 110 // Test nullptr mode
129 filter.reset(SkXfermodeImageFilter::Create(nullptr, background)); 111 filter.reset(SkXfermodeImageFilter::Create(nullptr, background));
130 paint.setImageFilter(filter); 112 paint.setImageFilter(filter);
131 drawClippedBitmap(canvas, fBitmap, paint, x, y); 113 DrawClippedBitmap(canvas, fBitmap, paint, x, y);
132 x += fBitmap.width() + MARGIN; 114 x += fBitmap.width() + MARGIN;
133 if (x + fBitmap.width() > WIDTH) { 115 if (x + fBitmap.width() > WIDTH) {
134 x = 0; 116 x = 0;
135 y += fBitmap.height() + MARGIN; 117 y += fBitmap.height() + MARGIN;
136 } 118 }
137 SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4), 119 SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4),
138 SkIntToScalar(fBitmap.height() + 4)); 120 SkIntToScalar(fBitmap.height() + 4));
139 // Test offsets on SrcMode (uses fixed-function blend) 121 // Test offsets on SrcMode (uses fixed-function blend)
140 SkAutoTUnref<SkImageFilter> foreground(SkBitmapSource::Create(fBitmap)); 122 SkAutoTUnref<SkImage> bitmapImage(SkImage::NewFromBitmap(fBitmap));
123 SkAutoTUnref<SkImageFilter> foreground(SkImageSource::Create(bitmapImage ));
141 SkAutoTUnref<SkImageFilter> offsetForeground(SkOffsetImageFilter::Create ( 124 SkAutoTUnref<SkImageFilter> offsetForeground(SkOffsetImageFilter::Create (
142 SkIntToScalar(4), SkIntToScalar(-4), foreground)); 125 SkIntToScalar(4), SkIntToScalar(-4), foreground));
143 SkAutoTUnref<SkImageFilter> offsetBackground(SkOffsetImageFilter::Create ( 126 SkAutoTUnref<SkImageFilter> offsetBackground(SkOffsetImageFilter::Create (
144 SkIntToScalar(4), SkIntToScalar(4), background)); 127 SkIntToScalar(4), SkIntToScalar(4), background));
145 mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); 128 mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
146 filter.reset(SkXfermodeImageFilter::Create(mode, offsetBackground, offse tForeground)); 129 filter.reset(SkXfermodeImageFilter::Create(mode, offsetBackground, offse tForeground));
147 paint.setImageFilter(filter); 130 paint.setImageFilter(filter);
148 drawClippedPaint(canvas, clipRect, paint, x, y); 131 DrawClippedPaint(canvas, clipRect, paint, x, y);
149 x += fBitmap.width() + MARGIN; 132 x += fBitmap.width() + MARGIN;
150 if (x + fBitmap.width() > WIDTH) { 133 if (x + fBitmap.width() > WIDTH) {
151 x = 0; 134 x = 0;
152 y += fBitmap.height() + MARGIN; 135 y += fBitmap.height() + MARGIN;
153 } 136 }
154 // Test offsets on Darken (uses shader blend) 137 // Test offsets on Darken (uses shader blend)
155 mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode)); 138 mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode));
156 filter.reset(SkXfermodeImageFilter::Create(mode, offsetBackground, offse tForeground)); 139 filter.reset(SkXfermodeImageFilter::Create(mode, offsetBackground, offse tForeground));
157 paint.setImageFilter(filter); 140 paint.setImageFilter(filter);
158 drawClippedPaint(canvas, clipRect, paint, x, y); 141 DrawClippedPaint(canvas, clipRect, paint, x, y);
159 x += fBitmap.width() + MARGIN; 142 x += fBitmap.width() + MARGIN;
160 if (x + fBitmap.width() > WIDTH) { 143 if (x + fBitmap.width() > WIDTH) {
161 x = 0; 144 x = 0;
162 y += fBitmap.height() + MARGIN; 145 y += fBitmap.height() + MARGIN;
163 } 146 }
164 // Test cropping 147 // Test cropping
165 static const size_t nbSamples = 3; 148 static const size_t nbSamples = 3;
166 SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode, 149 SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode,
167 SkXfermode::kSrcOver_Mode, 150 SkXfermode::kSrcOver_Mode,
168 SkXfermode::kPlus_Mode}; 151 SkXfermode::kPlus_Mode};
169 int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, 152 int offsets[nbSamples][4] = {{ 10, 10, -16, -16},
170 { 10, 10, 10, 10}, 153 { 10, 10, 10, 10},
171 {-10, -10, -6, -6}}; 154 {-10, -10, -6, -6}};
172 for (size_t i = 0; i < nbSamples; ++i) { 155 for (size_t i = 0; i < nbSamples; ++i) {
173 SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0], 156 SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0],
174 offsets[i][1], 157 offsets[i][1],
175 fBitmap.width() + offsets[i][2 ], 158 fBitmap.width() + offsets[i][2 ],
176 fBitmap.height() + offsets[i][3 ]); 159 fBitmap.height() + offsets[i][3 ]);
177 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); 160 SkImageFilter::CropRect rect(SkRect::Make(cropRect));
178 mode.reset(SkXfermode::Create(sampledModes[i])); 161 mode.reset(SkXfermode::Create(sampledModes[i]));
179 filter.reset(SkXfermodeImageFilter::Create( 162 filter.reset(SkXfermodeImageFilter::Create(
180 mode, offsetBackground, offsetForeground, &r ect)); 163 mode, offsetBackground, offsetForeground, &r ect));
181 paint.setImageFilter(filter); 164 paint.setImageFilter(filter);
182 drawClippedPaint(canvas, clipRect, paint, x, y); 165 DrawClippedPaint(canvas, clipRect, paint, x, y);
183 x += fBitmap.width() + MARGIN; 166 x += fBitmap.width() + MARGIN;
184 if (x + fBitmap.width() > WIDTH) { 167 if (x + fBitmap.width() > WIDTH) {
185 x = 0; 168 x = 0;
186 y += fBitmap.height() + MARGIN; 169 y += fBitmap.height() + MARGIN;
187 } 170 }
188 } 171 }
189 } 172 }
173
190 private: 174 private:
191 SkBitmap fBitmap, fCheckerboard; 175 static void DrawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, cons t SkPaint& paint,
176 int x, int y) {
177 canvas->save();
178 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
179 canvas->clipRect(SkRect::MakeWH(
180 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())));
181 canvas->drawBitmap(bitmap, 0, 0, &paint);
182 canvas->restore();
183 }
184
185 static void DrawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkP aint& paint,
186 int x, int y) {
187 canvas->save();
188 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
189 canvas->clipRect(rect);
190 canvas->drawPaint(paint);
191 canvas->restore();
192 }
193
194 SkBitmap fBitmap;
195 SkAutoTUnref<SkImage> fCheckerboard;
196
192 typedef GM INHERITED; 197 typedef GM INHERITED;
193 }; 198 };
194 199
195 ////////////////////////////////////////////////////////////////////////////// 200 //////////////////////////////////////////////////////////////////////////////
196 201
197 DEF_GM( return new XfermodeImageFilterGM; ); 202 DEF_GM( return new XfermodeImageFilterGM; );
198 203
199 } 204 }
OLDNEW
« no previous file with comments | « gm/tileimagefilter.cpp ('k') | samplecode/SampleFilterFuzz.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698