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 };
148 
149 class ANGLERenderTest : public ANGLEPerfTest
150 {
151   public:
152     ANGLERenderTest(const std::string &name,
153                     const RenderTestParams &testParams,
154                     const char *units = "ns");
155     ~ANGLERenderTest() override;
156 
157     void addExtensionPrerequisite(const char *extensionName);
158 
initializeBenchmark()159     virtual void initializeBenchmark() {}
destroyBenchmark()160     virtual void destroyBenchmark() {}
161 
162     virtual void drawBenchmark() = 0;
163 
164     bool popEvent(Event *event);
165 
166     OSWindow *getWindow();
167     GLWindowBase *getGLWindow();
168 
169     std::vector<TraceEvent> &getTraceEventBuffer();
170 
overrideWorkaroundsD3D(angle::FeaturesD3D * featuresD3D)171     virtual void overrideWorkaroundsD3D(angle::FeaturesD3D *featuresD3D) {}
172     void onErrorMessage(const char *errorMessage);
173 
174     uint32_t getCurrentThreadSerial();
getTraceEventMutex()175     std::mutex &getTraceEventMutex() { return mTraceEventMutex; }
176 
177   protected:
178     const RenderTestParams &mTestParams;
179 
180     void setWebGLCompatibilityEnabled(bool webglCompatibility);
181     void setRobustResourceInit(bool enabled);
182 
183     void startGpuTimer();
184     void stopGpuTimer();
185 
186     void beginInternalTraceEvent(const char *name);
187     void endInternalTraceEvent(const char *name);
188     void beginGLTraceEvent(const char *name, double hostTimeSec);
189     void endGLTraceEvent(const char *name, double hostTimeSec);
190 
disableTestHarnessSwap()191     void disableTestHarnessSwap() { mSwapEnabled = false; }
192 
193     bool mIsTimestampQueryAvailable;
194 
195   private:
196     void SetUp() override;
197     void TearDown() override;
198 
199     void step() override;
200     void startTest() override;
201     void finishTest() override;
202     void computeGPUTime() override;
203 
204     bool areExtensionPrerequisitesFulfilled() const;
205 
206     GLWindowBase *mGLWindow;
207     OSWindow *mOSWindow;
208     std::vector<const char *> mExtensionPrerequisites;
209     angle::PlatformMethods mPlatformMethods;
210     ConfigParameters mConfigParams;
211     bool mSwapEnabled;
212 
213     struct TimestampSample
214     {
215         GLuint beginQuery;
216         GLuint endQuery;
217     };
218 
219     GLuint mCurrentTimestampBeginQuery = 0;
220     std::vector<TimestampSample> mTimestampQueries;
221 
222     // Trace event record that can be output.
223     std::vector<TraceEvent> mTraceEventBuffer;
224 
225     // Handle to the entry point binding library.
226     std::unique_ptr<angle::Library> mEntryPointsLib;
227 
228     std::vector<std::thread::id> mThreadIDs;
229     std::mutex mTraceEventMutex;
230 };
231 
232 // Mixins.
233 namespace params
234 {
235 template <typename ParamsT>
Offscreen(const ParamsT & input)236 ParamsT Offscreen(const ParamsT &input)
237 {
238     ParamsT output     = input;
239     output.surfaceType = SurfaceType::Offscreen;
240     return output;
241 }
242 
243 template <typename ParamsT>
NullDevice(const ParamsT & input)244 ParamsT NullDevice(const ParamsT &input)
245 {
246     ParamsT output                  = input;
247     output.eglParameters.deviceType = EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE;
248     output.trackGpuTime             = false;
249     return output;
250 }
251 
252 template <typename ParamsT>
Passthrough(const ParamsT & input)253 ParamsT Passthrough(const ParamsT &input)
254 {
255     return input;
256 }
257 }  // namespace params
258 
259 namespace angle
260 {
261 // Returns the time of the host since the application started in seconds.
262 double GetHostTimeSeconds();
263 }  // namespace angle
264 #endif  // PERF_TESTS_ANGLE_PERF_TEST_H_
265