Flutter Engine
The Flutter Engine
GLTestContext.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
13#include "tools/gpu/GpuTimer.h"
14
15namespace {
16
17class GLGpuTimer : public sk_gpu_test::GpuTimer {
18public:
19 static std::unique_ptr<GLGpuTimer> MakeIfSupported(const sk_gpu_test::GLTestContext*);
20
22 std::chrono::nanoseconds getTimeElapsed(sk_gpu_test::PlatformTimerQuery) override;
24
25private:
26 GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext*, const char* ext = "");
27 bool validate() const;
28
31
32 inline static constexpr GrGLenum GL_QUERY_RESULT = 0x8866;
33 inline static constexpr GrGLenum GL_QUERY_RESULT_AVAILABLE = 0x8867;
34 inline static constexpr GrGLenum GL_TIME_ELAPSED = 0x88bf;
35 inline static constexpr GrGLenum GL_GPU_DISJOINT = 0x8fbb;
36
37 typedef void (GR_GL_FUNCTION_TYPE* GLGetIntegervProc) (GrGLenum, GrGLint*);
38 typedef void (GR_GL_FUNCTION_TYPE* GLGenQueriesProc) (GrGLsizei, GrGLuint*);
39 typedef void (GR_GL_FUNCTION_TYPE* GLDeleteQueriesProc) (GrGLsizei, const GrGLuint*);
40 typedef void (GR_GL_FUNCTION_TYPE* GLBeginQueryProc) (GrGLenum, GrGLuint);
41 typedef void (GR_GL_FUNCTION_TYPE* GLEndQueryProc) (GrGLenum);
42 typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectuivProc) (GrGLuint, GrGLenum, GrGLuint*);
43 typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectui64vProc) (GrGLuint, GrGLenum, GrGLuint64*);
44
45 GLGetIntegervProc fGLGetIntegerv;
46 GLGenQueriesProc fGLGenQueries;
47 GLDeleteQueriesProc fGLDeleteQueries;
48 GLBeginQueryProc fGLBeginQuery;
49 GLEndQueryProc fGLEndQuery;
50 GLGetQueryObjectuivProc fGLGetQueryObjectuiv;
51 GLGetQueryObjectui64vProc fGLGetQueryObjectui64v;
52
53
55};
56
57std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
58 std::unique_ptr<GLGpuTimer> ret;
59 const GrGLInterface* gl = ctx->gl();
60 if (gl->fExtensions.has("GL_EXT_disjoint_timer_query")) {
61 ret.reset(new GLGpuTimer(true, ctx, "EXT"));
62 } else if (kGL_GrGLStandard == gl->fStandard &&
63 (GrGLGetVersion(gl) > GR_GL_VER(3,3) || gl->fExtensions.has("GL_ARB_timer_query"))) {
64 ret.reset(new GLGpuTimer(false, ctx));
65 } else if (gl->fExtensions.has("GL_EXT_timer_query")) {
66 ret.reset(new GLGpuTimer(false, ctx, "EXT"));
67 }
68 if (ret && !ret->validate()) {
69 ret = nullptr;
70 }
71 return ret;
72}
73
74GLGpuTimer::GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext* ctx, const char* ext)
75 : INHERITED(disjointSupport) {
76 ctx->getGLProcAddress(&fGLGetIntegerv, "glGetIntegerv");
77 ctx->getGLProcAddress(&fGLGenQueries, "glGenQueries", ext);
78 ctx->getGLProcAddress(&fGLDeleteQueries, "glDeleteQueries", ext);
79 ctx->getGLProcAddress(&fGLBeginQuery, "glBeginQuery", ext);
80 ctx->getGLProcAddress(&fGLEndQuery, "glEndQuery", ext);
81 ctx->getGLProcAddress(&fGLGetQueryObjectuiv, "glGetQueryObjectuiv", ext);
82 ctx->getGLProcAddress(&fGLGetQueryObjectui64v, "glGetQueryObjectui64v", ext);
83}
84
85bool GLGpuTimer::validate() const {
86 return fGLGetIntegerv && fGLGenQueries && fGLDeleteQueries && fGLBeginQuery && fGLEndQuery &&
87 fGLGetQueryObjectuiv && fGLGetQueryObjectui64v;
88}
89
90sk_gpu_test::PlatformTimerQuery GLGpuTimer::onQueueTimerStart() const {
91 GrGLuint queryID;
92 fGLGenQueries(1, &queryID);
93 if (!queryID) {
95 }
96 if (this->disjointSupport()) {
97 // Clear the disjoint flag.
98 GrGLint disjoint;
99 fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint);
100 }
101 fGLBeginQuery(GL_TIME_ELAPSED, queryID);
102 return static_cast<sk_gpu_test::PlatformTimerQuery>(queryID);
103}
104
105void GLGpuTimer::onQueueTimerStop(sk_gpu_test::PlatformTimerQuery platformTimer) const {
106 if (sk_gpu_test::kInvalidTimerQuery == platformTimer) {
107 return;
108 }
109 fGLEndQuery(GL_TIME_ELAPSED);
110}
111
113GLGpuTimer::checkQueryStatus(sk_gpu_test::PlatformTimerQuery platformTimer) {
114 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
115 if (!queryID) {
117 }
118 GrGLuint available = 0;
119 fGLGetQueryObjectuiv(queryID, GL_QUERY_RESULT_AVAILABLE, &available);
120 if (!available) {
121 return QueryStatus::kPending;
122 }
123 if (this->disjointSupport()) {
124 GrGLint disjoint = 1;
125 fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint);
126 if (disjoint) {
127 return QueryStatus::kDisjoint;
128 }
129 }
130 return QueryStatus::kAccurate;
131}
132
133std::chrono::nanoseconds GLGpuTimer::getTimeElapsed(sk_gpu_test::PlatformTimerQuery platformTimer) {
134 SkASSERT(this->checkQueryStatus(platformTimer) >= QueryStatus::kDisjoint);
135 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
136 GrGLuint64 nanoseconds;
137 fGLGetQueryObjectui64v(queryID, GL_QUERY_RESULT, &nanoseconds);
138 return std::chrono::nanoseconds(nanoseconds);
139}
140
141void GLGpuTimer::deleteQuery(sk_gpu_test::PlatformTimerQuery platformTimer) {
142 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
143 fGLDeleteQueries(1, &queryID);
144}
145
146static_assert(sizeof(GrGLuint) <= sizeof(sk_gpu_test::PlatformTimerQuery));
147
148} // anonymous namespace
149
150namespace sk_gpu_test {
151
153
155 SkASSERT(!fGLInterface);
156 SkASSERT(!fOriginalGLInterface);
157}
158
160 return SkToBool(this->gl());
161}
162
163static bool fence_is_supported(const GLTestContext* ctx) {
164 if (kGL_GrGLStandard == ctx->gl()->fStandard) {
165 if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3, 2) &&
166 !ctx->gl()->hasExtension("GL_ARB_sync")) {
167 return false;
168 }
169 return true;
170 } else {
171 if (ctx->gl()->hasExtension("GL_APPLE_sync")) {
172 return true;
173 } else if (ctx->gl()->hasExtension("GL_NV_fence")) {
174 return true;
175 } else if (GrGLGetVersion(ctx->gl()) >= GR_GL_VER(3, 0)) {
176 return true;
177 } else {
178 return false;
179 }
180 }
181}
182
184 fGLInterface = std::move(gl);
185 fOriginalGLInterface = fGLInterface;
187 fGpuTimer = GLGpuTimer::MakeIfSupported(this);
188}
189
191 fGLInterface.reset();
192 fOriginalGLInterface.reset();
194}
195
198#if defined(GR_TEST_UTILS)
199 if (fGLInterface) {
200 fGLInterface->abandon();
201 fOriginalGLInterface->abandon();
202 }
203#endif
204}
205
206void GLTestContext::overrideVersion(const char* version, const char* shadingLanguageVersion) {
207 // GrGLFunction has both a limited capture size and doesn't call a destructor when it is
208 // initialized with a lambda. So here we're trusting fOriginalGLInterface will be kept alive.
209 auto getString = [wrapped = &fOriginalGLInterface->fFunctions.fGetString,
210 version,
211 shadingLanguageVersion](GrGLenum name) {
212 if (name == GR_GL_VERSION) {
213 return reinterpret_cast<const GrGLubyte*>(version);
214 } else if (name == GR_GL_SHADING_LANGUAGE_VERSION) {
215 return reinterpret_cast<const GrGLubyte*>(shadingLanguageVersion);
216 }
217 return (*wrapped)(name);
218 };
219 auto newInterface = sk_make_sp<GrGLInterface>(*fOriginalGLInterface);
220 newInterface->fFunctions.fGetString = getString;
221 fGLInterface = std::move(newInterface);
222}
223
225 return GrDirectContexts::MakeGL(fGLInterface, options);
226}
227
228} // namespace sk_gpu_test
const char * options
#define GR_GL_FUNCTION_TYPE
Definition: GrGLConfig.h:27
#define GR_GL_SHADING_LANGUAGE_VERSION
Definition: GrGLDefines.h:572
#define GR_GL_VERSION
Definition: GrGLDefines.h:602
unsigned int GrGLuint
Definition: GrGLTypes.h:113
@ kGL_GrGLStandard
Definition: GrGLTypes.h:21
int GrGLsizei
Definition: GrGLTypes.h:109
int GrGLint
Definition: GrGLTypes.h:108
unsigned int GrGLenum
Definition: GrGLTypes.h:102
unsigned char GrGLubyte
Definition: GrGLTypes.h:111
uint64_t GrGLuint64
Definition: GrGLTypes.h:114
GrGLVersion GrGLGetVersion(const GrGLInterface *gl)
Definition: GrGLUtil.cpp:109
#define GR_GL_VER(major, minor)
Definition: GrGLUtil.h:26
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kInvalid
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
static sk_sp< GrTextureProxy > wrapped(skiatest::Reporter *reporter, GrRecordingContext *rContext, GrProxyProvider *proxyProvider, SkBackingFit fit)
void getGLProcAddress(Ret(GR_GL_FUNCTION_TYPE **out)(Args...), const char *name, const char *ext=nullptr) const
Definition: GLTestContext.h:54
const GrGLInterface * gl() const
Definition: GLTestContext.h:30
void init(sk_sp< const GrGLInterface >)
sk_sp< GrDirectContext > makeContext(const GrContextOptions &options) override
void overrideVersion(const char *version, const char *shadingLanguageVersion)
virtual std::chrono::nanoseconds getTimeElapsed(PlatformTimerQuery)=0
virtual QueryStatus checkQueryStatus(PlatformTimerQuery)=0
virtual PlatformTimerQuery onQueueTimerStart() const =0
virtual void deleteQuery(PlatformTimerQuery)=0
virtual void onQueueTimerStop(PlatformTimerQuery) const =0
std::unique_ptr< GpuTimer > fGpuTimer
Definition: TestContext.h:89
virtual void teardown()
Definition: TestContext.cpp:70
virtual void testAbandon()
Definition: TestContext.cpp:67
void reset(T *ptr=nullptr)
Definition: SkRefCnt.h:310
SK_API sk_sp< GrDirectContext > MakeGL(sk_sp< const GrGLInterface >, const GrContextOptions &)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
gl
Definition: malisc.py:41
uint64_t PlatformTimerQuery
Definition: GpuTimer.h:17
static constexpr PlatformTimerQuery kInvalidTimerQuery
Definition: GpuTimer.h:18
static bool fence_is_supported(const GLTestContext *ctx)
GrGLFunction< GrGLGetStringFn > fGetString
bool hasExtension(const char ext[]) const
Definition: GrGLInterface.h:84
struct GrGLInterface::Functions fFunctions
GrGLStandard fStandard
Definition: GrGLInterface.h:81