fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "bench/Benchmark.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Mike Reed | 839eef3 | 2020-12-23 11:18:24 -0500 | [diff] [blame] | 9 | #include "include/core/SkImage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkMatrix.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkString.h" |
Mike Reed | 839eef3 | 2020-12-23 11:18:24 -0500 | [diff] [blame] | 13 | #include "include/core/SkSurface.h" |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * This bench measures the rendering time of SkCanvas::drawBitmap with different anti-aliasing / |
| 17 | * matrix combinations. |
| 18 | */ |
| 19 | |
| 20 | class DrawBitmapAABench : public Benchmark { |
| 21 | public: |
| 22 | DrawBitmapAABench(bool doAA, const SkMatrix& matrix, const char name[]) |
| 23 | : fMatrix(matrix) |
| 24 | , fName("draw_bitmap_") { |
| 25 | |
| 26 | fPaint.setAntiAlias(doAA); |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 27 | fName.appendf("%s_%s", doAA ? "aa" : "noaa", name); |
| 28 | } |
| 29 | |
| 30 | protected: |
| 31 | const char* onGetName() override { |
| 32 | return fName.c_str(); |
| 33 | } |
| 34 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 35 | void onDelayedSetup() override { |
Mike Reed | 839eef3 | 2020-12-23 11:18:24 -0500 | [diff] [blame] | 36 | auto surf = SkSurface::MakeRasterN32Premul(200, 200); |
| 37 | surf->getCanvas()->clear(0xFF00FF00); |
| 38 | fImage = surf->makeImageSnapshot(); |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 39 | } |
| 40 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 41 | void onDraw(int loops, SkCanvas* canvas) override { |
Mike Reed | 5ec2238 | 2021-01-14 21:59:01 -0500 | [diff] [blame] | 42 | SkSamplingOptions sampling(SkFilterMode::kLinear); |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 43 | canvas->concat(fMatrix); |
| 44 | for (int i = 0; i < loops; i++) { |
Mike Reed | 839eef3 | 2020-12-23 11:18:24 -0500 | [diff] [blame] | 45 | canvas->drawImage(fImage.get(), 0, 0, sampling, &fPaint); |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | SkPaint fPaint; |
| 51 | SkMatrix fMatrix; |
| 52 | SkString fName; |
Mike Reed | 839eef3 | 2020-12-23 11:18:24 -0500 | [diff] [blame] | 53 | sk_sp<SkImage> fImage; |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 54 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 55 | using INHERITED = Benchmark; |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 58 | DEF_BENCH( return new DrawBitmapAABench(false, SkMatrix::I(), "ident"); ) |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 59 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 60 | DEF_BENCH( return new DrawBitmapAABench(false, SkMatrix::Scale(1.17f, 1.17f), "scale"); ) |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 61 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 62 | DEF_BENCH( return new DrawBitmapAABench(false, SkMatrix::Translate(17.5f, 17.5f), "translate"); ) |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 63 | |
| 64 | DEF_BENCH( |
| 65 | SkMatrix m; |
| 66 | m.reset(); |
| 67 | m.preRotate(15); |
| 68 | return new DrawBitmapAABench(false, m, "rotate"); |
| 69 | ) |
| 70 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 71 | DEF_BENCH( return new DrawBitmapAABench(true, SkMatrix::I(), "ident"); ) |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 72 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 73 | DEF_BENCH( return new DrawBitmapAABench(true, SkMatrix::Scale(1.17f, 1.17f), "scale"); ) |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 74 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 75 | DEF_BENCH( return new DrawBitmapAABench(true, SkMatrix::Translate(17.5f, 17.5f), "translate"); ) |
fmalita | f0ebdd7 | 2015-07-06 05:25:17 -0700 | [diff] [blame] | 76 | |
| 77 | DEF_BENCH( |
| 78 | SkMatrix m; |
| 79 | m.reset(); |
| 80 | m.preRotate(15); |
| 81 | return new DrawBitmapAABench(true, m, "rotate"); |
| 82 | ) |