blob: bbe0af343ca40e69d193af353e9fd8ab8e2b7726 [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));
robertphillips3d32d762015-07-13 13:16:44 -070040 static SkPoint3 distantDirection = SkPoint3::Make(SkScalarMul(SkScalarCos(azimuthRad),
41 SkScalarCos(elevationRad)),
42 SkScalarMul(SkScalarSin(azimuthRad),
43 SkScalarCos(elevationRad)),
44 SkScalarSin(elevationRad));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000045 return distantDirection;
46 }
47
robertphillips3d32d762015-07-13 13:16:44 -070048 static SkPoint3 GetSpotLocation() {
49 static SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10),
50 SkIntToScalar(-10),
51 SkIntToScalar(20));
sugoi@google.com5d71adf2013-04-24 19:36:44 +000052 return spotLocation;
53 }
54
robertphillips3d32d762015-07-13 13:16:44 -070055 static SkPoint3 GetSpotTarget() {
56 static SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
sugoi@google.com5d71adf2013-04-24 19:36:44 +000057 return spotTarget;
58 }
59
robertphillips3d32d762015-07-13 13:16:44 -070060 static SkScalar GetSpotExponent() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000061 static SkScalar spotExponent = SK_Scalar1;
62 return spotExponent;
63 }
64
robertphillips3d32d762015-07-13 13:16:44 -070065 static SkScalar GetCutoffAngle() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000066 static SkScalar cutoffAngle = SkIntToScalar(15);
67 return cutoffAngle;
68 }
69
robertphillips3d32d762015-07-13 13:16:44 -070070 static SkScalar GetKd() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000071 static SkScalar kd = SkIntToScalar(2);
72 return kd;
73 }
74
robertphillips3d32d762015-07-13 13:16:44 -070075 static SkScalar GetKs() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000076 static SkScalar ks = SkIntToScalar(1);
77 return ks;
78 }
79
robertphillips3d32d762015-07-13 13:16:44 -070080 static SkScalar GetShininess() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000081 static SkScalar shininess = SkIntToScalar(8);
82 return shininess;
83 }
84
robertphillips3d32d762015-07-13 13:16:44 -070085 static SkScalar GetSurfaceScale() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000086 static SkScalar surfaceScale = SkIntToScalar(1);
87 return surfaceScale;
88 }
89
robertphillips3d32d762015-07-13 13:16:44 -070090 static SkColor GetWhite() {
sugoi@google.com5d71adf2013-04-24 19:36:44 +000091 static SkColor white(0xFFFFFFFF);
92 return white;
93 }
94
95 bool fIsSmall;
tfarinaf168b862014-06-19 12:32:29 -070096 typedef Benchmark INHERITED;
sugoi@google.com5d71adf2013-04-24 19:36:44 +000097};
98
99class LightingPointLitDiffuseBench : public LightingBaseBench {
100public:
robertphillips12fa47d2016-04-08 16:28:09 -0700101 LightingPointLitDiffuseBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000102
103protected:
mtklein36352bf2015-03-25 18:17:31 -0700104 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000105 return fIsSmall ? "lightingpointlitdiffuse_small" : "lightingpointlitdiffuse_large";
106 }
107
mtkleina1ebeb22015-10-01 09:43:39 -0700108 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700109 draw(loops, canvas, SkLightingImageFilter::MakePointLitDiffuse(GetPointLocation(),
110 GetWhite(),
111 GetSurfaceScale(),
112 GetKd(),
113 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000114 }
115
116private:
117 typedef LightingBaseBench INHERITED;
118};
119
120class LightingDistantLitDiffuseBench : public LightingBaseBench {
121public:
robertphillips12fa47d2016-04-08 16:28:09 -0700122 LightingDistantLitDiffuseBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000123
124protected:
mtklein36352bf2015-03-25 18:17:31 -0700125 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000126 return fIsSmall ? "lightingdistantlitdiffuse_small" : "lightingdistantlitdiffuse_large";
127 }
128
mtkleina1ebeb22015-10-01 09:43:39 -0700129 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700130 draw(loops, canvas, SkLightingImageFilter::MakeDistantLitDiffuse(GetDistantDirection(),
131 GetWhite(),
132 GetSurfaceScale(),
133 GetKd(),
134 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000135 }
136
137private:
138 typedef LightingBaseBench INHERITED;
139};
140
141class LightingSpotLitDiffuseBench : public LightingBaseBench {
142public:
robertphillips12fa47d2016-04-08 16:28:09 -0700143 LightingSpotLitDiffuseBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000144
145protected:
mtklein36352bf2015-03-25 18:17:31 -0700146 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000147 return fIsSmall ? "lightingspotlitdiffuse_small" : "lightingspotlitdiffuse_large";
148 }
149
mtkleina1ebeb22015-10-01 09:43:39 -0700150 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700151 draw(loops, canvas, SkLightingImageFilter::MakeSpotLitDiffuse(GetSpotLocation(),
152 GetSpotTarget(),
153 GetSpotExponent(),
154 GetCutoffAngle(),
155 GetWhite(),
156 GetSurfaceScale(),
157 GetKd(),
158 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000159 }
160
161private:
162 typedef LightingBaseBench INHERITED;
163};
164
165class LightingPointLitSpecularBench : public LightingBaseBench {
166public:
robertphillips12fa47d2016-04-08 16:28:09 -0700167 LightingPointLitSpecularBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000168
169protected:
mtklein36352bf2015-03-25 18:17:31 -0700170 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000171 return fIsSmall ? "lightingpointlitspecular_small" : "lightingpointlitspecular_large";
172 }
173
mtkleina1ebeb22015-10-01 09:43:39 -0700174 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700175 draw(loops, canvas, SkLightingImageFilter::MakePointLitSpecular(GetPointLocation(),
176 GetWhite(),
177 GetSurfaceScale(),
178 GetKs(),
179 GetShininess(),
180 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000181 }
182
183private:
184 typedef LightingBaseBench INHERITED;
185};
186
187class LightingDistantLitSpecularBench : public LightingBaseBench {
188public:
robertphillips12fa47d2016-04-08 16:28:09 -0700189 LightingDistantLitSpecularBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000190
191protected:
mtklein36352bf2015-03-25 18:17:31 -0700192 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000193 return fIsSmall ? "lightingdistantlitspecular_small" : "lightingdistantlitspecular_large";
194 }
195
mtkleina1ebeb22015-10-01 09:43:39 -0700196 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700197 draw(loops, canvas, SkLightingImageFilter::MakeDistantLitSpecular(GetDistantDirection(),
198 GetWhite(),
199 GetSurfaceScale(),
200 GetKs(),
201 GetShininess(),
202 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000203 }
204
205private:
206 typedef LightingBaseBench INHERITED;
207};
208
209class LightingSpotLitSpecularBench : public LightingBaseBench {
210public:
robertphillips12fa47d2016-04-08 16:28:09 -0700211 LightingSpotLitSpecularBench(bool small) : INHERITED(small) { }
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000212
213protected:
mtklein36352bf2015-03-25 18:17:31 -0700214 const char* onGetName() override {
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000215 return fIsSmall ? "lightingspotlitspecular_small" : "lightingspotlitspecular_large";
216 }
217
mtkleina1ebeb22015-10-01 09:43:39 -0700218 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips12fa47d2016-04-08 16:28:09 -0700219 draw(loops, canvas, SkLightingImageFilter::MakeSpotLitSpecular(GetSpotLocation(),
220 GetSpotTarget(),
221 GetSpotExponent(),
222 GetCutoffAngle(),
223 GetWhite(),
224 GetSurfaceScale(),
225 GetKs(),
226 GetShininess(),
227 nullptr));
sugoi@google.com5d71adf2013-04-24 19:36:44 +0000228 }
229
230private:
231 typedef LightingBaseBench INHERITED;
232};
233
234///////////////////////////////////////////////////////////////////////////////
235
mtklein@google.com410e6e82013-09-13 19:52:27 +0000236DEF_BENCH( return new LightingPointLitDiffuseBench(true); )
237DEF_BENCH( return new LightingPointLitDiffuseBench(false); )
238DEF_BENCH( return new LightingDistantLitDiffuseBench(true); )
239DEF_BENCH( return new LightingDistantLitDiffuseBench(false); )
240DEF_BENCH( return new LightingSpotLitDiffuseBench(true); )
241DEF_BENCH( return new LightingSpotLitDiffuseBench(false); )
242DEF_BENCH( return new LightingPointLitSpecularBench(true); )
243DEF_BENCH( return new LightingPointLitSpecularBench(false); )
244DEF_BENCH( return new LightingDistantLitSpecularBench(true); )
245DEF_BENCH( return new LightingDistantLitSpecularBench(false); )
246DEF_BENCH( return new LightingSpotLitSpecularBench(true); )
247DEF_BENCH( return new LightingSpotLitSpecularBench(false); )