ShadowBench.cpp revision 8160f20b0aca8c6595d4b385d673f59b6bcd16a4
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 "Vector.h"
22#include "VertexBuffer.h"
23#include "TessellationCache.h"
24#include "tests/microbench/MicroBench.h"
25
26#include <SkPath.h>
27
28#include <memory>
29
30using namespace android;
31using namespace android::uirenderer;
32
33struct ShadowTestData {
34    Matrix4 drawTransform;
35    Rect localClip;
36    Matrix4 casterTransformXY;
37    Matrix4 casterTransformZ;
38    Vector3 lightCenter;
39    float lightRadius;
40};
41
42void createShadowTestData(ShadowTestData* out) {
43    static float SAMPLE_DRAW_TRANSFORM[] = {
44            1, 0, 0, 0,
45            0, 1, 0, 0,
46            0, 0, 1, 0,
47            0, 0, 0, 1,
48    };
49    static float SAMPLE_CASTERXY[] = {
50            1, 0, 0, 0,
51            0, 1, 0, 0,
52            0, 0, 1, 0,
53            32, 32, 0, 1,
54    };
55    static float SAMPLE_CASTERZ[] = {
56            1, 0, 0, 0,
57            0, 1, 0, 0,
58            0, 0, 1, 0,
59            32, 32, 32, 1,
60    };
61    static Rect SAMPLE_CLIP(0, 0, 1536, 2048);
62    static Vector3 SAMPLE_LIGHT_CENTER{768, -400, 1600};
63    static float SAMPLE_LIGHT_RADIUS = 1600;
64
65    out->drawTransform.load(SAMPLE_DRAW_TRANSFORM);
66    out->localClip = SAMPLE_CLIP;
67    out->casterTransformXY.load(SAMPLE_CASTERXY);
68    out->casterTransformZ.load(SAMPLE_CASTERZ);
69    out->lightCenter = SAMPLE_LIGHT_CENTER;
70    out->lightRadius = SAMPLE_LIGHT_RADIUS;
71}
72
73static inline void tessellateShadows(ShadowTestData& testData, bool opaque,
74        const SkPath& shape, VertexBuffer* ambient, VertexBuffer* spot) {
75    tessellateShadows(&testData.drawTransform, &testData.localClip,
76            opaque, &shape, &testData.casterTransformXY,
77            &testData.casterTransformZ, testData.lightCenter,
78            testData.lightRadius, *ambient, *spot);
79}
80
81BENCHMARK_NO_ARG(BM_TessellateShadows_roundrect_opaque);
82void BM_TessellateShadows_roundrect_opaque::Run(int iters) {
83    ShadowTestData shadowData;
84    createShadowTestData(&shadowData);
85    SkPath path;
86    path.addRoundRect(SkRect::MakeWH(100, 100), 5, 5);
87
88    StartBenchmarkTiming();
89    for (int i = 0; i < iters; i++) {
90        VertexBuffer ambient;
91        VertexBuffer spot;
92        tessellateShadows(shadowData, true, path, &ambient, &spot);
93        MicroBench::DoNotOptimize(&ambient);
94        MicroBench::DoNotOptimize(&spot);
95    }
96    StopBenchmarkTiming();
97}
98
99BENCHMARK_NO_ARG(BM_TessellateShadows_roundrect_translucent);
100void BM_TessellateShadows_roundrect_translucent::Run(int iters) {
101    ShadowTestData shadowData;
102    createShadowTestData(&shadowData);
103    SkPath path;
104    path.reset();
105    path.addRoundRect(SkRect::MakeLTRB(0, 0, 100, 100), 5, 5);
106
107    StartBenchmarkTiming();
108    for (int i = 0; i < iters; i++) {
109        std::unique_ptr<VertexBuffer> ambient(new VertexBuffer);
110        std::unique_ptr<VertexBuffer> spot(new VertexBuffer);
111        tessellateShadows(shadowData, false, path, ambient.get(), spot.get());
112        MicroBench::DoNotOptimize(ambient.get());
113        MicroBench::DoNotOptimize(spot.get());
114    }
115    StopBenchmarkTiming();
116}
117