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.

324 lines
11KB

  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. bool 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. {
  81. window = ANativeWindow_fromSurface(env, jSurface);
  82. // if we didn't succeed the first time, wait 25ms and try again
  83. if (window == nullptr)
  84. {
  85. Thread::sleep (25);
  86. window = ANativeWindow_fromSurface (env, jSurface);
  87. }
  88. }
  89. if (window == nullptr)
  90. {
  91. // failed to get a pointer to the native window after second try so
  92. // bail out
  93. jassertfalse;
  94. return false;
  95. }
  96. // create the surface
  97. surface = eglCreateWindowSurface(display, config, window, 0);
  98. jassert (surface != EGL_NO_SURFACE);
  99. ANativeWindow_release (window);
  100. // create the OpenGL context
  101. EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
  102. context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
  103. jassert (context != EGL_NO_CONTEXT);
  104. juceContext = &aContext;
  105. return true;
  106. }
  107. void shutdownOnRenderThread()
  108. {
  109. jassert (hasInitialised);
  110. // is there a context available to detach?
  111. jassert (surface != EGL_NO_SURFACE && context != EGL_NO_CONTEXT);
  112. eglDestroyContext (display, context);
  113. context = EGL_NO_CONTEXT;
  114. eglDestroySurface (display, surface);
  115. surface = EGL_NO_SURFACE;
  116. }
  117. //==============================================================================
  118. bool makeActive() const noexcept
  119. {
  120. if (! hasInitialised)
  121. return false;
  122. if (surface == EGL_NO_SURFACE || context == EGL_NO_CONTEXT)
  123. return false;
  124. if (! eglMakeCurrent (display, surface, surface, context))
  125. return false;
  126. return true;
  127. }
  128. bool isActive() const noexcept { return eglGetCurrentContext() == context; }
  129. static void deactivateCurrentContext()
  130. {
  131. eglMakeCurrent (display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  132. }
  133. //==============================================================================
  134. void swapBuffers() const noexcept { eglSwapBuffers (display, surface); }
  135. bool setSwapInterval (const int) { return false; }
  136. int getSwapInterval() const { return 0; }
  137. //==============================================================================
  138. bool createdOk() const noexcept { return hasInitialised; }
  139. void* getRawContext() const noexcept { return surfaceView.get(); }
  140. GLuint getFrameBufferID() const noexcept { return 0; }
  141. //==============================================================================
  142. void updateWindowPosition (Rectangle<int> bounds)
  143. {
  144. if (lastBounds != bounds)
  145. {
  146. JNIEnv* env = getEnv();
  147. lastBounds = bounds;
  148. Rectangle<int> r = bounds * Desktop::getInstance().getDisplays().getMainDisplay().scale;
  149. env->CallVoidMethod (surfaceView.get(), NativeSurfaceView.layout,
  150. (jint) r.getX(), (jint) r.getY(), (jint) r.getRight(), (jint) r.getBottom());
  151. }
  152. }
  153. //==============================================================================
  154. // Android Surface Callbacks:
  155. void dispatchDraw (jobject canvas)
  156. {
  157. ignoreUnused (canvas);
  158. if (juceContext != nullptr)
  159. juceContext->triggerRepaint();
  160. }
  161. void surfaceChanged (jobject holder, int format, int width, int height)
  162. {
  163. ignoreUnused (holder, format, width, height);
  164. }
  165. void surfaceCreated (jobject holder);
  166. void surfaceDestroyed (jobject holder);
  167. //==============================================================================
  168. struct Locker { Locker (NativeContext&) {} };
  169. Component& component;
  170. private:
  171. //==============================================================================
  172. bool initEGLDisplay()
  173. {
  174. // already initialised?
  175. if (display != EGL_NO_DISPLAY)
  176. return true;
  177. const EGLint attribs[] =
  178. {
  179. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  180. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  181. EGL_BLUE_SIZE, 8,
  182. EGL_GREEN_SIZE, 8,
  183. EGL_RED_SIZE, 8,
  184. EGL_ALPHA_SIZE, 0,
  185. EGL_DEPTH_SIZE, 16,
  186. EGL_NONE
  187. };
  188. EGLint numConfigs;
  189. if ((display = eglGetDisplay (EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
  190. {
  191. jassertfalse;
  192. return false;
  193. }
  194. if (! eglInitialize (display, 0, 0))
  195. {
  196. jassertfalse;
  197. return false;
  198. }
  199. if (! eglChooseConfig (display, attribs, &config, 1, &numConfigs))
  200. {
  201. eglTerminate (display);
  202. jassertfalse;
  203. return false;
  204. }
  205. return true;
  206. }
  207. //==============================================================================
  208. bool hasInitialised;
  209. GlobalRef surfaceView;
  210. Rectangle<int> lastBounds;
  211. OpenGLContext* juceContext;
  212. EGLSurface surface;
  213. EGLContext context;
  214. static EGLDisplay display;
  215. static EGLConfig config;
  216. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  217. };
  218. //==============================================================================
  219. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), dispatchDrawNative,
  220. void, (JNIEnv* env, jobject nativeView, jlong host, jobject canvas))
  221. {
  222. ignoreUnused (nativeView);
  223. setEnv (env);
  224. reinterpret_cast<OpenGLContext::NativeContext*> (host)->dispatchDraw (canvas);
  225. }
  226. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceChangedNative,
  227. void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder, jint format, jint width, jint height))
  228. {
  229. ignoreUnused (nativeView);
  230. setEnv (env);
  231. reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceChanged (holder, format, width, height);
  232. }
  233. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceCreatedNative,
  234. void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder))
  235. {
  236. ignoreUnused (nativeView);
  237. setEnv (env);
  238. reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceCreated (holder);
  239. }
  240. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024NativeSurfaceView), surfaceDestroyedNative,
  241. void, (JNIEnv* env, jobject nativeView, jlong host, jobject holder))
  242. {
  243. ignoreUnused (nativeView);
  244. setEnv (env);
  245. reinterpret_cast<OpenGLContext::NativeContext*> (host)->surfaceDestroyed (holder);
  246. }
  247. //==============================================================================
  248. bool OpenGLHelpers::isContextActive()
  249. {
  250. return eglGetCurrentContext() != EGL_NO_CONTEXT;
  251. }
  252. } // namespace juce