fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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/SkColor.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkPicture.h" |
| 13 | #include "include/core/SkPictureRecorder.h" |
| 14 | #include "include/core/SkString.h" |
| 15 | #include "include/utils/SkNullCanvas.h" |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 16 | |
| 17 | class PictureNesting : public Benchmark { |
| 18 | public: |
| 19 | PictureNesting(const char* name, int maxLevel, int maxPictureLevel) |
| 20 | : fMaxLevel(maxLevel) |
| 21 | , fMaxPictureLevel(maxPictureLevel) { |
kkinnunen | b33402b | 2014-11-18 04:50:50 -0800 | [diff] [blame] | 22 | fName.printf("picture_nesting_%s_%d", name, this->countPics()); |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 23 | fPaint.setColor(SK_ColorRED); |
| 24 | fPaint.setAntiAlias(true); |
| 25 | fPaint.setStyle(SkPaint::kStroke_Style); |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | const char* onGetName() override { |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 30 | return fName.c_str(); |
| 31 | } |
| 32 | |
| 33 | void doDraw(SkCanvas* canvas) { |
| 34 | SkIPoint canvasSize = onGetSize(); |
| 35 | canvas->save(); |
| 36 | canvas->scale(SkIntToScalar(canvasSize.x()), SkIntToScalar(canvasSize.y())); |
| 37 | |
kkinnunen | b33402b | 2014-11-18 04:50:50 -0800 | [diff] [blame] | 38 | SkDEBUGCODE(int pics = ) this->sierpinsky(canvas, 0, fPaint); |
| 39 | SkASSERT(pics == this->countPics()); |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 40 | |
| 41 | canvas->restore(); |
| 42 | } |
| 43 | |
| 44 | int sierpinsky(SkCanvas* canvas, int lvl, const SkPaint& paint) { |
| 45 | if (++lvl > fMaxLevel) { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | int pics = 0; |
| 50 | bool recordPicture = lvl <= fMaxPictureLevel; |
| 51 | SkPictureRecorder recorder; |
| 52 | SkCanvas* c = canvas; |
| 53 | |
| 54 | if (recordPicture) { |
| 55 | c = recorder.beginRecording(1, 1); |
| 56 | pics++; |
| 57 | } |
| 58 | |
| 59 | c->drawLine(0.5, 0, 0, 1, paint); |
| 60 | c->drawLine(0.5, 0, 1, 1, paint); |
| 61 | c->drawLine(0, 1, 1, 1, paint); |
| 62 | |
| 63 | c->save(); |
| 64 | c->scale(0.5, 0.5); |
| 65 | |
| 66 | c->translate(0, 1); |
| 67 | pics += this->sierpinsky(c, lvl, paint); |
| 68 | |
| 69 | c->translate(1, 0); |
| 70 | pics += this->sierpinsky(c, lvl, paint); |
| 71 | |
| 72 | c->translate(-0.5, -1); |
| 73 | pics += this->sierpinsky(c, lvl, paint); |
| 74 | c->restore(); |
| 75 | |
| 76 | if (recordPicture) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 77 | canvas->drawPicture(recorder.finishRecordingAsPicture()); |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return pics; |
| 81 | } |
| 82 | |
| 83 | int fMaxLevel; |
| 84 | int fMaxPictureLevel; |
| 85 | |
| 86 | private: |
kkinnunen | b33402b | 2014-11-18 04:50:50 -0800 | [diff] [blame] | 87 | int countPics() const { |
| 88 | // Solve: pics from sierpinsky |
| 89 | // f(m) = 1 + 3*f(m - 1) |
| 90 | // f(0) = 0 |
| 91 | // via "recursive function to closed form" tricks |
| 92 | // f(m) = 1/2 (3^m - 1) |
mtklein | d0256a2 | 2015-01-09 08:33:36 -0800 | [diff] [blame] | 93 | int pics = 1; |
| 94 | for (int i = 0; i < fMaxPictureLevel; i++) { |
| 95 | pics *= 3; |
| 96 | } |
| 97 | pics--; |
| 98 | pics /= 2; |
| 99 | return pics; |
kkinnunen | b33402b | 2014-11-18 04:50:50 -0800 | [diff] [blame] | 100 | } |
| 101 | |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 102 | SkString fName; |
| 103 | SkPaint fPaint; |
| 104 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 105 | using INHERITED = Benchmark; |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | class PictureNestingRecording : public PictureNesting { |
| 109 | public: |
| 110 | PictureNestingRecording(int maxLevel, int maxPictureLevel) |
| 111 | : INHERITED("recording", maxLevel, maxPictureLevel) { |
| 112 | } |
| 113 | |
| 114 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 115 | bool isSuitableFor(Backend backend) override { |
mtklein | e1daac9 | 2014-09-16 10:12:42 -0700 | [diff] [blame] | 116 | return backend == kNonRendering_Backend; |
| 117 | } |
| 118 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 119 | void onDraw(int loops, SkCanvas*) override { |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 120 | SkIPoint canvasSize = onGetSize(); |
| 121 | SkPictureRecorder recorder; |
| 122 | |
| 123 | for (int i = 0; i < loops; i++) { |
| 124 | SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.x()), |
| 125 | SkIntToScalar(canvasSize.y())); |
| 126 | this->doDraw(c); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 127 | (void)recorder.finishRecordingAsPicture(); |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 132 | using INHERITED = PictureNesting; |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class PictureNestingPlayback : public PictureNesting { |
| 136 | public: |
| 137 | PictureNestingPlayback(int maxLevel, int maxPictureLevel) |
| 138 | : INHERITED("playback", maxLevel, maxPictureLevel) { |
kkinnunen | b33402b | 2014-11-18 04:50:50 -0800 | [diff] [blame] | 139 | } |
| 140 | protected: |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 141 | void onDelayedSetup() override { |
| 142 | this->INHERITED::onDelayedSetup(); |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 143 | |
| 144 | SkIPoint canvasSize = onGetSize(); |
| 145 | SkPictureRecorder recorder; |
| 146 | SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.x()), |
| 147 | SkIntToScalar(canvasSize.y())); |
| 148 | |
| 149 | this->doDraw(c); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 150 | fPicture = recorder.finishRecordingAsPicture(); |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 151 | } |
| 152 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 153 | void onDraw(int loops, SkCanvas* canvas) override { |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 154 | for (int i = 0; i < loops; i++) { |
| 155 | canvas->drawPicture(fPicture); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private: |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 160 | sk_sp<SkPicture> fPicture; |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 161 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 162 | using INHERITED = PictureNesting; |
fmalita | 4416267 | 2014-09-15 16:46:16 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | DEF_BENCH( return new PictureNestingRecording(8, 0); ) |
| 166 | DEF_BENCH( return new PictureNestingRecording(8, 1); ) |
| 167 | DEF_BENCH( return new PictureNestingRecording(8, 2); ) |
| 168 | DEF_BENCH( return new PictureNestingRecording(8, 3); ) |
| 169 | DEF_BENCH( return new PictureNestingRecording(8, 4); ) |
| 170 | DEF_BENCH( return new PictureNestingRecording(8, 5); ) |
| 171 | DEF_BENCH( return new PictureNestingRecording(8, 6); ) |
| 172 | DEF_BENCH( return new PictureNestingRecording(8, 7); ) |
| 173 | DEF_BENCH( return new PictureNestingRecording(8, 8); ) |
| 174 | |
| 175 | DEF_BENCH( return new PictureNestingPlayback(8, 0); ) |
| 176 | DEF_BENCH( return new PictureNestingPlayback(8, 1); ) |
| 177 | DEF_BENCH( return new PictureNestingPlayback(8, 2); ) |
| 178 | DEF_BENCH( return new PictureNestingPlayback(8, 3); ) |
| 179 | DEF_BENCH( return new PictureNestingPlayback(8, 4); ) |
| 180 | DEF_BENCH( return new PictureNestingPlayback(8, 5); ) |
| 181 | DEF_BENCH( return new PictureNestingPlayback(8, 6); ) |
| 182 | DEF_BENCH( return new PictureNestingPlayback(8, 7); ) |
| 183 | DEF_BENCH( return new PictureNestingPlayback(8, 8); ) |