fix macOS build (following Projucer changes made in Windows, which removed /Applications/JUCE/modules from its headers). move JUCE headers under source control, so that Windows and macOS can both build against same version of JUCE. remove AUv3 target (I think it's an iOS thing, so it will never work with this macOS fluidsynth dylib).

This commit is contained in:
Alex Birch
2018-06-17 13:34:53 +01:00
parent a2be47c887
commit dff4d13a1d
1563 changed files with 601601 additions and 3466 deletions

View File

@ -0,0 +1,167 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
/** These are important openGL values that aren't defined by default
by the GL headers on various platforms.
*/
enum MissingOpenGLDefinitions
{
#ifndef GL_CLAMP_TO_EDGE
GL_CLAMP_TO_EDGE = 0x812f,
#endif
#ifndef GL_NUM_EXTENSIONS
GL_NUM_EXTENSIONS = 0x821d,
#endif
#ifndef GL_BGRA_EXT
GL_BGRA_EXT = 0x80e1,
#endif
#ifndef GL_DEPTH24_STENCIL8
GL_DEPTH24_STENCIL8 = 0x88F0,
#endif
#ifndef GL_RGBA8
GL_RGBA8 = GL_RGBA,
#endif
#ifndef GL_RGBA32F
GL_RGBA32F = 0x8814,
#endif
#ifndef GL_COLOR_ATTACHMENT0
GL_COLOR_ATTACHMENT0 = 0x8CE0,
#endif
#ifndef GL_DEPTH_ATTACHMENT
GL_DEPTH_ATTACHMENT = 0x8D00,
#endif
#ifndef GL_FRAMEBUFFER
GL_FRAMEBUFFER = 0x8D40,
#endif
#ifndef GL_FRAMEBUFFER_BINDING
GL_FRAMEBUFFER_BINDING = 0x8CA6,
#endif
#ifndef GL_FRAMEBUFFER_COMPLETE
GL_FRAMEBUFFER_COMPLETE = 0x8CD5,
#endif
#ifndef GL_RENDERBUFFER
GL_RENDERBUFFER = 0x8D41,
#endif
#ifndef GL_RENDERBUFFER_DEPTH_SIZE
GL_RENDERBUFFER_DEPTH_SIZE = 0x8D54,
#endif
#ifndef GL_STENCIL_ATTACHMENT
GL_STENCIL_ATTACHMENT = 0x8D20,
#endif
#ifndef GL_MULTISAMPLE
GL_MULTISAMPLE = 0x809D,
#endif
#ifndef GL_MAX_ELEMENTS_INDICES
GL_MAX_ELEMENTS_INDICES = 0x80E9,
#endif
#if JUCE_WINDOWS && ! defined (GL_TEXTURE0)
GL_OPERAND0_RGB = 0x8590,
GL_OPERAND1_RGB = 0x8591,
GL_OPERAND0_ALPHA = 0x8598,
GL_OPERAND1_ALPHA = 0x8599,
GL_SRC0_RGB = 0x8580,
GL_SRC1_RGB = 0x8581,
GL_SRC0_ALPHA = 0x8588,
GL_SRC1_ALPHA = 0x8589,
GL_TEXTURE0 = 0x84C0,
GL_TEXTURE1 = 0x84C1,
GL_TEXTURE2 = 0x84C2,
GL_COMBINE = 0x8570,
GL_COMBINE_RGB = 0x8571,
GL_COMBINE_ALPHA = 0x8572,
GL_PREVIOUS = 0x8578,
GL_COMPILE_STATUS = 0x8B81,
GL_LINK_STATUS = 0x8B82,
GL_SHADING_LANGUAGE_VERSION = 0x8B8C,
GL_FRAGMENT_SHADER = 0x8B30,
GL_VERTEX_SHADER = 0x8B31,
GL_ARRAY_BUFFER = 0x8892,
GL_ELEMENT_ARRAY_BUFFER = 0x8893,
GL_STATIC_DRAW = 0x88E4,
GL_DYNAMIC_DRAW = 0x88E8,
GL_STREAM_DRAW = 0x88E0,
WGL_NUMBER_PIXEL_FORMATS_ARB = 0x2000,
WGL_DRAW_TO_WINDOW_ARB = 0x2001,
WGL_ACCELERATION_ARB = 0x2003,
WGL_SWAP_METHOD_ARB = 0x2007,
WGL_SUPPORT_OPENGL_ARB = 0x2010,
WGL_PIXEL_TYPE_ARB = 0x2013,
WGL_DOUBLE_BUFFER_ARB = 0x2011,
WGL_COLOR_BITS_ARB = 0x2014,
WGL_RED_BITS_ARB = 0x2015,
WGL_GREEN_BITS_ARB = 0x2017,
WGL_BLUE_BITS_ARB = 0x2019,
WGL_ALPHA_BITS_ARB = 0x201B,
WGL_DEPTH_BITS_ARB = 0x2022,
WGL_STENCIL_BITS_ARB = 0x2023,
WGL_FULL_ACCELERATION_ARB = 0x2027,
WGL_ACCUM_RED_BITS_ARB = 0x201E,
WGL_ACCUM_GREEN_BITS_ARB = 0x201F,
WGL_ACCUM_BLUE_BITS_ARB = 0x2020,
WGL_ACCUM_ALPHA_BITS_ARB = 0x2021,
WGL_STEREO_ARB = 0x2012,
WGL_SAMPLE_BUFFERS_ARB = 0x2041,
WGL_SAMPLES_ARB = 0x2042,
WGL_TYPE_RGBA_ARB = 0x202B,
WGL_CONTEXT_MAJOR_VERSION_ARB = 0x2091,
WGL_CONTEXT_MINOR_VERSION_ARB = 0x2092,
WGL_CONTEXT_PROFILE_MASK_ARB = 0x9126,
#endif
#if JUCE_ANDROID
JUCE_RGBA_FORMAT = GL_RGBA
#else
JUCE_RGBA_FORMAT = GL_BGRA_EXT
#endif
};
#if JUCE_WINDOWS
typedef char GLchar;
typedef pointer_sized_int GLsizeiptr;
typedef pointer_sized_int GLintptr;
#endif
} // namespace juce

