From 53c0436d7177215f4993d480cf779d43e5af4dfb Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 29 Mar 2012 12:40:14 +0100 Subject: [PATCH] Android fixes. OSX fix for old SDKs. --- modules/juce_core/maths/juce_BigInteger.cpp | 1 + .../native/juce_android_JNIHelpers.h | 2 +- .../messages/juce_MessageManager.cpp | 5 +- .../native/juce_android_Messaging.cpp | 23 +++--- .../native/juce_android_Windowing.cpp | 23 ++---- .../native/juce_mac_NSViewComponentPeer.mm | 2 +- .../juce_opengl/native/juce_OpenGL_android.h | 80 +++++++++---------- 7 files changed, 62 insertions(+), 74 deletions(-) diff --git a/modules/juce_core/maths/juce_BigInteger.cpp b/modules/juce_core/maths/juce_BigInteger.cpp index 0a8b515af3..b5aec76585 100644 --- a/modules/juce_core/maths/juce_BigInteger.cpp +++ b/modules/juce_core/maths/juce_BigInteger.cpp @@ -118,6 +118,7 @@ BigInteger& BigInteger::operator= (const BigInteger& other) if (this != &other) { highestBit = other.getHighestBit(); + jassert (other.numValues >= 4); numValues = (size_t) jmax ((size_t) 4, bitToIndex (highestBit) + 1); negative = other.negative; values.malloc (numValues + 1); diff --git a/modules/juce_core/native/juce_android_JNIHelpers.h b/modules/juce_core/native/juce_android_JNIHelpers.h index 12b210fb71..0831534749 100644 --- a/modules/juce_core/native/juce_android_JNIHelpers.h +++ b/modules/juce_core/native/juce_android_JNIHelpers.h @@ -270,7 +270,7 @@ public: JNIEnv* attach() { JNIEnv* env = nullptr; - jvm->AttachCurrentThread (&env, 0); + jvm->AttachCurrentThread (&env, nullptr); if (env != nullptr) addEnv (env); diff --git a/modules/juce_events/messages/juce_MessageManager.cpp b/modules/juce_events/messages/juce_MessageManager.cpp index 9b50f2e7cd..2e94a20682 100644 --- a/modules/juce_events/messages/juce_MessageManager.cpp +++ b/modules/juce_events/messages/juce_MessageManager.cpp @@ -158,9 +158,12 @@ private: void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* const func, void* const parameter) { - if (MessageManager::getInstance()->isThisTheMessageThread()) + if (isThisTheMessageThread()) return func (parameter); + // If this thread has the message manager locked, then this will deadlock! + jassert (! currentThreadHasLockedMessageManager()); + const ReferenceCountedObjectPtr message (new AsyncFunctionCallback (func, parameter)); message->post(); message->finished.wait(); diff --git a/modules/juce_events/native/juce_android_Messaging.cpp b/modules/juce_events/native/juce_android_Messaging.cpp index 6a5d44b949..b370e1ed5e 100644 --- a/modules/juce_events/native/juce_android_Messaging.cpp +++ b/modules/juce_events/native/juce_android_Messaging.cpp @@ -39,7 +39,7 @@ bool MessageManager::dispatchNextMessageOnSystemQueue (const bool returnIfNoPend bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message) { message->incReferenceCount(); - getEnv()->CallVoidMethod (android.activity, JuceAppActivity.postMessage, (jlong) (pointer_sized_uint) message); + android.activity.callVoidMethod (JuceAppActivity.postMessage, (jlong) (pointer_sized_uint) message); return true; } @@ -63,19 +63,18 @@ void MessageManager::runDispatchLoop() { } -class QuitCallback : public CallbackMessage -{ -public: - QuitCallback() {} - - void messageCallback() - { - android.activity.callVoidMethod (JuceAppActivity.finish); - } -}; - void MessageManager::stopDispatchLoop() { + struct QuitCallback : public CallbackMessage + { + QuitCallback() {} + + void messageCallback() + { + android.activity.callVoidMethod (JuceAppActivity.finish); + } + }; + (new QuitCallback())->post(); quitMessagePosted = true; } diff --git a/modules/juce_gui_basics/native/juce_android_Windowing.cpp b/modules/juce_gui_basics/native/juce_android_Windowing.cpp index 24fabdfc85..3902c72799 100644 --- a/modules/juce_gui_basics/native/juce_android_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_android_Windowing.cpp @@ -111,14 +111,9 @@ public: } else { - class ViewDeleter : public CallbackMessage + struct ViewDeleter : public CallbackMessage { - public: - ViewDeleter (const GlobalRef& view_) - : view (view_) - { - post(); - } + ViewDeleter (const GlobalRef& view_) : view (view_) {} void messageCallback() { @@ -129,7 +124,7 @@ public: GlobalRef view; }; - new ViewDeleter (view); + (new ViewDeleter (view))->post(); } view.clear(); @@ -148,14 +143,11 @@ public: } else { - class VisibilityChanger : public CallbackMessage + struct VisibilityChanger : public CallbackMessage { - public: VisibilityChanger (const GlobalRef& view_, bool shouldBeVisible_) : view (view_), shouldBeVisible (shouldBeVisible_) - { - post(); - } + {} void messageCallback() { @@ -167,7 +159,7 @@ public: bool shouldBeVisible; }; - new VisibilityChanger (view, shouldBeVisible); + (new VisibilityChanger (view, shouldBeVisible))->post(); } } @@ -714,8 +706,7 @@ JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, setScreenSize, void, (JNIEnv android.screenWidth = screenWidth; android.screenHeight = screenHeight; - if (isSystemInitialised) - Desktop::getInstance().refreshMonitorSizes(); + Desktop::getInstance().refreshMonitorSizes(); } //============================================================================== diff --git a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm index 1639a7cbc5..37571cc8f9 100644 --- a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm +++ b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm @@ -906,7 +906,7 @@ void NSViewComponentPeer::updateKeysDown (NSEvent* ev, bool isKeyDown) ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept { if ([NSEvent respondsToSelector: @selector (modifierFlags)]) - NSViewComponentPeer::updateModifiers ([NSEvent modifierFlags]); + NSViewComponentPeer::updateModifiers ((NSUInteger) [NSEvent modifierFlags]); return NSViewComponentPeer::currentModifiers; } diff --git a/modules/juce_opengl/native/juce_OpenGL_android.h b/modules/juce_opengl/native/juce_OpenGL_android.h index e9739b4c4f..f87ba1bfe2 100644 --- a/modules/juce_opengl/native/juce_OpenGL_android.h +++ b/modules/juce_opengl/native/juce_OpenGL_android.h @@ -42,11 +42,10 @@ public: : component (component_), isInsideGLCallback (false) { - glView = GlobalRef (createOpenGLView (component.getPeer())); - { - const ScopedLock sl (getContextListLock()); - getContextList().add (this); + const ScopedLock sl (contextListLock); + glView = GlobalRef (createOpenGLView (component.getPeer())); + contextList.add (this); } updateWindowPosition (component.getTopLevelComponent() @@ -56,8 +55,8 @@ public: ~NativeContext() { { - const ScopedLock sl (getContextListLock()); - getContextList().removeValue (this); + const ScopedLock sl (contextListLock); + contextList.removeValue (this); } android.activity.callVoidMethod (JuceAppActivity.deleteView, glView.get()); @@ -95,6 +94,10 @@ public: } } + void contextChangedSize() + { + } + void triggerRepaint() { glView.callVoidMethod (OpenGLView.requestRender); @@ -107,12 +110,11 @@ public: //============================================================================== static NativeContext* findContextFor (JNIEnv* env, jobject glView) { - const ScopedLock sl (getContextListLock()); - const ContextArray& contexts = getContextList(); + const ScopedLock sl (contextListLock); - for (int i = contexts.size(); --i >= 0;) + for (int i = contextList.size(); --i >= 0;) { - NativeContext* const c = contexts.getUnchecked(i); + NativeContext* const c = contextList.getUnchecked(i); if (env->IsSameObject (c->glView.get(), glView)) return c; @@ -123,12 +125,11 @@ public: static NativeContext* getActiveContext() noexcept { - const ScopedLock sl (getContextListLock()); - const ContextArray& contexts = getContextList(); + const ScopedLock sl (contextListLock); - for (int i = contexts.size(); --i >= 0;) + for (int i = contextList.size(); --i >= 0;) { - NativeContext* const c = contexts.getUnchecked(i); + NativeContext* const c = contextList.getUnchecked(i); if (c->isInsideGLCallback) return c; @@ -144,23 +145,16 @@ private: Rectangle lastBounds; bool isInsideGLCallback; - static CriticalSection& getContextListLock() - { - static CriticalSection lock; - return lock; - } - typedef Array ContextArray; - - static ContextArray& getContextList() - { - static ContextArray contexts; - return contexts; - } + static CriticalSection contextListLock; + static ContextArray contextList; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext); }; +CriticalSection OpenGLContext::NativeContext::contextListLock; +OpenGLContext::NativeContext::ContextArray OpenGLContext::NativeContext::contextList; + //============================================================================== bool OpenGLHelpers::isContextActive() { @@ -168,34 +162,34 @@ bool OpenGLHelpers::isContextActive() } //============================================================================== -JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView), contextCreated, void, (JNIEnv* env, jobject view)) +#define GL_VIEW_CLASS_NAME JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView) + +JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextCreated, void, (JNIEnv* env, jobject view)) { threadLocalJNIEnvHolder.getOrAttach(); - JUCE_CHECK_OPENGL_ERROR + OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view); - for (int i = 100; --i >= 0;) + if (context != nullptr) { - OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view); - - if (context != nullptr) - { - context->contextCreatedCallback(); - JUCE_CHECK_OPENGL_ERROR - return; - } - - Thread::sleep (20); + context->contextCreatedCallback(); + JUCE_CHECK_OPENGL_ERROR + } + else + { + jassertfalse; } - - jassertfalse; } -JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView), contextChangedSize, void, (JNIEnv* env, jobject view)) +JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextChangedSize, void, (JNIEnv* env, jobject view)) { + OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view); + + if (context != nullptr) + context->contextChangedSize(); } -JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView), render, void, (JNIEnv* env, jobject view)) +JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, render, void, (JNIEnv* env, jobject view)) { OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view);