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 #include "Benchmark.h" | 7 #include "Benchmark.h" |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
11 #include "SkGradientShader.h" | 11 #include "SkGradientShader.h" |
12 #include "SkPaint.h" | 12 #include "SkPaint.h" |
13 #include "SkPatchUtils.h" | 13 #include "SkPatchUtils.h" |
14 #include "SkRandom.h" | 14 #include "SkRandom.h" |
15 #include "SkShader.h" | 15 #include "SkShader.h" |
16 #include "SkString.h" | 16 #include "SkString.h" |
17 #include "SkTArray.h" | 17 #include "SkTArray.h" |
18 | 18 |
19 class PatchBench : public Benchmark { | 19 class PatchBench : public Benchmark { |
20 | 20 |
21 public: | 21 public: |
22 | 22 |
23 enum VertexMode { | 23 enum VertexMode { |
24 kNone_VertexMode, | 24 kNone_VertexMode, |
25 kColors_VertexMode, | 25 kColors_VertexMode, |
26 kTexCoords_VertexMode, | 26 kTexCoords_VertexMode, |
27 kBoth_VertexMode | 27 kBoth_VertexMode |
28 }; | 28 }; |
29 | 29 |
30 PatchBench(SkPoint scale, VertexMode vertexMode) | 30 PatchBench(SkPoint scale, VertexMode vertexMode) |
31 : fScale(scale) | 31 : fScale(scale) |
32 , fVertexMode(vertexMode) { } | 32 , fVertexMode(vertexMode) { } |
33 | 33 |
34 // to add name of specific class override this method | 34 // to add name of specific class override this method |
35 virtual void appendName(SkString* name) { | 35 virtual void appendName(SkString* name) { |
36 name->append("normal"); | 36 name->append("normal"); |
37 } | 37 } |
38 | 38 |
39 // to make other type of patches override this method | 39 // to make other type of patches override this method |
40 virtual void setCubics() { | 40 virtual void setCubics() { |
41 const SkPoint points[SkPatchUtils::kNumCtrlPts] = { | 41 const SkPoint points[SkPatchUtils::kNumCtrlPts] = { |
42 //top points | 42 //top points |
43 {100,100},{150,50},{250,150}, {300,100}, | 43 {100,100},{150,50},{250,150}, {300,100}, |
44 //right points | 44 //right points |
45 {350, 150},{250,200}, | 45 {350, 150},{250,200}, |
46 //bottom points | 46 //bottom points |
47 {300,300},{250,250},{150,350},{100,300}, | 47 {300,300},{250,250},{150,350},{100,300}, |
48 //left points | 48 //left points |
49 {50,250},{150,50} | 49 {50,250},{150,50} |
50 }; | 50 }; |
51 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); | 51 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); |
52 } | 52 } |
53 | 53 |
54 virtual void setColors() { | 54 virtual void setColors() { |
55 const SkColor colors[SkPatchUtils::kNumCorners] = { | 55 const SkColor colors[SkPatchUtils::kNumCorners] = { |
56 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN | 56 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorCYAN |
57 }; | 57 }; |
58 memcpy(fColors, colors, SkPatchUtils::kNumCorners * sizeof(SkColor)); | 58 memcpy(fColors, colors, SkPatchUtils::kNumCorners * sizeof(SkColor)); |
59 } | 59 } |
60 | 60 |
61 virtual void setTexCoords() { | 61 virtual void setTexCoords() { |
62 const SkPoint texCoords[SkPatchUtils::kNumCorners] = { | 62 const SkPoint texCoords[SkPatchUtils::kNumCorners] = { |
63 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f,1.0f}, {0.0f, 1.0f} | 63 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f,1.0f}, {0.0f, 1.0f} |
64 }; | 64 }; |
65 memcpy(fTexCoords, texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint )); | 65 memcpy(fTexCoords, texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint )); |
66 } | 66 } |
67 | 67 |
68 // override this method to change the shader | 68 // override this method to change the shader |
69 virtual SkShader* getShader() { | 69 virtual SkShader* createShader() { |
70 const SkColor colors[] = { | 70 const SkColor colors[] = { |
71 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, | 71 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, |
72 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, | 72 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, |
73 }; | 73 }; |
74 const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } }; | 74 const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } }; |
75 | 75 |
76 return SkGradientShader::CreateLinear(pts, colors, NULL, | 76 return SkGradientShader::CreateLinear(pts, colors, NULL, |
77 SK_ARRAY_COUNT(colors), | 77 SK_ARRAY_COUNT(colors), |
78 SkShader::kMirror_TileMode); | 78 SkShader::kMirror_TileMode); |
79 } | 79 } |
80 | 80 |
81 protected: | 81 protected: |
82 virtual const char* onGetName() SK_OVERRIDE { | 82 virtual const char* onGetName() SK_OVERRIDE { |
83 SkString vertexMode; | 83 SkString vertexMode; |
84 switch (fVertexMode) { | 84 switch (fVertexMode) { |
85 case kNone_VertexMode: | 85 case kNone_VertexMode: |
(...skipping 10 matching lines...) Expand all Loading... | |
96 break; | 96 break; |
97 default: | 97 default: |
98 break; | 98 break; |
99 } | 99 } |
100 SkString type; | 100 SkString type; |
101 this->appendName(&type); | 101 this->appendName(&type); |
102 fName.printf("patch_%s_%s_[%f,%f]", type.c_str(), vertexMode.c_str(), | 102 fName.printf("patch_%s_%s_[%f,%f]", type.c_str(), vertexMode.c_str(), |
103 fScale.x(), fScale.y()); | 103 fScale.x(), fScale.y()); |
104 return fName.c_str(); | 104 return fName.c_str(); |
105 } | 105 } |
106 | 106 |
107 virtual void preDraw() { | 107 virtual void preDraw() { |
108 | 108 |
109 } | 109 } |
110 | 110 |
111 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 111 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
112 | 112 |
113 this->setCubics(); | 113 this->setCubics(); |
114 this->setColors(); | 114 this->setColors(); |
115 this->setTexCoords(); | 115 this->setTexCoords(); |
116 this->setupPaint(&fPaint); | 116 this->setupPaint(&fPaint); |
117 switch (fVertexMode) { | 117 switch (fVertexMode) { |
118 case kTexCoords_VertexMode: | 118 case kTexCoords_VertexMode: |
119 case kBoth_VertexMode: | 119 case kBoth_VertexMode: |
120 fPaint.setShader(getShader()); | 120 fPaint.setShader(this->createShader())->unref(); |
tfarina
2014/08/13 16:54:33
lgtm
| |
121 break; | 121 break; |
122 default: | 122 default: |
123 fPaint.setShader(NULL); | 123 fPaint.setShader(NULL); |
124 break; | 124 break; |
125 } | 125 } |
126 | 126 |
127 canvas->scale(fScale.x(), fScale.y()); | 127 canvas->scale(fScale.x(), fScale.y()); |
128 for (int i = 0; i < loops; i++) { | 128 for (int i = 0; i < loops; i++) { |
129 switch (fVertexMode) { | 129 switch (fVertexMode) { |
130 case kNone_VertexMode: | 130 case kNone_VertexMode: |
131 canvas->drawPatch(fCubics, NULL, NULL, NULL, fPaint); | 131 canvas->drawPatch(fCubics, NULL, NULL, NULL, fPaint); |
132 break; | 132 break; |
133 case kColors_VertexMode: | 133 case kColors_VertexMode: |
134 canvas->drawPatch(fCubics, fColors, NULL, NULL, fPaint); | 134 canvas->drawPatch(fCubics, fColors, NULL, NULL, fPaint); |
135 break; | 135 break; |
136 case kTexCoords_VertexMode: | 136 case kTexCoords_VertexMode: |
137 canvas->drawPatch(fCubics, NULL, fTexCoords, NULL, fPaint); | 137 canvas->drawPatch(fCubics, NULL, fTexCoords, NULL, fPaint); |
138 break; | 138 break; |
139 case kBoth_VertexMode: | 139 case kBoth_VertexMode: |
140 canvas->drawPatch(fCubics, fColors, fTexCoords, NULL, fPaint ); | 140 canvas->drawPatch(fCubics, fColors, fTexCoords, NULL, fPaint ); |
141 break; | 141 break; |
142 default: | 142 default: |
143 break; | 143 break; |
144 } | 144 } |
145 } | 145 } |
146 } | 146 } |
147 | 147 |
148 SkPaint fPaint; | 148 SkPaint fPaint; |
149 SkString fName; | 149 SkString fName; |
150 SkVector fScale; | 150 SkVector fScale; |
151 SkPoint fCubics[12]; | 151 SkPoint fCubics[12]; |
152 SkPoint fTexCoords[4]; | 152 SkPoint fTexCoords[4]; |
153 SkColor fColors[4]; | 153 SkColor fColors[4]; |
154 VertexMode fVertexMode; | 154 VertexMode fVertexMode; |
155 | 155 |
156 typedef Benchmark INHERITED; | 156 typedef Benchmark INHERITED; |
157 }; | 157 }; |
158 | 158 |
159 class SquarePatchBench : public PatchBench { | 159 class SquarePatchBench : public PatchBench { |
160 public: | 160 public: |
161 SquarePatchBench(SkPoint scale, VertexMode vertexMode) | 161 SquarePatchBench(SkPoint scale, VertexMode vertexMode) |
162 : INHERITED(scale, vertexMode) { } | 162 : INHERITED(scale, vertexMode) { } |
163 | 163 |
164 virtual void appendName(SkString* name) SK_OVERRIDE { | 164 virtual void appendName(SkString* name) SK_OVERRIDE { |
165 name->append("square"); | 165 name->append("square"); |
166 } | 166 } |
167 | 167 |
168 virtual void setCubics() { | 168 virtual void setCubics() { |
169 const SkPoint points[SkPatchUtils::kNumCtrlPts] = { | 169 const SkPoint points[SkPatchUtils::kNumCtrlPts] = { |
170 //top points | 170 //top points |
171 {100,100},{150,100},{250,100}, {300,100}, | 171 {100,100},{150,100},{250,100}, {300,100}, |
172 //right points | 172 //right points |
173 {300, 150},{300,250}, | 173 {300, 150},{300,250}, |
174 //bottom points | 174 //bottom points |
175 {300,300},{250,300},{150,300},{100,300}, | 175 {300,300},{250,300},{150,300},{100,300}, |
176 //left points | 176 //left points |
177 {100,250},{100,150} | 177 {100,250},{100,150} |
178 }; | 178 }; |
179 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); | 179 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); |
180 } | 180 } |
181 private: | 181 private: |
182 typedef PatchBench INHERITED; | 182 typedef PatchBench INHERITED; |
183 }; | 183 }; |
184 | 184 |
185 class LODDiffPatchBench : public PatchBench { | 185 class LODDiffPatchBench : public PatchBench { |
186 public: | 186 public: |
187 LODDiffPatchBench(SkPoint scale, VertexMode vertexMode) | 187 LODDiffPatchBench(SkPoint scale, VertexMode vertexMode) |
188 : INHERITED(scale, vertexMode) { } | 188 : INHERITED(scale, vertexMode) { } |
189 | 189 |
190 virtual void appendName(SkString* name) SK_OVERRIDE { | 190 virtual void appendName(SkString* name) SK_OVERRIDE { |
191 name->append("LOD_Diff"); | 191 name->append("LOD_Diff"); |
192 } | 192 } |
193 | 193 |
194 virtual void setCubics() { | 194 virtual void setCubics() { |
195 const SkPoint points[SkPatchUtils::kNumCtrlPts] = { | 195 const SkPoint points[SkPatchUtils::kNumCtrlPts] = { |
196 //top points | 196 //top points |
197 {100,175},{150,100},{250,100}, {300,0}, | 197 {100,175},{150,100},{250,100}, {300,0}, |
198 //right points | 198 //right points |
199 {300, 150},{300,250}, | 199 {300, 150},{300,250}, |
200 //bottom points | 200 //bottom points |
201 {300,400},{250,300},{150,300},{100,225}, | 201 {300,400},{250,300},{150,300},{100,225}, |
202 //left points | 202 //left points |
203 {100,215},{100,185} | 203 {100,215},{100,185} |
204 }; | 204 }; |
205 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); | 205 memcpy(fCubics, points, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint)); |
206 } | 206 } |
207 private: | 207 private: |
208 typedef PatchBench INHERITED; | 208 typedef PatchBench INHERITED; |
209 }; | 209 }; |
210 | 210 |
211 class LoopPatchBench : public PatchBench { | 211 class LoopPatchBench : public PatchBench { |
212 public: | 212 public: |
213 LoopPatchBench(SkPoint scale, VertexMode vertexMode) | 213 LoopPatchBench(SkPoint scale, VertexMode vertexMode) |
214 : INHERITED(scale, vertexMode) { } | 214 : INHERITED(scale, vertexMode) { } |
215 | 215 |
216 virtual void appendName(SkString* name) SK_OVERRIDE { | 216 virtual void appendName(SkString* name) SK_OVERRIDE { |
217 name->append("loop"); | 217 name->append("loop"); |
218 } | 218 } |
219 | 219 |
220 virtual void setCubics() { | 220 virtual void setCubics() { |
221 const SkPoint points[SkPatchUtils::kNumCtrlPts] = { | 221 const SkPoint points[SkPatchUtils::kNumCtrlPts] = { |
222 //top points | 222 //top points |
223 {100,100},{300,200},{100,200}, {300,100}, | 223 {100,100},{300,200},{100,200}, {300,100}, |
224 //right points | 224 //right points |
225 {380, 400},{380,0}, | 225 {380, 400},{380,0}, |
226 //bottom points | 226 //bottom points |
227 {300,300},{250,250},{30,200},{100,300}, | 227 {300,300},{250,250},{30,200},{100,300}, |
228 //left points | 228 //left points |
229 {140,325},{150,150} | 229 {140,325},{150,150} |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.0f, 1.0f), | 316 DEF_BENCH( return new LoopPatchBench(SkVector::Make(1.0f, 1.0f), |
317 PatchBench::kBoth_VertexMode); ) | 317 PatchBench::kBoth_VertexMode); ) |
318 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f), | 318 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f), |
319 PatchBench::kNone_VertexMode); ) | 319 PatchBench::kNone_VertexMode); ) |
320 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f), | 320 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f), |
321 PatchBench::kColors_VertexMode); ) | 321 PatchBench::kColors_VertexMode); ) |
322 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f), | 322 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f), |
323 PatchBench::kTexCoords_VertexMode); ) | 323 PatchBench::kTexCoords_VertexMode); ) |
324 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f), | 324 DEF_BENCH( return new LoopPatchBench(SkVector::Make(3.0f, 3.0f), |
325 PatchBench::kBoth_VertexMode); ) | 325 PatchBench::kBoth_VertexMode); ) |
OLD | NEW |