OLD | NEW |
| (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/native_viewport/native_viewport_impl.h" | |
6 | |
7 #include "base/auto_reset.h" | |
8 #include "base/bind.h" | |
9 #include "base/macros.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/time/time.h" | |
12 #include "components/view_manager/gles2/gpu_state.h" | |
13 #include "components/view_manager/native_viewport/platform_viewport_headless.h" | |
14 #include "mojo/application/public/cpp/interface_factory.h" | |
15 #include "mojo/converters/geometry/geometry_type_converters.h" | |
16 #include "ui/events/event.h" | |
17 | |
18 namespace native_viewport { | |
19 | |
20 NativeViewportImpl::NativeViewportImpl( | |
21 bool is_headless, | |
22 const scoped_refptr<gles2::GpuState>& gpu_state, | |
23 mojo::InterfaceRequest<mojo::NativeViewport> request, | |
24 scoped_ptr<mojo::AppRefCount> app_refcount) | |
25 : is_headless_(is_headless), | |
26 app_refcount_(app_refcount.Pass()), | |
27 context_provider_(new OnscreenContextProvider(gpu_state)), | |
28 sent_metrics_(false), | |
29 metrics_(mojo::ViewportMetrics::New()), | |
30 binding_(this, request.Pass()), | |
31 weak_factory_(this) { | |
32 } | |
33 | |
34 NativeViewportImpl::~NativeViewportImpl() { | |
35 // Destroy before |platform_viewport_| because this will destroy | |
36 // CommandBufferDriver objects that contain child windows. Otherwise if this | |
37 // class destroys its window first, X errors will occur. | |
38 context_provider_.reset(); | |
39 | |
40 // Destroy the NativeViewport early on as it may call us back during | |
41 // destruction and we want to be in a known state. | |
42 platform_viewport_.reset(); | |
43 } | |
44 | |
45 void NativeViewportImpl::Create(mojo::SizePtr size, | |
46 const CreateCallback& callback) { | |
47 create_callback_ = callback; | |
48 metrics_->size = size.Clone(); | |
49 if (is_headless_) | |
50 platform_viewport_ = PlatformViewportHeadless::Create(this); | |
51 else | |
52 platform_viewport_ = PlatformViewport::Create(this); | |
53 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); | |
54 } | |
55 | |
56 void NativeViewportImpl::RequestMetrics( | |
57 const RequestMetricsCallback& callback) { | |
58 if (!sent_metrics_) { | |
59 callback.Run(metrics_.Clone()); | |
60 sent_metrics_ = true; | |
61 return; | |
62 } | |
63 metrics_callback_ = callback; | |
64 } | |
65 | |
66 void NativeViewportImpl::Show() { | |
67 platform_viewport_->Show(); | |
68 } | |
69 | |
70 void NativeViewportImpl::Hide() { | |
71 platform_viewport_->Hide(); | |
72 } | |
73 | |
74 void NativeViewportImpl::Close() { | |
75 DCHECK(platform_viewport_); | |
76 platform_viewport_->Close(); | |
77 } | |
78 | |
79 void NativeViewportImpl::SetSize(mojo::SizePtr size) { | |
80 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); | |
81 } | |
82 | |
83 void NativeViewportImpl::GetContextProvider( | |
84 mojo::InterfaceRequest<mojo::ContextProvider> request) { | |
85 context_provider_->Bind(request.Pass()); | |
86 } | |
87 | |
88 void NativeViewportImpl::SetEventDispatcher( | |
89 mojo::NativeViewportEventDispatcherPtr dispatcher) { | |
90 event_dispatcher_ = dispatcher.Pass(); | |
91 } | |
92 | |
93 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | |
94 if (metrics_->Equals(*metrics)) | |
95 return; | |
96 | |
97 metrics_ = metrics.Pass(); | |
98 sent_metrics_ = false; | |
99 | |
100 if (!metrics_callback_.is_null()) { | |
101 metrics_callback_.Run(metrics_.Clone()); | |
102 metrics_callback_.reset(); | |
103 sent_metrics_ = true; | |
104 } | |
105 } | |
106 | |
107 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | |
108 gfx::AcceleratedWidget widget, | |
109 float device_pixel_ratio) { | |
110 metrics_->device_pixel_ratio = device_pixel_ratio; | |
111 context_provider_->SetAcceleratedWidget(widget); | |
112 // TODO: The metrics here might not match the actual window size on android | |
113 // where we don't know the actual size until the first OnMetricsChanged call. | |
114 create_callback_.Run(metrics_.Clone()); | |
115 sent_metrics_ = true; | |
116 create_callback_.reset(); | |
117 } | |
118 | |
119 void NativeViewportImpl::OnAcceleratedWidgetDestroyed() { | |
120 context_provider_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | |
121 } | |
122 | |
123 bool NativeViewportImpl::OnEvent(mojo::EventPtr event) { | |
124 if (event.is_null() || !event_dispatcher_.get()) | |
125 return false; | |
126 | |
127 mojo::NativeViewportEventDispatcher::OnEventCallback callback; | |
128 switch (event->action) { | |
129 case mojo::EVENT_TYPE_POINTER_MOVE: { | |
130 // TODO(sky): add logic to remember last event location and not send if | |
131 // the same. | |
132 if (pointers_waiting_on_ack_.count(event->pointer_data->pointer_id)) | |
133 return false; | |
134 | |
135 pointers_waiting_on_ack_.insert(event->pointer_data->pointer_id); | |
136 callback = | |
137 base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr(), | |
138 event->pointer_data->pointer_id); | |
139 break; | |
140 } | |
141 | |
142 case mojo::EVENT_TYPE_POINTER_CANCEL: | |
143 pointers_waiting_on_ack_.clear(); | |
144 break; | |
145 | |
146 case mojo::EVENT_TYPE_POINTER_UP: | |
147 pointers_waiting_on_ack_.erase(event->pointer_data->pointer_id); | |
148 break; | |
149 | |
150 default: | |
151 break; | |
152 } | |
153 | |
154 event_dispatcher_->OnEvent(event.Pass(), callback); | |
155 return false; | |
156 } | |
157 | |
158 void NativeViewportImpl::OnDestroyed() { | |
159 delete this; | |
160 } | |
161 | |
162 void NativeViewportImpl::AckEvent(int32 pointer_id) { | |
163 pointers_waiting_on_ack_.erase(pointer_id); | |
164 } | |
165 | |
166 } // namespace native_viewport | |
OLD | NEW |