blob: a924f4a9ce3c773f009b2f25e0c883d051a08be1 [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"
sugoi@google.com5d71adf2013-04-24 19:36:44 +00009#include "SkLightingImageFilter.h"
robertphillips3d32d762015-07-13 13:16:44 -070010#include "SkPoint3.h"
sugoi@google.com5d71adf2013-04-24 19:36:44 +000011
12#define FILTER_WIDTH_SMALL SkIntToScalar(32)
13#define FILTER_HEIGHT_SMALL SkIntToScalar(32)
14#define FILTER_WIDTH_LARGE SkIntToScalar(256)
15#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
16
tfarinaf168b862014-06-19 12:32:29 -070017class LightingBaseBench : public Benchmark {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000018public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000019 LightingBaseBench(bool small) : fIsSmall(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +000020
21protected:
robertphillips12fa47d2016-04-08 16:28:09 -070022 void draw(int loops, SkCanvas* canvas, sk_sp<SkImageFilter> imageFilter) const {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000023 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
24 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
25 SkPaint paint;
robertphillips12fa47d2016-04-08 16:28:09 -070026 paint.setImageFilter(std::move(imageFilter));
commit-bot@chromium.org33614712013-12-03 18:17:16 +000027 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000028 canvas->drawRect(r, paint);
29 }
sugoi@google.com5d71adf2013-04-24 19:36:44 +000030 }
31
robertphillips3d32d762015-07-13 13:16:44 -070032 static SkPoint3 GetPointLocation() {
33 static SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000034 return pointLocation;
35 }
36
robertphillips3d32d762015-07-13 13:16:44 -070037 static SkPoint3 GetDistantDirection() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000038 static SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
39 static SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
Mike Reeddf85c382017-02-14 10:59:19 -050040 static SkPoint3 distantDirection = SkPoint3::Make(
41 SkScalarCos(azimuthRad) * SkScalarCos(elevationRad),
42 SkScalarSin(azimuthRad) * SkScalarCos(elevationRad),
43 SkScalarSin(elevationRad));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000044 return distantDirection;
45 }
46
robertphillips3d32d762015-07-13 13:16:44 -070047 static SkPoint3 GetSpotLocation() {
48 static SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10),
49 SkIntToScalar(-10),
50 SkIntToScalar(20));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000051 return spotLocation;
52 }
53
robertphillips3d32d762015-07-13 13:16:44 -070054 static SkPoint3 GetSpotTarget() {
55 static SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
sugoi@google.com5d71adf2013-04-24 19:36:44 +000056 return spotTarget;
57 }
58
robertphillips3d32d762015-07-13 13:16:44 -070059 static SkScalar GetSpotExponent() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000060 static SkScalar spotExponent = SK_Scalar1;
61 return spotExponent;
62 }
63
robertphillips3d32d762015-07-13 13:16:44 -070064 static SkScalar GetCutoffAngle() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000065 static SkScalar cutoffAngle = SkIntToScalar(15);
66 return cutoffAngle;
67 }
68
robertphillips3d32d762015-07-13 13:16:44 -070069 static SkScalar GetKd() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000070 static SkScalar kd = SkIntToScalar(2);
71 return kd;
72 }
73
robertphillips3d32d762015-07-13 13:16:44 -070074 static SkScalar GetKs() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000075 static SkScalar ks = SkIntToScalar(1);
76 return ks;
77 }
78
robertphillips3d32d762015-07-13 13:16:44 -070079 static SkScalar GetShininess() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000080 static SkScalar shininess = SkIntToScalar(8);
81 return shininess;
82 }
83
robertphillips3d32d762015-07-13 13:16:44 -070084 static SkScalar GetSurfaceScale() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000085 static SkScalar surfaceScale = SkIntToScalar(1);
86 return surfaceScale;
87 }
88
robertphillips3d32d762015-07-13 13:16:44 -070089 static SkColor GetWhite() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000090 static SkColor white(0xFFFFFFFF);
91 return white;
92 }
93
94 bool fIsSmall;
tfarinaf168b862014-06-19 12:32:29 -070095 typedef Benchmark INHERITED;
sugoi@google.com5d71adf2013-04-24 19:36:44 +000096};
97
98class LightingPointLitDiffuseBench : public LightingBaseBench {
99public:
robertphillips12fa47d2016-04-08 16:28:09 -0700100 LightingPointLitDiffuseBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000101
102protected:
mtklein36352bf2015-03-25 18:17:31 -0700103 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000104 return fIsSmall ? "lightingpointlitdiffuse_small" : "lightingpointlitdiffuse_large";
105 }
106
mtkleina1ebeb22015-10-01 09:43:39 -0700107 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700108 draw(loops, canvas, SkLightingImageFilter::MakePointLitDiffuse(GetPointLocation(),
109 GetWhite(),
110 GetSurfaceScale(),
111 GetKd(),
112 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000113 }
114
115private:
116 typedef LightingBaseBench INHERITED;
117};
118
119class LightingDistantLitDiffuseBench : public LightingBaseBench {
120public:
robertphillips12fa47d2016-04-08 16:28:09 -0700121 LightingDistantLitDiffuseBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000122
123protected:
mtklein36352bf2015-03-25 18:17:31 -0700124 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000125 return fIsSmall ? "lightingdistantlitdiffuse_small" : "lightingdistantlitdiffuse_large";
126 }
127
mtkleina1ebeb22015-10-01 09:43:39 -0700128 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700129 draw(loops, canvas, SkLightingImageFilter::MakeDistantLitDiffuse(GetDistantDirection(),
130 GetWhite(),
131 GetSurfaceScale(),
132 GetKd(),
133 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000134 }
135
136private:
137 typedef LightingBaseBench INHERITED;
138};
139
140class LightingSpotLitDiffuseBench : public LightingBaseBench {
141public:
robertphillips12fa47d2016-04-08 16:28:09 -0700142 LightingSpotLitDiffuseBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000143
144protected:
mtklein36352bf2015-03-25 18:17:31 -0700145 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000146 return fIsSmall ? "lightingspotlitdiffuse_small" : "lightingspotlitdiffuse_large";
147 }
148
mtkleina1ebeb22015-10-01 09:43:39 -0700149 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700150 draw(loops, canvas, SkLightingImageFilter::MakeSpotLitDiffuse(GetSpotLocation(),
151 GetSpotTarget(),
152 GetSpotExponent(),
153 GetCutoffAngle(),
154 GetWhite(),
155 GetSurfaceScale(),
156 GetKd(),
157 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000158 }
159
160private:
161 typedef LightingBaseBench INHERITED;
162};
163
164class LightingPointLitSpecularBench : public LightingBaseBench {
165public:
robertphillips12fa47d2016-04-08 16:28:09 -0700166 LightingPointLitSpecularBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000167
168protected:
mtklein36352bf2015-03-25 18:17:31 -0700169 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000170 return fIsSmall ? "lightingpointlitspecular_small" : "lightingpointlitspecular_large";
171 }
172
mtkleina1ebeb22015-10-01 09:43:39 -0700173 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700174 draw(loops, canvas, SkLightingImageFilter::MakePointLitSpecular(GetPointLocation(),
175 GetWhite(),
176 GetSurfaceScale(),
177 GetKs(),
178 GetShininess(),
179 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000180 }
181
182private:
183 typedef LightingBaseBench INHERITED;
184};
185
186class LightingDistantLitSpecularBench : public LightingBaseBench {
187public:
robertphillips12fa47d2016-04-08 16:28:09 -0700188 LightingDistantLitSpecularBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000189
190protected:
mtklein36352bf2015-03-25 18:17:31 -0700191 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000192 return fIsSmall ? "lightingdistantlitspecular_small" : "lightingdistantlitspecular_large";
193 }
194
mtkleina1ebeb22015-10-01 09:43:39 -0700195 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700196 draw(loops, canvas, SkLightingImageFilter::MakeDistantLitSpecular(GetDistantDirection(),
197 GetWhite(),
198 GetSurfaceScale(),
199 GetKs(),
200 GetShininess(),
201 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000202 }
203
204private:
205 typedef LightingBaseBench INHERITED;
206};
207
208class LightingSpotLitSpecularBench : public LightingBaseBench {
209public:
robertphillips12fa47d2016-04-08 16:28:09 -0700210 LightingSpotLitSpecularBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000211
212protected:
mtklein36352bf2015-03-25 18:17:31 -0700213 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000214 return fIsSmall ? "lightingspotlitspecular_small" : "lightingspotlitspecular_large";
215 }
216
mtkleina1ebeb22015-10-01 09:43:39 -0700217 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700218 draw(loops, canvas, SkLightingImageFilter::MakeSpotLitSpecular(GetSpotLocation(),
219 GetSpotTarget(),
220 GetSpotExponent(),
221 GetCutoffAngle(),
222 GetWhite(),
223 GetSurfaceScale(),
224 GetKs(),
225 GetShininess(),
226 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000227 }
228
229private:
230 typedef LightingBaseBench INHERITED;
231};
232
233///////////////////////////////////////////////////////////////////////////////
234
mtklein@google.com410e6e82013-09-13 19:52:27 +0000235DEF_BENCH( return new LightingPointLitDiffuseBench(true); )
236DEF_BENCH( return new LightingPointLitDiffuseBench(false); )
237DEF_BENCH( return new LightingDistantLitDiffuseBench(true); )
238DEF_BENCH( return new LightingDistantLitDiffuseBench(false); )
239DEF_BENCH( return new LightingSpotLitDiffuseBench(true); )
240DEF_BENCH( return new LightingSpotLitDiffuseBench(false); )
241DEF_BENCH( return new LightingPointLitSpecularBench(true); )
242DEF_BENCH( return new LightingPointLitSpecularBench(false); )
243DEF_BENCH( return new LightingDistantLitSpecularBench(true); )
244DEF_BENCH( return new LightingDistantLitSpecularBench(false); )
245DEF_BENCH( return new LightingSpotLitSpecularBench(true); )
246DEF_BENCH( return new LightingSpotLitSpecularBench(false); )