Disable failing PartnerDisableIncognitoModeIntegrationTest#testEnabledParentalControl...
[chromium-blink-merge.git] / components / view_manager / display_manager.cc
blobcf3f113ac4f31c5c2810402a5b909d45ad2276f9
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/input_events/input_events_type_converters.h"
20 #include "mojo/converters/input_events/mojo_extended_key_event_data.h"
21 #include "mojo/converters/surfaces/surfaces_type_converters.h"
22 #include "mojo/converters/surfaces/surfaces_utils.h"
23 #include "mojo/converters/transform/transform_type_converters.h"
24 #include "ui/events/event.h"
25 #include "ui/events/event_utils.h"
26 #include "ui/platform_window/platform_window.h"
27 #include "ui/platform_window/stub/stub_window.h"
29 #if defined(OS_WIN)
30 #include "ui/platform_window/win/win_window.h"
31 #elif defined(USE_X11)
32 #include "ui/platform_window/x11/x11_window.h"
33 #elif defined(OS_ANDROID)
34 #include "ui/platform_window/android/platform_window_android.h"
35 #endif
37 using mojo::Rect;
38 using mojo::Size;
40 namespace view_manager {
41 namespace {
43 void DrawViewTree(mojo::Pass* pass,
44 const ServerView* view,
45 const gfx::Vector2d& parent_to_root_origin_offset,
46 float opacity) {
47 if (!view->visible())
48 return;
50 const gfx::Rect absolute_bounds =
51 view->bounds() + parent_to_root_origin_offset;
52 std::vector<const ServerView*> children(view->GetChildren());
53 const float combined_opacity = opacity * view->opacity();
54 for (std::vector<const ServerView*>::reverse_iterator it = children.rbegin();
55 it != children.rend();
56 ++it) {
57 DrawViewTree(pass, *it, absolute_bounds.OffsetFromOrigin(),
58 combined_opacity);
61 cc::SurfaceId node_id = view->surface_id();
63 auto surface_quad_state = mojo::SurfaceQuadState::New();
64 surface_quad_state->surface = mojo::SurfaceId::From(node_id);
66 gfx::Transform node_transform;
67 node_transform.Translate(absolute_bounds.x(), absolute_bounds.y());
69 const gfx::Rect bounds_at_origin(view->bounds().size());
70 auto surface_quad = mojo::Quad::New();
71 surface_quad->material = mojo::Material::MATERIAL_SURFACE_CONTENT;
72 surface_quad->rect = Rect::From(bounds_at_origin);
73 surface_quad->opaque_rect = Rect::From(bounds_at_origin);
74 surface_quad->visible_rect = Rect::From(bounds_at_origin);
75 surface_quad->needs_blending = true;
76 surface_quad->shared_quad_state_index =
77 base::saturated_cast<int32_t>(pass->shared_quad_states.size());
78 surface_quad->surface_quad_state = surface_quad_state.Pass();
80 auto sqs = mojo::CreateDefaultSQS(view->bounds().size());
81 sqs->blend_mode = mojo::SK_XFERMODE_kSrcOver_Mode;
82 sqs->opacity = combined_opacity;
83 sqs->quad_to_target_transform = mojo::Transform::From(node_transform);
85 pass->quads.push_back(surface_quad.Pass());
86 pass->shared_quad_states.push_back(sqs.Pass());
89 float ConvertUIWheelValueToMojoValue(int offset) {
90 // Mojo's event type takes a value between -1 and 1. Normalize by allowing
91 // up to 20 of ui's offset. This is a bit arbitrary.
92 return std::max(
93 -1.0f, std::min(1.0f, static_cast<float>(offset) /
94 (20 * static_cast<float>(
95 ui::MouseWheelEvent::kWheelDelta))));
98 } // namespace
100 // static
101 DisplayManagerFactory* DisplayManager::factory_ = nullptr;
103 // static
104 DisplayManager* DisplayManager::Create(
105 bool is_headless,
106 mojo::ApplicationImpl* app_impl,
107 const scoped_refptr<gles2::GpuState>& gpu_state) {
108 if (factory_)
109 return factory_->CreateDisplayManager(is_headless, app_impl, gpu_state);
110 return new DefaultDisplayManager(is_headless, app_impl, gpu_state);
113 DefaultDisplayManager::DefaultDisplayManager(
114 bool is_headless,
115 mojo::ApplicationImpl* app_impl,
116 const scoped_refptr<gles2::GpuState>& gpu_state)
117 : is_headless_(is_headless),
118 app_impl_(app_impl),
119 gpu_state_(gpu_state),
120 delegate_(nullptr),
121 draw_timer_(false, false),
122 frame_pending_(false),
123 context_provider_(
124 new native_viewport::OnscreenContextProvider(gpu_state)),
125 weak_factory_(this) {
126 metrics_.size_in_pixels = mojo::Size::New();
127 metrics_.size_in_pixels->width = 800;
128 metrics_.size_in_pixels->height = 600;
131 void DefaultDisplayManager::Init(DisplayManagerDelegate* delegate) {
132 delegate_ = delegate;
134 gfx::Rect bounds(metrics_.size_in_pixels.To<gfx::Size>());
135 if (is_headless_) {
136 platform_window_.reset(new ui::StubWindow(this));
137 } else {
138 #if defined(OS_WIN)
139 platform_window_.reset(new ui::WinWindow(this, bounds));
140 #elif defined(USE_X11)
141 platform_window_.reset(new ui::X11Window(this));
142 #elif defined(OS_ANDROID)
143 platform_window_.reset(new ui::PlatformWindowAndroid(this));
144 #else
145 NOTREACHED() << "Unsupported platform";
146 #endif
148 platform_window_->SetBounds(bounds);
149 platform_window_->Show();
151 mojo::ContextProviderPtr context_provider;
152 context_provider_->Bind(GetProxy(&context_provider).Pass());
153 mojo::DisplayFactoryPtr display_factory;
154 mojo::URLRequestPtr request(mojo::URLRequest::New());
155 request->url = mojo::String::From("mojo:surfaces_service");
156 app_impl_->ConnectToService(request.Pass(), &display_factory);
157 // TODO(fsamuel): We should indicate to the delegate that this object failed
158 // to initialize.
159 if (!display_factory)
160 return;
161 display_factory->Create(context_provider.Pass(),
162 nullptr, // returner - we never submit resources.
163 GetProxy(&display_));
166 DefaultDisplayManager::~DefaultDisplayManager() {
167 // Destroy before |platform_window_| because this will destroy
168 // CommandBufferDriver objects that contain child windows. Otherwise if this
169 // class destroys its window first, X errors will occur.
170 context_provider_.reset();
172 // Destroy the PlatformWindow early on as it may call us back during
173 // destruction and we want to be in a known state.
174 platform_window_.reset();
177 void DefaultDisplayManager::SchedulePaint(const ServerView* view,
178 const gfx::Rect& bounds) {
179 DCHECK(view);
180 if (!view->IsDrawn())
181 return;
182 const gfx::Rect root_relative_rect =
183 ConvertRectBetweenViews(view, delegate_->GetRootView(), bounds);
184 if (root_relative_rect.IsEmpty())
185 return;
186 dirty_rect_.Union(root_relative_rect);
187 WantToDraw();
190 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) {
191 platform_window_->SetBounds(gfx::Rect(size));
194 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() {
195 return metrics_;
198 void DefaultDisplayManager::Draw() {
199 gfx::Rect rect(metrics_.size_in_pixels.To<gfx::Size>());
200 auto pass = mojo::CreateDefaultPass(1, rect);
201 pass->damage_rect = Rect::From(dirty_rect_);
203 DrawViewTree(pass.get(), delegate_->GetRootView(), gfx::Vector2d(), 1.0f);
205 auto frame = mojo::Frame::New();
206 frame->passes.push_back(pass.Pass());
207 frame->resources.resize(0u);
208 frame_pending_ = true;
209 if (display_) {
210 display_->SubmitFrame(
211 frame.Pass(),
212 base::Bind(&DefaultDisplayManager::DidDraw, base::Unretained(this)));
214 dirty_rect_ = gfx::Rect();
217 void DefaultDisplayManager::DidDraw() {
218 frame_pending_ = false;
219 if (!dirty_rect_.IsEmpty())
220 WantToDraw();
223 void DefaultDisplayManager::WantToDraw() {
224 if (draw_timer_.IsRunning() || frame_pending_)
225 return;
227 draw_timer_.Start(
228 FROM_HERE, base::TimeDelta(),
229 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this)));
232 void DefaultDisplayManager::UpdateMetrics(const gfx::Size& size,
233 float device_pixel_ratio) {
234 if (metrics_.size_in_pixels.To<gfx::Size>() == size &&
235 metrics_.device_pixel_ratio == device_pixel_ratio)
236 return;
237 mojo::ViewportMetrics old_metrics;
238 old_metrics.size_in_pixels = metrics_.size_in_pixels.Clone();
239 old_metrics.device_pixel_ratio = metrics_.device_pixel_ratio;
241 metrics_.size_in_pixels = mojo::Size::From(size);
242 metrics_.device_pixel_ratio = device_pixel_ratio;
244 delegate_->OnViewportMetricsChanged(old_metrics, metrics_);
247 void DefaultDisplayManager::OnBoundsChanged(const gfx::Rect& new_bounds) {
248 UpdateMetrics(new_bounds.size(), metrics_.device_pixel_ratio);
251 void DefaultDisplayManager::OnDamageRect(const gfx::Rect& damaged_region) {
254 void DefaultDisplayManager::DispatchEvent(ui::Event* event) {
255 mojo::EventPtr mojo_event(mojo::Event::From(*event));
256 if (event->IsMouseWheelEvent()) {
257 // Mojo's event type has a different meaning for wheel events. Convert
258 // between the two.
259 ui::MouseWheelEvent* wheel_event =
260 static_cast<ui::MouseWheelEvent*>(event);
261 DCHECK(mojo_event->pointer_data);
262 mojo_event->pointer_data->horizontal_wheel =
263 ConvertUIWheelValueToMojoValue(wheel_event->x_offset());
264 mojo_event->pointer_data->horizontal_wheel =
265 ConvertUIWheelValueToMojoValue(wheel_event->y_offset());
267 delegate_->OnEvent(mojo_event.Pass());
269 switch (event->type()) {
270 case ui::ET_MOUSE_PRESSED:
271 case ui::ET_TOUCH_PRESSED:
272 platform_window_->SetCapture();
273 break;
274 case ui::ET_MOUSE_RELEASED:
275 case ui::ET_TOUCH_RELEASED:
276 platform_window_->ReleaseCapture();
277 break;
278 default:
279 break;
283 void DefaultDisplayManager::OnCloseRequest() {
284 platform_window_->Close();
287 void DefaultDisplayManager::OnClosed() {
288 delegate_->OnDisplayClosed();
291 void DefaultDisplayManager::OnWindowStateChanged(
292 ui::PlatformWindowState new_state) {
295 void DefaultDisplayManager::OnLostCapture() {
298 void DefaultDisplayManager::OnAcceleratedWidgetAvailable(
299 gfx::AcceleratedWidget widget,
300 float device_pixel_ratio) {
301 context_provider_->SetAcceleratedWidget(widget);
302 UpdateMetrics(metrics_.size_in_pixels.To<gfx::Size>(), device_pixel_ratio);
305 void DefaultDisplayManager::OnActivationChanged(bool active) {
308 } // namespace view_manager