blob: 1f9011fbd972a9eed5468df2aa1bf5c2d7dcea5b [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001/*
robertphillips@google.com6177e692013-02-28 20:16:25 +00002 * Copyright 2013 Google Inc.
bsalomon@google.com373a6632011-10-19 20:43:20 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
robertphillips6f70d432016-03-30 09:26:24 -07007#include "gl/SkGLContext.h"
8#include "GrGLUtil.h"
cdaltond416a5b2015-06-23 13:23:44 -07009#include "SkGpuFenceSync.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000010
robertphillips6f70d432016-03-30 09:26:24 -070011class SkGLContext::GLFenceSync : public SkGpuFenceSync {
cdaltond416a5b2015-06-23 13:23:44 -070012public:
robertphillips6f70d432016-03-30 09:26:24 -070013 static GLFenceSync* CreateIfSupported(const SkGLContext*);
cdaltond416a5b2015-06-23 13:23:44 -070014
15 SkPlatformGpuFence SK_WARN_UNUSED_RESULT insertFence() const override;
joshualitt4e8f5672016-01-20 10:54:58 -080016 bool waitFence(SkPlatformGpuFence fence, bool flush) const override;
cdaltond416a5b2015-06-23 13:23:44 -070017 void deleteFence(SkPlatformGpuFence fence) const override;
18
19private:
20 GLFenceSync() {}
21
22 static const GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117;
23 static const GrGLenum GL_WAIT_FAILED = 0x911d;
24 static const GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001;
25
26 typedef struct __GLsync *GLsync;
27
28 typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfield);
29 typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbitfield, GrGLuint64);
30 typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync);
31
32 GLFenceSyncProc fGLFenceSync;
33 GLClientWaitSyncProc fGLClientWaitSync;
34 GLDeleteSyncProc fGLDeleteSync;
35
36 typedef SkGpuFenceSync INHERITED;
37};
38
robertphillips6f70d432016-03-30 09:26:24 -070039SkGLContext::SkGLContext()
cdaltond416a5b2015-06-23 13:23:44 -070040 : fCurrentFenceIdx(0) {
41 memset(fFrameFences, 0, sizeof(fFrameFences));
bsalomon@google.com373a6632011-10-19 20:43:20 +000042}
43
robertphillips6f70d432016-03-30 09:26:24 -070044SkGLContext::~SkGLContext() {
cdaltond416a5b2015-06-23 13:23:44 -070045 // Subclass should call teardown.
46#ifdef SK_DEBUG
47 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
48 SkASSERT(0 == fFrameFences[i]);
49 }
50#endif
halcanary96fcdcc2015-08-27 07:41:13 -070051 SkASSERT(nullptr == fGL.get());
52 SkASSERT(nullptr == fFenceSync.get());
cdaltond416a5b2015-06-23 13:23:44 -070053}
54
robertphillips6f70d432016-03-30 09:26:24 -070055void SkGLContext::init(const GrGLInterface* gl, SkGpuFenceSync* fenceSync) {
cdaltond416a5b2015-06-23 13:23:44 -070056 SkASSERT(!fGL.get());
57 fGL.reset(gl);
58 fFenceSync.reset(fenceSync ? fenceSync : GLFenceSync::CreateIfSupported(this));
59}
60
robertphillips6f70d432016-03-30 09:26:24 -070061void SkGLContext::teardown() {
cdaltond416a5b2015-06-23 13:23:44 -070062 if (fFenceSync) {
63 for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
64 if (fFrameFences[i]) {
65 fFenceSync->deleteFence(fFrameFences[i]);
66 fFrameFences[i] = 0;
67 }
68 }
halcanary96fcdcc2015-08-27 07:41:13 -070069 fFenceSync.reset(nullptr);
cdaltond416a5b2015-06-23 13:23:44 -070070 }
71
halcanary96fcdcc2015-08-27 07:41:13 -070072 fGL.reset(nullptr);
cdaltond416a5b2015-06-23 13:23:44 -070073}
74
robertphillips6f70d432016-03-30 09:26:24 -070075void SkGLContext::makeCurrent() const {
cdaltond416a5b2015-06-23 13:23:44 -070076 this->onPlatformMakeCurrent();
77}
78
robertphillips6f70d432016-03-30 09:26:24 -070079void SkGLContext::swapBuffers() {
joshualitt01836ad2016-01-20 13:09:12 -080080 this->onPlatformSwapBuffers();
81}
82
robertphillips6f70d432016-03-30 09:26:24 -070083void SkGLContext::waitOnSyncOrSwap() {
cdaltond416a5b2015-06-23 13:23:44 -070084 if (!fFenceSync) {
85 // Fallback on the platform SwapBuffers method for synchronization. This may have no effect.
joshualitt01836ad2016-01-20 13:09:12 -080086 this->swapBuffers();
cdaltond416a5b2015-06-23 13:23:44 -070087 return;
88 }
89
90 if (fFrameFences[fCurrentFenceIdx]) {
joshualitt4e8f5672016-01-20 10:54:58 -080091 if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx], true)) {
cdaltond416a5b2015-06-23 13:23:44 -070092 SkDebugf("WARNING: Wait failed for fence sync. Timings might not be accurate.\n");
93 }
94 fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]);
95 }
96
97 fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence();
98 fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences);
bsalomon@google.com373a6632011-10-19 20:43:20 +000099}
bsalomon944bcf02014-07-29 08:01:52 -0700100
robertphillips6f70d432016-03-30 09:26:24 -0700101void SkGLContext::testAbandon() {
bsalomon49f085d2014-09-05 13:34:00 -0700102 if (fGL) {
bsalomon944bcf02014-07-29 08:01:52 -0700103 fGL->abandon();
104 }
cdaltond416a5b2015-06-23 13:23:44 -0700105 if (fFenceSync) {
106 memset(fFrameFences, 0, sizeof(fFrameFences));
107 }
108}
109
robertphillips6f70d432016-03-30 09:26:24 -0700110SkGLContext::GLFenceSync* SkGLContext::GLFenceSync::CreateIfSupported(const SkGLContext* ctx) {
halcanary385fe4d2015-08-26 13:07:48 -0700111 SkAutoTDelete<GLFenceSync> ret(new GLFenceSync);
cdaltond416a5b2015-06-23 13:23:44 -0700112
113 if (kGL_GrGLStandard == ctx->gl()->fStandard) {
114 const GrGLubyte* versionStr;
robertphillips6f70d432016-03-30 09:26:24 -0700115 SK_GL_RET(*ctx, versionStr, GetString(GR_GL_VERSION));
cdaltond416a5b2015-06-23 13:23:44 -0700116 GrGLVersion version = GrGLGetVersionFromString(reinterpret_cast<const char*>(versionStr));
117 if (version < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync")) {
halcanary96fcdcc2015-08-27 07:41:13 -0700118 return nullptr;
cdaltond416a5b2015-06-23 13:23:44 -0700119 }
120 ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>(
121 ctx->onPlatformGetProcAddress("glFenceSync"));
122 ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>(
123 ctx->onPlatformGetProcAddress("glClientWaitSync"));
124 ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>(
125 ctx->onPlatformGetProcAddress("glDeleteSync"));
126 } else {
127 if (!ctx->gl()->hasExtension("GL_APPLE_sync")) {
halcanary96fcdcc2015-08-27 07:41:13 -0700128 return nullptr;
cdaltond416a5b2015-06-23 13:23:44 -0700129 }
130 ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>(
131 ctx->onPlatformGetProcAddress("glFenceSyncAPPLE"));
132 ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>(
133 ctx->onPlatformGetProcAddress("glClientWaitSyncAPPLE"));
134 ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>(
135 ctx->onPlatformGetProcAddress("glDeleteSyncAPPLE"));
136 }
137
138 if (!ret->fGLFenceSync || !ret->fGLClientWaitSync || !ret->fGLDeleteSync) {
halcanary96fcdcc2015-08-27 07:41:13 -0700139 return nullptr;
cdaltond416a5b2015-06-23 13:23:44 -0700140 }
141
mtklein18300a32016-03-16 13:53:35 -0700142 return ret.release();
cdaltond416a5b2015-06-23 13:23:44 -0700143}
144
robertphillips6f70d432016-03-30 09:26:24 -0700145SkPlatformGpuFence SkGLContext::GLFenceSync::insertFence() const {
cdaltond416a5b2015-06-23 13:23:44 -0700146 return fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
147}
148
robertphillips6f70d432016-03-30 09:26:24 -0700149bool SkGLContext::GLFenceSync::waitFence(SkPlatformGpuFence fence, bool flush) const {
cdaltond416a5b2015-06-23 13:23:44 -0700150 GLsync glsync = static_cast<GLsync>(fence);
joshualitt4e8f5672016-01-20 10:54:58 -0800151 return GL_WAIT_FAILED != fGLClientWaitSync(glsync, flush ? GL_SYNC_FLUSH_COMMANDS_BIT : 0, -1);
cdaltond416a5b2015-06-23 13:23:44 -0700152}
153
robertphillips6f70d432016-03-30 09:26:24 -0700154void SkGLContext::GLFenceSync::deleteFence(SkPlatformGpuFence fence) const {
cdaltond416a5b2015-06-23 13:23:44 -0700155 GLsync glsync = static_cast<GLsync>(fence);
156 fGLDeleteSync(glsync);
bsalomon944bcf02014-07-29 08:01:52 -0700157}
bsalomone5286e02016-01-14 09:24:09 -0800158
robertphillips6f70d432016-03-30 09:26:24 -0700159GrGLint SkGLContext::createTextureRectangle(int width, int height, GrGLenum internalFormat,
160 GrGLenum externalFormat, GrGLenum externalType,
161 GrGLvoid* data) {
bsalomone179a912016-01-20 06:18:10 -0800162 if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER(3, 1)) &&
bsalomone5286e02016-01-14 09:24:09 -0800163 !fGL->fExtensions.has("GL_ARB_texture_rectangle")) {
164 return 0;
165 }
bsalomone179a912016-01-20 06:18:10 -0800166
167 if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) {
168 return 0;
169 }
170
bsalomone5286e02016-01-14 09:24:09 -0800171 GrGLuint id;
172 GR_GL_CALL(fGL, GenTextures(1, &id));
173 GR_GL_CALL(fGL, BindTexture(GR_GL_TEXTURE_RECTANGLE, id));
174 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER,
175 GR_GL_NEAREST));
176 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER,
177 GR_GL_NEAREST));
178 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S,
robertphillips6f70d432016-03-30 09:26:24 -0700179 GR_GL_CLAMP_TO_EDGE));
bsalomone5286e02016-01-14 09:24:09 -0800180 GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T,
181 GR_GL_CLAMP_TO_EDGE));
182 GR_GL_CALL(fGL, TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0,
183 externalFormat, externalType, data));
184 return id;
185}