blob: e11f1b0eeff3561f4b0bd4cdaf47d4411a41bbe7 [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
Brian Salomon3d6801e2017-12-11 10:06:31 -0500287void GLTestContext::init(sk_sp<const GrGLInterface> gl, std::unique_ptr<FenceSync> fenceSync) {
288 fGL = std::move(gl);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400289 fFenceSync = fenceSync ? std::move(fenceSync) : GLFenceSync::MakeIfSupported(this);
290 fGpuTimer = GLGpuTimer::MakeIfSupported(this);
cdaltond416a5b2015-06-23 13:23:44 -0700291}
292
bsalomon273c0f52016-03-31 10:59:06 -0700293void GLTestContext::teardown() {
halcanary96fcdcc2015-08-27 07:41:13 -0700294 fGL.reset(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -0700295 INHERITED::teardown();
bsalomon@google.com373a6632011-10-19 20:43:20 +0000296}
bsalomon944bcf02014-07-29 08:01:52 -0700297
bsalomon273c0f52016-03-31 10:59:06 -0700298void GLTestContext::testAbandon() {
bsalomon18a2f9d2016-05-11 10:09:18 -0700299 INHERITED::testAbandon();
bsalomon49f085d2014-09-05 13:34:00 -0700300 if (fGL) {
bsalomon944bcf02014-07-29 08:01:52 -0700301 fGL->abandon();
302 }
cdaltond416a5b2015-06-23 13:23:44 -0700303}
304
bsalomonc8699322016-05-11 11:55:36 -0700305void GLTestContext::submit() {
306 if (fGL) {
307 GR_GL_CALL(fGL.get(), Flush());
308 }
309}
310
311void GLTestContext::finish() {
312 if (fGL) {
313 GR_GL_CALL(fGL.get(), Finish());
314 }
315}
316
bsalomon273c0f52016-03-31 10:59:06 -0700317GrGLint GLTestContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
bsalomon3724e572016-03-30 18:56:19 -0700318 GrGLenum externalFormat, GrGLenum externalType,
319 GrGLvoid* data) {
Weiliang Chen1e04b362018-07-12 17:13:35 -0400320 // Should match GrGLCaps check for fRectangleTextureSupport.
321 if (kGL_GrGLStandard != fGL->fStandard ||
322 (GrGLGetVersion(fGL.get()) < GR_GL_VER(3, 1) &&
323 !fGL->fExtensions.has("GL_ARB_texture_rectangle") &&
324 !fGL->fExtensions.has("GL_ANGLE_texture_rectangle"))) {
bsalomone5286e02016-01-14 09:24:09 -0800325 return 0;
326 }
bsalomone179a912016-01-20 06:18:10 -0800327
Hal Canary1b612a82016-11-03 16:26:13 -0400328 if (GrGLGetGLSLVersion(fGL.get()) < GR_GLSL_VER(1, 40)) {
bsalomone179a912016-01-20 06:18:10 -0800329 return 0;
330 }
331
bsalomone5286e02016-01-14 09:24:09 -0800332 GrGLuint id;
Hal Canary1b612a82016-11-03 16:26:13 -0400333 GR_GL_CALL(fGL.get(), GenTextures(1, &id));
334 GR_GL_CALL(fGL.get(), BindTexture(GR_GL_TEXTURE_RECTANGLE, id));
335 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER,
336 GR_GL_NEAREST));
337 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER,
338 GR_GL_NEAREST));
339 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S,
340 GR_GL_CLAMP_TO_EDGE));
341 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T,
342 GR_GL_CLAMP_TO_EDGE));
343 GR_GL_CALL(fGL.get(), TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0,
344 externalFormat, externalType, data));
bsalomone5286e02016-01-14 09:24:09 -0800345 return id;
346}
Greg Daniel02611d92017-07-25 10:05:01 -0400347
348sk_sp<GrContext> GLTestContext::makeGrContext(const GrContextOptions& options) {
Brian Salomon384fab42017-12-07 12:33:05 -0500349 return GrContext::MakeGL(fGL, options);
Greg Daniel02611d92017-07-25 10:05:01 -0400350}
351
bsalomon3724e572016-03-30 18:56:19 -0700352} // namespace sk_gpu_test