| 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/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkImage.h" |
| 12 |
#include "include/core/SkImageFilter.h" |
| 13 |
#include "include/core/SkImageInfo.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkScalar.h" |
| 18 |
#include "include/core/SkSize.h" |
| 19 |
#include "include/core/SkString.h" |
| 20 |
#include "include/core/SkSurface.h" |
| 21 |
#include "include/effects/SkImageFilters.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
|
| 24 |
#include <initializer_list> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
static sk_sp<SkImage> make_image(SkCanvas* canvas) { |
| 28 |
SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200); |
| 29 |
auto surface = ToolUtils::makeSurface(canvas, info); |
| 30 |
SkCanvas* c = surface->getCanvas(); |
| 31 |
SkPaint paint; |
| 32 |
paint.setAntiAlias(true); |
| 33 |
|
| 34 |
paint.setColor(SK_ColorBLUE); |
| 35 |
c->drawRect(SkRect::MakeIWH(info.width(), info.height()), paint); |
| 36 |
paint.setColor(SK_ColorGREEN); |
| 37 |
c->drawCircle(125, 100, 100, paint); |
| 38 |
paint.setColor(SK_ColorRED); |
| 39 |
c->drawRect(SkRect::MakeIWH(80, 80), paint); |
| 40 |
|
| 41 |
return surface->makeImageSnapshot(); |
| 42 |
} |
| 43 |
|
| 44 |
static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) { |
| 45 |
SkAutoCanvasRestore acr(canvas, true); |
| 46 |
SkPaint paint; |
| 47 |
paint.setImageFilter(std::move(filter)); |
| 48 |
|
| 49 |
canvas->translate(SkIntToScalar(30), 0); |
| 50 |
canvas->clipIRect(image->bounds()); |
| 51 |
canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint); |
| 52 |
} |
| 53 |
|
| 54 |
namespace skiagm { |
| 55 |
|
| 56 |
// This GM draws one rectangle, one green inscribed circle, and one red square |
| 57 |
// with different blur settings. |
| 58 |
class ImageBlurClampModeGM : public GM { |
| 59 |
public: |
| 60 |
ImageBlurClampModeGM() { |
| 61 |
this->setBGColor(0xFFCCCCCC); |
| 62 |
} |
| 63 |
|
| 64 |
protected: |
| 65 |
SkString getName() const override { return SkString("imageblurclampmode"); } |
| 66 |
|
| 67 |
SkISize getISize() override { return SkISize::Make(850, 920); } |
| 68 |
|
| 69 |
bool runAsBench() const override { return true; } |
| 70 |
|
| 71 |
void onDraw(SkCanvas* canvas) override { |
| 72 |
sk_sp<SkImage> image(make_image(canvas)); |
| 73 |
sk_sp<SkImageFilter> filter; |
| 74 |
|
| 75 |
canvas->translate(0, 30); |
| 76 |
// Test different kernel size, including the one to launch 2d Gaussian |
| 77 |
// blur. |
| 78 |
for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) { |
| 79 |
canvas->save(); |
| 80 |
|
| 81 |
// x-only blur |
| 82 |
filter = SkImageFilters::Blur( |
| 83 |
sigma, 0.0f, SkTileMode::kClamp, nullptr, image->bounds()); |
| 84 |
draw_image(canvas, image, std::move(filter)); |
| 85 |
canvas->translate(image->width() + 20, 0); |
| 86 |
|
| 87 |
// y-only blur |
| 88 |
filter = SkImageFilters::Blur( |
| 89 |
0.0f, sigma, SkTileMode::kClamp, nullptr, image->bounds()); |
| 90 |
draw_image(canvas, image, std::move(filter)); |
| 91 |
canvas->translate(image->width() + 20, 0); |
| 92 |
|
| 93 |
// both directions |
| 94 |
filter = SkImageFilters::Blur( |
| 95 |
sigma, sigma, SkTileMode::kClamp, nullptr, image->bounds()); |
| 96 |
draw_image(canvas, image, std::move(filter)); |
| 97 |
canvas->translate(image->width() + 20, 0); |
| 98 |
|
| 99 |
canvas->restore(); |
| 100 |
|
| 101 |
canvas->translate(0, image->height() + 20); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
private: |
| 106 |
using INHERITED = GM; |
| 107 |
}; |
| 108 |
|
| 109 |
////////////////////////////////////////////////////////////////////////////// |
| 110 |
|
| 111 |
DEF_GM(return new ImageBlurClampModeGM;) |
| 112 |
} // namespace skiagm |