OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "cc/surfaces/surface_id_allocator.h" | 8 #include "cc/surfaces/surface_id_allocator.h" |
9 #include "mojo/examples/surfaces_app/child.mojom.h" | 9 #include "mojo/examples/surfaces_app/child.mojom.h" |
10 #include "mojo/examples/surfaces_app/embedder.h" | 10 #include "mojo/examples/surfaces_app/embedder.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "mojo/services/public/interfaces/surfaces/surfaces_service.mojom.h" | 22 #include "mojo/services/public/interfaces/surfaces/surfaces_service.mojom.h" |
23 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
24 | 24 |
25 namespace mojo { | 25 namespace mojo { |
26 namespace examples { | 26 namespace examples { |
27 | 27 |
28 class SurfacesApp : public ApplicationDelegate, | 28 class SurfacesApp : public ApplicationDelegate, |
29 public SurfaceClient, | 29 public SurfaceClient, |
30 public NativeViewportClient { | 30 public NativeViewportClient { |
31 public: | 31 public: |
32 SurfacesApp() : native_viewport_id_(0) {} | 32 SurfacesApp() {} |
33 virtual ~SurfacesApp() {} | 33 virtual ~SurfacesApp() {} |
34 | 34 |
35 // ApplicationDelegate implementation | 35 // ApplicationDelegate implementation |
36 virtual bool ConfigureIncomingConnection( | 36 virtual bool ConfigureIncomingConnection( |
37 ApplicationConnection* connection) OVERRIDE { | 37 ApplicationConnection* connection) OVERRIDE { |
38 connection->ConnectToService("mojo:mojo_native_viewport_service", | 38 connection->ConnectToService("mojo:mojo_native_viewport_service", |
39 &viewport_); | 39 &viewport_); |
40 viewport_.set_client(this); | 40 viewport_.set_client(this); |
41 | 41 |
42 // TODO(jamesr): Should be mojo:mojo_gpu_service | |
43 connection->ConnectToService("mojo:mojo_native_viewport_service", | |
44 &gpu_service_); | |
45 | |
46 connection->ConnectToService("mojo:mojo_surfaces_service", | 42 connection->ConnectToService("mojo:mojo_surfaces_service", |
47 &surfaces_service_); | 43 &surfaces_service_); |
48 surfaces_service_->CreateSurfaceConnection(base::Bind( | 44 surfaces_service_->CreateSurfaceConnection(base::Bind( |
49 &SurfacesApp::SurfaceConnectionCreated, base::Unretained(this))); | 45 &SurfacesApp::SurfaceConnectionCreated, base::Unretained(this))); |
50 | 46 |
51 size_ = gfx::Size(800, 600); | 47 size_ = gfx::Size(800, 600); |
52 | 48 |
53 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); | 49 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); |
54 viewport_->Show(); | 50 viewport_->Show(); |
55 | 51 |
56 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); | 52 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); |
57 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); | 53 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); |
58 connection->ConnectToService("mojo:mojo_surfaces_child_gl_app", | 54 connection->ConnectToService("mojo:mojo_surfaces_child_gl_app", |
59 &child_two_); | 55 &child_two_); |
60 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), | 56 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), |
61 Size::From(child_size_), | 57 Size::From(child_size_), |
62 base::Bind(&SurfacesApp::ChildOneProducedFrame, | 58 base::Bind(&SurfacesApp::ChildOneProducedFrame, |
63 base::Unretained(this))); | 59 base::Unretained(this))); |
64 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), | 60 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), |
65 Size::From(child_size_), | 61 Size::From(child_size_), |
66 base::Bind(&SurfacesApp::ChildTwoProducedFrame, | 62 base::Bind(&SurfacesApp::ChildTwoProducedFrame, |
67 base::Unretained(this))); | 63 base::Unretained(this))); |
68 return true; | 64 return true; |
69 } | 65 } |
70 | 66 |
71 void ChildOneProducedFrame(SurfaceIdPtr id) { | 67 void ChildOneProducedFrame(SurfaceIdPtr id) { |
72 child_one_id_ = id.To<cc::SurfaceId>(); | 68 child_one_id_ = id.To<cc::SurfaceId>(); |
73 Draw(10); | |
74 } | 69 } |
75 | 70 |
76 void ChildTwoProducedFrame(SurfaceIdPtr id) { | 71 void ChildTwoProducedFrame(SurfaceIdPtr id) { |
77 child_two_id_ = id.To<cc::SurfaceId>(); | 72 child_two_id_ = id.To<cc::SurfaceId>(); |
78 Draw(10); | |
79 } | 73 } |
80 | 74 |
81 void Draw(int offset) { | 75 void Draw(int offset) { |
82 if (onscreen_id_.is_null() || child_one_id_.is_null() || | |
83 child_two_id_.is_null()) | |
84 return; | |
85 int bounced_offset = offset; | 76 int bounced_offset = offset; |
86 if (offset > 200) | 77 if (offset > 200) |
87 bounced_offset = 400 - offset; | 78 bounced_offset = 400 - offset; |
88 embedder_->ProduceFrame( | 79 embedder_->ProduceFrame( |
89 child_one_id_, child_two_id_, child_size_, size_, bounced_offset); | 80 child_one_id_, child_two_id_, child_size_, size_, bounced_offset); |
90 base::MessageLoop::current()->PostDelayedTask( | 81 base::MessageLoop::current()->PostDelayedTask( |
91 FROM_HERE, | 82 FROM_HERE, |
92 base::Bind( | 83 base::Bind( |
93 &SurfacesApp::Draw, base::Unretained(this), (offset + 2) % 400), | 84 &SurfacesApp::Draw, base::Unretained(this), (offset + 2) % 400), |
94 base::TimeDelta::FromMilliseconds(50)); | 85 base::TimeDelta::FromMilliseconds(50)); |
95 } | 86 } |
96 | 87 |
97 void SurfaceConnectionCreated(SurfacePtr surface, uint32_t id_namespace) { | 88 void SurfaceConnectionCreated(SurfacePtr surface, uint32_t id_namespace) { |
98 surface_ = surface.Pass(); | 89 surface_ = surface.Pass(); |
99 surface_.set_client(this); | 90 surface_.set_client(this); |
100 embedder_.reset(new Embedder(surface_.get())); | 91 embedder_.reset(new Embedder(surface_.get())); |
101 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | 92 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); |
102 CreateSurfaceIfReady(); | |
103 } | |
104 virtual void ReturnResources( | |
105 Array<ReturnedResourcePtr> resources) OVERRIDE { | |
106 DCHECK(!resources.size()); | |
107 } | |
108 | |
109 // NativeViewportClient implementation | |
110 virtual void OnCreated(uint64_t native_viewport_id) OVERRIDE { | |
111 native_viewport_id_ = native_viewport_id; | |
112 CreateSurfaceIfReady(); | |
113 } | |
114 | |
115 // We can't create our GLES2-bound surface until we have our id namespace from | |
116 // the surfaces service and our native viewport id from the native viewport | |
117 // service. There's no way of knowing which we'll get first, so we just start | |
118 // whenever both arrive. | |
119 void CreateSurfaceIfReady() { | |
120 if (!onscreen_id_.is_null()) | |
121 return; | |
122 if (native_viewport_id_ == 0) | |
123 return; | |
124 if (!allocator_) | |
125 return; | |
126 | 93 |
127 onscreen_id_ = allocator_->GenerateId(); | 94 onscreen_id_ = allocator_->GenerateId(); |
128 embedder_->SetSurfaceId(onscreen_id_); | 95 embedder_->SetSurfaceId(onscreen_id_); |
129 CommandBufferPtr cb; | 96 surface_->CreateSurface(SurfaceId::From(onscreen_id_), Size::From(size_)); |
130 gpu_service_->CreateOnscreenGLES2Context( | 97 viewport_->SubmittedFrame(SurfaceId::From(onscreen_id_)); |
131 native_viewport_id_, Size::From(size_), Get(&cb)); | |
132 surface_->CreateGLES2BoundSurface( | |
133 cb.Pass(), SurfaceId::From(onscreen_id_), Size::From(size_)); | |
134 Draw(10); | 98 Draw(10); |
135 } | 99 } |
| 100 |
| 101 // SurfaceClient implementation. |
| 102 virtual void ReturnResources(Array<ReturnedResourcePtr> resources) OVERRIDE { |
| 103 DCHECK(!resources.size()); |
| 104 } |
| 105 // NativeViewportClient implementation. |
| 106 virtual void OnCreated(uint64_t native_viewport_id) OVERRIDE {} |
136 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} | 107 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} |
137 virtual void OnDestroyed() OVERRIDE {} | 108 virtual void OnDestroyed() OVERRIDE {} |
138 virtual void OnEvent(mojo::EventPtr event, | 109 virtual void OnEvent(mojo::EventPtr event, |
139 const mojo::Callback<void()>& callback) OVERRIDE { | 110 const mojo::Callback<void()>& callback) OVERRIDE { |
140 callback.Run(); | 111 callback.Run(); |
141 } | 112 } |
142 | 113 |
143 private: | 114 private: |
144 SurfacesServicePtr surfaces_service_; | 115 SurfacesServicePtr surfaces_service_; |
145 SurfacePtr surface_; | 116 SurfacePtr surface_; |
146 cc::SurfaceId onscreen_id_; | 117 cc::SurfaceId onscreen_id_; |
147 uint64_t native_viewport_id_; | |
148 scoped_ptr<cc::SurfaceIdAllocator> allocator_; | 118 scoped_ptr<cc::SurfaceIdAllocator> allocator_; |
149 scoped_ptr<Embedder> embedder_; | 119 scoped_ptr<Embedder> embedder_; |
150 ChildPtr child_one_; | 120 ChildPtr child_one_; |
151 cc::SurfaceId child_one_id_; | 121 cc::SurfaceId child_one_id_; |
152 ChildPtr child_two_; | 122 ChildPtr child_two_; |
153 cc::SurfaceId child_two_id_; | 123 cc::SurfaceId child_two_id_; |
154 gfx::Size size_; | 124 gfx::Size size_; |
155 gfx::Size child_size_; | 125 gfx::Size child_size_; |
156 | 126 |
157 NativeViewportPtr viewport_; | 127 NativeViewportPtr viewport_; |
158 GpuPtr gpu_service_; | |
159 | 128 |
160 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); | 129 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); |
161 }; | 130 }; |
162 | 131 |
163 } // namespace examples | 132 } // namespace examples |
164 } // namespace mojo | 133 } // namespace mojo |
165 | 134 |
166 MojoResult MojoMain(MojoHandle shell_handle) { | 135 MojoResult MojoMain(MojoHandle shell_handle) { |
167 mojo::ApplicationRunnerChromium runner(new mojo::examples::SurfacesApp); | 136 mojo::ApplicationRunnerChromium runner(new mojo::examples::SurfacesApp); |
168 return runner.Run(shell_handle); | 137 return runner.Run(shell_handle); |
169 } | 138 } |
OLD | NEW |