Path: blob/main_old/src/tests/perf_tests/ANGLEPerfTest.h
1693 views
//1// Copyright 2014 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// ANGLEPerfTests:6// Base class for google test performance tests7//89#ifndef PERF_TESTS_ANGLE_PERF_TEST_H_10#define PERF_TESTS_ANGLE_PERF_TEST_H_1112#include <gtest/gtest.h>1314#include <mutex>15#include <string>16#include <thread>17#include <unordered_map>18#include <vector>1920#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"2930class Event;3132#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)3536#if !defined(ASSERT_GLENUM_EQ)37# define ASSERT_GLENUM_EQ(expected, actual) \38ASSERT_EQ(static_cast<GLenum>(expected), static_cast<GLenum>(actual))39#endif // !defined(ASSERT_GLENUM_EQ)4041// These are trace events according to Google's "Trace Event Format".42// See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU43// Only a subset of the properties are implemented.44struct TraceEvent final45{46TraceEvent() {}47TraceEvent(char phaseIn,48const char *categoryNameIn,49const char *nameIn,50double timestampIn,51uint32_t tidIn);5253static constexpr uint32_t kMaxNameLen = 64;5455char phase = 0;56const char *categoryName = nullptr;57char name[kMaxNameLen] = {};58double timestamp = 0;59uint32_t tid = 0;60};6162class ANGLEPerfTest : public testing::Test, angle::NonCopyable63{64public:65ANGLEPerfTest(const std::string &name,66const std::string &backend,67const std::string &story,68unsigned int iterationsPerStep,69const char *units = "ns");70~ANGLEPerfTest() override;7172virtual void step() = 0;7374// Called right after the timer starts to let the test initialize other metrics if necessary75virtual void startTest() {}76// Called right before timer is stopped to let the test wait for asynchronous operations.77virtual void finishTest() {}78virtual void flush() {}7980// Can be overridden in child tests that require a certain number of steps per trial.81virtual int getStepAlignment() const;8283protected:84enum class RunLoopPolicy85{86FinishEveryStep,87RunContinuously,88};8990void run();91void SetUp() override;92void TearDown() override;9394// Normalize a time value according to the number of test loop iterations (mFrameCount)95double normalizedTime(size_t value) const;9697// Call if the test step was aborted and the test should stop running.98void abortTest() { mRunning = false; }99100int getNumStepsPerformed() const { return mTrialNumStepsPerformed; }101102void doRunLoop(double maxRunTime, int maxStepsToRun, RunLoopPolicy runPolicy);103104// Overriden in trace perf tests.105virtual void saveScreenshot(const std::string &screenshotName) {}106virtual void computeGPUTime() {}107108double printResults();109void calibrateStepsToRun(RunLoopPolicy policy);110111std::string mName;112std::string mBackend;113std::string mStory;114Timer mTimer;115uint64_t mGPUTimeNs;116bool mSkipTest;117std::unique_ptr<perf_test::PerfResultReporter> mReporter;118int mStepsToRun;119int mTrialNumStepsPerformed;120int mTotalNumStepsPerformed;121int mIterationsPerStep;122bool mRunning;123std::vector<double> mTestTrialResults;124};125126enum class SurfaceType127{128Window,129WindowWithVSync,130Offscreen,131};132133struct RenderTestParams : public angle::PlatformParameters134{135virtual ~RenderTestParams() {}136137virtual std::string backend() const;138virtual std::string story() const;139std::string backendAndStory() const;140141EGLint windowWidth = 64;142EGLint windowHeight = 64;143unsigned int iterationsPerStep = 0;144bool trackGpuTime = false;145SurfaceType surfaceType = SurfaceType::Window;146EGLenum colorSpace = EGL_COLORSPACE_LINEAR;147};148149class ANGLERenderTest : public ANGLEPerfTest150{151public:152ANGLERenderTest(const std::string &name,153const RenderTestParams &testParams,154const char *units = "ns");155~ANGLERenderTest() override;156157void addExtensionPrerequisite(const char *extensionName);158159virtual void initializeBenchmark() {}160virtual void destroyBenchmark() {}161162virtual void drawBenchmark() = 0;163164bool popEvent(Event *event);165166OSWindow *getWindow();167GLWindowBase *getGLWindow();168169std::vector<TraceEvent> &getTraceEventBuffer();170171virtual void overrideWorkaroundsD3D(angle::FeaturesD3D *featuresD3D) {}172void onErrorMessage(const char *errorMessage);173174uint32_t getCurrentThreadSerial();175std::mutex &getTraceEventMutex() { return mTraceEventMutex; }176177protected:178const RenderTestParams &mTestParams;179180void setWebGLCompatibilityEnabled(bool webglCompatibility);181void setRobustResourceInit(bool enabled);182183void startGpuTimer();184void stopGpuTimer();185186void beginInternalTraceEvent(const char *name);187void endInternalTraceEvent(const char *name);188void beginGLTraceEvent(const char *name, double hostTimeSec);189void endGLTraceEvent(const char *name, double hostTimeSec);190191void disableTestHarnessSwap() { mSwapEnabled = false; }192193bool mIsTimestampQueryAvailable;194195private:196void SetUp() override;197void TearDown() override;198199void step() override;200void startTest() override;201void finishTest() override;202void computeGPUTime() override;203204bool areExtensionPrerequisitesFulfilled() const;205206GLWindowBase *mGLWindow;207OSWindow *mOSWindow;208std::vector<const char *> mExtensionPrerequisites;209angle::PlatformMethods mPlatformMethods;210ConfigParameters mConfigParams;211bool mSwapEnabled;212213struct TimestampSample214{215GLuint beginQuery;216GLuint endQuery;217};218219GLuint mCurrentTimestampBeginQuery = 0;220std::vector<TimestampSample> mTimestampQueries;221222// Trace event record that can be output.223std::vector<TraceEvent> mTraceEventBuffer;224225// Handle to the entry point binding library.226std::unique_ptr<angle::Library> mEntryPointsLib;227228std::vector<std::thread::id> mThreadIDs;229std::mutex mTraceEventMutex;230};231232// Mixins.233namespace params234{235template <typename ParamsT>236ParamsT Offscreen(const ParamsT &input)237{238ParamsT output = input;239output.surfaceType = SurfaceType::Offscreen;240return output;241}242243template <typename ParamsT>244ParamsT NullDevice(const ParamsT &input)245{246ParamsT output = input;247output.eglParameters.deviceType = EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE;248output.trackGpuTime = false;249return output;250}251252template <typename ParamsT>253ParamsT Passthrough(const ParamsT &input)254{255return input;256}257} // namespace params258259namespace angle260{261// Returns the time of the host since the application started in seconds.262double GetHostTimeSeconds();263} // namespace angle264#endif // PERF_TESTS_ANGLE_PERF_TEST_H_265266267