OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "mojo/examples/compositor_app/compositor_host.h" | 9 #include "mojo/examples/compositor_app/compositor_host.h" |
10 #include "mojo/public/bindings/allocation_scope.h" | 10 #include "mojo/public/bindings/allocation_scope.h" |
11 #include "mojo/public/bindings/remote_ptr.h" | 11 #include "mojo/public/bindings/remote_ptr.h" |
12 #include "mojo/public/gles2/gles2_cpp.h" | 12 #include "mojo/public/gles2/gles2_cpp.h" |
| 13 #include "mojo/public/shell/application.h" |
13 #include "mojo/public/system/core.h" | 14 #include "mojo/public/system/core.h" |
14 #include "mojo/public/system/macros.h" | 15 #include "mojo/public/system/macros.h" |
15 #include "mojo/services/native_viewport/geometry_conversions.h" | 16 #include "mojo/services/native_viewport/geometry_conversions.h" |
16 #include "mojom/native_viewport.h" | 17 #include "mojom/native_viewport.h" |
17 #include "mojom/shell.h" | 18 #include "mojom/shell.h" |
18 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
19 | 20 |
20 #if defined(WIN32) | 21 #if defined(WIN32) |
21 #if !defined(CDECL) | 22 #if !defined(CDECL) |
22 #define CDECL __cdecl | 23 #define CDECL __cdecl |
23 #endif | 24 #endif |
24 #define SAMPLE_APP_EXPORT __declspec(dllexport) | 25 #define SAMPLE_APP_EXPORT __declspec(dllexport) |
25 #else | 26 #else |
26 #define CDECL | 27 #define CDECL |
27 #define SAMPLE_APP_EXPORT __attribute__((visibility("default"))) | 28 #define SAMPLE_APP_EXPORT __attribute__((visibility("default"))) |
28 #endif | 29 #endif |
29 | 30 |
30 namespace mojo { | 31 namespace mojo { |
31 namespace examples { | 32 namespace examples { |
32 | 33 |
33 class SampleApp : public ShellClient { | 34 class SampleApp : public Application, public NativeViewportClient { |
34 public: | 35 public: |
35 explicit SampleApp(ScopedShellHandle shell_handle) | 36 explicit SampleApp(MojoHandle shell_handle) : Application(shell_handle) { |
36 : shell_(shell_handle.Pass(), this) { | 37 InterfacePipe<NativeViewport, AnyInterface> viewport_pipe; |
37 InterfacePipe<NativeViewport, AnyInterface> pipe; | |
38 | 38 |
39 AllocationScope scope; | 39 AllocationScope scope; |
40 shell_->Connect("mojo:mojo_native_viewport_service", | 40 shell()->Connect("mojo:mojo_native_viewport_service", |
41 pipe.handle_to_peer.Pass()); | 41 viewport_pipe.handle_to_peer.Pass()); |
42 | 42 |
43 native_viewport_client_.reset( | 43 viewport_.reset(viewport_pipe.handle_to_self.Pass(), this); |
44 new NativeViewportClientImpl(pipe.handle_to_self.Pass())); | 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())); |
45 } | 52 } |
46 | 53 |
47 virtual void AcceptConnection(const mojo::String& url, | 54 virtual void OnCreated() MOJO_OVERRIDE { |
48 ScopedMessagePipeHandle handle) MOJO_OVERRIDE { | 55 } |
49 NOTREACHED() << "SampleApp can't be connected to."; | 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 } |
50 } | 69 } |
51 | 70 |
52 private: | 71 private: |
53 class NativeViewportClientImpl : public NativeViewportClient { | 72 RemotePtr<NativeViewport> viewport_; |
54 public: | 73 scoped_ptr<CompositorHost> host_; |
55 explicit NativeViewportClientImpl( | |
56 ScopedNativeViewportHandle viewport_handle) | |
57 : viewport_(viewport_handle.Pass(), this) { | |
58 AllocationScope allocation; | |
59 viewport_->Create(gfx::Rect(10, 10, 800, 600)); | |
60 viewport_->Show(); | |
61 ScopedMessagePipeHandle gles2_handle; | |
62 ScopedMessagePipeHandle gles2_client_handle; | |
63 CreateMessagePipe(&gles2_handle, &gles2_client_handle); | |
64 | |
65 viewport_->CreateGLES2Context(gles2_client_handle.Pass()); | |
66 host_.reset(new CompositorHost(gles2_handle.Pass())); | |
67 } | |
68 | |
69 virtual ~NativeViewportClientImpl() {} | |
70 | |
71 virtual void OnCreated() MOJO_OVERRIDE { | |
72 } | |
73 | |
74 virtual void OnDestroyed() MOJO_OVERRIDE { | |
75 base::MessageLoop::current()->Quit(); | |
76 } | |
77 | |
78 virtual void OnBoundsChanged(const Rect& bounds) MOJO_OVERRIDE { | |
79 host_->SetSize(bounds.size()); | |
80 } | |
81 | |
82 virtual void OnEvent(const Event& event) MOJO_OVERRIDE { | |
83 if (!event.location().is_null()) { | |
84 viewport_->AckEvent(event); | |
85 } | |
86 } | |
87 | |
88 private: | |
89 RemotePtr<NativeViewport> viewport_; | |
90 scoped_ptr<CompositorHost> host_; | |
91 }; | |
92 RemotePtr<Shell> shell_; | |
93 scoped_ptr<NativeViewportClientImpl> native_viewport_client_; | |
94 }; | 74 }; |
95 | 75 |
96 } // namespace examples | 76 } // namespace examples |
97 } // namespace mojo | 77 } // namespace mojo |
98 | 78 |
99 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain( | 79 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain( |
100 MojoHandle shell_handle) { | 80 MojoHandle shell_handle) { |
101 base::MessageLoop loop; | 81 base::MessageLoop loop; |
102 mojo::GLES2Initializer gles2; | 82 mojo::GLES2Initializer gles2; |
103 | 83 |
104 mojo::examples::SampleApp app( | 84 mojo::examples::SampleApp app(shell_handle); |
105 mojo::MakeScopedHandle(mojo::ShellHandle(shell_handle)).Pass()); | |
106 | |
107 loop.Run(); | 85 loop.Run(); |
108 | |
109 return MOJO_RESULT_OK; | 86 return MOJO_RESULT_OK; |
110 } | 87 } |
OLD | NEW |