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 "components/native_viewport/native_viewport_impl.h" | 5 #include "components/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 "components/gles2/gpu_state.h" | 12 #include "components/gles2/gpu_state.h" |
13 #include "components/native_viewport/platform_viewport_headless.h" | 13 #include "components/native_viewport/platform_viewport_headless.h" |
14 #include "mojo/application/public/cpp/interface_factory.h" | 14 #include "mojo/application/public/cpp/interface_factory.h" |
15 #include "mojo/converters/geometry/geometry_type_converters.h" | 15 #include "mojo/converters/geometry/geometry_type_converters.h" |
16 #include "ui/events/event.h" | 16 #include "ui/events/event.h" |
17 | 17 |
18 namespace native_viewport { | 18 namespace native_viewport { |
19 | 19 |
20 NativeViewportImpl::NativeViewportImpl( | 20 NativeViewportImpl::NativeViewportImpl( |
21 bool is_headless, | 21 bool is_headless, |
22 const scoped_refptr<gles2::GpuState>& gpu_state, | 22 const scoped_refptr<gles2::GpuState>& gpu_state, |
23 mojo::InterfaceRequest<mojo::NativeViewport> request) | 23 mojo::InterfaceRequest<mojo::NativeViewport> request) |
24 : is_headless_(is_headless), | 24 : is_headless_(is_headless), |
25 context_provider_(gpu_state), | 25 context_provider_(new OnscreenContextProvider(gpu_state)), |
26 sent_metrics_(false), | 26 sent_metrics_(false), |
27 metrics_(mojo::ViewportMetrics::New()), | 27 metrics_(mojo::ViewportMetrics::New()), |
28 binding_(this, request.Pass()), | 28 binding_(this, request.Pass()), |
29 weak_factory_(this) { | 29 weak_factory_(this) { |
30 binding_.set_error_handler(this); | 30 binding_.set_error_handler(this); |
31 } | 31 } |
32 | 32 |
33 NativeViewportImpl::~NativeViewportImpl() { | 33 NativeViewportImpl::~NativeViewportImpl() { |
| 34 // Destroy before |platform_viewport_| because this will destroy |
| 35 // CommandBufferDriver objects that contain child windows. Otherwise if this |
| 36 // class destroys its window first, X errors will occur. |
| 37 context_provider_.reset(); |
| 38 |
34 // Destroy the NativeViewport early on as it may call us back during | 39 // Destroy the NativeViewport early on as it may call us back during |
35 // destruction and we want to be in a known state. | 40 // destruction and we want to be in a known state. |
36 platform_viewport_.reset(); | 41 platform_viewport_.reset(); |
37 } | 42 } |
38 | 43 |
39 void NativeViewportImpl::Create(mojo::SizePtr size, | 44 void NativeViewportImpl::Create(mojo::SizePtr size, |
40 const CreateCallback& callback) { | 45 const CreateCallback& callback) { |
41 create_callback_ = callback; | 46 create_callback_ = callback; |
42 metrics_->size = size.Clone(); | 47 metrics_->size = size.Clone(); |
43 if (is_headless_) | 48 if (is_headless_) |
(...skipping 25 matching lines...) Expand all Loading... |
69 DCHECK(platform_viewport_); | 74 DCHECK(platform_viewport_); |
70 platform_viewport_->Close(); | 75 platform_viewport_->Close(); |
71 } | 76 } |
72 | 77 |
73 void NativeViewportImpl::SetSize(mojo::SizePtr size) { | 78 void NativeViewportImpl::SetSize(mojo::SizePtr size) { |
74 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); | 79 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); |
75 } | 80 } |
76 | 81 |
77 void NativeViewportImpl::GetContextProvider( | 82 void NativeViewportImpl::GetContextProvider( |
78 mojo::InterfaceRequest<mojo::ContextProvider> request) { | 83 mojo::InterfaceRequest<mojo::ContextProvider> request) { |
79 context_provider_.Bind(request.Pass()); | 84 context_provider_->Bind(request.Pass()); |
80 } | 85 } |
81 | 86 |
82 void NativeViewportImpl::SetEventDispatcher( | 87 void NativeViewportImpl::SetEventDispatcher( |
83 mojo::NativeViewportEventDispatcherPtr dispatcher) { | 88 mojo::NativeViewportEventDispatcherPtr dispatcher) { |
84 event_dispatcher_ = dispatcher.Pass(); | 89 event_dispatcher_ = dispatcher.Pass(); |
85 } | 90 } |
86 | 91 |
87 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | 92 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
88 if (metrics_->Equals(*metrics)) | 93 if (metrics_->Equals(*metrics)) |
89 return; | 94 return; |
90 | 95 |
91 metrics_ = metrics.Pass(); | 96 metrics_ = metrics.Pass(); |
92 sent_metrics_ = false; | 97 sent_metrics_ = false; |
93 | 98 |
94 if (!metrics_callback_.is_null()) { | 99 if (!metrics_callback_.is_null()) { |
95 metrics_callback_.Run(metrics_.Clone()); | 100 metrics_callback_.Run(metrics_.Clone()); |
96 metrics_callback_.reset(); | 101 metrics_callback_.reset(); |
97 sent_metrics_ = true; | 102 sent_metrics_ = true; |
98 } | 103 } |
99 } | 104 } |
100 | 105 |
101 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | 106 void NativeViewportImpl::OnAcceleratedWidgetAvailable( |
102 gfx::AcceleratedWidget widget, | 107 gfx::AcceleratedWidget widget, |
103 float device_pixel_ratio) { | 108 float device_pixel_ratio) { |
104 metrics_->device_pixel_ratio = device_pixel_ratio; | 109 metrics_->device_pixel_ratio = device_pixel_ratio; |
105 context_provider_.SetAcceleratedWidget(widget); | 110 context_provider_->SetAcceleratedWidget(widget); |
106 // TODO: The metrics here might not match the actual window size on android | 111 // TODO: The metrics here might not match the actual window size on android |
107 // where we don't know the actual size until the first OnMetricsChanged call. | 112 // where we don't know the actual size until the first OnMetricsChanged call. |
108 create_callback_.Run(metrics_.Clone()); | 113 create_callback_.Run(metrics_.Clone()); |
109 sent_metrics_ = true; | 114 sent_metrics_ = true; |
110 create_callback_.reset(); | 115 create_callback_.reset(); |
111 } | 116 } |
112 | 117 |
113 void NativeViewportImpl::OnAcceleratedWidgetDestroyed() { | 118 void NativeViewportImpl::OnAcceleratedWidgetDestroyed() { |
114 context_provider_.SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | 119 context_provider_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); |
115 } | 120 } |
116 | 121 |
117 bool NativeViewportImpl::OnEvent(mojo::EventPtr event) { | 122 bool NativeViewportImpl::OnEvent(mojo::EventPtr event) { |
118 if (event.is_null() || !event_dispatcher_.get()) | 123 if (event.is_null() || !event_dispatcher_.get()) |
119 return false; | 124 return false; |
120 | 125 |
121 mojo::NativeViewportEventDispatcher::OnEventCallback callback; | 126 mojo::NativeViewportEventDispatcher::OnEventCallback callback; |
122 switch (event->action) { | 127 switch (event->action) { |
123 case mojo::EVENT_TYPE_POINTER_MOVE: { | 128 case mojo::EVENT_TYPE_POINTER_MOVE: { |
124 // TODO(sky): add logic to remember last event location and not send if | 129 // TODO(sky): add logic to remember last event location and not send if |
(...skipping 18 matching lines...) Expand all Loading... |
143 | 148 |
144 default: | 149 default: |
145 break; | 150 break; |
146 } | 151 } |
147 | 152 |
148 event_dispatcher_->OnEvent(event.Pass(), callback); | 153 event_dispatcher_->OnEvent(event.Pass(), callback); |
149 return false; | 154 return false; |
150 } | 155 } |
151 | 156 |
152 void NativeViewportImpl::OnDestroyed() { | 157 void NativeViewportImpl::OnDestroyed() { |
153 // This will signal a connection error and cause us to delete |this|. | 158 delete this; |
154 binding_.Close(); | |
155 } | 159 } |
156 | 160 |
157 void NativeViewportImpl::OnConnectionError() { | 161 void NativeViewportImpl::OnConnectionError() { |
158 binding_.set_error_handler(nullptr); | 162 binding_.set_error_handler(nullptr); |
159 delete this; | 163 delete this; |
160 } | 164 } |
161 | 165 |
162 void NativeViewportImpl::AckEvent(int32 pointer_id) { | 166 void NativeViewportImpl::AckEvent(int32 pointer_id) { |
163 pointers_waiting_on_ack_.erase(pointer_id); | 167 pointers_waiting_on_ack_.erase(pointer_id); |
164 } | 168 } |
165 | 169 |
166 } // namespace native_viewport | 170 } // namespace native_viewport |
OLD | NEW |