blob: 13ac3671c9c71ee4dbf23f3d9dd4ae680ef88f85 [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"
John Reck1bcacfd2017-11-03 10:12:19 -070019#include "renderthread/RenderProxy.h"
20#include "renderthread/RenderTask.h"
Chris Craik27e58b42015-12-07 10:01:38 -080021#include "tests/common/TestContext.h"
22#include "tests/common/TestScene.h"
Chris Craik8160f202015-12-02 14:50:25 -080023#include "tests/common/scenes/TestSceneBase.h"
John Reck322b8ab2019-03-14 13:15:28 -070024#include "utils/TraceUtils.h"
John Reck16c9d6a2015-11-17 15:51:08 -080025
John Reckf1480762016-07-03 18:28:25 -070026#include <benchmark/benchmark.h>
John Reck16c9d6a2015-11-17 15:51:08 -080027#include <gui/Surface.h>
Mark Salyzyn96bf5982016-09-28 16:15:30 -070028#include <log/log.h>
John Reck16c9d6a2015-11-17 15:51:08 -080029#include <ui/PixelFormat.h>
30
31using namespace android;
32using namespace android::uirenderer;
33using namespace android::uirenderer::renderthread;
34using namespace android::uirenderer::test;
35
36class ContextFactory : public IContextFactory {
37public:
38 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override {
39 return new AnimationContext(clock);
40 }
41};
42
John Reck1bcacfd2017-11-03 10:12:19 -070043template <class T>
John Reck682573c2015-10-30 10:37:35 -070044class ModifiedMovingAverage {
45public:
Chih-Hung Hsiehd53e3be2016-05-03 10:02:51 -070046 explicit ModifiedMovingAverage(int weight) : mWeight(weight) {}
John Reck682573c2015-10-30 10:37:35 -070047
48 T add(T today) {
49 if (!mHasValue) {
50 mAverage = today;
51 } else {
52 mAverage = (((mWeight - 1) * mAverage) + today) / mWeight;
53 }
54 return mAverage;
55 }
56
John Reck1bcacfd2017-11-03 10:12:19 -070057 T average() { return mAverage; }
John Reck682573c2015-10-30 10:37:35 -070058
59private:
60 bool mHasValue = false;
61 int mWeight;
62 T mAverage;
63};
64
John Reckf1480762016-07-03 18:28:25 -070065void outputBenchmarkReport(const TestScene::Info& info, const TestScene::Options& opts,
John Reck1bcacfd2017-11-03 10:12:19 -070066 benchmark::BenchmarkReporter* reporter, RenderProxy* proxy,
67 double durationInS) {
John Reckf1480762016-07-03 18:28:25 -070068 using namespace benchmark;
69
70 struct ReportInfo {
71 int percentile;
72 const char* suffix;
73 };
74
75 static std::array<ReportInfo, 4> REPORTS = {
John Reck1bcacfd2017-11-03 10:12:19 -070076 ReportInfo{50, "_50th"}, ReportInfo{90, "_90th"}, ReportInfo{95, "_95th"},
77 ReportInfo{99, "_99th"},
John Reckf1480762016-07-03 18:28:25 -070078 };
79
80 // Although a vector is used, it must stay with only a single element
81 // otherwise the BenchmarkReporter will automatically compute
82 // mean and stddev which doesn't make sense for our usage
83 std::vector<BenchmarkReporter::Run> reports;
84 BenchmarkReporter::Run report;
Haibo Huang5eb2e962019-04-04 17:29:15 -070085 report.run_name.function_name = info.name;
John Reckf1480762016-07-03 18:28:25 -070086 report.iterations = static_cast<int64_t>(opts.count);
87 report.real_accumulated_time = durationInS;
88 report.cpu_accumulated_time = durationInS;
Elliott Hughesa06d5172018-10-30 10:12:57 -070089 report.counters["items_per_second"] = opts.count / durationInS;
John Reckf1480762016-07-03 18:28:25 -070090 reports.push_back(report);
91 reporter->ReportRuns(reports);
92
93 // Pretend the percentiles are single-iteration runs of the test
94 // If rendering offscreen skip this as it's fps that's more interesting
95 // in that test case than percentiles.
96 if (!opts.renderOffscreen) {
97 for (auto& ri : REPORTS) {
Haibo Huang5eb2e962019-04-04 17:29:15 -070098 reports[0].run_name.function_name = info.name;
99 reports[0].run_name.function_name += ri.suffix;
John Reckf1480762016-07-03 18:28:25 -0700100 durationInS = proxy->frameTimePercentile(ri.percentile) / 1000.0;
101 reports[0].real_accumulated_time = durationInS;
102 reports[0].cpu_accumulated_time = durationInS;
103 reports[0].iterations = 1;
Elliott Hughesa06d5172018-10-30 10:12:57 -0700104 reports[0].counters["items_per_second"] = 0;
John Reckf1480762016-07-03 18:28:25 -0700105 reporter->ReportRuns(reports);
106 }
107 }
108}
109
110void run(const TestScene::Info& info, const TestScene::Options& opts,
John Reck1bcacfd2017-11-03 10:12:19 -0700111 benchmark::BenchmarkReporter* reporter) {
sergeyva5c73e32016-09-14 17:49:41 -0700112 Properties::forceDrawFrame = true;
John Reck16c9d6a2015-11-17 15:51:08 -0800113 TestContext testContext;
John Reckf1480762016-07-03 18:28:25 -0700114 testContext.setRenderOffscreen(opts.renderOffscreen);
John Reck16c9d6a2015-11-17 15:51:08 -0800115
116 // create the native surface
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800117 const ui::Size& resolution = getActiveDisplayResolution();
118 const int width = resolution.getWidth();
119 const int height = resolution.getHeight();
John Reck16c9d6a2015-11-17 15:51:08 -0800120 sp<Surface> surface = testContext.surface();
121
John Recked024d22017-11-10 15:06:32 -0800122 std::unique_ptr<TestScene> scene(info.createScene(opts));
123 scene->renderTarget = surface;
124
John Reck1bcacfd2017-11-03 10:12:19 -0700125 sp<RenderNode> rootNode = TestUtils::createNode(
126 0, 0, width, height, [&scene, width, height](RenderProperties& props, Canvas& canvas) {
127 props.setClipToBounds(false);
128 scene->createContent(width, height, canvas);
129 });
John Reck16c9d6a2015-11-17 15:51:08 -0800130
131 ContextFactory factory;
John Reck1bcacfd2017-11-03 10:12:19 -0700132 std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode.get(), &factory));
John Reck16c9d6a2015-11-17 15:51:08 -0800133 proxy->loadSystemProperties();
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800134 proxy->setSurface(surface.get());
John Reck16c9d6a2015-11-17 15:51:08 -0800135 float lightX = width / 2.0;
John Reck8785ceb2018-10-29 16:45:58 -0700136 proxy->setLightAlpha(255 * 0.075, 255 * 0.15);
137 proxy->setLightGeometry((Vector3){lightX, dp(-200.0f), dp(800.0f)}, dp(800.0f));
John Reck16c9d6a2015-11-17 15:51:08 -0800138
139 // Do a few cold runs then reset the stats so that the caches are all hot
John Reckf1480762016-07-03 18:28:25 -0700140 int warmupFrameCount = 5;
141 if (opts.renderOffscreen) {
142 // Do a few more warmups to try and boost the clocks up
143 warmupFrameCount = 10;
144 }
145 for (int i = 0; i < warmupFrameCount; i++) {
John Reck16c9d6a2015-11-17 15:51:08 -0800146 testContext.waitForVsync();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100147 nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700148 UiFrameInfoBuilder(proxy->frameInfo())
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100149 .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID,
150 UiFrameInfoBuilder::UNKNOWN_DEADLINE,
151 UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL);
John Reck2de950d2017-01-25 10:58:30 -0800152 proxy->syncAndDrawFrame();
John Reck16c9d6a2015-11-17 15:51:08 -0800153 }
John Reck682573c2015-10-30 10:37:35 -0700154
John Reck16c9d6a2015-11-17 15:51:08 -0800155 proxy->resetProfileInfo();
John Reck682573c2015-10-30 10:37:35 -0700156 proxy->fence();
157
158 ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight);
John Reck16c9d6a2015-11-17 15:51:08 -0800159
Jerome Gaillarde218c692019-06-14 12:58:57 +0100160 nsecs_t start = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck16c9d6a2015-11-17 15:51:08 -0800161 for (int i = 0; i < opts.count; i++) {
162 testContext.waitForVsync();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100163 nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck682573c2015-10-30 10:37:35 -0700164 {
165 ATRACE_NAME("UI-Draw Frame");
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700166 UiFrameInfoBuilder(proxy->frameInfo())
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100167 .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID,
168 UiFrameInfoBuilder::UNKNOWN_DEADLINE,
169 UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL);
John Reck682573c2015-10-30 10:37:35 -0700170 scene->doFrame(i);
John Reck2de950d2017-01-25 10:58:30 -0800171 proxy->syncAndDrawFrame();
John Reck682573c2015-10-30 10:37:35 -0700172 }
John Reck682573c2015-10-30 10:37:35 -0700173 if (opts.reportFrametimeWeight) {
Chris Craik2705c982016-02-03 17:39:40 -0800174 proxy->fence();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100175 nsecs_t done = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck682573c2015-10-30 10:37:35 -0700176 avgMs.add((done - vsync) / 1000000.0);
177 if (i % 10 == 9) {
178 printf("Average frametime %.3fms\n", avgMs.average());
179 }
180 }
John Reck16c9d6a2015-11-17 15:51:08 -0800181 }
John Reckf1480762016-07-03 18:28:25 -0700182 proxy->fence();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100183 nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck16c9d6a2015-11-17 15:51:08 -0800184
John Reckf1480762016-07-03 18:28:25 -0700185 if (reporter) {
John Reck1bcacfd2017-11-03 10:12:19 -0700186 outputBenchmarkReport(info, opts, reporter, proxy.get(), (end - start) / (double)s2ns(1));
John Reckf1480762016-07-03 18:28:25 -0700187 } else {
188 proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats);
189 }
John Reck16c9d6a2015-11-17 15:51:08 -0800190}