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