Florin Malita | 15a64e7 | 2018-04-18 11:14:50 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Florin Malita | 15a64e7 | 2018-04-18 11:14:50 -0400 | [diff] [blame] | 15 | |
| 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 | |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 24 | return surface->makeImageSnapshot()->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 25 | SkSamplingOptions()); |
Florin Malita | 15a64e7 | 2018-04-18 11:14:50 -0400 | [diff] [blame] | 26 | } |
| 27 | |
| 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 | |
Mike Reed | 10a5ff2 | 2021-03-18 17:18:58 -0400 | [diff] [blame] | 36 | return recorder.finishRecordingAsPicture()->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 37 | SkFilterMode::kNearest); |
Florin Malita | 15a64e7 | 2018-04-18 11:14:50 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | class ShaderMFBench final : public Benchmark { |
| 41 | |
| 42 | public: |
| 43 | using ShaderMaker = sk_sp<SkShader>(*)(); |
| 44 | |
Herb Derby | 12732e4 | 2021-09-23 14:35:56 -0400 | [diff] [blame] | 45 | ShaderMFBench(const char* nm, bool opaque, const ShaderMaker maker) |
| 46 | : fMaker{maker} |
| 47 | , fColor{opaque ? 0xff00ff00 : 0x8000ff00} { |
Florin Malita | 15a64e7 | 2018-04-18 11:14:50 -0400 | [diff] [blame] | 48 | fName = SkStringPrintf("shadermaskfilter_%s_%x", nm, SkColorGetA(fColor)); |
| 49 | } |
| 50 | |
| 51 | protected: |
| 52 | const char* onGetName() override { |
| 53 | return fName.c_str(); |
| 54 | } |
| 55 | |
Herb Derby | 12732e4 | 2021-09-23 14:35:56 -0400 | [diff] [blame] | 56 | void onDelayedSetup() override { |
| 57 | fMaskFilter = SkShaderMaskFilter::Make(fMaker()); |
| 58 | } |
| 59 | |
Florin Malita | 15a64e7 | 2018-04-18 11:14:50 -0400 | [diff] [blame] | 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: |
Herb Derby | 12732e4 | 2021-09-23 14:35:56 -0400 | [diff] [blame] | 72 | const ShaderMaker fMaker; |
| 73 | const SkColor fColor; |
| 74 | SkString fName; |
Florin Malita | 15a64e7 | 2018-04-18 11:14:50 -0400 | [diff] [blame] | 75 | sk_sp<SkMaskFilter> fMaskFilter; |
Florin Malita | 15a64e7 | 2018-04-18 11:14:50 -0400 | [diff] [blame] | 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); ) |