The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  22. METHOD (getParent, "getParent", "()Landroid/view/ViewParent;") \
  23. METHOD (layout, "layout", "(IIII)V" ) \
  24. METHOD (getNativeSurface, "getNativeSurface", "()Landroid/view/Surface;") \
  25. DECLARE_JNI_CLASS (NativeSurfaceView, JUCE_ANDROID_ACTIVITY_CLASSPATH "$NativeSurfaceView")
  26. #undef JNI_CLASS_MEMBERS
  27. //==============================================================================
  28. class OpenGLContext::NativeContext
  29. {
  30. public:
  31. NativeContext (Component& comp,
  32. const OpenGLPixelFormat& /*pixelFormat*/,
  33. void* /*contextToShareWith*/,
  34. bool /*useMultisampling*/,
  35. OpenGLVersion)
  36. : component (comp),
  37. hasInitialised (false),
  38. juceContext (nullptr), surface (EGL_NO_SURFACE), context (EGL_NO_CONTEXT)
  39. {
  40. JNIEnv* env = getEnv();
  41. // Do we have a native peer that we can attach to?
  42. if (component.getPeer()->getNativeHandle() == nullptr)
  43. return;
  44. // Initialise the EGL display
  45. if (! initEGLDisplay())
  46. return;
  47. // create a native surface view
  48. surfaceView = GlobalRef (env->CallObjectMethod (android.activity.get(),
  49. JuceAppActivity.createNativeSurfaceView,
  50. reinterpret_cast<jlong> (this)));
  51. if (surfaceView.get() == nullptr)
  52. return;
  53. // add the view to the view hierarchy
  54. // after this the nativecontext can receive callbacks
  55. env->CallVoidMethod ((jobject) component.getPeer()->getNativeHandle(),
  56. AndroidViewGroup.addView, surfaceView.get());
  57. // initialise the geometry of the view
  58. Rectangle<int> bounds = component.getTopLevelComponent()
  59. ->getLocalArea (&component, component.getLocalBounds());
  60. bounds *= component.getDesktopScaleFactor();
  61. updateWindowPosition (bounds);
  62. hasInitialised = true;
  63. }
  64. ~NativeContext()
  65. {
  66. JNIEnv* env = getEnv();
  67. if (jobject viewParent = env->CallObjectMethod (surfaceView.get(), NativeSurfaceView.getParent))
  68. env->CallVoidMethod (viewParent, AndroidViewGroup.removeView, surfaceView.get());
  69. }
  70. //==============================================================================
  71. void initialiseOnRenderThread (OpenGLContext& aContext)
  72. {
  73. jassert (hasInitialised);
  74. // has the context already attached?
  75. jassert (surface == EGL_NO_SURFACE && context == EGL_NO_CONTEXT);
  76. JNIEnv* env = getEnv();
  77. // get a pointer to the native window
  78. ANativeWindow* window = nullptr;
  79. if (jobject jSurface = env->CallObjectMethod (surfaceView.get(), NativeSurfaceView.getNativeSurface))
  80. window = ANativeWindow_fromSurface (env, jSurface);
  81. jassert (window != nullptr);
  82. // create the surface
  83. surface = eglCreateWindowSurface(display, config, window, 0);
  84. jassert (surface != EGL_NO_SURFACE);
  85. ANativeWindow_release (window);
  86. // create the OpenGL context
  87. EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
  88. context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
  89. jassert (context != EGL_NO_CONTEXT);
  90. juceContext = &aContext;
  91. }
  92. void shutdownOnRenderThread()
  93. {
  94. jassert (hasInitialised);
  95. // is there a context available to detach?
  96. jassert (surface != EGL_NO_SURFACE && context != EGL_NO_CONTEXT);
  97. eglDestroyContext (display, context);
  98. context = EGL_NO_CONTEXT;
  99. eglDestroySurface (display, surface);
  100. surface = EGL_NO_SURFACE;
  101. }
  102. //==============================================================================
  103. bool makeActive() const noexcept
  104. {
  105. if (! hasInitialised)
  106. return false;
  107. if (surface == EGL_NO_SURFACE || context == EGL_NO_CONTEXT)
  108. return false;
  109. if (! eglMakeCurrent (display, surface, surface, context))
  110. return false;
  111. return true;
  112. }
  113. bool isActive() const noexcept { return eglGetCurrentContext() == context; }
  114. static void deactivateCurrentContext()
  115. {
  116. eglMakeCurrent (display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  117. }
  118. //==============================================================================
  119. void swapBuffers() const noexcept { eglSwapBuffers (display, surface); }
  120. bool setSwapInterval (const int) { return false; }
  121. int getSwapInterval() const { return 0; }
  122. //==============================================================================
  123. bool createdOk() const noexcept { return hasInitialised; }
  124. void* getRawContext() const noexcept { return surfaceView.get(); }
  125. GLuint getFrameBufferID() const noexcept { return 0; }
  126. //==============================================================================
  127. void updateWindowPosition (Rectangle<int> bounds)
  128. {
  129. if (lastBounds != bounds)
  130. {
  131. JNIEnv* env = getEnv();
  132. lastBounds = bounds;
  133. Rectangle<int> r = bounds * Desktop::getInstance().getDisplays().getMainDisplay().scale;
  134. env->CallVoidMethod (surfaceView.get(), NativeSurfaceView.layout,
  135. (jint) r.getX(), (jint) r.getY(), (jint) r.getRight(), (jint) r.getBottom());
  136. }
  137. }
  138. //==============================================================================
  139. // Android Surface Callbacks:
  140. void dispatchDraw (jobject canvas)
  141. {
  142. ignoreUnused (canvas);
  143. if (juceContext != nullptr)
  144. juceContext->triggerRepaint();
  145. }
  146. void surfaceChanged (jobject holder, int format, int width, int height)
  147. {
  148. ignoreUnused (holder, format, width, height);
  149. }
  150. void surfaceCreated (jobject holder);
  151. void surfaceDestroyed (jobject holder);
  152. //==============================================================================
  153. struct Locker { Locker (NativeContext&) {} };
  154. Component& component;
  155. private:
  156. //==============================================================================
  157. bool initEGLDisplay()
  158. {
  159. // already initialised?
  160. if (display != EGL_NO_DISPLAY)
  161. return true;
  162. const EGLint attribs[] =
  163. {
  164. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  165. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  166. EGL_BLUE_SIZE, 8,
  167. EGL_GREEN_SIZE, 8,
  168. EGL_RED_SIZE, 8,
  169. EGL_ALPHA_SIZE, 0,
  170. EGL_DEPTH_SIZE, 16,
  171. EGL_NONE
  172. };
  173. EGLint numConfigs;
  174. if ((display = eglGetDisplay (EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
  175. {
  176. jassertfalse;
  177. return false;
  178. }
  179. if (! eglInitialize (display, 0, 0))
  180. {
  181. jassertfalse;
  182. return false;
  183. }
  184. if (! eglChooseConfig (display, attribs, &config, 1, &numConfigs))
  185. {
  186. eglTerminate (display);
  187. jassertfalse;
  188. return false;
  189. }
  190. return true;
  191. }
  192. //==============================================================================
  193. bool hasInitialised;
  194. GlobalRef surfaceView;
  195. Rectangle<int> lastBounds;
  196. OpenGLContext* juceContext;
  197. EGLSurface surface;
  198. EGLContext context;
  199. static EGLDisplay display;
  200. static EGLConfig config;
  201. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  202. };
  203. //==============================================================================
  204. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), dispatchDrawNative,
  205. void, (JNIEnv* env, jobject nativeView, jlong host, jobject canvas))
  206. {
  207. ignoreUnused (nativeView);
  208. setEnv (env);
  209. reinterpret_cast<OpenGLContext::NativeContext*> (host)->dispatchDraw (canvas);
  210. }
  211. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceChangedNative,
  212. void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder, jint format, jint width, jint height))
  213. {
  214. ignoreUnused (nativeView);
  215. setEnv (env);
  216. reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceChanged (holder, format, width, height);
  217. }
  218. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceCreatedNative,
  219. void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder))
  220. {
  221. ignoreUnused (nativeView);
  222. setEnv (env);
  223. reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceCreated (holder);
  224. }
  225. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceDestroyedNative,
  226. void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder))
  227. {
  228. ignoreUnused (nativeView);
  229. setEnv (env);
  230. reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceDestroyed (holder);
  231. }
  232. //==============================================================================
  233. bool OpenGLHelpers::isContextActive()
  234. {
  235. return eglGetCurrentContext() != EGL_NO_CONTEXT;
  236. }
  237. } // namespace juce