Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / sample_app / gles2_client_impl.cc
index b3f2003..6cdd87a 100644 (file)
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
 #include <math.h>
+#include <stdlib.h>
 
-#include "mojo/public/gles2/gles2.h"
-#include "ui/events/event_constants.h"
+#include "gpu/command_buffer/client/gles2_interface.h"
+#include "mojo/public/c/gles2/gles2.h"
+#include "mojo/public/cpp/environment/environment.h"
+#include "mojo/public/cpp/utility/run_loop.h"
 
-namespace mojo {
 namespace examples {
 namespace {
 
-float CalculateDragDistance(const gfx::PointF& start, const Point& end) {
-  return hypot(start.x() - end.x(), start.y() - end.y());
+float CalculateDragDistance(const mojo::Point& start, const mojo::Point& end) {
+  return hypot(static_cast<float>(start.x - end.x),
+               static_cast<float>(start.y - end.y));
 }
 
+float GetRandomColor() {
+  return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
 }
 
-GLES2ClientImpl::GLES2ClientImpl(ScopedMessagePipeHandle pipe)
-    : getting_animation_frames_(false) {
-  context_ = MojoGLES2CreateContext(
-      pipe.release().value(),
-      &DidCreateContextThunk,
-      &ContextLostThunk,
-      &DrawAnimationFrameThunk,
-      this);
+}
+
+GLES2ClientImpl::GLES2ClientImpl(mojo::CommandBufferPtr command_buffer)
+    : last_time_(mojo::GetTimeTicksNow()), waiting_to_draw_(false) {
+  context_ =
+      MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(),
+                             &ContextLostThunk,
+                             this,
+                             mojo::Environment::GetDefaultAsyncWaiter());
+  MojoGLES2MakeCurrent(context_);
 }
 
 GLES2ClientImpl::~GLES2ClientImpl() {
   MojoGLES2DestroyContext(context_);
 }
 
-void GLES2ClientImpl::HandleInputEvent(const Event& event) {
-  switch (event.action()) {
-  case ui::ET_MOUSE_PRESSED:
-  case ui::ET_TOUCH_PRESSED:
-    CancelAnimationFrames();
-    capture_point_.SetPoint(event.location().x(), event.location().y());
+void GLES2ClientImpl::SetSize(const mojo::Size& size) {
+  size_ = size;
+  if (size_.width == 0 || size_.height == 0)
+    return;
+  static_cast<gpu::gles2::GLES2Interface*>(
+      MojoGLES2GetGLES2Interface(context_))->ResizeCHROMIUM(size_.width,
+                                                            size_.height,
+                                                            1);
+  cube_.Init(size_.width, size_.height);
+  WantToDraw();
+}
+
+void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) {
+  switch (event.action) {
+  case mojo::EVENT_TYPE_MOUSE_PRESSED:
+  case mojo::EVENT_TYPE_TOUCH_PRESSED:
+    if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)
+      break;
+    capture_point_ = *event.location_data->in_view_location;
     last_drag_point_ = capture_point_;
-    drag_start_time_ = GetTimeTicksNow();
+    drag_start_time_ = mojo::GetTimeTicksNow();
     break;
-  case ui::ET_MOUSE_DRAGGED:
-  case ui::ET_TOUCH_MOVED:
-    if (!getting_animation_frames_) {
-      int direction = event.location().y() < last_drag_point_.y() ||
-          event.location().x() > last_drag_point_.x() ? 1 : -1;
-      cube_.set_direction(direction);
-      cube_.UpdateForDragDistance(
-          CalculateDragDistance(last_drag_point_, event.location()));
-      cube_.Draw();
-      MojoGLES2SwapBuffers();
-
-      last_drag_point_.SetPoint(event.location().x(), event.location().y());
-    }
+  case mojo::EVENT_TYPE_MOUSE_DRAGGED:
+  case mojo::EVENT_TYPE_TOUCH_MOVED: {
+    if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)
+      break;
+    int direction =
+        (event.location_data->in_view_location->y < last_drag_point_.y ||
+         event.location_data->in_view_location->x > last_drag_point_.x)
+        ? 1 : -1;
+    cube_.set_direction(direction);
+    cube_.UpdateForDragDistance(CalculateDragDistance(
+        last_drag_point_, *event.location_data->in_view_location));
+    WantToDraw();
+
+    last_drag_point_ = *event.location_data->in_view_location;
     break;
-  case ui::ET_MOUSE_RELEASED:
-  case ui::ET_TOUCH_RELEASED: {
-      MojoTimeTicks offset = GetTimeTicksNow() - drag_start_time_;
-      float delta = static_cast<float>(offset) / 1000000.;
-      cube_.SetFlingMultiplier(
-          CalculateDragDistance(capture_point_, event.location()),
-          delta);
-
-      capture_point_ = last_drag_point_ = gfx::PointF();
-      RequestAnimationFrames();
+  }
+  case mojo::EVENT_TYPE_MOUSE_RELEASED:
+  case mojo::EVENT_TYPE_TOUCH_RELEASED: {
+    if (event.flags & mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON) {
+      cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor());
+      break;
     }
+    MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_;
+    float delta = static_cast<float>(offset) / 1000000.;
+    cube_.SetFlingMultiplier(CalculateDragDistance(
+        capture_point_, *event.location_data->in_view_location), delta);
+
+    capture_point_ = last_drag_point_ = mojo::Point();
+    WantToDraw();
     break;
+  }
   default:
     break;
   }
 }
 
-void GLES2ClientImpl::DidCreateContext(uint32_t width,
-                                       uint32_t height) {
-  MojoGLES2MakeCurrent(context_);
-
-  cube_.Init(width, height);
-  RequestAnimationFrames();
-}
-
-void GLES2ClientImpl::DidCreateContextThunk(
-    void* closure,
-    uint32_t width,
-    uint32_t height) {
-  static_cast<GLES2ClientImpl*>(closure)->DidCreateContext(width, height);
-}
-
 void GLES2ClientImpl::ContextLost() {
-  CancelAnimationFrames();
 }
 
 void GLES2ClientImpl::ContextLostThunk(void* closure) {
   static_cast<GLES2ClientImpl*>(closure)->ContextLost();
 }
 
-void GLES2ClientImpl::DrawAnimationFrame() {
-  MojoTimeTicks now = GetTimeTicksNow();
+struct DrawRunnable {
+  explicit DrawRunnable(GLES2ClientImpl* impl) : impl(impl) {}
+  virtual ~DrawRunnable() {}
+
+  void Run() const { impl->Draw(); }
+
+  GLES2ClientImpl* impl;
+};
+
+void GLES2ClientImpl::WantToDraw() {
+  if (waiting_to_draw_)
+    return;
+  waiting_to_draw_ = true;
+  mojo::RunLoop::current()->PostDelayedTask(mojo::Closure(DrawRunnable(this)),
+                                            MojoTimeTicks(16667));
+}
+
+void GLES2ClientImpl::Draw() {
+  waiting_to_draw_ = false;
+  MojoTimeTicks now = mojo::GetTimeTicksNow();
   MojoTimeTicks offset = now - last_time_;
   float delta = static_cast<float>(offset) / 1000000.;
   last_time_ = now;
@@ -107,22 +134,7 @@ void GLES2ClientImpl::DrawAnimationFrame() {
   cube_.Draw();
 
   MojoGLES2SwapBuffers();
-}
-
-void GLES2ClientImpl::DrawAnimationFrameThunk(void* closure) {
-  static_cast<GLES2ClientImpl*>(closure)->DrawAnimationFrame();
-}
-
-void GLES2ClientImpl::RequestAnimationFrames() {
-  getting_animation_frames_ = true;
-  MojoGLES2RequestAnimationFrames(context_);
-  last_time_ = GetTimeTicksNow();
-}
-
-void GLES2ClientImpl::CancelAnimationFrames() {
-  getting_animation_frames_ = false;
-  MojoGLES2CancelAnimationFrames(context_);
+  WantToDraw();
 }
 
 }  // namespace examples
-}  // namespace mojo