View File

@ -0,0 +1,160 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
@see OpenGLExtensionFunctions
*/
#define JUCE_GL_BASE_FUNCTIONS(USE_FUNCTION) \
USE_FUNCTION (glActiveTexture, void, (GLenum p1), (p1))\
USE_FUNCTION (glBindBuffer, void, (GLenum p1, GLuint p2), (p1, p2))\
USE_FUNCTION (glDeleteBuffers, void, (GLsizei p1, const GLuint* p2), (p1, p2))\
USE_FUNCTION (glGenBuffers, void, (GLsizei p1, GLuint* p2), (p1, p2))\
USE_FUNCTION (glBufferData, void, (GLenum p1, GLsizeiptr p2, const GLvoid* p3, GLenum p4), (p1, p2, p3, p4))\
USE_FUNCTION (glBufferSubData, void, (GLenum p1, GLintptr p2, GLsizeiptr p3, const GLvoid* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glCreateProgram, GLuint, (), ())\
USE_FUNCTION (glDeleteProgram, void, (GLuint p1), (p1))\
USE_FUNCTION (glCreateShader, GLuint, (GLenum p1), (p1))\
USE_FUNCTION (glDeleteShader, void, (GLuint p1), (p1))\
USE_FUNCTION (glShaderSource, void, (GLuint p1, GLsizei p2, const GLchar** p3, const GLint* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glCompileShader, void, (GLuint p1), (p1))\
USE_FUNCTION (glAttachShader, void, (GLuint p1, GLuint p2), (p1, p2))\
USE_FUNCTION (glLinkProgram, void, (GLuint p1), (p1))\
USE_FUNCTION (glUseProgram, void, (GLuint p1), (p1))\
USE_FUNCTION (glGetShaderiv, void, (GLuint p1, GLenum p2, GLint* p3), (p1, p2, p3))\
USE_FUNCTION (glGetShaderInfoLog, void, (GLuint p1, GLsizei p2, GLsizei* p3, GLchar* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glGetProgramInfoLog, void, (GLuint p1, GLsizei p2, GLsizei* p3, GLchar* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glGetProgramiv, void, (GLuint p1, GLenum p2, GLint* p3), (p1, p2, p3))\
USE_FUNCTION (glGetUniformLocation, GLint, (GLuint p1, const GLchar* p2), (p1, p2))\
USE_FUNCTION (glGetAttribLocation, GLint, (GLuint p1, const GLchar* p2), (p1, p2))\
USE_FUNCTION (glVertexAttribPointer, void, (GLuint p1, GLint p2, GLenum p3, GLboolean p4, GLsizei p5, const GLvoid* p6), (p1, p2, p3, p4, p5, p6))\
USE_FUNCTION (glEnableVertexAttribArray, void, (GLuint p1), (p1))\
USE_FUNCTION (glDisableVertexAttribArray, void, (GLuint p1), (p1))\
USE_FUNCTION (glUniform1f, void, (GLint p1, GLfloat p2), (p1, p2))\
USE_FUNCTION (glUniform1i, void, (GLint p1, GLint p2), (p1, p2))\
USE_FUNCTION (glUniform2f, void, (GLint p1, GLfloat p2, GLfloat p3), (p1, p2, p3))\
USE_FUNCTION (glUniform3f, void, (GLint p1, GLfloat p2, GLfloat p3, GLfloat p4), (p1, p2, p3, p4))\
USE_FUNCTION (glUniform4f, void, (GLint p1, GLfloat p2, GLfloat p3, GLfloat p4, GLfloat p5), (p1, p2, p3, p4, p5))\
USE_FUNCTION (glUniform4i, void, (GLint p1, GLint p2, GLint p3, GLint p4, GLint p5), (p1, p2, p3, p4, p5))\
USE_FUNCTION (glUniform1fv, void, (GLint p1, GLsizei p2, const GLfloat* p3), (p1, p2, p3))\
USE_FUNCTION (glUniformMatrix2fv, void, (GLint p1, GLsizei p2, GLboolean p3, const GLfloat* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glUniformMatrix3fv, void, (GLint p1, GLsizei p2, GLboolean p3, const GLfloat* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glUniformMatrix4fv, void, (GLint p1, GLsizei p2, GLboolean p3, const GLfloat* p4), (p1, p2, p3, p4))
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
@see OpenGLExtensionFunctions
*/
#define JUCE_GL_EXTENSION_FUNCTIONS(USE_FUNCTION) \
USE_FUNCTION (glIsRenderbuffer, GLboolean, (GLuint p1), (p1))\
USE_FUNCTION (glBindRenderbuffer, void, (GLenum p1, GLuint p2), (p1, p2))\
USE_FUNCTION (glDeleteRenderbuffers, void, (GLsizei p1, const GLuint* p2), (p1, p2))\
USE_FUNCTION (glGenRenderbuffers, void, (GLsizei p1, GLuint* p2), (p1, p2))\
USE_FUNCTION (glRenderbufferStorage, void, (GLenum p1, GLenum p2, GLsizei p3, GLsizei p4), (p1, p2, p3, p4))\
USE_FUNCTION (glGetRenderbufferParameteriv, void, (GLenum p1, GLenum p2, GLint* p3), (p1, p2, p3))\
USE_FUNCTION (glIsFramebuffer, GLboolean, (GLuint p1), (p1))\
USE_FUNCTION (glBindFramebuffer, void, (GLenum p1, GLuint p2), (p1, p2))\
USE_FUNCTION (glDeleteFramebuffers, void, (GLsizei p1, const GLuint* p2), (p1, p2))\
USE_FUNCTION (glGenFramebuffers, void, (GLsizei p1, GLuint* p2), (p1, p2))\
USE_FUNCTION (glCheckFramebufferStatus, GLenum, (GLenum p1), (p1))\
USE_FUNCTION (glFramebufferTexture2D, void, (GLenum p1, GLenum p2, GLenum p3, GLuint p4, GLint p5), (p1, p2, p3, p4, p5))\
USE_FUNCTION (glFramebufferRenderbuffer, void, (GLenum p1, GLenum p2, GLenum p3, GLuint p4), (p1, p2, p3, p4))\
USE_FUNCTION (glGetFramebufferAttachmentParameteriv, void, (GLenum p1, GLenum p2, GLenum p3, GLint* p4), (p1, p2, p3, p4))
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
@see OpenGLExtensionFunctions
*/
#define JUCE_GL_VERTEXBUFFER_FUNCTIONS(USE_FUNCTION) \
USE_FUNCTION (glGenVertexArrays, void, (GLsizei p1, GLuint* p2), (p1, p2))\
USE_FUNCTION (glDeleteVertexArrays, void, (GLsizei p1, const GLuint* p2), (p1, p2))\
USE_FUNCTION (glBindVertexArray, void, (GLuint p1), (p1))
/** This class contains a generated list of OpenGL extension functions, which are either dynamically loaded
for a specific GL context, or simply call-through to the appropriate OS function where available.
@tags{OpenGL}
*/
struct OpenGLExtensionFunctions
{
void initialise();
#if JUCE_WINDOWS && ! DOXYGEN
typedef char GLchar;
typedef pointer_sized_int GLsizeiptr;
typedef pointer_sized_int GLintptr;
#endif
//==============================================================================
#if JUCE_WINDOWS
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
JUCE_GL_BASE_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
#if JUCE_OPENGL3
JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
#endif
//==============================================================================
#elif JUCE_LINUX
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) typedef returnType (*type_ ## name) params; type_ ## name name;
JUCE_GL_BASE_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
#if JUCE_OPENGL3
JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
#endif
//==============================================================================
#elif JUCE_OPENGL_ES
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) static returnType name params noexcept;
JUCE_GL_BASE_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
//==============================================================================
#else
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) inline static returnType name params noexcept { return ::name callparams; }
JUCE_GL_BASE_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
#ifndef GL3_PROTOTYPES
#undef JUCE_DECLARE_GL_FUNCTION
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) inline static returnType name params noexcept { return ::name ## EXT callparams; }
#endif
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
#if JUCE_OPENGL3
#ifndef GL3_PROTOTYPES
#undef JUCE_DECLARE_GL_FUNCTION
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) inline static returnType name params noexcept { return ::name ## APPLE callparams; }
#endif
JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
#endif
#endif
#undef JUCE_DECLARE_GL_FUNCTION
};
} // namespace juce

View File

@ -0,0 +1,323 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
#define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
METHOD (getParent, "getParent", "()Landroid/view/ViewParent;") \
METHOD (layout, "layout", "(IIII)V" ) \
METHOD (getNativeSurface, "getNativeSurface", "()Landroid/view/Surface;") \
DECLARE_JNI_CLASS (NativeSurfaceView, JUCE_ANDROID_ACTIVITY_CLASSPATH "$NativeSurfaceView")
#undef JNI_CLASS_MEMBERS
//==============================================================================
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& comp,
const OpenGLPixelFormat& /*pixelFormat*/,
void* /*contextToShareWith*/,
bool /*useMultisampling*/,
OpenGLVersion)
: component (comp),
hasInitialised (false),
juceContext (nullptr), surface (EGL_NO_SURFACE), context (EGL_NO_CONTEXT)
{
JNIEnv* env = getEnv();
// Do we have a native peer that we can attach to?
if (component.getPeer()->getNativeHandle() == nullptr)
return;
// Initialise the EGL display
if (! initEGLDisplay())
return;
// create a native surface view
surfaceView = GlobalRef (env->CallObjectMethod (android.activity.get(),
JuceAppActivity.createNativeSurfaceView,
reinterpret_cast<jlong> (this)));
if (surfaceView.get() == nullptr)
return;
// add the view to the view hierarchy
// after this the nativecontext can receive callbacks
env->CallVoidMethod ((jobject) component.getPeer()->getNativeHandle(),
AndroidViewGroup.addView, surfaceView.get());
// initialise the geometry of the view
Rectangle<int> bounds = component.getTopLevelComponent()
->getLocalArea (&component, component.getLocalBounds());
bounds *= component.getDesktopScaleFactor();
updateWindowPosition (bounds);
hasInitialised = true;
}
~NativeContext()
{
JNIEnv* env = getEnv();
if (jobject viewParent = env->CallObjectMethod (surfaceView.get(), NativeSurfaceView.getParent))
env->CallVoidMethod (viewParent, AndroidViewGroup.removeView, surfaceView.get());
}
//==============================================================================
bool initialiseOnRenderThread (OpenGLContext& aContext)
{
jassert (hasInitialised);
// has the context already attached?
jassert (surface == EGL_NO_SURFACE && context == EGL_NO_CONTEXT);
JNIEnv* env = getEnv();
// get a pointer to the native window
ANativeWindow* window = nullptr;
if (jobject jSurface = env->CallObjectMethod (surfaceView.get(), NativeSurfaceView.getNativeSurface))
{
window = ANativeWindow_fromSurface(env, jSurface);
// if we didn't succeed the first time, wait 25ms and try again
if (window == nullptr)
{
Thread::sleep (25);
window = ANativeWindow_fromSurface (env, jSurface);
}
}
if (window == nullptr)
{
// failed to get a pointer to the native window after second try so
// bail out
jassertfalse;
return false;
}
// create the surface
surface = eglCreateWindowSurface(display, config, window, 0);
jassert (surface != EGL_NO_SURFACE);
ANativeWindow_release (window);
// create the OpenGL context
EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
jassert (context != EGL_NO_CONTEXT);
juceContext = &aContext;
return true;
}
void shutdownOnRenderThread()
{
jassert (hasInitialised);
// is there a context available to detach?
jassert (surface != EGL_NO_SURFACE && context != EGL_NO_CONTEXT);
eglDestroyContext (display, context);
context = EGL_NO_CONTEXT;
eglDestroySurface (display, surface);
surface = EGL_NO_SURFACE;
}
//==============================================================================
bool makeActive() const noexcept
{
if (! hasInitialised)
return false;
if (surface == EGL_NO_SURFACE || context == EGL_NO_CONTEXT)
return false;
if (! eglMakeCurrent (display, surface, surface, context))
return false;
return true;
}
bool isActive() const noexcept { return eglGetCurrentContext() == context; }
static void deactivateCurrentContext()
{
eglMakeCurrent (display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}
//==============================================================================
void swapBuffers() const noexcept { eglSwapBuffers (display, surface); }
bool setSwapInterval (const int) { return false; }
int getSwapInterval() const { return 0; }
//==============================================================================
bool createdOk() const noexcept { return hasInitialised; }
void* getRawContext() const noexcept { return surfaceView.get(); }
GLuint getFrameBufferID() const noexcept { return 0; }
//==============================================================================
void updateWindowPosition (Rectangle<int> bounds)
{
if (lastBounds != bounds)
{
JNIEnv* env = getEnv();
lastBounds = bounds;
Rectangle<int> r = bounds * Desktop::getInstance().getDisplays().getMainDisplay().scale;
env->CallVoidMethod (surfaceView.get(), NativeSurfaceView.layout,
(jint) r.getX(), (jint) r.getY(), (jint) r.getRight(), (jint) r.getBottom());
}
}
//==============================================================================
// Android Surface Callbacks:
void dispatchDraw (jobject canvas)
{
ignoreUnused (canvas);
if (juceContext != nullptr)
juceContext->triggerRepaint();
}
void surfaceChanged (jobject holder, int format, int width, int height)
{
ignoreUnused (holder, format, width, height);
}
void surfaceCreated (jobject holder);
void surfaceDestroyed (jobject holder);
//==============================================================================
struct Locker { Locker (NativeContext&) {} };
Component& component;
private:
//==============================================================================
bool initEGLDisplay()
{
// already initialised?
if (display != EGL_NO_DISPLAY)
return true;
const EGLint attribs[] =
{
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 0,
EGL_DEPTH_SIZE, 16,
EGL_NONE
};
EGLint numConfigs;
if ((display = eglGetDisplay (EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
{
jassertfalse;
return false;
}
if (! eglInitialize (display, 0, 0))
{
jassertfalse;
return false;
}
if (! eglChooseConfig (display, attribs, &config, 1, &numConfigs))
{
eglTerminate (display);
jassertfalse;
return false;
}
return true;
}
//==============================================================================
bool hasInitialised;
GlobalRef surfaceView;
Rectangle<int> lastBounds;
OpenGLContext* juceContext;
EGLSurface surface;
EGLContext context;
static EGLDisplay display;
static EGLConfig config;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
};
//==============================================================================
JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), dispatchDrawNative,
void, (JNIEnv* env, jobject nativeView, jlong host, jobject canvas))
{
ignoreUnused (nativeView);
setEnv (env);
reinterpret_cast<OpenGLContext::NativeContext*> (host)->dispatchDraw (canvas);
}
JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceChangedNative,
void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder, jint format, jint width, jint height))
{
ignoreUnused (nativeView);
setEnv (env);
reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceChanged (holder, format, width, height);
}
JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceCreatedNative,
void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder))
{
ignoreUnused (nativeView);
setEnv (env);
reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceCreated (holder);
}
JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceDestroyedNative,
void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder))
{
ignoreUnused (nativeView);
setEnv (env);
reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceDestroyed (holder);
}
//==============================================================================
bool OpenGLHelpers::isContextActive()
{
return eglGetCurrentContext() != EGL_NO_CONTEXT;
}
} // namespace juce

