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 <gui/TraceUtils.h>
18 #include "AnimationContext.h"
19 #include "RenderNode.h"
20 #include "renderthread/RenderProxy.h"
21 #include "renderthread/RenderTask.h"
22 #include "tests/common/TestContext.h"
23 #include "tests/common/TestScene.h"
24 #include "tests/common/scenes/TestSceneBase.h"
25
26 #include <benchmark/benchmark.h>
27 #include <gui/Surface.h>
28 #include <log/log.h>
29 #include <ui/PixelFormat.h>
30
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
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:
createAnimationContext(renderthread::TimeLord & clock)52 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override {
53 return new AnimationContext(clock);
54 }
55 };
56
57 template <class T>
58 class ModifiedMovingAverage {
59 public:
ModifiedMovingAverage(int weight)60 explicit ModifiedMovingAverage(int weight) : mWeight(weight) {}
61
add(T today)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
average()71 T average() { return mAverage; }
72
73 private:
74 bool mHasValue = false;
75 int mWeight;
76 T mAverage;
77 };
78
79 using BenchmarkResults = std::vector<benchmark::BenchmarkReporter::Run>;
80
outputBenchmarkReport(const TestScene::Info & info,const TestScene::Options & opts,double durationInS,int repetationIndex,BenchmarkResults * reports)81 void outputBenchmarkReport(const TestScene::Info& info, const TestScene::Options& opts,
82 double durationInS, int repetationIndex, BenchmarkResults* reports) {
83 using namespace benchmark;
84 benchmark::BenchmarkReporter::Run report;
85 report.repetitions = opts.repeatCount;
86 report.repetition_index = repetationIndex;
87 report.run_name.function_name = info.name;
88 report.iterations = static_cast<int64_t>(opts.frameCount);
89 report.real_accumulated_time = durationInS;
90 report.cpu_accumulated_time = durationInS;
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 }
98 reports->push_back(report);
99 }
100
doRun(const TestScene::Info & info,const TestScene::Options & opts,int repetitionIndex,BenchmarkResults * reports)101 static void doRun(const TestScene::Info& info, const TestScene::Options& opts, int repetitionIndex,
102 BenchmarkResults* reports) {
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 }
107 Properties::forceDrawFrame = true;
108 TestContext testContext;
109 testContext.setRenderOffscreen(opts.renderOffscreen);
110
111 // create the native surface
112 const ui::Size& resolution = getActiveDisplayResolution();
113 const int width = resolution.getWidth();
114 const int height = resolution.getHeight();
115 sp<Surface> surface = testContext.surface();
116
117 std::unique_ptr<TestScene> scene(info.createScene(opts));
118 scene->renderTarget = surface;
119
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 });
125
126 ContextFactory factory;
127 std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode.get(), &factory));
128 proxy->loadSystemProperties();
129 proxy->setSurface(surface.get());
130 float lightX = width / 2.0;
131 proxy->setLightAlpha(255 * 0.075, 255 * 0.15);
132 proxy->setLightGeometry((Vector3){lightX, dp(-200.0f), dp(800.0f)}, dp(800.0f));
133
134 // Do a few cold runs then reset the stats so that the caches are all hot
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++) {
141 testContext.waitForVsync();
142 nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
143 UiFrameInfoBuilder(proxy->frameInfo())
144 .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID,
145 UiFrameInfoBuilder::UNKNOWN_DEADLINE,
146 UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL);
147 proxy->syncAndDrawFrame();
148 }
149
150 proxy->resetProfileInfo();
151 proxy->fence();
152
153 ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight);
154
155 nsecs_t start = systemTime(SYSTEM_TIME_MONOTONIC);
156 for (int i = 0; i < opts.frameCount; i++) {
157 testContext.waitForVsync();
158 nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
159 {
160 ATRACE_NAME("UI-Draw Frame");
161 UiFrameInfoBuilder(proxy->frameInfo())
162 .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID,
163 UiFrameInfoBuilder::UNKNOWN_DEADLINE,
164 UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL);
165 scene->doFrame(i);
166 proxy->syncAndDrawFrame();
167 }
168 if (opts.reportFrametimeWeight) {
169 proxy->fence();
170 nsecs_t done = systemTime(SYSTEM_TIME_MONOTONIC);
171 avgMs.add((done - vsync) / 1000000.0);
172 if (i % 10 == 9) {
173 printf("Average frametime %.3fms\n", avgMs.average());
174 }
175 }
176 }
177 proxy->fence();
178 nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC);
179
180 if (reports) {
181 outputBenchmarkReport(info, opts, (end - start) / (double)s2ns(1), repetitionIndex,
182 reports);
183 } else {
184 proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats);
185 }
186 }
187
run(const TestScene::Info & info,const TestScene::Options & opts,benchmark::BenchmarkReporter * reporter)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) {
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
209 }
210 }
211 if (opts.reportGpuMemoryUsageVerbose) {
212 RenderProxy::dumpGraphicsMemory(STDOUT_FILENO, false);
213 }
214 }
215