2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkAlphaThresholdFilter.h"
10 #include "SkOffsetImageFilter.h"
12 #include "SkSurface.h"
17 static void draw_rects(SkCanvas* canvas) {
19 rectPaint.setColor(SK_ColorBLUE);
20 canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
21 rectPaint.setColor(0xBFFF0000);
22 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
23 rectPaint.setColor(0x3F00FF00);
24 canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
25 rectPaint.setColor(SK_ColorTRANSPARENT);
26 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
29 static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
31 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
32 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
34 region.setRects(rects, 2);
37 sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
38 paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
42 class ImageAlphaThresholdGM : public skiagm::GM {
44 ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
45 this->setBGColor(SK_ColorWHITE);
50 SkString onShortName() override {
52 return SkString("imagealphathreshold_crop");
55 return SkString("imagealphathreshold");
58 SkISize onISize() override {
59 return SkISize::Make(WIDTH, HEIGHT);
62 void onDraw(SkCanvas* canvas) override {
65 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
66 matrix.postScale(.8f, .8f);
68 canvas->concat(matrix);
70 SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
71 SkImageFilter::CropRect cropRect(r);
73 SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
74 canvas->saveLayer(nullptr, &paint);
86 // Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
88 static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
89 SkAlphaType alphaType) {
91 SkColorType ct = canvas->imageInfo().colorType();
92 sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
94 if (kUnknown_SkColorType == ct) {
95 // For backends that aren't yet color-space aware we just fallback to N32.
96 ct = kN32_SkColorType;
100 SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs));
102 sk_sp<SkSurface> result = canvas->makeSurface(info);
104 result = SkSurface::MakeRaster(info);
110 class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
112 ImageAlphaThresholdSurfaceGM() {
113 this->setBGColor(0xFFFFFFFF);
117 SkString onShortName() override {
118 return SkString("imagealphathreshold_surface");
121 SkISize onISize() override {
122 return SkISize::Make(WIDTH, HEIGHT);
125 void onDraw(SkCanvas* canvas) override {
128 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
129 matrix.postScale(.8f, .8f);
131 canvas->concat(matrix);
133 sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
134 kPremul_SkAlphaType));
139 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
140 draw_rects(surface->getCanvas());
142 SkPaint paint = create_filter_paint();
143 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
144 canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
148 typedef skiagm::GM INHERITED;
151 //////////////////////////////////////////////////////////////////////////////
153 DEF_GM(return new ImageAlphaThresholdGM(true);)
154 DEF_GM(return new ImageAlphaThresholdGM(false);)
155 DEF_GM(return new ImageAlphaThresholdSurfaceGM();)