bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 1 | /* |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 2 | * Copyright 2013 Google Inc. |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 7 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 8 | #include "GLTestContext.h" |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 9 | #include "gl/GrGLUtil.h" |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 10 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 11 | namespace { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 12 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 13 | class GLFenceSync : public sk_gpu_test::FenceSync { |
| 14 | public: |
| 15 | static GLFenceSync* CreateIfSupported(const sk_gpu_test::GLTestContext*); |
| 16 | |
| 17 | sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override; |
| 18 | bool waitFence(sk_gpu_test::PlatformFence fence) const override; |
| 19 | void deleteFence(sk_gpu_test::PlatformFence fence) const override; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 20 | |
| 21 | private: |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 22 | GLFenceSync(const sk_gpu_test::GLTestContext*, const char* ext = ""); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 23 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 24 | bool validate() { return fGLFenceSync && fGLClientWaitSync && fGLDeleteSync; } |
| 25 | |
| 26 | static constexpr GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117; |
| 27 | static constexpr GrGLenum GL_WAIT_FAILED = 0x911d; |
| 28 | static constexpr GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 29 | |
| 30 | typedef struct __GLsync *GLsync; |
csmartdalton | 024229a | 2016-10-04 14:24:23 -0700 | [diff] [blame^] | 31 | GR_STATIC_ASSERT(sizeof(GLsync) <= sizeof(sk_gpu_test::PlatformFence)); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 32 | |
| 33 | typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfield); |
| 34 | typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbitfield, GrGLuint64); |
| 35 | typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync); |
| 36 | |
| 37 | GLFenceSyncProc fGLFenceSync; |
| 38 | GLClientWaitSyncProc fGLClientWaitSync; |
| 39 | GLDeleteSyncProc fGLDeleteSync; |
| 40 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 41 | typedef FenceSync INHERITED; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 44 | GLFenceSync* GLFenceSync::CreateIfSupported(const sk_gpu_test::GLTestContext* ctx) { |
| 45 | SkAutoTDelete<GLFenceSync> ret; |
| 46 | if (kGL_GrGLStandard == ctx->gl()->fStandard) { |
| 47 | if (GrGLGetVersion(ctx->gl()) < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync")) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | ret.reset(new GLFenceSync(ctx)); |
| 51 | } else { |
| 52 | if (!ctx->gl()->hasExtension("GL_APPLE_sync")) { |
| 53 | return nullptr; |
| 54 | } |
| 55 | ret.reset(new GLFenceSync(ctx, "APPLE")); |
| 56 | } |
| 57 | return ret->validate() ? ret.release() : nullptr; |
| 58 | } |
| 59 | |
| 60 | GLFenceSync::GLFenceSync(const sk_gpu_test::GLTestContext* ctx, const char* ext) { |
| 61 | ctx->getGLProcAddress(&fGLFenceSync, "glFenceSync"); |
| 62 | ctx->getGLProcAddress(&fGLClientWaitSync, "glClientWaitSync"); |
| 63 | ctx->getGLProcAddress(&fGLDeleteSync, "glDeleteSync"); |
| 64 | } |
| 65 | |
| 66 | sk_gpu_test::PlatformFence GLFenceSync::insertFence() const { |
| 67 | __GLsync* glsync = fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 68 | return reinterpret_cast<sk_gpu_test::PlatformFence>(glsync); |
| 69 | } |
| 70 | |
| 71 | bool GLFenceSync::waitFence(sk_gpu_test::PlatformFence fence) const { |
| 72 | GLsync glsync = reinterpret_cast<GLsync>(fence); |
| 73 | return GL_WAIT_FAILED != fGLClientWaitSync(glsync, GL_SYNC_FLUSH_COMMANDS_BIT, -1); |
| 74 | } |
| 75 | |
| 76 | void GLFenceSync::deleteFence(sk_gpu_test::PlatformFence fence) const { |
| 77 | GLsync glsync = reinterpret_cast<GLsync>(fence); |
| 78 | fGLDeleteSync(glsync); |
| 79 | } |
| 80 | |
| 81 | } // anonymous namespace |
| 82 | |
| 83 | namespace sk_gpu_test { |
| 84 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 85 | GLTestContext::GLTestContext() : TestContext() {} |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 86 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 87 | GLTestContext::~GLTestContext() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 88 | SkASSERT(nullptr == fGL.get()); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 89 | } |
| 90 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 91 | void GLTestContext::init(const GrGLInterface* gl, FenceSync* fenceSync) { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 92 | SkASSERT(!fGL.get()); |
| 93 | fGL.reset(gl); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 94 | fFenceSync = fenceSync ? fenceSync : GLFenceSync::CreateIfSupported(this); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 95 | } |
| 96 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 97 | void GLTestContext::teardown() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | fGL.reset(nullptr); |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 99 | INHERITED::teardown(); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 100 | } |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 101 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 102 | void GLTestContext::testAbandon() { |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 103 | INHERITED::testAbandon(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 104 | if (fGL) { |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 105 | fGL->abandon(); |
| 106 | } |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 107 | } |
| 108 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 109 | void GLTestContext::submit() { |
| 110 | if (fGL) { |
| 111 | GR_GL_CALL(fGL.get(), Flush()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void GLTestContext::finish() { |
| 116 | if (fGL) { |
| 117 | GR_GL_CALL(fGL.get(), Finish()); |
| 118 | } |
| 119 | } |
| 120 | |
bsalomon | 273c0f5 | 2016-03-31 10:59:06 -0700 | [diff] [blame] | 121 | GrGLint GLTestContext::createTextureRectangle(int width, int height, GrGLenum internalFormat, |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 122 | GrGLenum externalFormat, GrGLenum externalType, |
| 123 | GrGLvoid* data) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 124 | if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER(3, 1)) && |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 125 | !fGL->fExtensions.has("GL_ARB_texture_rectangle")) { |
| 126 | return 0; |
| 127 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 128 | |
| 129 | if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) { |
| 130 | return 0; |
| 131 | } |
| 132 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 133 | GrGLuint id; |
| 134 | GR_GL_CALL(fGL, GenTextures(1, &id)); |
| 135 | GR_GL_CALL(fGL, BindTexture(GR_GL_TEXTURE_RECTANGLE, id)); |
| 136 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER, |
| 137 | GR_GL_NEAREST)); |
| 138 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER, |
| 139 | GR_GL_NEAREST)); |
| 140 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S, |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 141 | GR_GL_CLAMP_TO_EDGE)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 142 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T, |
| 143 | GR_GL_CLAMP_TO_EDGE)); |
| 144 | GR_GL_CALL(fGL, TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0, |
| 145 | externalFormat, externalType, data)); |
| 146 | return id; |
| 147 | } |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 148 | } // namespace sk_gpu_test |