1 /*
2  * Copyright 2013 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 #include "Benchmark.h"
8 #include "SkBitmapSource.h"
9 #include "SkCanvas.h"
10 #include "SkDevice.h"
11 #include "SkLightingImageFilter.h"
12 
13 #define FILTER_WIDTH_SMALL  SkIntToScalar(32)
14 #define FILTER_HEIGHT_SMALL SkIntToScalar(32)
15 #define FILTER_WIDTH_LARGE  SkIntToScalar(256)
16 #define FILTER_HEIGHT_LARGE SkIntToScalar(256)
17 
18 class LightingBaseBench : public Benchmark {
19 public:
LightingBaseBench(bool small)20     LightingBaseBench(bool small) : fIsSmall(small) { }
21 
22 protected:
draw(const int loops,SkCanvas * canvas,SkImageFilter * imageFilter) const23     void draw(const int loops, SkCanvas* canvas, SkImageFilter* imageFilter) const {
24         SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
25                               SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
26         SkPaint paint;
27         paint.setImageFilter(imageFilter)->unref();
28         for (int i = 0; i < loops; i++) {
29             canvas->drawRect(r, paint);
30         }
31     }
32 
getPointLocation()33     static SkPoint3 getPointLocation() {
34         static SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
35         return pointLocation;
36     }
37 
getDistantDirection()38     static SkPoint3 getDistantDirection() {
39         static SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
40         static SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
41         static SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad),
42                                                      SkScalarCos(elevationRad)),
43                                          SkScalarMul(SkScalarSin(azimuthRad),
44                                                      SkScalarCos(elevationRad)),
45                                          SkScalarSin(elevationRad));
46         return distantDirection;
47     }
48 
getSpotLocation()49     static SkPoint3 getSpotLocation() {
50         static SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
51         return spotLocation;
52     }
53 
getSpotTarget()54     static SkPoint3 getSpotTarget() {
55         static SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
56         return spotTarget;
57     }
58 
getSpotExponent()59     static SkScalar getSpotExponent() {
60         static SkScalar spotExponent = SK_Scalar1;
61         return spotExponent;
62     }
63 
getCutoffAngle()64     static SkScalar getCutoffAngle() {
65         static SkScalar cutoffAngle = SkIntToScalar(15);
66         return cutoffAngle;
67     }
68 
getKd()69     static SkScalar getKd() {
70         static SkScalar kd = SkIntToScalar(2);
71         return kd;
72     }
73 
getKs()74     static SkScalar getKs() {
75         static SkScalar ks = SkIntToScalar(1);
76         return ks;
77     }
78 
getShininess()79     static SkScalar getShininess() {
80         static SkScalar shininess = SkIntToScalar(8);
81         return shininess;
82     }
83 
getSurfaceScale()84     static SkScalar getSurfaceScale() {
85         static SkScalar surfaceScale = SkIntToScalar(1);
86         return surfaceScale;
87     }
88 
getWhite()89     static SkColor getWhite() {
90         static SkColor white(0xFFFFFFFF);
91         return white;
92     }
93 
94     bool fIsSmall;
95     typedef Benchmark INHERITED;
96 };
97 
98 class LightingPointLitDiffuseBench : public LightingBaseBench {
99 public:
LightingPointLitDiffuseBench(bool small)100     LightingPointLitDiffuseBench(bool small) : INHERITED(small) {
101     }
102 
103 protected:
onGetName()104     const char* onGetName() override {
105         return fIsSmall ? "lightingpointlitdiffuse_small" : "lightingpointlitdiffuse_large";
106     }
107 
onDraw(const int loops,SkCanvas * canvas)108     void onDraw(const int loops, SkCanvas* canvas) override {
109         draw(loops, canvas, SkLightingImageFilter::CreatePointLitDiffuse(getPointLocation(),
110                                                                          getWhite(),
111                                                                          getSurfaceScale(),
112                                                                          getKd()));
113     }
114 
115 private:
116     typedef LightingBaseBench INHERITED;
117 };
118 
119 class LightingDistantLitDiffuseBench : public LightingBaseBench {
120 public:
LightingDistantLitDiffuseBench(bool small)121     LightingDistantLitDiffuseBench(bool small) : INHERITED(small) {
122     }
123 
124 protected:
onGetName()125     const char* onGetName() override {
126         return fIsSmall ? "lightingdistantlitdiffuse_small" : "lightingdistantlitdiffuse_large";
127     }
128 
onDraw(const int loops,SkCanvas * canvas)129     void onDraw(const int loops, SkCanvas* canvas) override {
130         draw(loops, canvas, SkLightingImageFilter::CreateDistantLitDiffuse(getDistantDirection(),
131                                                                            getWhite(),
132                                                                            getSurfaceScale(),
133                                                                            getKd()));
134     }
135 
136 private:
137     typedef LightingBaseBench INHERITED;
138 };
139 
140 class LightingSpotLitDiffuseBench : public LightingBaseBench {
141 public:
LightingSpotLitDiffuseBench(bool small)142     LightingSpotLitDiffuseBench(bool small) : INHERITED(small) {
143     }
144 
145 protected:
onGetName()146     const char* onGetName() override {
147         return fIsSmall ? "lightingspotlitdiffuse_small" : "lightingspotlitdiffuse_large";
148     }
149 
onDraw(const int loops,SkCanvas * canvas)150     void onDraw(const int loops, SkCanvas* canvas) override {
151         draw(loops, canvas, SkLightingImageFilter::CreateSpotLitDiffuse(getSpotLocation(),
152                                                                         getSpotTarget(),
153                                                                         getSpotExponent(),
154                                                                         getCutoffAngle(),
155                                                                         getWhite(),
156                                                                         getSurfaceScale(),
157                                                                         getKd()));
158     }
159 
160 private:
161     typedef LightingBaseBench INHERITED;
162 };
163 
164 class LightingPointLitSpecularBench : public LightingBaseBench {
165 public:
LightingPointLitSpecularBench(bool small)166     LightingPointLitSpecularBench(bool small) : INHERITED(small) {
167     }
168 
169 protected:
onGetName()170     const char* onGetName() override {
171         return fIsSmall ? "lightingpointlitspecular_small" : "lightingpointlitspecular_large";
172     }
173 
onDraw(const int loops,SkCanvas * canvas)174     void onDraw(const int loops, SkCanvas* canvas) override {
175         draw(loops, canvas, SkLightingImageFilter::CreatePointLitSpecular(getPointLocation(),
176                                                                           getWhite(),
177                                                                           getSurfaceScale(),
178                                                                           getKs(),
179                                                                           getShininess()));
180     }
181 
182 private:
183     typedef LightingBaseBench INHERITED;
184 };
185 
186 class LightingDistantLitSpecularBench : public LightingBaseBench {
187 public:
LightingDistantLitSpecularBench(bool small)188     LightingDistantLitSpecularBench(bool small) : INHERITED(small) {
189     }
190 
191 protected:
onGetName()192     const char* onGetName() override {
193         return fIsSmall ? "lightingdistantlitspecular_small" : "lightingdistantlitspecular_large";
194     }
195 
onDraw(const int loops,SkCanvas * canvas)196     void onDraw(const int loops, SkCanvas* canvas) override {
197         draw(loops, canvas, SkLightingImageFilter::CreateDistantLitSpecular(getDistantDirection(),
198                                                                             getWhite(),
199                                                                             getSurfaceScale(),
200                                                                             getKs(),
201                                                                             getShininess()));
202     }
203 
204 private:
205     typedef LightingBaseBench INHERITED;
206 };
207 
208 class LightingSpotLitSpecularBench : public LightingBaseBench {
209 public:
LightingSpotLitSpecularBench(bool small)210     LightingSpotLitSpecularBench(bool small) : INHERITED(small) {
211     }
212 
213 protected:
onGetName()214     const char* onGetName() override {
215         return fIsSmall ? "lightingspotlitspecular_small" : "lightingspotlitspecular_large";
216     }
217 
onDraw(const int loops,SkCanvas * canvas)218     void onDraw(const int loops, SkCanvas* canvas) override {
219         draw(loops, canvas, SkLightingImageFilter::CreateSpotLitSpecular(getSpotLocation(),
220                                                                          getSpotTarget(),
221                                                                          getSpotExponent(),
222                                                                          getCutoffAngle(),
223                                                                          getWhite(),
224                                                                          getSurfaceScale(),
225                                                                          getKs(),
226                                                                          getShininess()));
227     }
228 
229 private:
230     typedef LightingBaseBench INHERITED;
231 };
232 
233 ///////////////////////////////////////////////////////////////////////////////
234 
235 DEF_BENCH( return new LightingPointLitDiffuseBench(true); )
236 DEF_BENCH( return new LightingPointLitDiffuseBench(false); )
237 DEF_BENCH( return new LightingDistantLitDiffuseBench(true); )
238 DEF_BENCH( return new LightingDistantLitDiffuseBench(false); )
239 DEF_BENCH( return new LightingSpotLitDiffuseBench(true); )
240 DEF_BENCH( return new LightingSpotLitDiffuseBench(false); )
241 DEF_BENCH( return new LightingPointLitSpecularBench(true); )
242 DEF_BENCH( return new LightingPointLitSpecularBench(false); )
243 DEF_BENCH( return new LightingDistantLitSpecularBench(true); )
244 DEF_BENCH( return new LightingDistantLitSpecularBench(false); )
245 DEF_BENCH( return new LightingSpotLitSpecularBench(true); )
246 DEF_BENCH( return new LightingSpotLitSpecularBench(false); )
247