blob: 2c1d977b76f35047d50cc45d5d2e9d520f09a17c [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001/*
robertphillips@google.com6177e692013-02-28 20:16:25 +00002 * Copyright 2013 Google Inc.
bsalomon@google.com373a6632011-10-19 20:43:20 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bsalomon18a2f9d2016-05-11 10:09:18 -07007
bsalomon273c0f52016-03-31 10:59:06 -07008#include "GLTestContext.h"
csmartdaltonc6618dd2016-10-05 08:42:03 -07009
Greg Daniel02611d92017-07-25 10:05:01 -040010#include "GrContext.h"
csmartdaltonc6618dd2016-10-05 08:42:03 -070011#include "GpuTimer.h"
bsalomon3724e572016-03-30 18:56:19 -070012#include "gl/GrGLUtil.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000013
csmartdalton421a3c12016-10-04 11:08:45 -070014namespace {
cdaltond416a5b2015-06-23 13:23:44 -070015
csmartdalton421a3c12016-10-04 11:08:45 -070016class GLFenceSync : public sk_gpu_test::FenceSync {
17public:
Brian Osman5fbd1832017-10-10 16:39:50 -040018 static std::unique_ptr<FenceSync> MakeIfSupported(const sk_gpu_test::GLTestContext*);
csmartdalton421a3c12016-10-04 11:08:45 -070019
20 sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override;
21 bool waitFence(sk_gpu_test::PlatformFence fence) const override;
22 void deleteFence(sk_gpu_test::PlatformFence fence) const override;
cdaltond416a5b2015-06-23 13:23:44 -070023
24private:
csmartdalton421a3c12016-10-04 11:08:45 -070025 GLFenceSync(const sk_gpu_test::GLTestContext*, const char* ext = "");
cdaltond416a5b2015-06-23 13:23:44 -070026
Brian Osman5fbd1832017-10-10 16:39:50 -040027 bool validate() const override { return fGLFenceSync && fGLClientWaitSync && fGLDeleteSync; }
csmartdalton421a3c12016-10-04 11:08:45 -070028
29 static constexpr GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117;
30 static constexpr GrGLenum GL_WAIT_FAILED = 0x911d;
31 static constexpr GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001;
cdaltond416a5b2015-06-23 13:23:44 -070032
33 typedef struct __GLsync *GLsync;
csmartdalton024229a2016-10-04 14:24:23 -070034 GR_STATIC_ASSERT(sizeof(GLsync) <= sizeof(sk_gpu_test::PlatformFence));
cdaltond416a5b2015-06-23 13:23:44 -070035
36 typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfield);
37 typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbitfield, GrGLuint64);
38 typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync);
39
40 GLFenceSyncProc fGLFenceSync;
41 GLClientWaitSyncProc fGLClientWaitSync;
42 GLDeleteSyncProc fGLDeleteSync;
43
csmartdalton421a3c12016-10-04 11:08:45 -070044 typedef FenceSync INHERITED;
cdaltond416a5b2015-06-23 13:23:44 -070045};
46
Brian Osman5fbd1832017-10-10 16:39:50 -040047class GLNVFenceSync : public sk_gpu_test::FenceSync {
48public:
49 GLNVFenceSync(const sk_gpu_test::GLTestContext*);
50
51 sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override;
52 bool waitFence(sk_gpu_test::PlatformFence fence) const override;
53 void deleteFence(sk_gpu_test::PlatformFence fence) const override;
54
55private:
56 bool validate() const override {
57 return fGLGenFencesNV && fGLDeleteFencesNV && fGLSetFenceNV && fGLFinishFenceNV;
58 }
59
60 static constexpr GrGLenum GL_ALL_COMPLETED_NV = 0x84F2;
61
62 typedef GrGLvoid(GR_GL_FUNCTION_TYPE* GLGenFencesNVProc) (GrGLsizei, GrGLuint*);
63 typedef GrGLvoid(GR_GL_FUNCTION_TYPE* GLDeleteFencesNVProc) (GrGLsizei, const GrGLuint*);
64 typedef GrGLvoid(GR_GL_FUNCTION_TYPE* GLSetFenceNVProc) (GrGLuint, GrGLenum);
65 typedef GrGLvoid(GR_GL_FUNCTION_TYPE* GLFinishFenceNVProc) (GrGLuint);
66
67 GLGenFencesNVProc fGLGenFencesNV;
68 GLDeleteFencesNVProc fGLDeleteFencesNV;
69 GLSetFenceNVProc fGLSetFenceNV;
70 GLFinishFenceNVProc fGLFinishFenceNV;
71
72 typedef FenceSync INHERITED;
73};
74
75std::unique_ptr<sk_gpu_test::FenceSync> GLFenceSync::MakeIfSupported(
76 const sk_gpu_test::GLTestContext* ctx) {
77 std::unique_ptr<FenceSync> ret;
csmartdalton421a3c12016-10-04 11:08:45 -070078 if (kGL_GrGLStandard == ctx->gl()->fStandard) {
79 if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync")) {
80 return nullptr;
81 }
82 ret.reset(new GLFenceSync(ctx));
83 } else {
Brian Osman80be2402017-04-21 15:12:30 -040084 if (ctx->gl()->hasExtension("GL_APPLE_sync")) {
85 ret.reset(new GLFenceSync(ctx, "APPLE"));
Brian Osman5fbd1832017-10-10 16:39:50 -040086 } else if (ctx->gl()->hasExtension("GL_NV_fence")) {
87 ret.reset(new GLNVFenceSync(ctx));
Brian Osman80be2402017-04-21 15:12:30 -040088 } else if (GrGLGetVersion(ctx->gl()) >= GR_GL_VER(3, 0)) {
89 ret.reset(new GLFenceSync(ctx));
90 } else {
csmartdalton421a3c12016-10-04 11:08:45 -070091 return nullptr;
92 }
csmartdalton421a3c12016-10-04 11:08:45 -070093 }
Ben Wagner145dbcd2016-11-03 14:40:50 -040094 if (!ret->validate()) {
95 ret = nullptr;
96 }
97 return ret;
csmartdalton421a3c12016-10-04 11:08:45 -070098}
99
100GLFenceSync::GLFenceSync(const sk_gpu_test::GLTestContext* ctx, const char* ext) {
Brian Osmane6984a02017-10-10 14:45:29 -0400101 ctx->getGLProcAddress(&fGLFenceSync, "glFenceSync", ext);
102 ctx->getGLProcAddress(&fGLClientWaitSync, "glClientWaitSync", ext);
103 ctx->getGLProcAddress(&fGLDeleteSync, "glDeleteSync", ext);
csmartdalton421a3c12016-10-04 11:08:45 -0700104}
105
106sk_gpu_test::PlatformFence GLFenceSync::insertFence() const {
107 __GLsync* glsync = fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
108 return reinterpret_cast<sk_gpu_test::PlatformFence>(glsync);
109}
110
111bool GLFenceSync::waitFence(sk_gpu_test::PlatformFence fence) const {
112 GLsync glsync = reinterpret_cast<GLsync>(fence);
113 return GL_WAIT_FAILED != fGLClientWaitSync(glsync, GL_SYNC_FLUSH_COMMANDS_BIT, -1);
114}
115
116void GLFenceSync::deleteFence(sk_gpu_test::PlatformFence fence) const {
117 GLsync glsync = reinterpret_cast<GLsync>(fence);
118 fGLDeleteSync(glsync);
119}
120
Brian Osman5fbd1832017-10-10 16:39:50 -0400121GLNVFenceSync::GLNVFenceSync(const sk_gpu_test::GLTestContext* ctx) {
122 ctx->getGLProcAddress(&fGLGenFencesNV, "glGenFencesNV");
123 ctx->getGLProcAddress(&fGLDeleteFencesNV, "glDeleteFencesNV");
124 ctx->getGLProcAddress(&fGLSetFenceNV, "glSetFenceNV");
125 ctx->getGLProcAddress(&fGLFinishFenceNV, "glFinishFenceNV");
126}
127
128sk_gpu_test::PlatformFence GLNVFenceSync::insertFence() const {
129 GrGLuint fence;
130 fGLGenFencesNV(1, &fence);
131 fGLSetFenceNV(fence, GL_ALL_COMPLETED_NV);
132 return fence;
133}
134
135bool GLNVFenceSync::waitFence(sk_gpu_test::PlatformFence fence) const {
136 fGLFinishFenceNV(fence);
137 return true;
138}
139
140void GLNVFenceSync::deleteFence(sk_gpu_test::PlatformFence fence) const {
141 GrGLuint glFence = static_cast<GrGLuint>(fence);
142 fGLDeleteFencesNV(1, &glFence);
143}
144
csmartdaltonc6618dd2016-10-05 08:42:03 -0700145class GLGpuTimer : public sk_gpu_test::GpuTimer {
146public:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400147 static std::unique_ptr<GLGpuTimer> MakeIfSupported(const sk_gpu_test::GLTestContext*);
csmartdaltonc6618dd2016-10-05 08:42:03 -0700148
149 QueryStatus checkQueryStatus(sk_gpu_test::PlatformTimerQuery) override;
150 std::chrono::nanoseconds getTimeElapsed(sk_gpu_test::PlatformTimerQuery) override;
151 void deleteQuery(sk_gpu_test::PlatformTimerQuery) override;
152
153private:
154 GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext*, const char* ext = "");
155
156 bool validate() const;
157
158 sk_gpu_test::PlatformTimerQuery onQueueTimerStart() const override;
159 void onQueueTimerStop(sk_gpu_test::PlatformTimerQuery) const override;
160
161 static constexpr GrGLenum GL_QUERY_RESULT = 0x8866;
162 static constexpr GrGLenum GL_QUERY_RESULT_AVAILABLE = 0x8867;
163 static constexpr GrGLenum GL_TIME_ELAPSED = 0x88bf;
164 static constexpr GrGLenum GL_GPU_DISJOINT = 0x8fbb;
165
166 typedef void (GR_GL_FUNCTION_TYPE* GLGetIntegervProc) (GrGLenum, GrGLint*);
167 typedef void (GR_GL_FUNCTION_TYPE* GLGenQueriesProc) (GrGLsizei, GrGLuint*);
168 typedef void (GR_GL_FUNCTION_TYPE* GLDeleteQueriesProc) (GrGLsizei, const GrGLuint*);
169 typedef void (GR_GL_FUNCTION_TYPE* GLBeginQueryProc) (GrGLenum, GrGLuint);
170 typedef void (GR_GL_FUNCTION_TYPE* GLEndQueryProc) (GrGLenum);
171 typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectuivProc) (GrGLuint, GrGLenum, GrGLuint*);
172 typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectui64vProc) (GrGLuint, GrGLenum, GrGLuint64*);
173
174 GLGetIntegervProc fGLGetIntegerv;
175 GLGenQueriesProc fGLGenQueries;
176 GLDeleteQueriesProc fGLDeleteQueries;
177 GLBeginQueryProc fGLBeginQuery;
178 GLEndQueryProc fGLEndQuery;
179 GLGetQueryObjectuivProc fGLGetQueryObjectuiv;
180 GLGetQueryObjectui64vProc fGLGetQueryObjectui64v;
181
182
183 typedef sk_gpu_test::GpuTimer INHERITED;
184};
185
Ben Wagner145dbcd2016-11-03 14:40:50 -0400186std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
187 std::unique_ptr<GLGpuTimer> ret;
csmartdaltonc6618dd2016-10-05 08:42:03 -0700188 const GrGLInterface* gl = ctx->gl();
189 if (gl->fExtensions.has("GL_EXT_disjoint_timer_query")) {
190 ret.reset(new GLGpuTimer(true, ctx, "EXT"));
191 } else if (kGL_GrGLStandard == gl->fStandard &&
192 (GrGLGetVersion(gl) > GR_GL_VER(3,3) || gl->fExtensions.has("GL_ARB_timer_query"))) {
193 ret.reset(new GLGpuTimer(false, ctx));
194 } else if (gl->fExtensions.has("GL_EXT_timer_query")) {
195 ret.reset(new GLGpuTimer(false, ctx, "EXT"));
196 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400197 if (ret && !ret->validate()) {
198 ret = nullptr;
199 }
200 return ret;
csmartdaltonc6618dd2016-10-05 08:42:03 -0700201}
202
203GLGpuTimer::GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext* ctx, const char* ext)
204 : INHERITED(disjointSupport) {
205 ctx->getGLProcAddress(&fGLGetIntegerv, "glGetIntegerv");
206 ctx->getGLProcAddress(&fGLGenQueries, "glGenQueries", ext);
207 ctx->getGLProcAddress(&fGLDeleteQueries, "glDeleteQueries", ext);
208 ctx->getGLProcAddress(&fGLBeginQuery, "glBeginQuery", ext);
209 ctx->getGLProcAddress(&fGLEndQuery, "glEndQuery", ext);
210 ctx->getGLProcAddress(&fGLGetQueryObjectuiv, "glGetQueryObjectuiv", ext);
211 ctx->getGLProcAddress(&fGLGetQueryObjectui64v, "glGetQueryObjectui64v", ext);
212}
213
214bool GLGpuTimer::validate() const {
215 return fGLGetIntegerv && fGLGenQueries && fGLDeleteQueries && fGLBeginQuery && fGLEndQuery &&
216 fGLGetQueryObjectuiv && fGLGetQueryObjectui64v;
217}
218
219sk_gpu_test::PlatformTimerQuery GLGpuTimer::onQueueTimerStart() const {
220 GrGLuint queryID;
221 fGLGenQueries(1, &queryID);
222 if (!queryID) {
223 return sk_gpu_test::kInvalidTimerQuery;
224 }
225 if (this->disjointSupport()) {
226 // Clear the disjoint flag.
227 GrGLint disjoint;
228 fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint);
229 }
230 fGLBeginQuery(GL_TIME_ELAPSED, queryID);
231 return static_cast<sk_gpu_test::PlatformTimerQuery>(queryID);
232}
233
234void GLGpuTimer::onQueueTimerStop(sk_gpu_test::PlatformTimerQuery platformTimer) const {
235 if (sk_gpu_test::kInvalidTimerQuery == platformTimer) {
236 return;
237 }
238 fGLEndQuery(GL_TIME_ELAPSED);
239}
240
241sk_gpu_test::GpuTimer::QueryStatus
242GLGpuTimer::checkQueryStatus(sk_gpu_test::PlatformTimerQuery platformTimer) {
243 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
244 if (!queryID) {
245 return QueryStatus::kInvalid;
246 }
247 GrGLuint available = 0;
248 fGLGetQueryObjectuiv(queryID, GL_QUERY_RESULT_AVAILABLE, &available);
249 if (!available) {
250 return QueryStatus::kPending;
251 }
252 if (this->disjointSupport()) {
253 GrGLint disjoint = 1;
254 fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint);
255 if (disjoint) {
256 return QueryStatus::kDisjoint;
257 }
258 }
259 return QueryStatus::kAccurate;
260}
261
262std::chrono::nanoseconds GLGpuTimer::getTimeElapsed(sk_gpu_test::PlatformTimerQuery platformTimer) {
263 SkASSERT(this->checkQueryStatus(platformTimer) >= QueryStatus::kDisjoint);
264 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
265 GrGLuint64 nanoseconds;
266 fGLGetQueryObjectui64v(queryID, GL_QUERY_RESULT, &nanoseconds);
267 return std::chrono::nanoseconds(nanoseconds);
268}
269
270void GLGpuTimer::deleteQuery(sk_gpu_test::PlatformTimerQuery platformTimer) {
271 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
272 fGLDeleteQueries(1, &queryID);
273}
274
275GR_STATIC_ASSERT(sizeof(GrGLuint) <= sizeof(sk_gpu_test::PlatformTimerQuery));
276
csmartdalton421a3c12016-10-04 11:08:45 -0700277} // anonymous namespace
278
279namespace sk_gpu_test {
280
bsalomon18a2f9d2016-05-11 10:09:18 -0700281GLTestContext::GLTestContext() : TestContext() {}
bsalomon@google.com373a6632011-10-19 20:43:20 +0000282
bsalomon273c0f52016-03-31 10:59:06 -0700283GLTestContext::~GLTestContext() {
halcanary96fcdcc2015-08-27 07:41:13 -0700284 SkASSERT(nullptr == fGL.get());
cdaltond416a5b2015-06-23 13:23:44 -0700285}
286
Ben Wagner145dbcd2016-11-03 14:40:50 -0400287void GLTestContext::init(const GrGLInterface* gl, std::unique_ptr<FenceSync> fenceSync) {
cdaltond416a5b2015-06-23 13:23:44 -0700288 SkASSERT(!fGL.get());
289 fGL.reset(gl);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400290 fFenceSync = fenceSync ? std::move(fenceSync) : GLFenceSync::MakeIfSupported(this);
291 fGpuTimer = GLGpuTimer::MakeIfSupported(this);
cdaltond416a5b2015-06-23 13:23:44 -0700292}
293
bsalomon273c0f52016-03-31 10:59:06 -0700294void GLTestContext::teardown() {
halcanary96fcdcc2015-08-27 07:41:13 -0700295 fGL.reset(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -0700296 INHERITED::teardown();
bsalomon@google.com373a6632011-10-19 20:43:20 +0000297}
bsalomon944bcf02014-07-29 08:01:52 -0700298
bsalomon273c0f52016-03-31 10:59:06 -0700299void GLTestContext::testAbandon() {
bsalomon18a2f9d2016-05-11 10:09:18 -0700300 INHERITED::testAbandon();
bsalomon49f085d2014-09-05 13:34:00 -0700301 if (fGL) {
bsalomon944bcf02014-07-29 08:01:52 -0700302 fGL->abandon();
303 }
cdaltond416a5b2015-06-23 13:23:44 -0700304}
305
bsalomonc8699322016-05-11 11:55:36 -0700306void GLTestContext::submit() {
307 if (fGL) {
308 GR_GL_CALL(fGL.get(), Flush());
309 }
310}
311
312void GLTestContext::finish() {
313 if (fGL) {
314 GR_GL_CALL(fGL.get(), Finish());
315 }
316}
317
bsalomon273c0f52016-03-31 10:59:06 -0700318GrGLint GLTestContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
bsalomon3724e572016-03-30 18:56:19 -0700319 GrGLenum externalFormat, GrGLenum externalType,
320 GrGLvoid* data) {
Hal Canary1b612a82016-11-03 16:26:13 -0400321 if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL.get()) >= GR_GL_VER(3, 1)) &&
bsalomone5286e02016-01-14 09:24:09 -0800322 !fGL->fExtensions.has("GL_ARB_texture_rectangle")) {
323 return 0;
324 }
bsalomone179a912016-01-20 06:18:10 -0800325
Hal Canary1b612a82016-11-03 16:26:13 -0400326 if (GrGLGetGLSLVersion(fGL.get()) < GR_GLSL_VER(1, 40)) {
bsalomone179a912016-01-20 06:18:10 -0800327 return 0;
328 }
329
bsalomone5286e02016-01-14 09:24:09 -0800330 GrGLuint id;
Hal Canary1b612a82016-11-03 16:26:13 -0400331 GR_GL_CALL(fGL.get(), GenTextures(1, &id));
332 GR_GL_CALL(fGL.get(), BindTexture(GR_GL_TEXTURE_RECTANGLE, id));
333 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER,
334 GR_GL_NEAREST));
335 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER,
336 GR_GL_NEAREST));
337 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S,
338 GR_GL_CLAMP_TO_EDGE));
339 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T,
340 GR_GL_CLAMP_TO_EDGE));
341 GR_GL_CALL(fGL.get(), TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0,
342 externalFormat, externalType, data));
bsalomone5286e02016-01-14 09:24:09 -0800343 return id;
344}
Greg Daniel02611d92017-07-25 10:05:01 -0400345
346sk_sp<GrContext> GLTestContext::makeGrContext(const GrContextOptions& options) {
Brian Salomon384fab42017-12-07 12:33:05 -0500347 return GrContext::MakeGL(fGL, options);
Greg Daniel02611d92017-07-25 10:05:01 -0400348}
349
bsalomon3724e572016-03-30 18:56:19 -0700350} // namespace sk_gpu_test