James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <string> |
| 7 | |
| 8 | #include "base/bind.h" |
James Robinson | 91b812b | 2015-01-28 17:13:54 -0800 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
Przemyslaw Pietrzkiewicz | 6917151 | 2015-03-02 11:45:56 +0100 | [diff] [blame] | 10 | #include "examples/spinning_cube/gles2_client_impl.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 11 | #include "mojo/public/c/system/main.h" |
| 12 | #include "mojo/public/cpp/application/application_connection.h" |
| 13 | #include "mojo/public/cpp/application/application_delegate.h" |
| 14 | #include "mojo/public/cpp/application/application_impl.h" |
| 15 | #include "mojo/public/cpp/application/application_runner.h" |
| 16 | #include "mojo/public/cpp/system/core.h" |
| 17 | #include "mojo/public/cpp/system/macros.h" |
| 18 | #include "mojo/public/cpp/utility/run_loop.h" |
Colin Blundell | ecff1e7 | 2014-12-10 22:16:07 +0100 | [diff] [blame] | 19 | #include "mojo/services/gpu/public/interfaces/gpu.mojom.h" |
Colin Blundell | 45b7fba | 2014-12-10 21:48:15 +0100 | [diff] [blame] | 20 | #include "mojo/services/native_viewport/public/interfaces/native_viewport.mojom.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 21 | |
| 22 | namespace examples { |
| 23 | |
Przemyslaw Pietrzkiewicz | 6917151 | 2015-03-02 11:45:56 +0100 | [diff] [blame] | 24 | class SpinningCubeApp : public mojo::ApplicationDelegate, |
Viet-Trung Luu | 641ca6d | 2015-07-07 15:17:48 -0700 | [diff] [blame] | 25 | public mojo::NativeViewportEventDispatcher { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 26 | public: |
Przemyslaw Pietrzkiewicz | 6917151 | 2015-03-02 11:45:56 +0100 | [diff] [blame] | 27 | SpinningCubeApp() : dispatcher_binding_(this) {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 28 | |
Przemyslaw Pietrzkiewicz | 6917151 | 2015-03-02 11:45:56 +0100 | [diff] [blame] | 29 | ~SpinningCubeApp() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 30 | // TODO(darin): Fix shutdown so we don't need to leak this. |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 31 | mojo_ignore_result(gles2_client_.release()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Scott Violet | c947ecc | 2014-10-28 16:49:51 -0700 | [diff] [blame] | 34 | void Initialize(mojo::ApplicationImpl* app) override { |
James Robinson | 24218d7 | 2014-10-20 16:18:41 -0700 | [diff] [blame] | 35 | app->ConnectToService("mojo:native_viewport_service", &viewport_); |
Viet-Trung Luu | 641ca6d | 2015-07-07 15:17:48 -0700 | [diff] [blame] | 36 | viewport_.set_connection_error_handler( |
| 37 | [this]() { OnViewportConnectionError(); }); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 38 | |
Scott Violet | c947ecc | 2014-10-28 16:49:51 -0700 | [diff] [blame] | 39 | SetEventDispatcher(); |
| 40 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 41 | mojo::SizePtr size(mojo::Size::New()); |
| 42 | size->width = 800; |
| 43 | size->height = 600; |
Ian Fischer | f66f437 | 2015-06-09 16:50:28 -0700 | [diff] [blame] | 44 | |
| 45 | auto requested_configuration = mojo::SurfaceConfiguration::New(); |
| 46 | requested_configuration->depth_bits = 16; |
| 47 | |
James Robinson | 187e1fa | 2015-03-04 15:49:53 -0800 | [diff] [blame] | 48 | viewport_->Create( |
| 49 | size.Clone(), |
Ian Fischer | f66f437 | 2015-06-09 16:50:28 -0700 | [diff] [blame] | 50 | requested_configuration.Pass(), |
James Robinson | 187e1fa | 2015-03-04 15:49:53 -0800 | [diff] [blame] | 51 | base::Bind(&SpinningCubeApp::OnMetricsChanged, base::Unretained(this))); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 52 | viewport_->Show(); |
James Robinson | 187e1fa | 2015-03-04 15:49:53 -0800 | [diff] [blame] | 53 | mojo::ContextProviderPtr onscreen_context_provider; |
| 54 | viewport_->GetContextProvider(GetProxy(&onscreen_context_provider)); |
| 55 | |
| 56 | gles2_client_.reset(new GLES2ClientImpl(onscreen_context_provider.Pass())); |
| 57 | gles2_client_->SetSize(*size); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 58 | } |
| 59 | |
James Robinson | 91b812b | 2015-01-28 17:13:54 -0800 | [diff] [blame] | 60 | void OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
Adam Barth | 9329c44 | 2015-01-09 21:56:43 -0800 | [diff] [blame] | 61 | assert(metrics); |
James Robinson | 187e1fa | 2015-03-04 15:49:53 -0800 | [diff] [blame] | 62 | gles2_client_->SetSize(*metrics->size); |
James Robinson | 91b812b | 2015-01-28 17:13:54 -0800 | [diff] [blame] | 63 | viewport_->RequestMetrics( |
Przemyslaw Pietrzkiewicz | 6917151 | 2015-03-02 11:45:56 +0100 | [diff] [blame] | 64 | base::Bind(&SpinningCubeApp::OnMetricsChanged, base::Unretained(this))); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Scott Violet | c947ecc | 2014-10-28 16:49:51 -0700 | [diff] [blame] | 67 | void OnEvent(mojo::EventPtr event, |
| 68 | const mojo::Callback<void()>& callback) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 69 | assert(event); |
Scott Violet | b569888 | 2015-03-23 14:33:21 -0700 | [diff] [blame] | 70 | if (event->pointer_data.get()) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 71 | gles2_client_->HandleInputEvent(*event); |
| 72 | callback.Run(); |
| 73 | } |
| 74 | |
| 75 | private: |
Scott Violet | c947ecc | 2014-10-28 16:49:51 -0700 | [diff] [blame] | 76 | void SetEventDispatcher() { |
James Robinson | 91b812b | 2015-01-28 17:13:54 -0800 | [diff] [blame] | 77 | mojo::NativeViewportEventDispatcherPtr ptr; |
| 78 | dispatcher_binding_.Bind(GetProxy(&ptr)); |
| 79 | viewport_->SetEventDispatcher(ptr.Pass()); |
Scott Violet | c947ecc | 2014-10-28 16:49:51 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Viet-Trung Luu | 641ca6d | 2015-07-07 15:17:48 -0700 | [diff] [blame] | 82 | void OnViewportConnectionError() { mojo::RunLoop::current()->Quit(); } |
James Robinson | 91b812b | 2015-01-28 17:13:54 -0800 | [diff] [blame] | 83 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 84 | scoped_ptr<GLES2ClientImpl> gles2_client_; |
| 85 | mojo::NativeViewportPtr viewport_; |
James Robinson | 187e1fa | 2015-03-04 15:49:53 -0800 | [diff] [blame] | 86 | mojo::ContextProviderPtr onscreen_context_provider_; |
James Robinson | 91b812b | 2015-01-28 17:13:54 -0800 | [diff] [blame] | 87 | mojo::Binding<NativeViewportEventDispatcher> dispatcher_binding_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 88 | |
Przemyslaw Pietrzkiewicz | 6917151 | 2015-03-02 11:45:56 +0100 | [diff] [blame] | 89 | DISALLOW_COPY_AND_ASSIGN(SpinningCubeApp); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace examples |
| 93 | |
Mitch Rudominer | 963aa77 | 2015-04-03 08:22:46 -0700 | [diff] [blame] | 94 | MojoResult MojoMain(MojoHandle application_request) { |
Przemyslaw Pietrzkiewicz | 6917151 | 2015-03-02 11:45:56 +0100 | [diff] [blame] | 95 | mojo::ApplicationRunner runner(new examples::SpinningCubeApp); |
Mitch Rudominer | 963aa77 | 2015-04-03 08:22:46 -0700 | [diff] [blame] | 96 | return runner.Run(application_request); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 97 | } |