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 #include <stdlib.h> | 10 #include <stdlib.h> |
11 | 11 |
12 #include "mojo/public/c/gles2/gles2.h" | 12 #include "mojo/public/c/gles2/gles2.h" |
13 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
14 | 14 |
15 namespace mojo { | |
16 namespace examples { | 15 namespace examples { |
17 namespace { | 16 namespace { |
18 | 17 |
19 float CalculateDragDistance(const gfx::PointF& start, const Point& end) { | 18 float CalculateDragDistance(const gfx::PointF& start, const mojo::Point& end) { |
20 return hypot(start.x() - end.x, start.y() - end.y); | 19 return hypot(start.x() - end.x, start.y() - end.y); |
21 } | 20 } |
22 | 21 |
23 float GetRandomColor() { | 22 float GetRandomColor() { |
24 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); | 23 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); |
25 } | 24 } |
26 | 25 |
27 } | 26 } |
28 | 27 |
29 GLES2ClientImpl::GLES2ClientImpl(CommandBufferPtr command_buffer) | 28 GLES2ClientImpl::GLES2ClientImpl(mojo::CommandBufferPtr command_buffer) |
30 : getting_animation_frames_(false) { | 29 : getting_animation_frames_(false) { |
31 context_ = MojoGLES2CreateContext( | 30 context_ = MojoGLES2CreateContext( |
32 command_buffer.PassMessagePipe().release().value(), | 31 command_buffer.PassMessagePipe().release().value(), |
33 &ContextLostThunk, | 32 &ContextLostThunk, |
34 &DrawAnimationFrameThunk, | 33 &DrawAnimationFrameThunk, |
35 this); | 34 this); |
36 MojoGLES2MakeCurrent(context_); | 35 MojoGLES2MakeCurrent(context_); |
37 } | 36 } |
38 | 37 |
39 GLES2ClientImpl::~GLES2ClientImpl() { | 38 GLES2ClientImpl::~GLES2ClientImpl() { |
40 MojoGLES2DestroyContext(context_); | 39 MojoGLES2DestroyContext(context_); |
41 } | 40 } |
42 | 41 |
43 void GLES2ClientImpl::SetSize(const Size& size) { | 42 void GLES2ClientImpl::SetSize(const mojo::Size& size) { |
44 size_ = gfx::Size(size.width, size.height); | 43 size_ = gfx::Size(size.width, size.height); |
45 if (size_.IsEmpty()) | 44 if (size_.IsEmpty()) |
46 return; | 45 return; |
47 cube_.Init(size_.width(), size_.height()); | 46 cube_.Init(size_.width(), size_.height()); |
48 RequestAnimationFrames(); | 47 RequestAnimationFrames(); |
49 } | 48 } |
50 | 49 |
51 void GLES2ClientImpl::HandleInputEvent(const Event& event) { | 50 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) { |
52 switch (event.action) { | 51 switch (event.action) { |
53 case ui::ET_MOUSE_PRESSED: | 52 case ui::ET_MOUSE_PRESSED: |
54 case ui::ET_TOUCH_PRESSED: | 53 case ui::ET_TOUCH_PRESSED: |
55 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) | 54 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) |
56 break; | 55 break; |
57 CancelAnimationFrames(); | 56 CancelAnimationFrames(); |
58 capture_point_.SetPoint(event.location->x, event.location->y); | 57 capture_point_.SetPoint(event.location->x, event.location->y); |
59 last_drag_point_ = capture_point_; | 58 last_drag_point_ = capture_point_; |
60 drag_start_time_ = GetTimeTicksNow(); | 59 drag_start_time_ = mojo::GetTimeTicksNow(); |
61 break; | 60 break; |
62 case ui::ET_MOUSE_DRAGGED: | 61 case ui::ET_MOUSE_DRAGGED: |
63 case ui::ET_TOUCH_MOVED: | 62 case ui::ET_TOUCH_MOVED: |
64 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) | 63 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) |
65 break; | 64 break; |
66 if (!getting_animation_frames_) { | 65 if (!getting_animation_frames_) { |
67 int direction = event.location->y < last_drag_point_.y() || | 66 int direction = event.location->y < last_drag_point_.y() || |
68 event.location->x > last_drag_point_.x() ? 1 : -1; | 67 event.location->x > last_drag_point_.x() ? 1 : -1; |
69 cube_.set_direction(direction); | 68 cube_.set_direction(direction); |
70 cube_.UpdateForDragDistance( | 69 cube_.UpdateForDragDistance( |
71 CalculateDragDistance(last_drag_point_, *event.location)); | 70 CalculateDragDistance(last_drag_point_, *event.location)); |
72 cube_.Draw(); | 71 cube_.Draw(); |
73 MojoGLES2SwapBuffers(); | 72 MojoGLES2SwapBuffers(); |
74 | 73 |
75 last_drag_point_.SetPoint(event.location->x, event.location->y); | 74 last_drag_point_.SetPoint(event.location->x, event.location->y); |
76 } | 75 } |
77 break; | 76 break; |
78 case ui::ET_MOUSE_RELEASED: | 77 case ui::ET_MOUSE_RELEASED: |
79 case ui::ET_TOUCH_RELEASED: { | 78 case ui::ET_TOUCH_RELEASED: { |
80 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) { | 79 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) { |
81 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); | 80 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); |
82 break; | 81 break; |
83 } | 82 } |
84 MojoTimeTicks offset = GetTimeTicksNow() - drag_start_time_; | 83 MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_; |
85 float delta = static_cast<float>(offset) / 1000000.; | 84 float delta = static_cast<float>(offset) / 1000000.; |
86 cube_.SetFlingMultiplier( | 85 cube_.SetFlingMultiplier( |
87 CalculateDragDistance(capture_point_, *event.location), | 86 CalculateDragDistance(capture_point_, *event.location), |
88 delta); | 87 delta); |
89 | 88 |
90 capture_point_ = last_drag_point_ = gfx::PointF(); | 89 capture_point_ = last_drag_point_ = gfx::PointF(); |
91 RequestAnimationFrames(); | 90 RequestAnimationFrames(); |
92 break; | 91 break; |
93 } | 92 } |
94 default: | 93 default: |
95 break; | 94 break; |
96 } | 95 } |
97 } | 96 } |
98 | 97 |
99 void GLES2ClientImpl::ContextLost() { | 98 void GLES2ClientImpl::ContextLost() { |
100 CancelAnimationFrames(); | 99 CancelAnimationFrames(); |
101 } | 100 } |
102 | 101 |
103 void GLES2ClientImpl::ContextLostThunk(void* closure) { | 102 void GLES2ClientImpl::ContextLostThunk(void* closure) { |
104 static_cast<GLES2ClientImpl*>(closure)->ContextLost(); | 103 static_cast<GLES2ClientImpl*>(closure)->ContextLost(); |
105 } | 104 } |
106 | 105 |
107 void GLES2ClientImpl::DrawAnimationFrame() { | 106 void GLES2ClientImpl::DrawAnimationFrame() { |
108 MojoTimeTicks now = GetTimeTicksNow(); | 107 MojoTimeTicks now = mojo::GetTimeTicksNow(); |
109 MojoTimeTicks offset = now - last_time_; | 108 MojoTimeTicks offset = now - last_time_; |
110 float delta = static_cast<float>(offset) / 1000000.; | 109 float delta = static_cast<float>(offset) / 1000000.; |
111 last_time_ = now; | 110 last_time_ = now; |
112 cube_.UpdateForTimeDelta(delta); | 111 cube_.UpdateForTimeDelta(delta); |
113 cube_.Draw(); | 112 cube_.Draw(); |
114 | 113 |
115 MojoGLES2SwapBuffers(); | 114 MojoGLES2SwapBuffers(); |
116 } | 115 } |
117 | 116 |
118 void GLES2ClientImpl::DrawAnimationFrameThunk(void* closure) { | 117 void GLES2ClientImpl::DrawAnimationFrameThunk(void* closure) { |
119 static_cast<GLES2ClientImpl*>(closure)->DrawAnimationFrame(); | 118 static_cast<GLES2ClientImpl*>(closure)->DrawAnimationFrame(); |
120 } | 119 } |
121 | 120 |
122 void GLES2ClientImpl::RequestAnimationFrames() { | 121 void GLES2ClientImpl::RequestAnimationFrames() { |
123 getting_animation_frames_ = true; | 122 getting_animation_frames_ = true; |
124 MojoGLES2RequestAnimationFrames(context_); | 123 MojoGLES2RequestAnimationFrames(context_); |
125 last_time_ = GetTimeTicksNow(); | 124 last_time_ = mojo::GetTimeTicksNow(); |
126 } | 125 } |
127 | 126 |
128 void GLES2ClientImpl::CancelAnimationFrames() { | 127 void GLES2ClientImpl::CancelAnimationFrames() { |
129 getting_animation_frames_ = false; | 128 getting_animation_frames_ = false; |
130 MojoGLES2CancelAnimationFrames(context_); | 129 MojoGLES2CancelAnimationFrames(context_); |
131 } | 130 } |
132 | 131 |
133 } // namespace examples | 132 } // namespace examples |
134 } // namespace mojo | |
OLD | NEW |