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 | |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 31 | // These are unstable internal APIs in google-benchmark. We should just implement our own variant |
| 32 | // of these instead, but this was quicker. Disabled-by-default to avoid any breakages when |
| 33 | // google-benchmark updates if they change anything |
| 34 | #if 0 |
| 35 | #define USE_SKETCHY_INTERNAL_STATS |
| 36 | namespace benchmark { |
| 37 | std::vector<BenchmarkReporter::Run> ComputeStats( |
| 38 | const std::vector<BenchmarkReporter::Run> &reports); |
| 39 | double StatisticsMean(const std::vector<double>& v); |
| 40 | double StatisticsMedian(const std::vector<double>& v); |
| 41 | double StatisticsStdDev(const std::vector<double>& v); |
| 42 | } |
| 43 | #endif |
| 44 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 45 | using namespace android; |
| 46 | using namespace android::uirenderer; |
| 47 | using namespace android::uirenderer::renderthread; |
| 48 | using namespace android::uirenderer::test; |
| 49 | |
| 50 | class ContextFactory : public IContextFactory { |
| 51 | public: |
| 52 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override { |
| 53 | return new AnimationContext(clock); |
| 54 | } |
| 55 | }; |
| 56 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 57 | template <class T> |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 58 | class ModifiedMovingAverage { |
| 59 | public: |
Chih-Hung Hsieh | d53e3be | 2016-05-03 10:02:51 -0700 | [diff] [blame] | 60 | explicit ModifiedMovingAverage(int weight) : mWeight(weight) {} |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 61 | |
| 62 | T add(T today) { |
| 63 | if (!mHasValue) { |
| 64 | mAverage = today; |
| 65 | } else { |
| 66 | mAverage = (((mWeight - 1) * mAverage) + today) / mWeight; |
| 67 | } |
| 68 | return mAverage; |
| 69 | } |
| 70 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 71 | T average() { return mAverage; } |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | bool mHasValue = false; |
| 75 | int mWeight; |
| 76 | T mAverage; |
| 77 | }; |
| 78 | |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 79 | using BenchmarkResults = std::vector<benchmark::BenchmarkReporter::Run>; |
| 80 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 81 | void outputBenchmarkReport(const TestScene::Info& info, const TestScene::Options& opts, |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 82 | double durationInS, int repetationIndex, BenchmarkResults* reports) { |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 83 | using namespace benchmark; |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 84 | benchmark::BenchmarkReporter::Run report; |
| 85 | report.repetitions = opts.repeatCount; |
| 86 | report.repetition_index = repetationIndex; |
Haibo Huang | 5eb2e96 | 2019-04-04 17:29:15 -0700 | [diff] [blame] | 87 | report.run_name.function_name = info.name; |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 88 | report.iterations = static_cast<int64_t>(opts.frameCount); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 89 | report.real_accumulated_time = durationInS; |
| 90 | report.cpu_accumulated_time = durationInS; |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 91 | report.counters["FPS"] = opts.frameCount / durationInS; |
| 92 | if (opts.reportGpuMemoryUsage) { |
| 93 | size_t cpuUsage, gpuUsage; |
| 94 | RenderProxy::getMemoryUsage(&cpuUsage, &gpuUsage); |
| 95 | report.counters["Rendering RAM"] = Counter{static_cast<double>(cpuUsage + gpuUsage), |
| 96 | Counter::kDefaults, Counter::kIs1024}; |
| 97 | } |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 98 | reports->push_back(report); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 99 | } |
| 100 | |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 101 | static void doRun(const TestScene::Info& info, const TestScene::Options& opts, int repetitionIndex, |
| 102 | BenchmarkResults* reports) { |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 103 | if (opts.reportGpuMemoryUsage) { |
| 104 | // If we're reporting GPU memory usage we need to first start with a clean slate |
| 105 | RenderProxy::purgeCaches(); |
| 106 | } |
sergeyv | a5c73e3 | 2016-09-14 17:49:41 -0700 | [diff] [blame] | 107 | Properties::forceDrawFrame = true; |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 108 | TestContext testContext; |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 109 | testContext.setRenderOffscreen(opts.renderOffscreen); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 110 | |
| 111 | // create the native surface |
Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 112 | const ui::Size& resolution = getActiveDisplayResolution(); |
| 113 | const int width = resolution.getWidth(); |
| 114 | const int height = resolution.getHeight(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 115 | sp<Surface> surface = testContext.surface(); |
| 116 | |
John Reck | ed024d2 | 2017-11-10 15:06:32 -0800 | [diff] [blame] | 117 | std::unique_ptr<TestScene> scene(info.createScene(opts)); |
| 118 | scene->renderTarget = surface; |
| 119 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 120 | sp<RenderNode> rootNode = TestUtils::createNode( |
| 121 | 0, 0, width, height, [&scene, width, height](RenderProperties& props, Canvas& canvas) { |
| 122 | props.setClipToBounds(false); |
| 123 | scene->createContent(width, height, canvas); |
| 124 | }); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 125 | |
| 126 | ContextFactory factory; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 127 | std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode.get(), &factory)); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 128 | proxy->loadSystemProperties(); |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 129 | proxy->setSurface(surface.get()); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 130 | float lightX = width / 2.0; |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 131 | proxy->setLightAlpha(255 * 0.075, 255 * 0.15); |
| 132 | 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] | 133 | |
| 134 | // 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] | 135 | int warmupFrameCount = 5; |
| 136 | if (opts.renderOffscreen) { |
| 137 | // Do a few more warmups to try and boost the clocks up |
| 138 | warmupFrameCount = 10; |
| 139 | } |
| 140 | for (int i = 0; i < warmupFrameCount; i++) { |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 141 | testContext.waitForVsync(); |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 142 | nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC); |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 143 | UiFrameInfoBuilder(proxy->frameInfo()) |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 144 | .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID, |
| 145 | UiFrameInfoBuilder::UNKNOWN_DEADLINE, |
| 146 | UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 147 | proxy->syncAndDrawFrame(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 148 | } |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 149 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 150 | proxy->resetProfileInfo(); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 151 | proxy->fence(); |
| 152 | |
| 153 | ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 154 | |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 155 | nsecs_t start = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 156 | for (int i = 0; i < opts.frameCount; i++) { |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 157 | testContext.waitForVsync(); |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 158 | nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 159 | { |
| 160 | ATRACE_NAME("UI-Draw Frame"); |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 161 | UiFrameInfoBuilder(proxy->frameInfo()) |
Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 162 | .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID, |
| 163 | UiFrameInfoBuilder::UNKNOWN_DEADLINE, |
| 164 | UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 165 | scene->doFrame(i); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 166 | proxy->syncAndDrawFrame(); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 167 | } |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 168 | if (opts.reportFrametimeWeight) { |
Chris Craik | 2705c98 | 2016-02-03 17:39:40 -0800 | [diff] [blame] | 169 | proxy->fence(); |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 170 | nsecs_t done = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 171 | avgMs.add((done - vsync) / 1000000.0); |
| 172 | if (i % 10 == 9) { |
| 173 | printf("Average frametime %.3fms\n", avgMs.average()); |
| 174 | } |
| 175 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 176 | } |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 177 | proxy->fence(); |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 178 | nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 179 | |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 180 | if (reports) { |
| 181 | outputBenchmarkReport(info, opts, (end - start) / (double)s2ns(1), repetitionIndex, |
| 182 | reports); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 183 | } else { |
| 184 | proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats); |
| 185 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 186 | } |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 187 | |
| 188 | void run(const TestScene::Info& info, const TestScene::Options& opts, |
| 189 | benchmark::BenchmarkReporter* reporter) { |
| 190 | BenchmarkResults results; |
| 191 | for (int i = 0; i < opts.repeatCount; i++) { |
| 192 | doRun(info, opts, i, reporter ? &results : nullptr); |
| 193 | } |
| 194 | if (reporter) { |
| 195 | reporter->ReportRuns(results); |
| 196 | if (results.size() > 1) { |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 197 | #ifdef USE_SKETCHY_INTERNAL_STATS |
| 198 | std::vector<benchmark::internal::Statistics> stats; |
| 199 | stats.reserve(3); |
| 200 | stats.emplace_back("mean", benchmark::StatisticsMean); |
| 201 | stats.emplace_back("median", benchmark::StatisticsMedian); |
| 202 | stats.emplace_back("stddev", benchmark::StatisticsStdDev); |
| 203 | for (auto& it : results) { |
| 204 | it.statistics = &stats; |
| 205 | } |
| 206 | auto summary = benchmark::ComputeStats(results); |
| 207 | reporter->ReportRuns(summary); |
| 208 | #endif |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 209 | } |
| 210 | } |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 211 | if (opts.reportGpuMemoryUsageVerbose) { |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame] | 212 | RenderProxy::dumpGraphicsMemory(STDOUT_FILENO, false); |
| 213 | } |
John Reck | a842780 | 2021-05-10 17:47:50 -0400 | [diff] [blame] | 214 | } |