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 TestContext testContext;
108 testContext.setRenderOffscreen(opts.renderOffscreen);
109
110 // create the native surface
111 const ui::Size& resolution = getActiveDisplayResolution();
112 const int width = resolution.getWidth();
113 const int height = resolution.getHeight();
114 sp<Surface> surface = testContext.surface();
115
116 std::unique_ptr<TestScene> scene(info.createScene(opts));
117 scene->renderTarget = surface;
118
119 sp<RenderNode> rootNode = TestUtils::createNode(
120 0, 0, width, height, [&scene, width, height](RenderProperties& props, Canvas& canvas) {
121 props.setClipToBounds(false);
122 scene->createContent(width, height, canvas);
123 });
124
125 ContextFactory factory;
126 std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode.get(), &factory));
127 proxy->loadSystemProperties();
128 proxy->setSurface(surface.get());
129 float lightX = width / 2.0;
130 proxy->setLightAlpha(255 * 0.075, 255 * 0.15);
131 proxy->setLightGeometry((Vector3){lightX, dp(-200.0f), dp(800.0f)}, dp(800.0f));
132
133 // Do a few cold runs then reset the stats so that the caches are all hot
134 int warmupFrameCount = 5;
135 if (opts.renderOffscreen) {
136 // Do a few more warmups to try and boost the clocks up
137 warmupFrameCount = 10;
138 }
139 for (int i = 0; i < warmupFrameCount; i++) {
140 testContext.waitForVsync();
141 nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
142 UiFrameInfoBuilder(proxy->frameInfo())
143 .setVsync(vsync, vsync, UiFrameInfoBuilder::INVALID_VSYNC_ID,
144 UiFrameInfoBuilder::UNKNOWN_DEADLINE,
145 UiFrameInfoBuilder::UNKNOWN_FRAME_INTERVAL);
146 proxy->forceDrawNextFrame();
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->forceDrawNextFrame();
167 proxy->syncAndDrawFrame();
168 }
169 if (opts.reportFrametimeWeight) {
170 proxy->fence();
171 nsecs_t done = systemTime(SYSTEM_TIME_MONOTONIC);
172 avgMs.add((done - vsync) / 1000000.0);
173 if (i % 10 == 9) {
174 printf("Average frametime %.3fms\n", avgMs.average());
175 }
176 }
177 }
178 proxy->fence();
179 nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC);
180
181 if (reports) {
182 outputBenchmarkReport(info, opts, (end - start) / (double)s2ns(1), repetitionIndex,
183 reports);
184 } else {
185 proxy->dumpProfileInfo(STDOUT_FILENO, DumpFlags::JankStats);
186 }
187 }
188
run(const TestScene::Info & info,const TestScene::Options & opts,benchmark::BenchmarkReporter * reporter)189 void run(const TestScene::Info& info, const TestScene::Options& opts,
190 benchmark::BenchmarkReporter* reporter) {
191 BenchmarkResults results;
192 for (int i = 0; i < opts.repeatCount; i++) {
193 doRun(info, opts, i, reporter ? &results : nullptr);
194 }
195 if (reporter) {
196 reporter->ReportRuns(results);
197 if (results.size() > 1) {
198 #ifdef USE_SKETCHY_INTERNAL_STATS
199 std::vector<benchmark::internal::Statistics> stats;
200 stats.reserve(3);
201 stats.emplace_back("mean", benchmark::StatisticsMean);
202 stats.emplace_back("median", benchmark::StatisticsMedian);
203 stats.emplace_back("stddev", benchmark::StatisticsStdDev);
204 for (auto& it : results) {
205 it.statistics = &stats;
206 }
207 auto summary = benchmark::ComputeStats(results);
208 reporter->ReportRuns(summary);
209 #endif
210 }
211 }
212 if (opts.reportGpuMemoryUsageVerbose) {
213 RenderProxy::dumpGraphicsMemory(STDOUT_FILENO, false);
214 }
215 }
216