John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 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 "AnimationContext.h" |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 18 | #include "RenderNode.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 19 | #include "renderthread/RenderProxy.h" |
| 20 | #include "renderthread/RenderTask.h" |
Chris Craik | 27e58b4 | 2015-12-07 10:01:38 -0800 | [diff] [blame] | 21 | #include "tests/common/TestContext.h" |
| 22 | #include "tests/common/TestScene.h" |
Chris Craik | 8160f20 | 2015-12-02 14:50:25 -0800 | [diff] [blame] | 23 | #include "tests/common/scenes/TestSceneBase.h" |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 24 | #include "utils/TraceUtils.h" |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 25 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 26 | #include <benchmark/benchmark.h> |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 27 | #include <gui/Surface.h> |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 29 | #include <ui/PixelFormat.h> |
| 30 | |
| 31 | using namespace android; |
| 32 | using namespace android::uirenderer; |
| 33 | using namespace android::uirenderer::renderthread; |
| 34 | using namespace android::uirenderer::test; |
| 35 | |
| 36 | class ContextFactory : public IContextFactory { |
| 37 | public: |
| 38 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override { |
| 39 | return new AnimationContext(clock); |
| 40 | } |
| 41 | }; |
| 42 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 43 | template <class T> |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 44 | class ModifiedMovingAverage { |
| 45 | public: |
Chih-Hung Hsieh | d53e3be | 2016-05-03 10:02:51 -0700 | [diff] [blame] | 46 | explicit ModifiedMovingAverage(int weight) : mWeight(weight) {} |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 47 | |
| 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 57 | T average() { return mAverage; } |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | bool mHasValue = false; |
| 61 | int mWeight; |
| 62 | T mAverage; |
| 63 | }; |
| 64 | |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 65 | using BenchmarkResults = std::vector<benchmark::BenchmarkReporter::Run>; |
| 66 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 67 | void outputBenchmarkReport(const TestScene::Info& info, const TestScene::Options& opts, |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 68 | double durationInS, int repetationIndex, BenchmarkResults* reports) { |
| 69 | benchmark::BenchmarkReporter::Run report; |
| 70 | report.repetitions = opts.repeatCount; |
| 71 | report.repetition_index = repetationIndex; |
Haibo Huang | 5eb2e96 | 2019-04-04 17:29:15 -0700 | [diff] [blame] | 72 | report.run_name.function_name = info.name; |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 73 | report.iterations = static_cast<int64_t>(opts.frameCount); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 74 | report.real_accumulated_time = durationInS; |
| 75 | report.cpu_accumulated_time = durationInS; |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 76 | report.counters["items_per_second"] = opts.frameCount / durationInS; |
| 77 | reports->push_back(report); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 78 | } |
| 79 | |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 80 | static void doRun(const TestScene::Info& info, const TestScene::Options& opts, int repetitionIndex, |
| 81 | BenchmarkResults* reports) { |
sergeyv | a5c73e3 | 2016-09-14 17:49:41 -0700 | [diff] [blame] | 82 | Properties::forceDrawFrame = true; |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 83 | TestContext testContext; |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 84 | testContext.setRenderOffscreen(opts.renderOffscreen); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 85 | |
| 86 | // create the native surface |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 87 | const ui::Size& resolution = getActiveDisplayResolution(); |
| 88 | const int width = resolution.getWidth(); |
| 89 | const int height = resolution.getHeight(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 90 | sp<Surface> surface = testContext.surface(); |
| 91 | |
John Reck | ed024d2 | 2017-11-10 15:06:32 -0800 | [diff] [blame] | 92 | std::unique_ptr<TestScene> scene(info.createScene(opts)); |
| 93 | scene->renderTarget = surface; |
| 94 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 95 | 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 Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 100 | |
| 101 | ContextFactory factory; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 102 | std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode.get(), &factory)); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 103 | proxy->loadSystemProperties(); |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 104 | proxy->setSurface(surface.get()); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 105 | float lightX = width / 2.0; |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 106 | proxy->setLightAlpha(255 * 0.075, 255 * 0.15); |
| 107 | proxy->setLightGeometry((Vector3){lightX, dp(-200.0f), dp(800.0f)}, dp(800.0f)); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 108 | |
| 109 | // Do a few cold runs then reset the stats so that the caches are all hot |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 110 | 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 Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 116 | testContext.waitForVsync(); |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 117 | nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC); |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 118 | UiFrameInfoBuilder(proxy->frameInfo()) |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 119 | .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID, |
| 120 | UiFrameInfoBuilder::UNKNOWN_DEADLINE, |
| 121 | UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 122 | proxy->syncAndDrawFrame(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 123 | } |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 124 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 125 | proxy->resetProfileInfo(); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 126 | proxy->fence(); |
| 127 | |
| 128 | ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 129 | |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 130 | nsecs_t start = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 131 | for (int i = 0; i < opts.frameCount; i++) { |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 132 | testContext.waitForVsync(); |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 133 | nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 134 | { |
| 135 | ATRACE_NAME("UI-Draw Frame"); |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 136 | UiFrameInfoBuilder(proxy->frameInfo()) |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 137 | .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID, |
| 138 | UiFrameInfoBuilder::UNKNOWN_DEADLINE, |
| 139 | UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 140 | scene->doFrame(i); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 141 | proxy->syncAndDrawFrame(); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 142 | } |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 143 | if (opts.reportFrametimeWeight) { |
Chris Craik | 2705c98 | 2016-02-03 17:39:40 -0800 | [diff] [blame] | 144 | proxy->fence(); |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 145 | nsecs_t done = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 146 | avgMs.add((done - vsync) / 1000000.0); |
| 147 | if (i % 10 == 9) { |
| 148 | printf("Average frametime %.3fms\n", avgMs.average()); |
| 149 | } |
| 150 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 151 | } |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 152 | proxy->fence(); |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 153 | nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 154 | |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 155 | if (reports) { |
| 156 | outputBenchmarkReport(info, opts, (end - start) / (double)s2ns(1), repetitionIndex, |
| 157 | reports); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 158 | } else { |
| 159 | proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats); |
| 160 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 161 | } |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 162 | |
| 163 | void run(const TestScene::Info& info, const TestScene::Options& opts, |
| 164 | benchmark::BenchmarkReporter* reporter) { |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame^] | 165 | if (opts.reportGpuMemoryUsage) { |
| 166 | // If we're reporting GPU memory usage we need to first start with a clean slate |
| 167 | // All repetitions of the same test will share a single memory usage report |
| 168 | RenderProxy::trimMemory(100); |
| 169 | } |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 170 | BenchmarkResults results; |
| 171 | for (int i = 0; i < opts.repeatCount; i++) { |
| 172 | doRun(info, opts, i, reporter ? &results : nullptr); |
| 173 | } |
| 174 | if (reporter) { |
| 175 | reporter->ReportRuns(results); |
| 176 | if (results.size() > 1) { |
| 177 | // TODO: Report summary |
| 178 | } |
| 179 | } |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame^] | 180 | if (opts.reportGpuMemoryUsage) { |
| 181 | RenderProxy::dumpGraphicsMemory(STDOUT_FILENO, false); |
| 182 | } |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 183 | } |