Flutter Engine
The Flutter Engine
MatrixConvolutionBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 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#include "bench/Benchmark.h"
13#include "src/base/SkRandom.h"
14
15#include "tools/ToolUtils.h"
16
18public:
19 MatrixConvolutionBench(bool bigKernel, SkTileMode tileMode, bool convolveAlpha)
20 : fName(SkStringPrintf("matrixconvolution_%s%s%s",
21 bigKernel ? "bigKernel_" : "",
22 ToolUtils::tilemode_name(tileMode),
23 convolveAlpha ? "" : "_noConvolveAlpha")) {
24 if (bigKernel) {
25 SkISize kernelSize = SkISize::Make(9, 9);
26 SkScalar kernel[81];
27 for (int i = 0; i < 81; i++) {
28 kernel[i] = SkIntToScalar(1);
29 }
30 kernel[40] = SkIntToScalar(-79);
31 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
32 SkIPoint kernelOffset = SkIPoint::Make(4, 4);
33 fFilter = SkImageFilters::MatrixConvolution(kernelSize, kernel, gain, bias,
34 kernelOffset, tileMode, convolveAlpha,
35 nullptr);
36 } else {
37 SkISize kernelSize = SkISize::Make(3, 3);
38 SkScalar kernel[9] = {
42 };
43 SkScalar gain = 0.3f, bias = SkIntToScalar(100);
44 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
45 fFilter = SkImageFilters::MatrixConvolution(kernelSize, kernel, gain, bias,
46 kernelOffset, tileMode, convolveAlpha,
47 nullptr);
48 }
49 }
50
51protected:
52 const char* onGetName() override {
53 return fName.c_str();
54 }
55
56 void onDraw(int loops, SkCanvas* canvas) override {
58 this->setupPaint(&paint);
59 paint.setImageFilter(fFilter);
60 paint.setAntiAlias(true);
61
62 SkRandom rand;
63 for (int i = 0; i < loops; i++) {
64 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
65 rand.nextUScalar1() * 400);
66 canvas->drawOval(r, paint);
67 }
68 }
69
70private:
72 SkString fName;
73
74 using INHERITED = Benchmark;
75};
76
77DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kClamp, true); )
78DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kRepeat, true); )
79DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kMirror, true); )
80DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kDecal, true); )
81DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kDecal, false); )
82
83DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kClamp, true); )
86DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kDecal, true); )
87DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kDecal, false); )
#define DEF_BENCH(code)
Definition: Benchmark.h:20
#define SkIntToScalar(x)
Definition: SkScalar.h:57
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
SkTileMode
Definition: SkTileMode.h:13
virtual void setupPaint(SkPaint *paint)
Definition: Benchmark.cpp:55
const char * onGetName() override
void onDraw(int loops, SkCanvas *canvas) override
MatrixConvolutionBench(bool bigKernel, SkTileMode tileMode, bool convolveAlpha)
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
static sk_sp< SkImageFilter > MatrixConvolution(const SkISize &kernelSize, const SkScalar kernel[], SkScalar gain, SkScalar bias, const SkIPoint &kernelOffset, SkTileMode tileMode, bool convolveAlpha, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
SkScalar nextUScalar1()
Definition: SkRandom.h:101
const char * c_str() const
Definition: SkString.h:133
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
const char * tilemode_name(SkTileMode mode)
Definition: ToolUtils.cpp:129
static constexpr SkIPoint Make(int32_t x, int32_t y)
Definition: SkPoint_impl.h:38
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609