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.

183 lines
6.2KB

  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. updateWindowPosition (component.getTopLevelComponent()
  41. ->getLocalArea (&component, component.getLocalBounds()));
  42. }
  43. ~NativeContext()
  44. {
  45. {
  46. const ScopedLock sl (contextListLock);
  47. contextList.removeFirstMatchingValue (this);
  48. }
  49. android.activity.callVoidMethod (JuceAppActivity.deleteView, glView.get());
  50. glView.clear();
  51. }
  52. void initialiseOnRenderThread (OpenGLContext&) {}
  53. void shutdownOnRenderThread() {}
  54. bool makeActive() const noexcept { return isInsideGLCallback; }
  55. bool isActive() const noexcept { return isInsideGLCallback; }
  56. static void deactivateCurrentContext() {}
  57. void swapBuffers() const noexcept {}
  58. bool setSwapInterval (const int) { return false; }
  59. int getSwapInterval() const { return 0; }
  60. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  61. void* getRawContext() const noexcept { return glView.get(); }
  62. GLuint getFrameBufferID() const noexcept { return 0; }
  63. void updateWindowPosition (const Rectangle<int>& bounds)
  64. {
  65. if (lastBounds != bounds)
  66. {
  67. lastBounds = bounds;
  68. glView.callVoidMethod (OpenGLView.layout,
  69. bounds.getX(), bounds.getY(),
  70. bounds.getRight(), bounds.getBottom());
  71. }
  72. }
  73. void triggerRepaint()
  74. {
  75. glView.callVoidMethod (OpenGLView.requestRender);
  76. }
  77. //==============================================================================
  78. void contextCreatedCallback();
  79. void contextChangedSize() {}
  80. void renderCallback();
  81. //==============================================================================
  82. static NativeContext* findContextFor (JNIEnv* env, jobject glView)
  83. {
  84. const ScopedLock sl (contextListLock);
  85. for (int i = contextList.size(); --i >= 0;)
  86. {
  87. NativeContext* const c = contextList.getUnchecked(i);
  88. if (env->IsSameObject (c->glView.get(), glView))
  89. return c;
  90. }
  91. return nullptr;
  92. }
  93. static NativeContext* getActiveContext() noexcept
  94. {
  95. const ScopedLock sl (contextListLock);
  96. for (int i = contextList.size(); --i >= 0;)
  97. {
  98. NativeContext* const c = contextList.getUnchecked(i);
  99. if (c->isInsideGLCallback)
  100. return c;
  101. }
  102. return nullptr;
  103. }
  104. struct Locker { Locker (NativeContext&) {} };
  105. Component& component;
  106. private:
  107. GlobalRef glView;
  108. Rectangle<int> lastBounds;
  109. bool isInsideGLCallback;
  110. typedef Array<NativeContext*> ContextArray;
  111. static CriticalSection contextListLock;
  112. static ContextArray contextList;
  113. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  114. };
  115. CriticalSection OpenGLContext::NativeContext::contextListLock;
  116. OpenGLContext::NativeContext::ContextArray OpenGLContext::NativeContext::contextList;
  117. //==============================================================================
  118. bool OpenGLHelpers::isContextActive()
  119. {
  120. return OpenGLContext::NativeContext::getActiveContext() != nullptr;
  121. }
  122. //==============================================================================
  123. #define GL_VIEW_CLASS_NAME JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView)
  124. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextCreated, void, (JNIEnv* env, jobject view))
  125. {
  126. threadLocalJNIEnvHolder.getOrAttach();
  127. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  128. context->contextCreatedCallback();
  129. else
  130. jassertfalse;
  131. }
  132. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextChangedSize, void, (JNIEnv* env, jobject view))
  133. {
  134. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  135. context->contextChangedSize();
  136. }
  137. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, render, void, (JNIEnv* env, jobject view))
  138. {
  139. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  140. context->renderCallback();
  141. }