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