View File

@ -0,0 +1,311 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
@interface JuceGLView : UIView
{
}
+ (Class) layerClass;
@end
@implementation JuceGLView
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
@end
extern "C" GLvoid glResolveMultisampleFramebufferAPPLE();
namespace juce
{
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& c,
const OpenGLPixelFormat& pixFormat,
void* contextToShare,
bool multisampling,
OpenGLVersion version)
: component (c), openGLversion (version),
useDepthBuffer (pixFormat.depthBufferBits > 0),
useMSAA (multisampling)
{
JUCE_AUTORELEASEPOOL
{
if (auto* peer = component.getPeer())
{
auto bounds = peer->getAreaCoveredBy (component);
view = [[JuceGLView alloc] initWithFrame: convertToCGRect (bounds)];
view.opaque = YES;
view.hidden = NO;
view.backgroundColor = [UIColor blackColor];
view.userInteractionEnabled = NO;
glLayer = (CAEAGLLayer*) [view layer];
glLayer.opaque = true;
updateWindowPosition (bounds);
[((UIView*) peer->getNativeHandle()) addSubview: view];
if (version == openGL3_2 && [[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
{
if (! createContext (kEAGLRenderingAPIOpenGLES3, contextToShare))
{
releaseContext();
createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
}
}
else
{
createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
}
if (context != nil)
{
// I'd prefer to put this stuff in the initialiseOnRenderThread() call, but doing
// so causes myserious timing-related failures.
[EAGLContext setCurrentContext: context];
createGLBuffers();
deactivateCurrentContext();
}
else
{
jassertfalse;
}
}
else
{
jassertfalse;
}
}
}
~NativeContext()
{
releaseContext();
[view removeFromSuperview];
[view release];
}
bool initialiseOnRenderThread (OpenGLContext&) { return true; }
void shutdownOnRenderThread()
{
JUCE_CHECK_OPENGL_ERROR
freeGLBuffers();
deactivateCurrentContext();
}
bool createdOk() const noexcept { return getRawContext() != nullptr; }
void* getRawContext() const noexcept { return context; }
GLuint getFrameBufferID() const noexcept { return useMSAA ? msaaBufferHandle : frameBufferHandle; }
bool makeActive() const noexcept
{
if (! [EAGLContext setCurrentContext: context])
return false;
glBindFramebuffer (GL_FRAMEBUFFER, useMSAA ? msaaBufferHandle
: frameBufferHandle);
return true;
}
bool isActive() const noexcept
{
return [EAGLContext currentContext] == context;
}
static void deactivateCurrentContext()
{
[EAGLContext setCurrentContext: nil];
}
void swapBuffers()
{
if (useMSAA)
{
glBindFramebuffer (GL_DRAW_FRAMEBUFFER, frameBufferHandle);
glBindFramebuffer (GL_READ_FRAMEBUFFER, msaaBufferHandle);
if (openGLversion >= openGL3_2)
{
glBlitFramebuffer (0, 0, lastBounds.getWidth(), lastBounds.getHeight(),
0, 0, lastBounds.getWidth(), lastBounds.getHeight(),
GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
else
{
glResolveMultisampleFramebufferAPPLE();
}
}
glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
[context presentRenderbuffer: GL_RENDERBUFFER];
if (needToRebuildBuffers)
{
needToRebuildBuffers = false;
freeGLBuffers();
createGLBuffers();
makeActive();
}
}
void updateWindowPosition (Rectangle<int> bounds)
{
view.frame = convertToCGRect (bounds);
glLayer.contentsScale = (CGFloat) (Desktop::getInstance().getDisplays().getMainDisplay().scale
/ component.getDesktopScaleFactor());
if (lastBounds != bounds)
{
lastBounds = bounds;
needToRebuildBuffers = true;
}
}
bool setSwapInterval (int numFramesPerSwap) noexcept
{
swapFrames = numFramesPerSwap;
return false;
}
int getSwapInterval() const noexcept { return swapFrames; }
struct Locker { Locker (NativeContext&) {} };
private:
Component& component;
JuceGLView* view = nil;
CAEAGLLayer* glLayer = nil;
EAGLContext* context = nil;
const OpenGLVersion openGLversion;
const bool useDepthBuffer, useMSAA;
GLuint frameBufferHandle = 0, colorBufferHandle = 0, depthBufferHandle = 0,
msaaColorHandle = 0, msaaBufferHandle = 0;
Rectangle<int> lastBounds;
int swapFrames = 0;
bool needToRebuildBuffers = false;
bool createContext (EAGLRenderingAPI type, void* contextToShare)
{
jassert (context == nil);
context = [EAGLContext alloc];
context = contextToShare != nullptr
? [context initWithAPI: type sharegroup: [(EAGLContext*) contextToShare sharegroup]]
: [context initWithAPI: type];
return context != nil;
}
void releaseContext()
{
[context release];
context = nil;
}
//==============================================================================
void createGLBuffers()
{
glGenFramebuffers (1, &frameBufferHandle);
glGenRenderbuffers (1, &colorBufferHandle);
glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferHandle);
bool ok = [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: glLayer];
jassert (ok); ignoreUnused (ok);
GLint width, height;
glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
if (useMSAA)
{
glGenFramebuffers (1, &msaaBufferHandle);
glGenRenderbuffers (1, &msaaColorHandle);
glBindFramebuffer (GL_FRAMEBUFFER, msaaBufferHandle);
glBindRenderbuffer (GL_RENDERBUFFER, msaaColorHandle);
glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_RGBA8, width, height);
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorHandle);
}
if (useDepthBuffer)
{
glGenRenderbuffers (1, &depthBufferHandle);
glBindRenderbuffer (GL_RENDERBUFFER, depthBufferHandle);
if (useMSAA)
glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height);
else
glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferHandle);
}
jassert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
JUCE_CHECK_OPENGL_ERROR
}
void freeGLBuffers()
{
JUCE_CHECK_OPENGL_ERROR
[context renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
deleteFrameBuffer (frameBufferHandle);
deleteFrameBuffer (msaaBufferHandle);
deleteRenderBuffer (colorBufferHandle);
deleteRenderBuffer (depthBufferHandle);
deleteRenderBuffer (msaaColorHandle);
JUCE_CHECK_OPENGL_ERROR
}
static void deleteFrameBuffer (GLuint& i) { if (i != 0) glDeleteFramebuffers (1, &i); i = 0; }
static void deleteRenderBuffer (GLuint& i) { if (i != 0) glDeleteRenderbuffers (1, &i); i = 0; }
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
};
//==============================================================================
bool OpenGLHelpers::isContextActive()
{
return [EAGLContext currentContext] != nil;
}
} // namespace juce

