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.
5 #include "mojo/examples/sample_app/gles2_client_impl.h"
8 #include <GLES2/gl2ext.h>
11 #include "mojo/public/gles2/gles2.h"
12 #include "ui/events/event_constants.h"
18 float CalculateDragDistance(const gfx::PointF& start, const Point& end) {
19 return hypot(start.x() - end.x(), start.y() - end.y());
24 GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe)
25 : getting_animation_frames_(false) {
26 context_ = MojoGLES2CreateContext(
27 pipe.release().value(),
29 &DrawAnimationFrameThunk,
31 MojoGLES2MakeCurrent(context_);
34 GLES2ClientImpl::~GLES2ClientImpl() {
35 MojoGLES2DestroyContext(context_);
38 void GLES2ClientImpl::SetSize(const Size& size) {
39 size_ = gfx::Size(size.width(), size.height());
42 cube_.Init(size_.width(), size_.height());
43 RequestAnimationFrames();
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();
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()));
64 MojoGLES2SwapBuffers();
66 last_drag_point_.SetPoint(event.location().x(), event.location().y());
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()),
77 capture_point_ = last_drag_point_ = gfx::PointF();
78 RequestAnimationFrames();
86 void GLES2ClientImpl::ContextLost() {
87 CancelAnimationFrames();
90 void GLES2ClientImpl::ContextLostThunk(void* closure) {
91 static_cast<GLES2ClientImpl*>(closure)->ContextLost();
94 void GLES2ClientImpl::DrawAnimationFrame() {
95 MojoTimeTicks now = GetTimeTicksNow();
96 MojoTimeTicks offset = now - last_time_;
97 float delta = static_cast<float>(offset) / 1000000.;
99 cube_.UpdateForTimeDelta(delta);
102 MojoGLES2SwapBuffers();
105 void GLES2ClientImpl::DrawAnimationFrameThunk(void* closure) {
106 static_cast<GLES2ClientImpl*>(closure)->DrawAnimationFrame();
109 void GLES2ClientImpl::RequestAnimationFrames() {
110 getting_animation_frames_ = true;
111 MojoGLES2RequestAnimationFrames(context_);
112 last_time_ = GetTimeTicksNow();
115 void GLES2ClientImpl::CancelAnimationFrames() {
116 getting_animation_frames_ = false;
117 MojoGLES2CancelAnimationFrames(context_);
120 } // namespace examples