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.
5 #include "components/view_manager/display_manager.h"
7 #include "base/numerics/safe_conversions.h"
8 #include "components/view_manager/display_manager_factory.h"
9 #include "components/view_manager/gles2/gpu_state.h"
10 #include "components/view_manager/native_viewport/onscreen_context_provider.h"
11 #include "components/view_manager/public/interfaces/gpu.mojom.h"
12 #include "components/view_manager/public/interfaces/quads.mojom.h"
13 #include "components/view_manager/public/interfaces/surfaces.mojom.h"
14 #include "components/view_manager/server_view.h"
15 #include "components/view_manager/view_coordinate_conversions.h"
16 #include "mojo/application/public/cpp/application_connection.h"
17 #include "mojo/application/public/cpp/application_impl.h"
18 #include "mojo/converters/geometry/geometry_type_converters.h"
19 #include "mojo/converters/surfaces/surfaces_type_converters.h"
20 #include "mojo/converters/surfaces/surfaces_utils.h"
21 #include "mojo/converters/transform/transform_type_converters.h"
26 namespace view_manager
{
29 void DrawViewTree(mojo::Pass
* pass
,
30 const ServerView
* view
,
31 const gfx::Vector2d
& parent_to_root_origin_offset
,
36 const gfx::Rect absolute_bounds
=
37 view
->bounds() + parent_to_root_origin_offset
;
38 std::vector
<const ServerView
*> children(view
->GetChildren());
39 const float combined_opacity
= opacity
* view
->opacity();
40 for (std::vector
<const ServerView
*>::reverse_iterator it
= children
.rbegin();
41 it
!= children
.rend();
43 DrawViewTree(pass
, *it
, absolute_bounds
.OffsetFromOrigin(),
47 cc::SurfaceId node_id
= view
->surface_id();
49 auto surface_quad_state
= mojo::SurfaceQuadState::New();
50 surface_quad_state
->surface
= mojo::SurfaceId::From(node_id
);
52 gfx::Transform node_transform
;
53 node_transform
.Translate(absolute_bounds
.x(), absolute_bounds
.y());
55 const gfx::Rect
bounds_at_origin(view
->bounds().size());
56 auto surface_quad
= mojo::Quad::New();
57 surface_quad
->material
= mojo::Material::MATERIAL_SURFACE_CONTENT
;
58 surface_quad
->rect
= Rect::From(bounds_at_origin
);
59 surface_quad
->opaque_rect
= Rect::From(bounds_at_origin
);
60 surface_quad
->visible_rect
= Rect::From(bounds_at_origin
);
61 surface_quad
->needs_blending
= true;
62 surface_quad
->shared_quad_state_index
=
63 base::saturated_cast
<int32_t>(pass
->shared_quad_states
.size());
64 surface_quad
->surface_quad_state
= surface_quad_state
.Pass();
66 auto sqs
= mojo::CreateDefaultSQS(view
->bounds().size());
67 sqs
->blend_mode
= mojo::SK_XFERMODE_kSrcOver_Mode
;
68 sqs
->opacity
= combined_opacity
;
69 sqs
->quad_to_target_transform
= mojo::Transform::From(node_transform
);
71 pass
->quads
.push_back(surface_quad
.Pass());
72 pass
->shared_quad_states
.push_back(sqs
.Pass());
78 DisplayManagerFactory
* DisplayManager::factory_
= nullptr;
81 DisplayManager
* DisplayManager::Create(
83 mojo::ApplicationImpl
* app_impl
,
84 const scoped_refptr
<gles2::GpuState
>& gpu_state
) {
86 return factory_
->CreateDisplayManager(is_headless
, app_impl
, gpu_state
);
87 return new DefaultDisplayManager(is_headless
, app_impl
, gpu_state
);
90 DefaultDisplayManager::DefaultDisplayManager(
92 mojo::ApplicationImpl
* app_impl
,
93 const scoped_refptr
<gles2::GpuState
>& gpu_state
)
94 : is_headless_(is_headless
),
96 gpu_state_(gpu_state
),
98 draw_timer_(false, false),
99 frame_pending_(false),
101 new native_viewport::OnscreenContextProvider(gpu_state
)),
102 weak_factory_(this) {
103 metrics_
.size_in_pixels
= mojo::Size::New();
104 metrics_
.size_in_pixels
->width
= 800;
105 metrics_
.size_in_pixels
->height
= 600;
108 void DefaultDisplayManager::Init(DisplayManagerDelegate
* delegate
) {
109 delegate_
= delegate
;
112 native_viewport::PlatformViewport::Create(this, is_headless_
).Pass();
113 platform_viewport_
->Init(gfx::Rect(metrics_
.size_in_pixels
.To
<gfx::Size
>()));
114 platform_viewport_
->Show();
116 mojo::ContextProviderPtr context_provider
;
117 context_provider_
->Bind(GetProxy(&context_provider
).Pass());
118 mojo::DisplayFactoryPtr display_factory
;
119 mojo::URLRequestPtr
request(mojo::URLRequest::New());
120 request
->url
= mojo::String::From("mojo:surfaces_service");
121 app_impl_
->ConnectToService(request
.Pass(), &display_factory
);
122 display_factory
->Create(context_provider
.Pass(),
123 nullptr, // returner - we never submit resources.
124 GetProxy(&display_
));
127 DefaultDisplayManager::~DefaultDisplayManager() {
128 // Destroy before |platform_viewport_| because this will destroy
129 // CommandBufferDriver objects that contain child windows. Otherwise if this
130 // class destroys its window first, X errors will occur.
131 context_provider_
.reset();
133 // Destroy the NativeViewport early on as it may call us back during
134 // destruction and we want to be in a known state.
135 platform_viewport_
.reset();
138 void DefaultDisplayManager::SchedulePaint(const ServerView
* view
,
139 const gfx::Rect
& bounds
) {
141 if (!view
->IsDrawn())
143 const gfx::Rect root_relative_rect
=
144 ConvertRectBetweenViews(view
, delegate_
->GetRootView(), bounds
);
145 if (root_relative_rect
.IsEmpty())
147 dirty_rect_
.Union(root_relative_rect
);
151 void DefaultDisplayManager::SetViewportSize(const gfx::Size
& size
) {
152 platform_viewport_
->SetBounds(gfx::Rect(size
));
155 const mojo::ViewportMetrics
& DefaultDisplayManager::GetViewportMetrics() {
159 void DefaultDisplayManager::Draw() {
160 gfx::Rect
rect(metrics_
.size_in_pixels
.To
<gfx::Size
>());
161 auto pass
= mojo::CreateDefaultPass(1, rect
);
162 pass
->damage_rect
= Rect::From(dirty_rect_
);
164 DrawViewTree(pass
.get(), delegate_
->GetRootView(), gfx::Vector2d(), 1.0f
);
166 auto frame
= mojo::Frame::New();
167 frame
->passes
.push_back(pass
.Pass());
168 frame
->resources
.resize(0u);
169 frame_pending_
= true;
170 display_
->SubmitFrame(
172 base::Bind(&DefaultDisplayManager::DidDraw
, base::Unretained(this)));
173 dirty_rect_
= gfx::Rect();
176 void DefaultDisplayManager::DidDraw() {
177 frame_pending_
= false;
178 if (!dirty_rect_
.IsEmpty())
182 void DefaultDisplayManager::WantToDraw() {
183 if (draw_timer_
.IsRunning() || frame_pending_
)
187 FROM_HERE
, base::TimeDelta(),
188 base::Bind(&DefaultDisplayManager::Draw
, base::Unretained(this)));
191 void DefaultDisplayManager::OnAcceleratedWidgetAvailable(
192 gfx::AcceleratedWidget widget
,
193 float device_pixel_ratio
) {
194 context_provider_
->SetAcceleratedWidget(widget
);
195 OnMetricsChanged(metrics_
.size_in_pixels
.To
<gfx::Size
>(), device_pixel_ratio
);
198 void DefaultDisplayManager::OnAcceleratedWidgetDestroyed() {
199 context_provider_
->SetAcceleratedWidget(gfx::kNullAcceleratedWidget
);
202 void DefaultDisplayManager::OnEvent(mojo::EventPtr event
) {
203 delegate_
->OnEvent(event
.Pass());
206 void DefaultDisplayManager::OnMetricsChanged(const gfx::Size
& size
,
207 float device_scale_factor
) {
208 if ((metrics_
.size_in_pixels
.To
<gfx::Size
>() == size
) &&
209 (metrics_
.device_pixel_ratio
== device_scale_factor
)) {
213 mojo::ViewportMetrics metrics
;
214 metrics
.size_in_pixels
= mojo::Size::From(size
);
215 metrics
.device_pixel_ratio
= device_scale_factor
;
217 delegate_
->GetRootView()->SetBounds(gfx::Rect(size
));
218 delegate_
->OnViewportMetricsChanged(metrics_
, metrics
);
220 metrics_
.size_in_pixels
= metrics
.size_in_pixels
.Clone();
221 metrics_
.device_pixel_ratio
= metrics
.device_pixel_ratio
;
224 void DefaultDisplayManager::OnDestroyed() {
225 delegate_
->OnDisplayClosed();
228 } // namespace view_manager