View File

@ -0,0 +1,258 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
extern XContext windowHandleXContext;
//==============================================================================
// Defined juce_linux_Windowing.cpp
Rectangle<int> juce_LinuxScaledToPhysicalBounds (ComponentPeer*, Rectangle<int>);
void juce_LinuxAddRepaintListener (ComponentPeer*, Component* dummy);
void juce_LinuxRemoveRepaintListener (ComponentPeer*, Component* dummy);
//==============================================================================
class OpenGLContext::NativeContext
{
private:
struct DummyComponent : public Component
{
DummyComponent (OpenGLContext::NativeContext& nativeParentContext)
: native (nativeParentContext)
{
}
void handleCommandMessage (int commandId) override
{
if (commandId == 0)
native.triggerRepaint();
}
OpenGLContext::NativeContext& native;
};
public:
NativeContext (Component& comp,
const OpenGLPixelFormat& cPixelFormat,
void* shareContext,
bool /*useMultisampling*/,
OpenGLVersion)
: component (comp), contextToShareWith (shareContext), dummy (*this)
{
display = XWindowSystem::getInstance()->displayRef();
ScopedXLock xlock (display);
XSync (display, False);
GLint attribs[] =
{
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_RED_SIZE, cPixelFormat.redBits,
GLX_GREEN_SIZE, cPixelFormat.greenBits,
GLX_BLUE_SIZE, cPixelFormat.blueBits,
GLX_ALPHA_SIZE, cPixelFormat.alphaBits,
GLX_DEPTH_SIZE, cPixelFormat.depthBufferBits,
GLX_STENCIL_SIZE, cPixelFormat.stencilBufferBits,
GLX_ACCUM_RED_SIZE, cPixelFormat.accumulationBufferRedBits,
GLX_ACCUM_GREEN_SIZE, cPixelFormat.accumulationBufferGreenBits,
GLX_ACCUM_BLUE_SIZE, cPixelFormat.accumulationBufferBlueBits,
GLX_ACCUM_ALPHA_SIZE, cPixelFormat.accumulationBufferAlphaBits,
None
};
bestVisual = glXChooseVisual (display, DefaultScreen (display), attribs);
if (bestVisual == nullptr)
return;
auto* peer = component.getPeer();
jassert (peer != nullptr);
auto windowH = (Window) peer->getNativeHandle();
auto colourMap = XCreateColormap (display, windowH, bestVisual->visual, AllocNone);
XSetWindowAttributes swa;
swa.colormap = colourMap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | StructureNotifyMask;
auto glBounds = component.getTopLevelComponent()
->getLocalArea (&component, component.getLocalBounds());
glBounds = juce_LinuxScaledToPhysicalBounds (peer, glBounds);
embeddedWindow = XCreateWindow (display, windowH,
glBounds.getX(), glBounds.getY(),
(unsigned int) jmax (1, glBounds.getWidth()),
(unsigned int) jmax (1, glBounds.getHeight()),
0, bestVisual->depth,
InputOutput,
bestVisual->visual,
CWBorderPixel | CWColormap | CWEventMask,
&swa);
XSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
XMapWindow (display, embeddedWindow);
XFreeColormap (display, colourMap);
XSync (display, False);
juce_LinuxAddRepaintListener (peer, &dummy);
}
~NativeContext()
{
juce_LinuxRemoveRepaintListener (component.getPeer(), &dummy);
if (embeddedWindow != 0)
{
ScopedXLock xlock (display);
XUnmapWindow (display, embeddedWindow);
XDestroyWindow (display, embeddedWindow);
}
if (bestVisual != nullptr)
XFree (bestVisual);
XWindowSystem::getInstance()->displayUnref();
}
bool initialiseOnRenderThread (OpenGLContext& c)
{
ScopedXLock xlock (display);
renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
c.makeActive();
context = &c;
return true;
}
void shutdownOnRenderThread()
{
context = nullptr;
deactivateCurrentContext();
glXDestroyContext (display, renderContext);
renderContext = nullptr;
}
bool makeActive() const noexcept
{
return renderContext != 0
&& glXMakeCurrent (display, embeddedWindow, renderContext);
}
bool isActive() const noexcept
{
return glXGetCurrentContext() == renderContext && renderContext != 0;
}
static void deactivateCurrentContext()
{
ScopedXDisplay xDisplay;
glXMakeCurrent (xDisplay.display, None, 0);
}
void swapBuffers()
{
glXSwapBuffers (display, embeddedWindow);
}
void updateWindowPosition (Rectangle<int> newBounds)
{
bounds = newBounds;
auto physicalBounds = juce_LinuxScaledToPhysicalBounds (component.getPeer(), bounds);
ScopedXLock xlock (display);
XMoveResizeWindow (display, embeddedWindow,
physicalBounds.getX(), physicalBounds.getY(),
(unsigned int) jmax (1, physicalBounds.getWidth()),
(unsigned int) jmax (1, physicalBounds.getHeight()));
}
bool setSwapInterval (int numFramesPerSwap)
{
if (numFramesPerSwap == swapFrames)
return true;
if (auto GLXSwapIntervalSGI
= (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI"))
{
swapFrames = numFramesPerSwap;
GLXSwapIntervalSGI (numFramesPerSwap);
return true;
}
return false;
}
int getSwapInterval() const { return swapFrames; }
bool createdOk() const noexcept { return true; }
void* getRawContext() const noexcept { return renderContext; }
GLuint getFrameBufferID() const noexcept { return 0; }
void triggerRepaint()
{
if (context != nullptr)
context->triggerRepaint();
}
struct Locker { Locker (NativeContext&) {} };
private:
Component& component;
GLXContext renderContext = {};
Window embeddedWindow = {};
int swapFrames = 0;
Rectangle<int> bounds;
XVisualInfo* bestVisual = {};
void* contextToShareWith;
OpenGLContext* context = {};
DummyComponent dummy;
::Display* display = {};
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
};
//==============================================================================
bool OpenGLHelpers::isContextActive()
{
ScopedXDisplay xDisplay;
if (xDisplay.display)
{
ScopedXLock xlock (xDisplay.display);
return glXGetCurrentContext() != 0;
}
return false;
}
} // namespace juce

View File

@ -0,0 +1,262 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& component,
const OpenGLPixelFormat& pixFormat,
void* contextToShare,
bool shouldUseMultisampling,
OpenGLVersion version)
: lastSwapTime (0), minSwapTimeMs (0), underrunCounter (0)
{
NSOpenGLPixelFormatAttribute attribs[64] = { 0 };
createAttribs (attribs, version, pixFormat, shouldUseMultisampling);
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
static MouseForwardingNSOpenGLViewClass cls;
view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
pixelFormat: format];
#if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
[view setWantsBestResolutionOpenGLSurface: YES];
#endif
[[NSNotificationCenter defaultCenter] addObserver: view
selector: @selector (_surfaceNeedsUpdate:)
name: NSViewGlobalFrameDidChangeNotification
object: view];
renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
shareContext: (NSOpenGLContext*) contextToShare] autorelease];
[view setOpenGLContext: renderContext];
[format release];
viewAttachment = NSViewComponent::attachViewToComponent (component, view);
}
~NativeContext()
{
[[NSNotificationCenter defaultCenter] removeObserver: view];
[renderContext clearDrawable];
[renderContext setView: nil];
[view setOpenGLContext: nil];
renderContext = nil;
}
static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version,
const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling)
{
ignoreUnused (version);
int numAttribs = 0;
#if JUCE_OPENGL3
attribs [numAttribs++] = NSOpenGLPFAOpenGLProfile;
attribs [numAttribs++] = version >= openGL3_2 ? NSOpenGLProfileVersion3_2Core
: NSOpenGLProfileVersionLegacy;
#endif
attribs [numAttribs++] = NSOpenGLPFADoubleBuffer;
attribs [numAttribs++] = NSOpenGLPFAClosestPolicy;
attribs [numAttribs++] = NSOpenGLPFANoRecovery;
attribs [numAttribs++] = NSOpenGLPFAColorSize;
attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits);
attribs [numAttribs++] = NSOpenGLPFAAlphaSize;
attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits;
attribs [numAttribs++] = NSOpenGLPFADepthSize;
attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits;
attribs [numAttribs++] = NSOpenGLPFAStencilSize;
attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits;
attribs [numAttribs++] = NSOpenGLPFAAccumSize;
attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
+ pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits);
if (shouldUseMultisampling)
{
attribs [numAttribs++] = NSOpenGLPFAMultisample;
attribs [numAttribs++] = NSOpenGLPFASampleBuffers;
attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) 1;
attribs [numAttribs++] = NSOpenGLPFASamples;
attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel;
}
}
bool initialiseOnRenderThread (OpenGLContext&) { return true; }
void shutdownOnRenderThread() { deactivateCurrentContext(); }
bool createdOk() const noexcept { return getRawContext() != nullptr; }
void* getRawContext() const noexcept { return static_cast<void*> (renderContext); }
GLuint getFrameBufferID() const noexcept { return 0; }
bool makeActive() const noexcept
{
jassert (renderContext != nil);
if ([renderContext view] != view)
[renderContext setView: view];
if (NSOpenGLContext* context = [view openGLContext])
{
[context makeCurrentContext];
return true;
}
return false;
}
bool isActive() const noexcept
{
return [NSOpenGLContext currentContext] == renderContext;
}
static void deactivateCurrentContext()
{
[NSOpenGLContext clearCurrentContext];
}
struct Locker
{
Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
{
CGLLockContext (cglContext);
}
~Locker()
{
CGLUnlockContext (cglContext);
}
private:
CGLContextObj cglContext;
};
void swapBuffers()
{
double now = Time::getMillisecondCounterHiRes();
[renderContext flushBuffer];
if (minSwapTimeMs > 0)
{
// When our window is entirely occluded by other windows, flushBuffer
// fails to wait for the swap interval, so the render loop spins at full
// speed, burning CPU. This hack detects when things are going too fast
// and sleeps if necessary.
const double swapTime = Time::getMillisecondCounterHiRes() - now;
const int frameTime = (int) (now - lastSwapTime);
if (swapTime < 0.5 && frameTime < minSwapTimeMs - 3)
{
if (underrunCounter > 3)
{
Thread::sleep (2 * (minSwapTimeMs - frameTime));
now = Time::getMillisecondCounterHiRes();
}
else
++underrunCounter;
}
else
{
if (underrunCounter > 0)
--underrunCounter;
}
}
lastSwapTime = now;
}
void updateWindowPosition (Rectangle<int>) {}
bool setSwapInterval (int numFramesPerSwap)
{
minSwapTimeMs = (numFramesPerSwap * 1000) / 60;
[renderContext setValues: (const GLint*) &numFramesPerSwap
forParameter: NSOpenGLCPSwapInterval];
return true;
}
int getSwapInterval() const
{
GLint numFrames = 0;
[renderContext getValues: &numFrames
forParameter: NSOpenGLCPSwapInterval];
return numFrames;
}
NSOpenGLContext* renderContext;
NSOpenGLView* view;
ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
double lastSwapTime;
int minSwapTimeMs, underrunCounter;
//==============================================================================
struct MouseForwardingNSOpenGLViewClass : public ObjCClass<NSOpenGLView>
{
MouseForwardingNSOpenGLViewClass() : ObjCClass<NSOpenGLView> ("JUCEGLView_")
{
addMethod (@selector (rightMouseDown:), rightMouseDown, "v@:@");
addMethod (@selector (rightMouseUp:), rightMouseUp, "v@:@");
addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse, "v@:@");
registerClass();
}
private:
static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }
static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
};
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
};
//==============================================================================
bool OpenGLHelpers::isContextActive()
{
return CGLGetCurrentContext() != 0;
}
//==============================================================================
void componentPeerAboutToChange (Component& comp, bool shouldSuspend)
{
if (auto* context = OpenGLContext::getContextAttachedTo (comp))
context->overrideCanBeAttached (shouldSuspend);
for (auto* child : comp.getChildren())
componentPeerAboutToChange (*child, shouldSuspend);
}
} // namespace juce

