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