blob: 91fa82ff9a86a94c1b0455b11b1e7a6aab8eae5a [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:
Ben Wagner145dbcd2016-11-03 14:40:50 -040018 static std::unique_ptr<GLFenceSync> 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
csmartdalton421a3c12016-10-04 11:08:45 -070027 bool validate() { return fGLFenceSync && fGLClientWaitSync && fGLDeleteSync; }
28
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
Ben Wagner145dbcd2016-11-03 14:40:50 -040047std::unique_ptr<GLFenceSync> GLFenceSync::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
48 std::unique_ptr<GLFenceSync> ret;
csmartdalton421a3c12016-10-04 11:08:45 -070049 if (kGL_GrGLStandard == ctx->gl()->fStandard) {
50 if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync")) {
51 return nullptr;
52 }
53 ret.reset(new GLFenceSync(ctx));
54 } else {
Brian Osman80be2402017-04-21 15:12:30 -040055 if (ctx->gl()->hasExtension("GL_APPLE_sync")) {
56 ret.reset(new GLFenceSync(ctx, "APPLE"));
57 } else if (GrGLGetVersion(ctx->gl()) >= GR_GL_VER(3, 0)) {
58 ret.reset(new GLFenceSync(ctx));
59 } else {
csmartdalton421a3c12016-10-04 11:08:45 -070060 return nullptr;
61 }
csmartdalton421a3c12016-10-04 11:08:45 -070062 }
Ben Wagner145dbcd2016-11-03 14:40:50 -040063 if (!ret->validate()) {
64 ret = nullptr;
65 }
66 return ret;
csmartdalton421a3c12016-10-04 11:08:45 -070067}
68
69GLFenceSync::GLFenceSync(const sk_gpu_test::GLTestContext* ctx, const char* ext) {
70 ctx->getGLProcAddress(&fGLFenceSync, "glFenceSync");
71 ctx->getGLProcAddress(&fGLClientWaitSync, "glClientWaitSync");
72 ctx->getGLProcAddress(&fGLDeleteSync, "glDeleteSync");
73}
74
75sk_gpu_test::PlatformFence GLFenceSync::insertFence() const {
76 __GLsync* glsync = fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
77 return reinterpret_cast<sk_gpu_test::PlatformFence>(glsync);
78}
79
80bool GLFenceSync::waitFence(sk_gpu_test::PlatformFence fence) const {
81 GLsync glsync = reinterpret_cast<GLsync>(fence);
82 return GL_WAIT_FAILED != fGLClientWaitSync(glsync, GL_SYNC_FLUSH_COMMANDS_BIT, -1);
83}
84
85void GLFenceSync::deleteFence(sk_gpu_test::PlatformFence fence) const {
86 GLsync glsync = reinterpret_cast<GLsync>(fence);
87 fGLDeleteSync(glsync);
88}
89
csmartdaltonc6618dd2016-10-05 08:42:03 -070090class GLGpuTimer : public sk_gpu_test::GpuTimer {
91public:
Ben Wagner145dbcd2016-11-03 14:40:50 -040092 static std::unique_ptr<GLGpuTimer> MakeIfSupported(const sk_gpu_test::GLTestContext*);
csmartdaltonc6618dd2016-10-05 08:42:03 -070093
94 QueryStatus checkQueryStatus(sk_gpu_test::PlatformTimerQuery) override;
95 std::chrono::nanoseconds getTimeElapsed(sk_gpu_test::PlatformTimerQuery) override;
96 void deleteQuery(sk_gpu_test::PlatformTimerQuery) override;
97
98private:
99 GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext*, const char* ext = "");
100
101 bool validate() const;
102
103 sk_gpu_test::PlatformTimerQuery onQueueTimerStart() const override;
104 void onQueueTimerStop(sk_gpu_test::PlatformTimerQuery) const override;
105
106 static constexpr GrGLenum GL_QUERY_RESULT = 0x8866;
107 static constexpr GrGLenum GL_QUERY_RESULT_AVAILABLE = 0x8867;
108 static constexpr GrGLenum GL_TIME_ELAPSED = 0x88bf;
109 static constexpr GrGLenum GL_GPU_DISJOINT = 0x8fbb;
110
111 typedef void (GR_GL_FUNCTION_TYPE* GLGetIntegervProc) (GrGLenum, GrGLint*);
112 typedef void (GR_GL_FUNCTION_TYPE* GLGenQueriesProc) (GrGLsizei, GrGLuint*);
113 typedef void (GR_GL_FUNCTION_TYPE* GLDeleteQueriesProc) (GrGLsizei, const GrGLuint*);
114 typedef void (GR_GL_FUNCTION_TYPE* GLBeginQueryProc) (GrGLenum, GrGLuint);
115 typedef void (GR_GL_FUNCTION_TYPE* GLEndQueryProc) (GrGLenum);
116 typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectuivProc) (GrGLuint, GrGLenum, GrGLuint*);
117 typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectui64vProc) (GrGLuint, GrGLenum, GrGLuint64*);
118
119 GLGetIntegervProc fGLGetIntegerv;
120 GLGenQueriesProc fGLGenQueries;
121 GLDeleteQueriesProc fGLDeleteQueries;
122 GLBeginQueryProc fGLBeginQuery;
123 GLEndQueryProc fGLEndQuery;
124 GLGetQueryObjectuivProc fGLGetQueryObjectuiv;
125 GLGetQueryObjectui64vProc fGLGetQueryObjectui64v;
126
127
128 typedef sk_gpu_test::GpuTimer INHERITED;
129};
130
Ben Wagner145dbcd2016-11-03 14:40:50 -0400131std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
132 std::unique_ptr<GLGpuTimer> ret;
csmartdaltonc6618dd2016-10-05 08:42:03 -0700133 const GrGLInterface* gl = ctx->gl();
134 if (gl->fExtensions.has("GL_EXT_disjoint_timer_query")) {
135 ret.reset(new GLGpuTimer(true, ctx, "EXT"));
136 } else if (kGL_GrGLStandard == gl->fStandard &&
137 (GrGLGetVersion(gl) > GR_GL_VER(3,3) || gl->fExtensions.has("GL_ARB_timer_query"))) {
138 ret.reset(new GLGpuTimer(false, ctx));
139 } else if (gl->fExtensions.has("GL_EXT_timer_query")) {
140 ret.reset(new GLGpuTimer(false, ctx, "EXT"));
141 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400142 if (ret && !ret->validate()) {
143 ret = nullptr;
144 }
145 return ret;
csmartdaltonc6618dd2016-10-05 08:42:03 -0700146}
147
148GLGpuTimer::GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext* ctx, const char* ext)
149 : INHERITED(disjointSupport) {
150 ctx->getGLProcAddress(&fGLGetIntegerv, "glGetIntegerv");
151 ctx->getGLProcAddress(&fGLGenQueries, "glGenQueries", ext);
152 ctx->getGLProcAddress(&fGLDeleteQueries, "glDeleteQueries", ext);
153 ctx->getGLProcAddress(&fGLBeginQuery, "glBeginQuery", ext);
154 ctx->getGLProcAddress(&fGLEndQuery, "glEndQuery", ext);
155 ctx->getGLProcAddress(&fGLGetQueryObjectuiv, "glGetQueryObjectuiv", ext);
156 ctx->getGLProcAddress(&fGLGetQueryObjectui64v, "glGetQueryObjectui64v", ext);
157}
158
159bool GLGpuTimer::validate() const {
160 return fGLGetIntegerv && fGLGenQueries && fGLDeleteQueries && fGLBeginQuery && fGLEndQuery &&
161 fGLGetQueryObjectuiv && fGLGetQueryObjectui64v;
162}
163
164sk_gpu_test::PlatformTimerQuery GLGpuTimer::onQueueTimerStart() const {
165 GrGLuint queryID;
166 fGLGenQueries(1, &queryID);
167 if (!queryID) {
168 return sk_gpu_test::kInvalidTimerQuery;
169 }
170 if (this->disjointSupport()) {
171 // Clear the disjoint flag.
172 GrGLint disjoint;
173 fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint);
174 }
175 fGLBeginQuery(GL_TIME_ELAPSED, queryID);
176 return static_cast<sk_gpu_test::PlatformTimerQuery>(queryID);
177}
178
179void GLGpuTimer::onQueueTimerStop(sk_gpu_test::PlatformTimerQuery platformTimer) const {
180 if (sk_gpu_test::kInvalidTimerQuery == platformTimer) {
181 return;
182 }
183 fGLEndQuery(GL_TIME_ELAPSED);
184}
185
186sk_gpu_test::GpuTimer::QueryStatus
187GLGpuTimer::checkQueryStatus(sk_gpu_test::PlatformTimerQuery platformTimer) {
188 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
189 if (!queryID) {
190 return QueryStatus::kInvalid;
191 }
192 GrGLuint available = 0;
193 fGLGetQueryObjectuiv(queryID, GL_QUERY_RESULT_AVAILABLE, &available);
194 if (!available) {
195 return QueryStatus::kPending;
196 }
197 if (this->disjointSupport()) {
198 GrGLint disjoint = 1;
199 fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint);
200 if (disjoint) {
201 return QueryStatus::kDisjoint;
202 }
203 }
204 return QueryStatus::kAccurate;
205}
206
207std::chrono::nanoseconds GLGpuTimer::getTimeElapsed(sk_gpu_test::PlatformTimerQuery platformTimer) {
208 SkASSERT(this->checkQueryStatus(platformTimer) >= QueryStatus::kDisjoint);
209 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
210 GrGLuint64 nanoseconds;
211 fGLGetQueryObjectui64v(queryID, GL_QUERY_RESULT, &nanoseconds);
212 return std::chrono::nanoseconds(nanoseconds);
213}
214
215void GLGpuTimer::deleteQuery(sk_gpu_test::PlatformTimerQuery platformTimer) {
216 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
217 fGLDeleteQueries(1, &queryID);
218}
219
220GR_STATIC_ASSERT(sizeof(GrGLuint) <= sizeof(sk_gpu_test::PlatformTimerQuery));
221
csmartdalton421a3c12016-10-04 11:08:45 -0700222} // anonymous namespace
223
224namespace sk_gpu_test {
225
bsalomon18a2f9d2016-05-11 10:09:18 -0700226GLTestContext::GLTestContext() : TestContext() {}
bsalomon@google.com373a6632011-10-19 20:43:20 +0000227
bsalomon273c0f52016-03-31 10:59:06 -0700228GLTestContext::~GLTestContext() {
halcanary96fcdcc2015-08-27 07:41:13 -0700229 SkASSERT(nullptr == fGL.get());
cdaltond416a5b2015-06-23 13:23:44 -0700230}
231
Ben Wagner145dbcd2016-11-03 14:40:50 -0400232void GLTestContext::init(const GrGLInterface* gl, std::unique_ptr<FenceSync> fenceSync) {
cdaltond416a5b2015-06-23 13:23:44 -0700233 SkASSERT(!fGL.get());
234 fGL.reset(gl);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400235 fFenceSync = fenceSync ? std::move(fenceSync) : GLFenceSync::MakeIfSupported(this);
236 fGpuTimer = GLGpuTimer::MakeIfSupported(this);
cdaltond416a5b2015-06-23 13:23:44 -0700237}
238
bsalomon273c0f52016-03-31 10:59:06 -0700239void GLTestContext::teardown() {
halcanary96fcdcc2015-08-27 07:41:13 -0700240 fGL.reset(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -0700241 INHERITED::teardown();
bsalomon@google.com373a6632011-10-19 20:43:20 +0000242}
bsalomon944bcf02014-07-29 08:01:52 -0700243
bsalomon273c0f52016-03-31 10:59:06 -0700244void GLTestContext::testAbandon() {
bsalomon18a2f9d2016-05-11 10:09:18 -0700245 INHERITED::testAbandon();
bsalomon49f085d2014-09-05 13:34:00 -0700246 if (fGL) {
bsalomon944bcf02014-07-29 08:01:52 -0700247 fGL->abandon();
248 }
cdaltond416a5b2015-06-23 13:23:44 -0700249}
250
bsalomonc8699322016-05-11 11:55:36 -0700251void GLTestContext::submit() {
252 if (fGL) {
253 GR_GL_CALL(fGL.get(), Flush());
254 }
255}
256
257void GLTestContext::finish() {
258 if (fGL) {
259 GR_GL_CALL(fGL.get(), Finish());
260 }
261}
262
bsalomon273c0f52016-03-31 10:59:06 -0700263GrGLint GLTestContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
bsalomon3724e572016-03-30 18:56:19 -0700264 GrGLenum externalFormat, GrGLenum externalType,
265 GrGLvoid* data) {
Hal Canary1b612a82016-11-03 16:26:13 -0400266 if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL.get()) >= GR_GL_VER(3, 1)) &&
bsalomone5286e02016-01-14 09:24:09 -0800267 !fGL->fExtensions.has("GL_ARB_texture_rectangle")) {
268 return 0;
269 }
bsalomone179a912016-01-20 06:18:10 -0800270
Hal Canary1b612a82016-11-03 16:26:13 -0400271 if (GrGLGetGLSLVersion(fGL.get()) < GR_GLSL_VER(1, 40)) {
bsalomone179a912016-01-20 06:18:10 -0800272 return 0;
273 }
274
bsalomone5286e02016-01-14 09:24:09 -0800275 GrGLuint id;
Hal Canary1b612a82016-11-03 16:26:13 -0400276 GR_GL_CALL(fGL.get(), GenTextures(1, &id));
277 GR_GL_CALL(fGL.get(), BindTexture(GR_GL_TEXTURE_RECTANGLE, id));
278 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER,
279 GR_GL_NEAREST));
280 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER,
281 GR_GL_NEAREST));
282 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S,
283 GR_GL_CLAMP_TO_EDGE));
284 GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T,
285 GR_GL_CLAMP_TO_EDGE));
286 GR_GL_CALL(fGL.get(), TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0,
287 externalFormat, externalType, data));
bsalomone5286e02016-01-14 09:24:09 -0800288 return id;
289}
Greg Daniel02611d92017-07-25 10:05:01 -0400290
291sk_sp<GrContext> GLTestContext::makeGrContext(const GrContextOptions& options) {
292 return GrContext::MakeGL(fGL.get(), options);
293}
294
bsalomon3724e572016-03-30 18:56:19 -0700295} // namespace sk_gpu_test