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 "Benchmark.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkPictureRecorder.h"
12 #include "SkPictureShader.h"
13 #include "SkShaderMaskFilter.h"
14 #include "SkSurface.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(SkShader::kRepeat_TileMode,
25 SkShader::kRepeat_TileMode);
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 SkPictureShader::Make(recorder.finishRecordingAsPicture(),
37 SkShader::kRepeat_TileMode,
38 SkShader::kRepeat_TileMode,
39 nullptr, nullptr);
40 }
41
42 class ShaderMFBench final : public Benchmark {
43
44 public:
45 using ShaderMaker = sk_sp<SkShader>(*)();
46
ShaderMFBench(const char * nm,bool opaque,const ShaderMaker & maker)47 ShaderMFBench(const char* nm, bool opaque, const ShaderMaker& maker) {
48 fMaskFilter = SkShaderMaskFilter::Make(maker());
49 fColor = opaque ? 0xff00ff00 : 0x8000ff00;
50 fName = SkStringPrintf("shadermaskfilter_%s_%x", nm, SkColorGetA(fColor));
51 }
52
53 protected:
onGetName()54 const char* onGetName() override {
55 return fName.c_str();
56 }
57
onDraw(int loops,SkCanvas * canvas)58 void onDraw(int loops, SkCanvas* canvas) override {
59 SkPaint maskPaint;
60 maskPaint.setMaskFilter(fMaskFilter);
61
62 for (int i = 0; i < loops; ++i) {
63 SkAutoCanvasRestore arc(canvas, false);
64 canvas->saveLayer(nullptr, &maskPaint);
65 canvas->drawColor(fColor);
66 }
67 }
68
69 private:
70 SkString fName;
71 sk_sp<SkMaskFilter> fMaskFilter;
72 SkColor fColor;
73
74 using INHERITED = Benchmark;
75 };
76
77 DEF_BENCH( return new ShaderMFBench("bitmap" , true , make_bitmap_shader ); )
78 DEF_BENCH( return new ShaderMFBench("bitmap" , false, make_bitmap_shader ); )
79 DEF_BENCH( return new ShaderMFBench("picture", true , make_picture_shader); )
80 DEF_BENCH( return new ShaderMFBench("picture", false, make_picture_shader); )
81