Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Side by Side Diff: services/ui/ws/platform_display.cc

Issue 2112993002: Factor draw functionality out of DefaultPlatformDisplay into FrameGenerator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix namespace comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/ui/ws/platform_display.h ('k') | services/ui/ws/test_utils.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "services/ui/ws/platform_display.h" 5 #include "services/ui/ws/platform_display.h"
6 6
7 #include "base/numerics/safe_conversions.h" 7 #include "base/numerics/safe_conversions.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 #include "cc/ipc/quads.mojom.h" 9 #include "cc/ipc/quads.mojom.h"
10 #include "cc/output/compositor_frame.h"
11 #include "cc/output/copy_output_request.h" 10 #include "cc/output/copy_output_request.h"
12 #include "cc/output/delegated_frame_data.h" 11 #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 "services/shell/public/cpp/connection.h" 12 #include "services/shell/public/cpp/connection.h"
17 #include "services/shell/public/cpp/connector.h" 13 #include "services/shell/public/cpp/connector.h"
18 #include "services/ui/gles2/gpu_state.h" 14 #include "services/ui/gles2/gpu_state.h"
19 #include "services/ui/public/interfaces/gpu.mojom.h" 15 #include "services/ui/public/interfaces/gpu.mojom.h"
20 #include "services/ui/surfaces/display_compositor.h" 16 #include "services/ui/surfaces/display_compositor.h"
21 #include "services/ui/surfaces/surfaces_state.h" 17 #include "services/ui/surfaces/surfaces_state.h"
22 #include "services/ui/ws/platform_display_factory.h" 18 #include "services/ui/ws/platform_display_factory.h"
19 #include "services/ui/ws/platform_display_init_params.h"
23 #include "services/ui/ws/server_window.h" 20 #include "services/ui/ws/server_window.h"
24 #include "services/ui/ws/server_window_surface.h"
25 #include "services/ui/ws/server_window_surface_manager.h"
26 #include "services/ui/ws/window_coordinate_conversions.h" 21 #include "services/ui/ws/window_coordinate_conversions.h"
27 #include "third_party/skia/include/core/SkXfermode.h" 22 #include "third_party/skia/include/core/SkXfermode.h"
28 #include "ui/base/cursor/cursor_loader.h" 23 #include "ui/base/cursor/cursor_loader.h"
29 #include "ui/display/display.h" 24 #include "ui/display/display.h"
30 #include "ui/events/event.h" 25 #include "ui/events/event.h"
31 #include "ui/events/event_utils.h" 26 #include "ui/events/event_utils.h"
32 #include "ui/platform_window/platform_ime_controller.h" 27 #include "ui/platform_window/platform_ime_controller.h"
33 #include "ui/platform_window/platform_window.h" 28 #include "ui/platform_window/platform_window.h"
34 29
35 #if defined(OS_WIN) 30 #if defined(OS_WIN)
36 #include "ui/platform_window/win/win_window.h" 31 #include "ui/platform_window/win/win_window.h"
37 #elif defined(USE_X11) 32 #elif defined(USE_X11)
38 #include "ui/platform_window/x11/x11_window.h" 33 #include "ui/platform_window/x11/x11_window.h"
39 #elif defined(OS_ANDROID) 34 #elif defined(OS_ANDROID)
40 #include "ui/platform_window/android/platform_window_android.h" 35 #include "ui/platform_window/android/platform_window_android.h"
41 #elif defined(USE_OZONE) 36 #elif defined(USE_OZONE)
42 #include "ui/ozone/public/ozone_platform.h" 37 #include "ui/ozone/public/ozone_platform.h"
43 #endif 38 #endif
44 39
45 namespace ui { 40 namespace ui {
46 41
47 namespace ws { 42 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 43
129 // static 44 // static
130 PlatformDisplayFactory* PlatformDisplay::factory_ = nullptr; 45 PlatformDisplayFactory* PlatformDisplay::factory_ = nullptr;
131 46
132 // static 47 // static
133 PlatformDisplay* PlatformDisplay::Create( 48 PlatformDisplay* PlatformDisplay::Create(
134 const PlatformDisplayInitParams& init_params) { 49 const PlatformDisplayInitParams& init_params) {
135 if (factory_) 50 if (factory_)
136 return factory_->CreatePlatformDisplay(); 51 return factory_->CreatePlatformDisplay();
137 52
138 return new DefaultPlatformDisplay(init_params); 53 return new DefaultPlatformDisplay(init_params);
139 } 54 }
140 55
141 DefaultPlatformDisplay::DefaultPlatformDisplay( 56 DefaultPlatformDisplay::DefaultPlatformDisplay(
142 const PlatformDisplayInitParams& init_params) 57 const PlatformDisplayInitParams& init_params)
143 : display_id_(init_params.display_id), 58 : 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) 59 #if !defined(OS_ANDROID)
150 cursor_loader_(ui::CursorLoader::Create()), 60 cursor_loader_(ui::CursorLoader::Create()),
151 #endif 61 #endif
152 weak_factory_(this) { 62 frame_generator_(new FrameGenerator(this,
63 init_params.gpu_state,
64 init_params.surfaces_state)) {
153 metrics_.size_in_pixels = init_params.display_bounds.size(); 65 metrics_.size_in_pixels = init_params.display_bounds.size();
154 // TODO(rjkroege): Preserve the display_id when Ozone platform can use it. 66 // TODO(rjkroege): Preserve the display_id when Ozone platform can use it.
155 } 67 }
156 68
157 void DefaultPlatformDisplay::Init(PlatformDisplayDelegate* delegate) { 69 void DefaultPlatformDisplay::Init(PlatformDisplayDelegate* delegate) {
158 delegate_ = delegate; 70 delegate_ = delegate;
159 71
160 gfx::Rect bounds(metrics_.size_in_pixels); 72 gfx::Rect bounds(metrics_.size_in_pixels);
161 #if defined(OS_WIN) 73 #if defined(OS_WIN)
162 platform_window_.reset(new ui::WinWindow(this, bounds)); 74 platform_window_.reset(new ui::WinWindow(this, bounds));
163 #elif defined(USE_X11) 75 #elif defined(USE_X11)
164 platform_window_.reset(new ui::X11Window(this)); 76 platform_window_.reset(new ui::X11Window(this));
165 #elif defined(OS_ANDROID) 77 #elif defined(OS_ANDROID)
166 platform_window_.reset(new ui::PlatformWindowAndroid(this)); 78 platform_window_.reset(new ui::PlatformWindowAndroid(this));
167 #elif defined(USE_OZONE) 79 #elif defined(USE_OZONE)
168 platform_window_ = 80 platform_window_ =
169 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds); 81 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds);
170 #else 82 #else
171 NOTREACHED() << "Unsupported platform"; 83 NOTREACHED() << "Unsupported platform";
172 #endif 84 #endif
173 platform_window_->SetBounds(bounds); 85 platform_window_->SetBounds(bounds);
174 platform_window_->Show(); 86 platform_window_->Show();
175 } 87 }
176 88
177 DefaultPlatformDisplay::~DefaultPlatformDisplay() { 89 DefaultPlatformDisplay::~DefaultPlatformDisplay() {
178 // Don't notify the delegate from the destructor. 90 // Don't notify the delegate from the destructor.
179 delegate_ = nullptr; 91 delegate_ = nullptr;
180 92
181 // Invalidate WeakPtrs now to avoid callbacks back into the 93 frame_generator_.reset();
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 94 // 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 95 // 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. 96 // first because it can still be using the platform window.
188 platform_window_.reset(); 97 platform_window_.reset();
189 } 98 }
190 99
191 void DefaultPlatformDisplay::SchedulePaint(const ServerWindow* window, 100 void DefaultPlatformDisplay::SchedulePaint(const ServerWindow* window,
192 const gfx::Rect& bounds) { 101 const gfx::Rect& bounds) {
193 DCHECK(window); 102 DCHECK(window);
194 if (!window->IsDrawn()) 103 if (!window->IsDrawn())
195 return; 104 return;
196 const gfx::Rect root_relative_rect = 105 const gfx::Rect root_relative_rect =
197 ConvertRectBetweenWindows(window, delegate_->GetRootWindow(), bounds); 106 ConvertRectBetweenWindows(window, delegate_->GetRootWindow(), bounds);
198 if (root_relative_rect.IsEmpty()) 107 if (root_relative_rect.IsEmpty())
199 return; 108 return;
200 dirty_rect_.Union(root_relative_rect); 109 frame_generator_->RequestRedraw(root_relative_rect);
201 WantToDraw();
202 } 110 }
203 111
204 void DefaultPlatformDisplay::SetViewportSize(const gfx::Size& size) { 112 void DefaultPlatformDisplay::SetViewportSize(const gfx::Size& size) {
205 platform_window_->SetBounds(gfx::Rect(size)); 113 platform_window_->SetBounds(gfx::Rect(size));
206 } 114 }
207 115
208 void DefaultPlatformDisplay::SetTitle(const base::string16& title) { 116 void DefaultPlatformDisplay::SetTitle(const base::string16& title) {
209 platform_window_->SetTitle(title); 117 platform_window_->SetTitle(title);
210 } 118 }
211 119
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 if (ime) 153 if (ime)
246 ime->UpdateTextInputState(state); 154 ime->UpdateTextInputState(state);
247 } 155 }
248 156
249 void DefaultPlatformDisplay::SetImeVisibility(bool visible) { 157 void DefaultPlatformDisplay::SetImeVisibility(bool visible) {
250 ui::PlatformImeController* ime = platform_window_->GetPlatformImeController(); 158 ui::PlatformImeController* ime = platform_window_->GetPlatformImeController();
251 if (ime) 159 if (ime)
252 ime->SetImeVisibility(visible); 160 ime->SetImeVisibility(visible);
253 } 161 }
254 162
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 { 163 bool DefaultPlatformDisplay::IsFramePending() const {
278 return frame_pending_; 164 return frame_generator_->is_frame_pending();
279 } 165 }
280 166
281 int64_t DefaultPlatformDisplay::GetDisplayId() const { 167 int64_t DefaultPlatformDisplay::GetDisplayId() const {
282 return display_id_; 168 return display_id_;
283 } 169 }
284 170
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, 171 void DefaultPlatformDisplay::UpdateMetrics(const gfx::Size& size,
296 float device_scale_factor) { 172 float device_scale_factor) {
297 if (display::Display::HasForceDeviceScaleFactor()) 173 if (display::Display::HasForceDeviceScaleFactor())
298 device_scale_factor = display::Display::GetForcedDeviceScaleFactor(); 174 device_scale_factor = display::Display::GetForcedDeviceScaleFactor();
299 if (metrics_.size_in_pixels == size && 175 if (metrics_.size_in_pixels == size &&
300 metrics_.device_scale_factor == device_scale_factor) 176 metrics_.device_scale_factor == device_scale_factor)
301 return; 177 return;
302 178
303 ViewportMetrics old_metrics = metrics_; 179 ViewportMetrics old_metrics = metrics_;
304 metrics_.size_in_pixels = size; 180 metrics_.size_in_pixels = size;
305 metrics_.device_scale_factor = device_scale_factor; 181 metrics_.device_scale_factor = device_scale_factor;
306 delegate_->OnViewportMetricsChanged(old_metrics, metrics_); 182 delegate_->OnViewportMetricsChanged(old_metrics, metrics_);
307 } 183 }
308 184
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) { 185 void DefaultPlatformDisplay::OnBoundsChanged(const gfx::Rect& new_bounds) {
327 UpdateMetrics(new_bounds.size(), metrics_.device_scale_factor); 186 UpdateMetrics(new_bounds.size(), metrics_.device_scale_factor);
328 } 187 }
329 188
330 void DefaultPlatformDisplay::OnDamageRect(const gfx::Rect& damaged_region) { 189 void DefaultPlatformDisplay::OnDamageRect(const gfx::Rect& damaged_region) {
331 dirty_rect_.Union(damaged_region); 190 frame_generator_->RequestRedraw(damaged_region);
332 WantToDraw();
333 } 191 }
334 192
335 void DefaultPlatformDisplay::DispatchEvent(ui::Event* event) { 193 void DefaultPlatformDisplay::DispatchEvent(ui::Event* event) {
336 if (event->IsScrollEvent()) { 194 if (event->IsScrollEvent()) {
337 // TODO(moshayedi): crbug.com/602859. Dispatch scroll events as 195 // TODO(moshayedi): crbug.com/602859. Dispatch scroll events as
338 // they are once we have proper support for scroll events. 196 // they are once we have proper support for scroll events.
339 delegate_->OnEvent(ui::MouseWheelEvent(*event->AsScrollEvent())); 197 delegate_->OnEvent(ui::MouseWheelEvent(*event->AsScrollEvent()));
340 } else if (event->IsMouseEvent() && !event->IsMouseWheelEvent()) { 198 } else if (event->IsMouseEvent() && !event->IsMouseWheelEvent()) {
341 delegate_->OnEvent(ui::PointerEvent(*event->AsMouseEvent())); 199 delegate_->OnEvent(ui::PointerEvent(*event->AsMouseEvent()));
342 } else if (event->IsTouchEvent()) { 200 } else if (event->IsTouchEvent()) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 void DefaultPlatformDisplay::OnWindowStateChanged( 240 void DefaultPlatformDisplay::OnWindowStateChanged(
383 ui::PlatformWindowState new_state) {} 241 ui::PlatformWindowState new_state) {}
384 242
385 void DefaultPlatformDisplay::OnLostCapture() { 243 void DefaultPlatformDisplay::OnLostCapture() {
386 delegate_->OnNativeCaptureLost(); 244 delegate_->OnNativeCaptureLost();
387 } 245 }
388 246
389 void DefaultPlatformDisplay::OnAcceleratedWidgetAvailable( 247 void DefaultPlatformDisplay::OnAcceleratedWidgetAvailable(
390 gfx::AcceleratedWidget widget, 248 gfx::AcceleratedWidget widget,
391 float device_scale_factor) { 249 float device_scale_factor) {
392 if (widget != gfx::kNullAcceleratedWidget) { 250 frame_generator_->OnAcceleratedWidgetAvailable(widget);
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); 251 UpdateMetrics(metrics_.size_in_pixels, device_scale_factor);
398 } 252 }
399 253
400 void DefaultPlatformDisplay::OnAcceleratedWidgetDestroyed() { 254 void DefaultPlatformDisplay::OnAcceleratedWidgetDestroyed() {
401 NOTREACHED(); 255 NOTREACHED();
402 } 256 }
403 257
404 void DefaultPlatformDisplay::OnActivationChanged(bool active) {} 258 void DefaultPlatformDisplay::OnActivationChanged(bool active) {}
405 259
406 void DefaultPlatformDisplay::RequestCopyOfOutput( 260 void DefaultPlatformDisplay::RequestCopyOfOutput(
407 std::unique_ptr<cc::CopyOutputRequest> output_request) { 261 std::unique_ptr<cc::CopyOutputRequest> output_request) {
408 if (display_compositor_) 262 frame_generator_->RequestCopyOfOutput(std::move(output_request));
409 display_compositor_->RequestCopyOfOutput(std::move(output_request)); 263 }
264
265 ServerWindow* DefaultPlatformDisplay::GetRootWindow() {
266 return delegate_->GetRootWindow();
267 }
268
269 void DefaultPlatformDisplay::OnCompositorFrameDrawn() {
270 if (delegate_)
271 delegate_->OnCompositorFrameDrawn();
272 }
273
274 const ViewportMetrics& DefaultPlatformDisplay::GetViewportMetrics() {
275 return metrics_;
410 } 276 }
411 277
412 } // namespace ws 278 } // namespace ws
413 279
414 } // namespace ui 280 } // namespace ui
OLDNEW
« no previous file with comments | « services/ui/ws/platform_display.h ('k') | services/ui/ws/test_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698