bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 1 | /* |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 2 | * Copyright 2013 Google Inc. |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tools/gpu/gl/GLTestContext.h" |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 9 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/gl/GrGLUtil.h" |
| 12 | #include "tools/gpu/GpuTimer.h" |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 13 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 14 | namespace { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 15 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 16 | class GLGpuTimer : public sk_gpu_test::GpuTimer { |
| 17 | public: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 18 | static std::unique_ptr<GLGpuTimer> MakeIfSupported(const sk_gpu_test::GLTestContext*); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 19 | |
| 20 | QueryStatus checkQueryStatus(sk_gpu_test::PlatformTimerQuery) override; |
| 21 | std::chrono::nanoseconds getTimeElapsed(sk_gpu_test::PlatformTimerQuery) override; |
| 22 | void deleteQuery(sk_gpu_test::PlatformTimerQuery) override; |
| 23 | |
| 24 | private: |
Ben Wagner | a3f996c | 2021-08-05 10:14:23 -0400 | [diff] [blame^] | 25 | #ifdef SK_GL |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 26 | GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext*, const char* ext = ""); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 27 | bool validate() const; |
Ben Wagner | a3f996c | 2021-08-05 10:14:23 -0400 | [diff] [blame^] | 28 | #endif |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 29 | |
| 30 | sk_gpu_test::PlatformTimerQuery onQueueTimerStart() const override; |
| 31 | void onQueueTimerStop(sk_gpu_test::PlatformTimerQuery) const override; |
| 32 | |
| 33 | static constexpr GrGLenum GL_QUERY_RESULT = 0x8866; |
| 34 | static constexpr GrGLenum GL_QUERY_RESULT_AVAILABLE = 0x8867; |
| 35 | static constexpr GrGLenum GL_TIME_ELAPSED = 0x88bf; |
| 36 | static constexpr GrGLenum GL_GPU_DISJOINT = 0x8fbb; |
| 37 | |
| 38 | typedef void (GR_GL_FUNCTION_TYPE* GLGetIntegervProc) (GrGLenum, GrGLint*); |
| 39 | typedef void (GR_GL_FUNCTION_TYPE* GLGenQueriesProc) (GrGLsizei, GrGLuint*); |
| 40 | typedef void (GR_GL_FUNCTION_TYPE* GLDeleteQueriesProc) (GrGLsizei, const GrGLuint*); |
| 41 | typedef void (GR_GL_FUNCTION_TYPE* GLBeginQueryProc) (GrGLenum, GrGLuint); |
| 42 | typedef void (GR_GL_FUNCTION_TYPE* GLEndQueryProc) (GrGLenum); |
| 43 | typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectuivProc) (GrGLuint, GrGLenum, GrGLuint*); |
| 44 | typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectui64vProc) (GrGLuint, GrGLenum, GrGLuint64*); |
| 45 | |
| 46 | GLGetIntegervProc fGLGetIntegerv; |
| 47 | GLGenQueriesProc fGLGenQueries; |
| 48 | GLDeleteQueriesProc fGLDeleteQueries; |
| 49 | GLBeginQueryProc fGLBeginQuery; |
| 50 | GLEndQueryProc fGLEndQuery; |
| 51 | GLGetQueryObjectuivProc fGLGetQueryObjectuiv; |
| 52 | GLGetQueryObjectui64vProc fGLGetQueryObjectui64v; |
| 53 | |
| 54 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 55 | using INHERITED = sk_gpu_test::GpuTimer; |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 58 | std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) { |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 59 | #ifdef SK_GL |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 60 | std::unique_ptr<GLGpuTimer> ret; |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 61 | const GrGLInterface* gl = ctx->gl(); |
| 62 | if (gl->fExtensions.has("GL_EXT_disjoint_timer_query")) { |
| 63 | ret.reset(new GLGpuTimer(true, ctx, "EXT")); |
| 64 | } else if (kGL_GrGLStandard == gl->fStandard && |
| 65 | (GrGLGetVersion(gl) > GR_GL_VER(3,3) || gl->fExtensions.has("GL_ARB_timer_query"))) { |
| 66 | ret.reset(new GLGpuTimer(false, ctx)); |
| 67 | } else if (gl->fExtensions.has("GL_EXT_timer_query")) { |
| 68 | ret.reset(new GLGpuTimer(false, ctx, "EXT")); |
| 69 | } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 70 | if (ret && !ret->validate()) { |
| 71 | ret = nullptr; |
| 72 | } |
| 73 | return ret; |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 74 | #else |
| 75 | return nullptr; |
| 76 | #endif |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Ben Wagner | a3f996c | 2021-08-05 10:14:23 -0400 | [diff] [blame^] | 79 | #ifdef SK_GL |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 80 | GLGpuTimer::GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext* ctx, const char* ext) |
| 81 | : INHERITED(disjointSupport) { |
| 82 | ctx->getGLProcAddress(&fGLGetIntegerv, "glGetIntegerv"); |
| 83 | ctx->getGLProcAddress(&fGLGenQueries, "glGenQueries", ext); |
| 84 | ctx->getGLProcAddress(&fGLDeleteQueries, "glDeleteQueries", ext); |
| 85 | ctx->getGLProcAddress(&fGLBeginQuery, "glBeginQuery", ext); |
| 86 | ctx->getGLProcAddress(&fGLEndQuery, "glEndQuery", ext); |
| 87 | ctx->getGLProcAddress(&fGLGetQueryObjectuiv, "glGetQueryObjectuiv", ext); |
| 88 | ctx->getGLProcAddress(&fGLGetQueryObjectui64v, "glGetQueryObjectui64v", ext); |
| 89 | } |
| 90 | |
| 91 | bool GLGpuTimer::validate() const { |
| 92 | return fGLGetIntegerv && fGLGenQueries && fGLDeleteQueries && fGLBeginQuery && fGLEndQuery && |
| 93 | fGLGetQueryObjectuiv && fGLGetQueryObjectui64v; |
| 94 | } |
Ben Wagner | a3f996c | 2021-08-05 10:14:23 -0400 | [diff] [blame^] | 95 | #endif |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 96 | |
| 97 | sk_gpu_test::PlatformTimerQuery GLGpuTimer::onQueueTimerStart() const { |
| 98 | GrGLuint queryID; |
| 99 | fGLGenQueries(1, &queryID); |
| 100 | if (!queryID) { |
| 101 | return sk_gpu_test::kInvalidTimerQuery; |
| 102 | } |
| 103 | if (this->disjointSupport()) { |
| 104 | // Clear the disjoint flag. |
| 105 | GrGLint disjoint; |
| 106 | fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint); |
| 107 | } |
| 108 | fGLBeginQuery(GL_TIME_ELAPSED, queryID); |
| 109 | return static_cast<sk_gpu_test::PlatformTimerQuery>(queryID); |
| 110 | } |
| 111 | |
| 112 | void GLGpuTimer::onQueueTimerStop(sk_gpu_test::PlatformTimerQuery platformTimer) const { |
| 113 | if (sk_gpu_test::kInvalidTimerQuery == platformTimer) { |
| 114 | return; |
| 115 | } |
| 116 | fGLEndQuery(GL_TIME_ELAPSED); |
| 117 | } |
| 118 | |
| 119 | sk_gpu_test::GpuTimer::QueryStatus |
| 120 | GLGpuTimer::checkQueryStatus(sk_gpu_test::PlatformTimerQuery platformTimer) { |
| 121 | const GrGLuint queryID = static_cast<GrGLuint>(platformTimer); |
| 122 | if (!queryID) { |
| 123 | return QueryStatus::kInvalid; |
| 124 | } |
| 125 | GrGLuint available = 0; |
| 126 | fGLGetQueryObjectuiv(queryID, GL_QUERY_RESULT_AVAILABLE, &available); |
| 127 | if (!available) { |
| 128 | return QueryStatus::kPending; |
| 129 | } |
| 130 | if (this->disjointSupport()) { |
| 131 | GrGLint disjoint = 1; |
| 132 | fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint); |
| 133 | if (disjoint) { |
| 134 | return QueryStatus::kDisjoint; |
| 135 | } |
| 136 | } |
| 137 | return QueryStatus::kAccurate; |
| 138 | } |
| 139 | |
| 140 | std::chrono::nanoseconds GLGpuTimer::getTimeElapsed(sk_gpu_test::PlatformTimerQuery platformTimer) { |
| 141 | SkASSERT(this->checkQueryStatus(platformTimer) >= QueryStatus::kDisjoint); |
| 142 | const GrGLuint queryID = static_cast<GrGLuint>(platformTimer); |
| 143 | GrGLuint64 nanoseconds; |
| 144 | fGLGetQueryObjectui64v(queryID, GL_QUERY_RESULT, &nanoseconds); |
| 145 | return std::chrono::nanoseconds(nanoseconds); |
| 146 | } |
| 147 | |
| 148 | void GLGpuTimer::deleteQuery(sk_gpu_test::PlatformTimerQuery platformTimer) { |
| 149 | const GrGLuint queryID = static_cast<GrGLuint>(platformTimer); |
| 150 | fGLDeleteQueries(1, &queryID); |
| 151 | } |
| 152 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 153 | static_assert(sizeof(GrGLuint) <= sizeof(sk_gpu_test::PlatformTimerQuery)); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 154 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 155 | } // anonymous namespace |
| 156 | |
| 157 | namespace sk_gpu_test { |
| 158 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 159 | GLTestContext::GLTestContext() : TestContext() {} |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 160 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 161 | GLTestContext::~GLTestContext() { |
Brian Salomon | b2b7f80 | 2020-08-26 15:27:03 -0400 | [diff] [blame] | 162 | SkASSERT(!fGLInterface); |
| 163 | SkASSERT(!fOriginalGLInterface); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 166 | bool GLTestContext::isValid() const { |
| 167 | #ifdef SK_GL |
| 168 | return SkToBool(this->gl()); |
| 169 | #else |
| 170 | return fWasInitialized; |
| 171 | #endif |
| 172 | } |
| 173 | |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 174 | static bool fence_is_supported(const GLTestContext* ctx) { |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 175 | #ifdef SK_GL |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 176 | if (kGL_GrGLStandard == ctx->gl()->fStandard) { |
| 177 | if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3, 2) && |
| 178 | !ctx->gl()->hasExtension("GL_ARB_sync")) { |
| 179 | return false; |
| 180 | } |
| 181 | return true; |
| 182 | } else { |
| 183 | if (ctx->gl()->hasExtension("GL_APPLE_sync")) { |
| 184 | return true; |
| 185 | } else if (ctx->gl()->hasExtension("GL_NV_fence")) { |
| 186 | return true; |
| 187 | } else if (GrGLGetVersion(ctx->gl()) >= GR_GL_VER(3, 0)) { |
| 188 | return true; |
| 189 | } else { |
| 190 | return false; |
| 191 | } |
| 192 | } |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 193 | #else |
| 194 | return false; |
| 195 | #endif |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void GLTestContext::init(sk_sp<const GrGLInterface> gl) { |
Brian Salomon | b2b7f80 | 2020-08-26 15:27:03 -0400 | [diff] [blame] | 199 | fGLInterface = std::move(gl); |
| 200 | fOriginalGLInterface = fGLInterface; |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 201 | fFenceSupport = fence_is_supported(this); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 202 | fGpuTimer = GLGpuTimer::MakeIfSupported(this); |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 203 | #ifndef SK_GL |
| 204 | fWasInitialized = true; |
| 205 | #endif |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 206 | } |
| 207 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 208 | void GLTestContext::teardown() { |
Brian Salomon | b2b7f80 | 2020-08-26 15:27:03 -0400 | [diff] [blame] | 209 | fGLInterface.reset(); |
| 210 | fOriginalGLInterface.reset(); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 211 | INHERITED::teardown(); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 212 | } |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 213 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 214 | void GLTestContext::testAbandon() { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 215 | INHERITED::testAbandon(); |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 216 | #ifdef SK_GL |
Brian Salomon | b2b7f80 | 2020-08-26 15:27:03 -0400 | [diff] [blame] | 217 | if (fGLInterface) { |
| 218 | fGLInterface->abandon(); |
| 219 | fOriginalGLInterface->abandon(); |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 220 | } |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 221 | #endif |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 222 | } |
| 223 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 224 | void GLTestContext::finish() { |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 225 | #ifdef SK_GL |
Brian Salomon | b2b7f80 | 2020-08-26 15:27:03 -0400 | [diff] [blame] | 226 | if (fGLInterface) { |
| 227 | GR_GL_CALL(fGLInterface.get(), Finish()); |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 228 | } |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 229 | #endif |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Brian Salomon | b2b7f80 | 2020-08-26 15:27:03 -0400 | [diff] [blame] | 232 | void GLTestContext::overrideVersion(const char* version, const char* shadingLanguageVersion) { |
| 233 | #ifdef SK_GL |
| 234 | // GrGLFunction has both a limited capture size and doesn't call a destructor when it is |
| 235 | // initialized with a lambda. So here we're trusting fOriginalGLInterface will be kept alive. |
| 236 | auto getString = [wrapped = &fOriginalGLInterface->fFunctions.fGetString, |
| 237 | version, |
| 238 | shadingLanguageVersion](GrGLenum name) { |
| 239 | if (name == GR_GL_VERSION) { |
| 240 | return reinterpret_cast<const GrGLubyte*>(version); |
| 241 | } else if (name == GR_GL_SHADING_LANGUAGE_VERSION) { |
| 242 | return reinterpret_cast<const GrGLubyte*>(shadingLanguageVersion); |
| 243 | } |
| 244 | return (*wrapped)(name); |
| 245 | }; |
| 246 | auto newInterface = sk_make_sp<GrGLInterface>(*fOriginalGLInterface); |
| 247 | newInterface->fFunctions.fGetString = getString; |
| 248 | fGLInterface = std::move(newInterface); |
| 249 | #endif |
| 250 | }; |
| 251 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 252 | sk_sp<GrDirectContext> GLTestContext::makeContext(const GrContextOptions& options) { |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 253 | #ifdef SK_GL |
Brian Salomon | b2b7f80 | 2020-08-26 15:27:03 -0400 | [diff] [blame] | 254 | return GrDirectContext::MakeGL(fGLInterface, options); |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 255 | #else |
| 256 | return nullptr; |
| 257 | #endif |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 258 | } |
| 259 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 260 | } // namespace sk_gpu_test |