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.

182 lines
6.1KB

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