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 | |
| 54 | typedef sk_gpu_test::GpuTimer INHERITED; |
| 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() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 159 | SkASSERT(nullptr == fGL.get()); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 162 | bool GLTestContext::isValid() const { |
| 163 | #ifdef SK_GL |
| 164 | return SkToBool(this->gl()); |
| 165 | #else |
| 166 | return fWasInitialized; |
| 167 | #endif |
| 168 | } |
| 169 | |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 170 | static bool fence_is_supported(const GLTestContext* ctx) { |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 171 | #ifdef SK_GL |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 172 | if (kGL_GrGLStandard == ctx->gl()->fStandard) { |
| 173 | if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3, 2) && |
| 174 | !ctx->gl()->hasExtension("GL_ARB_sync")) { |
| 175 | return false; |
| 176 | } |
| 177 | return true; |
| 178 | } else { |
| 179 | if (ctx->gl()->hasExtension("GL_APPLE_sync")) { |
| 180 | return true; |
| 181 | } else if (ctx->gl()->hasExtension("GL_NV_fence")) { |
| 182 | return true; |
| 183 | } else if (GrGLGetVersion(ctx->gl()) >= GR_GL_VER(3, 0)) { |
| 184 | return true; |
| 185 | } else { |
| 186 | return false; |
| 187 | } |
| 188 | } |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 189 | #else |
| 190 | return false; |
| 191 | #endif |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void GLTestContext::init(sk_sp<const GrGLInterface> gl) { |
Brian Salomon | 3d6801e | 2017-12-11 10:06:31 -0500 | [diff] [blame] | 195 | fGL = std::move(gl); |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame] | 196 | fFenceSupport = fence_is_supported(this); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 197 | fGpuTimer = GLGpuTimer::MakeIfSupported(this); |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 198 | #ifndef SK_GL |
| 199 | fWasInitialized = true; |
| 200 | #endif |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 201 | } |
| 202 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 203 | void GLTestContext::teardown() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 204 | fGL.reset(nullptr); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 205 | INHERITED::teardown(); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 206 | } |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 207 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 208 | void GLTestContext::testAbandon() { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 209 | INHERITED::testAbandon(); |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 210 | #ifdef SK_GL |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 211 | if (fGL) { |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 212 | fGL->abandon(); |
| 213 | } |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 214 | #endif |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 215 | } |
| 216 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 217 | void GLTestContext::finish() { |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 218 | #ifdef SK_GL |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 219 | if (fGL) { |
| 220 | GR_GL_CALL(fGL.get(), Finish()); |
| 221 | } |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 222 | #endif |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame^] | 225 | sk_sp<GrDirectContext> GLTestContext::makeContext(const GrContextOptions& options) { |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 226 | #ifdef SK_GL |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame^] | 227 | return GrDirectContext::MakeGL(fGL, options); |
Brian Salomon | f4ba4ec | 2020-03-19 15:54:28 -0400 | [diff] [blame] | 228 | #else |
| 229 | return nullptr; |
| 230 | #endif |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 231 | } |
| 232 | |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 233 | } // namespace sk_gpu_test |