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) { |
24 fName.printf("picture_nesting_%s_%d", name, this->countPics()); | |
25 } | |
22 | 26 |
27 protected: | |
28 virtual void onPreDraw() SK_OVERRIDE { | |
mtklein
2014/11/13 16:15:04
Moving this part to onPreDraw() might be overkill.
Kimmo Kinnunen
2014/11/18 12:42:19
Done.
| |
23 fPaint.setColor(SK_ColorRED); | 29 fPaint.setColor(SK_ColorRED); |
24 fPaint.setAntiAlias(true); | 30 fPaint.setAntiAlias(true); |
25 fPaint.setStyle(SkPaint::kStroke_Style); | 31 fPaint.setStyle(SkPaint::kStroke_Style); |
26 SkAutoTUnref<SkCanvas> nullCanvas(SkCreateNullCanvas()); | |
27 fName.printf("picture_nesting_%s_%d", name, this->sierpinsky(nullCanvas, 0, fPaint)); | |
28 } | 32 } |
29 | 33 |
30 protected: | |
31 virtual const char* onGetName() SK_OVERRIDE { | 34 virtual const char* onGetName() SK_OVERRIDE { |
32 return fName.c_str(); | 35 return fName.c_str(); |
33 } | 36 } |
34 | 37 |
35 void doDraw(SkCanvas* canvas) { | 38 void doDraw(SkCanvas* canvas) { |
36 SkIPoint canvasSize = onGetSize(); | 39 SkIPoint canvasSize = onGetSize(); |
37 canvas->save(); | 40 canvas->save(); |
38 canvas->scale(SkIntToScalar(canvasSize.x()), SkIntToScalar(canvasSize.y( ))); | 41 canvas->scale(SkIntToScalar(canvasSize.x()), SkIntToScalar(canvasSize.y( ))); |
39 | 42 |
40 this->sierpinsky(canvas, 0, fPaint); | 43 SkDEBUGCODE(int pics = ) this->sierpinsky(canvas, 0, fPaint); |
44 SkASSERT(pics == this->countPics()); | |
41 | 45 |
42 canvas->restore(); | 46 canvas->restore(); |
43 } | 47 } |
44 | 48 |
45 int sierpinsky(SkCanvas* canvas, int lvl, const SkPaint& paint) { | 49 int sierpinsky(SkCanvas* canvas, int lvl, const SkPaint& paint) { |
46 if (++lvl > fMaxLevel) { | 50 if (++lvl > fMaxLevel) { |
47 return 0; | 51 return 0; |
48 } | 52 } |
49 | 53 |
50 int pics = 0; | 54 int pics = 0; |
(...skipping 28 matching lines...) Expand all Loading... | |
79 canvas->drawPicture(picture); | 83 canvas->drawPicture(picture); |
80 } | 84 } |
81 | 85 |
82 return pics; | 86 return pics; |
83 } | 87 } |
84 | 88 |
85 int fMaxLevel; | 89 int fMaxLevel; |
86 int fMaxPictureLevel; | 90 int fMaxPictureLevel; |
87 | 91 |
88 private: | 92 private: |
93 int countPics() const { | |
94 // Solve: pics from sierpinsky | |
95 // f(m) = 1 + 3*f(m - 1) | |
96 // f(0) = 0 | |
97 // via "recursive function to closed form" tricks | |
98 // f(m) = 1/2 (3^m - 1) | |
99 return (pow(3, fMaxPictureLevel) - 1) / 2; | |
mtklein
2014/11/13 16:15:04
You may need some explicit casts in here. Our com
Kimmo Kinnunen
2014/11/18 12:42:19
Done.
| |
100 } | |
101 | |
89 SkString fName; | 102 SkString fName; |
90 SkPaint fPaint; | 103 SkPaint fPaint; |
91 | 104 |
92 typedef Benchmark INHERITED; | 105 typedef Benchmark INHERITED; |
93 }; | 106 }; |
94 | 107 |
95 class PictureNestingRecording : public PictureNesting { | 108 class PictureNestingRecording : public PictureNesting { |
96 public: | 109 public: |
97 PictureNestingRecording(int maxLevel, int maxPictureLevel) | 110 PictureNestingRecording(int maxLevel, int maxPictureLevel) |
98 : INHERITED("recording", maxLevel, maxPictureLevel) { | 111 : INHERITED("recording", maxLevel, maxPictureLevel) { |
(...skipping 17 matching lines...) Expand all Loading... | |
116 } | 129 } |
117 | 130 |
118 private: | 131 private: |
119 typedef PictureNesting INHERITED; | 132 typedef PictureNesting INHERITED; |
120 }; | 133 }; |
121 | 134 |
122 class PictureNestingPlayback : public PictureNesting { | 135 class PictureNestingPlayback : public PictureNesting { |
123 public: | 136 public: |
124 PictureNestingPlayback(int maxLevel, int maxPictureLevel) | 137 PictureNestingPlayback(int maxLevel, int maxPictureLevel) |
125 : INHERITED("playback", maxLevel, maxPictureLevel) { | 138 : INHERITED("playback", maxLevel, maxPictureLevel) { |
139 } | |
140 protected: | |
141 virtual void onPreDraw() SK_OVERRIDE { | |
142 this->INHERITED::onPreDraw(); | |
126 | 143 |
127 SkIPoint canvasSize = onGetSize(); | 144 SkIPoint canvasSize = onGetSize(); |
128 SkPictureRecorder recorder; | 145 SkPictureRecorder recorder; |
129 SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.x()), | 146 SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.x()), |
130 SkIntToScalar(canvasSize.y())); | 147 SkIntToScalar(canvasSize.y())); |
131 | 148 |
132 this->doDraw(c); | 149 this->doDraw(c); |
133 fPicture.reset(recorder.endRecording()); | 150 fPicture.reset(recorder.endRecording()); |
134 } | 151 } |
135 | 152 |
136 protected: | |
137 virtual void onDraw(const int loops, SkCanvas* canvas) { | 153 virtual void onDraw(const int loops, SkCanvas* canvas) { |
138 for (int i = 0; i < loops; i++) { | 154 for (int i = 0; i < loops; i++) { |
139 canvas->drawPicture(fPicture); | 155 canvas->drawPicture(fPicture); |
140 } | 156 } |
141 } | 157 } |
142 | 158 |
143 private: | 159 private: |
144 SkAutoTUnref<SkPicture> fPicture; | 160 SkAutoTUnref<SkPicture> fPicture; |
145 | 161 |
146 typedef PictureNesting INHERITED; | 162 typedef PictureNesting INHERITED; |
(...skipping 11 matching lines...) Expand all Loading... | |
158 | 174 |
159 DEF_BENCH( return new PictureNestingPlayback(8, 0); ) | 175 DEF_BENCH( return new PictureNestingPlayback(8, 0); ) |
160 DEF_BENCH( return new PictureNestingPlayback(8, 1); ) | 176 DEF_BENCH( return new PictureNestingPlayback(8, 1); ) |
161 DEF_BENCH( return new PictureNestingPlayback(8, 2); ) | 177 DEF_BENCH( return new PictureNestingPlayback(8, 2); ) |
162 DEF_BENCH( return new PictureNestingPlayback(8, 3); ) | 178 DEF_BENCH( return new PictureNestingPlayback(8, 3); ) |
163 DEF_BENCH( return new PictureNestingPlayback(8, 4); ) | 179 DEF_BENCH( return new PictureNestingPlayback(8, 4); ) |
164 DEF_BENCH( return new PictureNestingPlayback(8, 5); ) | 180 DEF_BENCH( return new PictureNestingPlayback(8, 5); ) |
165 DEF_BENCH( return new PictureNestingPlayback(8, 6); ) | 181 DEF_BENCH( return new PictureNestingPlayback(8, 6); ) |
166 DEF_BENCH( return new PictureNestingPlayback(8, 7); ) | 182 DEF_BENCH( return new PictureNestingPlayback(8, 7); ) |
167 DEF_BENCH( return new PictureNestingPlayback(8, 8); ) | 183 DEF_BENCH( return new PictureNestingPlayback(8, 8); ) |
OLD | NEW |