1 /*
2 * Copyright 2018 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 "bench/Benchmark.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPictureRecorder.h"
12 #include "include/core/SkSurface.h"
13 #include "include/effects/SkShaderMaskFilter.h"
14 #include "src/shaders/SkPictureShader.h"
15
make_bitmap_shader()16 static sk_sp<SkShader> make_bitmap_shader() {
17 SkPaint p;
18 p.setColor(SK_ColorBLACK);
19 p.setAntiAlias(true);
20
21 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
22 surface->getCanvas()->drawCircle(50, 50, 50, p);
23
24 return surface->makeImageSnapshot()->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
25 SkSamplingOptions());
26 }
27
make_picture_shader()28 static sk_sp<SkShader> make_picture_shader() {
29 SkPaint p;
30 p.setColor(SK_ColorBLACK);
31 p.setAntiAlias(true);
32
33 SkPictureRecorder recorder;
34 recorder.beginRecording(100, 100)->drawCircle(50, 50, 50, p);
35
36 return recorder.finishRecordingAsPicture()->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
37 SkFilterMode::kNearest);
38 }
39
40 class ShaderMFBench final : public Benchmark {
41
42 public:
43 using ShaderMaker = sk_sp<SkShader>(*)();
44
ShaderMFBench(const char * nm,bool opaque,const ShaderMaker maker)45 ShaderMFBench(const char* nm, bool opaque, const ShaderMaker maker)
46 : fMaker{maker}
47 , fColor{opaque ? 0xff00ff00 : 0x8000ff00} {
48 fName = SkStringPrintf("shadermaskfilter_%s_%x", nm, SkColorGetA(fColor));
49 }
50
51 protected:
onGetName()52 const char* onGetName() override {
53 return fName.c_str();
54 }
55
onDelayedSetup()56 void onDelayedSetup() override {
57 fMaskFilter = SkShaderMaskFilter::Make(fMaker());
58 }
59
onDraw(int loops,SkCanvas * canvas)60 void onDraw(int loops, SkCanvas* canvas) override {
61 SkPaint maskPaint;
62 maskPaint.setMaskFilter(fMaskFilter);
63
64 for (int i = 0; i < loops; ++i) {
65 SkAutoCanvasRestore arc(canvas, false);
66 canvas->saveLayer(nullptr, &maskPaint);
67 canvas->drawColor(fColor);
68 }
69 }
70
71 private:
72 const ShaderMaker fMaker;
73 const SkColor fColor;
74 SkString fName;
75 sk_sp<SkMaskFilter> fMaskFilter;
76
77 using INHERITED = Benchmark;
78 };
79
80 DEF_BENCH( return new ShaderMFBench("bitmap" , true , make_bitmap_shader ); )
81 DEF_BENCH( return new ShaderMFBench("bitmap" , false, make_bitmap_shader ); )
82 DEF_BENCH( return new ShaderMFBench("picture", true , make_picture_shader); )
83 DEF_BENCH( return new ShaderMFBench("picture", false, make_picture_shader); )
84