From bd649af62dee635b4a65dac9e669e7eaec603b26 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 5 Jun 2012 18:15:56 +0000 Subject: [PATCH] nacl: NaCl binaries no longer need a modified main() to work, the NaCl instance object takes care of everything for us. --- src/Makefile.am | 6 +- src/application/application.cpp | 2 +- .../nacl/{naclapp.cpp => nacl-app.cpp} | 23 +++--- src/platform/nacl/{naclapp.h => nacl-app.h} | 0 .../{nacl_instance.cpp => nacl-instance.cpp} | 60 ++++++++------ src/platform/nacl/nacl-instance.h | 78 +++++++++++++++++++ .../nacl/{nacl_module.cpp => nacl-module.cpp} | 4 +- src/platform/nacl/nacl_instance.h | 63 --------------- src/platform/nacl/opengl_context.h | 2 +- 9 files changed, 132 insertions(+), 106 deletions(-) rename src/platform/nacl/{naclapp.cpp => nacl-app.cpp} (74%) rename src/platform/nacl/{naclapp.h => nacl-app.h} (100%) rename src/platform/nacl/{nacl_instance.cpp => nacl-instance.cpp} (74%) create mode 100644 src/platform/nacl/nacl-instance.h rename src/platform/nacl/{nacl_module.cpp => nacl-module.cpp} (95%) delete mode 100644 src/platform/nacl/nacl_instance.h diff --git a/src/Makefile.am b/src/Makefile.am index e337f900..a50612ff 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -71,9 +71,9 @@ d3d9_sources = \ if USE_NACL nacl_sources = \ - platform/nacl/naclapp.cpp platform/nacl/naclapp.h \ - platform/nacl/nacl_instance.cpp platform/nacl/nacl_instance.h \ - platform/nacl/nacl_module.cpp \ + platform/nacl/nacl-app.cpp platform/nacl/nacl-app.h \ + platform/nacl/nacl-instance.cpp platform/nacl/nacl-instance.h \ + platform/nacl/nacl-module.cpp \ platform/nacl/opengl_context.cpp platform/nacl/opengl_context.h \ platform/nacl/opengl_context_ptrs.h endif diff --git a/src/application/application.cpp b/src/application/application.cpp index b7f8951c..990920e5 100644 --- a/src/application/application.cpp +++ b/src/application/application.cpp @@ -20,7 +20,7 @@ #elif defined _XBOX # include "platform/xbox/xboxapp.h" #elif defined __native_client__ -# include "platform/nacl/naclapp.h" +# include "platform/nacl/nacl-app.h" #elif defined __ANDROID__ # include "platform/android/androidapp.h" #elif defined HAVE_GLES_2X diff --git a/src/platform/nacl/naclapp.cpp b/src/platform/nacl/nacl-app.cpp similarity index 74% rename from src/platform/nacl/naclapp.cpp rename to src/platform/nacl/nacl-app.cpp index 1d1a9e63..ad2a2ac3 100644 --- a/src/platform/nacl/naclapp.cpp +++ b/src/platform/nacl/nacl-app.cpp @@ -12,15 +12,10 @@ # include "config.h" #endif -#if defined __native_client__ -# include -# include -# include -#endif - #include "core.h" -#include "lolgl.h" -#include "naclapp.h" + +#include "nacl-app.h" +#include "nacl-instance.h" namespace lol { @@ -45,6 +40,7 @@ private: NaClApp::NaClApp(char const *title, ivec2 res, float fps) : data(new NaClAppData()) { + Ticker::Setup(fps); #if defined __native_client__ #endif } @@ -56,14 +52,13 @@ void NaClApp::ShowPointer(bool show) void NaClApp::Run() { - while (!Ticker::Finished()) - { - /* Tick the renderer, show the frame and clamp to desired framerate. */ - Ticker::TickDraw(); - #if defined __native_client__ + NaClInstance::MainSignal(); #endif - } + + /* Wait forever */ + Queue q; + q.Pop(); } NaClApp::~NaClApp() diff --git a/src/platform/nacl/naclapp.h b/src/platform/nacl/nacl-app.h similarity index 100% rename from src/platform/nacl/naclapp.h rename to src/platform/nacl/nacl-app.h diff --git a/src/platform/nacl/nacl_instance.cpp b/src/platform/nacl/nacl-instance.cpp similarity index 74% rename from src/platform/nacl/nacl_instance.cpp rename to src/platform/nacl/nacl-instance.cpp index fceb5670..45070d59 100644 --- a/src/platform/nacl/nacl_instance.cpp +++ b/src/platform/nacl/nacl-instance.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -20,9 +19,8 @@ #include #include "core.h" -#include "debug/quad.h" -#include "platform/nacl/nacl_instance.h" +#include "platform/nacl/nacl-instance.h" #include "platform/nacl/opengl_context.h" /* One of these wrappers will be overridden by the user's version */ @@ -46,7 +44,7 @@ NaClInstance::NaClInstance(PP_Instance instance) NaClInstance::~NaClInstance() { // Destroy the cube view while GL context is current. - opengl_context_->MakeContextCurrent(this); + m_opengl_ctx->MakeContextCurrent(this); } static double const DELTA_MS = 1000.0 / 60.0; @@ -84,17 +82,23 @@ void NaClInstance::TickCallback(void* data, int32_t result) } } +Mutex NaClInstance::main_mutex; +Queue NaClInstance::main_queue; + bool NaClInstance::Init(uint32_t argc, const char* /* argn */[], const char* argv[]) { - Ticker::Setup(60.0f); - - /* Call the user's main() function. FIXME: run it in a thread */ + /* Ensure only one NaClInstance does Init() at the same time. */ + main_mutex.Lock(); char *env[] = { NULL }; - lol_nacl_main(); - lol_nacl_main(argc, const_cast(argv)); - lol_nacl_main(argc, const_cast(argv), (char **)env); + Args arglist(argc, const_cast(argv), const_cast(env)); + main_queue.Push(&arglist); + m_main_thread = new Thread(MainRun, NULL); + /* Push so that only MainSignal() can unblock us */ + main_queue.Push(NULL); + main_queue.Push(NULL); + main_mutex.Unlock(); // My timer callback pp::Module::Get()->core()->CallOnMainThread( @@ -107,11 +111,23 @@ bool NaClInstance::Init(uint32_t argc, return true; } -void NaClInstance::RunMain(uint32_t argc, - const char* /* argn */[], - const char* argv[]) +void * NaClInstance::MainRun(void *data) { + Args *arglist = main_queue.Pop(); + + /* Call the user's main() function. One of these will work. */ + lol_nacl_main(); + lol_nacl_main(arglist->m_argc, arglist->m_argv); + lol_nacl_main(arglist->m_argc, arglist->m_argv, arglist->m_env); + + return NULL; +} +void NaClInstance::MainSignal() +{ + /* FIXME: find something more elegant. */ + main_queue.Pop(); + main_queue.Pop(); } void NaClInstance::HandleMessage(const pp::Var& message) @@ -129,11 +145,11 @@ void NaClInstance::DidChangeView(const pp::Rect& position, const pp::Rect& clip) m_size = ivec2(position.size().width(), position.size().height()); - if (opengl_context_ == NULL) - opengl_context_.reset(new OpenGLContext(this)); - opengl_context_->InvalidateContext(this); - opengl_context_->ResizeContext(position.size()); - if (!opengl_context_->MakeContextCurrent(this)) + if (m_opengl_ctx == NULL) + m_opengl_ctx.reset(new OpenGLContext(this)); + m_opengl_ctx->InvalidateContext(this); + m_opengl_ctx->ResizeContext(position.size()); + if (!m_opengl_ctx->MakeContextCurrent(this)) return; Video::Setup(m_size); @@ -151,7 +167,7 @@ bool NaClInstance::HandleInputEvent(const pp::InputEvent& event) Input::UnsetMouseButton(pp::MouseInputEvent(event).GetButton()); break; case PP_INPUTEVENT_TYPE_MOUSEMOVE: - Input::SetMousePos(ivec2(pp::MouseInputEvent(event).GetPosition().x(), opengl_context_->GetSize().height() - 1 - pp::MouseInputEvent(event).GetPosition().y())); + Input::SetMousePos(ivec2(pp::MouseInputEvent(event).GetPosition().x(), m_opengl_ctx->GetSize().height() - 1 - pp::MouseInputEvent(event).GetPosition().y())); break; default: break; @@ -161,12 +177,12 @@ bool NaClInstance::HandleInputEvent(const pp::InputEvent& event) void NaClInstance::DrawSelf() { - if (opengl_context_ == NULL) + if (m_opengl_ctx == NULL) return; - opengl_context_->MakeContextCurrent(this); + m_opengl_ctx->MakeContextCurrent(this); Ticker::TickDraw(); - opengl_context_->FlushContext(); + m_opengl_ctx->FlushContext(); } } // namespace lol diff --git a/src/platform/nacl/nacl-instance.h b/src/platform/nacl/nacl-instance.h new file mode 100644 index 00000000..f63bcadd --- /dev/null +++ b/src/platform/nacl/nacl-instance.h @@ -0,0 +1,78 @@ +// Copyright (c) 2011 The Native Client Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef EXAMPLES_TUMBLER_TUMBLER_H_ +#define EXAMPLES_TUMBLER_TUMBLER_H_ + +#include +#include + +#include "platform/nacl/opengl_context.h" +#include "platform/nacl/opengl_context_ptrs.h" + +#include "input/input.h" + +namespace lol { + +class NaClInstance : public pp::Instance +{ +public: + explicit NaClInstance(PP_Instance instance); + + // The dtor makes the 3D context current before deleting the cube view, then + // destroys the 3D context both in the module and in the browser. + virtual ~NaClInstance(); + + // Called by the browser when the NaCl module is loaded and all ready to go. + virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]); + + // Called whenever the in-browser window changes size. + virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip); + + // Called by the browser to handle the postMessage() call in Javascript. + virtual void HandleMessage(const pp::Var& message); + + // Bind and publish the module's methods to JavaScript. + //void InitializeMethods(ScriptingBridge* bridge); + + // Called to draw the contents of the module's browser area. + virtual bool HandleInputEvent(const pp::InputEvent& event); + + void DrawSelf(); + + /* Communication with the application object */ + static void MainSignal(); + +private: + SharedOpenGLContext m_opengl_ctx; + + ivec2 m_size; + + static void TickCallback(void* data, int32_t result); + static void * MainRun(void *data); + + /* Gamepad support */ + PPB_Gamepad const *m_pad_interface; + Array m_sticks; + + /* Communication with the application object */ + struct Args + { + Args(int argc, char **argv, char **env) + : m_argc(argc), m_argv(argv), m_env(env) {} + Args() {} + + int m_argc; + char **m_argv; + char **m_env; + }; + static Mutex main_mutex; + static Queue main_queue; + + Thread *m_main_thread; +}; + +} // namespace lol + +#endif // EXAMPLES_TUMBLER_TUMBLER_H_ diff --git a/src/platform/nacl/nacl_module.cpp b/src/platform/nacl/nacl-module.cpp similarity index 95% rename from src/platform/nacl/nacl_module.cpp rename to src/platform/nacl/nacl-module.cpp index b4408f78..b2102b35 100644 --- a/src/platform/nacl/nacl_module.cpp +++ b/src/platform/nacl/nacl-module.cpp @@ -11,9 +11,9 @@ #include #include "core.h" -#include "lolgl.h" +#include "lolgl.h" /* needed for GL_TRUE */ -#include "platform/nacl/nacl_instance.h" +#include "platform/nacl/nacl-instance.h" /// The Module class. The browser calls the CreateInstance() method to create /// an instance of your NaCl module on the web page. The browser creates a new diff --git a/src/platform/nacl/nacl_instance.h b/src/platform/nacl/nacl_instance.h deleted file mode 100644 index 420138d2..00000000 --- a/src/platform/nacl/nacl_instance.h +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) 2011 The Native Client Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef EXAMPLES_TUMBLER_TUMBLER_H_ -#define EXAMPLES_TUMBLER_TUMBLER_H_ - -#include -#include - -#include "platform/nacl/opengl_context.h" -#include "platform/nacl/opengl_context_ptrs.h" - -#include "input/input.h" - -namespace lol { - -class NaClInstance : public pp::Instance { - public: - explicit NaClInstance(PP_Instance instance); - - // The dtor makes the 3D context current before deleting the cube view, then - // destroys the 3D context both in the module and in the browser. - virtual ~NaClInstance(); - - // Called by the browser when the NaCl module is loaded and all ready to go. - virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]); - - // Called whenever the in-browser window changes size. - virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip); - - // Called by the browser to handle the postMessage() call in Javascript. - virtual void HandleMessage(const pp::Var& message); - - // Bind and publish the module's methods to JavaScript. - //void InitializeMethods(ScriptingBridge* bridge); - - - // Called to draw the contents of the module's browser area. - virtual bool HandleInputEvent(const pp::InputEvent& event); - - void DrawSelf(); - - SharedOpenGLContext opengl_context_; - - ivec2 m_size; - -private: - static void TickCallback(void* data, int32_t result); - - static void CallIntMainWrappers(int argc, char const* argn[]); - static void CallVoidMainWrappers(int argc, char const* argn[]); - - static void RunMain(uint32_t argc, const char* argn[], const char* argv[]); - - /* Gamepad support */ - PPB_Gamepad const *m_pad_interface; - Array m_sticks; -}; - -} // namespace lol - -#endif // EXAMPLES_TUMBLER_TUMBLER_H_ diff --git a/src/platform/nacl/opengl_context.h b/src/platform/nacl/opengl_context.h index 95c237ca..56879a97 100644 --- a/src/platform/nacl/opengl_context.h +++ b/src/platform/nacl/opengl_context.h @@ -69,7 +69,7 @@ class OpenGLContext : public pp::Graphics3DClient { } /// The PP_Resource needed to make GLES2 calls through the Pepper interface. - const PP_Resource gl_context() const { + PP_Resource gl_context() const { return context_.pp_resource(); }