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