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/bind.h" | 8 #include "base/bind.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "examples/sample_app/gles2_client_impl.h" | 10 #include "examples/sample_app/gles2_client_impl.h" |
11 #include "mojo/public/c/system/main.h" | 11 #include "mojo/public/c/system/main.h" |
12 #include "mojo/public/cpp/application/application_connection.h" | 12 #include "mojo/public/cpp/application/application_connection.h" |
13 #include "mojo/public/cpp/application/application_delegate.h" | 13 #include "mojo/public/cpp/application/application_delegate.h" |
14 #include "mojo/public/cpp/application/application_impl.h" | 14 #include "mojo/public/cpp/application/application_impl.h" |
15 #include "mojo/public/cpp/application/application_runner.h" | 15 #include "mojo/public/cpp/application/application_runner.h" |
16 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/public/cpp/system/core.h" |
17 #include "mojo/public/cpp/system/macros.h" | 17 #include "mojo/public/cpp/system/macros.h" |
18 #include "mojo/public/cpp/utility/run_loop.h" | 18 #include "mojo/public/cpp/utility/run_loop.h" |
19 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" | 19 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" |
20 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" | 20 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" |
21 | 21 |
22 namespace examples { | 22 namespace examples { |
23 | 23 |
24 class SampleApp : public mojo::ApplicationDelegate, | 24 class SampleApp |
25 public mojo::NativeViewportClient { | 25 : public mojo::ApplicationDelegate, |
| 26 public mojo::NativeViewportClient, |
| 27 public mojo::InterfaceImpl<mojo::NativeViewportEventDispatcher> { |
26 public: | 28 public: |
27 SampleApp() : weak_factory_(this) {} | 29 SampleApp() : weak_factory_(this) {} |
28 | 30 |
29 virtual ~SampleApp() { | 31 ~SampleApp() override { |
30 // TODO(darin): Fix shutdown so we don't need to leak this. | 32 // TODO(darin): Fix shutdown so we don't need to leak this. |
31 mojo_ignore_result(gles2_client_.release()); | 33 mojo_ignore_result(gles2_client_.release()); |
32 } | 34 } |
33 | 35 |
34 virtual void Initialize(mojo::ApplicationImpl* app) override { | 36 void Initialize(mojo::ApplicationImpl* app) override { |
35 app->ConnectToService("mojo:native_viewport_service", &viewport_); | 37 app->ConnectToService("mojo:native_viewport_service", &viewport_); |
36 viewport_.set_client(this); | 38 viewport_.set_client(this); |
37 | 39 |
| 40 SetEventDispatcher(); |
| 41 |
38 // TODO(jamesr): Should be mojo:gpu_service | 42 // TODO(jamesr): Should be mojo:gpu_service |
39 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); | 43 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); |
40 | 44 |
41 mojo::SizePtr size(mojo::Size::New()); | 45 mojo::SizePtr size(mojo::Size::New()); |
42 size->width = 800; | 46 size->width = 800; |
43 size->height = 600; | 47 size->height = 600; |
44 viewport_->Create(size.Pass(), | 48 viewport_->Create(size.Pass(), |
45 base::Bind(&SampleApp::OnCreatedNativeViewport, | 49 base::Bind(&SampleApp::OnCreatedNativeViewport, |
46 weak_factory_.GetWeakPtr())); | 50 weak_factory_.GetWeakPtr())); |
47 viewport_->Show(); | 51 viewport_->Show(); |
48 } | 52 } |
49 | 53 |
50 virtual void OnDestroyed() override { mojo::RunLoop::current()->Quit(); } | 54 void OnDestroyed() override { mojo::RunLoop::current()->Quit(); } |
51 | 55 |
52 virtual void OnSizeChanged(mojo::SizePtr size) override { | 56 void OnSizeChanged(mojo::SizePtr size) override { |
53 assert(size); | 57 assert(size); |
54 if (gles2_client_) | 58 if (gles2_client_) |
55 gles2_client_->SetSize(*size); | 59 gles2_client_->SetSize(*size); |
56 } | 60 } |
57 | 61 |
58 virtual void OnEvent(mojo::EventPtr event, | 62 void OnEvent(mojo::EventPtr event, |
59 const mojo::Callback<void()>& callback) override { | 63 const mojo::Callback<void()>& callback) override { |
60 assert(event); | 64 assert(event); |
61 if (event->location_data && event->location_data->in_view_location) | 65 if (event->location_data && event->location_data->in_view_location) |
62 gles2_client_->HandleInputEvent(*event); | 66 gles2_client_->HandleInputEvent(*event); |
63 callback.Run(); | 67 callback.Run(); |
64 } | 68 } |
65 | 69 |
66 private: | 70 private: |
| 71 void SetEventDispatcher() { |
| 72 mojo::NativeViewportEventDispatcherPtr proxy; |
| 73 mojo::WeakBindToProxy(this, &proxy); |
| 74 viewport_->SetEventDispatcher(proxy.Pass()); |
| 75 } |
| 76 |
67 void OnCreatedNativeViewport(uint64_t native_viewport_id) { | 77 void OnCreatedNativeViewport(uint64_t native_viewport_id) { |
68 mojo::SizePtr size = mojo::Size::New(); | 78 mojo::SizePtr size = mojo::Size::New(); |
69 size->width = 800; | 79 size->width = 800; |
70 size->height = 600; | 80 size->height = 600; |
71 mojo::CommandBufferPtr command_buffer; | 81 mojo::CommandBufferPtr command_buffer; |
72 // TODO(jamesr): Output to a surface instead. | 82 // TODO(jamesr): Output to a surface instead. |
73 gpu_service_->CreateOnscreenGLES2Context( | 83 gpu_service_->CreateOnscreenGLES2Context( |
74 native_viewport_id, size.Pass(), GetProxy(&command_buffer)); | 84 native_viewport_id, size.Pass(), GetProxy(&command_buffer)); |
75 gles2_client_.reset(new GLES2ClientImpl(command_buffer.Pass())); | 85 gles2_client_.reset(new GLES2ClientImpl(command_buffer.Pass())); |
76 } | 86 } |
77 | 87 |
78 scoped_ptr<GLES2ClientImpl> gles2_client_; | 88 scoped_ptr<GLES2ClientImpl> gles2_client_; |
79 mojo::NativeViewportPtr viewport_; | 89 mojo::NativeViewportPtr viewport_; |
80 mojo::GpuPtr gpu_service_; | 90 mojo::GpuPtr gpu_service_; |
81 base::WeakPtrFactory<SampleApp> weak_factory_; | 91 base::WeakPtrFactory<SampleApp> weak_factory_; |
82 | 92 |
83 DISALLOW_COPY_AND_ASSIGN(SampleApp); | 93 DISALLOW_COPY_AND_ASSIGN(SampleApp); |
84 }; | 94 }; |
85 | 95 |
86 } // namespace examples | 96 } // namespace examples |
87 | 97 |
88 MojoResult MojoMain(MojoHandle shell_handle) { | 98 MojoResult MojoMain(MojoHandle shell_handle) { |
89 mojo::ApplicationRunner runner(new examples::SampleApp); | 99 mojo::ApplicationRunner runner(new examples::SampleApp); |
90 return runner.Run(shell_handle); | 100 return runner.Run(shell_handle); |
91 } | 101 } |
OLD | NEW |