OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/renderer/render_widget_mus_connection.h" | 5 #include "content/renderer/render_widget_mus_connection.h" |
6 | 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/lazy_instance.h" |
| 10 #include "components/mus/public/cpp/context_provider.h" |
| 11 #include "components/mus/public/cpp/output_surface.h" |
| 12 #include "components/mus/public/interfaces/command_buffer.mojom.h" |
7 #include "components/mus/public/interfaces/compositor_frame.mojom.h" | 13 #include "components/mus/public/interfaces/compositor_frame.mojom.h" |
| 14 #include "components/mus/public/interfaces/gpu.mojom.h" |
8 #include "components/mus/public/interfaces/window_tree.mojom.h" | 15 #include "components/mus/public/interfaces/window_tree.mojom.h" |
9 #include "content/public/common/mojo_shell_connection.h" | 16 #include "content/public/common/mojo_shell_connection.h" |
10 #include "mojo/application/public/cpp/application_impl.h" | 17 #include "mojo/application/public/cpp/application_impl.h" |
11 #include "mojo/converters/geometry/geometry_type_converters.h" | 18 #include "mojo/converters/geometry/geometry_type_converters.h" |
12 #include "mojo/converters/surfaces/surfaces_utils.h" | 19 #include "mojo/converters/surfaces/surfaces_utils.h" |
13 | 20 |
14 namespace content { | 21 namespace content { |
15 | 22 |
16 RenderWidgetMusConnection::RenderWidgetMusConnection( | 23 namespace { |
17 int routing_id, | 24 |
18 mojo::InterfaceRequest<mus::mojom::WindowTreeClient> request) | 25 typedef std::map<int, RenderWidgetMusConnection*> ConnectionMap; |
| 26 base::LazyInstance<ConnectionMap>::Leaky g_connections = |
| 27 LAZY_INSTANCE_INITIALIZER; |
| 28 } |
| 29 |
| 30 RenderWidgetMusConnection::RenderWidgetMusConnection(int routing_id) |
19 : routing_id_(routing_id), root_(nullptr) { | 31 : routing_id_(routing_id), root_(nullptr) { |
20 // TODO(fsamuel): We should probably introduce a | 32 DCHECK(routing_id); |
21 // RenderWidgetMusConnection::FromRoutingID that's usable from RenderWidget. | 33 } |
22 // TODO(fsamuel): We probably want to pause processing of incoming | 34 |
23 // messages until we have an associated RenderWidget. | 35 RenderWidgetMusConnection::~RenderWidgetMusConnection() {} |
| 36 |
| 37 void RenderWidgetMusConnection::Bind( |
| 38 mojo::InterfaceRequest<mus::mojom::WindowTreeClient> request) { |
| 39 DCHECK(!root_); |
24 mus::WindowTreeConnection::Create( | 40 mus::WindowTreeConnection::Create( |
25 this, request.Pass(), | 41 this, request.Pass(), |
26 mus::WindowTreeConnection::CreateType::DONT_WAIT_FOR_EMBED); | 42 mus::WindowTreeConnection::CreateType::DONT_WAIT_FOR_EMBED); |
27 } | 43 } |
28 | 44 |
29 RenderWidgetMusConnection::~RenderWidgetMusConnection() {} | 45 scoped_ptr<cc::OutputSurface> RenderWidgetMusConnection::CreateOutputSurface() { |
| 46 DCHECK(!window_surface_binding_); |
| 47 mus::mojom::GpuPtr gpu_service; |
| 48 MojoShellConnection::Get()->GetApplication()->ConnectToService("mojo:mus", |
| 49 &gpu_service); |
| 50 mus::mojom::CommandBufferPtr cb; |
| 51 gpu_service->CreateOffscreenGLES2Context(GetProxy(&cb)); |
| 52 scoped_refptr<cc::ContextProvider> context_provider( |
| 53 new mus::ContextProvider(cb.PassInterface().PassHandle())); |
| 54 scoped_ptr<cc::OutputSurface> output_surface(new mus::OutputSurface( |
| 55 context_provider, mus::WindowSurface::Create(&window_surface_binding_))); |
| 56 if (root_) { |
| 57 root_->AttachSurface(mus::mojom::SURFACE_TYPE_DEFAULT, |
| 58 window_surface_binding_.Pass()); |
| 59 } |
| 60 return output_surface.Pass(); |
| 61 } |
30 | 62 |
31 void RenderWidgetMusConnection::SubmitCompositorFrame() { | 63 // static |
32 const gfx::Rect bounds(root_->bounds()); | 64 RenderWidgetMusConnection* RenderWidgetMusConnection::GetOrCreate( |
33 mus::mojom::PassPtr pass = mojo::CreateDefaultPass(1, bounds); | 65 int routing_id) { |
34 mus::mojom::CompositorFramePtr frame = mus::mojom::CompositorFrame::New(); | 66 auto it = g_connections.Get().find(routing_id); |
| 67 if (it != g_connections.Get().end()) |
| 68 return it->second; |
35 | 69 |
36 mus::mojom::CompositorFrameMetadataPtr meta = | 70 RenderWidgetMusConnection* connection = |
37 mus::mojom::CompositorFrameMetadata::New(); | 71 new RenderWidgetMusConnection(routing_id); |
38 meta->device_scale_factor = 1.0f; | 72 g_connections.Get().insert(std::make_pair(routing_id, connection)); |
39 frame->metadata = meta.Pass(); | 73 return connection; |
40 | |
41 frame->resources.resize(0u); | |
42 | |
43 pass->quads.resize(0u); | |
44 pass->shared_quad_states.push_back(mojo::CreateDefaultSQS(bounds.size())); | |
45 | |
46 mus::mojom::QuadPtr quad = mus::mojom::Quad::New(); | |
47 quad->material = mus::mojom::MATERIAL_SOLID_COLOR; | |
48 quad->rect = mojo::Rect::From(bounds); | |
49 quad->opaque_rect = mojo::Rect::New(); | |
50 quad->visible_rect = mojo::Rect::From(bounds); | |
51 quad->needs_blending = false; | |
52 quad->shared_quad_state_index = 0u; | |
53 | |
54 mus::mojom::SolidColorQuadStatePtr color_state = | |
55 mus::mojom::SolidColorQuadState::New(); | |
56 color_state->color = mus::mojom::Color::New(); | |
57 color_state->color->rgba = 0xff00ff00; | |
58 color_state->force_anti_aliasing_off = false; | |
59 | |
60 quad->solid_color_quad_state = color_state.Pass(); | |
61 pass->quads.push_back(quad.Pass()); | |
62 frame->passes.push_back(pass.Pass()); | |
63 surface_->SubmitCompositorFrame(frame.Pass(), mojo::Closure()); | |
64 } | 74 } |
65 | 75 |
66 void RenderWidgetMusConnection::OnConnectionLost( | 76 void RenderWidgetMusConnection::OnConnectionLost( |
67 mus::WindowTreeConnection* connection) { | 77 mus::WindowTreeConnection* connection) { |
| 78 g_connections.Get().erase(routing_id_); |
68 delete this; | 79 delete this; |
69 } | 80 } |
70 | 81 |
71 void RenderWidgetMusConnection::OnEmbed(mus::Window* root) { | 82 void RenderWidgetMusConnection::OnEmbed(mus::Window* root) { |
72 root_ = root; | 83 root_ = root; |
73 root_->AddObserver(this); | 84 root_->AddObserver(this); |
74 surface_ = root_->RequestSurface(mus::mojom::SURFACE_TYPE_DEFAULT); | 85 if (window_surface_binding_) { |
75 surface_->BindToThread(); | 86 root->AttachSurface(mus::mojom::SURFACE_TYPE_DEFAULT, |
76 SubmitCompositorFrame(); | 87 window_surface_binding_.Pass()); |
| 88 } |
77 } | 89 } |
78 | 90 |
79 void RenderWidgetMusConnection::OnUnembed() {} | 91 void RenderWidgetMusConnection::OnUnembed() {} |
80 | 92 |
81 void RenderWidgetMusConnection::OnWindowBoundsChanged( | 93 void RenderWidgetMusConnection::OnWindowBoundsChanged( |
82 mus::Window* window, | 94 mus::Window* window, |
83 const gfx::Rect& old_bounds, | 95 const gfx::Rect& old_bounds, |
84 const gfx::Rect& new_bounds) { | 96 const gfx::Rect& new_bounds) { |
85 SubmitCompositorFrame(); | |
86 } | 97 } |
87 | 98 |
88 } // namespace content | 99 } // namespace content |
OLD | NEW |