Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(755)

Side by Side Diff: mojo/examples/sample_app/gles2_client_impl.cc

Issue 384513003: Remove RequestAnimationFrame from mojo, add delayed tasks to RunLoop (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/examples/sample_app/gles2_client_impl.h ('k') | mojo/gles2/command_buffer_client_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "mojo/public/cpp/utility/run_loop.h"
13 14
14 namespace examples { 15 namespace examples {
15 namespace { 16 namespace {
16 17
17 float CalculateDragDistance(const mojo::Point& start, const mojo::Point& end) { 18 float CalculateDragDistance(const mojo::Point& start, const mojo::Point& end) {
18 return hypot(static_cast<float>(start.x - end.x), 19 return hypot(static_cast<float>(start.x - end.x),
19 static_cast<float>(start.y - end.y)); 20 static_cast<float>(start.y - end.y));
20 } 21 }
21 22
22 float GetRandomColor() { 23 float GetRandomColor() {
23 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); 24 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
24 } 25 }
25 26
26 } 27 }
27 28
28 GLES2ClientImpl::GLES2ClientImpl(mojo::CommandBufferPtr command_buffer) 29 GLES2ClientImpl::GLES2ClientImpl(mojo::CommandBufferPtr command_buffer)
29 : getting_animation_frames_(false) { 30 : last_time_(mojo::GetTimeTicksNow()), waiting_to_draw_(false) {
30 context_ = MojoGLES2CreateContext( 31 context_ = MojoGLES2CreateContext(
31 command_buffer.PassMessagePipe().release().value(), 32 command_buffer.PassMessagePipe().release().value(),
32 &ContextLostThunk, 33 &ContextLostThunk,
33 &DrawAnimationFrameThunk,
34 this); 34 this);
35 MojoGLES2MakeCurrent(context_); 35 MojoGLES2MakeCurrent(context_);
36 } 36 }
37 37
38 GLES2ClientImpl::~GLES2ClientImpl() { 38 GLES2ClientImpl::~GLES2ClientImpl() {
39 MojoGLES2DestroyContext(context_); 39 MojoGLES2DestroyContext(context_);
40 } 40 }
41 41
42 void GLES2ClientImpl::SetSize(const mojo::Size& size) { 42 void GLES2ClientImpl::SetSize(const mojo::Size& size) {
43 size_ = size; 43 size_ = size;
44 if (size_.width == 0 || size_.height == 0) 44 if (size_.width == 0 || size_.height == 0)
45 return; 45 return;
46 cube_.Init(size_.width, size_.height); 46 cube_.Init(size_.width, size_.height);
47 RequestAnimationFrames(); 47 WantToDraw();
48 } 48 }
49 49
50 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) { 50 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) {
51 switch (event.action) { 51 switch (event.action) {
52 case mojo::EVENT_TYPE_MOUSE_PRESSED: 52 case mojo::EVENT_TYPE_MOUSE_PRESSED:
53 case mojo::EVENT_TYPE_TOUCH_PRESSED: 53 case mojo::EVENT_TYPE_TOUCH_PRESSED:
54 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) 54 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)
55 break; 55 break;
56 CancelAnimationFrames();
57 capture_point_ = *event.location; 56 capture_point_ = *event.location;
58 last_drag_point_ = capture_point_; 57 last_drag_point_ = capture_point_;
59 drag_start_time_ = mojo::GetTimeTicksNow(); 58 drag_start_time_ = mojo::GetTimeTicksNow();
60 break; 59 break;
61 case mojo::EVENT_TYPE_MOUSE_DRAGGED: 60 case mojo::EVENT_TYPE_MOUSE_DRAGGED:
62 case mojo::EVENT_TYPE_TOUCH_MOVED: 61 case mojo::EVENT_TYPE_TOUCH_MOVED: {
63 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) 62 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)
64 break; 63 break;
65 if (!getting_animation_frames_) { 64 int direction = event.location->y < last_drag_point_.y ||
66 int direction = event.location->y < last_drag_point_.y || 65 event.location->x > last_drag_point_.x
67 event.location->x > last_drag_point_.x ? 1 : -1; 66 ? 1
68 cube_.set_direction(direction); 67 : -1;
69 cube_.UpdateForDragDistance( 68 cube_.set_direction(direction);
70 CalculateDragDistance(last_drag_point_, *event.location)); 69 cube_.UpdateForDragDistance(
71 cube_.Draw(); 70 CalculateDragDistance(last_drag_point_, *event.location));
72 MojoGLES2SwapBuffers(); 71 WantToDraw();
73 72
74 last_drag_point_ = *event.location; 73 last_drag_point_ = *event.location;
75 }
76 break; 74 break;
75 }
77 case mojo::EVENT_TYPE_MOUSE_RELEASED: 76 case mojo::EVENT_TYPE_MOUSE_RELEASED:
78 case mojo::EVENT_TYPE_TOUCH_RELEASED: { 77 case mojo::EVENT_TYPE_TOUCH_RELEASED: {
79 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) { 78 if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) {
80 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); 79 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor());
81 break; 80 break;
82 } 81 }
83 MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_; 82 MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_;
84 float delta = static_cast<float>(offset) / 1000000.; 83 float delta = static_cast<float>(offset) / 1000000.;
85 cube_.SetFlingMultiplier( 84 cube_.SetFlingMultiplier(
86 CalculateDragDistance(capture_point_, *event.location), 85 CalculateDragDistance(capture_point_, *event.location),
87 delta); 86 delta);
88 87
89 capture_point_ = last_drag_point_ = mojo::Point(); 88 capture_point_ = last_drag_point_ = mojo::Point();
90 RequestAnimationFrames(); 89 WantToDraw();
91 break; 90 break;
92 } 91 }
93 default: 92 default:
94 break; 93 break;
95 } 94 }
96 } 95 }
97 96
98 void GLES2ClientImpl::ContextLost() { 97 void GLES2ClientImpl::ContextLost() {
99 CancelAnimationFrames();
100 } 98 }
101 99
102 void GLES2ClientImpl::ContextLostThunk(void* closure) { 100 void GLES2ClientImpl::ContextLostThunk(void* closure) {
103 static_cast<GLES2ClientImpl*>(closure)->ContextLost(); 101 static_cast<GLES2ClientImpl*>(closure)->ContextLost();
104 } 102 }
105 103
106 void GLES2ClientImpl::DrawAnimationFrame() { 104 struct DrawRunnable {
105 explicit DrawRunnable(GLES2ClientImpl* impl) : impl(impl) {}
106 virtual ~DrawRunnable() {}
107
108 void Run() const { impl->Draw(); }
109
110 GLES2ClientImpl* impl;
111 };
112
113 void GLES2ClientImpl::WantToDraw() {
114 if (waiting_to_draw_)
115 return;
116 waiting_to_draw_ = true;
117 mojo::RunLoop::current()->PostDelayedTask(mojo::Closure(DrawRunnable(this)),
darin (slow to review) 2014/07/25 02:47:50 actually, what ensures that |this| remains valid w
118 MojoTimeTicks(16667));
119 }
120
121 void GLES2ClientImpl::Draw() {
122 waiting_to_draw_ = false;
107 MojoTimeTicks now = mojo::GetTimeTicksNow(); 123 MojoTimeTicks now = mojo::GetTimeTicksNow();
108 MojoTimeTicks offset = now - last_time_; 124 MojoTimeTicks offset = now - last_time_;
109 float delta = static_cast<float>(offset) / 1000000.; 125 float delta = static_cast<float>(offset) / 1000000.;
110 last_time_ = now; 126 last_time_ = now;
111 cube_.UpdateForTimeDelta(delta); 127 cube_.UpdateForTimeDelta(delta);
112 cube_.Draw(); 128 cube_.Draw();
113 129
114 MojoGLES2SwapBuffers(); 130 MojoGLES2SwapBuffers();
115 } 131 WantToDraw();
116
117 void GLES2ClientImpl::DrawAnimationFrameThunk(void* closure) {
118 static_cast<GLES2ClientImpl*>(closure)->DrawAnimationFrame();
119 }
120
121 void GLES2ClientImpl::RequestAnimationFrames() {
122 getting_animation_frames_ = true;
123 MojoGLES2RequestAnimationFrames(context_);
124 last_time_ = mojo::GetTimeTicksNow();
125 }
126
127 void GLES2ClientImpl::CancelAnimationFrames() {
128 getting_animation_frames_ = false;
129 MojoGLES2CancelAnimationFrames(context_);
130 } 132 }
131 133
132 } // namespace examples 134 } // namespace examples
OLDNEW
« no previous file with comments | « mojo/examples/sample_app/gles2_client_impl.h ('k') | mojo/gles2/command_buffer_client_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698