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 }
26
make_picture_shader()27 static sk_sp<SkShader> make_picture_shader() {
28 SkPaint p;
29 p.setColor(SK_ColorBLACK);
30 p.setAntiAlias(true);
31
32 SkPictureRecorder recorder;
33 recorder.beginRecording(100, 100)->drawCircle(50, 50, 50, p);
34
35 return recorder.finishRecordingAsPicture()->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
36 }
37
38 class ShaderMFBench final : public Benchmark {
39
40 public:
41 using ShaderMaker = sk_sp<SkShader>(*)();
42
ShaderMFBench(const char * nm,bool opaque,const ShaderMaker & maker)43 ShaderMFBench(const char* nm, bool opaque, const ShaderMaker& maker) {
44 fMaskFilter = SkShaderMaskFilter::Make(maker());
45 fColor = opaque ? 0xff00ff00 : 0x8000ff00;
46 fName = SkStringPrintf("shadermaskfilter_%s_%x", nm, SkColorGetA(fColor));
47 }
48
49 protected:
onGetName()50 const char* onGetName() override {
51 return fName.c_str();
52 }
53
onDraw(int loops,SkCanvas * canvas)54 void onDraw(int loops, SkCanvas* canvas) override {
55 SkPaint maskPaint;
56 maskPaint.setMaskFilter(fMaskFilter);
57
58 for (int i = 0; i < loops; ++i) {
59 SkAutoCanvasRestore arc(canvas, false);
60 canvas->saveLayer(nullptr, &maskPaint);
61 canvas->drawColor(fColor);
62 }
63 }
64
65 private:
66 SkString fName;
67 sk_sp<SkMaskFilter> fMaskFilter;
68 SkColor fColor;
69
70 using INHERITED = Benchmark;
71 };
72
73 DEF_BENCH( return new ShaderMFBench("bitmap" , true , make_bitmap_shader ); )
74 DEF_BENCH( return new ShaderMFBench("bitmap" , false, make_bitmap_shader ); )
75 DEF_BENCH( return new ShaderMFBench("picture", true , make_picture_shader); )
76 DEF_BENCH( return new ShaderMFBench("picture", false, make_picture_shader); )
77