blob: f03dcbf4c24c31f32a85911d13f6347c67774e2e [file] [log] [blame]
John Reck16c9d6a2015-11-17 15:51:08 -08001/*
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 "AnimationContext.h"
John Reck16c9d6a2015-11-17 15:51:08 -080018#include "RenderNode.h"
Chris Craik27e58b42015-12-07 10:01:38 -080019#include "tests/common/TestContext.h"
20#include "tests/common/TestScene.h"
Chris Craik8160f202015-12-02 14:50:25 -080021#include "tests/common/scenes/TestSceneBase.h"
John Reck16c9d6a2015-11-17 15:51:08 -080022#include "renderthread/RenderProxy.h"
23#include "renderthread/RenderTask.h"
24
John Reckf1480762016-07-03 18:28:25 -070025#include <benchmark/benchmark.h>
John Reck16c9d6a2015-11-17 15:51:08 -080026#include <cutils/log.h>
27#include <gui/Surface.h>
28#include <ui/PixelFormat.h>
29
30using namespace android;
31using namespace android::uirenderer;
32using namespace android::uirenderer::renderthread;
33using namespace android::uirenderer::test;
34
35class ContextFactory : public IContextFactory {
36public:
37 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override {
38 return new AnimationContext(clock);
39 }
40};
41
John Reck682573c2015-10-30 10:37:35 -070042template<class T>
43class ModifiedMovingAverage {
44public:
Chih-Hung Hsiehd53e3be2016-05-03 10:02:51 -070045 explicit ModifiedMovingAverage(int weight) : mWeight(weight) {}
John Reck682573c2015-10-30 10:37:35 -070046
47 T add(T today) {
48 if (!mHasValue) {
49 mAverage = today;
50 } else {
51 mAverage = (((mWeight - 1) * mAverage) + today) / mWeight;
52 }
53 return mAverage;
54 }
55
56 T average() {
57 return mAverage;
58 }
59
60private:
61 bool mHasValue = false;
62 int mWeight;
63 T mAverage;
64};
65
John Reckf1480762016-07-03 18:28:25 -070066void outputBenchmarkReport(const TestScene::Info& info, const TestScene::Options& opts,
67 benchmark::BenchmarkReporter* reporter, RenderProxy* proxy,
68 double durationInS) {
69 using namespace benchmark;
70
71 struct ReportInfo {
72 int percentile;
73 const char* suffix;
74 };
75
76 static std::array<ReportInfo, 4> REPORTS = {
77 ReportInfo { 50, "_50th" },
78 ReportInfo { 90, "_90th" },
79 ReportInfo { 95, "_95th" },
80 ReportInfo { 99, "_99th" },
81 };
82
83 // Although a vector is used, it must stay with only a single element
84 // otherwise the BenchmarkReporter will automatically compute
85 // mean and stddev which doesn't make sense for our usage
86 std::vector<BenchmarkReporter::Run> reports;
87 BenchmarkReporter::Run report;
88 report.benchmark_name = info.name;
89 report.iterations = static_cast<int64_t>(opts.count);
90 report.real_accumulated_time = durationInS;
91 report.cpu_accumulated_time = durationInS;
92 report.items_per_second = opts.count / durationInS;
93 reports.push_back(report);
94 reporter->ReportRuns(reports);
95
96 // Pretend the percentiles are single-iteration runs of the test
97 // If rendering offscreen skip this as it's fps that's more interesting
98 // in that test case than percentiles.
99 if (!opts.renderOffscreen) {
100 for (auto& ri : REPORTS) {
101 reports[0].benchmark_name = info.name;
102 reports[0].benchmark_name += ri.suffix;
103 durationInS = proxy->frameTimePercentile(ri.percentile) / 1000.0;
104 reports[0].real_accumulated_time = durationInS;
105 reports[0].cpu_accumulated_time = durationInS;
106 reports[0].iterations = 1;
107 reports[0].items_per_second = 0;
108 reporter->ReportRuns(reports);
109 }
110 }
111}
112
113void run(const TestScene::Info& info, const TestScene::Options& opts,
114 benchmark::BenchmarkReporter* reporter) {
John Reck16c9d6a2015-11-17 15:51:08 -0800115 // Switch to the real display
116 gDisplay = getBuiltInDisplay();
117
118 std::unique_ptr<TestScene> scene(info.createScene(opts));
119
120 TestContext testContext;
John Reckf1480762016-07-03 18:28:25 -0700121 testContext.setRenderOffscreen(opts.renderOffscreen);
John Reck16c9d6a2015-11-17 15:51:08 -0800122
123 // create the native surface
124 const int width = gDisplay.w;
125 const int height = gDisplay.h;
126 sp<Surface> surface = testContext.surface();
127
128 sp<RenderNode> rootNode = TestUtils::createNode(0, 0, width, height,
129 [&scene, width, height](RenderProperties& props, TestCanvas& canvas) {
130 props.setClipToBounds(false);
131 scene->createContent(width, height, canvas);
132 });
133
134 ContextFactory factory;
135 std::unique_ptr<RenderProxy> proxy(new RenderProxy(false,
136 rootNode.get(), &factory));
137 proxy->loadSystemProperties();
138 proxy->initialize(surface);
139 float lightX = width / 2.0;
John Reckab1080c2016-06-21 16:24:20 -0700140 proxy->setup(dp(800.0f), 255 * 0.075, 255 * 0.15);
John Reck16c9d6a2015-11-17 15:51:08 -0800141 proxy->setLightCenter((Vector3){lightX, dp(-200.0f), dp(800.0f)});
142
143 // Do a few cold runs then reset the stats so that the caches are all hot
John Reckf1480762016-07-03 18:28:25 -0700144 int warmupFrameCount = 5;
145 if (opts.renderOffscreen) {
146 // Do a few more warmups to try and boost the clocks up
147 warmupFrameCount = 10;
148 }
149 for (int i = 0; i < warmupFrameCount; i++) {
John Reck16c9d6a2015-11-17 15:51:08 -0800150 testContext.waitForVsync();
151 nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
152 UiFrameInfoBuilder(proxy->frameInfo()).setVsync(vsync, vsync);
John Reck51f2d602016-04-06 07:50:47 -0700153 proxy->syncAndDrawFrame(nullptr);
John Reck16c9d6a2015-11-17 15:51:08 -0800154 }
John Reck682573c2015-10-30 10:37:35 -0700155
John Reck16c9d6a2015-11-17 15:51:08 -0800156 proxy->resetProfileInfo();
John Reck682573c2015-10-30 10:37:35 -0700157 proxy->fence();
158
159 ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight);
John Reck16c9d6a2015-11-17 15:51:08 -0800160
John Reckf1480762016-07-03 18:28:25 -0700161 nsecs_t start = systemTime(CLOCK_MONOTONIC);
John Reck16c9d6a2015-11-17 15:51:08 -0800162 for (int i = 0; i < opts.count; i++) {
163 testContext.waitForVsync();
John Reck16c9d6a2015-11-17 15:51:08 -0800164 nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
John Reck682573c2015-10-30 10:37:35 -0700165 {
166 ATRACE_NAME("UI-Draw Frame");
167 UiFrameInfoBuilder(proxy->frameInfo()).setVsync(vsync, vsync);
168 scene->doFrame(i);
John Reck51f2d602016-04-06 07:50:47 -0700169 proxy->syncAndDrawFrame(nullptr);
John Reck682573c2015-10-30 10:37:35 -0700170 }
John Reck682573c2015-10-30 10:37:35 -0700171 if (opts.reportFrametimeWeight) {
Chris Craik2705c982016-02-03 17:39:40 -0800172 proxy->fence();
173 nsecs_t done = systemTime(CLOCK_MONOTONIC);
John Reck682573c2015-10-30 10:37:35 -0700174 avgMs.add((done - vsync) / 1000000.0);
175 if (i % 10 == 9) {
176 printf("Average frametime %.3fms\n", avgMs.average());
177 }
178 }
John Reck16c9d6a2015-11-17 15:51:08 -0800179 }
John Reckf1480762016-07-03 18:28:25 -0700180 proxy->fence();
181 nsecs_t end = systemTime(CLOCK_MONOTONIC);
John Reck16c9d6a2015-11-17 15:51:08 -0800182
John Reckf1480762016-07-03 18:28:25 -0700183 if (reporter) {
184 outputBenchmarkReport(info, opts, reporter, proxy.get(),
185 (end - start) / (double) s2ns(1));
186 } else {
187 proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats);
188 }
John Reck16c9d6a2015-11-17 15:51:08 -0800189}