blob: 01f827c8fa0ee0572c1e28fdac0f7732df5795ce [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001
2/*
robertphillips@google.com6177e692013-02-28 20:16:25 +00003 * Copyright 2013 Google Inc.
bsalomon@google.com373a6632011-10-19 20:43:20 +00004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
kkinnunen9e61bb72014-10-09 05:24:15 -07008#include "gl/SkGLContext.h"
bsalomon@google.com1744f972013-02-26 21:46:32 +00009#include "GrGLUtil.h"
cdaltond416a5b2015-06-23 13:23:44 -070010#include "SkGpuFenceSync.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000011
cdaltond416a5b2015-06-23 13:23:44 -070012class SkGLContext::GLFenceSync : public SkGpuFenceSync {
13public:
14 static GLFenceSync* CreateIfSupported(const SkGLContext*);
15
16 SkPlatformGpuFence SK_WARN_UNUSED_RESULT insertFence() const override;
joshualitt4e8f5672016-01-20 10:54:58 -080017 bool waitFence(SkPlatformGpuFence fence, bool flush) const override;
cdaltond416a5b2015-06-23 13:23:44 -070018 void deleteFence(SkPlatformGpuFence fence) const override;
19
20private:
21 GLFenceSync() {}
22
23 static const GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117;
24 static const GrGLenum GL_WAIT_FAILED = 0x911d;
25 static const GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001;
26
27 typedef struct __GLsync *GLsync;
28
29 typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfield);
30 typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbitfield, GrGLuint64);
31 typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync);
32
33 GLFenceSyncProc fGLFenceSync;
34 GLClientWaitSyncProc fGLClientWaitSync;
35 GLDeleteSyncProc fGLDeleteSync;
36
37 typedef SkGpuFenceSync INHERITED;
38};
39
40SkGLContext::SkGLContext()
41 : fCurrentFenceIdx(0) {
42 memset(fFrameFences, 0, sizeof(fFrameFences));
bsalomon@google.com373a6632011-10-19 20:43:20 +000043}
44
kkinnunen9e61bb72014-10-09 05:24:15 -070045SkGLContext::~SkGLContext() {
cdaltond416a5b2015-06-23 13:23:44 -070046 // Subclass should call teardown.
47#ifdef SK_DEBUG
48 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
49 SkASSERT(0 == fFrameFences[i]);
50 }
51#endif
halcanary96fcdcc2015-08-27 07:41:13 -070052 SkASSERT(nullptr == fGL.get());
53 SkASSERT(nullptr == fFenceSync.get());
cdaltond416a5b2015-06-23 13:23:44 -070054}
55
56void SkGLContext::init(const GrGLInterface* gl, SkGpuFenceSync* fenceSync) {
57 SkASSERT(!fGL.get());
58 fGL.reset(gl);
59 fFenceSync.reset(fenceSync ? fenceSync : GLFenceSync::CreateIfSupported(this));
60}
61
62void SkGLContext::teardown() {
63 if (fFenceSync) {
64 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
65 if (fFrameFences[i]) {
66 fFenceSync->deleteFence(fFrameFences[i]);
67 fFrameFences[i] = 0;
68 }
69 }
halcanary96fcdcc2015-08-27 07:41:13 -070070 fFenceSync.reset(nullptr);
cdaltond416a5b2015-06-23 13:23:44 -070071 }
72
halcanary96fcdcc2015-08-27 07:41:13 -070073 fGL.reset(nullptr);
cdaltond416a5b2015-06-23 13:23:44 -070074}
75
76void SkGLContext::makeCurrent() const {
77 this->onPlatformMakeCurrent();
78}
79
80void SkGLContext::swapBuffers() {
joshualitt01836ad2016-01-20 13:09:12 -080081 this->onPlatformSwapBuffers();
82}
83
84void SkGLContext::waitOnSyncOrSwap() {
cdaltond416a5b2015-06-23 13:23:44 -070085 if (!fFenceSync) {
86 // Fallback on the platform SwapBuffers method for synchronization. This may have no effect.
joshualitt01836ad2016-01-20 13:09:12 -080087 this->swapBuffers();
cdaltond416a5b2015-06-23 13:23:44 -070088 return;
89 }
90
91 if (fFrameFences[fCurrentFenceIdx]) {
joshualitt4e8f5672016-01-20 10:54:58 -080092 if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx], true)) {
cdaltond416a5b2015-06-23 13:23:44 -070093 SkDebugf("WARNING: Wait failed for fence sync. Timings might not be accurate.\n");
94 }
95 fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]);
96 }
97
98 fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence();
99 fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences);
bsalomon@google.com373a6632011-10-19 20:43:20 +0000100}
bsalomon944bcf02014-07-29 08:01:52 -0700101
kkinnunen9e61bb72014-10-09 05:24:15 -0700102void SkGLContext::testAbandon() {
bsalomon49f085d2014-09-05 13:34:00 -0700103 if (fGL) {
bsalomon944bcf02014-07-29 08:01:52 -0700104 fGL->abandon();
105 }
cdaltond416a5b2015-06-23 13:23:44 -0700106 if (fFenceSync) {
107 memset(fFrameFences, 0, sizeof(fFrameFences));
108 }
109}
110
111SkGLContext::GLFenceSync* SkGLContext::GLFenceSync::CreateIfSupported(const SkGLContext* ctx) {
halcanary385fe4d2015-08-26 13:07:48 -0700112 SkAutoTDelete<GLFenceSync> ret(new GLFenceSync);
cdaltond416a5b2015-06-23 13:23:44 -0700113
114 if (kGL_GrGLStandard == ctx->gl()->fStandard) {
115 const GrGLubyte* versionStr;
116 SK_GL_RET(*ctx, versionStr, GetString(GR_GL_VERSION));
117 GrGLVersion version = GrGLGetVersionFromString(reinterpret_cast<const char*>(versionStr));
118 if (version < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync")) {
halcanary96fcdcc2015-08-27 07:41:13 -0700119 return nullptr;
cdaltond416a5b2015-06-23 13:23:44 -0700120 }
121 ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>(
122 ctx->onPlatformGetProcAddress("glFenceSync"));
123 ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>(
124 ctx->onPlatformGetProcAddress("glClientWaitSync"));
125 ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>(
126 ctx->onPlatformGetProcAddress("glDeleteSync"));
127 } else {
128 if (!ctx->gl()->hasExtension("GL_APPLE_sync")) {
halcanary96fcdcc2015-08-27 07:41:13 -0700129 return nullptr;
cdaltond416a5b2015-06-23 13:23:44 -0700130 }
131 ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>(
132 ctx->onPlatformGetProcAddress("glFenceSyncAPPLE"));
133 ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>(
134 ctx->onPlatformGetProcAddress("glClientWaitSyncAPPLE"));
135 ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>(
136 ctx->onPlatformGetProcAddress("glDeleteSyncAPPLE"));
137 }
138
139 if (!ret->fGLFenceSync || !ret->fGLClientWaitSync || !ret->fGLDeleteSync) {
halcanary96fcdcc2015-08-27 07:41:13 -0700140 return nullptr;
cdaltond416a5b2015-06-23 13:23:44 -0700141 }
142
143 return ret.detach();
144}
145
146SkPlatformGpuFence SkGLContext::GLFenceSync::insertFence() const {
147 return fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
148}
149
joshualitt4e8f5672016-01-20 10:54:58 -0800150bool SkGLContext::GLFenceSync::waitFence(SkPlatformGpuFence fence, bool flush) const {
cdaltond416a5b2015-06-23 13:23:44 -0700151 GLsync glsync = static_cast<GLsync>(fence);
joshualitt4e8f5672016-01-20 10:54:58 -0800152 return GL_WAIT_FAILED != fGLClientWaitSync(glsync, flush ? GL_SYNC_FLUSH_COMMANDS_BIT : 0, -1);
cdaltond416a5b2015-06-23 13:23:44 -0700153}
154
155void SkGLContext::GLFenceSync::deleteFence(SkPlatformGpuFence fence) const {
156 GLsync glsync = static_cast<GLsync>(fence);
157 fGLDeleteSync(glsync);
bsalomon944bcf02014-07-29 08:01:52 -0700158}
bsalomone5286e02016-01-14 09:24:09 -0800159
160GrGLint SkGLContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
161 GrGLenum externalFormat, GrGLenum externalType,
162 GrGLvoid* data) {
bsalomone179a912016-01-20 06:18:10 -0800163 if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER(3, 1)) &&
bsalomone5286e02016-01-14 09:24:09 -0800164 !fGL->fExtensions.has("GL_ARB_texture_rectangle")) {
165 return 0;
166 }
bsalomone179a912016-01-20 06:18:10 -0800167
168 if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) {
169 return 0;
170 }
171
bsalomone5286e02016-01-14 09:24:09 -0800172 GrGLuint id;
173 GR_GL_CALL(fGL, GenTextures(1, &id));
174 GR_GL_CALL(fGL, BindTexture(GR_GL_TEXTURE_RECTANGLE, id));
175 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER,
176 GR_GL_NEAREST));
177 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER,
178 GR_GL_NEAREST));
179 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S,
180 GR_GL_CLAMP_TO_EDGE));
181 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T,
182 GR_GL_CLAMP_TO_EDGE));
183 GR_GL_CALL(fGL, TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0,
184 externalFormat, externalType, data));
185 return id;
186}