cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / view_manager / display_manager.cc
blobe3c4201b8ee94e5c3ceefecc0b1a55706ad305e1
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 "cc/output/compositor_frame.h"
9 #include "cc/output/delegated_frame_data.h"
10 #include "cc/quads/render_pass.h"
11 #include "cc/quads/shared_quad_state.h"
12 #include "cc/quads/surface_draw_quad.h"
13 #include "components/view_manager/display_manager_factory.h"
14 #include "components/view_manager/gles2/gpu_state.h"
15 #include "components/view_manager/public/interfaces/gpu.mojom.h"
16 #include "components/view_manager/public/interfaces/quads.mojom.h"
17 #include "components/view_manager/server_view.h"
18 #include "components/view_manager/surfaces/surfaces_state.h"
19 #include "components/view_manager/view_coordinate_conversions.h"
20 #include "mojo/application/public/cpp/application_connection.h"
21 #include "mojo/application/public/cpp/application_impl.h"
22 #include "mojo/converters/geometry/geometry_type_converters.h"
23 #include "mojo/converters/input_events/input_events_type_converters.h"
24 #include "mojo/converters/input_events/mojo_extended_key_event_data.h"
25 #include "mojo/converters/surfaces/surfaces_type_converters.h"
26 #include "mojo/converters/surfaces/surfaces_utils.h"
27 #include "mojo/converters/transform/transform_type_converters.h"
28 #include "third_party/skia/include/core/SkXfermode.h"
29 #include "ui/events/event.h"
30 #include "ui/events/event_utils.h"
31 #include "ui/gfx/display.h"
32 #include "ui/platform_window/platform_ime_controller.h"
33 #include "ui/platform_window/platform_window.h"
34 #include "ui/platform_window/stub/stub_window.h"
36 #if defined(OS_WIN)
37 #include "ui/platform_window/win/win_window.h"
38 #elif defined(USE_X11)
39 #include "ui/platform_window/x11/x11_window.h"
40 #elif defined(OS_ANDROID)
41 #include "ui/platform_window/android/platform_window_android.h"
42 #endif
44 using mojo::Rect;
45 using mojo::Size;
47 namespace view_manager {
48 namespace {
50 void DrawViewTree(cc::RenderPass* pass,
51 const ServerView* view,
52 const gfx::Vector2d& parent_to_root_origin_offset,
53 float opacity) {
54 if (!view->visible())
55 return;
57 const gfx::Rect absolute_bounds =
58 view->bounds() + parent_to_root_origin_offset;
59 std::vector<const ServerView*> children(view->GetChildren());
60 const float combined_opacity = opacity * view->opacity();
61 for (std::vector<const ServerView*>::reverse_iterator it = children.rbegin();
62 it != children.rend();
63 ++it) {
64 DrawViewTree(pass, *it, absolute_bounds.OffsetFromOrigin(),
65 combined_opacity);
68 gfx::Transform quad_to_target_transform;
69 quad_to_target_transform.Translate(absolute_bounds.x(), absolute_bounds.y());
70 const gfx::Rect bounds_at_origin(view->bounds().size());
71 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
72 // TODO(fsamuel): These clipping and visible rects are incorrect. They need
73 // to be populated from CompositorFrame structs.
74 sqs->SetAll(quad_to_target_transform,
75 bounds_at_origin.size() /* layer_bounds */,
76 bounds_at_origin /* visible_layer_bounds */,
77 bounds_at_origin /* clip_rect */,
78 false /* is_clipped */,
79 view->opacity(),
80 SkXfermode::kSrc_Mode,
81 0 /* sorting-context_id */);
83 auto surface_quad =
84 pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>();
85 surface_quad->SetNew(sqs,
86 bounds_at_origin /* rect */,
87 bounds_at_origin /* visible_rect */,
88 view->surface_id());
91 float ConvertUIWheelValueToMojoValue(int offset) {
92 // Mojo's event type takes a value between -1 and 1. Normalize by allowing
93 // up to 20 of ui's offset. This is a bit arbitrary.
94 return std::max(
95 -1.0f, std::min(1.0f, static_cast<float>(offset) /
96 (20 * static_cast<float>(
97 ui::MouseWheelEvent::kWheelDelta))));
100 } // namespace
102 // static
103 DisplayManagerFactory* DisplayManager::factory_ = nullptr;
105 // static
106 DisplayManager* DisplayManager::Create(
107 bool is_headless,
108 mojo::ApplicationImpl* app_impl,
109 const scoped_refptr<gles2::GpuState>& gpu_state,
110 const scoped_refptr<surfaces::SurfacesState>& surfaces_state) {
111 if (factory_) {
112 return factory_->CreateDisplayManager(is_headless, app_impl, gpu_state,
113 surfaces_state);
115 return new DefaultDisplayManager(is_headless, app_impl, gpu_state,
116 surfaces_state);
119 DefaultDisplayManager::DefaultDisplayManager(
120 bool is_headless,
121 mojo::ApplicationImpl* app_impl,
122 const scoped_refptr<gles2::GpuState>& gpu_state,
123 const scoped_refptr<surfaces::SurfacesState>& surfaces_state)
124 : is_headless_(is_headless),
125 app_impl_(app_impl),
126 gpu_state_(gpu_state),
127 surfaces_state_(surfaces_state),
128 delegate_(nullptr),
129 draw_timer_(false, false),
130 frame_pending_(false) {
131 metrics_.size_in_pixels = mojo::Size::New();
132 metrics_.size_in_pixels->width = 800;
133 metrics_.size_in_pixels->height = 600;
136 void DefaultDisplayManager::Init(DisplayManagerDelegate* delegate) {
137 delegate_ = delegate;
139 gfx::Rect bounds(metrics_.size_in_pixels.To<gfx::Size>());
140 if (is_headless_) {
141 platform_window_.reset(new ui::StubWindow(this));
142 } else {
143 #if defined(OS_WIN)
144 platform_window_.reset(new ui::WinWindow(this, bounds));
145 #elif defined(USE_X11)
146 platform_window_.reset(new ui::X11Window(this));
147 #elif defined(OS_ANDROID)
148 platform_window_.reset(new ui::PlatformWindowAndroid(this));
149 #else
150 NOTREACHED() << "Unsupported platform";
151 #endif
153 platform_window_->SetBounds(bounds);
154 platform_window_->Show();
157 DefaultDisplayManager::~DefaultDisplayManager() {
158 // Destroy the PlatformWindow early on as it may call us back during
159 // destruction and we want to be in a known state. But destroy the surface
160 // first because it can still be using the platform window.
161 top_level_display_client_.reset();
162 platform_window_.reset();
165 void DefaultDisplayManager::SchedulePaint(const ServerView* view,
166 const gfx::Rect& bounds) {
167 DCHECK(view);
168 if (!view->IsDrawn())
169 return;
170 const gfx::Rect root_relative_rect =
171 ConvertRectBetweenViews(view, delegate_->GetRootView(), bounds);
172 if (root_relative_rect.IsEmpty())
173 return;
174 dirty_rect_.Union(root_relative_rect);
175 WantToDraw();
178 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) {
179 platform_window_->SetBounds(gfx::Rect(size));
182 void DefaultDisplayManager::SetTitle(const base::string16& title) {
183 platform_window_->SetTitle(title);
186 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() {
187 return metrics_;
190 void DefaultDisplayManager::UpdateTextInputState(
191 const ui::TextInputState& state) {
192 ui::PlatformImeController* ime = platform_window_->GetPlatformImeController();
193 if (ime)
194 ime->UpdateTextInputState(state);
197 void DefaultDisplayManager::SetImeVisibility(bool visible) {
198 ui::PlatformImeController* ime = platform_window_->GetPlatformImeController();
199 if (ime)
200 ime->SetImeVisibility(visible);
203 void DefaultDisplayManager::Draw() {
204 if (!delegate_->GetRootView()->visible())
205 return;
207 scoped_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create();
208 render_pass->damage_rect = dirty_rect_;
209 render_pass->output_rect = gfx::Rect(metrics_.size_in_pixels.To<gfx::Size>());
211 DrawViewTree(render_pass.get(),
212 delegate_->GetRootView(),
213 gfx::Vector2d(), 1.0f);
215 scoped_ptr<cc::DelegatedFrameData> frame_data(new cc::DelegatedFrameData);
216 frame_data->device_scale_factor = 1.f;
217 frame_data->render_pass_list.push_back(render_pass.Pass());
219 scoped_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame);
220 frame->delegated_frame_data = frame_data.Pass();
221 frame_pending_ = true;
222 if (top_level_display_client_) {
223 top_level_display_client_->SubmitCompositorFrame(
224 frame.Pass(),
225 base::Bind(&DefaultDisplayManager::DidDraw, base::Unretained(this)));
227 dirty_rect_ = gfx::Rect();
230 void DefaultDisplayManager::DidDraw() {
231 frame_pending_ = false;
232 if (!dirty_rect_.IsEmpty())
233 WantToDraw();
236 void DefaultDisplayManager::WantToDraw() {
237 if (draw_timer_.IsRunning() || frame_pending_)
238 return;
240 draw_timer_.Start(
241 FROM_HERE, base::TimeDelta(),
242 base::Bind(&DefaultDisplayManager::Draw, base::Unretained(this)));
245 void DefaultDisplayManager::UpdateMetrics(const gfx::Size& size,
246 float device_pixel_ratio) {
247 if (gfx::Display::HasForceDeviceScaleFactor())
248 device_pixel_ratio = gfx::Display::GetForcedDeviceScaleFactor();
249 if (metrics_.size_in_pixels.To<gfx::Size>() == size &&
250 metrics_.device_pixel_ratio == device_pixel_ratio)
251 return;
252 mojo::ViewportMetrics old_metrics;
253 old_metrics.size_in_pixels = metrics_.size_in_pixels.Clone();
254 old_metrics.device_pixel_ratio = metrics_.device_pixel_ratio;
256 metrics_.size_in_pixels = mojo::Size::From(size);
257 metrics_.device_pixel_ratio = device_pixel_ratio;
259 delegate_->OnViewportMetricsChanged(old_metrics, metrics_);
262 void DefaultDisplayManager::OnBoundsChanged(const gfx::Rect& new_bounds) {
263 UpdateMetrics(new_bounds.size(), metrics_.device_pixel_ratio);
266 void DefaultDisplayManager::OnDamageRect(const gfx::Rect& damaged_region) {
267 dirty_rect_.Union(damaged_region);
268 WantToDraw();
271 void DefaultDisplayManager::DispatchEvent(ui::Event* event) {
272 mojo::EventPtr mojo_event(mojo::Event::From(*event));
273 if (event->IsMouseWheelEvent()) {
274 // Mojo's event type has a different meaning for wheel events. Convert
275 // between the two.
276 ui::MouseWheelEvent* wheel_event =
277 static_cast<ui::MouseWheelEvent*>(event);
278 DCHECK(mojo_event->pointer_data);
279 mojo_event->pointer_data->horizontal_wheel =
280 ConvertUIWheelValueToMojoValue(wheel_event->x_offset());
281 mojo_event->pointer_data->horizontal_wheel =
282 ConvertUIWheelValueToMojoValue(wheel_event->y_offset());
284 delegate_->OnEvent(mojo_event.Pass());
286 switch (event->type()) {
287 case ui::ET_MOUSE_PRESSED:
288 case ui::ET_TOUCH_PRESSED:
289 platform_window_->SetCapture();
290 break;
291 case ui::ET_MOUSE_RELEASED:
292 case ui::ET_TOUCH_RELEASED:
293 platform_window_->ReleaseCapture();
294 break;
295 default:
296 break;
299 #if defined(USE_X11)
300 // We want to emulate the WM_CHAR generation behaviour of Windows.
302 // On Linux, we've previously inserted characters by having
303 // InputMethodAuraLinux take all key down events and send a character event
304 // to the TextInputClient. This causes a mismatch in code that has to be
305 // shared between Windows and Linux, including blink code. Now that we're
306 // trying to have one way of doing things, we need to standardize on and
307 // emulate Windows character events.
309 // This is equivalent to what we're doing in the current Linux port, but
310 // done once instead of done multiple times in different places.
311 if (event->type() == ui::ET_KEY_PRESSED) {
312 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event);
313 ui::KeyEvent char_event(key_press_event->GetCharacter(),
314 key_press_event->key_code(),
315 key_press_event->flags());
317 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter());
318 DCHECK_EQ(key_press_event->key_code(), char_event.key_code());
319 DCHECK_EQ(key_press_event->flags(), char_event.flags());
321 char_event.SetExtendedKeyEventData(
322 make_scoped_ptr(new mojo::MojoExtendedKeyEventData(
323 key_press_event->GetLocatedWindowsKeyboardCode(),
324 key_press_event->GetText(),
325 key_press_event->GetUnmodifiedText())));
326 char_event.set_platform_keycode(key_press_event->platform_keycode());
328 delegate_->OnEvent(mojo::Event::From(char_event));
330 #endif
333 void DefaultDisplayManager::OnCloseRequest() {
334 platform_window_->Close();
337 void DefaultDisplayManager::OnClosed() {
338 delegate_->OnDisplayClosed();
341 void DefaultDisplayManager::OnWindowStateChanged(
342 ui::PlatformWindowState new_state) {
345 void DefaultDisplayManager::OnLostCapture() {
348 void DefaultDisplayManager::OnAcceleratedWidgetAvailable(
349 gfx::AcceleratedWidget widget,
350 float device_pixel_ratio) {
351 if (widget != gfx::kNullAcceleratedWidget) {
352 top_level_display_client_.reset(new surfaces::TopLevelDisplayClient(
353 widget, gpu_state_, surfaces_state_));
355 UpdateMetrics(metrics_.size_in_pixels.To<gfx::Size>(), device_pixel_ratio);
358 void DefaultDisplayManager::OnActivationChanged(bool active) {
361 } // namespace view_manager