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 "mojo/services/native_viewport/native_viewport_impl.h"
8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/time/time.h"
11 #include "mojo/public/cpp/application/application_delegate.h"
12 #include "mojo/public/cpp/application/application_impl.h"
13 #include "mojo/public/cpp/application/interface_factory.h"
14 #include "mojo/services/native_viewport/platform_viewport_headless.h"
15 #include "mojo/services/native_viewport/viewport_surface.h"
16 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
17 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
18 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h"
19 #include "ui/events/event.h"
24 bool IsRateLimitedEventType(ui::Event* event) {
25 return event->type() == ui::ET_MOUSE_MOVED ||
26 event->type() == ui::ET_MOUSE_DRAGGED ||
27 event->type() == ui::ET_TOUCH_MOVED;
32 NativeViewportImpl::NativeViewportImpl(ApplicationImpl* app, bool is_headless)
33 : is_headless_(is_headless),
35 waiting_for_event_ack_(false),
37 app->ConnectToService("mojo:mojo_surfaces_service", &surfaces_service_);
38 // TODO(jamesr): Should be mojo_gpu_service
39 app->ConnectToService("mojo:mojo_native_viewport_service", &gpu_service_);
42 NativeViewportImpl::~NativeViewportImpl() {
43 // Destroy the NativeViewport early on as it may call us back during
44 // destruction and we want to be in a known state.
45 platform_viewport_.reset();
48 void NativeViewportImpl::Create(SizePtr bounds) {
50 platform_viewport_ = PlatformViewportHeadless::Create(this);
52 platform_viewport_ = PlatformViewport::Create(this);
53 gfx::Rect rect = gfx::Rect(bounds.To<gfx::Size>());
54 platform_viewport_->Init(rect);
55 OnBoundsChanged(rect);
58 void NativeViewportImpl::Show() {
59 platform_viewport_->Show();
62 void NativeViewportImpl::Hide() {
63 platform_viewport_->Hide();
66 void NativeViewportImpl::Close() {
67 DCHECK(platform_viewport_);
68 platform_viewport_->Close();
71 void NativeViewportImpl::SetBounds(SizePtr bounds) {
72 platform_viewport_->SetBounds(gfx::Rect(bounds.To<gfx::Size>()));
75 void NativeViewportImpl::SubmittedFrame(SurfaceIdPtr child_surface_id) {
76 if (child_surface_id_.is_null()) {
77 // If this is the first indication that the client will use surfaces,
78 // initialize that system.
79 // TODO(jamesr): When everything is converted to surfaces initialize this
81 viewport_surface_.reset(
82 new ViewportSurface(surfaces_service_.get(),
85 child_surface_id.To<cc::SurfaceId>()));
87 viewport_surface_->SetWidgetId(widget_id_);
89 child_surface_id_ = child_surface_id.To<cc::SurfaceId>();
90 if (viewport_surface_)
91 viewport_surface_->SetChildId(child_surface_id_);
94 void NativeViewportImpl::OnBoundsChanged(const gfx::Rect& bounds) {
96 client()->OnBoundsChanged(Size::From(bounds.size()));
97 if (viewport_surface_)
98 viewport_surface_->SetSize(bounds.size());
101 void NativeViewportImpl::OnAcceleratedWidgetAvailable(
102 gfx::AcceleratedWidget widget) {
103 widget_id_ = static_cast<uint64_t>(bit_cast<uintptr_t>(widget));
104 // TODO(jamesr): Remove once everything is converted to surfaces.
105 client()->OnCreated(widget_id_);
106 if (viewport_surface_)
107 viewport_surface_->SetWidgetId(widget_id_);
110 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) {
111 // Must not return early before updating capture.
112 switch (ui_event->type()) {
113 case ui::ET_MOUSE_PRESSED:
114 case ui::ET_TOUCH_PRESSED:
115 platform_viewport_->SetCapture();
117 case ui::ET_MOUSE_RELEASED:
118 case ui::ET_TOUCH_RELEASED:
119 platform_viewport_->ReleaseCapture();
125 if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event))
129 Event::From(*ui_event),
130 base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr()));
131 waiting_for_event_ack_ = true;
135 void NativeViewportImpl::OnDestroyed() {
136 client()->OnDestroyed();
139 void NativeViewportImpl::AckEvent() {
140 waiting_for_event_ack_ = false;