Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / compositor_app / compositor_app.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <string>
7
8 #include "base/message_loop/message_loop.h"
9 #include "mojo/examples/compositor_app/compositor_host.h"
10 #include "mojo/public/bindings/allocation_scope.h"
11 #include "mojo/public/bindings/remote_ptr.h"
12 #include "mojo/public/gles2/gles2_cpp.h"
13 #include "mojo/public/shell/application.h"
14 #include "mojo/public/system/core.h"
15 #include "mojo/public/system/macros.h"
16 #include "mojo/services/native_viewport/geometry_conversions.h"
17 #include "mojom/native_viewport.h"
18 #include "mojom/shell.h"
19 #include "ui/gfx/rect.h"
20
21 #if defined(WIN32)
22 #if !defined(CDECL)
23 #define CDECL __cdecl
24 #endif
25 #define SAMPLE_APP_EXPORT __declspec(dllexport)
26 #else
27 #define CDECL
28 #define SAMPLE_APP_EXPORT __attribute__((visibility("default")))
29 #endif
30
31 namespace mojo {
32 namespace examples {
33
34 class SampleApp : public Application, public NativeViewportClient {
35  public:
36   explicit SampleApp(MojoHandle shell_handle) : Application(shell_handle) {
37     InterfacePipe<NativeViewport, AnyInterface> viewport_pipe;
38
39     AllocationScope scope;
40     shell()->Connect("mojo:mojo_native_viewport_service",
41                      viewport_pipe.handle_to_peer.Pass());
42
43     viewport_.reset(viewport_pipe.handle_to_self.Pass(), this);
44     viewport_->Create(gfx::Rect(10, 10, 800, 600));
45     viewport_->Show();
46     ScopedMessagePipeHandle gles2_handle;
47     ScopedMessagePipeHandle gles2_client_handle;
48     CreateMessagePipe(&gles2_handle, &gles2_client_handle);
49
50     viewport_->CreateGLES2Context(gles2_client_handle.Pass());
51     host_.reset(new CompositorHost(gles2_handle.Pass()));
52   }
53
54   virtual void OnCreated() MOJO_OVERRIDE {
55   }
56
57   virtual void OnDestroyed() MOJO_OVERRIDE {
58     base::MessageLoop::current()->Quit();
59   }
60
61   virtual void OnBoundsChanged(const Rect& bounds) MOJO_OVERRIDE {
62     host_->SetSize(bounds.size());
63   }
64
65   virtual void OnEvent(const Event& event) MOJO_OVERRIDE {
66     if (!event.location().is_null()) {
67       viewport_->AckEvent(event);
68     }
69   }
70
71  private:
72   RemotePtr<NativeViewport> viewport_;
73   scoped_ptr<CompositorHost> host_;
74 };
75
76 }  // namespace examples
77 }  // namespace mojo
78
79 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain(
80     MojoHandle shell_handle) {
81   base::MessageLoop loop;
82   mojo::GLES2Initializer gles2;
83
84   mojo::examples::SampleApp app(shell_handle);
85   loop.Run();
86   return MOJO_RESULT_OK;
87 }