2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkBitmapSource.h"
11 #include "SkLightingImageFilter.h"
13 #define FILTER_WIDTH_SMALL SkIntToScalar(32)
14 #define FILTER_HEIGHT_SMALL SkIntToScalar(32)
15 #define FILTER_WIDTH_LARGE SkIntToScalar(256)
16 #define FILTER_HEIGHT_LARGE SkIntToScalar(256)
18 class LightingBaseBench : public Benchmark {
20 LightingBaseBench(bool small) : fIsSmall(small) { }
23 void draw(const int loops, SkCanvas* canvas, SkImageFilter* imageFilter) const {
24 SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
25 SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
27 paint.setImageFilter(imageFilter)->unref();
28 for (int i = 0; i < loops; i++) {
29 canvas->drawRect(r, paint);
33 static SkPoint3 getPointLocation() {
34 static SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
38 static SkPoint3 getDistantDirection() {
39 static SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
40 static SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
41 static SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad),
42 SkScalarCos(elevationRad)),
43 SkScalarMul(SkScalarSin(azimuthRad),
44 SkScalarCos(elevationRad)),
45 SkScalarSin(elevationRad));
46 return distantDirection;
49 static SkPoint3 getSpotLocation() {
50 static SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
54 static SkPoint3 getSpotTarget() {
55 static SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
59 static SkScalar getSpotExponent() {
60 static SkScalar spotExponent = SK_Scalar1;
64 static SkScalar getCutoffAngle() {
65 static SkScalar cutoffAngle = SkIntToScalar(15);
69 static SkScalar getKd() {
70 static SkScalar kd = SkIntToScalar(2);
74 static SkScalar getKs() {
75 static SkScalar ks = SkIntToScalar(1);
79 static SkScalar getShininess() {
80 static SkScalar shininess = SkIntToScalar(8);
84 static SkScalar getSurfaceScale() {
85 static SkScalar surfaceScale = SkIntToScalar(1);
89 static SkColor getWhite() {
90 static SkColor white(0xFFFFFFFF);
95 typedef Benchmark INHERITED;
98 class LightingPointLitDiffuseBench : public LightingBaseBench {
100 LightingPointLitDiffuseBench(bool small) : INHERITED(small) {
104 const char* onGetName() SK_OVERRIDE {
105 return fIsSmall ? "lightingpointlitdiffuse_small" : "lightingpointlitdiffuse_large";
108 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
109 draw(loops, canvas, SkLightingImageFilter::CreatePointLitDiffuse(getPointLocation(),
116 typedef LightingBaseBench INHERITED;
119 class LightingDistantLitDiffuseBench : public LightingBaseBench {
121 LightingDistantLitDiffuseBench(bool small) : INHERITED(small) {
125 const char* onGetName() SK_OVERRIDE {
126 return fIsSmall ? "lightingdistantlitdiffuse_small" : "lightingdistantlitdiffuse_large";
129 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
130 draw(loops, canvas, SkLightingImageFilter::CreateDistantLitDiffuse(getDistantDirection(),
137 typedef LightingBaseBench INHERITED;
140 class LightingSpotLitDiffuseBench : public LightingBaseBench {
142 LightingSpotLitDiffuseBench(bool small) : INHERITED(small) {
146 const char* onGetName() SK_OVERRIDE {
147 return fIsSmall ? "lightingspotlitdiffuse_small" : "lightingspotlitdiffuse_large";
150 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
151 draw(loops, canvas, SkLightingImageFilter::CreateSpotLitDiffuse(getSpotLocation(),
161 typedef LightingBaseBench INHERITED;
164 class LightingPointLitSpecularBench : public LightingBaseBench {
166 LightingPointLitSpecularBench(bool small) : INHERITED(small) {
170 const char* onGetName() SK_OVERRIDE {
171 return fIsSmall ? "lightingpointlitspecular_small" : "lightingpointlitspecular_large";
174 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
175 draw(loops, canvas, SkLightingImageFilter::CreatePointLitSpecular(getPointLocation(),
183 typedef LightingBaseBench INHERITED;
186 class LightingDistantLitSpecularBench : public LightingBaseBench {
188 LightingDistantLitSpecularBench(bool small) : INHERITED(small) {
192 const char* onGetName() SK_OVERRIDE {
193 return fIsSmall ? "lightingdistantlitspecular_small" : "lightingdistantlitspecular_large";
196 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
197 draw(loops, canvas, SkLightingImageFilter::CreateDistantLitSpecular(getDistantDirection(),
205 typedef LightingBaseBench INHERITED;
208 class LightingSpotLitSpecularBench : public LightingBaseBench {
210 LightingSpotLitSpecularBench(bool small) : INHERITED(small) {
214 const char* onGetName() SK_OVERRIDE {
215 return fIsSmall ? "lightingspotlitspecular_small" : "lightingspotlitspecular_large";
218 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
219 draw(loops, canvas, SkLightingImageFilter::CreateSpotLitSpecular(getSpotLocation(),
230 typedef LightingBaseBench INHERITED;
233 ///////////////////////////////////////////////////////////////////////////////
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); )