summaryrefslogtreecommitdiffstats
path: root/mojo/examples/sample_app/sample_app.cc
blob: 8b3d763d537c64078ffd54672dc3640aaef62748 (plain)
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
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdio.h>
#include <string>

#include "mojo/examples/sample_app/gles2_client_impl.h"
#include "mojo/public/bindings/lib/remote_ptr.h"
#include "mojo/public/environment/environment.h"
#include "mojo/public/gles2/gles2.h"
#include "mojo/public/system/core.h"
#include "mojo/public/system/macros.h"
#include "mojo/public/utility/run_loop.h"
#include "mojom/native_viewport.h"
#include "mojom/shell.h"

#if defined(WIN32)
#if !defined(CDECL)
#define CDECL __cdecl
#endif
#define SAMPLE_APP_EXPORT __declspec(dllexport)
#else
#define CDECL
#define SAMPLE_APP_EXPORT __attribute__((visibility("default")))
#endif

namespace mojo {
namespace examples {

class SampleApp : public ShellClient {
 public:
  explicit SampleApp(ScopedMessagePipeHandle shell_handle)
      : shell_(shell_handle.Pass(), this) {

    MessagePipe pipe;
    native_viewport_client_.reset(
        new NativeViewportClientImpl(pipe.handle0.Pass()));
    mojo::AllocationScope scope;
    shell_->Connect("mojo:mojo_native_viewport_service", pipe.handle1.Pass());
  }

  virtual void AcceptConnection(ScopedMessagePipeHandle handle) MOJO_OVERRIDE {
  }

 private:
  class NativeViewportClientImpl : public mojo::NativeViewportClient {
   public:
    explicit NativeViewportClientImpl(ScopedMessagePipeHandle viewport_handle)
        : viewport_(viewport_handle.Pass(), this) {
      viewport_->Open();

      MessagePipe pipe;
      gles2_client_.reset(new GLES2ClientImpl(pipe.handle0.Pass()));
      viewport_->CreateGLES2Context(pipe.handle1.Pass());
    }

    virtual ~NativeViewportClientImpl() {}

    virtual void OnCreated() MOJO_OVERRIDE {
    }

    virtual void OnDestroyed() MOJO_OVERRIDE {
      RunLoop::current()->Quit();
    }

    virtual void OnEvent(const Event& event) MOJO_OVERRIDE {
      if (!event.location().is_null()) {
        gles2_client_->HandleInputEvent(event);
        viewport_->AckEvent(event);
      }
    }

   private:
    scoped_ptr<GLES2ClientImpl> gles2_client_;
    RemotePtr<NativeViewport> viewport_;
  };
  mojo::RemotePtr<Shell> shell_;
  scoped_ptr<NativeViewportClientImpl> native_viewport_client_;
};

}  // namespace examples
}  // namespace mojo

extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain(
    MojoHandle shell_handle) {
  mojo::Environment env;
  mojo::RunLoop loop;
  MojoGLES2Initialize();

  mojo::examples::SampleApp app(
      mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)).Pass());
  loop.Run();

  MojoGLES2Terminate();
  return MOJO_RESULT_OK;
}