OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkColor.h" | 10 #include "SkColor.h" |
11 #include "SkNullCanvas.h" | 11 #include "SkNullCanvas.h" |
12 #include "SkPaint.h" | 12 #include "SkPaint.h" |
13 #include "SkPicture.h" | 13 #include "SkPicture.h" |
14 #include "SkPictureRecorder.h" | 14 #include "SkPictureRecorder.h" |
15 #include "SkString.h" | 15 #include "SkString.h" |
16 | 16 |
| 17 #include <math.h> |
| 18 |
17 class PictureNesting : public Benchmark { | 19 class PictureNesting : public Benchmark { |
18 public: | 20 public: |
19 PictureNesting(const char* name, int maxLevel, int maxPictureLevel) | 21 PictureNesting(const char* name, int maxLevel, int maxPictureLevel) |
20 : fMaxLevel(maxLevel) | 22 : fMaxLevel(maxLevel) |
21 , fMaxPictureLevel(maxPictureLevel) { | 23 , fMaxPictureLevel(maxPictureLevel) { |
22 | 24 fName.printf("picture_nesting_%s_%d", name, this->countPics()); |
23 fPaint.setColor(SK_ColorRED); | 25 fPaint.setColor(SK_ColorRED); |
24 fPaint.setAntiAlias(true); | 26 fPaint.setAntiAlias(true); |
25 fPaint.setStyle(SkPaint::kStroke_Style); | 27 fPaint.setStyle(SkPaint::kStroke_Style); |
26 SkAutoTUnref<SkCanvas> nullCanvas(SkCreateNullCanvas()); | |
27 fName.printf("picture_nesting_%s_%d", name, this->sierpinsky(nullCanvas,
0, fPaint)); | |
28 } | 28 } |
29 | 29 |
30 protected: | 30 protected: |
31 virtual const char* onGetName() SK_OVERRIDE { | 31 virtual const char* onGetName() SK_OVERRIDE { |
32 return fName.c_str(); | 32 return fName.c_str(); |
33 } | 33 } |
34 | 34 |
35 void doDraw(SkCanvas* canvas) { | 35 void doDraw(SkCanvas* canvas) { |
36 SkIPoint canvasSize = onGetSize(); | 36 SkIPoint canvasSize = onGetSize(); |
37 canvas->save(); | 37 canvas->save(); |
38 canvas->scale(SkIntToScalar(canvasSize.x()), SkIntToScalar(canvasSize.y(
))); | 38 canvas->scale(SkIntToScalar(canvasSize.x()), SkIntToScalar(canvasSize.y(
))); |
39 | 39 |
40 this->sierpinsky(canvas, 0, fPaint); | 40 SkDEBUGCODE(int pics = ) this->sierpinsky(canvas, 0, fPaint); |
| 41 SkASSERT(pics == this->countPics()); |
41 | 42 |
42 canvas->restore(); | 43 canvas->restore(); |
43 } | 44 } |
44 | 45 |
45 int sierpinsky(SkCanvas* canvas, int lvl, const SkPaint& paint) { | 46 int sierpinsky(SkCanvas* canvas, int lvl, const SkPaint& paint) { |
46 if (++lvl > fMaxLevel) { | 47 if (++lvl > fMaxLevel) { |
47 return 0; | 48 return 0; |
48 } | 49 } |
49 | 50 |
50 int pics = 0; | 51 int pics = 0; |
(...skipping 28 matching lines...) Expand all Loading... |
79 canvas->drawPicture(picture); | 80 canvas->drawPicture(picture); |
80 } | 81 } |
81 | 82 |
82 return pics; | 83 return pics; |
83 } | 84 } |
84 | 85 |
85 int fMaxLevel; | 86 int fMaxLevel; |
86 int fMaxPictureLevel; | 87 int fMaxPictureLevel; |
87 | 88 |
88 private: | 89 private: |
| 90 int countPics() const { |
| 91 // Solve: pics from sierpinsky |
| 92 // f(m) = 1 + 3*f(m - 1) |
| 93 // f(0) = 0 |
| 94 // via "recursive function to closed form" tricks |
| 95 // f(m) = 1/2 (3^m - 1) |
| 96 return static_cast<int>((pow(3.0, fMaxPictureLevel) - 1.0) / 2.0); |
| 97 } |
| 98 |
89 SkString fName; | 99 SkString fName; |
90 SkPaint fPaint; | 100 SkPaint fPaint; |
91 | 101 |
92 typedef Benchmark INHERITED; | 102 typedef Benchmark INHERITED; |
93 }; | 103 }; |
94 | 104 |
95 class PictureNestingRecording : public PictureNesting { | 105 class PictureNestingRecording : public PictureNesting { |
96 public: | 106 public: |
97 PictureNestingRecording(int maxLevel, int maxPictureLevel) | 107 PictureNestingRecording(int maxLevel, int maxPictureLevel) |
98 : INHERITED("recording", maxLevel, maxPictureLevel) { | 108 : INHERITED("recording", maxLevel, maxPictureLevel) { |
(...skipping 17 matching lines...) Expand all Loading... |
116 } | 126 } |
117 | 127 |
118 private: | 128 private: |
119 typedef PictureNesting INHERITED; | 129 typedef PictureNesting INHERITED; |
120 }; | 130 }; |
121 | 131 |
122 class PictureNestingPlayback : public PictureNesting { | 132 class PictureNestingPlayback : public PictureNesting { |
123 public: | 133 public: |
124 PictureNestingPlayback(int maxLevel, int maxPictureLevel) | 134 PictureNestingPlayback(int maxLevel, int maxPictureLevel) |
125 : INHERITED("playback", maxLevel, maxPictureLevel) { | 135 : INHERITED("playback", maxLevel, maxPictureLevel) { |
| 136 } |
| 137 protected: |
| 138 virtual void onPreDraw() SK_OVERRIDE { |
| 139 this->INHERITED::onPreDraw(); |
126 | 140 |
127 SkIPoint canvasSize = onGetSize(); | 141 SkIPoint canvasSize = onGetSize(); |
128 SkPictureRecorder recorder; | 142 SkPictureRecorder recorder; |
129 SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.x()), | 143 SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.x()), |
130 SkIntToScalar(canvasSize.y())); | 144 SkIntToScalar(canvasSize.y())); |
131 | 145 |
132 this->doDraw(c); | 146 this->doDraw(c); |
133 fPicture.reset(recorder.endRecording()); | 147 fPicture.reset(recorder.endRecording()); |
134 } | 148 } |
135 | 149 |
136 protected: | |
137 virtual void onDraw(const int loops, SkCanvas* canvas) { | 150 virtual void onDraw(const int loops, SkCanvas* canvas) { |
138 for (int i = 0; i < loops; i++) { | 151 for (int i = 0; i < loops; i++) { |
139 canvas->drawPicture(fPicture); | 152 canvas->drawPicture(fPicture); |
140 } | 153 } |
141 } | 154 } |
142 | 155 |
143 private: | 156 private: |
144 SkAutoTUnref<SkPicture> fPicture; | 157 SkAutoTUnref<SkPicture> fPicture; |
145 | 158 |
146 typedef PictureNesting INHERITED; | 159 typedef PictureNesting INHERITED; |
(...skipping 11 matching lines...) Expand all Loading... |
158 | 171 |
159 DEF_BENCH( return new PictureNestingPlayback(8, 0); ) | 172 DEF_BENCH( return new PictureNestingPlayback(8, 0); ) |
160 DEF_BENCH( return new PictureNestingPlayback(8, 1); ) | 173 DEF_BENCH( return new PictureNestingPlayback(8, 1); ) |
161 DEF_BENCH( return new PictureNestingPlayback(8, 2); ) | 174 DEF_BENCH( return new PictureNestingPlayback(8, 2); ) |
162 DEF_BENCH( return new PictureNestingPlayback(8, 3); ) | 175 DEF_BENCH( return new PictureNestingPlayback(8, 3); ) |
163 DEF_BENCH( return new PictureNestingPlayback(8, 4); ) | 176 DEF_BENCH( return new PictureNestingPlayback(8, 4); ) |
164 DEF_BENCH( return new PictureNestingPlayback(8, 5); ) | 177 DEF_BENCH( return new PictureNestingPlayback(8, 5); ) |
165 DEF_BENCH( return new PictureNestingPlayback(8, 6); ) | 178 DEF_BENCH( return new PictureNestingPlayback(8, 6); ) |
166 DEF_BENCH( return new PictureNestingPlayback(8, 7); ) | 179 DEF_BENCH( return new PictureNestingPlayback(8, 7); ) |
167 DEF_BENCH( return new PictureNestingPlayback(8, 8); ) | 180 DEF_BENCH( return new PictureNestingPlayback(8, 8); ) |
OLD | NEW |