blob: 2c78749d9f01f2155c3d07c26d4686b2a3d8b610 [file] [log] [blame]
sugoi@google.com5d71adf2013-04-24 19:36:44 +00001/*
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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
sugoi@google.com5d71adf2013-04-24 19:36:44 +00008#include "SkCanvas.h"
9#include "SkDevice.h"
10#include "SkLightingImageFilter.h"
robertphillips3d32d762015-07-13 13:16:44 -070011#include "SkPoint3.h"
sugoi@google.com5d71adf2013-04-24 19:36:44 +000012
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
tfarinaf168b862014-06-19 12:32:29 -070018class LightingBaseBench : public Benchmark {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000019public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000020 LightingBaseBench(bool small) : fIsSmall(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +000021
22protected:
mtkleina1ebeb22015-10-01 09:43:39 -070023 void draw(int loops, SkCanvas* canvas, SkImageFilter* imageFilter) const {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000024 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();
commit-bot@chromium.org33614712013-12-03 18:17:16 +000028 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000029 canvas->drawRect(r, paint);
30 }
sugoi@google.com5d71adf2013-04-24 19:36:44 +000031 }
32
robertphillips3d32d762015-07-13 13:16:44 -070033 static SkPoint3 GetPointLocation() {
34 static SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000035 return pointLocation;
36 }
37
robertphillips3d32d762015-07-13 13:16:44 -070038 static SkPoint3 GetDistantDirection() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000039 static SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
40 static SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
robertphillips3d32d762015-07-13 13:16:44 -070041 static SkPoint3 distantDirection = SkPoint3::Make(SkScalarMul(SkScalarCos(azimuthRad),
42 SkScalarCos(elevationRad)),
43 SkScalarMul(SkScalarSin(azimuthRad),
44 SkScalarCos(elevationRad)),
45 SkScalarSin(elevationRad));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000046 return distantDirection;
47 }
48
robertphillips3d32d762015-07-13 13:16:44 -070049 static SkPoint3 GetSpotLocation() {
50 static SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10),
51 SkIntToScalar(-10),
52 SkIntToScalar(20));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000053 return spotLocation;
54 }
55
robertphillips3d32d762015-07-13 13:16:44 -070056 static SkPoint3 GetSpotTarget() {
57 static SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
sugoi@google.com5d71adf2013-04-24 19:36:44 +000058 return spotTarget;
59 }
60
robertphillips3d32d762015-07-13 13:16:44 -070061 static SkScalar GetSpotExponent() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000062 static SkScalar spotExponent = SK_Scalar1;
63 return spotExponent;
64 }
65
robertphillips3d32d762015-07-13 13:16:44 -070066 static SkScalar GetCutoffAngle() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000067 static SkScalar cutoffAngle = SkIntToScalar(15);
68 return cutoffAngle;
69 }
70
robertphillips3d32d762015-07-13 13:16:44 -070071 static SkScalar GetKd() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000072 static SkScalar kd = SkIntToScalar(2);
73 return kd;
74 }
75
robertphillips3d32d762015-07-13 13:16:44 -070076 static SkScalar GetKs() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000077 static SkScalar ks = SkIntToScalar(1);
78 return ks;
79 }
80
robertphillips3d32d762015-07-13 13:16:44 -070081 static SkScalar GetShininess() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000082 static SkScalar shininess = SkIntToScalar(8);
83 return shininess;
84 }
85
robertphillips3d32d762015-07-13 13:16:44 -070086 static SkScalar GetSurfaceScale() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000087 static SkScalar surfaceScale = SkIntToScalar(1);
88 return surfaceScale;
89 }
90
robertphillips3d32d762015-07-13 13:16:44 -070091 static SkColor GetWhite() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000092 static SkColor white(0xFFFFFFFF);
93 return white;
94 }
95
96 bool fIsSmall;
tfarinaf168b862014-06-19 12:32:29 -070097 typedef Benchmark INHERITED;
sugoi@google.com5d71adf2013-04-24 19:36:44 +000098};
99
100class LightingPointLitDiffuseBench : public LightingBaseBench {
101public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000102 LightingPointLitDiffuseBench(bool small) : INHERITED(small) {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000103 }
104
105protected:
mtklein36352bf2015-03-25 18:17:31 -0700106 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000107 return fIsSmall ? "lightingpointlitdiffuse_small" : "lightingpointlitdiffuse_large";
108 }
109
mtkleina1ebeb22015-10-01 09:43:39 -0700110 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips3d32d762015-07-13 13:16:44 -0700111 draw(loops, canvas, SkLightingImageFilter::CreatePointLitDiffuse(GetPointLocation(),
112 GetWhite(),
113 GetSurfaceScale(),
114 GetKd()));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000115 }
116
117private:
118 typedef LightingBaseBench INHERITED;
119};
120
121class LightingDistantLitDiffuseBench : public LightingBaseBench {
122public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000123 LightingDistantLitDiffuseBench(bool small) : INHERITED(small) {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000124 }
125
126protected:
mtklein36352bf2015-03-25 18:17:31 -0700127 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000128 return fIsSmall ? "lightingdistantlitdiffuse_small" : "lightingdistantlitdiffuse_large";
129 }
130
mtkleina1ebeb22015-10-01 09:43:39 -0700131 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips3d32d762015-07-13 13:16:44 -0700132 draw(loops, canvas, SkLightingImageFilter::CreateDistantLitDiffuse(GetDistantDirection(),
133 GetWhite(),
134 GetSurfaceScale(),
135 GetKd()));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000136 }
137
138private:
139 typedef LightingBaseBench INHERITED;
140};
141
142class LightingSpotLitDiffuseBench : public LightingBaseBench {
143public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000144 LightingSpotLitDiffuseBench(bool small) : INHERITED(small) {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000145 }
146
147protected:
mtklein36352bf2015-03-25 18:17:31 -0700148 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000149 return fIsSmall ? "lightingspotlitdiffuse_small" : "lightingspotlitdiffuse_large";
150 }
151
mtkleina1ebeb22015-10-01 09:43:39 -0700152 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips3d32d762015-07-13 13:16:44 -0700153 draw(loops, canvas, SkLightingImageFilter::CreateSpotLitDiffuse(GetSpotLocation(),
154 GetSpotTarget(),
155 GetSpotExponent(),
156 GetCutoffAngle(),
157 GetWhite(),
158 GetSurfaceScale(),
159 GetKd()));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000160 }
161
162private:
163 typedef LightingBaseBench INHERITED;
164};
165
166class LightingPointLitSpecularBench : public LightingBaseBench {
167public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000168 LightingPointLitSpecularBench(bool small) : INHERITED(small) {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000169 }
170
171protected:
mtklein36352bf2015-03-25 18:17:31 -0700172 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000173 return fIsSmall ? "lightingpointlitspecular_small" : "lightingpointlitspecular_large";
174 }
175
mtkleina1ebeb22015-10-01 09:43:39 -0700176 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips3d32d762015-07-13 13:16:44 -0700177 draw(loops, canvas, SkLightingImageFilter::CreatePointLitSpecular(GetPointLocation(),
178 GetWhite(),
179 GetSurfaceScale(),
180 GetKs(),
181 GetShininess()));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000182 }
183
184private:
185 typedef LightingBaseBench INHERITED;
186};
187
188class LightingDistantLitSpecularBench : public LightingBaseBench {
189public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000190 LightingDistantLitSpecularBench(bool small) : INHERITED(small) {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000191 }
192
193protected:
mtklein36352bf2015-03-25 18:17:31 -0700194 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000195 return fIsSmall ? "lightingdistantlitspecular_small" : "lightingdistantlitspecular_large";
196 }
197
mtkleina1ebeb22015-10-01 09:43:39 -0700198 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips3d32d762015-07-13 13:16:44 -0700199 draw(loops, canvas, SkLightingImageFilter::CreateDistantLitSpecular(GetDistantDirection(),
200 GetWhite(),
201 GetSurfaceScale(),
202 GetKs(),
203 GetShininess()));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000204 }
205
206private:
207 typedef LightingBaseBench INHERITED;
208};
209
210class LightingSpotLitSpecularBench : public LightingBaseBench {
211public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000212 LightingSpotLitSpecularBench(bool small) : INHERITED(small) {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000213 }
214
215protected:
mtklein36352bf2015-03-25 18:17:31 -0700216 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000217 return fIsSmall ? "lightingspotlitspecular_small" : "lightingspotlitspecular_large";
218 }
219
mtkleina1ebeb22015-10-01 09:43:39 -0700220 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips3d32d762015-07-13 13:16:44 -0700221 draw(loops, canvas, SkLightingImageFilter::CreateSpotLitSpecular(GetSpotLocation(),
222 GetSpotTarget(),
223 GetSpotExponent(),
224 GetCutoffAngle(),
225 GetWhite(),
226 GetSurfaceScale(),
227 GetKs(),
228 GetShininess()));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000229 }
230
231private:
232 typedef LightingBaseBench INHERITED;
233};
234
235///////////////////////////////////////////////////////////////////////////////
236
mtklein@google.com410e6e82013-09-13 19:52:27 +0000237DEF_BENCH( return new LightingPointLitDiffuseBench(true); )
238DEF_BENCH( return new LightingPointLitDiffuseBench(false); )
239DEF_BENCH( return new LightingDistantLitDiffuseBench(true); )
240DEF_BENCH( return new LightingDistantLitDiffuseBench(false); )
241DEF_BENCH( return new LightingSpotLitDiffuseBench(true); )
242DEF_BENCH( return new LightingSpotLitDiffuseBench(false); )
243DEF_BENCH( return new LightingPointLitSpecularBench(true); )
244DEF_BENCH( return new LightingPointLitSpecularBench(false); )
245DEF_BENCH( return new LightingDistantLitSpecularBench(true); )
246DEF_BENCH( return new LightingDistantLitSpecularBench(false); )
247DEF_BENCH( return new LightingSpotLitSpecularBench(true); )
248DEF_BENCH( return new LightingSpotLitSpecularBench(false); )