1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <benchmark/benchmark.h>
18
19 #include "Matrix.h"
20 #include "Rect.h"
21 #include "TessellationCache.h"
22 #include "Vector.h"
23 #include "VertexBuffer.h"
24
25 #include <SkPath.h>
26
27 #include <memory>
28
29 using namespace android;
30 using namespace android::uirenderer;
31
32 struct ShadowTestData {
33 Matrix4 drawTransform;
34 Rect localClip;
35 Matrix4 casterTransformXY;
36 Matrix4 casterTransformZ;
37 Vector3 lightCenter;
38 float lightRadius;
39 };
40
createShadowTestData(ShadowTestData * out)41 void createShadowTestData(ShadowTestData* out) {
42 static float SAMPLE_DRAW_TRANSFORM[] = {
43 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
44 };
45 static float SAMPLE_CASTERXY[] = {
46 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 32, 32, 0, 1,
47 };
48 static float SAMPLE_CASTERZ[] = {
49 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 32, 32, 32, 1,
50 };
51 static Rect SAMPLE_CLIP(0, 0, 1536, 2048);
52 static Vector3 SAMPLE_LIGHT_CENTER{768, -400, 1600};
53 static float SAMPLE_LIGHT_RADIUS = 1600;
54
55 out->drawTransform.load(SAMPLE_DRAW_TRANSFORM);
56 out->localClip = SAMPLE_CLIP;
57 out->casterTransformXY.load(SAMPLE_CASTERXY);
58 out->casterTransformZ.load(SAMPLE_CASTERZ);
59 out->lightCenter = SAMPLE_LIGHT_CENTER;
60 out->lightRadius = SAMPLE_LIGHT_RADIUS;
61 }
62
tessellateShadows(ShadowTestData & testData,bool opaque,const SkPath & shape,VertexBuffer * ambient,VertexBuffer * spot)63 static inline void tessellateShadows(ShadowTestData& testData, bool opaque, const SkPath& shape,
64 VertexBuffer* ambient, VertexBuffer* spot) {
65 tessellateShadows(&testData.drawTransform, &testData.localClip, opaque, &shape,
66 &testData.casterTransformXY, &testData.casterTransformZ, testData.lightCenter,
67 testData.lightRadius, *ambient, *spot);
68 }
69
BM_TessellateShadows_roundrect_opaque(benchmark::State & state)70 void BM_TessellateShadows_roundrect_opaque(benchmark::State& state) {
71 ShadowTestData shadowData;
72 createShadowTestData(&shadowData);
73 SkPath path;
74 path.addRoundRect(SkRect::MakeWH(100, 100), 5, 5);
75
76 while (state.KeepRunning()) {
77 VertexBuffer ambient;
78 VertexBuffer spot;
79 tessellateShadows(shadowData, true, path, &ambient, &spot);
80 benchmark::DoNotOptimize(&ambient);
81 benchmark::DoNotOptimize(&spot);
82 }
83 }
84 BENCHMARK(BM_TessellateShadows_roundrect_opaque);
85
BM_TessellateShadows_roundrect_translucent(benchmark::State & state)86 void BM_TessellateShadows_roundrect_translucent(benchmark::State& state) {
87 ShadowTestData shadowData;
88 createShadowTestData(&shadowData);
89 SkPath path;
90 path.reset();
91 path.addRoundRect(SkRect::MakeLTRB(0, 0, 100, 100), 5, 5);
92
93 while (state.KeepRunning()) {
94 std::unique_ptr<VertexBuffer> ambient(new VertexBuffer);
95 std::unique_ptr<VertexBuffer> spot(new VertexBuffer);
96 tessellateShadows(shadowData, false, path, ambient.get(), spot.get());
97 benchmark::DoNotOptimize(ambient.get());
98 benchmark::DoNotOptimize(spot.get());
99 }
100 }
101 BENCHMARK(BM_TessellateShadows_roundrect_translucent);
102