diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-10 23:24:27 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-10 23:24:27 +0000 |
commit | 3df1e15f05e97223a7acff7373f0b4fb5a39fc76 (patch) | |
tree | 69ac96a449de70f5fa47a1e9ec53930daa9661ec /mojo | |
parent | af7b5864f521efbd3c3758dc3d10f0a4a10c0080 (diff) | |
download | chromium_src-3df1e15f05e97223a7acff7373f0b4fb5a39fc76.zip chromium_src-3df1e15f05e97223a7acff7373f0b4fb5a39fc76.tar.gz chromium_src-3df1e15f05e97223a7acff7373f0b4fb5a39fc76.tar.bz2 |
Do something more interesting than printf when an event is received by sample_app.
R=abarth@chromium.org
http://crbug.com/324639
Review URL: https://codereview.chromium.org/108273008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@239910 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
-rw-r--r-- | mojo/examples/sample_app/DEPS | 4 | ||||
-rw-r--r-- | mojo/examples/sample_app/gles2_client_impl.cc | 62 | ||||
-rw-r--r-- | mojo/examples/sample_app/gles2_client_impl.h | 9 | ||||
-rw-r--r-- | mojo/examples/sample_app/native_viewport_client_impl.cc | 4 | ||||
-rw-r--r-- | mojo/examples/sample_app/spinning_cube.cc | 61 | ||||
-rw-r--r-- | mojo/examples/sample_app/spinning_cube.h | 9 | ||||
-rw-r--r-- | mojo/mojo_examples.gypi | 1 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport.h | 3 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport.mojom | 1 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport_android.cc | 8 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport_android.h | 2 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport_impl.cc | 76 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport_impl.h | 4 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport_mac.mm | 8 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport_win.cc | 10 | ||||
-rw-r--r-- | mojo/services/native_viewport/native_viewport_x11.cc | 8 |
16 files changed, 234 insertions, 36 deletions
diff --git a/mojo/examples/sample_app/DEPS b/mojo/examples/sample_app/DEPS new file mode 100644 index 0000000..49b9c28 --- /dev/null +++ b/mojo/examples/sample_app/DEPS @@ -0,0 +1,4 @@ +include_rules = [ + "+ui/events/event_constants.h", # pending enum support in mojom + "+ui/gfx", # TODO(beng): trim the size of this dep. +] diff --git a/mojo/examples/sample_app/gles2_client_impl.cc b/mojo/examples/sample_app/gles2_client_impl.cc index 2a87ca0..ea89cb7 100644 --- a/mojo/examples/sample_app/gles2_client_impl.cc +++ b/mojo/examples/sample_app/gles2_client_impl.cc @@ -8,9 +8,17 @@ #include <GLES2/gl2ext.h> #include "mojo/public/gles2/gles2.h" +#include "ui/events/event_constants.h" namespace mojo { namespace examples { +namespace { + +float CalculateDragDistance(const gfx::PointF& start, const Point& end) { + return hypot(start.x() - end.x(), start.y() - end.y()); +} + +} GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe) : service_(pipe.Pass()) { @@ -21,29 +29,71 @@ GLES2ClientImpl::~GLES2ClientImpl() { service_->Destroy(); } +void GLES2ClientImpl::HandleInputEvent(const Event& event) { + switch (event.action()) { + case ui::ET_MOUSE_PRESSED: + case ui::ET_TOUCH_PRESSED: + timer_.Stop(); + capture_point_.SetPoint(event.location().x(), event.location().y()); + last_drag_point_ = capture_point_; + drag_start_time_ = base::Time::Now(); + break; + case ui::ET_MOUSE_DRAGGED: + case ui::ET_TOUCH_MOVED: + if (!timer_.IsRunning()) { + int direction = event.location().y() < last_drag_point_.y() || + event.location().x() > last_drag_point_.x() ? 1 : -1; + cube_.set_direction(direction); + cube_.UpdateForDragDistance( + CalculateDragDistance(last_drag_point_, event.location())); + cube_.Draw(); + MojoGLES2SwapBuffers(); + + last_drag_point_.SetPoint(event.location().x(), event.location().y()); + } + break; + case ui::ET_MOUSE_RELEASED: + case ui::ET_TOUCH_RELEASED: { + cube_.SetFlingMultiplier( + CalculateDragDistance(capture_point_, event.location()), + base::TimeDelta(base::Time::Now() - drag_start_time_).InSecondsF()); + + capture_point_ = last_drag_point_ = gfx::PointF(); + StartTimer(); + } + break; + default: + break; + } +} + void GLES2ClientImpl::DidCreateContext(uint64_t encoded, uint32_t width, uint32_t height) { MojoGLES2MakeCurrent(encoded); cube_.Init(width, height); - last_time_ = base::Time::Now(); - timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), - this, &GLES2ClientImpl::Draw); + StartTimer(); +} + +void GLES2ClientImpl::ContextLost() { + timer_.Stop(); } void GLES2ClientImpl::Draw() { base::Time now = base::Time::Now(); base::TimeDelta offset = now - last_time_; last_time_ = now; - cube_.Update(offset.InSecondsF()); + cube_.UpdateForTimeDelta(offset.InSecondsF()); cube_.Draw(); MojoGLES2SwapBuffers(); } -void GLES2ClientImpl::ContextLost() { - timer_.Stop(); +void GLES2ClientImpl::StartTimer() { + last_time_ = base::Time::Now(); + timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), + this, &GLES2ClientImpl::Draw); } } // namespace examples diff --git a/mojo/examples/sample_app/gles2_client_impl.h b/mojo/examples/sample_app/gles2_client_impl.h index 1d38a3c..300557a 100644 --- a/mojo/examples/sample_app/gles2_client_impl.h +++ b/mojo/examples/sample_app/gles2_client_impl.h @@ -5,10 +5,13 @@ #ifndef MOJO_EXAMPLES_SAMPLE_APP_GLES2_CLIENT_IMPL_H_ #define MOJO_EXAMPLES_SAMPLE_APP_GLES2_CLIENT_IMPL_H_ +#include "base/time/time.h" #include "base/timer/timer.h" #include "mojo/examples/sample_app/spinning_cube.h" #include "mojo/public/bindings/lib/remote_ptr.h" #include "mojom/gles2.h" +#include "mojom/native_viewport.h" +#include "ui/gfx/point_f.h" namespace mojo { namespace examples { @@ -18,6 +21,8 @@ class GLES2ClientImpl : public GLES2ClientStub { explicit GLES2ClientImpl(ScopedMessagePipeHandle pipe); virtual ~GLES2ClientImpl(); + void HandleInputEvent(const Event& event); + private: virtual void DidCreateContext(uint64_t encoded, uint32_t width, @@ -25,10 +30,14 @@ class GLES2ClientImpl : public GLES2ClientStub { virtual void ContextLost() MOJO_OVERRIDE; void Draw(); + void StartTimer(); base::Time last_time_; base::RepeatingTimer<GLES2ClientImpl> timer_; SpinningCube cube_; + gfx::PointF capture_point_; + gfx::PointF last_drag_point_; + base::Time drag_start_time_; RemotePtr<GLES2> service_; diff --git a/mojo/examples/sample_app/native_viewport_client_impl.cc b/mojo/examples/sample_app/native_viewport_client_impl.cc index 2fc2d98..17991da 100644 --- a/mojo/examples/sample_app/native_viewport_client_impl.cc +++ b/mojo/examples/sample_app/native_viewport_client_impl.cc @@ -41,8 +41,8 @@ void NativeViewportClientImpl::OnDestroyed() { void NativeViewportClientImpl::OnEvent(const Event& event) { if (!event.location().is_null()) { - LOG(INFO) << "Located Event @" - << event.location().x() << "," << event.location().y(); + gles2_client_->HandleInputEvent(event); + service_->AckEvent(event); } } diff --git a/mojo/examples/sample_app/spinning_cube.cc b/mojo/examples/sample_app/spinning_cube.cc index 3a09132..7983563 100644 --- a/mojo/examples/sample_app/spinning_cube.cc +++ b/mojo/examples/sample_app/spinning_cube.cc @@ -293,8 +293,16 @@ class ESMatrix { } }; +float RotationForTimeDelta(float delta_time) { + return delta_time * 40.0f; } +float RotationForDragDistance(float drag_distance) { + return drag_distance / 5; // Arbitrary damping. +} + +} // namespace + class SpinningCube::GLState { public: GLState(); @@ -330,7 +338,9 @@ SpinningCube::SpinningCube() : initialized_(false), width_(0), height_(0), - state_(new GLState()) { + state_(new GLState()), + fling_multiplier_(1.0f), + direction_(1) { state_->angle_ = 45.0f; } @@ -384,23 +394,33 @@ void SpinningCube::OnGLContextLost() { state_->OnGLContextLost(); } -void SpinningCube::Update(float delta_time) { - state_->angle_ += ( delta_time * 40.0f ); - if (state_->angle_ >= 360.0f ) +void SpinningCube::SetFlingMultiplier(float drag_distance, + float drag_time) { + fling_multiplier_ = RotationForDragDistance(drag_distance) / + RotationForTimeDelta(drag_time); + +} + +void SpinningCube::UpdateForTimeDelta(float delta_time) { + state_->angle_ += RotationForTimeDelta(delta_time) * fling_multiplier_; + if (state_->angle_ >= 360.0f) state_->angle_ -= 360.0f; - float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); + // Arbitrary 50-step linear reduction in spin speed. + if (fling_multiplier_ > 1.0f) { + fling_multiplier_ = + std::max(1.0f, fling_multiplier_ - (fling_multiplier_ - 1.0f) / 50); + } - ESMatrix perspective; - perspective.LoadIdentity(); - perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); + Update(); +} - ESMatrix modelview; - modelview.LoadIdentity(); - modelview.Translate(0.0, 0.0, -2.0); - modelview.Rotate(state_->angle_, 1.0, 0.0, 1.0); +void SpinningCube::UpdateForDragDistance(float distance) { + state_->angle_ += RotationForDragDistance(distance); + if (state_->angle_ >= 360.0f ) + state_->angle_ -= 360.0f; - state_->mvp_matrix_.Multiply(&modelview, &perspective); + Update(); } void SpinningCube::Draw() { @@ -425,5 +445,20 @@ void SpinningCube::Draw() { 0); } +void SpinningCube::Update() { + float aspect = static_cast<GLfloat>(width_) / static_cast<GLfloat>(height_); + + ESMatrix perspective; + perspective.LoadIdentity(); + perspective.Perspective(60.0f, aspect, 1.0f, 20.0f ); + + ESMatrix modelview; + modelview.LoadIdentity(); + modelview.Translate(0.0, 0.0, -2.0); + modelview.Rotate(state_->angle_ * direction_, 1.0, 0.0, 1.0); + + state_->mvp_matrix_.Multiply(&modelview, &perspective); +} + } // namespace examples } // namespace mojo diff --git a/mojo/examples/sample_app/spinning_cube.h b/mojo/examples/sample_app/spinning_cube.h index c4a81ea..9cd78b8 100644 --- a/mojo/examples/sample_app/spinning_cube.h +++ b/mojo/examples/sample_app/spinning_cube.h @@ -18,7 +18,10 @@ class SpinningCube { ~SpinningCube(); void Init(uint32_t width, uint32_t height); - void Update(float delta_time); + void set_direction(int direction) { direction_ = direction; } + void SetFlingMultiplier(float drag_distance, float drag_time); + void UpdateForTimeDelta(float delta_time); + void UpdateForDragDistance(float distance); void Draw(); void OnGLContextLost(); @@ -26,10 +29,14 @@ class SpinningCube { private: class GLState; + void Update(); + bool initialized_; uint32_t width_; uint32_t height_; scoped_ptr<GLState> state_; + float fling_multiplier_; + int direction_; }; } // namespace examples diff --git a/mojo/mojo_examples.gypi b/mojo/mojo_examples.gypi index 8796824..7b56cb2 100644 --- a/mojo/mojo_examples.gypi +++ b/mojo/mojo_examples.gypi @@ -6,6 +6,7 @@ 'dependencies': [ '../base/base.gyp:base', '../gpu/gpu.gyp:gles2_c_lib', + '../ui/gfx/gfx.gyp:gfx', '../ui/gl/gl.gyp:gl', 'mojo_common_lib', 'mojo_gles2', diff --git a/mojo/services/native_viewport/native_viewport.h b/mojo/services/native_viewport/native_viewport.h index 8597156..3c36c6c 100644 --- a/mojo/services/native_viewport/native_viewport.h +++ b/mojo/services/native_viewport/native_viewport.h @@ -41,6 +41,9 @@ class NativeViewport { virtual void Close() = 0; virtual gfx::Size GetSize() = 0; + virtual void SetCapture() = 0; + virtual void ReleaseCapture() = 0; + static scoped_ptr<NativeViewport> Create(shell::Context* context, NativeViewportDelegate* delegate); }; diff --git a/mojo/services/native_viewport/native_viewport.mojom b/mojo/services/native_viewport/native_viewport.mojom index aa7543b..d90bf7c 100644 --- a/mojo/services/native_viewport/native_viewport.mojom +++ b/mojo/services/native_viewport/native_viewport.mojom @@ -25,6 +25,7 @@ interface NativeViewport { void Open(); void Close(); void CreateGLES2Context(handle<message_pipe> gles2_client); + void AckEvent(Event event); }; [Peer=NativeViewport] diff --git a/mojo/services/native_viewport/native_viewport_android.cc b/mojo/services/native_viewport/native_viewport_android.cc index 1afd7bc..de24084 100644 --- a/mojo/services/native_viewport/native_viewport_android.cc +++ b/mojo/services/native_viewport/native_viewport_android.cc @@ -112,6 +112,14 @@ gfx::Size NativeViewportAndroid::GetSize() { return size_; } +void NativeViewportAndroid::SetCapture() { + NOTIMPLEMENTED(); +} + +void NativeViewportAndroid::ReleaseCapture() { + NOTIMPLEMENTED(); +} + //////////////////////////////////////////////////////////////////////////////// // NativeViewportAndroid, private: diff --git a/mojo/services/native_viewport/native_viewport_android.h b/mojo/services/native_viewport/native_viewport_android.h index 0858425..c456033 100644 --- a/mojo/services/native_viewport/native_viewport_android.h +++ b/mojo/services/native_viewport/native_viewport_android.h @@ -42,6 +42,8 @@ class NativeViewportAndroid : public NativeViewport { virtual void Init() OVERRIDE; virtual void Close() OVERRIDE; virtual gfx::Size GetSize() OVERRIDE; + virtual void SetCapture() OVERRIDE; + virtual void ReleaseCapture() OVERRIDE; void ReleaseWindow(); diff --git a/mojo/services/native_viewport/native_viewport_impl.cc b/mojo/services/native_viewport/native_viewport_impl.cc index 5b980c8..6d04ca4 100644 --- a/mojo/services/native_viewport/native_viewport_impl.cc +++ b/mojo/services/native_viewport/native_viewport_impl.cc @@ -5,17 +5,32 @@ #include "mojo/services/native_viewport/native_viewport_impl.h" #include "base/message_loop/message_loop.h" +#include "base/time/time.h" #include "mojo/services/gles2/gles2_impl.h" #include "mojo/services/native_viewport/native_viewport.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; +} + +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeViewportImpl, public: NativeViewportImpl::NativeViewportImpl(shell::Context* context, ScopedMessagePipeHandle pipe) : context_(context), widget_(gfx::kNullAcceleratedWidget), + waiting_for_event_ack_(false), + pending_event_timestamp_(0), client_(pipe.Pass()) { client_.SetPeer(this); } @@ -23,6 +38,9 @@ NativeViewportImpl::NativeViewportImpl(shell::Context* context, NativeViewportImpl::~NativeViewportImpl() { } +//////////////////////////////////////////////////////////////////////////////// +// NativeViewportImpl, NativeViewportStub overrides: + void NativeViewportImpl::Open() { native_viewport_ = services::NativeViewport::Create(context_, this); native_viewport_->Init(); @@ -40,18 +58,47 @@ void NativeViewportImpl::CreateGLES2Context( CreateGLES2ContextIfNeeded(); } -void NativeViewportImpl::CreateGLES2ContextIfNeeded() { - if (widget_ == gfx::kNullAcceleratedWidget || !gles2_) - return; - gles2_->CreateContext(widget_, native_viewport_->GetSize()); +void NativeViewportImpl::AckEvent(const Event& event) { + DCHECK_EQ(event.time_stamp(), pending_event_timestamp_); + waiting_for_event_ack_ = false; +} + +//////////////////////////////////////////////////////////////////////////////// +// NativeViewportImpl, NativeViewportDelegate implementation: + +void NativeViewportImpl::OnResized(const gfx::Size& size) { +} + +void NativeViewportImpl::OnAcceleratedWidgetAvailable( + gfx::AcceleratedWidget widget) { + widget_ = widget; + CreateGLES2ContextIfNeeded(); } bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { + // 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; + AllocationScope scope; Event::Builder event; event.set_action(ui_event->type()); - event.set_time_stamp(ui_event->time_stamp().ToInternalValue()); + pending_event_timestamp_ = ui_event->time_stamp().ToInternalValue(); + event.set_time_stamp(pending_event_timestamp_); if (ui_event->IsMouseEvent() || ui_event->IsTouchEvent()) { ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(ui_event); @@ -69,18 +116,10 @@ bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { } client_->OnEvent(event.Finish()); + waiting_for_event_ack_ = true; return false; } -void NativeViewportImpl::OnAcceleratedWidgetAvailable( - gfx::AcceleratedWidget widget) { - widget_ = widget; - CreateGLES2ContextIfNeeded(); -} - -void NativeViewportImpl::OnResized(const gfx::Size& size) { -} - void NativeViewportImpl::OnDestroyed() { // TODO(beng): // Destroying |gles2_| on the shell thread here hits thread checker asserts. @@ -94,5 +133,14 @@ void NativeViewportImpl::OnDestroyed() { client_->OnDestroyed(); } +//////////////////////////////////////////////////////////////////////////////// +// NativeViewportImpl, private: + +void NativeViewportImpl::CreateGLES2ContextIfNeeded() { + if (widget_ == gfx::kNullAcceleratedWidget || !gles2_) + return; + gles2_->CreateContext(widget_, native_viewport_->GetSize()); +} + } // namespace services } // namespace mojo diff --git a/mojo/services/native_viewport/native_viewport_impl.h b/mojo/services/native_viewport/native_viewport_impl.h index fe9d821..5a97155 100644 --- a/mojo/services/native_viewport/native_viewport_impl.h +++ b/mojo/services/native_viewport/native_viewport_impl.h @@ -27,10 +27,12 @@ class NativeViewportImpl : public NativeViewportStub, ScopedMessagePipeHandle pipe); virtual ~NativeViewportImpl(); + // Overridden from NativeViewportStub: virtual void Open() OVERRIDE; virtual void Close() OVERRIDE; virtual void CreateGLES2Context( ScopedMessagePipeHandle gles2_client) OVERRIDE; + virtual void AckEvent(const Event& event) OVERRIDE; private: // Overridden from services::NativeViewportDelegate: @@ -46,6 +48,8 @@ class NativeViewportImpl : public NativeViewportStub, gfx::AcceleratedWidget widget_; scoped_ptr<services::NativeViewport> native_viewport_; scoped_ptr<GLES2Impl> gles2_; + bool waiting_for_event_ack_; + int64 pending_event_timestamp_; RemotePtr<NativeViewportClient> client_; diff --git a/mojo/services/native_viewport/native_viewport_mac.mm b/mojo/services/native_viewport/native_viewport_mac.mm index 74f55a9..be0080a 100644 --- a/mojo/services/native_viewport/native_viewport_mac.mm +++ b/mojo/services/native_viewport/native_viewport_mac.mm @@ -50,6 +50,14 @@ class NativeViewportMac : public NativeViewport { delegate_->OnDestroyed(); } + virtual void SetCapture() OVERRIDE { + NOTIMPLEMENTED(); + } + + virtual void ReleaseCapture() OVERRIDE { + NOTIMPLEMENTED(); + } + NativeViewportDelegate* delegate_; NSWindow* window_; gfx::Rect rect_; diff --git a/mojo/services/native_viewport/native_viewport_win.cc b/mojo/services/native_viewport/native_viewport_win.cc index 7bda7ab..668a6ab 100644 --- a/mojo/services/native_viewport/native_viewport_win.cc +++ b/mojo/services/native_viewport/native_viewport_win.cc @@ -39,6 +39,16 @@ class NativeViewportWin : public gfx::WindowImpl, DestroyWindow(hwnd()); } + virtual void SetCapture() OVERRIDE { + DCHECK(::GetCapture() != hwnd()); + ::SetCapture(hwnd()); + } + + virtual void ReleaseCapture() OVERRIDE { + if (::GetCapture() == hwnd()) + ::ReleaseCapture(); + } + BEGIN_MSG_MAP_EX(NativeViewportWin) MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange) diff --git a/mojo/services/native_viewport/native_viewport_x11.cc b/mojo/services/native_viewport/native_viewport_x11.cc index 1941516..98a740a 100644 --- a/mojo/services/native_viewport/native_viewport_x11.cc +++ b/mojo/services/native_viewport/native_viewport_x11.cc @@ -65,6 +65,14 @@ class NativeViewportX11 : public NativeViewport, delegate_->OnDestroyed(); } + virtual void SetCapture() OVERRIDE { + NOTIMPLEMENTED(); + } + + virtual void ReleaseCapture() OVERRIDE { + NOTIMPLEMENTED(); + } + // Overridden from base::MessagePumpDispatcher: virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE { return true; |