OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/examples/sample_app/gles2_client_impl.h" | 5 #include "mojo/examples/sample_app/gles2_client_impl.h" |
6 | 6 |
7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
8 #include <GLES2/gl2ext.h> | 8 #include <GLES2/gl2ext.h> |
9 #include <math.h> | 9 #include <math.h> |
10 | 10 |
11 #include "mojo/public/gles2/gles2.h" | 11 #include "mojo/public/gles2/gles2.h" |
12 #include "ui/events/event_constants.h" | 12 #include "ui/events/event_constants.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 namespace examples { | 15 namespace examples { |
16 namespace { | 16 namespace { |
17 | 17 |
18 float CalculateDragDistance(const gfx::PointF& start, const Point& end) { | 18 float CalculateDragDistance(const gfx::PointF& start, const Point& end) { |
19 return hypot(start.x() - end.x(), start.y() - end.y()); | 19 return hypot(start.x() - end.x(), start.y() - end.y()); |
20 } | 20 } |
21 | 21 |
22 } | 22 } |
23 | 23 |
24 GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe) | 24 GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe) |
25 : service_(pipe.Pass(), this) { | 25 : getting_animation_frames_(false), |
| 26 service_(pipe.Pass(), this) { |
26 } | 27 } |
27 | 28 |
28 GLES2ClientImpl::~GLES2ClientImpl() { | 29 GLES2ClientImpl::~GLES2ClientImpl() { |
29 service_->Destroy(); | 30 service_->Destroy(); |
30 } | 31 } |
31 | 32 |
32 void GLES2ClientImpl::HandleInputEvent(const Event& event) { | 33 void GLES2ClientImpl::HandleInputEvent(const Event& event) { |
33 switch (event.action()) { | 34 switch (event.action()) { |
34 case ui::ET_MOUSE_PRESSED: | 35 case ui::ET_MOUSE_PRESSED: |
35 case ui::ET_TOUCH_PRESSED: | 36 case ui::ET_TOUCH_PRESSED: |
36 timer_.Stop(); | 37 CancelAnimationFrames(); |
37 capture_point_.SetPoint(event.location().x(), event.location().y()); | 38 capture_point_.SetPoint(event.location().x(), event.location().y()); |
38 last_drag_point_ = capture_point_; | 39 last_drag_point_ = capture_point_; |
39 drag_start_time_ = base::Time::Now(); | 40 drag_start_time_ = GetTimeTicksNow(); |
40 break; | 41 break; |
41 case ui::ET_MOUSE_DRAGGED: | 42 case ui::ET_MOUSE_DRAGGED: |
42 case ui::ET_TOUCH_MOVED: | 43 case ui::ET_TOUCH_MOVED: |
43 if (!timer_.IsRunning()) { | 44 if (!getting_animation_frames_) { |
44 int direction = event.location().y() < last_drag_point_.y() || | 45 int direction = event.location().y() < last_drag_point_.y() || |
45 event.location().x() > last_drag_point_.x() ? 1 : -1; | 46 event.location().x() > last_drag_point_.x() ? 1 : -1; |
46 cube_.set_direction(direction); | 47 cube_.set_direction(direction); |
47 cube_.UpdateForDragDistance( | 48 cube_.UpdateForDragDistance( |
48 CalculateDragDistance(last_drag_point_, event.location())); | 49 CalculateDragDistance(last_drag_point_, event.location())); |
49 cube_.Draw(); | 50 cube_.Draw(); |
50 MojoGLES2SwapBuffers(); | 51 MojoGLES2SwapBuffers(); |
51 | 52 |
52 last_drag_point_.SetPoint(event.location().x(), event.location().y()); | 53 last_drag_point_.SetPoint(event.location().x(), event.location().y()); |
53 } | 54 } |
54 break; | 55 break; |
55 case ui::ET_MOUSE_RELEASED: | 56 case ui::ET_MOUSE_RELEASED: |
56 case ui::ET_TOUCH_RELEASED: { | 57 case ui::ET_TOUCH_RELEASED: { |
| 58 MojoTimeTicks offset = GetTimeTicksNow() - drag_start_time_; |
| 59 float delta = static_cast<float>(offset) / 1000000.; |
57 cube_.SetFlingMultiplier( | 60 cube_.SetFlingMultiplier( |
58 CalculateDragDistance(capture_point_, event.location()), | 61 CalculateDragDistance(capture_point_, event.location()), |
59 base::TimeDelta(base::Time::Now() - drag_start_time_).InSecondsF()); | 62 delta); |
60 | 63 |
61 capture_point_ = last_drag_point_ = gfx::PointF(); | 64 capture_point_ = last_drag_point_ = gfx::PointF(); |
62 StartTimer(); | 65 RequestAnimationFrames(); |
63 } | 66 } |
64 break; | 67 break; |
65 default: | 68 default: |
66 break; | 69 break; |
67 } | 70 } |
68 } | 71 } |
69 | 72 |
70 void GLES2ClientImpl::DidCreateContext(uint64_t encoded, | 73 void GLES2ClientImpl::DidCreateContext(uint64_t encoded, |
71 uint32_t width, | 74 uint32_t width, |
72 uint32_t height) { | 75 uint32_t height) { |
73 MojoGLES2MakeCurrent(encoded); | 76 MojoGLES2MakeCurrent(encoded); |
74 | 77 |
75 cube_.Init(width, height); | 78 cube_.Init(width, height); |
76 StartTimer(); | 79 RequestAnimationFrames(); |
77 } | 80 } |
78 | 81 |
79 void GLES2ClientImpl::ContextLost() { | 82 void GLES2ClientImpl::ContextLost() { |
80 timer_.Stop(); | 83 CancelAnimationFrames(); |
81 } | 84 } |
82 | 85 |
83 void GLES2ClientImpl::Draw() { | 86 void GLES2ClientImpl::DrawAnimationFrame() { |
84 base::Time now = base::Time::Now(); | 87 MojoTimeTicks now = GetTimeTicksNow(); |
85 base::TimeDelta offset = now - last_time_; | 88 MojoTimeTicks offset = now - last_time_; |
| 89 float delta = static_cast<float>(offset) / 1000000.; |
86 last_time_ = now; | 90 last_time_ = now; |
87 cube_.UpdateForTimeDelta(offset.InSecondsF()); | 91 cube_.UpdateForTimeDelta(delta); |
88 cube_.Draw(); | 92 cube_.Draw(); |
89 | 93 |
90 MojoGLES2SwapBuffers(); | 94 MojoGLES2SwapBuffers(); |
91 } | 95 } |
92 | 96 |
93 void GLES2ClientImpl::StartTimer() { | 97 void GLES2ClientImpl::RequestAnimationFrames() { |
94 last_time_ = base::Time::Now(); | 98 getting_animation_frames_ = true; |
95 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(16), | 99 service_->RequestAnimationFrames(); |
96 this, &GLES2ClientImpl::Draw); | 100 last_time_ = GetTimeTicksNow(); |
| 101 } |
| 102 |
| 103 void GLES2ClientImpl::CancelAnimationFrames() { |
| 104 getting_animation_frames_ = false; |
| 105 service_->CancelAnimationFrames(); |
97 } | 106 } |
98 | 107 |
99 } // namespace examples | 108 } // namespace examples |
100 } // namespace mojo | 109 } // namespace mojo |
OLD | NEW |