1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "GLTestContext.h"
9 
10 #include "GpuTimer.h"
11 #include "gl/GrGLUtil.h"
12 
13 namespace {
14 
15 class GLFenceSync : public sk_gpu_test::FenceSync {
16 public:
17     static std::unique_ptr<GLFenceSync> MakeIfSupported(const sk_gpu_test::GLTestContext*);
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;
22 
23 private:
24     GLFenceSync(const sk_gpu_test::GLTestContext*, const char* ext = "");
25 
validate()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;
31 
32     typedef struct __GLsync *GLsync;
33     GR_STATIC_ASSERT(sizeof(GLsync) <= sizeof(sk_gpu_test::PlatformFence));
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 
43     typedef FenceSync INHERITED;
44 };
45 
MakeIfSupported(const sk_gpu_test::GLTestContext * ctx)46 std::unique_ptr<GLFenceSync> GLFenceSync::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
47     std::unique_ptr<GLFenceSync> ret;
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     }
59     if (!ret->validate()) {
60         ret = nullptr;
61     }
62     return ret;
63 }
64 
GLFenceSync(const sk_gpu_test::GLTestContext * ctx,const char * ext)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 
insertFence() const71 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 
waitFence(sk_gpu_test::PlatformFence fence) const76 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 
deleteFence(sk_gpu_test::PlatformFence fence) const81 void GLFenceSync::deleteFence(sk_gpu_test::PlatformFence fence) const {
82     GLsync glsync = reinterpret_cast<GLsync>(fence);
83     fGLDeleteSync(glsync);
84 }
85 
86 class GLGpuTimer : public sk_gpu_test::GpuTimer {
87 public:
88     static std::unique_ptr<GLGpuTimer> MakeIfSupported(const sk_gpu_test::GLTestContext*);
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 
MakeIfSupported(const sk_gpu_test::GLTestContext * ctx)127 std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
128     std::unique_ptr<GLGpuTimer> ret;
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     }
138     if (ret && !ret->validate()) {
139         ret = nullptr;
140     }
141     return ret;
142 }
143 
GLGpuTimer(bool disjointSupport,const sk_gpu_test::GLTestContext * ctx,const char * ext)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 
validate() const155 bool GLGpuTimer::validate() const {
156     return fGLGetIntegerv && fGLGenQueries && fGLDeleteQueries && fGLBeginQuery && fGLEndQuery &&
157            fGLGetQueryObjectuiv && fGLGetQueryObjectui64v;
158 }
159 
onQueueTimerStart() const160 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 
onQueueTimerStop(sk_gpu_test::PlatformTimerQuery platformTimer) const175 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
checkQueryStatus(sk_gpu_test::PlatformTimerQuery platformTimer)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 
getTimeElapsed(sk_gpu_test::PlatformTimerQuery platformTimer)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 
deleteQuery(sk_gpu_test::PlatformTimerQuery platformTimer)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 
218 }  // anonymous namespace
219 
220 namespace sk_gpu_test {
221 
GLTestContext()222 GLTestContext::GLTestContext() : TestContext() {}
223 
~GLTestContext()224 GLTestContext::~GLTestContext() {
225     SkASSERT(nullptr == fGL.get());
226 }
227 
init(const GrGLInterface * gl,std::unique_ptr<FenceSync> fenceSync)228 void GLTestContext::init(const GrGLInterface* gl, std::unique_ptr<FenceSync> fenceSync) {
229     SkASSERT(!fGL.get());
230     fGL.reset(gl);
231     fFenceSync = fenceSync ? std::move(fenceSync) : GLFenceSync::MakeIfSupported(this);
232     fGpuTimer = GLGpuTimer::MakeIfSupported(this);
233 }
234 
teardown()235 void GLTestContext::teardown() {
236     fGL.reset(nullptr);
237     INHERITED::teardown();
238 }
239 
testAbandon()240 void GLTestContext::testAbandon() {
241     INHERITED::testAbandon();
242     if (fGL) {
243         fGL->abandon();
244     }
245 }
246 
submit()247 void GLTestContext::submit() {
248     if (fGL) {
249         GR_GL_CALL(fGL.get(), Flush());
250     }
251 }
252 
finish()253 void GLTestContext::finish() {
254     if (fGL) {
255         GR_GL_CALL(fGL.get(), Finish());
256     }
257 }
258 
createTextureRectangle(int width,int height,GrGLenum internalFormat,GrGLenum externalFormat,GrGLenum externalType,GrGLvoid * data)259 GrGLint GLTestContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
260                                           GrGLenum externalFormat, GrGLenum externalType,
261                                           GrGLvoid* data) {
262     if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL.get()) >= GR_GL_VER(3, 1)) &&
263         !fGL->fExtensions.has("GL_ARB_texture_rectangle")) {
264         return 0;
265     }
266 
267     if  (GrGLGetGLSLVersion(fGL.get()) < GR_GLSL_VER(1, 40)) {
268         return 0;
269     }
270 
271     GrGLuint id;
272     GR_GL_CALL(fGL.get(), GenTextures(1, &id));
273     GR_GL_CALL(fGL.get(), BindTexture(GR_GL_TEXTURE_RECTANGLE, id));
274     GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER,
275                                         GR_GL_NEAREST));
276     GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER,
277                                         GR_GL_NEAREST));
278     GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S,
279                                         GR_GL_CLAMP_TO_EDGE));
280     GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T,
281                                         GR_GL_CLAMP_TO_EDGE));
282     GR_GL_CALL(fGL.get(), TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0,
283                                      externalFormat, externalType, data));
284     return id;
285 }
286 }  // namespace sk_gpu_test
287