1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/services/native_viewport/native_viewport_service.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/time/time.h"
#include "mojo/public/bindings/allocation_scope.h"
#include "mojo/public/shell/shell.mojom.h"
#include "mojo/services/gles2/command_buffer_impl.h"
#include "mojo/services/native_viewport/geometry_conversions.h"
#include "mojo/services/native_viewport/native_viewport.h"
#include "mojo/services/native_viewport/native_viewport.mojom.h"
#include "ui/events/event.h"
namespace mojo {
namespace services {
namespace {
bool IsRateLimitedEventType(ui::Event* event) {
return event->type() == ui::ET_MOUSE_MOVED ||
event->type() == ui::ET_MOUSE_DRAGGED ||
event->type() == ui::ET_TOUCH_MOVED;
}
}
class NativeViewportImpl
: public Service<mojo::NativeViewport, NativeViewportImpl, shell::Context>,
public NativeViewportDelegate {
public:
NativeViewportImpl()
: widget_(gfx::kNullAcceleratedWidget),
waiting_for_event_ack_(false),
pending_event_timestamp_(0) {}
virtual ~NativeViewportImpl() {}
virtual void Create(const Rect& bounds) OVERRIDE {
native_viewport_ =
services::NativeViewport::Create(context(), this);
native_viewport_->Init(bounds);
client()->OnCreated();
OnBoundsChanged(bounds);
}
virtual void Show() OVERRIDE {
native_viewport_->Show();
}
virtual void Hide() OVERRIDE {
native_viewport_->Hide();
}
virtual void Close() OVERRIDE {
command_buffer_.reset();
DCHECK(native_viewport_);
native_viewport_->Close();
}
virtual void SetBounds(const Rect& bounds) OVERRIDE {
gfx::Rect gfx_bounds(bounds.position().x(), bounds.position().y(),
bounds.size().width(), bounds.size().height());
native_viewport_->SetBounds(gfx_bounds);
}
virtual void CreateGLES2Context(ScopedMessagePipeHandle client_handle)
OVERRIDE {
if (command_buffer_ || command_buffer_handle_.is_valid()) {
LOG(ERROR) << "Can't create multiple contexts on a NativeViewport";
return;
}
// TODO(darin):
// CreateGLES2Context should accept a ScopedCommandBufferClientHandle once
// it is possible to import interface definitions from another module. For
// now, we just kludge it.
command_buffer_handle_.reset(
InterfaceHandle<CommandBufferClient>(client_handle.release().value()));
CreateCommandBufferIfNeeded();
}
virtual void AckEvent(const Event& event) OVERRIDE {
DCHECK_EQ(event.time_stamp(), pending_event_timestamp_);
waiting_for_event_ack_ = false;
}
void CreateCommandBufferIfNeeded() {
if (!command_buffer_handle_.is_valid())
return;
DCHECK(!command_buffer_.get());
if (widget_ == gfx::kNullAcceleratedWidget)
return;
gfx::Size size = native_viewport_->GetSize();
if (size.IsEmpty())
return;
command_buffer_.reset(new CommandBufferImpl(
command_buffer_handle_.Pass(), widget_, native_viewport_->GetSize()));
}
virtual bool OnEvent(ui::Event* ui_event) OVERRIDE {
// Must not return early before updating capture.
switch (ui_event->type()) {
case ui::ET_MOUSE_PRESSED:
case ui::ET_TOUCH_PRESSED:
native_viewport_->SetCapture();
break;
case ui::ET_MOUSE_RELEASED:
case ui::ET_TOUCH_RELEASED:
native_viewport_->ReleaseCapture();
break;
default:
break;
}
if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event))
return false;
pending_event_timestamp_ = ui_event->time_stamp().ToInternalValue();
AllocationScope scope;
Event::Builder event;
event.set_action(ui_event->type());
event.set_flags(ui_event->flags());
event.set_time_stamp(pending_event_timestamp_);
if (ui_event->IsMouseEvent() || ui_event->IsTouchEvent()) {
ui::LocatedEvent* located_event =
static_cast<ui::LocatedEvent*>(ui_event);
Point::Builder location;
location.set_x(located_event->location().x());
location.set_y(located_event->location().y());
event.set_location(location.Finish());
}
if (ui_event->IsTouchEvent()) {
ui::TouchEvent* touch_event = static_cast<ui::TouchEvent*>(ui_event);
TouchData::Builder touch_data;
touch_data.set_pointer_id(touch_event->touch_id());
event.set_touch_data(touch_data.Finish());
} else if (ui_event->IsKeyEvent()) {
ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
KeyData::Builder key_data;
key_data.set_key_code(key_event->key_code());
key_data.set_is_char(key_event->is_char());
event.set_key_data(key_data.Finish());
}
client()->OnEvent(event.Finish());
waiting_for_event_ack_ = true;
return false;
}
virtual void OnAcceleratedWidgetAvailable(
gfx::AcceleratedWidget widget) OVERRIDE {
widget_ = widget;
CreateCommandBufferIfNeeded();
}
virtual void OnBoundsChanged(const gfx::Rect& bounds) OVERRIDE {
CreateCommandBufferIfNeeded();
AllocationScope scope;
client()->OnBoundsChanged(bounds);
}
virtual void OnDestroyed() OVERRIDE {
command_buffer_.reset();
client()->OnDestroyed();
base::MessageLoop::current()->Quit();
}
private:
gfx::AcceleratedWidget widget_;
scoped_ptr<services::NativeViewport> native_viewport_;
ScopedCommandBufferClientHandle command_buffer_handle_;
scoped_ptr<CommandBufferImpl> command_buffer_;
bool waiting_for_event_ack_;
int64 pending_event_timestamp_;
};
} // namespace services
} // namespace mojo
#if defined(OS_ANDROID)
// Android will call this.
MOJO_NATIVE_VIEWPORT_EXPORT mojo::Application*
CreateNativeViewportService(mojo::shell::Context* context,
mojo::ScopedShellHandle shell_handle) {
mojo::Application* app = new mojo::Application(shell_handle.Pass());
app->AddServiceFactory(
new mojo::ServiceFactory<mojo::services::NativeViewportImpl,
mojo::shell::Context>(context));
return app;
}
#else
extern "C" MOJO_NATIVE_VIEWPORT_EXPORT MojoResult MojoMain(
const MojoHandle shell_handle) {
base::MessageLoopForUI loop;
mojo::Application app(shell_handle);
app.AddServiceFactory(
new mojo::ServiceFactory<mojo::services::NativeViewportImpl,
mojo::shell::Context>);
loop.Run();
return MOJO_RESULT_OK;
}
#endif // !OS_ANDROID
|