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 "SkCanvas.h" 9 #include "SkLightingImageFilter.h" 10 #include "SkPoint3.h" 11 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 17 class LightingBaseBench : public Benchmark { 18 public: LightingBaseBench(bool small)19 LightingBaseBench(bool small) : fIsSmall(small) { } 20 21 protected: draw(int loops,SkCanvas * canvas,sk_sp<SkImageFilter> imageFilter) const22 void draw(int loops, SkCanvas* canvas, sk_sp<SkImageFilter> imageFilter) const { 23 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) : 24 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE); 25 SkPaint paint; 26 paint.setImageFilter(std::move(imageFilter)); 27 for (int i = 0; i < loops; i++) { 28 canvas->drawRect(r, paint); 29 } 30 } 31 GetPointLocation()32 static SkPoint3 GetPointLocation() { 33 static SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10)); 34 return pointLocation; 35 } 36 GetDistantDirection()37 static SkPoint3 GetDistantDirection() { 38 static SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225)); 39 static SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); 40 static SkPoint3 distantDirection = SkPoint3::Make( 41 SkScalarCos(azimuthRad) * SkScalarCos(elevationRad), 42 SkScalarSin(azimuthRad) * SkScalarCos(elevationRad), 43 SkScalarSin(elevationRad)); 44 return distantDirection; 45 } 46 GetSpotLocation()47 static SkPoint3 GetSpotLocation() { 48 static SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10), 49 SkIntToScalar(-10), 50 SkIntToScalar(20)); 51 return spotLocation; 52 } 53 GetSpotTarget()54 static SkPoint3 GetSpotTarget() { 55 static SkPoint3 spotTarget = SkPoint3::Make(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 protected: onGetName()103 const char* onGetName() override { 104 return fIsSmall ? "lightingpointlitdiffuse_small" : "lightingpointlitdiffuse_large"; 105 } 106 onDraw(int loops,SkCanvas * canvas)107 void onDraw(int loops, SkCanvas* canvas) override { 108 draw(loops, canvas, SkLightingImageFilter::MakePointLitDiffuse(GetPointLocation(), 109 GetWhite(), 110 GetSurfaceScale(), 111 GetKd(), 112 nullptr)); 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 protected: onGetName()124 const char* onGetName() override { 125 return fIsSmall ? "lightingdistantlitdiffuse_small" : "lightingdistantlitdiffuse_large"; 126 } 127 onDraw(int loops,SkCanvas * canvas)128 void onDraw(int loops, SkCanvas* canvas) override { 129 draw(loops, canvas, SkLightingImageFilter::MakeDistantLitDiffuse(GetDistantDirection(), 130 GetWhite(), 131 GetSurfaceScale(), 132 GetKd(), 133 nullptr)); 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 protected: onGetName()145 const char* onGetName() override { 146 return fIsSmall ? "lightingspotlitdiffuse_small" : "lightingspotlitdiffuse_large"; 147 } 148 onDraw(int loops,SkCanvas * canvas)149 void onDraw(int loops, SkCanvas* canvas) override { 150 draw(loops, canvas, SkLightingImageFilter::MakeSpotLitDiffuse(GetSpotLocation(), 151 GetSpotTarget(), 152 GetSpotExponent(), 153 GetCutoffAngle(), 154 GetWhite(), 155 GetSurfaceScale(), 156 GetKd(), 157 nullptr)); 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 protected: onGetName()169 const char* onGetName() override { 170 return fIsSmall ? "lightingpointlitspecular_small" : "lightingpointlitspecular_large"; 171 } 172 onDraw(int loops,SkCanvas * canvas)173 void onDraw(int loops, SkCanvas* canvas) override { 174 draw(loops, canvas, SkLightingImageFilter::MakePointLitSpecular(GetPointLocation(), 175 GetWhite(), 176 GetSurfaceScale(), 177 GetKs(), 178 GetShininess(), 179 nullptr)); 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 protected: onGetName()191 const char* onGetName() override { 192 return fIsSmall ? "lightingdistantlitspecular_small" : "lightingdistantlitspecular_large"; 193 } 194 onDraw(int loops,SkCanvas * canvas)195 void onDraw(int loops, SkCanvas* canvas) override { 196 draw(loops, canvas, SkLightingImageFilter::MakeDistantLitSpecular(GetDistantDirection(), 197 GetWhite(), 198 GetSurfaceScale(), 199 GetKs(), 200 GetShininess(), 201 nullptr)); 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 protected: onGetName()213 const char* onGetName() override { 214 return fIsSmall ? "lightingspotlitspecular_small" : "lightingspotlitspecular_large"; 215 } 216 onDraw(int loops,SkCanvas * canvas)217 void onDraw(int loops, SkCanvas* canvas) override { 218 draw(loops, canvas, SkLightingImageFilter::MakeSpotLitSpecular(GetSpotLocation(), 219 GetSpotTarget(), 220 GetSpotExponent(), 221 GetCutoffAngle(), 222 GetWhite(), 223 GetSurfaceScale(), 224 GetKs(), 225 GetShininess(), 226 nullptr)); 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