1c47cdb59caa7b6e124d1d6a10170011733c6f35
[platform/framework/web/crosswalk.git] / src / mojo / examples / compositor_app / compositor_host.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/compositor_app/compositor_host.h"
6
7 #include "cc/layers/layer.h"
8 #include "cc/layers/solid_color_layer.h"
9 #include "cc/output/context_provider.h"
10 #include "cc/output/output_surface.h"
11 #include "cc/trees/layer_tree_host.h"
12 #include "mojo/cc/context_provider_mojo.h"
13
14 namespace mojo {
15 namespace examples {
16
17 CompositorHost::CompositorHost(ScopedMessagePipeHandle command_buffer_handle)
18     : command_buffer_handle_(command_buffer_handle.Pass()),
19       compositor_thread_("compositor") {
20   DCHECK(command_buffer_handle_.is_valid());
21   bool started = compositor_thread_.Start();
22   DCHECK(started);
23
24   cc::LayerTreeSettings settings;
25   tree_ = cc::LayerTreeHost::CreateThreaded(
26       this,
27       NULL,
28       settings,
29       base::MessageLoopProxy::current(),
30       compositor_thread_.message_loop_proxy());
31   SetupScene();
32 }
33
34 CompositorHost::~CompositorHost() {}
35
36 void CompositorHost::SetSize(const gfx::Size& viewport_size) {
37   tree_->SetViewportSize(viewport_size);
38   tree_->SetLayerTreeHostClientReady();
39 }
40
41 void CompositorHost::SetupScene() {
42   scoped_refptr<cc::Layer> root_layer = cc::SolidColorLayer::Create();
43   root_layer->SetBounds(gfx::Size(500, 500));
44   root_layer->SetBackgroundColor(SK_ColorBLUE);
45   root_layer->SetIsDrawable(true);
46   tree_->SetRootLayer(root_layer);
47
48   child_layer_ = cc::SolidColorLayer::Create();
49   child_layer_->SetBounds(gfx::Size(100, 100));
50   child_layer_->SetBackgroundColor(SK_ColorGREEN);
51   child_layer_->SetIsDrawable(true);
52   gfx::Transform child_transform;
53   child_transform.Translate(200, 200);
54   child_layer_->SetTransform(child_transform);
55   root_layer->AddChild(child_layer_);
56 }
57
58 void CompositorHost::WillBeginMainFrame(int frame_id) {}
59 void CompositorHost::DidBeginMainFrame() {}
60
61 void CompositorHost::Animate(base::TimeTicks frame_begin_time) {
62   // TODO(jamesr): Should use cc's animation system.
63   static const double kDegreesPerSecond = 70.0;
64   double time_in_seconds = (frame_begin_time - base::TimeTicks()).InSecondsF();
65   double child_rotation_degrees = kDegreesPerSecond * time_in_seconds;
66   gfx::Transform child_transform;
67   child_transform.Translate(200, 200);
68   child_transform.Rotate(child_rotation_degrees);
69   child_layer_->SetTransform(child_transform);
70   tree_->SetNeedsAnimate();
71 }
72
73 void CompositorHost::Layout() {}
74 void CompositorHost::ApplyScrollAndScale(const gfx::Vector2d& scroll_delta,
75                                          float page_scale) {}
76
77 scoped_ptr<cc::OutputSurface> CompositorHost::CreateOutputSurface(
78     bool fallback) {
79   return make_scoped_ptr(
80       new cc::OutputSurface(
81           new ContextProviderMojo(command_buffer_handle_.Pass())));
82 }
83
84 void CompositorHost::DidInitializeOutputSurface() {
85 }
86
87 void CompositorHost::WillCommit() {}
88 void CompositorHost::DidCommit() {}
89 void CompositorHost::DidCommitAndDrawFrame() {}
90 void CompositorHost::DidCompleteSwapBuffers() {}
91
92 }  // namespace examples
93 }  // namespace mojo