Upstream version 7.36.149.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/macros.h"
9 #include "base/message_loop/message_loop.h"
10 #include "mojo/examples/compositor_app/compositor_host.h"
11 #include "mojo/public/cpp/bindings/allocation_scope.h"
12 #include "mojo/public/cpp/gles2/gles2.h"
13 #include "mojo/public/cpp/shell/application.h"
14 #include "mojo/public/cpp/system/core.h"
15 #include "mojo/public/interfaces/shell/shell.mojom.h"
16 #include "mojo/services/native_viewport/geometry_conversions.h"
17 #include "mojo/services/native_viewport/native_viewport.mojom.h"
18 #include "ui/gfx/rect.h"
19
20 #if defined(WIN32)
21 #if !defined(CDECL)
22 #define CDECL __cdecl
23 #endif
24 #define SAMPLE_APP_EXPORT __declspec(dllexport)
25 #else
26 #define CDECL
27 #define SAMPLE_APP_EXPORT __attribute__((visibility("default")))
28 #endif
29
30 namespace mojo {
31 namespace examples {
32
33 class SampleApp : public Application, public NativeViewportClient {
34  public:
35   explicit SampleApp(MojoHandle shell_handle) : Application(shell_handle) {
36     AllocationScope scope;
37
38     ConnectTo("mojo:mojo_native_viewport_service", &viewport_);
39     viewport_->SetClient(this);
40
41     viewport_->Create(gfx::Rect(10, 10, 800, 600));
42     viewport_->Show();
43
44     MessagePipe gles2_pipe;
45     viewport_->CreateGLES2Context(gles2_pipe.handle0.Pass());
46     host_.reset(new CompositorHost(gles2_pipe.handle1.Pass()));
47   }
48
49   virtual void OnCreated() OVERRIDE {
50   }
51
52   virtual void OnDestroyed() OVERRIDE {
53     base::MessageLoop::current()->Quit();
54   }
55
56   virtual void OnBoundsChanged(const Rect& bounds) OVERRIDE {
57     host_->SetSize(bounds.size());
58   }
59
60   virtual void OnEvent(const Event& event,
61                        const mojo::Callback<void()>& callback) OVERRIDE {
62     callback.Run();
63   }
64
65  private:
66   NativeViewportPtr viewport_;
67   scoped_ptr<CompositorHost> host_;
68 };
69
70 }  // namespace examples
71 }  // namespace mojo
72
73 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain(
74     MojoHandle shell_handle) {
75   base::MessageLoop loop;
76   mojo::GLES2Initializer gles2;
77
78   mojo::examples::SampleApp app(shell_handle);
79   loop.Run();
80   return MOJO_RESULT_OK;
81 }