Browse Source

nacl: NaCl binaries no longer need a modified main() to work, the NaCl

instance object takes care of everything for us.
legacy
Sam Hocevar sam 12 years ago
parent
commit
bd649af62d
9 changed files with 132 additions and 106 deletions
  1. +3
    -3
      src/Makefile.am
  2. +1
    -1
      src/application/application.cpp
  3. +9
    -14
      src/platform/nacl/nacl-app.cpp
  4. +0
    -0
      src/platform/nacl/nacl-app.h
  5. +38
    -22
      src/platform/nacl/nacl-instance.cpp
  6. +78
    -0
      src/platform/nacl/nacl-instance.h
  7. +2
    -2
      src/platform/nacl/nacl-module.cpp
  8. +0
    -63
      src/platform/nacl/nacl_instance.h
  9. +1
    -1
      src/platform/nacl/opengl_context.h

+ 3
- 3
src/Makefile.am View File

@@ -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


+ 1
- 1
src/application/application.cpp View File

@@ -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


src/platform/nacl/naclapp.cpp → src/platform/nacl/nacl-app.cpp View File

@@ -12,15 +12,10 @@
# include "config.h"
#endif

#if defined __native_client__
# include <ppapi/cpp/instance.h>
# include <ppapi/cpp/module.h>
# include <ppapi/cpp/var.h>
#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<int, 1> q;
q.Pop();
}

NaClApp::~NaClApp()

src/platform/nacl/naclapp.h → src/platform/nacl/nacl-app.h View File


src/platform/nacl/nacl_instance.cpp → src/platform/nacl/nacl-instance.cpp View File

@@ -10,7 +10,6 @@
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#include <ppapi/cpp/rect.h>
#include <ppapi/cpp/size.h>
@@ -20,9 +19,8 @@
#include <ppapi/cpp/input_event.h>

#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::Args *, 1> 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<char **>(argv));
lol_nacl_main(argc, const_cast<char **>(argv), (char **)env);
Args arglist(argc, const_cast<char **>(argv), const_cast<char **>(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

+ 78
- 0
src/platform/nacl/nacl-instance.h View File

@@ -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 <ppapi/cpp/instance.h>
#include <ppapi/c/ppb_gamepad.h>

#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<Stick *> 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<Args *, 1> main_queue;

Thread *m_main_thread;
};

} // namespace lol

#endif // EXAMPLES_TUMBLER_TUMBLER_H_

src/platform/nacl/nacl_module.cpp → src/platform/nacl/nacl-module.cpp View File

@@ -11,9 +11,9 @@
#include <ppapi/gles2/gl2ext_ppapi.h>

#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

+ 0
- 63
src/platform/nacl/nacl_instance.h View File

@@ -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 <ppapi/cpp/instance.h>
#include <ppapi/c/ppb_gamepad.h>

#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<Stick *> m_sticks;
};

} // namespace lol

#endif // EXAMPLES_TUMBLER_TUMBLER_H_

+ 1
- 1
src/platform/nacl/opengl_context.h View File

@@ -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();
}



Loading…
Cancel
Save