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.

188 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  18. METHOD (layout, "layout", "(IIII)V") \
  19. METHOD (requestRender, "requestRender", "()V") \
  20. DECLARE_JNI_CLASS (OpenGLView, JUCE_ANDROID_ACTIVITY_CLASSPATH "$OpenGLView");
  21. #undef JNI_CLASS_MEMBERS
  22. extern jobject createOpenGLView (ComponentPeer*);
  23. //==============================================================================
  24. class OpenGLContext::NativeContext
  25. {
  26. public:
  27. NativeContext (Component& comp,
  28. const OpenGLPixelFormat& pixelFormat,
  29. void* /*contextToShareWith*/,
  30. bool /*useMultisampling*/,
  31. OpenGLVersion)
  32. : component (comp),
  33. isInsideGLCallback (false)
  34. {
  35. {
  36. const ScopedLock sl (contextListLock);
  37. glView = GlobalRef (createOpenGLView (component.getPeer()));
  38. contextList.add (this);
  39. }
  40. Rectangle<int> bounds = component.getTopLevelComponent()
  41. ->getLocalArea (&component, component.getLocalBounds());
  42. bounds *= component.getDesktopScaleFactor();
  43. updateWindowPosition (component.getTopLevelComponent()
  44. ->getLocalArea (&component, component.getLocalBounds()));
  45. }
  46. ~NativeContext()
  47. {
  48. {
  49. const ScopedLock sl (contextListLock);
  50. contextList.removeFirstMatchingValue (this);
  51. }
  52. android.activity.callVoidMethod (JuceAppActivity.deleteOpenGLView, glView.get());
  53. glView.clear();
  54. }
  55. void initialiseOnRenderThread (OpenGLContext&) {}
  56. void shutdownOnRenderThread() {}
  57. bool makeActive() const noexcept { return isInsideGLCallback; }
  58. bool isActive() const noexcept { return isInsideGLCallback; }
  59. static void deactivateCurrentContext() {}
  60. void swapBuffers() const noexcept {}
  61. bool setSwapInterval (const int) { return false; }
  62. int getSwapInterval() const { return 0; }
  63. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  64. void* getRawContext() const noexcept { return glView.get(); }
  65. GLuint getFrameBufferID() const noexcept { return 0; }
  66. void updateWindowPosition (const Rectangle<int>& bounds)
  67. {
  68. if (lastBounds != bounds)
  69. {
  70. lastBounds = bounds;
  71. glView.callVoidMethod (OpenGLView.layout,
  72. bounds.getX(), bounds.getY(),
  73. bounds.getRight(), bounds.getBottom());
  74. }
  75. }
  76. void triggerRepaint()
  77. {
  78. glView.callVoidMethod (OpenGLView.requestRender);
  79. }
  80. //==============================================================================
  81. void contextCreatedCallback();
  82. void contextChangedSize() {}
  83. void renderCallback();
  84. //==============================================================================
  85. static NativeContext* findContextFor (JNIEnv* env, jobject glView)
  86. {
  87. const ScopedLock sl (contextListLock);
  88. for (int i = contextList.size(); --i >= 0;)
  89. {
  90. NativeContext* const c = contextList.getUnchecked(i);
  91. if (env->IsSameObject (c->glView.get(), glView))
  92. return c;
  93. }
  94. return nullptr;
  95. }
  96. static NativeContext* getActiveContext() noexcept
  97. {
  98. const ScopedLock sl (contextListLock);
  99. for (int i = contextList.size(); --i >= 0;)
  100. {
  101. NativeContext* const c = contextList.getUnchecked(i);
  102. if (c->isInsideGLCallback)
  103. return c;
  104. }
  105. return nullptr;
  106. }
  107. struct Locker { Locker (NativeContext&) {} };
  108. Component& component;
  109. private:
  110. GlobalRef glView;
  111. Rectangle<int> lastBounds;
  112. bool isInsideGLCallback;
  113. typedef Array<NativeContext*> ContextArray;
  114. static CriticalSection contextListLock;
  115. static ContextArray contextList;
  116. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  117. };
  118. CriticalSection OpenGLContext::NativeContext::contextListLock;
  119. OpenGLContext::NativeContext::ContextArray OpenGLContext::NativeContext::contextList;
  120. //==============================================================================
  121. bool OpenGLHelpers::isContextActive()
  122. {
  123. return OpenGLContext::NativeContext::getActiveContext() != nullptr;
  124. }
  125. //==============================================================================
  126. #define GL_VIEW_CLASS_NAME JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView)
  127. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextCreated, void, (JNIEnv* env, jobject view))
  128. {
  129. threadLocalJNIEnvHolder.removeCurrentThreadFromCache();
  130. threadLocalJNIEnvHolder.getOrAttach();
  131. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  132. context->contextCreatedCallback();
  133. else
  134. jassertfalse;
  135. }
  136. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextChangedSize, void, (JNIEnv* env, jobject view))
  137. {
  138. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  139. context->contextChangedSize();
  140. }
  141. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, render, void, (JNIEnv* env, jobject view))
  142. {
  143. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  144. context->renderCallback();
  145. }