2011-10-19 20:43:20 +00:00
|
|
|
/*
|
2013-02-28 20:16:25 +00:00
|
|
|
* Copyright 2013 Google Inc.
|
2011-10-19 20:43:20 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-05-11 17:09:18 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/gl/GLTestContext.h"
|
2016-10-05 15:42:03 +00:00
|
|
|
|
2020-07-13 20:13:31 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/gl/GrGLUtil.h"
|
|
|
|
#include "tools/gpu/GpuTimer.h"
|
2011-10-19 20:43:20 +00:00
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-10-05 15:42:03 +00:00
|
|
|
class GLGpuTimer : public sk_gpu_test::GpuTimer {
|
|
|
|
public:
|
2016-11-03 18:40:50 +00:00
|
|
|
static std::unique_ptr<GLGpuTimer> MakeIfSupported(const sk_gpu_test::GLTestContext*);
|
2016-10-05 15:42:03 +00:00
|
|
|
|
|
|
|
QueryStatus checkQueryStatus(sk_gpu_test::PlatformTimerQuery) override;
|
|
|
|
std::chrono::nanoseconds getTimeElapsed(sk_gpu_test::PlatformTimerQuery) override;
|
|
|
|
void deleteQuery(sk_gpu_test::PlatformTimerQuery) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext*, const char* ext = "");
|
|
|
|
|
|
|
|
bool validate() const;
|
|
|
|
|
|
|
|
sk_gpu_test::PlatformTimerQuery onQueueTimerStart() const override;
|
|
|
|
void onQueueTimerStop(sk_gpu_test::PlatformTimerQuery) const override;
|
|
|
|
|
|
|
|
static constexpr GrGLenum GL_QUERY_RESULT = 0x8866;
|
|
|
|
static constexpr GrGLenum GL_QUERY_RESULT_AVAILABLE = 0x8867;
|
|
|
|
static constexpr GrGLenum GL_TIME_ELAPSED = 0x88bf;
|
|
|
|
static constexpr GrGLenum GL_GPU_DISJOINT = 0x8fbb;
|
|
|
|
|
|
|
|
typedef void (GR_GL_FUNCTION_TYPE* GLGetIntegervProc) (GrGLenum, GrGLint*);
|
|
|
|
typedef void (GR_GL_FUNCTION_TYPE* GLGenQueriesProc) (GrGLsizei, GrGLuint*);
|
|
|
|
typedef void (GR_GL_FUNCTION_TYPE* GLDeleteQueriesProc) (GrGLsizei, const GrGLuint*);
|
|
|
|
typedef void (GR_GL_FUNCTION_TYPE* GLBeginQueryProc) (GrGLenum, GrGLuint);
|
|
|
|
typedef void (GR_GL_FUNCTION_TYPE* GLEndQueryProc) (GrGLenum);
|
|
|
|
typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectuivProc) (GrGLuint, GrGLenum, GrGLuint*);
|
|
|
|
typedef void (GR_GL_FUNCTION_TYPE* GLGetQueryObjectui64vProc) (GrGLuint, GrGLenum, GrGLuint64*);
|
|
|
|
|
|
|
|
GLGetIntegervProc fGLGetIntegerv;
|
|
|
|
GLGenQueriesProc fGLGenQueries;
|
|
|
|
GLDeleteQueriesProc fGLDeleteQueries;
|
|
|
|
GLBeginQueryProc fGLBeginQuery;
|
|
|
|
GLEndQueryProc fGLEndQuery;
|
|
|
|
GLGetQueryObjectuivProc fGLGetQueryObjectuiv;
|
|
|
|
GLGetQueryObjectui64vProc fGLGetQueryObjectui64v;
|
|
|
|
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = sk_gpu_test::GpuTimer;
|
2016-10-05 15:42:03 +00:00
|
|
|
};
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
|
2020-03-19 19:54:28 +00:00
|
|
|
#ifdef SK_GL
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<GLGpuTimer> ret;
|
2016-10-05 15:42:03 +00:00
|
|
|
const GrGLInterface* gl = ctx->gl();
|
|
|
|
if (gl->fExtensions.has("GL_EXT_disjoint_timer_query")) {
|
|
|
|
ret.reset(new GLGpuTimer(true, ctx, "EXT"));
|
|
|
|
} else if (kGL_GrGLStandard == gl->fStandard &&
|
|
|
|
(GrGLGetVersion(gl) > GR_GL_VER(3,3) || gl->fExtensions.has("GL_ARB_timer_query"))) {
|
|
|
|
ret.reset(new GLGpuTimer(false, ctx));
|
|
|
|
} else if (gl->fExtensions.has("GL_EXT_timer_query")) {
|
|
|
|
ret.reset(new GLGpuTimer(false, ctx, "EXT"));
|
|
|
|
}
|
2016-11-03 18:40:50 +00:00
|
|
|
if (ret && !ret->validate()) {
|
|
|
|
ret = nullptr;
|
|
|
|
}
|
|
|
|
return ret;
|
2020-03-19 19:54:28 +00:00
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2016-10-05 15:42:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLGpuTimer::GLGpuTimer(bool disjointSupport, const sk_gpu_test::GLTestContext* ctx, const char* ext)
|
|
|
|
: INHERITED(disjointSupport) {
|
|
|
|
ctx->getGLProcAddress(&fGLGetIntegerv, "glGetIntegerv");
|
|
|
|
ctx->getGLProcAddress(&fGLGenQueries, "glGenQueries", ext);
|
|
|
|
ctx->getGLProcAddress(&fGLDeleteQueries, "glDeleteQueries", ext);
|
|
|
|
ctx->getGLProcAddress(&fGLBeginQuery, "glBeginQuery", ext);
|
|
|
|
ctx->getGLProcAddress(&fGLEndQuery, "glEndQuery", ext);
|
|
|
|
ctx->getGLProcAddress(&fGLGetQueryObjectuiv, "glGetQueryObjectuiv", ext);
|
|
|
|
ctx->getGLProcAddress(&fGLGetQueryObjectui64v, "glGetQueryObjectui64v", ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLGpuTimer::validate() const {
|
|
|
|
return fGLGetIntegerv && fGLGenQueries && fGLDeleteQueries && fGLBeginQuery && fGLEndQuery &&
|
|
|
|
fGLGetQueryObjectuiv && fGLGetQueryObjectui64v;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_gpu_test::PlatformTimerQuery GLGpuTimer::onQueueTimerStart() const {
|
|
|
|
GrGLuint queryID;
|
|
|
|
fGLGenQueries(1, &queryID);
|
|
|
|
if (!queryID) {
|
|
|
|
return sk_gpu_test::kInvalidTimerQuery;
|
|
|
|
}
|
|
|
|
if (this->disjointSupport()) {
|
|
|
|
// Clear the disjoint flag.
|
|
|
|
GrGLint disjoint;
|
|
|
|
fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint);
|
|
|
|
}
|
|
|
|
fGLBeginQuery(GL_TIME_ELAPSED, queryID);
|
|
|
|
return static_cast<sk_gpu_test::PlatformTimerQuery>(queryID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLGpuTimer::onQueueTimerStop(sk_gpu_test::PlatformTimerQuery platformTimer) const {
|
|
|
|
if (sk_gpu_test::kInvalidTimerQuery == platformTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fGLEndQuery(GL_TIME_ELAPSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_gpu_test::GpuTimer::QueryStatus
|
|
|
|
GLGpuTimer::checkQueryStatus(sk_gpu_test::PlatformTimerQuery platformTimer) {
|
|
|
|
const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
|
|
|
|
if (!queryID) {
|
|
|
|
return QueryStatus::kInvalid;
|
|
|
|
}
|
|
|
|
GrGLuint available = 0;
|
|
|
|
fGLGetQueryObjectuiv(queryID, GL_QUERY_RESULT_AVAILABLE, &available);
|
|
|
|
if (!available) {
|
|
|
|
return QueryStatus::kPending;
|
|
|
|
}
|
|
|
|
if (this->disjointSupport()) {
|
|
|
|
GrGLint disjoint = 1;
|
|
|
|
fGLGetIntegerv(GL_GPU_DISJOINT, &disjoint);
|
|
|
|
if (disjoint) {
|
|
|
|
return QueryStatus::kDisjoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QueryStatus::kAccurate;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::nanoseconds GLGpuTimer::getTimeElapsed(sk_gpu_test::PlatformTimerQuery platformTimer) {
|
|
|
|
SkASSERT(this->checkQueryStatus(platformTimer) >= QueryStatus::kDisjoint);
|
|
|
|
const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
|
|
|
|
GrGLuint64 nanoseconds;
|
|
|
|
fGLGetQueryObjectui64v(queryID, GL_QUERY_RESULT, &nanoseconds);
|
|
|
|
return std::chrono::nanoseconds(nanoseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLGpuTimer::deleteQuery(sk_gpu_test::PlatformTimerQuery platformTimer) {
|
|
|
|
const GrGLuint queryID = static_cast<GrGLuint>(platformTimer);
|
|
|
|
fGLDeleteQueries(1, &queryID);
|
|
|
|
}
|
|
|
|
|
2019-12-18 15:43:10 +00:00
|
|
|
static_assert(sizeof(GrGLuint) <= sizeof(sk_gpu_test::PlatformTimerQuery));
|
2016-10-05 15:42:03 +00:00
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace sk_gpu_test {
|
|
|
|
|
2016-05-11 17:09:18 +00:00
|
|
|
GLTestContext::GLTestContext() : TestContext() {}
|
2011-10-19 20:43:20 +00:00
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
GLTestContext::~GLTestContext() {
|
2020-08-26 19:27:03 +00:00
|
|
|
SkASSERT(!fGLInterface);
|
|
|
|
SkASSERT(!fOriginalGLInterface);
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 19:54:28 +00:00
|
|
|
bool GLTestContext::isValid() const {
|
|
|
|
#ifdef SK_GL
|
|
|
|
return SkToBool(this->gl());
|
|
|
|
#else
|
|
|
|
return fWasInitialized;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-21 20:46:27 +00:00
|
|
|
static bool fence_is_supported(const GLTestContext* ctx) {
|
2020-03-19 19:54:28 +00:00
|
|
|
#ifdef SK_GL
|
2020-02-21 20:46:27 +00:00
|
|
|
if (kGL_GrGLStandard == ctx->gl()->fStandard) {
|
|
|
|
if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3, 2) &&
|
|
|
|
!ctx->gl()->hasExtension("GL_ARB_sync")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (ctx->gl()->hasExtension("GL_APPLE_sync")) {
|
|
|
|
return true;
|
|
|
|
} else if (ctx->gl()->hasExtension("GL_NV_fence")) {
|
|
|
|
return true;
|
|
|
|
} else if (GrGLGetVersion(ctx->gl()) >= GR_GL_VER(3, 0)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 19:54:28 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2020-02-21 20:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLTestContext::init(sk_sp<const GrGLInterface> gl) {
|
2020-08-26 19:27:03 +00:00
|
|
|
fGLInterface = std::move(gl);
|
|
|
|
fOriginalGLInterface = fGLInterface;
|
2020-02-21 20:46:27 +00:00
|
|
|
fFenceSupport = fence_is_supported(this);
|
2016-11-03 18:40:50 +00:00
|
|
|
fGpuTimer = GLGpuTimer::MakeIfSupported(this);
|
2020-03-19 19:54:28 +00:00
|
|
|
#ifndef SK_GL
|
|
|
|
fWasInitialized = true;
|
|
|
|
#endif
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
void GLTestContext::teardown() {
|
2020-08-26 19:27:03 +00:00
|
|
|
fGLInterface.reset();
|
|
|
|
fOriginalGLInterface.reset();
|
2016-05-11 17:09:18 +00:00
|
|
|
INHERITED::teardown();
|
2011-10-19 20:43:20 +00:00
|
|
|
}
|
2014-07-29 15:01:52 +00:00
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
void GLTestContext::testAbandon() {
|
2016-05-11 17:09:18 +00:00
|
|
|
INHERITED::testAbandon();
|
2020-03-19 19:54:28 +00:00
|
|
|
#ifdef SK_GL
|
2020-08-26 19:27:03 +00:00
|
|
|
if (fGLInterface) {
|
|
|
|
fGLInterface->abandon();
|
|
|
|
fOriginalGLInterface->abandon();
|
2014-07-29 15:01:52 +00:00
|
|
|
}
|
2020-03-19 19:54:28 +00:00
|
|
|
#endif
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 18:55:36 +00:00
|
|
|
void GLTestContext::finish() {
|
2020-03-19 19:54:28 +00:00
|
|
|
#ifdef SK_GL
|
2020-08-26 19:27:03 +00:00
|
|
|
if (fGLInterface) {
|
|
|
|
GR_GL_CALL(fGLInterface.get(), Finish());
|
2016-05-11 18:55:36 +00:00
|
|
|
}
|
2020-03-19 19:54:28 +00:00
|
|
|
#endif
|
2016-05-11 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 19:27:03 +00:00
|
|
|
void GLTestContext::overrideVersion(const char* version, const char* shadingLanguageVersion) {
|
|
|
|
#ifdef SK_GL
|
|
|
|
// GrGLFunction has both a limited capture size and doesn't call a destructor when it is
|
|
|
|
// initialized with a lambda. So here we're trusting fOriginalGLInterface will be kept alive.
|
|
|
|
auto getString = [wrapped = &fOriginalGLInterface->fFunctions.fGetString,
|
|
|
|
version,
|
|
|
|
shadingLanguageVersion](GrGLenum name) {
|
|
|
|
if (name == GR_GL_VERSION) {
|
|
|
|
return reinterpret_cast<const GrGLubyte*>(version);
|
|
|
|
} else if (name == GR_GL_SHADING_LANGUAGE_VERSION) {
|
|
|
|
return reinterpret_cast<const GrGLubyte*>(shadingLanguageVersion);
|
|
|
|
}
|
|
|
|
return (*wrapped)(name);
|
|
|
|
};
|
|
|
|
auto newInterface = sk_make_sp<GrGLInterface>(*fOriginalGLInterface);
|
|
|
|
newInterface->fFunctions.fGetString = getString;
|
|
|
|
fGLInterface = std::move(newInterface);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-07-13 20:13:31 +00:00
|
|
|
sk_sp<GrDirectContext> GLTestContext::makeContext(const GrContextOptions& options) {
|
2020-03-19 19:54:28 +00:00
|
|
|
#ifdef SK_GL
|
2020-08-26 19:27:03 +00:00
|
|
|
return GrDirectContext::MakeGL(fGLInterface, options);
|
2020-03-19 19:54:28 +00:00
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2017-07-25 14:05:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:56:19 +00:00
|
|
|
} // namespace sk_gpu_test
|