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
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
#include "GLTestContext.h"
|
2016-10-05 15:42:03 +00:00
|
|
|
|
|
|
|
#include "GpuTimer.h"
|
2016-03-31 01:56:19 +00:00
|
|
|
#include "gl/GrGLUtil.h"
|
2011-10-19 20:43:20 +00:00
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class GLFenceSync : public sk_gpu_test::FenceSync {
|
2015-06-23 20:23:44 +00:00
|
|
|
public:
|
2016-11-03 18:40:50 +00:00
|
|
|
static std::unique_ptr<GLFenceSync> MakeIfSupported(const sk_gpu_test::GLTestContext*);
|
2015-06-23 20:23:44 +00:00
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override;
|
|
|
|
bool waitFence(sk_gpu_test::PlatformFence fence) const override;
|
|
|
|
void deleteFence(sk_gpu_test::PlatformFence fence) const override;
|
2015-06-23 20:23:44 +00:00
|
|
|
|
|
|
|
private:
|
2016-10-04 18:08:45 +00:00
|
|
|
GLFenceSync(const sk_gpu_test::GLTestContext*, const char* ext = "");
|
|
|
|
|
|
|
|
bool validate() { return fGLFenceSync && fGLClientWaitSync && fGLDeleteSync; }
|
2015-06-23 20:23:44 +00:00
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
static constexpr GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117;
|
|
|
|
static constexpr GrGLenum GL_WAIT_FAILED = 0x911d;
|
|
|
|
static constexpr GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001;
|
2015-06-23 20:23:44 +00:00
|
|
|
|
|
|
|
typedef struct __GLsync *GLsync;
|
2016-10-04 21:24:23 +00:00
|
|
|
GR_STATIC_ASSERT(sizeof(GLsync) <= sizeof(sk_gpu_test::PlatformFence));
|
2015-06-23 20:23:44 +00:00
|
|
|
|
|
|
|
typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfield);
|
|
|
|
typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbitfield, GrGLuint64);
|
|
|
|
typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync);
|
|
|
|
|
|
|
|
GLFenceSyncProc fGLFenceSync;
|
|
|
|
GLClientWaitSyncProc fGLClientWaitSync;
|
|
|
|
GLDeleteSyncProc fGLDeleteSync;
|
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
typedef FenceSync INHERITED;
|
2015-06-23 20:23:44 +00:00
|
|
|
};
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<GLFenceSync> GLFenceSync::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
|
|
|
|
std::unique_ptr<GLFenceSync> ret;
|
2016-10-04 18:08:45 +00:00
|
|
|
if (kGL_GrGLStandard == ctx->gl()->fStandard) {
|
|
|
|
if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync")) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ret.reset(new GLFenceSync(ctx));
|
|
|
|
} else {
|
2017-04-21 19:12:30 +00:00
|
|
|
if (ctx->gl()->hasExtension("GL_APPLE_sync")) {
|
|
|
|
ret.reset(new GLFenceSync(ctx, "APPLE"));
|
|
|
|
} else if (GrGLGetVersion(ctx->gl()) >= GR_GL_VER(3, 0)) {
|
|
|
|
ret.reset(new GLFenceSync(ctx));
|
|
|
|
} else {
|
2016-10-04 18:08:45 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 18:40:50 +00:00
|
|
|
if (!ret->validate()) {
|
|
|
|
ret = nullptr;
|
|
|
|
}
|
|
|
|
return ret;
|
2016-10-04 18:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLFenceSync::GLFenceSync(const sk_gpu_test::GLTestContext* ctx, const char* ext) {
|
|
|
|
ctx->getGLProcAddress(&fGLFenceSync, "glFenceSync");
|
|
|
|
ctx->getGLProcAddress(&fGLClientWaitSync, "glClientWaitSync");
|
|
|
|
ctx->getGLProcAddress(&fGLDeleteSync, "glDeleteSync");
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_gpu_test::PlatformFence GLFenceSync::insertFence() const {
|
|
|
|
__GLsync* glsync = fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
|
|
return reinterpret_cast<sk_gpu_test::PlatformFence>(glsync);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLFenceSync::waitFence(sk_gpu_test::PlatformFence fence) const {
|
|
|
|
GLsync glsync = reinterpret_cast<GLsync>(fence);
|
|
|
|
return GL_WAIT_FAILED != fGLClientWaitSync(glsync, GL_SYNC_FLUSH_COMMANDS_BIT, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLFenceSync::deleteFence(sk_gpu_test::PlatformFence fence) const {
|
|
|
|
GLsync glsync = reinterpret_cast<GLsync>(fence);
|
|
|
|
fGLDeleteSync(glsync);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
typedef sk_gpu_test::GpuTimer INHERITED;
|
|
|
|
};
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<GLGpuTimer> GLGpuTimer::MakeIfSupported(const sk_gpu_test::GLTestContext* ctx) {
|
|
|
|
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;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
GR_STATIC_ASSERT(sizeof(GrGLuint) <= sizeof(sk_gpu_test::PlatformTimerQuery));
|
|
|
|
|
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() {
|
2015-08-27 14:41:13 +00:00
|
|
|
SkASSERT(nullptr == fGL.get());
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
void GLTestContext::init(const GrGLInterface* gl, std::unique_ptr<FenceSync> fenceSync) {
|
2015-06-23 20:23:44 +00:00
|
|
|
SkASSERT(!fGL.get());
|
|
|
|
fGL.reset(gl);
|
2016-11-03 18:40:50 +00:00
|
|
|
fFenceSync = fenceSync ? std::move(fenceSync) : GLFenceSync::MakeIfSupported(this);
|
|
|
|
fGpuTimer = GLGpuTimer::MakeIfSupported(this);
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
void GLTestContext::teardown() {
|
2015-08-27 14:41:13 +00:00
|
|
|
fGL.reset(nullptr);
|
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();
|
2014-09-05 20:34:00 +00:00
|
|
|
if (fGL) {
|
2014-07-29 15:01:52 +00:00
|
|
|
fGL->abandon();
|
|
|
|
}
|
2015-06-23 20:23:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 18:55:36 +00:00
|
|
|
void GLTestContext::submit() {
|
|
|
|
if (fGL) {
|
|
|
|
GR_GL_CALL(fGL.get(), Flush());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLTestContext::finish() {
|
|
|
|
if (fGL) {
|
|
|
|
GR_GL_CALL(fGL.get(), Finish());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
GrGLint GLTestContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
|
2016-03-31 01:56:19 +00:00
|
|
|
GrGLenum externalFormat, GrGLenum externalType,
|
|
|
|
GrGLvoid* data) {
|
2016-11-03 20:26:13 +00:00
|
|
|
if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL.get()) >= GR_GL_VER(3, 1)) &&
|
2016-01-14 17:24:09 +00:00
|
|
|
!fGL->fExtensions.has("GL_ARB_texture_rectangle")) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-20 14:18:10 +00:00
|
|
|
|
2016-11-03 20:26:13 +00:00
|
|
|
if (GrGLGetGLSLVersion(fGL.get()) < GR_GLSL_VER(1, 40)) {
|
2016-01-20 14:18:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-14 17:24:09 +00:00
|
|
|
GrGLuint id;
|
2016-11-03 20:26:13 +00:00
|
|
|
GR_GL_CALL(fGL.get(), GenTextures(1, &id));
|
|
|
|
GR_GL_CALL(fGL.get(), BindTexture(GR_GL_TEXTURE_RECTANGLE, id));
|
|
|
|
GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER,
|
|
|
|
GR_GL_NEAREST));
|
|
|
|
GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER,
|
|
|
|
GR_GL_NEAREST));
|
|
|
|
GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S,
|
|
|
|
GR_GL_CLAMP_TO_EDGE));
|
|
|
|
GR_GL_CALL(fGL.get(), TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T,
|
|
|
|
GR_GL_CLAMP_TO_EDGE));
|
|
|
|
GR_GL_CALL(fGL.get(), TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0,
|
|
|
|
externalFormat, externalType, data));
|
2016-01-14 17:24:09 +00:00
|
|
|
return id;
|
|
|
|
}
|
2016-03-31 01:56:19 +00:00
|
|
|
} // namespace sk_gpu_test
|