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 "bench/Benchmark.h" 8 #include "include/core/SkCanvas.h" 9 #include "include/core/SkPoint3.h" 10 #include "include/effects/SkImageFilters.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 using INHERITED = Benchmark; 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, SkImageFilters::PointLitDiffuse( 109 GetPointLocation(), GetWhite(), GetSurfaceScale(), GetKd(), nullptr)); 110 } 111 112 private: 113 using INHERITED = LightingBaseBench; 114 }; 115 116 class LightingDistantLitDiffuseBench : public LightingBaseBench { 117 public: LightingDistantLitDiffuseBench(bool small)118 LightingDistantLitDiffuseBench(bool small) : INHERITED(small) { } 119 120 protected: onGetName()121 const char* onGetName() override { 122 return fIsSmall ? "lightingdistantlitdiffuse_small" : "lightingdistantlitdiffuse_large"; 123 } 124 onDraw(int loops,SkCanvas * canvas)125 void onDraw(int loops, SkCanvas* canvas) override { 126 draw(loops, canvas, SkImageFilters::DistantLitDiffuse( 127 GetDistantDirection(), GetWhite(), GetSurfaceScale(), GetKd(), nullptr)); 128 } 129 130 private: 131 using INHERITED = LightingBaseBench; 132 }; 133 134 class LightingSpotLitDiffuseBench : public LightingBaseBench { 135 public: LightingSpotLitDiffuseBench(bool small)136 LightingSpotLitDiffuseBench(bool small) : INHERITED(small) { } 137 138 protected: onGetName()139 const char* onGetName() override { 140 return fIsSmall ? "lightingspotlitdiffuse_small" : "lightingspotlitdiffuse_large"; 141 } 142 onDraw(int loops,SkCanvas * canvas)143 void onDraw(int loops, SkCanvas* canvas) override { 144 draw(loops, canvas, SkImageFilters::SpotLitDiffuse( 145 GetSpotLocation(), GetSpotTarget(), GetSpotExponent(), GetCutoffAngle(), 146 GetWhite(), GetSurfaceScale(), GetKd(), nullptr)); 147 } 148 149 private: 150 using INHERITED = LightingBaseBench; 151 }; 152 153 class LightingPointLitSpecularBench : public LightingBaseBench { 154 public: LightingPointLitSpecularBench(bool small)155 LightingPointLitSpecularBench(bool small) : INHERITED(small) { } 156 157 protected: onGetName()158 const char* onGetName() override { 159 return fIsSmall ? "lightingpointlitspecular_small" : "lightingpointlitspecular_large"; 160 } 161 onDraw(int loops,SkCanvas * canvas)162 void onDraw(int loops, SkCanvas* canvas) override { 163 draw(loops, canvas, SkImageFilters::PointLitSpecular( 164 GetPointLocation(), GetWhite(), GetSurfaceScale(), GetKs(), GetShininess(), 165 nullptr)); 166 } 167 168 private: 169 using INHERITED = LightingBaseBench; 170 }; 171 172 class LightingDistantLitSpecularBench : public LightingBaseBench { 173 public: LightingDistantLitSpecularBench(bool small)174 LightingDistantLitSpecularBench(bool small) : INHERITED(small) { } 175 176 protected: onGetName()177 const char* onGetName() override { 178 return fIsSmall ? "lightingdistantlitspecular_small" : "lightingdistantlitspecular_large"; 179 } 180 onDraw(int loops,SkCanvas * canvas)181 void onDraw(int loops, SkCanvas* canvas) override { 182 draw(loops, canvas, SkImageFilters::DistantLitSpecular( 183 GetDistantDirection(), GetWhite(), GetSurfaceScale(), GetKs(), GetShininess(), 184 nullptr)); 185 } 186 187 private: 188 using INHERITED = LightingBaseBench; 189 }; 190 191 class LightingSpotLitSpecularBench : public LightingBaseBench { 192 public: LightingSpotLitSpecularBench(bool small)193 LightingSpotLitSpecularBench(bool small) : INHERITED(small) { } 194 195 protected: onGetName()196 const char* onGetName() override { 197 return fIsSmall ? "lightingspotlitspecular_small" : "lightingspotlitspecular_large"; 198 } 199 onDraw(int loops,SkCanvas * canvas)200 void onDraw(int loops, SkCanvas* canvas) override { 201 draw(loops, canvas, SkImageFilters::SpotLitSpecular( 202 GetSpotLocation(), GetSpotTarget(), GetSpotExponent(), GetCutoffAngle(), 203 GetWhite(), GetSurfaceScale(), GetKs(), GetShininess(), nullptr)); 204 } 205 206 private: 207 using INHERITED = LightingBaseBench; 208 }; 209 210 /////////////////////////////////////////////////////////////////////////////// 211 212 DEF_BENCH( return new LightingPointLitDiffuseBench(true); ) 213 DEF_BENCH( return new LightingPointLitDiffuseBench(false); ) 214 DEF_BENCH( return new LightingDistantLitDiffuseBench(true); ) 215 DEF_BENCH( return new LightingDistantLitDiffuseBench(false); ) 216 DEF_BENCH( return new LightingSpotLitDiffuseBench(true); ) 217 DEF_BENCH( return new LightingSpotLitDiffuseBench(false); ) 218 DEF_BENCH( return new LightingPointLitSpecularBench(true); ) 219 DEF_BENCH( return new LightingPointLitSpecularBench(false); ) 220 DEF_BENCH( return new LightingDistantLitSpecularBench(true); ) 221 DEF_BENCH( return new LightingDistantLitSpecularBench(false); ) 222 DEF_BENCH( return new LightingSpotLitSpecularBench(true); ) 223 DEF_BENCH( return new LightingSpotLitSpecularBench(false); ) 224