Chromium Code Reviews

Side by Side Diff: gm/imagefiltersgraph.cpp

Issue 1343123002: Convert unit tests, GMs from SkBitmapSource to SkImagesource (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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 9
10 #include "SkArithmeticMode.h" 10 #include "SkArithmeticMode.h"
11 #include "SkDevice.h" 11 #include "SkDevice.h"
12 #include "SkBitmapSource.h"
13 #include "SkBlurImageFilter.h" 12 #include "SkBlurImageFilter.h"
14 #include "SkColorFilter.h" 13 #include "SkColorFilter.h"
15 #include "SkColorFilterImageFilter.h" 14 #include "SkColorFilterImageFilter.h"
16 #include "SkColorMatrixFilter.h" 15 #include "SkColorMatrixFilter.h"
16 #include "SkImage.h"
17 #include "SkImageSource.h"
17 #include "SkReadBuffer.h" 18 #include "SkReadBuffer.h"
18 #include "SkWriteBuffer.h" 19 #include "SkWriteBuffer.h"
19 #include "SkMergeImageFilter.h" 20 #include "SkMergeImageFilter.h"
20 #include "SkMorphologyImageFilter.h" 21 #include "SkMorphologyImageFilter.h"
21 #include "SkTestImageFilters.h" 22 #include "SkTestImageFilters.h"
22 #include "SkXfermodeImageFilter.h" 23 #include "SkXfermodeImageFilter.h"
23 24
24 // More closely models how Blink's OffsetFilter works as of 10/23/13. SkOffsetIm ageFilter doesn't 25 // More closely models how Blink's OffsetFilter works as of 10/23/13. SkOffsetIm ageFilter doesn't
25 // perform a draw and this one does. 26 // perform a draw and this one does.
26 class SimpleOffsetFilter : public SkImageFilter { 27 class SimpleOffsetFilter : public SkImageFilter {
(...skipping 72 matching lines...)
99 100
100 class ImageFiltersGraphGM : public skiagm::GM { 101 class ImageFiltersGraphGM : public skiagm::GM {
101 public: 102 public:
102 ImageFiltersGraphGM() {} 103 ImageFiltersGraphGM() {}
103 104
104 protected: 105 protected:
105 106
106 SkString onShortName() override { 107 SkString onShortName() override {
107 return SkString("imagefiltersgraph"); 108 return SkString("imagefiltersgraph");
108 } 109 }
109 110
robertphillips 2015/09/15 17:43:52 Can this be static & DrawClippedImage ?
f(malita) 2015/09/15 18:12:53 Done.
110 void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPai nt& paint) { 111 void drawClippedImage(SkCanvas* canvas, const SkImage* image, const SkPaint& paint) {
111 canvas->save(); 112 canvas->save();
112 canvas->clipRect(SkRect::MakeXYWH(0, 0, 113 canvas->clipRect(SkRect::MakeIWH(image->width(), image->height()));
113 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); 114 canvas->drawImage(image, 0, 0, &paint);
114 canvas->drawBitmap(bitmap, 0, 0, &paint);
115 canvas->restore(); 115 canvas->restore();
116 } 116 }
117 117
118 SkISize onISize() override { return SkISize::Make(500, 150); } 118 SkISize onISize() override { return SkISize::Make(500, 150); }
119 119
120 void onOnceBeforeDraw() override { 120 void onOnceBeforeDraw() override {
121 fBitmap = sk_tool_utils::create_string_bitmap(100, 100, SK_ColorWHITE, 2 0, 70, 96, "e"); 121 fImage.reset(SkImage::NewFromBitmap(
122 sk_tool_utils::create_string_bitmap(100, 100, SK_ColorWHITE, 20, 70, 96, "e")));
122 } 123 }
123 124
124 void onDraw(SkCanvas* canvas) override { 125 void onDraw(SkCanvas* canvas) override {
125 canvas->clear(SK_ColorBLACK); 126 canvas->clear(SK_ColorBLACK);
126 { 127 {
127 SkAutoTUnref<SkImageFilter> bitmapSource(SkBitmapSource::Create(fBit map)); 128 SkAutoTUnref<SkImageFilter> bitmapSource(SkImageSource::Create(fImag e));
128 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_Co lorRED, 129 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_Co lorRED,
129 SkXfermode::kSrcIn_Mode )); 130 SkXfermode::kSrcIn_Mode ));
130 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(4.0f, 4.0 f, bitmapSource)); 131 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(4.0f, 4.0 f, bitmapSource));
131 SkAutoTUnref<SkImageFilter> erode(SkErodeImageFilter::Create(4, 4, b lur)); 132 SkAutoTUnref<SkImageFilter> erode(SkErodeImageFilter::Create(4, 4, b lur));
132 SkAutoTUnref<SkImageFilter> color(SkColorFilterImageFilter::Create(c f, erode)); 133 SkAutoTUnref<SkImageFilter> color(SkColorFilterImageFilter::Create(c f, erode));
133 SkAutoTUnref<SkImageFilter> merge(SkMergeImageFilter::Create(blur, c olor)); 134 SkAutoTUnref<SkImageFilter> merge(SkMergeImageFilter::Create(blur, c olor));
134 135
135 SkPaint paint; 136 SkPaint paint;
136 paint.setImageFilter(merge); 137 paint.setImageFilter(merge);
137 canvas->drawPaint(paint); 138 canvas->drawPaint(paint);
138 canvas->translate(SkIntToScalar(100), 0); 139 canvas->translate(SkIntToScalar(100), 0);
139 } 140 }
140 { 141 {
141 SkAutoTUnref<SkImageFilter> morph(SkDilateImageFilter::Create(5, 5)) ; 142 SkAutoTUnref<SkImageFilter> morph(SkDilateImageFilter::Create(5, 5)) ;
142 143
143 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, 144 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
144 0, SK_Scalar1, 0, 0, 0, 145 0, SK_Scalar1, 0, 0, 0,
145 0, 0, SK_Scalar1, 0, 0, 146 0, 0, SK_Scalar1, 0, 0,
146 0, 0, 0, 0.5f, 0 }; 147 0, 0, 0, 0.5f, 0 };
147 148
148 SkAutoTUnref<SkColorFilter> matrixFilter(SkColorMatrixFilter::Create (matrix)); 149 SkAutoTUnref<SkColorFilter> matrixFilter(SkColorMatrixFilter::Create (matrix));
149 SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Cre ate(matrixFilter, morph)); 150 SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Cre ate(matrixFilter, morph));
150 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOve r_Mode)); 151 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOve r_Mode));
151 SkAutoTUnref<SkImageFilter> blendColor(SkXfermodeImageFilter::Create (mode, colorMorph)); 152 SkAutoTUnref<SkImageFilter> blendColor(SkXfermodeImageFilter::Create (mode, colorMorph));
152 153
153 SkPaint paint; 154 SkPaint paint;
154 paint.setImageFilter(blendColor); 155 paint.setImageFilter(blendColor);
155 drawClippedBitmap(canvas, fBitmap, paint); 156 drawClippedImage(canvas, fImage, paint);
156 canvas->translate(SkIntToScalar(100), 0); 157 canvas->translate(SkIntToScalar(100), 0);
157 } 158 }
158 { 159 {
159 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, 160 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
160 0, SK_Scalar1, 0, 0, 0, 161 0, SK_Scalar1, 0, 0, 0,
161 0, 0, SK_Scalar1, 0, 0, 162 0, 0, SK_Scalar1, 0, 0,
162 0, 0, 0, 0.5f, 0 }; 163 0, 0, 0, 0.5f, 0 };
163 SkAutoTUnref<SkColorMatrixFilter> matrixCF(SkColorMatrixFilter::Crea te(matrix)); 164 SkAutoTUnref<SkColorMatrixFilter> matrixCF(SkColorMatrixFilter::Crea te(matrix));
164 SkAutoTUnref<SkImageFilter> matrixFilter(SkColorFilterImageFilter::C reate(matrixCF)); 165 SkAutoTUnref<SkImageFilter> matrixFilter(SkColorFilterImageFilter::C reate(matrixCF));
165 SkAutoTUnref<SkImageFilter> offsetFilter( 166 SkAutoTUnref<SkImageFilter> offsetFilter(
166 SimpleOffsetFilter::Create(10.0f, 10.f, matrixFilter)); 167 SimpleOffsetFilter::Create(10.0f, 10.f, matrixFilter));
167 168
168 SkAutoTUnref<SkXfermode> arith(SkArithmeticMode::Create(0, SK_Scalar 1, SK_Scalar1, 0)); 169 SkAutoTUnref<SkXfermode> arith(SkArithmeticMode::Create(0, SK_Scalar 1, SK_Scalar1, 0));
169 SkAutoTUnref<SkXfermodeImageFilter> arithFilter( 170 SkAutoTUnref<SkXfermodeImageFilter> arithFilter(
170 SkXfermodeImageFilter::Create(arith, matrixFilter, offsetFilter) ); 171 SkXfermodeImageFilter::Create(arith, matrixFilter, offsetFilter) );
171 172
172 SkPaint paint; 173 SkPaint paint;
173 paint.setImageFilter(arithFilter); 174 paint.setImageFilter(arithFilter);
174 drawClippedBitmap(canvas, fBitmap, paint); 175 drawClippedImage(canvas, fImage, paint);
175 canvas->translate(SkIntToScalar(100), 0); 176 canvas->translate(SkIntToScalar(100), 0);
176 } 177 }
177 { 178 {
178 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create( 179 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(
179 SkIntToScalar(10), SkIntToScalar(10))); 180 SkIntToScalar(10), SkIntToScalar(10)));
180 181
181 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcIn_ Mode)); 182 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcIn_ Mode));
182 SkImageFilter::CropRect cropRect(SkRect::MakeWH(SkIntToScalar(95), S kIntToScalar(100))); 183 SkImageFilter::CropRect cropRect(SkRect::MakeWH(SkIntToScalar(95), S kIntToScalar(100)));
183 SkAutoTUnref<SkImageFilter> blend( 184 SkAutoTUnref<SkImageFilter> blend(
184 SkXfermodeImageFilter::Create(mode, blur, nullptr, &cropRect)); 185 SkXfermodeImageFilter::Create(mode, blur, nullptr, &cropRect));
185 186
186 SkPaint paint; 187 SkPaint paint;
187 paint.setImageFilter(blend); 188 paint.setImageFilter(blend);
188 drawClippedBitmap(canvas, fBitmap, paint); 189 drawClippedImage(canvas, fImage, paint);
189 canvas->translate(SkIntToScalar(100), 0); 190 canvas->translate(SkIntToScalar(100), 0);
190 } 191 }
191 { 192 {
192 // Test that crop offsets are absolute, not relative to the parent's crop rect. 193 // Test that crop offsets are absolute, not relative to the parent's crop rect.
193 SkAutoTUnref<SkColorFilter> cf1(SkColorFilter::CreateModeFilter(SK_C olorBLUE, 194 SkAutoTUnref<SkColorFilter> cf1(SkColorFilter::CreateModeFilter(SK_C olorBLUE,
194 SkXf ermode::kSrcIn_Mode)); 195 SkXf ermode::kSrcIn_Mode));
195 SkAutoTUnref<SkColorFilter> cf2(SkColorFilter::CreateModeFilter(SK_C olorGREEN, 196 SkAutoTUnref<SkColorFilter> cf2(SkColorFilter::CreateModeFilter(SK_C olorGREEN,
196 SkXf ermode::kSrcIn_Mode)); 197 SkXf ermode::kSrcIn_Mode));
197 SkImageFilter::CropRect outerRect(SkRect::MakeXYWH(SkIntToScalar(10) , SkIntToScalar(10), 198 SkImageFilter::CropRect outerRect(SkRect::MakeXYWH(SkIntToScalar(10) , SkIntToScalar(10),
198 SkIntToScalar(80) , SkIntToScalar(80))); 199 SkIntToScalar(80) , SkIntToScalar(80)));
199 SkImageFilter::CropRect innerRect(SkRect::MakeXYWH(SkIntToScalar(20) , SkIntToScalar(20), 200 SkImageFilter::CropRect innerRect(SkRect::MakeXYWH(SkIntToScalar(20) , SkIntToScalar(20),
200 SkIntToScalar(60) , SkIntToScalar(60))); 201 SkIntToScalar(60) , SkIntToScalar(60)));
201 SkAutoTUnref<SkImageFilter> color1(SkColorFilterImageFilter::Create( cf1, nullptr, &outerRect)); 202 SkAutoTUnref<SkImageFilter> color1(SkColorFilterImageFilter::Create( cf1, nullptr, &outerRect));
202 SkAutoTUnref<SkImageFilter> color2(SkColorFilterImageFilter::Create( cf2, color1, &innerRect)); 203 SkAutoTUnref<SkImageFilter> color2(SkColorFilterImageFilter::Create( cf2, color1, &innerRect));
203 204
204 SkPaint paint; 205 SkPaint paint;
205 paint.setImageFilter(color2); 206 paint.setImageFilter(color2);
206 paint.setColor(SK_ColorRED); 207 paint.setColor(SK_ColorRED);
207 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint); 208 canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint);
208 canvas->translate(SkIntToScalar(100), 0); 209 canvas->translate(SkIntToScalar(100), 0);
209 } 210 }
210 } 211 }
211 212
212 private: 213 private:
213 typedef GM INHERITED; 214 typedef GM INHERITED;
214 SkBitmap fBitmap; 215 SkAutoTUnref<SkImage> fImage;
215 }; 216 };
216 217
217 /////////////////////////////////////////////////////////////////////////////// 218 ///////////////////////////////////////////////////////////////////////////////
218 219
219 DEF_GM(return new ImageFiltersGraphGM;) 220 DEF_GM(return new ImageFiltersGraphGM;)
OLDNEW

Powered by Google App Engine