OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/mus/ws/platform_display.h" | |
6 | |
7 #include "base/numerics/safe_conversions.h" | |
8 #include "build/build_config.h" | |
9 #include "cc/ipc/quads.mojom.h" | |
10 #include "cc/output/compositor_frame.h" | |
11 #include "cc/output/copy_output_request.h" | |
12 #include "cc/output/delegated_frame_data.h" | |
13 #include "cc/quads/render_pass.h" | |
14 #include "cc/quads/shared_quad_state.h" | |
15 #include "cc/quads/surface_draw_quad.h" | |
16 #include "components/mus/gles2/gpu_state.h" | |
17 #include "components/mus/public/interfaces/gpu.mojom.h" | |
18 #include "components/mus/surfaces/display_compositor.h" | |
19 #include "components/mus/surfaces/surfaces_state.h" | |
20 #include "components/mus/ws/platform_display_factory.h" | |
21 #include "components/mus/ws/server_window.h" | |
22 #include "components/mus/ws/server_window_surface.h" | |
23 #include "components/mus/ws/server_window_surface_manager.h" | |
24 #include "components/mus/ws/window_coordinate_conversions.h" | |
25 #include "services/shell/public/cpp/connection.h" | |
26 #include "services/shell/public/cpp/connector.h" | |
27 #include "third_party/skia/include/core/SkXfermode.h" | |
28 #include "ui/base/cursor/cursor_loader.h" | |
29 #include "ui/display/display.h" | |
30 #include "ui/events/event.h" | |
31 #include "ui/events/event_utils.h" | |
32 #include "ui/platform_window/platform_ime_controller.h" | |
33 #include "ui/platform_window/platform_window.h" | |
34 | |
35 #if defined(OS_WIN) | |
36 #include "ui/platform_window/win/win_window.h" | |
37 #elif defined(USE_X11) | |
38 #include "ui/platform_window/x11/x11_window.h" | |
39 #elif defined(OS_ANDROID) | |
40 #include "ui/platform_window/android/platform_window_android.h" | |
41 #elif defined(USE_OZONE) | |
42 #include "ui/ozone/public/ozone_platform.h" | |
43 #endif | |
44 | |
45 namespace mus { | |
46 | |
47 namespace ws { | |
48 namespace { | |
49 | |
50 // DrawWindowTree recursively visits ServerWindows, creating a SurfaceDrawQuad | |
51 // for each that lacks one. | |
52 void DrawWindowTree(cc::RenderPass* pass, | |
53 ServerWindow* window, | |
54 const gfx::Vector2d& parent_to_root_origin_offset, | |
55 float opacity) { | |
56 if (!window->visible()) | |
57 return; | |
58 | |
59 ServerWindowSurface* default_surface = | |
60 window->surface_manager() ? window->surface_manager()->GetDefaultSurface() | |
61 : nullptr; | |
62 | |
63 const gfx::Rect absolute_bounds = | |
64 window->bounds() + parent_to_root_origin_offset; | |
65 std::vector<ServerWindow*> children(window->GetChildren()); | |
66 // TODO(rjkroege, fsamuel): Make sure we're handling alpha correctly. | |
67 const float combined_opacity = opacity * window->opacity(); | |
68 for (auto it = children.rbegin(); it != children.rend(); ++it) { | |
69 DrawWindowTree(pass, *it, absolute_bounds.OffsetFromOrigin(), | |
70 combined_opacity); | |
71 } | |
72 | |
73 if (!window->surface_manager() || !window->surface_manager()->ShouldDraw()) | |
74 return; | |
75 | |
76 ServerWindowSurface* underlay_surface = | |
77 window->surface_manager()->GetUnderlaySurface(); | |
78 if (!default_surface && !underlay_surface) | |
79 return; | |
80 | |
81 if (default_surface) { | |
82 gfx::Transform quad_to_target_transform; | |
83 quad_to_target_transform.Translate(absolute_bounds.x(), | |
84 absolute_bounds.y()); | |
85 | |
86 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | |
87 | |
88 const gfx::Rect bounds_at_origin(window->bounds().size()); | |
89 // TODO(fsamuel): These clipping and visible rects are incorrect. They need | |
90 // to be populated from CompositorFrame structs. | |
91 sqs->SetAll(quad_to_target_transform, | |
92 bounds_at_origin.size() /* layer_bounds */, | |
93 bounds_at_origin /* visible_layer_bounds */, | |
94 bounds_at_origin /* clip_rect */, false /* is_clipped */, | |
95 window->opacity(), SkXfermode::kSrcOver_Mode, | |
96 0 /* sorting-context_id */); | |
97 auto quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); | |
98 quad->SetAll(sqs, bounds_at_origin /* rect */, | |
99 gfx::Rect() /* opaque_rect */, | |
100 bounds_at_origin /* visible_rect */, true /* needs_blending*/, | |
101 default_surface->id()); | |
102 } | |
103 if (underlay_surface) { | |
104 const gfx::Rect underlay_absolute_bounds = | |
105 absolute_bounds - window->underlay_offset(); | |
106 gfx::Transform quad_to_target_transform; | |
107 quad_to_target_transform.Translate(underlay_absolute_bounds.x(), | |
108 underlay_absolute_bounds.y()); | |
109 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | |
110 const gfx::Rect bounds_at_origin( | |
111 underlay_surface->last_submitted_frame_size()); | |
112 sqs->SetAll(quad_to_target_transform, | |
113 bounds_at_origin.size() /* layer_bounds */, | |
114 bounds_at_origin /* visible_layer_bounds */, | |
115 bounds_at_origin /* clip_rect */, false /* is_clipped */, | |
116 window->opacity(), SkXfermode::kSrcOver_Mode, | |
117 0 /* sorting-context_id */); | |
118 | |
119 auto quad = pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); | |
120 quad->SetAll(sqs, bounds_at_origin /* rect */, | |
121 gfx::Rect() /* opaque_rect */, | |
122 bounds_at_origin /* visible_rect */, true /* needs_blending*/, | |
123 underlay_surface->id()); | |
124 } | |
125 } | |
126 | |
127 } // namespace | |
128 | |
129 // static | |
130 PlatformDisplayFactory* PlatformDisplay::factory_ = nullptr; | |
131 | |
132 // static | |
133 PlatformDisplay* PlatformDisplay::Create( | |
134 const PlatformDisplayInitParams& init_params) { | |
135 if (factory_) | |
136 return factory_->CreatePlatformDisplay(); | |
137 | |
138 return new DefaultPlatformDisplay(init_params); | |
139 } | |
140 | |
141 DefaultPlatformDisplay::DefaultPlatformDisplay( | |
142 const PlatformDisplayInitParams& init_params) | |
143 : display_id_(init_params.display_id), | |
144 gpu_state_(init_params.gpu_state), | |
145 surfaces_state_(init_params.surfaces_state), | |
146 delegate_(nullptr), | |
147 draw_timer_(false, false), | |
148 frame_pending_(false), | |
149 #if !defined(OS_ANDROID) | |
150 cursor_loader_(ui::CursorLoader::Create()), | |
151 #endif | |
152 weak_factory_(this) { | |
153 metrics_.size_in_pixels = init_params.display_bounds.size(); | |
154 // TODO(rjkroege): Preserve the display_id when Ozone platform can use it. | |
155 } | |
156 | |
157 void DefaultPlatformDisplay::Init(PlatformDisplayDelegate* delegate) { | |
158 delegate_ = delegate; | |
159 | |
160 gfx::Rect bounds(metrics_.size_in_pixels); | |
161 #if defined(OS_WIN) | |
162 platform_window_.reset(new ui::WinWindow(this, bounds)); | |
163 #elif defined(USE_X11) | |
164 platform_window_.reset(new ui::X11Window(this)); | |
165 #elif defined(OS_ANDROID) | |
166 platform_window_.reset(new ui::PlatformWindowAndroid(this)); | |
167 #elif defined(USE_OZONE) | |
168 platform_window_ = | |
169 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds); | |
170 #else | |
171 NOTREACHED() << "Unsupported platform"; | |
172 #endif | |
173 platform_window_->SetBounds(bounds); | |
174 platform_window_->Show(); | |
175 } | |
176 | |
177 DefaultPlatformDisplay::~DefaultPlatformDisplay() { | |
178 // Don't notify the delegate from the destructor. | |
179 delegate_ = nullptr; | |
180 | |
181 // Invalidate WeakPtrs now to avoid callbacks back into the | |
182 // DefaultPlatformDisplay during destruction of |display_compositor_|. | |
183 weak_factory_.InvalidateWeakPtrs(); | |
184 display_compositor_.reset(); | |
185 // Destroy the PlatformWindow early on as it may call us back during | |
186 // destruction and we want to be in a known state. But destroy the surface | |
187 // first because it can still be using the platform window. | |
188 platform_window_.reset(); | |
189 } | |
190 | |
191 void DefaultPlatformDisplay::SchedulePaint(const ServerWindow* window, | |
192 const gfx::Rect& bounds) { | |
193 DCHECK(window); | |
194 if (!window->IsDrawn()) | |
195 return; | |
196 const gfx::Rect root_relative_rect = | |
197 ConvertRectBetweenWindows(window, delegate_->GetRootWindow(), bounds); | |
198 if (root_relative_rect.IsEmpty()) | |
199 return; | |
200 dirty_rect_.Union(root_relative_rect); | |
201 WantToDraw(); | |
202 } | |
203 | |
204 void DefaultPlatformDisplay::SetViewportSize(const gfx::Size& size) { | |
205 platform_window_->SetBounds(gfx::Rect(size)); | |
206 } | |
207 | |
208 void DefaultPlatformDisplay::SetTitle(const base::string16& title) { | |
209 platform_window_->SetTitle(title); | |
210 } | |
211 | |
212 void DefaultPlatformDisplay::SetCapture() { | |
213 platform_window_->SetCapture(); | |
214 } | |
215 | |
216 void DefaultPlatformDisplay::ReleaseCapture() { | |
217 platform_window_->ReleaseCapture(); | |
218 } | |
219 | |
220 void DefaultPlatformDisplay::SetCursorById(int32_t cursor_id) { | |
221 #if !defined(OS_ANDROID) | |
222 // TODO(erg): This still isn't sufficient, and will only use native cursors | |
223 // that chrome would use, not custom image cursors. For that, we should | |
224 // delegate to the window manager to load images from resource packs. | |
225 // | |
226 // We probably also need to deal with different DPIs. | |
227 ui::Cursor cursor(cursor_id); | |
228 cursor_loader_->SetPlatformCursor(&cursor); | |
229 platform_window_->SetCursor(cursor.platform()); | |
230 #endif | |
231 } | |
232 | |
233 float DefaultPlatformDisplay::GetDeviceScaleFactor() { | |
234 return metrics_.device_scale_factor; | |
235 } | |
236 | |
237 mojom::Rotation DefaultPlatformDisplay::GetRotation() { | |
238 // TODO(sky): implement me. | |
239 return mojom::Rotation::VALUE_0; | |
240 } | |
241 | |
242 void DefaultPlatformDisplay::UpdateTextInputState( | |
243 const ui::TextInputState& state) { | |
244 ui::PlatformImeController* ime = platform_window_->GetPlatformImeController(); | |
245 if (ime) | |
246 ime->UpdateTextInputState(state); | |
247 } | |
248 | |
249 void DefaultPlatformDisplay::SetImeVisibility(bool visible) { | |
250 ui::PlatformImeController* ime = platform_window_->GetPlatformImeController(); | |
251 if (ime) | |
252 ime->SetImeVisibility(visible); | |
253 } | |
254 | |
255 void DefaultPlatformDisplay::Draw() { | |
256 if (!delegate_->GetRootWindow()->visible()) | |
257 return; | |
258 | |
259 // TODO(fsamuel): We should add a trace for generating a top level frame. | |
260 cc::CompositorFrame frame(GenerateCompositorFrame()); | |
261 frame_pending_ = true; | |
262 if (display_compositor_) { | |
263 display_compositor_->SubmitCompositorFrame( | |
264 std::move(frame), base::Bind(&DefaultPlatformDisplay::DidDraw, | |
265 weak_factory_.GetWeakPtr())); | |
266 } | |
267 dirty_rect_ = gfx::Rect(); | |
268 } | |
269 | |
270 void DefaultPlatformDisplay::DidDraw(cc::SurfaceDrawStatus status) { | |
271 frame_pending_ = false; | |
272 delegate_->OnCompositorFrameDrawn(); | |
273 if (!dirty_rect_.IsEmpty()) | |
274 WantToDraw(); | |
275 } | |
276 | |
277 bool DefaultPlatformDisplay::IsFramePending() const { | |
278 return frame_pending_; | |
279 } | |
280 | |
281 int64_t DefaultPlatformDisplay::GetDisplayId() const { | |
282 return display_id_; | |
283 } | |
284 | |
285 void DefaultPlatformDisplay::WantToDraw() { | |
286 if (draw_timer_.IsRunning() || frame_pending_) | |
287 return; | |
288 | |
289 // TODO(rjkroege): Use vblank to kick off Draw. | |
290 draw_timer_.Start( | |
291 FROM_HERE, base::TimeDelta(), | |
292 base::Bind(&DefaultPlatformDisplay::Draw, weak_factory_.GetWeakPtr())); | |
293 } | |
294 | |
295 void DefaultPlatformDisplay::UpdateMetrics(const gfx::Size& size, | |
296 float device_scale_factor) { | |
297 if (display::Display::HasForceDeviceScaleFactor()) | |
298 device_scale_factor = display::Display::GetForcedDeviceScaleFactor(); | |
299 if (metrics_.size_in_pixels == size && | |
300 metrics_.device_scale_factor == device_scale_factor) | |
301 return; | |
302 | |
303 ViewportMetrics old_metrics = metrics_; | |
304 metrics_.size_in_pixels = size; | |
305 metrics_.device_scale_factor = device_scale_factor; | |
306 delegate_->OnViewportMetricsChanged(old_metrics, metrics_); | |
307 } | |
308 | |
309 cc::CompositorFrame DefaultPlatformDisplay::GenerateCompositorFrame() { | |
310 std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); | |
311 render_pass->damage_rect = dirty_rect_; | |
312 render_pass->output_rect = gfx::Rect(metrics_.size_in_pixels); | |
313 | |
314 DrawWindowTree(render_pass.get(), delegate_->GetRootWindow(), gfx::Vector2d(), | |
315 1.0f); | |
316 | |
317 std::unique_ptr<cc::DelegatedFrameData> frame_data( | |
318 new cc::DelegatedFrameData); | |
319 frame_data->render_pass_list.push_back(std::move(render_pass)); | |
320 | |
321 cc::CompositorFrame frame; | |
322 frame.delegated_frame_data = std::move(frame_data); | |
323 return frame; | |
324 } | |
325 | |
326 void DefaultPlatformDisplay::OnBoundsChanged(const gfx::Rect& new_bounds) { | |
327 UpdateMetrics(new_bounds.size(), metrics_.device_scale_factor); | |
328 } | |
329 | |
330 void DefaultPlatformDisplay::OnDamageRect(const gfx::Rect& damaged_region) { | |
331 dirty_rect_.Union(damaged_region); | |
332 WantToDraw(); | |
333 } | |
334 | |
335 void DefaultPlatformDisplay::DispatchEvent(ui::Event* event) { | |
336 if (event->IsScrollEvent()) { | |
337 // TODO(moshayedi): crbug.com/602859. Dispatch scroll events as | |
338 // they are once we have proper support for scroll events. | |
339 delegate_->OnEvent(ui::MouseWheelEvent(*event->AsScrollEvent())); | |
340 } else if (event->IsMouseEvent() && !event->IsMouseWheelEvent()) { | |
341 delegate_->OnEvent(ui::PointerEvent(*event->AsMouseEvent())); | |
342 } else if (event->IsTouchEvent()) { | |
343 delegate_->OnEvent(ui::PointerEvent(*event->AsTouchEvent())); | |
344 } else { | |
345 delegate_->OnEvent(*event); | |
346 } | |
347 | |
348 #if defined(USE_X11) || defined(USE_OZONE) | |
349 // We want to emulate the WM_CHAR generation behaviour of Windows. | |
350 // | |
351 // On Linux, we've previously inserted characters by having | |
352 // InputMethodAuraLinux take all key down events and send a character event | |
353 // to the TextInputClient. This causes a mismatch in code that has to be | |
354 // shared between Windows and Linux, including blink code. Now that we're | |
355 // trying to have one way of doing things, we need to standardize on and | |
356 // emulate Windows character events. | |
357 // | |
358 // This is equivalent to what we're doing in the current Linux port, but | |
359 // done once instead of done multiple times in different places. | |
360 if (event->type() == ui::ET_KEY_PRESSED) { | |
361 ui::KeyEvent* key_press_event = event->AsKeyEvent(); | |
362 ui::KeyEvent char_event(key_press_event->GetCharacter(), | |
363 key_press_event->key_code(), | |
364 key_press_event->flags()); | |
365 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter()); | |
366 DCHECK_EQ(key_press_event->key_code(), char_event.key_code()); | |
367 DCHECK_EQ(key_press_event->flags(), char_event.flags()); | |
368 delegate_->OnEvent(char_event); | |
369 } | |
370 #endif | |
371 } | |
372 | |
373 void DefaultPlatformDisplay::OnCloseRequest() { | |
374 platform_window_->Close(); | |
375 } | |
376 | |
377 void DefaultPlatformDisplay::OnClosed() { | |
378 if (delegate_) | |
379 delegate_->OnDisplayClosed(); | |
380 } | |
381 | |
382 void DefaultPlatformDisplay::OnWindowStateChanged( | |
383 ui::PlatformWindowState new_state) {} | |
384 | |
385 void DefaultPlatformDisplay::OnLostCapture() { | |
386 delegate_->OnNativeCaptureLost(); | |
387 } | |
388 | |
389 void DefaultPlatformDisplay::OnAcceleratedWidgetAvailable( | |
390 gfx::AcceleratedWidget widget, | |
391 float device_scale_factor) { | |
392 if (widget != gfx::kNullAcceleratedWidget) { | |
393 display_compositor_.reset( | |
394 new DisplayCompositor(base::ThreadTaskRunnerHandle::Get(), widget, | |
395 gpu_state_, surfaces_state_)); | |
396 } | |
397 UpdateMetrics(metrics_.size_in_pixels, device_scale_factor); | |
398 } | |
399 | |
400 void DefaultPlatformDisplay::OnAcceleratedWidgetDestroyed() { | |
401 NOTREACHED(); | |
402 } | |
403 | |
404 void DefaultPlatformDisplay::OnActivationChanged(bool active) {} | |
405 | |
406 void DefaultPlatformDisplay::RequestCopyOfOutput( | |
407 std::unique_ptr<cc::CopyOutputRequest> output_request) { | |
408 if (display_compositor_) | |
409 display_compositor_->RequestCopyOfOutput(std::move(output_request)); | |
410 } | |
411 | |
412 } // namespace ws | |
413 | |
414 } // namespace mus | |
OLD | NEW |