summaryrefslogtreecommitdiffstats
path: root/mojo/examples/sample_app/sample_app.cc
blob: 74b9d99cc20ec897579237cd54e41d080c848768 (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
98
// 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/cpp/application/application.h"
#include "mojo/public/cpp/bindings/allocation_scope.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/cpp/gles2/gles2.h"
#include "mojo/public/cpp/system/core.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/public/cpp/utility/run_loop.h"
#include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
#include "mojo/services/native_viewport/native_viewport.mojom.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 Application, public NativeViewportClient {
 public:
  explicit SampleApp(MojoHandle service_provider_handle)
      : Application(service_provider_handle) {
    ConnectTo("mojo:mojo_native_viewport_service", &viewport_);
    viewport_.set_client(this);

    AllocationScope scope;

    Rect::Builder rect;
    Point::Builder point;
    point.set_x(10);
    point.set_y(10);
    rect.set_position(point.Finish());
    Size::Builder size;
    size.set_width(800);
    size.set_height(600);
    rect.set_size(size.Finish());
    viewport_->Create(rect.Finish());
    viewport_->Show();

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

  virtual ~SampleApp() {
    // TODO(darin): Fix shutdown so we don't need to leak this.
    MOJO_ALLOW_UNUSED GLES2ClientImpl* leaked = gles2_client_.release();
  }

  virtual void OnCreated() MOJO_OVERRIDE {
  }

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

  virtual void OnBoundsChanged(const Rect& bounds) MOJO_OVERRIDE {
    gles2_client_->SetSize(bounds.size());
  }

  virtual void OnEvent(const Event& event,
                       const Callback<void()>& callback) MOJO_OVERRIDE {
    if (!event.location().is_null())
      gles2_client_->HandleInputEvent(event);
    callback.Run();
  }

 private:
  scoped_ptr<GLES2ClientImpl> gles2_client_;
  NativeViewportPtr viewport_;
};

}  // namespace examples
}  // namespace mojo

extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain(
    MojoHandle service_provider_handle) {
  mojo::Environment env;
  mojo::RunLoop loop;
  mojo::GLES2Initializer gles2;

  mojo::examples::SampleApp app(service_provider_handle);
  loop.Run();
  return MOJO_RESULT_OK;
}