blob: cf9b0c5536fc2b64d3320e341d50906143354790 [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 Recka8427802021-05-10 17:47:50 -040065using BenchmarkResults = std::vector<benchmark::BenchmarkReporter::Run>;
66
John Reckf1480762016-07-03 18:28:25 -070067void outputBenchmarkReport(const TestScene::Info& info, const TestScene::Options& opts,
John Recka8427802021-05-10 17:47:50 -040068 double durationInS, int repetationIndex, BenchmarkResults* reports) {
69 benchmark::BenchmarkReporter::Run report;
70 report.repetitions = opts.repeatCount;
71 report.repetition_index = repetationIndex;
Haibo Huang5eb2e962019-04-04 17:29:15 -070072 report.run_name.function_name = info.name;
John Recka8427802021-05-10 17:47:50 -040073 report.iterations = static_cast<int64_t>(opts.frameCount);
John Reckf1480762016-07-03 18:28:25 -070074 report.real_accumulated_time = durationInS;
75 report.cpu_accumulated_time = durationInS;
John Recka8427802021-05-10 17:47:50 -040076 report.counters["items_per_second"] = opts.frameCount / durationInS;
77 reports->push_back(report);
John Reckf1480762016-07-03 18:28:25 -070078}
79
John Recka8427802021-05-10 17:47:50 -040080static void doRun(const TestScene::Info& info, const TestScene::Options& opts, int repetitionIndex,
81 BenchmarkResults* reports) {
sergeyva5c73e32016-09-14 17:49:41 -070082 Properties::forceDrawFrame = true;
John Reck16c9d6a2015-11-17 15:51:08 -080083 TestContext testContext;
John Reckf1480762016-07-03 18:28:25 -070084 testContext.setRenderOffscreen(opts.renderOffscreen);
John Reck16c9d6a2015-11-17 15:51:08 -080085
86 // create the native surface
Dominik Laskowski69b281d2019-11-22 14:13:12 -080087 const ui::Size& resolution = getActiveDisplayResolution();
88 const int width = resolution.getWidth();
89 const int height = resolution.getHeight();
John Reck16c9d6a2015-11-17 15:51:08 -080090 sp<Surface> surface = testContext.surface();
91
John Recked024d22017-11-10 15:06:32 -080092 std::unique_ptr<TestScene> scene(info.createScene(opts));
93 scene->renderTarget = surface;
94
John Reck1bcacfd2017-11-03 10:12:19 -070095 sp<RenderNode> rootNode = TestUtils::createNode(
96 0, 0, width, height, [&scene, width, height](RenderProperties& props, Canvas& canvas) {
97 props.setClipToBounds(false);
98 scene->createContent(width, height, canvas);
99 });
John Reck16c9d6a2015-11-17 15:51:08 -0800100
101 ContextFactory factory;
John Reck1bcacfd2017-11-03 10:12:19 -0700102 std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode.get(), &factory));
John Reck16c9d6a2015-11-17 15:51:08 -0800103 proxy->loadSystemProperties();
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800104 proxy->setSurface(surface.get());
John Reck16c9d6a2015-11-17 15:51:08 -0800105 float lightX = width / 2.0;
John Reck8785ceb2018-10-29 16:45:58 -0700106 proxy->setLightAlpha(255 * 0.075, 255 * 0.15);
107 proxy->setLightGeometry((Vector3){lightX, dp(-200.0f), dp(800.0f)}, dp(800.0f));
John Reck16c9d6a2015-11-17 15:51:08 -0800108
109 // Do a few cold runs then reset the stats so that the caches are all hot
John Reckf1480762016-07-03 18:28:25 -0700110 int warmupFrameCount = 5;
111 if (opts.renderOffscreen) {
112 // Do a few more warmups to try and boost the clocks up
113 warmupFrameCount = 10;
114 }
115 for (int i = 0; i < warmupFrameCount; i++) {
John Reck16c9d6a2015-11-17 15:51:08 -0800116 testContext.waitForVsync();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100117 nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700118 UiFrameInfoBuilder(proxy->frameInfo())
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100119 .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID,
120 UiFrameInfoBuilder::UNKNOWN_DEADLINE,
121 UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL);
John Reck2de950d2017-01-25 10:58:30 -0800122 proxy->syncAndDrawFrame();
John Reck16c9d6a2015-11-17 15:51:08 -0800123 }
John Reck682573c2015-10-30 10:37:35 -0700124
John Reck16c9d6a2015-11-17 15:51:08 -0800125 proxy->resetProfileInfo();
John Reck682573c2015-10-30 10:37:35 -0700126 proxy->fence();
127
128 ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight);
John Reck16c9d6a2015-11-17 15:51:08 -0800129
Jerome Gaillarde218c692019-06-14 12:58:57 +0100130 nsecs_t start = systemTime(SYSTEM_TIME_MONOTONIC);
John Recka8427802021-05-10 17:47:50 -0400131 for (int i = 0; i < opts.frameCount; i++) {
John Reck16c9d6a2015-11-17 15:51:08 -0800132 testContext.waitForVsync();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100133 nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck682573c2015-10-30 10:37:35 -0700134 {
135 ATRACE_NAME("UI-Draw Frame");
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700136 UiFrameInfoBuilder(proxy->frameInfo())
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100137 .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID,
138 UiFrameInfoBuilder::UNKNOWN_DEADLINE,
139 UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL);
John Reck682573c2015-10-30 10:37:35 -0700140 scene->doFrame(i);
John Reck2de950d2017-01-25 10:58:30 -0800141 proxy->syncAndDrawFrame();
John Reck682573c2015-10-30 10:37:35 -0700142 }
John Reck682573c2015-10-30 10:37:35 -0700143 if (opts.reportFrametimeWeight) {
Chris Craik2705c982016-02-03 17:39:40 -0800144 proxy->fence();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100145 nsecs_t done = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck682573c2015-10-30 10:37:35 -0700146 avgMs.add((done - vsync) / 1000000.0);
147 if (i % 10 == 9) {
148 printf("Average frametime %.3fms\n", avgMs.average());
149 }
150 }
John Reck16c9d6a2015-11-17 15:51:08 -0800151 }
John Reckf1480762016-07-03 18:28:25 -0700152 proxy->fence();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100153 nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC);
John Reck16c9d6a2015-11-17 15:51:08 -0800154
John Recka8427802021-05-10 17:47:50 -0400155 if (reports) {
156 outputBenchmarkReport(info, opts, (end - start) / (double)s2ns(1), repetitionIndex,
157 reports);
John Reckf1480762016-07-03 18:28:25 -0700158 } else {
159 proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats);
160 }
John Reck16c9d6a2015-11-17 15:51:08 -0800161}
John Recka8427802021-05-10 17:47:50 -0400162
163void run(const TestScene::Info& info, const TestScene::Options& opts,
164 benchmark::BenchmarkReporter* reporter) {
165 BenchmarkResults results;
166 for (int i = 0; i < opts.repeatCount; i++) {
167 doRun(info, opts, i, reporter ? &results : nullptr);
168 }
169 if (reporter) {
170 reporter->ReportRuns(results);
171 if (results.size() > 1) {
172 // TODO: Report summary
173 }
174 }
175}