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 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 8 | #include "GLTestContext.h" |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 9 | |
| 10 | #include "GpuTimer.h" |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 11 | #include "gl/GrGLUtil.h" |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 12 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 13 | namespace { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 14 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 15 | class GLFenceSync : public sk_gpu_test::FenceSync { |
| 16 | public: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame^] | 17 | static std::unique_ptr<GLFenceSync> MakeIfSupported(const sk_gpu_test::GLTestContext*); |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 18 | |
| 19 | sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override; |
| 20 | bool waitFence(sk_gpu_test::PlatformFence fence) const override; |
| 21 | void deleteFence(sk_gpu_test::PlatformFence fence) const override; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 22 | |
| 23 | private: |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 24 | GLFenceSync(const sk_gpu_test::GLTestContext*, const char* ext = ""); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 25 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 26 | bool validate() { return fGLFenceSync && fGLClientWaitSync && fGLDeleteSync; } |
| 27 | |
| 28 | static constexpr GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117; |
| 29 | static constexpr GrGLenum GL_WAIT_FAILED = 0x911d; |
| 30 | static constexpr GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 31 | |
| 32 | typedef struct __GLsync *GLsync; |
csmartdalton | 024229a | 2016-10-04 14:24:23 -0700 | [diff] [blame] | 33 | GR_STATIC_ASSERT(sizeof(GLsync) <= sizeof(sk_gpu_test::PlatformFence)); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 34 | |
| 35 | typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfield); |
| 36 | typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbitfield, GrGLuint64); |
| 37 | typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync); |
| 38 | |
| 39 | GLFenceSyncProc fGLFenceSync; |
| 40 | GLClientWaitSyncProc fGLClientWaitSync; |
| 41 | GLDeleteSyncProc fGLDeleteSync; |
| 42 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 43 | typedef FenceSync INHERITED; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame^] | 46 | std::unique_ptr<GLFenceSync> GLFenceSync::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) { |
| 47 | std::unique_ptr<GLFenceSync> ret; |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 48 | if (kGL_GrGLStandard == ctx->gl()->fStandard) { |
| 49 | if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync")) { |
| 50 | return nullptr; |
| 51 | } |
| 52 | ret.reset(new GLFenceSync(ctx)); |
| 53 | } else { |
| 54 | if (!ctx->gl()->hasExtension("GL_APPLE_sync")) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | ret.reset(new GLFenceSync(ctx, "APPLE")); |
| 58 | } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame^] | 59 | if (!ret->validate()) { |
| 60 | ret = nullptr; |
| 61 | } |
| 62 | return ret; |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | GLFenceSync::GLFenceSync(const sk_gpu_test::GLTestContext* ctx, const char* ext) { |
| 66 | ctx->getGLProcAddress(&fGLFenceSync, "glFenceSync"); |
| 67 | ctx->getGLProcAddress(&fGLClientWaitSync, "glClientWaitSync"); |
| 68 | ctx->getGLProcAddress(&fGLDeleteSync, "glDeleteSync"); |
| 69 | } |
| 70 | |
| 71 | sk_gpu_test::PlatformFence GLFenceSync::insertFence() const { |
| 72 | __GLsync* glsync = fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 73 | return reinterpret_cast<sk_gpu_test::PlatformFence>(glsync); |
| 74 | } |
| 75 | |
| 76 | bool GLFenceSync::waitFence(sk_gpu_test::PlatformFence fence) const { |
| 77 | GLsync glsync = reinterpret_cast<GLsync>(fence); |
| 78 | return GL_WAIT_FAILED != fGLClientWaitSync(glsync, GL_SYNC_FLUSH_COMMANDS_BIT, -1); |
| 79 | } |
| 80 | |
| 81 | void GLFenceSync::deleteFence(sk_gpu_test::PlatformFence fence) const { |
| 82 | GLsync glsync = reinterpret_cast<GLsync>(fence); |
| 83 | fGLDeleteSync(glsync); |
| 84 | } |
| 85 | |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 86 | class GLGpuTimer : public sk_gpu_test::GpuTimer { |
| 87 | public: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame^] | 88 | static std::unique_ptr<GLGpuTimer> MakeIfSupported(const sk_gpu_test::GLTestContext*); |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 89 | |
| 90 | QueryStatus checkQueryStatus(sk_gpu_test::PlatformTimerQuery) override; |
| 91 | std::chrono::nanoseconds getTimeElapsed(sk_gpu_test::PlatformTimerQuery) override; |
| 92 | void deleteQuery(sk_gpu_test::PlatformTimerQuery) override; |
| 93 | |
| 94 | private: |
| 95 | GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext*, const char* ext = ""); |
| 96 | |
| 97 | bool validate() const; |
| 98 | |
| 99 | sk_gpu_test::PlatformTimerQuery onQueueTimerStart() const override; |
| 100 | void onQueueTimerStop(sk_gpu_test::PlatformTimerQuery) const override; |
| 101 | |
| 102 | static constexpr GrGLenum GL_QUERY_RESULT = 0x8866; |
| 103 | static constexpr GrGLenum GL_QUERY_RESULT_AVAILABLE = 0x8867; |
| 104 | static constexpr GrGLenum GL_TIME_ELAPSED = 0x88bf; |
| 105 | static constexpr GrGLenum GL_GPU_DISJOINT = 0x8fbb; |
| 106 | |
| 107 | typedef void (GR_GL_FUNCTION_TYPE* GLGetIntegervProc) (GrGLenum, GrGLint*); |
| 108 | typedef void (GR_GL_FUNCTION_TYPE* GLGenQueriesProc) (GrGLsizei, GrGLuint*); |
| 109 | typedef void (GR_GL_FUNCTION_TYPE* GLDeleteQueriesProc) (GrGLsizei, const GrGLuint*); |
| 110 | typedef void (GR_GL_FUNCTION_TYPE* GLBeginQueryProc) (GrGLenum, GrGLuint); |
| 111 | typedef void (GR_GL_FUNCTION_TYPE* GLEndQueryProc) (GrGLenum); |
| 112 | typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectuivProc) (GrGLuint, GrGLenum, GrGLuint*); |
| 113 | typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectui64vProc) (GrGLuint, GrGLenum, GrGLuint64*); |
| 114 | |
| 115 | GLGetIntegervProc fGLGetIntegerv; |
| 116 | GLGenQueriesProc fGLGenQueries; |
| 117 | GLDeleteQueriesProc fGLDeleteQueries; |
| 118 | GLBeginQueryProc fGLBeginQuery; |
| 119 | GLEndQueryProc fGLEndQuery; |
| 120 | GLGetQueryObjectuivProc fGLGetQueryObjectuiv; |
| 121 | GLGetQueryObjectui64vProc fGLGetQueryObjectui64v; |
| 122 | |
| 123 | |
| 124 | typedef sk_gpu_test::GpuTimer INHERITED; |
| 125 | }; |
| 126 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame^] | 127 | std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) { |
| 128 | std::unique_ptr<GLGpuTimer> ret; |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 129 | const GrGLInterface* gl = ctx->gl(); |
| 130 | if (gl->fExtensions.has("GL_EXT_disjoint_timer_query")) { |
| 131 | ret.reset(new GLGpuTimer(true, ctx, "EXT")); |
| 132 | } else if (kGL_GrGLStandard == gl->fStandard && |
| 133 | (GrGLGetVersion(gl) > GR_GL_VER(3,3) || gl->fExtensions.has("GL_ARB_timer_query"))) { |
| 134 | ret.reset(new GLGpuTimer(false, ctx)); |
| 135 | } else if (gl->fExtensions.has("GL_EXT_timer_query")) { |
| 136 | ret.reset(new GLGpuTimer(false, ctx, "EXT")); |
| 137 | } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame^] | 138 | if (ret && !ret->validate()) { |
| 139 | ret = nullptr; |
| 140 | } |
| 141 | return ret; |
csmartdalton | c6618dd | 2016-10-05 08:42:03 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | GLGpuTimer::GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext* ctx, const char* ext) |
| 145 | : INHERITED(disjointSupport) { |
| 146 | ctx->getGLProcAddress(&fGLGetIntegerv, "glGetIntegerv"); |
| 147 | ctx->getGLProcAddress(&fGLGenQueries, "glGenQueries", ext); |
| 148 | ctx->getGLProcAddress(&fGLDeleteQueries, "glDeleteQueries", ext); |
| 149 | ctx->getGLProcAddress(&fGLBeginQuery, "glBeginQuery", ext); |
| 150 | ctx->getGLProcAddress(&fGLEndQuery, "glEndQuery", ext); |
| 151 | ctx->getGLProcAddress(&fGLGetQueryObjectuiv, "glGetQueryObjectuiv", ext); |
| 152 | ctx->getGLProcAddress(&fGLGetQueryObjectui64v, "glGetQueryObjectui64v", ext); |
| 153 | } |
| 154 | |
| 155 | bool GLGpuTimer::validate() const { |
| 156 | return fGLGetIntegerv && fGLGenQueries && fGLDeleteQueries && fGLBeginQuery && fGLEndQuery && |
| 157 | fGLGetQueryObjectuiv && fGLGetQueryObjectui64v; |
| 158 | } |
| 159 | |
| 160 | sk_gpu_test::PlatformTimerQuery GLGpuTimer::onQueueTimerStart() const { |
| 161 | GrGLuint queryID; |
| 162 | fGLGenQueries(1, &queryID); |
| 163 | if (!queryID) { |
| 164 | return sk_gpu_test::kInvalidTimerQuery; |
| 165 | } |
| 166 | if (this->disjointSupport()) { |
| 167 | // Clear the disjoint flag. |
| 168 | GrGLint disjoint; |
| 169 | fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint); |
| 170 | } |
| 171 | fGLBeginQuery(GL_TIME_ELAPSED, queryID); |
| 172 | return static_cast<sk_gpu_test::PlatformTimerQuery>(queryID); |
| 173 | } |
| 174 | |
| 175 | void GLGpuTimer::onQueueTimerStop(sk_gpu_test::PlatformTimerQuery platformTimer) const { |
| 176 | if (sk_gpu_test::kInvalidTimerQuery == platformTimer) { |
| 177 | return; |
| 178 | } |
| 179 | fGLEndQuery(GL_TIME_ELAPSED); |
| 180 | } |
| 181 | |
| 182 | sk_gpu_test::GpuTimer::QueryStatus |
| 183 | GLGpuTimer::checkQueryStatus(sk_gpu_test::PlatformTimerQuery platformTimer) { |
| 184 | const GrGLuint queryID = static_cast<GrGLuint>(platformTimer); |
| 185 | if (!queryID) { |
| 186 | return QueryStatus::kInvalid; |
| 187 | } |
| 188 | GrGLuint available = 0; |
| 189 | fGLGetQueryObjectuiv(queryID, GL_QUERY_RESULT_AVAILABLE, &available); |
| 190 | if (!available) { |
| 191 | return QueryStatus::kPending; |
| 192 | } |
| 193 | if (this->disjointSupport()) { |
| 194 | GrGLint disjoint = 1; |
| 195 | fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint); |
| 196 | if (disjoint) { |
| 197 | return QueryStatus::kDisjoint; |
| 198 | } |
| 199 | } |
| 200 | return QueryStatus::kAccurate; |
| 201 | } |
| 202 | |
| 203 | std::chrono::nanoseconds GLGpuTimer::getTimeElapsed(sk_gpu_test::PlatformTimerQuery platformTimer) { |
| 204 | SkASSERT(this->checkQueryStatus(platformTimer) >= QueryStatus::kDisjoint); |
| 205 | const GrGLuint queryID = static_cast<GrGLuint>(platformTimer); |
| 206 | GrGLuint64 nanoseconds; |
| 207 | fGLGetQueryObjectui64v(queryID, GL_QUERY_RESULT, &nanoseconds); |
| 208 | return std::chrono::nanoseconds(nanoseconds); |
| 209 | } |
| 210 | |
| 211 | void GLGpuTimer::deleteQuery(sk_gpu_test::PlatformTimerQuery platformTimer) { |
| 212 | const GrGLuint queryID = static_cast<GrGLuint>(platformTimer); |
| 213 | fGLDeleteQueries(1, &queryID); |
| 214 | } |
| 215 | |
| 216 | GR_STATIC_ASSERT(sizeof(GrGLuint) <= sizeof(sk_gpu_test::PlatformTimerQuery)); |
| 217 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 218 | } // anonymous namespace |
| 219 | |
| 220 | namespace sk_gpu_test { |
| 221 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 222 | GLTestContext::GLTestContext() : TestContext() {} |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 223 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 224 | GLTestContext::~GLTestContext() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 225 | SkASSERT(nullptr == fGL.get()); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame^] | 228 | void GLTestContext::init(const GrGLInterface* gl, std::unique_ptr<FenceSync> fenceSync) { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 229 | SkASSERT(!fGL.get()); |
| 230 | fGL.reset(gl); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame^] | 231 | fFenceSync = fenceSync ? std::move(fenceSync) : GLFenceSync::MakeIfSupported(this); |
| 232 | fGpuTimer = GLGpuTimer::MakeIfSupported(this); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 233 | } |
| 234 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 235 | void GLTestContext::teardown() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 236 | fGL.reset(nullptr); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 237 | INHERITED::teardown(); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 238 | } |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 239 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 240 | void GLTestContext::testAbandon() { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 241 | INHERITED::testAbandon(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 242 | if (fGL) { |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 243 | fGL->abandon(); |
| 244 | } |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 245 | } |
| 246 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 247 | void GLTestContext::submit() { |
| 248 | if (fGL) { |
| 249 | GR_GL_CALL(fGL.get(), Flush()); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void GLTestContext::finish() { |
| 254 | if (fGL) { |
| 255 | GR_GL_CALL(fGL.get(), Finish()); |
| 256 | } |
| 257 | } |
| 258 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 259 | GrGLint GLTestContext::createTextureRectangle(int width, int height, GrGLenum internalFormat, |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 260 | GrGLenum externalFormat, GrGLenum externalType, |
| 261 | GrGLvoid* data) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 262 | if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER(3, 1)) && |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 263 | !fGL->fExtensions.has("GL_ARB_texture_rectangle")) { |
| 264 | return 0; |
| 265 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 266 | |
| 267 | if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) { |
| 268 | return 0; |
| 269 | } |
| 270 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 271 | GrGLuint id; |
| 272 | GR_GL_CALL(fGL, GenTextures(1, &id)); |
| 273 | GR_GL_CALL(fGL, BindTexture(GR_GL_TEXTURE_RECTANGLE, id)); |
| 274 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER, |
| 275 | GR_GL_NEAREST)); |
| 276 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER, |
| 277 | GR_GL_NEAREST)); |
| 278 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S, |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 279 | GR_GL_CLAMP_TO_EDGE)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 280 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T, |
| 281 | GR_GL_CLAMP_TO_EDGE)); |
| 282 | GR_GL_CALL(fGL, TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0, |
| 283 | externalFormat, externalType, data)); |
| 284 | return id; |
| 285 | } |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 286 | } // namespace sk_gpu_test |