OLD | NEW |
---|---|
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/native_viewport/native_viewport_impl.h" | 5 #include "services/native_viewport/native_viewport_impl.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "mojo/converters/geometry/geometry_type_converters.h" | 12 #include "mojo/converters/geometry/geometry_type_converters.h" |
13 #include "mojo/converters/input_events/input_events_type_converters.h" | 13 #include "mojo/converters/input_events/input_events_type_converters.h" |
14 #include "mojo/converters/surfaces/surfaces_type_converters.h" | |
15 #include "mojo/public/cpp/application/application_delegate.h" | |
16 #include "mojo/public/cpp/application/application_impl.h" | |
17 #include "mojo/public/cpp/application/interface_factory.h" | 14 #include "mojo/public/cpp/application/interface_factory.h" |
15 #include "services/gles2/gpu_state.h" | |
18 #include "services/native_viewport/platform_viewport_headless.h" | 16 #include "services/native_viewport/platform_viewport_headless.h" |
19 #include "services/native_viewport/viewport_surface.h" | |
20 #include "ui/events/event.h" | 17 #include "ui/events/event.h" |
21 | 18 |
22 namespace native_viewport { | 19 namespace native_viewport { |
23 namespace { | 20 namespace { |
24 | 21 |
25 bool IsRateLimitedEventType(ui::Event* event) { | 22 bool IsRateLimitedEventType(ui::Event* event) { |
26 return event->type() == ui::ET_MOUSE_MOVED || | 23 return event->type() == ui::ET_MOUSE_MOVED || |
27 event->type() == ui::ET_MOUSE_DRAGGED || | 24 event->type() == ui::ET_MOUSE_DRAGGED || |
28 event->type() == ui::ET_TOUCH_MOVED; | 25 event->type() == ui::ET_TOUCH_MOVED; |
29 } | 26 } |
30 | 27 |
31 } // namespace | 28 } // namespace |
32 | 29 |
33 NativeViewportImpl::NativeViewportImpl( | 30 NativeViewportImpl::NativeViewportImpl( |
34 mojo::ApplicationImpl* app, | |
35 bool is_headless, | 31 bool is_headless, |
32 const scoped_refptr<gles2::GpuState>& gpu_state, | |
36 mojo::InterfaceRequest<mojo::NativeViewport> request) | 33 mojo::InterfaceRequest<mojo::NativeViewport> request) |
37 : is_headless_(is_headless), | 34 : is_headless_(is_headless), |
38 widget_id_(0u), | 35 context_provider_(gpu_state), |
39 sent_metrics_(false), | 36 sent_metrics_(false), |
40 metrics_(mojo::ViewportMetrics::New()), | 37 metrics_(mojo::ViewportMetrics::New()), |
41 waiting_for_event_ack_(false), | 38 waiting_for_event_ack_(false), |
42 binding_(this, request.Pass()), | 39 binding_(this, request.Pass()), |
43 weak_factory_(this) { | 40 weak_factory_(this) { |
44 binding_.set_error_handler(this); | 41 binding_.set_error_handler(this); |
45 app->ConnectToService("mojo:surfaces_service", &surface_); | |
46 // TODO(jamesr): Should be mojo_gpu_service | |
47 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); | |
48 } | 42 } |
49 | 43 |
50 NativeViewportImpl::~NativeViewportImpl() { | 44 NativeViewportImpl::~NativeViewportImpl() { |
51 // Destroy the NativeViewport early on as it may call us back during | 45 // Destroy the NativeViewport early on as it may call us back during |
52 // destruction and we want to be in a known state. | 46 // destruction and we want to be in a known state. |
53 platform_viewport_.reset(); | 47 platform_viewport_.reset(); |
54 } | 48 } |
55 | 49 |
56 void NativeViewportImpl::Create( | 50 void NativeViewportImpl::Create(mojo::SizePtr size, |
57 mojo::SizePtr size, | 51 const CreateCallback& callback) { |
58 const mojo::Callback<void(uint64_t, mojo::ViewportMetricsPtr metrics)>& | |
59 callback) { | |
60 create_callback_ = callback; | 52 create_callback_ = callback; |
61 metrics_->size = size.Clone(); | 53 metrics_->size = size.Clone(); |
62 if (is_headless_) | 54 if (is_headless_) |
63 platform_viewport_ = PlatformViewportHeadless::Create(this); | 55 platform_viewport_ = PlatformViewportHeadless::Create(this); |
64 else | 56 else |
65 platform_viewport_ = PlatformViewport::Create(this); | 57 platform_viewport_ = PlatformViewport::Create(this); |
66 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); | 58 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); |
67 } | 59 } |
68 | 60 |
69 void NativeViewportImpl::RequestMetrics(const MetricsCallback& callback) { | 61 void NativeViewportImpl::RequestMetrics( |
62 const RequestMetricsCallback& callback) { | |
70 if (!sent_metrics_) { | 63 if (!sent_metrics_) { |
71 callback.Run(metrics_.Clone()); | 64 callback.Run(metrics_.Clone()); |
72 sent_metrics_ = true; | 65 sent_metrics_ = true; |
73 return; | 66 return; |
74 } | 67 } |
75 metrics_callback_ = callback; | 68 metrics_callback_ = callback; |
76 } | 69 } |
77 | 70 |
78 void NativeViewportImpl::Show() { | 71 void NativeViewportImpl::Show() { |
79 platform_viewport_->Show(); | 72 platform_viewport_->Show(); |
80 } | 73 } |
81 | 74 |
82 void NativeViewportImpl::Hide() { | 75 void NativeViewportImpl::Hide() { |
83 platform_viewport_->Hide(); | 76 platform_viewport_->Hide(); |
84 } | 77 } |
85 | 78 |
86 void NativeViewportImpl::Close() { | 79 void NativeViewportImpl::Close() { |
87 DCHECK(platform_viewport_); | 80 DCHECK(platform_viewport_); |
88 platform_viewport_->Close(); | 81 platform_viewport_->Close(); |
89 } | 82 } |
90 | 83 |
91 void NativeViewportImpl::SetSize(mojo::SizePtr size) { | 84 void NativeViewportImpl::SetSize(mojo::SizePtr size) { |
92 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); | 85 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); |
93 } | 86 } |
94 | 87 |
95 void NativeViewportImpl::SubmittedFrame(mojo::SurfaceIdPtr child_surface_id) { | 88 void NativeViewportImpl::GetContextProvider( |
96 if (child_surface_id_.is_null()) { | 89 mojo::InterfaceRequest<mojo::ContextProvider> request) { |
97 // If this is the first indication that the client will use surfaces, | 90 context_provider_.Bind(request.Pass()); |
98 // initialize that system. | |
99 // TODO(jamesr): When everything is converted to surfaces initialize this | |
100 // eagerly. | |
101 viewport_surface_.reset(new ViewportSurface( | |
102 surface_.Pass(), gpu_service_.get(), metrics_->size.To<gfx::Size>(), | |
103 child_surface_id.To<cc::SurfaceId>())); | |
104 if (widget_id_) | |
105 viewport_surface_->SetWidgetId(widget_id_); | |
106 } | |
107 child_surface_id_ = child_surface_id.To<cc::SurfaceId>(); | |
108 if (viewport_surface_) | |
109 viewport_surface_->SetChildId(child_surface_id_); | |
110 } | 91 } |
111 | 92 |
112 void NativeViewportImpl::SetEventDispatcher( | 93 void NativeViewportImpl::SetEventDispatcher( |
113 mojo::NativeViewportEventDispatcherPtr dispatcher) { | 94 mojo::NativeViewportEventDispatcherPtr dispatcher) { |
114 event_dispatcher_ = dispatcher.Pass(); | 95 event_dispatcher_ = dispatcher.Pass(); |
115 } | 96 } |
116 | 97 |
117 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | 98 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
118 if (metrics_->Equals(*metrics)) | 99 if (metrics_->Equals(*metrics)) |
119 return; | 100 return; |
120 | 101 |
121 metrics_ = metrics.Pass(); | 102 metrics_ = metrics.Pass(); |
122 sent_metrics_ = false; | 103 sent_metrics_ = false; |
123 | 104 |
124 if (!metrics_callback_.is_null()) { | 105 if (!metrics_callback_.is_null()) { |
125 metrics_callback_.Run(metrics_.Clone()); | 106 metrics_callback_.Run(metrics_.Clone()); |
126 metrics_callback_.reset(); | 107 metrics_callback_.reset(); |
127 sent_metrics_ = true; | 108 sent_metrics_ = true; |
128 } | 109 } |
129 if (viewport_surface_) | |
130 viewport_surface_->SetSize(metrics_->size.To<gfx::Size>()); | |
131 } | 110 } |
132 | 111 |
133 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | 112 void NativeViewportImpl::OnAcceleratedWidgetAvailable( |
qsr
2015/03/04 10:33:18
You need to add another method notifying you of th
jamesr
2015/03/04 23:00:27
What do mean by "need to"? The previous code didn
qsr
2015/03/04 23:22:29
I mean that until now, returning from the backgrou
| |
134 gfx::AcceleratedWidget widget) { | 113 gfx::AcceleratedWidget widget) { |
135 widget_id_ = static_cast<uint64_t>(bit_cast<uintptr_t>(widget)); | 114 context_provider_.SetAcceleratedWidget(widget); |
136 // TODO(jamesr): Remove once everything is converted to surfaces. | 115 // TODO: The metrics here might not match the actual window size on android |
137 create_callback_.Run(widget_id_, metrics_.Clone()); | 116 // where we don't know the actual size until the first OnMetricsChanged call. |
117 create_callback_.Run(metrics_.Clone()); | |
138 sent_metrics_ = true; | 118 sent_metrics_ = true; |
139 create_callback_.reset(); | 119 create_callback_.reset(); |
140 if (viewport_surface_) | |
141 viewport_surface_->SetWidgetId(widget_id_); | |
142 } | 120 } |
143 | 121 |
144 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { | 122 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { |
145 if (!event_dispatcher_.get()) | 123 if (!event_dispatcher_.get()) |
146 return false; | 124 return false; |
147 | 125 |
148 // Must not return early before updating capture. | 126 // Must not return early before updating capture. |
149 switch (ui_event->type()) { | 127 switch (ui_event->type()) { |
150 case ui::ET_MOUSE_PRESSED: | 128 case ui::ET_MOUSE_PRESSED: |
151 case ui::ET_TOUCH_PRESSED: | 129 case ui::ET_TOUCH_PRESSED: |
(...skipping 25 matching lines...) Expand all Loading... | |
177 void NativeViewportImpl::OnConnectionError() { | 155 void NativeViewportImpl::OnConnectionError() { |
178 binding_.set_error_handler(nullptr); | 156 binding_.set_error_handler(nullptr); |
179 delete this; | 157 delete this; |
180 } | 158 } |
181 | 159 |
182 void NativeViewportImpl::AckEvent() { | 160 void NativeViewportImpl::AckEvent() { |
183 waiting_for_event_ack_ = false; | 161 waiting_for_event_ack_ = false; |
184 } | 162 } |
185 | 163 |
186 } // namespace native_viewport | 164 } // namespace native_viewport |
OLD | NEW |