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.

191 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. Rectangle<int> r = bounds * Desktop::getInstance().getDisplays().getMainDisplay().scale;
  72. glView.callVoidMethod (OpenGLView.layout,
  73. r.getX(), r.getY(),
  74. r.getRight(), r.getBottom());
  75. }
  76. }
  77. void triggerRepaint()
  78. {
  79. glView.callVoidMethod (OpenGLView.requestRender);
  80. }
  81. //==============================================================================
  82. void contextCreatedCallback();
  83. void contextChangedSize() {}
  84. void renderCallback();
  85. //==============================================================================
  86. static NativeContext* findContextFor (JNIEnv* env, jobject glView)
  87. {
  88. const ScopedLock sl (contextListLock);
  89. for (int i = contextList.size(); --i >= 0;)
  90. {
  91. NativeContext* const c = contextList.getUnchecked(i);
  92. if (env->IsSameObject (c->glView.get(), glView))
  93. return c;
  94. }
  95. return nullptr;
  96. }
  97. static NativeContext* getActiveContext() noexcept
  98. {
  99. const ScopedLock sl (contextListLock);
  100. for (int i = contextList.size(); --i >= 0;)
  101. {
  102. NativeContext* const c = contextList.getUnchecked(i);
  103. if (c->isInsideGLCallback)
  104. return c;
  105. }
  106. return nullptr;
  107. }
  108. struct Locker { Locker (NativeContext&) {} };
  109. Component& component;
  110. private:
  111. GlobalRef glView;
  112. Rectangle<int> lastBounds;
  113. bool isInsideGLCallback;
  114. typedef Array<NativeContext*> ContextArray;
  115. static CriticalSection contextListLock;
  116. static ContextArray contextList;
  117. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  118. };
  119. CriticalSection OpenGLContext::NativeContext::contextListLock;
  120. OpenGLContext::NativeContext::ContextArray OpenGLContext::NativeContext::contextList;
  121. //==============================================================================
  122. bool OpenGLHelpers::isContextActive()
  123. {
  124. return OpenGLContext::NativeContext::getActiveContext() != nullptr;
  125. }
  126. //==============================================================================
  127. #define GL_VIEW_CLASS_NAME JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView)
  128. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextCreated, void, (JNIEnv* env, jobject view))
  129. {
  130. threadLocalJNIEnvHolder.removeCurrentThreadFromCache();
  131. threadLocalJNIEnvHolder.getOrAttach();
  132. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  133. context->contextCreatedCallback();
  134. else
  135. jassertfalse;
  136. }
  137. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextChangedSize, void, (JNIEnv* env, jobject view))
  138. {
  139. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  140. context->contextChangedSize();
  141. }
  142. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, render, void, (JNIEnv* env, jobject view))
  143. {
  144. if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
  145. context->renderCallback();
  146. }