1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2013 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci#include "bench/Benchmark.h" 8cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h" 9cb93a386Sopenharmony_ci#include "include/core/SkPoint3.h" 10cb93a386Sopenharmony_ci#include "include/effects/SkImageFilters.h" 11cb93a386Sopenharmony_ci 12cb93a386Sopenharmony_ci#define FILTER_WIDTH_SMALL SkIntToScalar(32) 13cb93a386Sopenharmony_ci#define FILTER_HEIGHT_SMALL SkIntToScalar(32) 14cb93a386Sopenharmony_ci#define FILTER_WIDTH_LARGE SkIntToScalar(256) 15cb93a386Sopenharmony_ci#define FILTER_HEIGHT_LARGE SkIntToScalar(256) 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ciclass LightingBaseBench : public Benchmark { 18cb93a386Sopenharmony_cipublic: 19cb93a386Sopenharmony_ci LightingBaseBench(bool small) : fIsSmall(small) { } 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_ciprotected: 22cb93a386Sopenharmony_ci void draw(int loops, SkCanvas* canvas, sk_sp<SkImageFilter> imageFilter) const { 23cb93a386Sopenharmony_ci SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) : 24cb93a386Sopenharmony_ci SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE); 25cb93a386Sopenharmony_ci SkPaint paint; 26cb93a386Sopenharmony_ci paint.setImageFilter(std::move(imageFilter)); 27cb93a386Sopenharmony_ci for (int i = 0; i < loops; i++) { 28cb93a386Sopenharmony_ci canvas->drawRect(r, paint); 29cb93a386Sopenharmony_ci } 30cb93a386Sopenharmony_ci } 31cb93a386Sopenharmony_ci 32cb93a386Sopenharmony_ci static SkPoint3 GetPointLocation() { 33cb93a386Sopenharmony_ci static SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10)); 34cb93a386Sopenharmony_ci return pointLocation; 35cb93a386Sopenharmony_ci } 36cb93a386Sopenharmony_ci 37cb93a386Sopenharmony_ci static SkPoint3 GetDistantDirection() { 38cb93a386Sopenharmony_ci static SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225)); 39cb93a386Sopenharmony_ci static SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5)); 40cb93a386Sopenharmony_ci static SkPoint3 distantDirection = SkPoint3::Make( 41cb93a386Sopenharmony_ci SkScalarCos(azimuthRad) * SkScalarCos(elevationRad), 42cb93a386Sopenharmony_ci SkScalarSin(azimuthRad) * SkScalarCos(elevationRad), 43cb93a386Sopenharmony_ci SkScalarSin(elevationRad)); 44cb93a386Sopenharmony_ci return distantDirection; 45cb93a386Sopenharmony_ci } 46cb93a386Sopenharmony_ci 47cb93a386Sopenharmony_ci static SkPoint3 GetSpotLocation() { 48cb93a386Sopenharmony_ci static SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10), 49cb93a386Sopenharmony_ci SkIntToScalar(-10), 50cb93a386Sopenharmony_ci SkIntToScalar(20)); 51cb93a386Sopenharmony_ci return spotLocation; 52cb93a386Sopenharmony_ci } 53cb93a386Sopenharmony_ci 54cb93a386Sopenharmony_ci static SkPoint3 GetSpotTarget() { 55cb93a386Sopenharmony_ci static SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0); 56cb93a386Sopenharmony_ci return spotTarget; 57cb93a386Sopenharmony_ci } 58cb93a386Sopenharmony_ci 59cb93a386Sopenharmony_ci static SkScalar GetSpotExponent() { 60cb93a386Sopenharmony_ci static SkScalar spotExponent = SK_Scalar1; 61cb93a386Sopenharmony_ci return spotExponent; 62cb93a386Sopenharmony_ci } 63cb93a386Sopenharmony_ci 64cb93a386Sopenharmony_ci static SkScalar GetCutoffAngle() { 65cb93a386Sopenharmony_ci static SkScalar cutoffAngle = SkIntToScalar(15); 66cb93a386Sopenharmony_ci return cutoffAngle; 67cb93a386Sopenharmony_ci } 68cb93a386Sopenharmony_ci 69cb93a386Sopenharmony_ci static SkScalar GetKd() { 70cb93a386Sopenharmony_ci static SkScalar kd = SkIntToScalar(2); 71cb93a386Sopenharmony_ci return kd; 72cb93a386Sopenharmony_ci } 73cb93a386Sopenharmony_ci 74cb93a386Sopenharmony_ci static SkScalar GetKs() { 75cb93a386Sopenharmony_ci static SkScalar ks = SkIntToScalar(1); 76cb93a386Sopenharmony_ci return ks; 77cb93a386Sopenharmony_ci } 78cb93a386Sopenharmony_ci 79cb93a386Sopenharmony_ci static SkScalar GetShininess() { 80cb93a386Sopenharmony_ci static SkScalar shininess = SkIntToScalar(8); 81cb93a386Sopenharmony_ci return shininess; 82cb93a386Sopenharmony_ci } 83cb93a386Sopenharmony_ci 84cb93a386Sopenharmony_ci static SkScalar GetSurfaceScale() { 85cb93a386Sopenharmony_ci static SkScalar surfaceScale = SkIntToScalar(1); 86cb93a386Sopenharmony_ci return surfaceScale; 87cb93a386Sopenharmony_ci } 88cb93a386Sopenharmony_ci 89cb93a386Sopenharmony_ci static SkColor GetWhite() { 90cb93a386Sopenharmony_ci static SkColor white(0xFFFFFFFF); 91cb93a386Sopenharmony_ci return white; 92cb93a386Sopenharmony_ci } 93cb93a386Sopenharmony_ci 94cb93a386Sopenharmony_ci bool fIsSmall; 95cb93a386Sopenharmony_ci using INHERITED = Benchmark; 96cb93a386Sopenharmony_ci}; 97cb93a386Sopenharmony_ci 98cb93a386Sopenharmony_ciclass LightingPointLitDiffuseBench : public LightingBaseBench { 99cb93a386Sopenharmony_cipublic: 100cb93a386Sopenharmony_ci LightingPointLitDiffuseBench(bool small) : INHERITED(small) { } 101cb93a386Sopenharmony_ci 102cb93a386Sopenharmony_ciprotected: 103cb93a386Sopenharmony_ci const char* onGetName() override { 104cb93a386Sopenharmony_ci return fIsSmall ? "lightingpointlitdiffuse_small" : "lightingpointlitdiffuse_large"; 105cb93a386Sopenharmony_ci } 106cb93a386Sopenharmony_ci 107cb93a386Sopenharmony_ci void onDraw(int loops, SkCanvas* canvas) override { 108cb93a386Sopenharmony_ci draw(loops, canvas, SkImageFilters::PointLitDiffuse( 109cb93a386Sopenharmony_ci GetPointLocation(), GetWhite(), GetSurfaceScale(), GetKd(), nullptr)); 110cb93a386Sopenharmony_ci } 111cb93a386Sopenharmony_ci 112cb93a386Sopenharmony_ciprivate: 113cb93a386Sopenharmony_ci using INHERITED = LightingBaseBench; 114cb93a386Sopenharmony_ci}; 115cb93a386Sopenharmony_ci 116cb93a386Sopenharmony_ciclass LightingDistantLitDiffuseBench : public LightingBaseBench { 117cb93a386Sopenharmony_cipublic: 118cb93a386Sopenharmony_ci LightingDistantLitDiffuseBench(bool small) : INHERITED(small) { } 119cb93a386Sopenharmony_ci 120cb93a386Sopenharmony_ciprotected: 121cb93a386Sopenharmony_ci const char* onGetName() override { 122cb93a386Sopenharmony_ci return fIsSmall ? "lightingdistantlitdiffuse_small" : "lightingdistantlitdiffuse_large"; 123cb93a386Sopenharmony_ci } 124cb93a386Sopenharmony_ci 125cb93a386Sopenharmony_ci void onDraw(int loops, SkCanvas* canvas) override { 126cb93a386Sopenharmony_ci draw(loops, canvas, SkImageFilters::DistantLitDiffuse( 127cb93a386Sopenharmony_ci GetDistantDirection(), GetWhite(), GetSurfaceScale(), GetKd(), nullptr)); 128cb93a386Sopenharmony_ci } 129cb93a386Sopenharmony_ci 130cb93a386Sopenharmony_ciprivate: 131cb93a386Sopenharmony_ci using INHERITED = LightingBaseBench; 132cb93a386Sopenharmony_ci}; 133cb93a386Sopenharmony_ci 134cb93a386Sopenharmony_ciclass LightingSpotLitDiffuseBench : public LightingBaseBench { 135cb93a386Sopenharmony_cipublic: 136cb93a386Sopenharmony_ci LightingSpotLitDiffuseBench(bool small) : INHERITED(small) { } 137cb93a386Sopenharmony_ci 138cb93a386Sopenharmony_ciprotected: 139cb93a386Sopenharmony_ci const char* onGetName() override { 140cb93a386Sopenharmony_ci return fIsSmall ? "lightingspotlitdiffuse_small" : "lightingspotlitdiffuse_large"; 141cb93a386Sopenharmony_ci } 142cb93a386Sopenharmony_ci 143cb93a386Sopenharmony_ci void onDraw(int loops, SkCanvas* canvas) override { 144cb93a386Sopenharmony_ci draw(loops, canvas, SkImageFilters::SpotLitDiffuse( 145cb93a386Sopenharmony_ci GetSpotLocation(), GetSpotTarget(), GetSpotExponent(), GetCutoffAngle(), 146cb93a386Sopenharmony_ci GetWhite(), GetSurfaceScale(), GetKd(), nullptr)); 147cb93a386Sopenharmony_ci } 148cb93a386Sopenharmony_ci 149cb93a386Sopenharmony_ciprivate: 150cb93a386Sopenharmony_ci using INHERITED = LightingBaseBench; 151cb93a386Sopenharmony_ci}; 152cb93a386Sopenharmony_ci 153cb93a386Sopenharmony_ciclass LightingPointLitSpecularBench : public LightingBaseBench { 154cb93a386Sopenharmony_cipublic: 155cb93a386Sopenharmony_ci LightingPointLitSpecularBench(bool small) : INHERITED(small) { } 156cb93a386Sopenharmony_ci 157cb93a386Sopenharmony_ciprotected: 158cb93a386Sopenharmony_ci const char* onGetName() override { 159cb93a386Sopenharmony_ci return fIsSmall ? "lightingpointlitspecular_small" : "lightingpointlitspecular_large"; 160cb93a386Sopenharmony_ci } 161cb93a386Sopenharmony_ci 162cb93a386Sopenharmony_ci void onDraw(int loops, SkCanvas* canvas) override { 163cb93a386Sopenharmony_ci draw(loops, canvas, SkImageFilters::PointLitSpecular( 164cb93a386Sopenharmony_ci GetPointLocation(), GetWhite(), GetSurfaceScale(), GetKs(), GetShininess(), 165cb93a386Sopenharmony_ci nullptr)); 166cb93a386Sopenharmony_ci } 167cb93a386Sopenharmony_ci 168cb93a386Sopenharmony_ciprivate: 169cb93a386Sopenharmony_ci using INHERITED = LightingBaseBench; 170cb93a386Sopenharmony_ci}; 171cb93a386Sopenharmony_ci 172cb93a386Sopenharmony_ciclass LightingDistantLitSpecularBench : public LightingBaseBench { 173cb93a386Sopenharmony_cipublic: 174cb93a386Sopenharmony_ci LightingDistantLitSpecularBench(bool small) : INHERITED(small) { } 175cb93a386Sopenharmony_ci 176cb93a386Sopenharmony_ciprotected: 177cb93a386Sopenharmony_ci const char* onGetName() override { 178cb93a386Sopenharmony_ci return fIsSmall ? "lightingdistantlitspecular_small" : "lightingdistantlitspecular_large"; 179cb93a386Sopenharmony_ci } 180cb93a386Sopenharmony_ci 181cb93a386Sopenharmony_ci void onDraw(int loops, SkCanvas* canvas) override { 182cb93a386Sopenharmony_ci draw(loops, canvas, SkImageFilters::DistantLitSpecular( 183cb93a386Sopenharmony_ci GetDistantDirection(), GetWhite(), GetSurfaceScale(), GetKs(), GetShininess(), 184cb93a386Sopenharmony_ci nullptr)); 185cb93a386Sopenharmony_ci } 186cb93a386Sopenharmony_ci 187cb93a386Sopenharmony_ciprivate: 188cb93a386Sopenharmony_ci using INHERITED = LightingBaseBench; 189cb93a386Sopenharmony_ci}; 190cb93a386Sopenharmony_ci 191cb93a386Sopenharmony_ciclass LightingSpotLitSpecularBench : public LightingBaseBench { 192cb93a386Sopenharmony_cipublic: 193cb93a386Sopenharmony_ci LightingSpotLitSpecularBench(bool small) : INHERITED(small) { } 194cb93a386Sopenharmony_ci 195cb93a386Sopenharmony_ciprotected: 196cb93a386Sopenharmony_ci const char* onGetName() override { 197cb93a386Sopenharmony_ci return fIsSmall ? "lightingspotlitspecular_small" : "lightingspotlitspecular_large"; 198cb93a386Sopenharmony_ci } 199cb93a386Sopenharmony_ci 200cb93a386Sopenharmony_ci void onDraw(int loops, SkCanvas* canvas) override { 201cb93a386Sopenharmony_ci draw(loops, canvas, SkImageFilters::SpotLitSpecular( 202cb93a386Sopenharmony_ci GetSpotLocation(), GetSpotTarget(), GetSpotExponent(), GetCutoffAngle(), 203cb93a386Sopenharmony_ci GetWhite(), GetSurfaceScale(), GetKs(), GetShininess(), nullptr)); 204cb93a386Sopenharmony_ci } 205cb93a386Sopenharmony_ci 206cb93a386Sopenharmony_ciprivate: 207cb93a386Sopenharmony_ci using INHERITED = LightingBaseBench; 208cb93a386Sopenharmony_ci}; 209cb93a386Sopenharmony_ci 210cb93a386Sopenharmony_ci/////////////////////////////////////////////////////////////////////////////// 211cb93a386Sopenharmony_ci 212cb93a386Sopenharmony_ciDEF_BENCH( return new LightingPointLitDiffuseBench(true); ) 213cb93a386Sopenharmony_ciDEF_BENCH( return new LightingPointLitDiffuseBench(false); ) 214cb93a386Sopenharmony_ciDEF_BENCH( return new LightingDistantLitDiffuseBench(true); ) 215cb93a386Sopenharmony_ciDEF_BENCH( return new LightingDistantLitDiffuseBench(false); ) 216cb93a386Sopenharmony_ciDEF_BENCH( return new LightingSpotLitDiffuseBench(true); ) 217cb93a386Sopenharmony_ciDEF_BENCH( return new LightingSpotLitDiffuseBench(false); ) 218cb93a386Sopenharmony_ciDEF_BENCH( return new LightingPointLitSpecularBench(true); ) 219cb93a386Sopenharmony_ciDEF_BENCH( return new LightingPointLitSpecularBench(false); ) 220cb93a386Sopenharmony_ciDEF_BENCH( return new LightingDistantLitSpecularBench(true); ) 221cb93a386Sopenharmony_ciDEF_BENCH( return new LightingDistantLitSpecularBench(false); ) 222cb93a386Sopenharmony_ciDEF_BENCH( return new LightingSpotLitSpecularBench(true); ) 223cb93a386Sopenharmony_ciDEF_BENCH( return new LightingSpotLitSpecularBench(false); ) 224