OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "base/bind.h" | |
6 #include "base/macros.h" | |
7 #include "base/memory/weak_ptr.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "cc/surfaces/surface_id_allocator.h" | |
10 #include "mojo/application/application_runner_chromium.h" | |
11 #include "mojo/converters/geometry/geometry_type_converters.h" | |
12 #include "mojo/converters/surfaces/surfaces_type_converters.h" | |
13 #include "mojo/examples/surfaces_app/child.mojom.h" | |
14 #include "mojo/examples/surfaces_app/embedder.h" | |
15 #include "mojo/public/c/system/main.h" | |
16 #include "mojo/public/cpp/application/application_connection.h" | |
17 #include "mojo/public/cpp/application/application_delegate.h" | |
18 #include "mojo/public/cpp/system/core.h" | |
19 #include "mojo/services/public/interfaces/gpu/command_buffer.mojom.h" | |
20 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" | |
21 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" | |
22 #include "mojo/services/public/interfaces/surfaces/surfaces.mojom.h" | |
23 #include "mojo/services/public/interfaces/surfaces/surfaces_service.mojom.h" | |
24 #include "ui/gfx/rect.h" | |
25 | |
26 namespace mojo { | |
27 namespace examples { | |
28 | |
29 class SurfacesApp : public ApplicationDelegate, | |
30 public SurfaceClient, | |
31 public NativeViewportClient { | |
32 public: | |
33 SurfacesApp() : weak_factory_(this) {} | |
34 virtual ~SurfacesApp() {} | |
35 | |
36 // ApplicationDelegate implementation | |
37 virtual bool ConfigureIncomingConnection( | |
38 ApplicationConnection* connection) override { | |
39 connection->ConnectToService("mojo:native_viewport_service", &viewport_); | |
40 viewport_.set_client(this); | |
41 | |
42 connection->ConnectToService("mojo:surfaces_service", &surfaces_service_); | |
43 surfaces_service_->CreateSurfaceConnection(base::Bind( | |
44 &SurfacesApp::SurfaceConnectionCreated, base::Unretained(this))); | |
45 | |
46 size_ = gfx::Size(800, 600); | |
47 | |
48 viewport_->Create(Size::From(size_), | |
49 base::Bind(&SurfacesApp::OnCreatedNativeViewport, | |
50 weak_factory_.GetWeakPtr())); | |
51 viewport_->Show(); | |
52 | |
53 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); | |
54 connection->ConnectToService("mojo:surfaces_child_app", &child_one_); | |
55 connection->ConnectToService("mojo:surfaces_child_gl_app", &child_two_); | |
56 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), | |
57 Size::From(child_size_), | |
58 base::Bind(&SurfacesApp::ChildOneProducedFrame, | |
59 base::Unretained(this))); | |
60 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), | |
61 Size::From(child_size_), | |
62 base::Bind(&SurfacesApp::ChildTwoProducedFrame, | |
63 base::Unretained(this))); | |
64 return true; | |
65 } | |
66 | |
67 void ChildOneProducedFrame(SurfaceIdPtr id) { | |
68 child_one_id_ = id.To<cc::SurfaceId>(); | |
69 } | |
70 | |
71 void ChildTwoProducedFrame(SurfaceIdPtr id) { | |
72 child_two_id_ = id.To<cc::SurfaceId>(); | |
73 } | |
74 | |
75 void Draw(int offset) { | |
76 int bounced_offset = offset; | |
77 if (offset > 200) | |
78 bounced_offset = 400 - offset; | |
79 embedder_->ProduceFrame( | |
80 child_one_id_, child_two_id_, child_size_, size_, bounced_offset); | |
81 base::MessageLoop::current()->PostDelayedTask( | |
82 FROM_HERE, | |
83 base::Bind( | |
84 &SurfacesApp::Draw, base::Unretained(this), (offset + 2) % 400), | |
85 base::TimeDelta::FromMilliseconds(50)); | |
86 } | |
87 | |
88 void SurfaceConnectionCreated(SurfacePtr surface, uint32_t id_namespace) { | |
89 surface_ = surface.Pass(); | |
90 surface_.set_client(this); | |
91 embedder_.reset(new Embedder(surface_.get())); | |
92 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | |
93 | |
94 onscreen_id_ = allocator_->GenerateId(); | |
95 embedder_->SetSurfaceId(onscreen_id_); | |
96 surface_->CreateSurface(SurfaceId::From(onscreen_id_), Size::From(size_)); | |
97 viewport_->SubmittedFrame(SurfaceId::From(onscreen_id_)); | |
98 Draw(10); | |
99 } | |
100 | |
101 // SurfaceClient implementation. | |
102 virtual void ReturnResources(Array<ReturnedResourcePtr> resources) override { | |
103 DCHECK(!resources.size()); | |
104 } | |
105 // NativeViewportClient implementation. | |
106 virtual void OnSizeChanged(mojo::SizePtr size) override {} | |
107 virtual void OnDestroyed() override {} | |
108 virtual void OnEvent(mojo::EventPtr event, | |
109 const mojo::Callback<void()>& callback) override { | |
110 callback.Run(); | |
111 } | |
112 | |
113 private: | |
114 void OnCreatedNativeViewport(uint64_t native_viewport_id) {} | |
115 | |
116 SurfacesServicePtr surfaces_service_; | |
117 SurfacePtr surface_; | |
118 cc::SurfaceId onscreen_id_; | |
119 scoped_ptr<cc::SurfaceIdAllocator> allocator_; | |
120 scoped_ptr<Embedder> embedder_; | |
121 ChildPtr child_one_; | |
122 cc::SurfaceId child_one_id_; | |
123 ChildPtr child_two_; | |
124 cc::SurfaceId child_two_id_; | |
125 gfx::Size size_; | |
126 gfx::Size child_size_; | |
127 | |
128 NativeViewportPtr viewport_; | |
129 | |
130 base::WeakPtrFactory<SurfacesApp> weak_factory_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); | |
133 }; | |
134 | |
135 } // namespace examples | |
136 } // namespace mojo | |
137 | |
138 MojoResult MojoMain(MojoHandle shell_handle) { | |
139 mojo::ApplicationRunnerChromium runner(new mojo::examples::SurfacesApp); | |
140 return runner.Run(shell_handle); | |
141 } | |
OLD | NEW |