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