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 "mojo/aura/context_factory_mojo.h" | 9 #include "mojo/aura/context_factory_mojo.h" |
10 #include "mojo/aura/screen_mojo.h" | 10 #include "mojo/aura/screen_mojo.h" |
11 #include "mojo/aura/window_tree_host_mojo.h" | 11 #include "mojo/aura/window_tree_host_mojo.h" |
12 #include "mojo/aura/window_tree_host_mojo_delegate.h" | 12 #include "mojo/aura/window_tree_host_mojo_delegate.h" |
13 #include "mojo/public/cpp/application/application.h" | 13 #include "mojo/public/cpp/application/application.h" |
14 #include "mojo/public/cpp/system/core.h" | 14 #include "mojo/public/cpp/system/core.h" |
15 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" | 15 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
| 16 #include "mojo/services/public/cpp/view_manager/node.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
16 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" | 20 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" |
17 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" | |
18 #include "ui/aura/client/default_capture_client.h" | 21 #include "ui/aura/client/default_capture_client.h" |
19 #include "ui/aura/client/window_tree_client.h" | 22 #include "ui/aura/client/window_tree_client.h" |
20 #include "ui/aura/env.h" | 23 #include "ui/aura/env.h" |
21 #include "ui/aura/window.h" | 24 #include "ui/aura/window.h" |
22 #include "ui/aura/window_delegate.h" | 25 #include "ui/aura/window_delegate.h" |
23 #include "ui/base/hit_test.h" | 26 #include "ui/base/hit_test.h" |
24 #include "ui/gfx/canvas.h" | 27 #include "ui/gfx/canvas.h" |
25 #include "ui/gfx/codec/png_codec.h" | 28 #include "ui/gfx/codec/png_codec.h" |
26 | 29 |
27 namespace mojo { | 30 namespace mojo { |
28 namespace examples { | 31 namespace examples { |
29 | 32 |
30 void OnSetViewContentsDone(bool value) { | |
31 VLOG(1) << "OnSetViewContentsDone " << value; | |
32 DCHECK(value); | |
33 } | |
34 | |
35 bool CreateMapAndDupSharedBuffer(size_t size, | |
36 void** memory, | |
37 ScopedSharedBufferHandle* handle, | |
38 ScopedSharedBufferHandle* duped) { | |
39 MojoResult result = CreateSharedBuffer(NULL, size, handle); | |
40 if (result != MOJO_RESULT_OK) | |
41 return false; | |
42 DCHECK(handle->is_valid()); | |
43 | |
44 result = DuplicateBuffer(handle->get(), NULL, duped); | |
45 if (result != MOJO_RESULT_OK) | |
46 return false; | |
47 DCHECK(duped->is_valid()); | |
48 | |
49 result = MapBuffer( | |
50 handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE); | |
51 if (result != MOJO_RESULT_OK) | |
52 return false; | |
53 DCHECK(*memory); | |
54 | |
55 return true; | |
56 } | |
57 | |
58 // Trivial WindowDelegate implementation that draws a colored background. | 33 // Trivial WindowDelegate implementation that draws a colored background. |
59 class DemoWindowDelegate : public aura::WindowDelegate { | 34 class DemoWindowDelegate : public aura::WindowDelegate { |
60 public: | 35 public: |
61 explicit DemoWindowDelegate(SkColor color) : color_(color) {} | 36 explicit DemoWindowDelegate(SkColor color) : color_(color) {} |
62 | 37 |
63 // Overridden from WindowDelegate: | 38 // Overridden from WindowDelegate: |
64 virtual gfx::Size GetMinimumSize() const OVERRIDE { | 39 virtual gfx::Size GetMinimumSize() const OVERRIDE { |
65 return gfx::Size(); | 40 return gfx::Size(); |
66 } | 41 } |
67 | 42 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 return window_; | 96 return window_; |
122 } | 97 } |
123 | 98 |
124 private: | 99 private: |
125 aura::Window* window_; | 100 aura::Window* window_; |
126 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_; | 101 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_; |
127 | 102 |
128 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient); | 103 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient); |
129 }; | 104 }; |
130 | 105 |
131 class AuraDemo; | 106 class AuraDemo : public Application, |
132 | 107 public WindowTreeHostMojoDelegate, |
133 // Trivial ViewManagerClient implementation. Forwards to AuraDemo when | 108 public view_manager::ViewManagerDelegate { |
134 // connection established. | |
135 class ViewManagerClientImpl | |
136 : public InterfaceImpl<view_manager::ViewManagerClient> { | |
137 public: | |
138 explicit ViewManagerClientImpl(AuraDemo* aura_demo) | |
139 : aura_demo_(aura_demo) {} | |
140 virtual ~ViewManagerClientImpl() {} | |
141 | |
142 private: | |
143 void OnResult(bool result) { | |
144 VLOG(1) << "ViewManagerClientImpl::::OnResult result=" << result; | |
145 DCHECK(result); | |
146 } | |
147 | |
148 // ViewManagerClient: | |
149 virtual void OnViewManagerConnectionEstablished( | |
150 uint16_t connection_id, | |
151 const String& creator_url, | |
152 uint32_t next_server_change_id, | |
153 mojo::Array<view_manager::NodeDataPtr> nodes) OVERRIDE; | |
154 virtual void OnRootsAdded(Array<view_manager::NodeDataPtr> nodes) OVERRIDE { | |
155 NOTREACHED(); | |
156 } | |
157 virtual void OnServerChangeIdAdvanced( | |
158 uint32_t next_server_change_id) OVERRIDE { | |
159 } | |
160 virtual void OnNodeBoundsChanged(uint32_t node, | |
161 mojo::RectPtr old_bounds, | |
162 mojo::RectPtr new_bounds) OVERRIDE { | |
163 } | |
164 virtual void OnNodeHierarchyChanged( | |
165 uint32_t node, | |
166 uint32_t new_parent, | |
167 uint32_t old_parent, | |
168 uint32_t server_change_id, | |
169 mojo::Array<view_manager::NodeDataPtr> nodes) OVERRIDE { | |
170 } | |
171 virtual void OnNodeReordered( | |
172 uint32_t node_id, | |
173 uint32_t relative_node_id, | |
174 view_manager::OrderDirection direction, | |
175 uint32_t server_change_id) OVERRIDE { | |
176 } | |
177 virtual void OnNodeDeleted(uint32_t node, uint32_t server_change_id) | |
178 OVERRIDE { | |
179 } | |
180 virtual void OnNodeViewReplaced(uint32_t node, | |
181 uint32_t new_view_id, | |
182 uint32_t old_view_id) OVERRIDE { | |
183 } | |
184 virtual void OnViewDeleted(uint32_t view) OVERRIDE { | |
185 } | |
186 virtual void OnViewInputEvent(uint32_t view_id, | |
187 EventPtr event, | |
188 const Callback<void()>& callback) OVERRIDE { | |
189 } | |
190 virtual void DispatchOnViewInputEvent(uint32_t view_id, | |
191 EventPtr event) OVERRIDE { | |
192 } | |
193 | |
194 AuraDemo* aura_demo_; | |
195 | |
196 DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl); | |
197 }; | |
198 | |
199 class AuraDemo : public Application, public WindowTreeHostMojoDelegate { | |
200 public: | 109 public: |
201 AuraDemo() | 110 AuraDemo() |
202 : view_manager_(NULL), | 111 : window1_(NULL), |
203 window1_(NULL), | |
204 window2_(NULL), | 112 window2_(NULL), |
205 window21_(NULL), | 113 window21_(NULL), |
206 view_id_(0) { | 114 view_(NULL) { |
207 AddService<ViewManagerClientImpl>(this); | 115 view_manager::ViewManager::Create(this, this); |
208 } | 116 } |
209 virtual ~AuraDemo() {} | 117 virtual ~AuraDemo() {} |
210 | 118 |
211 void SetRoot(view_manager::ViewManagerService* view_manager, | 119 private: |
212 uint32_t view_id) { | 120 // Overridden from view_manager::ViewManagerDelegate: |
213 aura::Env::CreateInstance(true); | 121 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
214 view_manager_ = view_manager; | 122 view_manager::Node* root) OVERRIDE { |
215 view_id_ = view_id; | 123 // TODO(beng): this function could be called multiple times! |
216 context_factory_.reset(new ContextFactoryMojo); | 124 view_ = view_manager::View::Create(view_manager); |
217 aura::Env::GetInstance()->set_context_factory(context_factory_.get()); | 125 root->SetActiveView(view_); |
218 screen_.reset(ScreenMojo::Create()); | |
219 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); | |
220 | 126 |
221 window_tree_host_.reset(new WindowTreeHostMojo(gfx::Rect(800, 600), this)); | 127 window_tree_host_.reset(new WindowTreeHostMojo(root, this)); |
222 window_tree_host_->InitHost(); | 128 window_tree_host_->InitHost(); |
223 | 129 |
224 window_tree_client_.reset( | 130 window_tree_client_.reset( |
225 new DemoWindowTreeClient(window_tree_host_->window())); | 131 new DemoWindowTreeClient(window_tree_host_->window())); |
226 | 132 |
227 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE)); | 133 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE)); |
228 window1_ = new aura::Window(delegate1_.get()); | 134 window1_ = new aura::Window(delegate1_.get()); |
229 window1_->Init(aura::WINDOW_LAYER_TEXTURED); | 135 window1_->Init(aura::WINDOW_LAYER_TEXTURED); |
230 window1_->SetBounds(gfx::Rect(100, 100, 400, 400)); | 136 window1_->SetBounds(gfx::Rect(100, 100, 400, 400)); |
231 window1_->Show(); | 137 window1_->Show(); |
(...skipping 11 matching lines...) Expand all Loading... |
243 window21_->Init(aura::WINDOW_LAYER_TEXTURED); | 149 window21_->Init(aura::WINDOW_LAYER_TEXTURED); |
244 window21_->SetBounds(gfx::Rect(10, 10, 50, 50)); | 150 window21_->SetBounds(gfx::Rect(10, 10, 50, 50)); |
245 window21_->Show(); | 151 window21_->Show(); |
246 window2_->AddChild(window21_); | 152 window2_->AddChild(window21_); |
247 | 153 |
248 window_tree_host_->Show(); | 154 window_tree_host_->Show(); |
249 } | 155 } |
250 | 156 |
251 // WindowTreeHostMojoDelegate: | 157 // WindowTreeHostMojoDelegate: |
252 virtual void CompositorContentsChanged(const SkBitmap& bitmap) OVERRIDE { | 158 virtual void CompositorContentsChanged(const SkBitmap& bitmap) OVERRIDE { |
253 std::vector<unsigned char> data; | 159 view_->SetContents(bitmap); |
254 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data); | |
255 | |
256 void* memory = NULL; | |
257 ScopedSharedBufferHandle duped; | |
258 bool result = CreateMapAndDupSharedBuffer(data.size(), | |
259 &memory, | |
260 &shared_state_handle_, | |
261 &duped); | |
262 if (!result) | |
263 return; | |
264 | |
265 memcpy(memory, &data[0], data.size()); | |
266 | |
267 view_manager_->SetViewContents( | |
268 view_id_, duped.Pass(), static_cast<uint32_t>(data.size()), | |
269 base::Bind(&OnSetViewContentsDone)); | |
270 } | 160 } |
271 | 161 |
272 virtual void Initialize() OVERRIDE { | 162 virtual void Initialize() OVERRIDE { |
| 163 aura::Env::CreateInstance(true); |
| 164 context_factory_.reset(new ContextFactoryMojo); |
| 165 aura::Env::GetInstance()->set_context_factory(context_factory_.get()); |
| 166 screen_.reset(ScreenMojo::Create()); |
| 167 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); |
273 } | 168 } |
274 | 169 |
275 scoped_ptr<DemoWindowTreeClient> window_tree_client_; | 170 scoped_ptr<DemoWindowTreeClient> window_tree_client_; |
276 | 171 |
277 scoped_ptr<ui::ContextFactory> context_factory_; | 172 scoped_ptr<ui::ContextFactory> context_factory_; |
278 | 173 |
279 scoped_ptr<ScreenMojo> screen_; | 174 scoped_ptr<ScreenMojo> screen_; |
280 | 175 |
281 scoped_ptr<DemoWindowDelegate> delegate1_; | 176 scoped_ptr<DemoWindowDelegate> delegate1_; |
282 scoped_ptr<DemoWindowDelegate> delegate2_; | 177 scoped_ptr<DemoWindowDelegate> delegate2_; |
283 scoped_ptr<DemoWindowDelegate> delegate21_; | 178 scoped_ptr<DemoWindowDelegate> delegate21_; |
284 | 179 |
285 view_manager::ViewManagerService* view_manager_; | |
286 | |
287 aura::Window* window1_; | 180 aura::Window* window1_; |
288 aura::Window* window2_; | 181 aura::Window* window2_; |
289 aura::Window* window21_; | 182 aura::Window* window21_; |
290 | 183 |
291 uint32_t view_id_; | 184 view_manager::View* view_; |
292 | 185 |
293 scoped_ptr<aura::WindowTreeHost> window_tree_host_; | 186 scoped_ptr<aura::WindowTreeHost> window_tree_host_; |
294 | 187 |
295 ScopedSharedBufferHandle shared_state_handle_; | |
296 | |
297 DISALLOW_COPY_AND_ASSIGN(AuraDemo); | 188 DISALLOW_COPY_AND_ASSIGN(AuraDemo); |
298 }; | 189 }; |
299 | 190 |
300 void ViewManagerClientImpl::OnViewManagerConnectionEstablished( | |
301 uint16_t connection_id, | |
302 const String& creator_url, | |
303 uint32_t next_server_change_id, | |
304 mojo::Array<view_manager::NodeDataPtr> nodes) { | |
305 const uint32_t view_id = connection_id << 16 | 1; | |
306 client()->CreateView(view_id, base::Bind(&ViewManagerClientImpl::OnResult, | |
307 base::Unretained(this))); | |
308 client()->SetView(nodes[0]->node_id, view_id, | |
309 base::Bind(&ViewManagerClientImpl::OnResult, | |
310 base::Unretained(this))); | |
311 | |
312 aura_demo_->SetRoot(client(), view_id); | |
313 } | |
314 | |
315 } // namespace examples | 191 } // namespace examples |
316 | 192 |
317 // static | 193 // static |
318 Application* Application::Create() { | 194 Application* Application::Create() { |
319 return new examples::AuraDemo(); | 195 return new examples::AuraDemo(); |
320 } | 196 } |
321 | 197 |
322 } // namespace mojo | 198 } // namespace mojo |
OLD | NEW |