OLD | NEW |
| 1 |
1 /* | 2 /* |
2 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
3 * | 4 * |
4 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 6 * found in the LICENSE file. |
6 */ | 7 */ |
7 #include "gl/SkGLContext.h" | 8 #include "GLContext.h" |
8 #include "GrGLUtil.h" | 9 #include "gl/GrGLUtil.h" |
9 #include "SkGpuFenceSync.h" | 10 #include "SkGpuFenceSync.h" |
10 | 11 |
11 class SkGLContext::GLFenceSync : public SkGpuFenceSync { | 12 namespace sk_gpu_test { |
| 13 class GLContext::GLFenceSync : public SkGpuFenceSync { |
12 public: | 14 public: |
13 static GLFenceSync* CreateIfSupported(const SkGLContext*); | 15 static GLFenceSync* CreateIfSupported(const GLContext*); |
14 | 16 |
15 SkPlatformGpuFence SK_WARN_UNUSED_RESULT insertFence() const override; | 17 SkPlatformGpuFence SK_WARN_UNUSED_RESULT insertFence() const override; |
16 bool waitFence(SkPlatformGpuFence fence, bool flush) const override; | 18 bool waitFence(SkPlatformGpuFence fence, bool flush) const override; |
17 void deleteFence(SkPlatformGpuFence fence) const override; | 19 void deleteFence(SkPlatformGpuFence fence) const override; |
18 | 20 |
19 private: | 21 private: |
20 GLFenceSync() {} | 22 GLFenceSync() {} |
21 | 23 |
22 static const GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117; | 24 static const GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117; |
23 static const GrGLenum GL_WAIT_FAILED = 0x911d; | 25 static const GrGLenum GL_WAIT_FAILED = 0x911d; |
24 static const GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001; | 26 static const GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001; |
25 | 27 |
26 typedef struct __GLsync *GLsync; | 28 typedef struct __GLsync *GLsync; |
27 | 29 |
28 typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfiel
d); | 30 typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfiel
d); |
29 typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbi
tfield, GrGLuint64); | 31 typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbi
tfield, GrGLuint64); |
30 typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync); | 32 typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync); |
31 | 33 |
32 GLFenceSyncProc fGLFenceSync; | 34 GLFenceSyncProc fGLFenceSync; |
33 GLClientWaitSyncProc fGLClientWaitSync; | 35 GLClientWaitSyncProc fGLClientWaitSync; |
34 GLDeleteSyncProc fGLDeleteSync; | 36 GLDeleteSyncProc fGLDeleteSync; |
35 | 37 |
36 typedef SkGpuFenceSync INHERITED; | 38 typedef SkGpuFenceSync INHERITED; |
37 }; | 39 }; |
38 | 40 |
39 SkGLContext::SkGLContext() | 41 GLContext::GLContext() |
40 : fCurrentFenceIdx(0) { | 42 : fCurrentFenceIdx(0) { |
41 memset(fFrameFences, 0, sizeof(fFrameFences)); | 43 memset(fFrameFences, 0, sizeof(fFrameFences)); |
42 } | 44 } |
43 | 45 |
44 SkGLContext::~SkGLContext() { | 46 GLContext::~GLContext() { |
45 // Subclass should call teardown. | 47 // Subclass should call teardown. |
46 #ifdef SK_DEBUG | 48 #ifdef SK_DEBUG |
47 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) { | 49 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) { |
48 SkASSERT(0 == fFrameFences[i]); | 50 SkASSERT(0 == fFrameFences[i]); |
49 } | 51 } |
50 #endif | 52 #endif |
51 SkASSERT(nullptr == fGL.get()); | 53 SkASSERT(nullptr == fGL.get()); |
52 SkASSERT(nullptr == fFenceSync.get()); | 54 SkASSERT(nullptr == fFenceSync.get()); |
53 } | 55 } |
54 | 56 |
55 void SkGLContext::init(const GrGLInterface* gl, SkGpuFenceSync* fenceSync) { | 57 void GLContext::init(const GrGLInterface* gl, SkGpuFenceSync* fenceSync) { |
56 SkASSERT(!fGL.get()); | 58 SkASSERT(!fGL.get()); |
57 fGL.reset(gl); | 59 fGL.reset(gl); |
58 fFenceSync.reset(fenceSync ? fenceSync : GLFenceSync::CreateIfSupported(this
)); | 60 fFenceSync.reset(fenceSync ? fenceSync : GLFenceSync::CreateIfSupported(this
)); |
59 } | 61 } |
60 | 62 |
61 void SkGLContext::teardown() { | 63 void GLContext::teardown() { |
62 if (fFenceSync) { | 64 if (fFenceSync) { |
63 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) { | 65 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) { |
64 if (fFrameFences[i]) { | 66 if (fFrameFences[i]) { |
65 fFenceSync->deleteFence(fFrameFences[i]); | 67 fFenceSync->deleteFence(fFrameFences[i]); |
66 fFrameFences[i] = 0; | 68 fFrameFences[i] = 0; |
67 } | 69 } |
68 } | 70 } |
69 fFenceSync.reset(nullptr); | 71 fFenceSync.reset(nullptr); |
70 } | 72 } |
71 | 73 |
72 fGL.reset(nullptr); | 74 fGL.reset(nullptr); |
73 } | 75 } |
74 | 76 |
75 void SkGLContext::makeCurrent() const { | 77 void GLContext::makeCurrent() const { |
76 this->onPlatformMakeCurrent(); | 78 this->onPlatformMakeCurrent(); |
77 } | 79 } |
78 | 80 |
79 void SkGLContext::swapBuffers() { | 81 void GLContext::swapBuffers() { |
80 this->onPlatformSwapBuffers(); | 82 this->onPlatformSwapBuffers(); |
81 } | 83 } |
82 | 84 |
83 void SkGLContext::waitOnSyncOrSwap() { | 85 void GLContext::waitOnSyncOrSwap() { |
84 if (!fFenceSync) { | 86 if (!fFenceSync) { |
85 // Fallback on the platform SwapBuffers method for synchronization. This
may have no effect. | 87 // Fallback on the platform SwapBuffers method for synchronization. This
may have no effect. |
86 this->swapBuffers(); | 88 this->swapBuffers(); |
87 return; | 89 return; |
88 } | 90 } |
89 | 91 |
90 if (fFrameFences[fCurrentFenceIdx]) { | 92 if (fFrameFences[fCurrentFenceIdx]) { |
91 if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx], true)) { | 93 if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx], true)) { |
92 SkDebugf("WARNING: Wait failed for fence sync. Timings might not be
accurate.\n"); | 94 SkDebugf("WARNING: Wait failed for fence sync. Timings might not be
accurate.\n"); |
93 } | 95 } |
94 fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]); | 96 fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]); |
95 } | 97 } |
96 | 98 |
97 fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence(); | 99 fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence(); |
98 fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences); | 100 fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences); |
99 } | 101 } |
100 | 102 |
101 void SkGLContext::testAbandon() { | 103 void GLContext::testAbandon() { |
102 if (fGL) { | 104 if (fGL) { |
103 fGL->abandon(); | 105 fGL->abandon(); |
104 } | 106 } |
105 if (fFenceSync) { | 107 if (fFenceSync) { |
106 memset(fFrameFences, 0, sizeof(fFrameFences)); | 108 memset(fFrameFences, 0, sizeof(fFrameFences)); |
107 } | 109 } |
108 } | 110 } |
109 | 111 |
110 SkGLContext::GLFenceSync* SkGLContext::GLFenceSync::CreateIfSupported(const SkGL
Context* ctx) { | 112 GLContext::GLFenceSync* GLContext::GLFenceSync::CreateIfSupported(const GLContex
t* ctx) { |
111 SkAutoTDelete<GLFenceSync> ret(new GLFenceSync); | 113 SkAutoTDelete<GLFenceSync> ret(new GLFenceSync); |
112 | 114 |
113 if (kGL_GrGLStandard == ctx->gl()->fStandard) { | 115 if (kGL_GrGLStandard == ctx->gl()->fStandard) { |
114 const GrGLubyte* versionStr; | 116 const GrGLubyte* versionStr; |
115 SK_GL_RET(*ctx, versionStr, GetString(GR_GL_VERSION)); | 117 GR_GL_CALL_RET(ctx->gl(), versionStr, GetString(GR_GL_VERSION)); |
116 GrGLVersion version = GrGLGetVersionFromString(reinterpret_cast<const ch
ar*>(versionStr)); | 118 GrGLVersion version = GrGLGetVersionFromString(reinterpret_cast<const ch
ar*>(versionStr)); |
117 if (version < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync"))
{ | 119 if (version < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync"))
{ |
118 return nullptr; | 120 return nullptr; |
119 } | 121 } |
120 ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>( | 122 ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>( |
121 ctx->onPlatformGetProcAddress("glFenceSync")); | 123 ctx->onPlatformGetProcAddress("glFenceSync")); |
122 ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>( | 124 ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>( |
123 ctx->onPlatformGetProcAddress("glClientWaitSync")); | 125 ctx->onPlatformGetProcAddress("glClientWaitSync")); |
124 ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>( | 126 ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>( |
125 ctx->onPlatformGetProcAddress("glDeleteSync")); | 127 ctx->onPlatformGetProcAddress("glDeleteSync")); |
126 } else { | 128 } else { |
127 if (!ctx->gl()->hasExtension("GL_APPLE_sync")) { | 129 if (!ctx->gl()->hasExtension("GL_APPLE_sync")) { |
128 return nullptr; | 130 return nullptr; |
129 } | 131 } |
130 ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>( | 132 ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>( |
131 ctx->onPlatformGetProcAddress("glFenceSyncAPPLE")); | 133 ctx->onPlatformGetProcAddress("glFenceSyncAPPLE")); |
132 ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>( | 134 ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>( |
133 ctx->onPlatformGetProcAddress("glClientWaitSyncAPPLE")); | 135 ctx->onPlatformGetProcAddress("glClientWaitSyncAPPLE")); |
134 ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>( | 136 ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>( |
135 ctx->onPlatformGetProcAddress("glDeleteSyncAPPLE")); | 137 ctx->onPlatformGetProcAddress("glDeleteSyncAPPLE")); |
136 } | 138 } |
137 | 139 |
138 if (!ret->fGLFenceSync || !ret->fGLClientWaitSync || !ret->fGLDeleteSync) { | 140 if (!ret->fGLFenceSync || !ret->fGLClientWaitSync || !ret->fGLDeleteSync) { |
139 return nullptr; | 141 return nullptr; |
140 } | 142 } |
141 | 143 |
142 return ret.release(); | 144 return ret.release(); |
143 } | 145 } |
144 | 146 |
145 SkPlatformGpuFence SkGLContext::GLFenceSync::insertFence() const { | 147 SkPlatformGpuFence GLContext::GLFenceSync::insertFence() const { |
146 return fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); | 148 return fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
147 } | 149 } |
148 | 150 |
149 bool SkGLContext::GLFenceSync::waitFence(SkPlatformGpuFence fence, bool flush) c
onst { | 151 bool GLContext::GLFenceSync::waitFence(SkPlatformGpuFence fence, bool flush) con
st { |
150 GLsync glsync = static_cast<GLsync>(fence); | 152 GLsync glsync = static_cast<GLsync>(fence); |
151 return GL_WAIT_FAILED != fGLClientWaitSync(glsync, flush ? GL_SYNC_FLUSH_COM
MANDS_BIT : 0, -1); | 153 return GL_WAIT_FAILED != fGLClientWaitSync(glsync, flush ? GL_SYNC_FLUSH_COM
MANDS_BIT : 0, -1); |
152 } | 154 } |
153 | 155 |
154 void SkGLContext::GLFenceSync::deleteFence(SkPlatformGpuFence fence) const { | 156 void GLContext::GLFenceSync::deleteFence(SkPlatformGpuFence fence) const { |
155 GLsync glsync = static_cast<GLsync>(fence); | 157 GLsync glsync = static_cast<GLsync>(fence); |
156 fGLDeleteSync(glsync); | 158 fGLDeleteSync(glsync); |
157 } | 159 } |
158 | 160 |
159 GrGLint SkGLContext::createTextureRectangle(int width, int height, GrGLenum inte
rnalFormat, | 161 GrGLint GLContext::createTextureRectangle(int width, int height, GrGLenum intern
alFormat, |
160 GrGLenum externalFormat, GrGLenum ex
ternalType, | 162 GrGLenum externalFormat, GrGLenum exte
rnalType, |
161 GrGLvoid* data) { | 163 GrGLvoid* data) { |
162 if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER
(3, 1)) && | 164 if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER
(3, 1)) && |
163 !fGL->fExtensions.has("GL_ARB_texture_rectangle")) { | 165 !fGL->fExtensions.has("GL_ARB_texture_rectangle")) { |
164 return 0; | 166 return 0; |
165 } | 167 } |
166 | 168 |
167 if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) { | 169 if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) { |
168 return 0; | 170 return 0; |
169 } | 171 } |
170 | 172 |
171 GrGLuint id; | 173 GrGLuint id; |
172 GR_GL_CALL(fGL, GenTextures(1, &id)); | 174 GR_GL_CALL(fGL, GenTextures(1, &id)); |
173 GR_GL_CALL(fGL, BindTexture(GR_GL_TEXTURE_RECTANGLE, id)); | 175 GR_GL_CALL(fGL, BindTexture(GR_GL_TEXTURE_RECTANGLE, id)); |
174 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FIL
TER, | 176 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FIL
TER, |
175 GR_GL_NEAREST)); | 177 GR_GL_NEAREST)); |
176 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FIL
TER, | 178 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FIL
TER, |
177 GR_GL_NEAREST)); | 179 GR_GL_NEAREST)); |
178 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S, | 180 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S, |
179 GR_GL_CLAMP_TO_EDGE)); | 181 GR_GL_CLAMP_TO_EDGE)); |
180 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T, | 182 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T, |
181 GR_GL_CLAMP_TO_EDGE)); | 183 GR_GL_CLAMP_TO_EDGE)); |
182 GR_GL_CALL(fGL, TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width
, height, 0, | 184 GR_GL_CALL(fGL, TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width
, height, 0, |
183 externalFormat, externalType, data)); | 185 externalFormat, externalType, data)); |
184 return id; | 186 return id; |
185 } | 187 } |
| 188 } // namespace sk_gpu_test |
OLD | NEW |