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.

205 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  19. METHOD (layout, "layout", "(IIII)V") \
  20. METHOD (requestRender, "requestRender", "()V") \
  21. DECLARE_JNI_CLASS (OpenGLView, JUCE_ANDROID_ACTIVITY_CLASSPATH "$OpenGLView");
  22. #undef JNI_CLASS_MEMBERS
  23. extern jobject createOpenGLView (ComponentPeer*);
  24. //==============================================================================
  25. class OpenGLContext::NativeContext
  26. {
  27. public:
  28. NativeContext (Component& component_,
  29. const OpenGLPixelFormat& pixelFormat,
  30. const NativeContext* contextToShareWith)
  31. : component (component_),
  32. isInsideGLCallback (false)
  33. {
  34. glView = GlobalRef (createOpenGLView (component.getPeer()));
  35. {
  36. const ScopedLock sl (getContextListLock());
  37. getContextList().add (this);
  38. }
  39. updateWindowPosition (component.getTopLevelComponent()
  40. ->getLocalArea (&component, component.getLocalBounds()));
  41. }
  42. ~NativeContext()
  43. {
  44. {
  45. const ScopedLock sl (getContextListLock());
  46. getContextList().removeValue (this);
  47. }
  48. android.activity.callVoidMethod (JuceAppActivity.deleteView, glView.get());
  49. glView.clear();
  50. }
  51. void initialiseOnRenderThread()
  52. {
  53. }
  54. void shutdownOnRenderThread()
  55. {
  56. }
  57. bool makeActive() const noexcept { return isInsideGLCallback; }
  58. bool makeInactive() const noexcept { return true; }
  59. bool isActive() const noexcept { return isInsideGLCallback; }
  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 renderCallback();
  83. //==============================================================================
  84. static NativeContext* findContextFor (JNIEnv* env, jobject glView)
  85. {
  86. const ScopedLock sl (getContextListLock());
  87. const ContextArray& contexts = getContextList();
  88. for (int i = contexts.size(); --i >= 0;)
  89. {
  90. NativeContext* const c = contexts.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 (getContextListLock());
  99. const ContextArray& contexts = getContextList();
  100. for (int i = contexts.size(); --i >= 0;)
  101. {
  102. NativeContext* const c = contexts.getUnchecked(i);
  103. if (c->isInsideGLCallback)
  104. return c;
  105. }
  106. return nullptr;
  107. }
  108. Component& component;
  109. private:
  110. GlobalRef glView;
  111. Rectangle<int> lastBounds;
  112. bool isInsideGLCallback;
  113. static CriticalSection& getContextListLock()
  114. {
  115. static CriticalSection lock;
  116. return lock;
  117. }
  118. typedef Array<NativeContext*> ContextArray;
  119. static ContextArray& getContextList()
  120. {
  121. static ContextArray contexts;
  122. return contexts;
  123. }
  124. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext);
  125. };
  126. //==============================================================================
  127. bool OpenGLHelpers::isContextActive()
  128. {
  129. return OpenGLContext::NativeContext::getActiveContext() != nullptr;
  130. }
  131. //==============================================================================
  132. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView), contextCreated, void, (JNIEnv* env, jobject view))
  133. {
  134. threadLocalJNIEnvHolder.getOrAttach();
  135. JUCE_CHECK_OPENGL_ERROR
  136. for (int i = 100; --i >= 0;)
  137. {
  138. OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view);
  139. if (context != nullptr)
  140. {
  141. context->contextCreatedCallback();
  142. JUCE_CHECK_OPENGL_ERROR
  143. return;
  144. }
  145. Thread::sleep (20);
  146. }
  147. jassertfalse;
  148. }
  149. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView), contextChangedSize, void, (JNIEnv* env, jobject view))
  150. {
  151. }
  152. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView), render, void, (JNIEnv* env, jobject view))
  153. {
  154. OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view);
  155. if (context != nullptr)
  156. context->renderCallback();
  157. }