• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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/gm.h"
9 #include "include/core/SkBlurTypes.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkMaskFilter.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/effects/SkImageFilters.h"
22 #include "tools/Resources.h"
23 #include "tools/ToolUtils.h"
24 
25 #include <stdio.h>
26 
27 DEF_SIMPLE_GM(blurimagevmask, canvas, 700, 1200) {
28     SkPaint paint;
29     paint.setAntiAlias(true);
30     paint.setColor(SK_ColorBLACK);
31 
32     SkFont font(ToolUtils::create_portable_typeface(), 25);
33 
34     const double sigmas[] = {3.0, 8.0, 16.0, 24.0, 32.0};
35 
36     canvas->drawString("mask blur",  285, 50, font, paint);
37     canvas->drawString("image blur", 285 + 250, 50, font, paint);
38 
39 
40     SkRect r = {35, 100, 135, 200};
41     for (auto sigma:sigmas) {
42 
43         canvas->drawRect(r, paint);
44 
45         char out[100];
46         sprintf(out, "Sigma: %g", sigma);
47         canvas->drawString(out, r.left(), r.bottom() + 35, font, paint);
48 
49         r.offset(250, 0);
50 
51         paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma));
52         canvas->drawRect(r, paint);
53         paint.setMaskFilter(nullptr);
54 
55         SkPaint imageBlurPaint;
56         r.offset(250, 0);
57         imageBlurPaint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
58         canvas->saveLayer(nullptr, &imageBlurPaint);
59 
60         canvas->drawRect(r, paint);
61         canvas->restore();
62         r.offset(-500, 200);
63     }
64 
65 }
66 
67 DEF_SIMPLE_GM_CAN_FAIL(blur_image, canvas, errorMsg, 500, 500) {
68     auto image = GetResourceAsImage("images/mandrill_128.png");
69     if (!image) {
70         *errorMsg = "Could not load mandrill_128.png. Did you forget to set the resourcePath?";
71         return skiagm::DrawResult::kFail;
72     }
73 
74     SkPaint paint;
75     paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4));
76 
77     // both of these should draw with the blur, but (formerally) we had a bug where the unscaled
78     // version (taking the spriteblitter code path) ignore the maskfilter.
79 
80     canvas->drawImage(image, 10, 10, &paint);
81     canvas->scale(1.01f, 1.01f);
82     canvas->drawImage(image, 10 + image->width() + 10.f, 10, &paint);
83     return skiagm::DrawResult::kOk;
84 }
85