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

Side by Side Diff: components/view_manager/display_manager.cc

Issue 1344573002: Mandoline: Rename components/view_manager to components/mus (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years, 3 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
OLDNEW
(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/view_manager/display_manager.h"
6
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"
35
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
43
44 using mojo::Rect;
45 using mojo::Size;
46
47 namespace view_manager {
48 namespace {
49
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;
56
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);
66 }
67
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 */);
82
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());
89 }
90
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))));
98 }
99
100 } // namespace
101
102 // static
103 DisplayManagerFactory* DisplayManager::factory_ = nullptr;
104
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);
114 }
115 return new DefaultDisplayManager(is_headless, app_impl, gpu_state,
116 surfaces_state);
117 }
118
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 weak_factory_(this) {
132 metrics_.size_in_pixels = mojo::Size::New();
133 metrics_.size_in_pixels->width = 800;
134 metrics_.size_in_pixels->height = 600;
135 }
136
137 void DefaultDisplayManager::Init(DisplayManagerDelegate* delegate) {
138 delegate_ = delegate;
139
140 gfx::Rect bounds(metrics_.size_in_pixels.To<gfx::Size>());
141 if (is_headless_) {
142 platform_window_.reset(new ui::StubWindow(this));
143 } else {
144 #if defined(OS_WIN)
145 platform_window_.reset(new ui::WinWindow(this, bounds));
146 #elif defined(USE_X11)
147 platform_window_.reset(new ui::X11Window(this));
148 #elif defined(OS_ANDROID)
149 platform_window_.reset(new ui::PlatformWindowAndroid(this));
150 #else
151 NOTREACHED() << "Unsupported platform";
152 #endif
153 }
154 platform_window_->SetBounds(bounds);
155 platform_window_->Show();
156 }
157
158 DefaultDisplayManager::~DefaultDisplayManager() {
159 // Invalidate WeakPtrs now to avoid callbacks back into the
160 // DefaultDisplayManager during destruction of |top_level_display_client_|.
161 weak_factory_.InvalidateWeakPtrs();
162 top_level_display_client_.reset();
163 // Destroy the PlatformWindow early on as it may call us back during
164 // destruction and we want to be in a known state. But destroy the surface
165 // first because it can still be using the platform window.
166 platform_window_.reset();
167 }
168
169 void DefaultDisplayManager::SchedulePaint(const ServerView* view,
170 const gfx::Rect& bounds) {
171 DCHECK(view);
172 if (!view->IsDrawn())
173 return;
174 const gfx::Rect root_relative_rect =
175 ConvertRectBetweenViews(view, delegate_->GetRootView(), bounds);
176 if (root_relative_rect.IsEmpty())
177 return;
178 dirty_rect_.Union(root_relative_rect);
179 WantToDraw();
180 }
181
182 void DefaultDisplayManager::SetViewportSize(const gfx::Size& size) {
183 platform_window_->SetBounds(gfx::Rect(size));
184 }
185
186 void DefaultDisplayManager::SetTitle(const base::string16& title) {
187 platform_window_->SetTitle(title);
188 }
189
190 const mojo::ViewportMetrics& DefaultDisplayManager::GetViewportMetrics() {
191 return metrics_;
192 }
193
194 void DefaultDisplayManager::UpdateTextInputState(
195 const ui::TextInputState& state) {
196 ui::PlatformImeController* ime = platform_window_->GetPlatformImeController();
197 if (ime)
198 ime->UpdateTextInputState(state);
199 }
200
201 void DefaultDisplayManager::SetImeVisibility(bool visible) {
202 ui::PlatformImeController* ime = platform_window_->GetPlatformImeController();
203 if (ime)
204 ime->SetImeVisibility(visible);
205 }
206
207 void DefaultDisplayManager::Draw() {
208 if (!delegate_->GetRootView()->visible())
209 return;
210
211 scoped_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create();
212 render_pass->damage_rect = dirty_rect_;
213 render_pass->output_rect = gfx::Rect(metrics_.size_in_pixels.To<gfx::Size>());
214
215 DrawViewTree(render_pass.get(),
216 delegate_->GetRootView(),
217 gfx::Vector2d(), 1.0f);
218
219 scoped_ptr<cc::DelegatedFrameData> frame_data(new cc::DelegatedFrameData);
220 frame_data->device_scale_factor = 1.f;
221 frame_data->render_pass_list.push_back(render_pass.Pass());
222
223 scoped_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame);
224 frame->delegated_frame_data = frame_data.Pass();
225 frame_pending_ = true;
226 if (top_level_display_client_) {
227 top_level_display_client_->SubmitCompositorFrame(
228 frame.Pass(),
229 base::Bind(&DefaultDisplayManager::DidDraw,
230 weak_factory_.GetWeakPtr()));
231 }
232 dirty_rect_ = gfx::Rect();
233 }
234
235 void DefaultDisplayManager::DidDraw() {
236 frame_pending_ = false;
237 if (!dirty_rect_.IsEmpty())
238 WantToDraw();
239 }
240
241 void DefaultDisplayManager::WantToDraw() {
242 if (draw_timer_.IsRunning() || frame_pending_)
243 return;
244
245 draw_timer_.Start(
246 FROM_HERE, base::TimeDelta(),
247 base::Bind(&DefaultDisplayManager::Draw, weak_factory_.GetWeakPtr()));
248 }
249
250 void DefaultDisplayManager::UpdateMetrics(const gfx::Size& size,
251 float device_pixel_ratio) {
252 if (gfx::Display::HasForceDeviceScaleFactor())
253 device_pixel_ratio = gfx::Display::GetForcedDeviceScaleFactor();
254 if (metrics_.size_in_pixels.To<gfx::Size>() == size &&
255 metrics_.device_pixel_ratio == device_pixel_ratio)
256 return;
257 mojo::ViewportMetrics old_metrics;
258 old_metrics.size_in_pixels = metrics_.size_in_pixels.Clone();
259 old_metrics.device_pixel_ratio = metrics_.device_pixel_ratio;
260
261 metrics_.size_in_pixels = mojo::Size::From(size);
262 metrics_.device_pixel_ratio = device_pixel_ratio;
263
264 delegate_->OnViewportMetricsChanged(old_metrics, metrics_);
265 }
266
267 void DefaultDisplayManager::OnBoundsChanged(const gfx::Rect& new_bounds) {
268 UpdateMetrics(new_bounds.size(), metrics_.device_pixel_ratio);
269 }
270
271 void DefaultDisplayManager::OnDamageRect(const gfx::Rect& damaged_region) {
272 dirty_rect_.Union(damaged_region);
273 WantToDraw();
274 }
275
276 void DefaultDisplayManager::DispatchEvent(ui::Event* event) {
277 mojo::EventPtr mojo_event(mojo::Event::From(*event));
278 if (event->IsMouseWheelEvent()) {
279 // Mojo's event type has a different meaning for wheel events. Convert
280 // between the two.
281 ui::MouseWheelEvent* wheel_event =
282 static_cast<ui::MouseWheelEvent*>(event);
283 DCHECK(mojo_event->pointer_data);
284 mojo_event->pointer_data->horizontal_wheel =
285 ConvertUIWheelValueToMojoValue(wheel_event->x_offset());
286 mojo_event->pointer_data->horizontal_wheel =
287 ConvertUIWheelValueToMojoValue(wheel_event->y_offset());
288 }
289 delegate_->OnEvent(mojo_event.Pass());
290
291 switch (event->type()) {
292 case ui::ET_MOUSE_PRESSED:
293 case ui::ET_TOUCH_PRESSED:
294 platform_window_->SetCapture();
295 break;
296 case ui::ET_MOUSE_RELEASED:
297 case ui::ET_TOUCH_RELEASED:
298 platform_window_->ReleaseCapture();
299 break;
300 default:
301 break;
302 }
303
304 #if defined(USE_X11)
305 // We want to emulate the WM_CHAR generation behaviour of Windows.
306 //
307 // On Linux, we've previously inserted characters by having
308 // InputMethodAuraLinux take all key down events and send a character event
309 // to the TextInputClient. This causes a mismatch in code that has to be
310 // shared between Windows and Linux, including blink code. Now that we're
311 // trying to have one way of doing things, we need to standardize on and
312 // emulate Windows character events.
313 //
314 // This is equivalent to what we're doing in the current Linux port, but
315 // done once instead of done multiple times in different places.
316 if (event->type() == ui::ET_KEY_PRESSED) {
317 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event);
318 ui::KeyEvent char_event(key_press_event->GetCharacter(),
319 key_press_event->key_code(),
320 key_press_event->flags());
321
322 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter());
323 DCHECK_EQ(key_press_event->key_code(), char_event.key_code());
324 DCHECK_EQ(key_press_event->flags(), char_event.flags());
325
326 char_event.SetExtendedKeyEventData(
327 make_scoped_ptr(new mojo::MojoExtendedKeyEventData(
328 key_press_event->GetLocatedWindowsKeyboardCode(),
329 key_press_event->GetText(),
330 key_press_event->GetUnmodifiedText())));
331 char_event.set_platform_keycode(key_press_event->platform_keycode());
332
333 delegate_->OnEvent(mojo::Event::From(char_event));
334 }
335 #endif
336 }
337
338 void DefaultDisplayManager::OnCloseRequest() {
339 platform_window_->Close();
340 }
341
342 void DefaultDisplayManager::OnClosed() {
343 delegate_->OnDisplayClosed();
344 }
345
346 void DefaultDisplayManager::OnWindowStateChanged(
347 ui::PlatformWindowState new_state) {
348 }
349
350 void DefaultDisplayManager::OnLostCapture() {
351 }
352
353 void DefaultDisplayManager::OnAcceleratedWidgetAvailable(
354 gfx::AcceleratedWidget widget,
355 float device_pixel_ratio) {
356 if (widget != gfx::kNullAcceleratedWidget) {
357 top_level_display_client_.reset(new surfaces::TopLevelDisplayClient(
358 widget, gpu_state_, surfaces_state_));
359 }
360 UpdateMetrics(metrics_.size_in_pixels.To<gfx::Size>(), device_pixel_ratio);
361 }
362
363 void DefaultDisplayManager::OnActivationChanged(bool active) {
364 }
365
366 } // namespace view_manager
OLDNEW
« no previous file with comments | « components/view_manager/display_manager.h ('k') | components/view_manager/display_manager_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698