1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // ANGLEPerfTests:
7 // Base class for google test performance tests
8 //
9
10 #ifndef PERF_TESTS_ANGLE_PERF_TEST_H_
11 #define PERF_TESTS_ANGLE_PERF_TEST_H_
12
13 #include <gtest/gtest.h>
14
15 #include <mutex>
16 #include <string>
17 #include <thread>
18 #include <unordered_map>
19 #include <vector>
20
21 #include "platform/PlatformMethods.h"
22 #include "test_utils/angle_test_configs.h"
23 #include "test_utils/angle_test_instantiate.h"
24 #include "test_utils/angle_test_platform.h"
25 #include "third_party/perf/perf_result_reporter.h"
26 #include "util/EGLWindow.h"
27 #include "util/OSWindow.h"
28 #include "util/Timer.h"
29 #include "util/util_gl.h"
30
31 class Event;
32
33 #if !defined(ASSERT_GL_NO_ERROR)
34 # define ASSERT_GL_NO_ERROR() ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError())
35 #endif // !defined(ASSERT_GL_NO_ERROR)
36
37 #if !defined(ASSERT_GLENUM_EQ)
38 # define ASSERT_GLENUM_EQ(expected, actual) \
39 ASSERT_EQ(static_cast<GLenum>(expected), static_cast<GLenum>(actual))
40 #endif // !defined(ASSERT_GLENUM_EQ)
41
42 // These are trace events according to Google's "Trace Event Format".
43 // See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU
44 // Only a subset of the properties are implemented.
45 struct TraceEvent final
46 {
TraceEventfinal47 TraceEvent() {}
48 TraceEvent(char phaseIn,
49 const char *categoryNameIn,
50 const char *nameIn,
51 double timestampIn,
52 uint32_t tidIn);
53
54 static constexpr uint32_t kMaxNameLen = 64;
55
56 char phase = 0;
57 const char *categoryName = nullptr;
58 char name[kMaxNameLen] = {};
59 double timestamp = 0;
60 uint32_t tid = 0;
61 };
62
63 class ANGLEPerfTest : public testing::Test, angle::NonCopyable
64 {
65 public:
66 ANGLEPerfTest(const std::string &name,
67 const std::string &backend,
68 const std::string &story,
69 unsigned int iterationsPerStep,
70 const char *units = "ns");
71 ~ANGLEPerfTest() override;
72
73 virtual void step() = 0;
74
75 // Called right after the timer starts to let the test initialize other metrics if necessary
startTest()76 virtual void startTest() {}
77 // Called right before timer is stopped to let the test wait for asynchronous operations.
finishTest()78 virtual void finishTest() {}
flush()79 virtual void flush() {}
80
81 // Can be overridden in child tests that require a certain number of steps per trial.
82 virtual int getStepAlignment() const;
83
84 protected:
85 enum class RunLoopPolicy
86 {
87 FinishEveryStep,
88 RunContinuously,
89 };
90
91 void run();
92 void SetUp() override;
93 void TearDown() override;
94
95 // Normalize a time value according to the number of test loop iterations (mFrameCount)
96 double normalizedTime(size_t value) const;
97
98 // Call if the test step was aborted and the test should stop running.
abortTest()99 void abortTest() { mRunning = false; }
100
getNumStepsPerformed()101 int getNumStepsPerformed() const { return mTrialNumStepsPerformed; }
102
103 void doRunLoop(double maxRunTime, int maxStepsToRun, RunLoopPolicy runPolicy);
104
105 // Overriden in trace perf tests.
saveScreenshot(const std::string & screenshotName)106 virtual void saveScreenshot(const std::string &screenshotName) {}
computeGPUTime()107 virtual void computeGPUTime() {}
108
109 double printResults();
110 void calibrateStepsToRun(RunLoopPolicy policy);
111
112 std::string mName;
113 std::string mBackend;
114 std::string mStory;
115 Timer mTimer;
116 uint64_t mGPUTimeNs;
117 bool mSkipTest;
118 std::unique_ptr<perf_test::PerfResultReporter> mReporter;
119 int mStepsToRun;
120 int mTrialNumStepsPerformed;
121 int mTotalNumStepsPerformed;
122 int mIterationsPerStep;
123 bool mRunning;
124 std::vector<double> mTestTrialResults;
125 };
126
127 enum class SurfaceType
128 {
129 Window,
130 WindowWithVSync,
131 Offscreen,
132 };
133
134 struct RenderTestParams : public angle::PlatformParameters
135 {
~RenderTestParamsRenderTestParams136 virtual ~RenderTestParams() {}
137
138 virtual std::string backend() const;
139 virtual std::string story() const;
140 std::string backendAndStory() const;
141
142 EGLint windowWidth = 64;
143 EGLint windowHeight = 64;
144 unsigned int iterationsPerStep = 0;
145 bool trackGpuTime = false;
146 SurfaceType surfaceType = SurfaceType::Window;
147 EGLenum colorSpace = EGL_COLORSPACE_LINEAR;
148 };
149
150 class ANGLERenderTest : public ANGLEPerfTest
151 {
152 public:
153 ANGLERenderTest(const std::string &name,
154 const RenderTestParams &testParams,
155 const char *units = "ns");
156 ~ANGLERenderTest() override;
157
158 void addExtensionPrerequisite(const char *extensionName);
159
initializeBenchmark()160 virtual void initializeBenchmark() {}
destroyBenchmark()161 virtual void destroyBenchmark() {}
162
163 virtual void drawBenchmark() = 0;
164
165 bool popEvent(Event *event);
166
167 OSWindow *getWindow();
168 GLWindowBase *getGLWindow();
169
170 std::vector<TraceEvent> &getTraceEventBuffer();
171
overrideWorkaroundsD3D(angle::FeaturesD3D * featuresD3D)172 virtual void overrideWorkaroundsD3D(angle::FeaturesD3D *featuresD3D) {}
173 void onErrorMessage(const char *errorMessage);
174
175 uint32_t getCurrentThreadSerial();
getTraceEventMutex()176 std::mutex &getTraceEventMutex() { return mTraceEventMutex; }
177
178 protected:
179 const RenderTestParams &mTestParams;
180
181 void setWebGLCompatibilityEnabled(bool webglCompatibility);
182 void setRobustResourceInit(bool enabled);
183
184 void startGpuTimer();
185 void stopGpuTimer();
186
187 void beginInternalTraceEvent(const char *name);
188 void endInternalTraceEvent(const char *name);
189 void beginGLTraceEvent(const char *name, double hostTimeSec);
190 void endGLTraceEvent(const char *name, double hostTimeSec);
191
disableTestHarnessSwap()192 void disableTestHarnessSwap() { mSwapEnabled = false; }
193
194 bool mIsTimestampQueryAvailable;
195
196 private:
197 void SetUp() override;
198 void TearDown() override;
199
200 void step() override;
201 void startTest() override;
202 void finishTest() override;
203 void computeGPUTime() override;
204
205 bool areExtensionPrerequisitesFulfilled() const;
206
207 GLWindowBase *mGLWindow;
208 OSWindow *mOSWindow;
209 std::vector<const char *> mExtensionPrerequisites;
210 angle::PlatformMethods mPlatformMethods;
211 ConfigParameters mConfigParams;
212 bool mSwapEnabled;
213
214 struct TimestampSample
215 {
216 GLuint beginQuery;
217 GLuint endQuery;
218 };
219
220 GLuint mCurrentTimestampBeginQuery = 0;
221 std::vector<TimestampSample> mTimestampQueries;
222
223 // Trace event record that can be output.
224 std::vector<TraceEvent> mTraceEventBuffer;
225
226 // Handle to the entry point binding library.
227 std::unique_ptr<angle::Library> mEntryPointsLib;
228
229 std::vector<std::thread::id> mThreadIDs;
230 std::mutex mTraceEventMutex;
231 };
232
233 // Mixins.
234 namespace params
235 {
236 template <typename ParamsT>
Offscreen(const ParamsT & input)237 ParamsT Offscreen(const ParamsT &input)
238 {
239 ParamsT output = input;
240 output.surfaceType = SurfaceType::Offscreen;
241 return output;
242 }
243
244 template <typename ParamsT>
NullDevice(const ParamsT & input)245 ParamsT NullDevice(const ParamsT &input)
246 {
247 ParamsT output = input;
248 output.eglParameters.deviceType = EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE;
249 output.trackGpuTime = false;
250 return output;
251 }
252
253 template <typename ParamsT>
Passthrough(const ParamsT & input)254 ParamsT Passthrough(const ParamsT &input)
255 {
256 return input;
257 }
258 } // namespace params
259
260 namespace angle
261 {
262 // Returns the time of the host since the application started in seconds.
263 double GetHostTimeSeconds();
264 } // namespace angle
265 #endif // PERF_TESTS_ANGLE_PERF_TEST_H_
266