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