View File

@ -0,0 +1,278 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component&, void* parent);
//==============================================================================
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& component,
const OpenGLPixelFormat& pixelFormat,
void* contextToShareWith,
bool /*useMultisampling*/,
OpenGLVersion)
{
dummyComponent.reset (new DummyComponent (*this));
createNativeWindow (component);
PIXELFORMATDESCRIPTOR pfd;
initialisePixelFormatDescriptor (pfd, pixelFormat);
auto pixFormat = ChoosePixelFormat (dc, &pfd);
if (pixFormat != 0)
SetPixelFormat (dc, pixFormat, &pfd);
renderContext = wglCreateContext (dc);
if (renderContext != 0)
{
makeActive();
initialiseGLExtensions();
auto wglFormat = wglChoosePixelFormatExtension (pixelFormat);
deactivateCurrentContext();
if (wglFormat != pixFormat && wglFormat != 0)
{
// can't change the pixel format of a window, so need to delete the
// old one and create a new one..
releaseDC();
nativeWindow = nullptr;
createNativeWindow (component);
if (SetPixelFormat (dc, wglFormat, &pfd))
{
deleteRenderContext();
renderContext = wglCreateContext (dc);
}
}
if (contextToShareWith != nullptr)
wglShareLists ((HGLRC) contextToShareWith, renderContext);
component.getTopLevelComponent()->repaint();
component.repaint();
}
}
~NativeContext()
{
deleteRenderContext();
releaseDC();
}
bool initialiseOnRenderThread (OpenGLContext& c)
{
context = &c;
return true;
}
void shutdownOnRenderThread() { deactivateCurrentContext(); context = nullptr; }
static void deactivateCurrentContext() { wglMakeCurrent (0, 0); }
bool makeActive() const noexcept { return isActive() || wglMakeCurrent (dc, renderContext) != FALSE; }
bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; }
void swapBuffers() const noexcept { SwapBuffers (dc); }
bool setSwapInterval (int numFramesPerSwap)
{
jassert (isActive()); // this can only be called when the context is active..
return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
}
int getSwapInterval() const
{
jassert (isActive()); // this can only be called when the context is active..
return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
}
void updateWindowPosition (Rectangle<int> bounds)
{
if (nativeWindow != nullptr)
SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
}
bool createdOk() const noexcept { return getRawContext() != nullptr; }
void* getRawContext() const noexcept { return renderContext; }
unsigned int getFrameBufferID() const noexcept { return 0; }
void triggerRepaint()
{
if (context != nullptr)
context->triggerRepaint();
}
struct Locker { Locker (NativeContext&) {} };
private:
struct DummyComponent : public Component
{
DummyComponent (NativeContext& c) : context (c) {}
// The windowing code will call this when a paint callback happens
void handleCommandMessage (int) override { context.triggerRepaint(); }
NativeContext& context;
};
std::unique_ptr<DummyComponent> dummyComponent;
std::unique_ptr<ComponentPeer> nativeWindow;
HGLRC renderContext;
HDC dc;
OpenGLContext* context = {};
#define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
#undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
void initialiseGLExtensions()
{
#define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
#undef JUCE_INIT_WGL_FUNCTION
}
void createNativeWindow (Component& component)
{
auto* topComp = component.getTopLevelComponent();
nativeWindow.reset (createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, topComp->getWindowHandle()));
if (auto* peer = topComp->getPeer())
updateWindowPosition (peer->getAreaCoveredBy (component));
nativeWindow->setVisible (true);
dc = GetDC ((HWND) nativeWindow->getNativeHandle());
}
void deleteRenderContext()
{
if (renderContext != 0)
{
wglDeleteContext (renderContext);
renderContext = 0;
}
}
void releaseDC()
{
ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
}
static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
{
zerostruct (pfd);
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
pfd.cRedBits = (BYTE) pixelFormat.redBits;
pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
+ pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
}
int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
{
int format = 0;
if (wglChoosePixelFormatARB != nullptr)
{
int atts[64];
int n = 0;
atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
atts[n++] = WGL_ACCELERATION_ARB;
atts[n++] = WGL_FULL_ACCELERATION_ARB;
atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
if (pixelFormat.multisamplingLevel > 0
&& OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
{
atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
atts[n++] = 1;
atts[n++] = WGL_SAMPLES_ARB;
atts[n++] = pixelFormat.multisamplingLevel;
}
atts[n++] = 0;
jassert (n <= numElementsInArray (atts));
UINT formatsCount = 0;
wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
}
return format;
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
};
//==============================================================================
bool OpenGLHelpers::isContextActive()
{
return wglGetCurrentContext() != 0;
}
} // namespace juce