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/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "examples/sample_app/gles2_client_impl.h" | 10 #include "examples/spinning_cube/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/gpu/public/interfaces/gpu.mojom.h" | 19 #include "mojo/services/gpu/public/interfaces/gpu.mojom.h" |
20 #include "mojo/services/native_viewport/public/interfaces/native_viewport.mojom.
h" | 20 #include "mojo/services/native_viewport/public/interfaces/native_viewport.mojom.
h" |
21 | 21 |
22 namespace examples { | 22 namespace examples { |
23 | 23 |
24 class SampleApp : public mojo::ApplicationDelegate, | 24 class SpinningCubeApp : public mojo::ApplicationDelegate, |
25 public mojo::NativeViewportEventDispatcher, | 25 public mojo::NativeViewportEventDispatcher, |
26 public mojo::ErrorHandler { | 26 public mojo::ErrorHandler { |
27 public: | 27 public: |
28 SampleApp() : dispatcher_binding_(this) {} | 28 SpinningCubeApp() : dispatcher_binding_(this) {} |
29 | 29 |
30 ~SampleApp() override { | 30 ~SpinningCubeApp() override { |
31 // TODO(darin): Fix shutdown so we don't need to leak this. | 31 // TODO(darin): Fix shutdown so we don't need to leak this. |
32 mojo_ignore_result(gles2_client_.release()); | 32 mojo_ignore_result(gles2_client_.release()); |
33 } | 33 } |
34 | 34 |
35 void Initialize(mojo::ApplicationImpl* app) override { | 35 void Initialize(mojo::ApplicationImpl* app) override { |
36 app->ConnectToService("mojo:native_viewport_service", &viewport_); | 36 app->ConnectToService("mojo:native_viewport_service", &viewport_); |
37 viewport_.set_error_handler(this); | 37 viewport_.set_error_handler(this); |
38 | 38 |
39 SetEventDispatcher(); | 39 SetEventDispatcher(); |
40 | 40 |
41 // TODO(jamesr): Should be mojo:gpu_service | 41 // TODO(jamesr): Should be mojo:gpu_service |
42 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); | 42 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); |
43 | 43 |
44 mojo::SizePtr size(mojo::Size::New()); | 44 mojo::SizePtr size(mojo::Size::New()); |
45 size->width = 800; | 45 size->width = 800; |
46 size->height = 600; | 46 size->height = 600; |
47 viewport_->Create(size.Pass(), | 47 viewport_->Create(size.Pass(), |
48 base::Bind(&SampleApp::OnCreatedNativeViewport, | 48 base::Bind(&SpinningCubeApp::OnCreatedNativeViewport, |
49 base::Unretained(this))); | 49 base::Unretained(this))); |
50 viewport_->Show(); | 50 viewport_->Show(); |
51 } | 51 } |
52 | 52 |
53 void OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | 53 void OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
54 assert(metrics); | 54 assert(metrics); |
55 if (gles2_client_) | 55 if (gles2_client_) |
56 gles2_client_->SetSize(*metrics->size); | 56 gles2_client_->SetSize(*metrics->size); |
57 viewport_->RequestMetrics( | 57 viewport_->RequestMetrics( |
58 base::Bind(&SampleApp::OnMetricsChanged, base::Unretained(this))); | 58 base::Bind(&SpinningCubeApp::OnMetricsChanged, base::Unretained(this))); |
59 } | 59 } |
60 | 60 |
61 void OnEvent(mojo::EventPtr event, | 61 void OnEvent(mojo::EventPtr event, |
62 const mojo::Callback<void()>& callback) override { | 62 const mojo::Callback<void()>& callback) override { |
63 assert(event); | 63 assert(event); |
64 if (event->location_data && event->location_data->in_view_location) | 64 if (event->location_data && event->location_data->in_view_location) |
65 gles2_client_->HandleInputEvent(*event); | 65 gles2_client_->HandleInputEvent(*event); |
66 callback.Run(); | 66 callback.Run(); |
67 } | 67 } |
68 | 68 |
69 private: | 69 private: |
70 void SetEventDispatcher() { | 70 void SetEventDispatcher() { |
71 mojo::NativeViewportEventDispatcherPtr ptr; | 71 mojo::NativeViewportEventDispatcherPtr ptr; |
72 dispatcher_binding_.Bind(GetProxy(&ptr)); | 72 dispatcher_binding_.Bind(GetProxy(&ptr)); |
73 viewport_->SetEventDispatcher(ptr.Pass()); | 73 viewport_->SetEventDispatcher(ptr.Pass()); |
74 } | 74 } |
75 | 75 |
76 void OnCreatedNativeViewport(uint64_t native_viewport_id, | 76 void OnCreatedNativeViewport(uint64_t native_viewport_id, |
77 mojo::ViewportMetricsPtr metrics) { | 77 mojo::ViewportMetricsPtr metrics) { |
78 mojo::ViewportParameterListenerPtr listener; | 78 mojo::ViewportParameterListenerPtr listener; |
79 mojo::CommandBufferPtr command_buffer; | 79 mojo::CommandBufferPtr command_buffer; |
80 // TODO(jamesr): Output to a surface instead. | 80 // TODO(jamesr): Output to a surface instead. |
81 gpu_service_->CreateOnscreenGLES2Context( | 81 gpu_service_->CreateOnscreenGLES2Context( |
82 native_viewport_id, metrics->size.Clone(), GetProxy(&command_buffer), | 82 native_viewport_id, metrics->size.Clone(), GetProxy(&command_buffer), |
83 listener.Pass()); | 83 listener.Pass()); |
84 gles2_client_.reset(new GLES2ClientImpl(command_buffer.Pass())); | 84 gles2_client_.reset(new GLES2ClientImpl(command_buffer.Pass())); |
85 gles2_client_->SetSize(*metrics->size); | 85 gles2_client_->SetSize(*metrics->size); |
86 viewport_->RequestMetrics( | 86 viewport_->RequestMetrics( |
87 base::Bind(&SampleApp::OnMetricsChanged, base::Unretained(this))); | 87 base::Bind(&SpinningCubeApp::OnMetricsChanged, base::Unretained(this))); |
88 } | 88 } |
89 | 89 |
90 // ErrorHandler implementation. | 90 // ErrorHandler implementation. |
91 void OnConnectionError() override { mojo::RunLoop::current()->Quit(); } | 91 void OnConnectionError() override { mojo::RunLoop::current()->Quit(); } |
92 | 92 |
93 scoped_ptr<GLES2ClientImpl> gles2_client_; | 93 scoped_ptr<GLES2ClientImpl> gles2_client_; |
94 mojo::NativeViewportPtr viewport_; | 94 mojo::NativeViewportPtr viewport_; |
95 mojo::GpuPtr gpu_service_; | 95 mojo::GpuPtr gpu_service_; |
96 mojo::Binding<NativeViewportEventDispatcher> dispatcher_binding_; | 96 mojo::Binding<NativeViewportEventDispatcher> dispatcher_binding_; |
97 | 97 |
98 DISALLOW_COPY_AND_ASSIGN(SampleApp); | 98 DISALLOW_COPY_AND_ASSIGN(SpinningCubeApp); |
99 }; | 99 }; |
100 | 100 |
101 } // namespace examples | 101 } // namespace examples |
102 | 102 |
103 MojoResult MojoMain(MojoHandle shell_handle) { | 103 MojoResult MojoMain(MojoHandle shell_handle) { |
104 mojo::ApplicationRunner runner(new examples::SampleApp); | 104 mojo::ApplicationRunner runner(new examples::SpinningCubeApp); |
105 return runner.Run(shell_handle); | 105 return runner.Run(shell_handle); |
106 } | 106 } |
OLD | NEW |