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