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.

198 lines
6.4KB

  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. {
  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.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 isActive() const noexcept { return isInsideGLCallback; }
  59. void swapBuffers() const noexcept {}
  60. bool setSwapInterval (const int) { return false; }
  61. int getSwapInterval() const { return 0; }
  62. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  63. void* getRawContext() const noexcept { return glView.get(); }
  64. GLuint getFrameBufferID() const noexcept { return 0; }
  65. void updateWindowPosition (const Rectangle<int>& bounds)
  66. {
  67. if (lastBounds != bounds)
  68. {
  69. lastBounds = bounds;
  70. glView.callVoidMethod (OpenGLView.layout,
  71. bounds.getX(), bounds.getY(),
  72. bounds.getRight(), bounds.getBottom());
  73. }
  74. }
  75. void contextChangedSize()
  76. {
  77. }
  78. void triggerRepaint()
  79. {
  80. glView.callVoidMethod (OpenGLView.requestRender);
  81. }
  82. //==============================================================================
  83. void contextCreatedCallback();
  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. 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.getOrAttach();
  130. OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view);
  131. if (context != nullptr)
  132. {
  133. context->contextCreatedCallback();
  134. JUCE_CHECK_OPENGL_ERROR
  135. }
  136. else
  137. {
  138. jassertfalse;
  139. }
  140. }
  141. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextChangedSize, void, (JNIEnv* env, jobject view))
  142. {
  143. OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view);
  144. if (context != nullptr)
  145. context->contextChangedSize();
  146. }
  147. JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, render, void, (JNIEnv* env, jobject view))
  148. {
  149. OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view);
  150. if (context != nullptr)
  151. context->renderCallback();
  152. }