blob: e6d0e40db83f577cece2f4e45e467f1b6b003de7 [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
10#include "GpuTimer.h"
bsalomon3724e572016-03-30 18:56:19 -070011#include "gl/GrGLUtil.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000012
csmartdalton421a3c12016-10-04 11:08:45 -070013namespace {
cdaltond416a5b2015-06-23 13:23:44 -070014
csmartdalton421a3c12016-10-04 11:08:45 -070015class GLFenceSync : public sk_gpu_test::FenceSync {
16public:
Ben Wagner145dbcd2016-11-03 14:40:50 -040017 static std::unique_ptr<GLFenceSync> MakeIfSupported(const sk_gpu_test::GLTestContext*);
csmartdalton421a3c12016-10-04 11:08:45 -070018
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;
cdaltond416a5b2015-06-23 13:23:44 -070022
23private:
csmartdalton421a3c12016-10-04 11:08:45 -070024 GLFenceSync(const sk_gpu_test::GLTestContext*, const char* ext = "");
cdaltond416a5b2015-06-23 13:23:44 -070025
csmartdalton421a3c12016-10-04 11:08:45 -070026 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;
cdaltond416a5b2015-06-23 13:23:44 -070031
32 typedef struct __GLsync *GLsync;
csmartdalton024229a2016-10-04 14:24:23 -070033 GR_STATIC_ASSERT(sizeof(GLsync) <= sizeof(sk_gpu_test::PlatformFence));
cdaltond416a5b2015-06-23 13:23:44 -070034
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
csmartdalton421a3c12016-10-04 11:08:45 -070043 typedef FenceSync INHERITED;
cdaltond416a5b2015-06-23 13:23:44 -070044};
45
Ben Wagner145dbcd2016-11-03 14:40:50 -040046std::unique_ptr<GLFenceSync> GLFenceSync::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
47 std::unique_ptr<GLFenceSync> ret;
csmartdalton421a3c12016-10-04 11:08:45 -070048 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 Wagner145dbcd2016-11-03 14:40:50 -040059 if (!ret->validate()) {
60 ret = nullptr;
61 }
62 return ret;
csmartdalton421a3c12016-10-04 11:08:45 -070063}
64
65GLFenceSync::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
71sk_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
76bool 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
81void GLFenceSync::deleteFence(sk_gpu_test::PlatformFence fence) const {
82 GLsync glsync = reinterpret_cast<GLsync>(fence);
83 fGLDeleteSync(glsync);
84}
85
csmartdaltonc6618dd2016-10-05 08:42:03 -070086class GLGpuTimer : public sk_gpu_test::GpuTimer {
87public:
Ben Wagner145dbcd2016-11-03 14:40:50 -040088 static std::unique_ptr<GLGpuTimer> MakeIfSupported(const sk_gpu_test::GLTestContext*);
csmartdaltonc6618dd2016-10-05 08:42:03 -070089
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
94private:
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 Wagner145dbcd2016-11-03 14:40:50 -0400127std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
128 std::unique_ptr<GLGpuTimer> ret;
csmartdaltonc6618dd2016-10-05 08:42:03 -0700129 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 Wagner145dbcd2016-11-03 14:40:50 -0400138 if (ret && !ret->validate()) {
139 ret = nullptr;
140 }
141 return ret;
csmartdaltonc6618dd2016-10-05 08:42:03 -0700142}
143
144GLGpuTimer::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
155bool GLGpuTimer::validate() const {
156 return fGLGetIntegerv && fGLGenQueries && fGLDeleteQueries && fGLBeginQuery && fGLEndQuery &&
157 fGLGetQueryObjectuiv && fGLGetQueryObjectui64v;
158}
159
160sk_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
175void 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
182sk_gpu_test::GpuTimer::QueryStatus
183GLGpuTimer::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
203std::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
211void GLGpuTimer::deleteQuery(sk_gpu_test::PlatformTimerQuery platformTimer) {
212 const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
213 fGLDeleteQueries(1, &queryID);
214}
215
216GR_STATIC_ASSERT(sizeof(GrGLuint) <= sizeof(sk_gpu_test::PlatformTimerQuery));
217
csmartdalton421a3c12016-10-04 11:08:45 -0700218} // anonymous namespace
219
220namespace sk_gpu_test {
221
bsalomon18a2f9d2016-05-11 10:09:18 -0700222GLTestContext::GLTestContext() : TestContext() {}
bsalomon@google.com373a6632011-10-19 20:43:20 +0000223
bsalomon273c0f52016-03-31 10:59:06 -0700224GLTestContext::~GLTestContext() {
halcanary96fcdcc2015-08-27 07:41:13 -0700225 SkASSERT(nullptr == fGL.get());
cdaltond416a5b2015-06-23 13:23:44 -0700226}
227
Ben Wagner145dbcd2016-11-03 14:40:50 -0400228void GLTestContext::init(const GrGLInterface* gl, std::unique_ptr<FenceSync> fenceSync) {
cdaltond416a5b2015-06-23 13:23:44 -0700229 SkASSERT(!fGL.get());
230 fGL.reset(gl);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400231 fFenceSync = fenceSync ? std::move(fenceSync) : GLFenceSync::MakeIfSupported(this);
232 fGpuTimer = GLGpuTimer::MakeIfSupported(this);
cdaltond416a5b2015-06-23 13:23:44 -0700233}
234
bsalomon273c0f52016-03-31 10:59:06 -0700235void GLTestContext::teardown() {
halcanary96fcdcc2015-08-27 07:41:13 -0700236 fGL.reset(nullptr);
bsalomon18a2f9d2016-05-11 10:09:18 -0700237 INHERITED::teardown();
bsalomon@google.com373a6632011-10-19 20:43:20 +0000238}
bsalomon944bcf02014-07-29 08:01:52 -0700239
bsalomon273c0f52016-03-31 10:59:06 -0700240void GLTestContext::testAbandon() {
bsalomon18a2f9d2016-05-11 10:09:18 -0700241 INHERITED::testAbandon();
bsalomon49f085d2014-09-05 13:34:00 -0700242 if (fGL) {
bsalomon944bcf02014-07-29 08:01:52 -0700243 fGL->abandon();
244 }
cdaltond416a5b2015-06-23 13:23:44 -0700245}
246
bsalomonc8699322016-05-11 11:55:36 -0700247void GLTestContext::submit() {
248 if (fGL) {
249 GR_GL_CALL(fGL.get(), Flush());
250 }
251}
252
253void GLTestContext::finish() {
254 if (fGL) {
255 GR_GL_CALL(fGL.get(), Finish());
256 }
257}
258
bsalomon273c0f52016-03-31 10:59:06 -0700259GrGLint GLTestContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
bsalomon3724e572016-03-30 18:56:19 -0700260 GrGLenum externalFormat, GrGLenum externalType,
261 GrGLvoid* data) {
bsalomone179a912016-01-20 06:18:10 -0800262 if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER(3, 1)) &&
bsalomone5286e02016-01-14 09:24:09 -0800263 !fGL->fExtensions.has("GL_ARB_texture_rectangle")) {
264 return 0;
265 }
bsalomone179a912016-01-20 06:18:10 -0800266
267 if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) {
268 return 0;
269 }
270
bsalomone5286e02016-01-14 09:24:09 -0800271 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,
bsalomon3724e572016-03-30 18:56:19 -0700279 GR_GL_CLAMP_TO_EDGE));
bsalomone5286e02016-01-14 09:24:09 -0800280 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}
bsalomon3724e572016-03-30 18:56:19 -0700286} // namespace sk_gpu_test