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