• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "gm.h"
9 #include "SkAlphaThresholdFilter.h"
10 #include "SkImageSource.h"
11 #include "SkOffsetImageFilter.h"
12 #include "SkRandom.h"
13 #include "SkRegion.h"
14 #include "SkSurface.h"
15 #include "sk_tool_utils.h"
16 
17 #define WIDTH 500
18 #define HEIGHT 500
19 
draw_rects(SkCanvas * canvas)20 static void draw_rects(SkCanvas* canvas) {
21     SkPaint rectPaint;
22     rectPaint.setColor(SK_ColorBLUE);
23     canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
24     rectPaint.setColor(0xBFFF0000);
25     canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
26     rectPaint.setColor(0x3F00FF00);
27     canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
28     rectPaint.setColor(SK_ColorTRANSPARENT);
29     canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
30 }
31 
create_filter_paint(SkImageFilter::CropRect * cropRect=nullptr)32 static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
33     SkIRect rects[2];
34     rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
35     rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
36     SkRegion region;
37     region.setRects(rects, 2);
38 
39     SkPaint paint;
40     sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
41     paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
42     return paint;
43 }
44 
45 class ImageAlphaThresholdGM : public skiagm::GM {
46 public:
ImageAlphaThresholdGM(bool useCropRect)47     ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
48         this->setBGColor(SK_ColorWHITE);
49     }
50 
51 protected:
52 
onShortName()53     SkString onShortName() override {
54         if (fUseCropRect) {
55             return SkString("imagealphathreshold_crop");
56         }
57 
58         return SkString("imagealphathreshold");
59     }
60 
onISize()61     SkISize onISize() override {
62         return SkISize::Make(WIDTH, HEIGHT);
63     }
64 
onDraw(SkCanvas * canvas)65     void onDraw(SkCanvas* canvas) override {
66         SkMatrix matrix;
67         matrix.reset();
68         matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
69         matrix.postScale(.8f, .8f);
70 
71         canvas->concat(matrix);
72 
73         SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
74         SkImageFilter::CropRect cropRect(r);
75 
76         SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
77         canvas->saveLayer(nullptr, &paint);
78         draw_rects(canvas);
79 
80         canvas->restore();
81     }
82 
83 private:
84     bool fUseCropRect;
85 
86     typedef GM INHERITED;
87 };
88 
89 // Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
90 // best we can
make_color_matching_surface(SkCanvas * canvas,int width,int height,SkAlphaType alphaType)91 static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
92                                                     SkAlphaType alphaType) {
93 
94     SkColorType ct = canvas->imageInfo().colorType();
95     sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
96 
97     if (kUnknown_SkColorType == ct) {
98         // For backends that aren't yet color-space aware we just fallback to N32.
99         ct = kN32_SkColorType;
100         cs = nullptr;
101     }
102 
103     SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs));
104 
105     return sk_tool_utils::makeSurface(canvas, info);
106 }
107 
108 class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
109 public:
ImageAlphaThresholdSurfaceGM()110     ImageAlphaThresholdSurfaceGM() {
111         this->setBGColor(0xFFFFFFFF);
112     }
113 
114 protected:
onShortName()115     SkString onShortName() override {
116         return SkString("imagealphathreshold_surface");
117     }
118 
onISize()119     SkISize onISize() override {
120         return SkISize::Make(WIDTH, HEIGHT);
121     }
122 
onDraw(SkCanvas * canvas)123     void onDraw(SkCanvas* canvas) override {
124         SkMatrix matrix;
125         matrix.reset();
126         matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
127         matrix.postScale(.8f, .8f);
128 
129         canvas->concat(matrix);
130 
131         sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
132                                                              kPremul_SkAlphaType));
133         if (!surface) {
134             return;
135         }
136 
137         surface->getCanvas()->clear(SK_ColorTRANSPARENT);
138         draw_rects(surface->getCanvas());
139 
140         SkPaint paint = create_filter_paint();
141         canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
142         canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
143     }
144 
145 private:
146     typedef skiagm::GM INHERITED;
147 };
148 
149 //////////////////////////////////////////////////////////////////////////////
150 
151 DEF_GM(return new ImageAlphaThresholdGM(true);)
DEF_GM(return new ImageAlphaThresholdGM (false);)152 DEF_GM(return new ImageAlphaThresholdGM(false);)
153 DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
154 
155 //////////////////////////////////////////////////////////////////////////////
156 
157 static sk_sp<SkImage> make_img() {
158     SkBitmap bitmap;
159     bitmap.allocPixels(SkImageInfo::MakeS32(WIDTH, HEIGHT, kPremul_SkAlphaType));
160     SkCanvas canvas(bitmap);
161 
162     SkPaint paint;
163     SkRect rect = SkRect::MakeWH(WIDTH, HEIGHT);
164     SkRandom rnd;
165 
166     while (!rect.isEmpty()) {
167         paint.setColor(rnd.nextU() | (0xFF << 24));
168         canvas.drawRect(rect, paint);
169         rect.inset(25, 25);
170     }
171 
172     return SkImage::MakeFromBitmap(bitmap);
173 }
174 
175 DEF_SIMPLE_GM_BG(imagealphathreshold_image, canvas, WIDTH * 2, HEIGHT, SK_ColorBLACK) {
176     sk_sp<SkImage> image(make_img());
177 
178     SkIRect rects[2];
179     rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
180     rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
181     SkRegion region;
182     region.setRects(rects, 2);
183 
184     SkPaint filterPaint;
185     sk_sp<SkImageFilter> imageSource(SkImageSource::Make(image));
186     filterPaint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f,
187                                                             std::move(imageSource)));
188 
189     canvas->saveLayer(nullptr, &filterPaint);
190     canvas->restore();
191     canvas->translate(WIDTH, 0);
192     canvas->drawImage(image, 0, 0);
193 }
194