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/compositor_app/compositor_host.h"
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"
17 CompositorHost::CompositorHost(ScopedMessagePipeHandle gl_pipe)
18 : gl_pipe_(gl_pipe.Pass()), compositor_thread_("compositor") {
19 DCHECK(gl_pipe_.is_valid());
20 bool started = compositor_thread_.Start();
23 cc::LayerTreeSettings settings;
24 tree_ = cc::LayerTreeHost::CreateThreaded(
25 this, NULL, settings, compositor_thread_.message_loop_proxy());
29 CompositorHost::~CompositorHost() {}
31 void CompositorHost::SetSize(gfx::Size viewport_size) {
32 tree_->SetViewportSize(viewport_size);
33 tree_->SetLayerTreeHostClientReady();
34 tree_->InitializeOutputSurfaceIfNeeded();
37 void CompositorHost::SetupScene() {
38 scoped_refptr<cc::Layer> root_layer = cc::SolidColorLayer::Create();
39 root_layer->SetBounds(gfx::Size(500, 500));
40 root_layer->SetBackgroundColor(SK_ColorBLUE);
41 root_layer->SetIsDrawable(true);
42 tree_->SetRootLayer(root_layer);
44 child_layer_ = cc::SolidColorLayer::Create();
45 child_layer_->SetBounds(gfx::Size(100, 100));
46 child_layer_->SetBackgroundColor(SK_ColorGREEN);
47 child_layer_->SetIsDrawable(true);
48 gfx::Transform child_transform;
49 child_transform.Translate(200, 200);
50 child_layer_->SetTransform(child_transform);
51 root_layer->AddChild(child_layer_);
54 void CompositorHost::WillBeginMainFrame(int frame_id) {}
55 void CompositorHost::DidBeginMainFrame() {}
57 void CompositorHost::Animate(base::TimeTicks frame_begin_time) {
58 // TODO(jamesr): Should use cc's animation system.
59 static const double kDegreesPerSecond = 70.0;
60 double time_in_seconds = (frame_begin_time - base::TimeTicks()).InSecondsF();
61 double child_rotation_degrees = kDegreesPerSecond * time_in_seconds;
62 gfx::Transform child_transform;
63 child_transform.Translate(200, 200);
64 child_transform.Rotate(child_rotation_degrees);
65 child_layer_->SetTransform(child_transform);
66 tree_->SetNeedsAnimate();
69 void CompositorHost::Layout() {}
70 void CompositorHost::ApplyScrollAndScale(const gfx::Vector2d& scroll_delta,
73 scoped_ptr<cc::OutputSurface> CompositorHost::CreateOutputSurface(
75 return make_scoped_ptr(
76 new cc::OutputSurface(new ContextProviderMojo(gl_pipe_.Pass())));
79 void CompositorHost::DidInitializeOutputSurface() {
82 void CompositorHost::WillCommit() {}
83 void CompositorHost::DidCommit() {}
84 void CompositorHost::DidCommitAndDrawFrame() {}
85 void CompositorHost::DidCompleteSwapBuffers() {}
87 } // namespace examples