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 | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 24 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 25 | #include <benchmark/benchmark.h> |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 26 | #include <gui/Surface.h> |
Mark Salyzyn | 96bf598 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 27 | #include <log/log.h> |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 28 | #include <ui/PixelFormat.h> |
| 29 | |
| 30 | using namespace android; |
| 31 | using namespace android::uirenderer; |
| 32 | using namespace android::uirenderer::renderthread; |
| 33 | using namespace android::uirenderer::test; |
| 34 | |
| 35 | class ContextFactory : public IContextFactory { |
| 36 | public: |
| 37 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override { |
| 38 | return new AnimationContext(clock); |
| 39 | } |
| 40 | }; |
| 41 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 42 | template <class T> |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 43 | class ModifiedMovingAverage { |
| 44 | public: |
Chih-Hung Hsieh | d53e3be | 2016-05-03 10:02:51 -0700 | [diff] [blame] | 45 | explicit ModifiedMovingAverage(int weight) : mWeight(weight) {} |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 46 | |
| 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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 56 | T average() { return mAverage; } |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | bool mHasValue = false; |
| 60 | int mWeight; |
| 61 | T mAverage; |
| 62 | }; |
| 63 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 64 | void outputBenchmarkReport(const TestScene::Info& info, const TestScene::Options& opts, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 65 | benchmark::BenchmarkReporter* reporter, RenderProxy* proxy, |
| 66 | double durationInS) { |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 67 | using namespace benchmark; |
| 68 | |
| 69 | struct ReportInfo { |
| 70 | int percentile; |
| 71 | const char* suffix; |
| 72 | }; |
| 73 | |
| 74 | static std::array<ReportInfo, 4> REPORTS = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 75 | ReportInfo{50, "_50th"}, ReportInfo{90, "_90th"}, ReportInfo{95, "_95th"}, |
| 76 | ReportInfo{99, "_99th"}, |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | // Although a vector is used, it must stay with only a single element |
| 80 | // otherwise the BenchmarkReporter will automatically compute |
| 81 | // mean and stddev which doesn't make sense for our usage |
| 82 | std::vector<BenchmarkReporter::Run> reports; |
| 83 | BenchmarkReporter::Run report; |
Haibo Huang | 5eb2e96 | 2019-04-04 17:29:15 -0700 | [diff] [blame^] | 84 | report.run_name.function_name = info.name; |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 85 | report.iterations = static_cast<int64_t>(opts.count); |
| 86 | report.real_accumulated_time = durationInS; |
| 87 | report.cpu_accumulated_time = durationInS; |
Elliott Hughes | a06d517 | 2018-10-30 10:12:57 -0700 | [diff] [blame] | 88 | report.counters["items_per_second"] = opts.count / durationInS; |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 89 | reports.push_back(report); |
| 90 | reporter->ReportRuns(reports); |
| 91 | |
| 92 | // Pretend the percentiles are single-iteration runs of the test |
| 93 | // If rendering offscreen skip this as it's fps that's more interesting |
| 94 | // in that test case than percentiles. |
| 95 | if (!opts.renderOffscreen) { |
| 96 | for (auto& ri : REPORTS) { |
Haibo Huang | 5eb2e96 | 2019-04-04 17:29:15 -0700 | [diff] [blame^] | 97 | reports[0].run_name.function_name = info.name; |
| 98 | reports[0].run_name.function_name += ri.suffix; |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 99 | durationInS = proxy->frameTimePercentile(ri.percentile) / 1000.0; |
| 100 | reports[0].real_accumulated_time = durationInS; |
| 101 | reports[0].cpu_accumulated_time = durationInS; |
| 102 | reports[0].iterations = 1; |
Elliott Hughes | a06d517 | 2018-10-30 10:12:57 -0700 | [diff] [blame] | 103 | reports[0].counters["items_per_second"] = 0; |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 104 | reporter->ReportRuns(reports); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void run(const TestScene::Info& info, const TestScene::Options& opts, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 110 | benchmark::BenchmarkReporter* reporter) { |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 111 | // Switch to the real display |
| 112 | gDisplay = getBuiltInDisplay(); |
| 113 | |
sergeyv | a5c73e3 | 2016-09-14 17:49:41 -0700 | [diff] [blame] | 114 | Properties::forceDrawFrame = true; |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 115 | TestContext testContext; |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 116 | testContext.setRenderOffscreen(opts.renderOffscreen); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 117 | |
| 118 | // create the native surface |
| 119 | const int width = gDisplay.w; |
| 120 | const int height = gDisplay.h; |
| 121 | sp<Surface> surface = testContext.surface(); |
| 122 | |
John Reck | ed024d2 | 2017-11-10 15:06:32 -0800 | [diff] [blame] | 123 | std::unique_ptr<TestScene> scene(info.createScene(opts)); |
| 124 | scene->renderTarget = surface; |
| 125 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 126 | sp<RenderNode> rootNode = TestUtils::createNode( |
| 127 | 0, 0, width, height, [&scene, width, height](RenderProperties& props, Canvas& canvas) { |
| 128 | props.setClipToBounds(false); |
| 129 | scene->createContent(width, height, canvas); |
| 130 | }); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 131 | |
| 132 | ContextFactory factory; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 133 | std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode.get(), &factory)); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 134 | proxy->loadSystemProperties(); |
| 135 | proxy->initialize(surface); |
| 136 | float lightX = width / 2.0; |
John Reck | ab1080c | 2016-06-21 16:24:20 -0700 | [diff] [blame] | 137 | proxy->setup(dp(800.0f), 255 * 0.075, 255 * 0.15); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 138 | proxy->setLightCenter((Vector3){lightX, dp(-200.0f), dp(800.0f)}); |
| 139 | |
| 140 | // 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] | 141 | int warmupFrameCount = 5; |
| 142 | if (opts.renderOffscreen) { |
| 143 | // Do a few more warmups to try and boost the clocks up |
| 144 | warmupFrameCount = 10; |
| 145 | } |
| 146 | for (int i = 0; i < warmupFrameCount; i++) { |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 147 | testContext.waitForVsync(); |
| 148 | nsecs_t vsync = systemTime(CLOCK_MONOTONIC); |
| 149 | UiFrameInfoBuilder(proxy->frameInfo()).setVsync(vsync, vsync); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 150 | proxy->syncAndDrawFrame(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 151 | } |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 152 | |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 153 | proxy->resetProfileInfo(); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 154 | proxy->fence(); |
| 155 | |
| 156 | ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 157 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 158 | nsecs_t start = systemTime(CLOCK_MONOTONIC); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 159 | for (int i = 0; i < opts.count; i++) { |
| 160 | testContext.waitForVsync(); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 161 | nsecs_t vsync = systemTime(CLOCK_MONOTONIC); |
John Reck | 682573c | 2015-10-30 10:37:35 -0700 | [diff] [blame] | 162 | { |
| 163 | ATRACE_NAME("UI-Draw Frame"); |
| 164 | UiFrameInfoBuilder(proxy->frameInfo()).setVsync(vsync, vsync); |
| 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(); |
| 170 | nsecs_t done = systemTime(CLOCK_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(); |
| 178 | nsecs_t end = systemTime(CLOCK_MONOTONIC); |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 179 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 180 | if (reporter) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 181 | outputBenchmarkReport(info, opts, reporter, proxy.get(), (end - start) / (double)s2ns(1)); |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 182 | } else { |
| 183 | proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats); |
| 184 | } |
John Reck | 16c9d6a | 2015-11-17 15:51:08 -0800 | [diff] [blame] | 185 | } |