

artic
@ 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
#include "ViewDrawNotifier.h" #include <vector> #include <android/native_window.h> // requires ndk r5 or newer #include <strings.h> #include <android/log.h> #include <unthread.h> #include <unmutex.h> #define LOG_TAG "Android::ViewDrawNotifier" #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) #define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) namespace Android { struct ViewDrawNotifier:public IViewDrawNotifier { struct CMessage { int Code; void* pArg; int Arg; }; std::vector<CMessage> mMessageQueue; enum RenderThreadMessage { MSG_NONE = 0, MSG_WINDOW_SET, MSG_RENDER_LOOP_EXIT }; un::thread mThread; un::mutex mMutex; IViewDrawDelegate* mpDelegate; // android window, supported by NDK r5 and newer ANativeWindow* mWindow; volatile bool mPause; ViewDrawNotifier() { LOG_INFO("Renderer instance created"); mpDelegate=0x0; mWindow=0x0; mPause=false; } ~ViewDrawNotifier() { LOG_INFO("Renderer instance destroyed"); } void Start(IViewDrawDelegate* ipDelegate) { mpDelegate=ipDelegate; LOG_INFO("Creating renderer thread"); un::thread thread(threadStartCallback, (void*)this); mThread.swap(thread); //pthread_create(&_threadId, 0, threadStartCallback, this); } void Stop() { LOG_INFO("Stopping renderer thread"); // send message to render thread to stop rendering CMessage message; message.Code=MSG_RENDER_LOOP_EXIT; mMutex.lock(); mMessageQueue.push_back(message); mMutex.unlock(); mThread.join(); //pthread_join(_threadId, 0); LOG_INFO("Renderer thread stopped"); return; } void Pause() { } void Resume() { } void SetView(void *ipView) { CMessage message; message.Code=MSG_WINDOW_SET; message.pArg=ipView; mMutex.lock(); mMessageQueue.push_back(message); mMutex.unlock(); return; } void renderLoop() { bool renderingEnabled = true; LOG_INFO("renderLoop()"); while (renderingEnabled) { if(mPause) { } mMutex.lock(); size_t n=mMessageQueue.size(); for(size_t a=0;a<n;a++) { CMessage& message=mMessageQueue[a]; switch (message.Code) { case MSG_WINDOW_SET: { LOG_INFO("MSG_WINDOW_SET"); ANativeWindow* window=reinterpret_cast<ANativeWindow*>(message.pArg); mWindow=window; if(!mpDelegate->OnSetView(window)) mpDelegate->OnDestroy(); } break; case MSG_RENDER_LOOP_EXIT: { LOG_INFO("MSG_RENDER_LOOP_EXIT"); renderingEnabled = false; mpDelegate->OnDestroy(); } break; default: break; } } mMessageQueue.resize(0); mMutex.unlock(); if (renderingEnabled) { mpDelegate->OnDrawFrame(); } } LOG_INFO("Render loop exits"); return; } static void* threadStartCallback(void *myself) { ViewDrawNotifier *renderer = (ViewDrawNotifier*)myself; renderer->renderLoop(); return 0; } }; } IViewDrawNotifier* IViewDrawNotifier::New() { return new Android::ViewDrawNotifier(); } |
Commits for artic/ARTIC/jni/ViewDrawNotifier_Android.cpp
Revision | Author | Commited | Message |
---|---|---|---|
3 |
![]() |
Tue 01 Mar, 2016 17:50:10 +0000 | Subiendo Proyecto Artic |