child_impl.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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/surfaces_app/child_impl.h"
6
7#include "base/bind.h"
8#include "cc/output/compositor_frame.h"
9#include "cc/output/delegated_frame_data.h"
10#include "cc/quads/render_pass.h"
11#include "cc/quads/solid_color_draw_quad.h"
12#include "mojo/examples/surfaces_app/surfaces_util.h"
13#include "mojo/public/cpp/application/application_connection.h"
14#include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
15#include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h"
16#include "mojo/services/public/interfaces/surfaces/surface_id.mojom.h"
17#include "mojo/services/public/interfaces/surfaces/surfaces.mojom.h"
18#include "ui/gfx/rect.h"
19#include "ui/gfx/transform.h"
20
21namespace mojo {
22namespace examples {
23
24using cc::RenderPass;
25using cc::DrawQuad;
26using cc::SolidColorDrawQuad;
27using cc::DelegatedFrameData;
28using cc::CompositorFrame;
29
30class SurfaceClientImpl : public surfaces::SurfaceClient {
31 public:
32  explicit SurfaceClientImpl(ChildImpl* impl) : impl_(impl) {}
33  virtual ~SurfaceClientImpl() {}
34
35  // surfaces::SurfaceClient implementation
36  virtual void SetIdNamespace(uint32_t id_namespace) OVERRIDE {
37    impl_->SetIdNamespace(id_namespace);
38  }
39
40  virtual void ReturnResources(
41      Array<surfaces::ReturnedResourcePtr> resources) OVERRIDE {
42    DCHECK(!resources.size());
43  }
44
45 private:
46  ChildImpl* impl_;
47};
48
49ChildImpl::ChildImpl(ApplicationConnection* connection, Context* context)
50    : surface_client_(new SurfaceClientImpl(this)) {
51  context->ShellConnection("mojo:mojo_surfaces_service")
52      ->ConnectToService(&surface_);
53  surface_.set_client(surface_client_.get());
54}
55
56ChildImpl::~ChildImpl() {
57  surface_->DestroySurface(mojo::surfaces::SurfaceId::From(id_));
58}
59
60void ChildImpl::ProduceFrame(
61    surfaces::ColorPtr color,
62    SizePtr size,
63    const mojo::Callback<void(surfaces::SurfaceIdPtr id)>& callback) {
64  color_ = color.To<SkColor>();
65  size_ = size.To<gfx::Size>();
66  produce_callback_ = callback;
67  if (allocator_)
68    Draw();
69}
70
71void ChildImpl::SetIdNamespace(uint32_t id_namespace) {
72  allocator_.reset(new cc::SurfaceIdAllocator(id_namespace));
73  if (!produce_callback_.is_null())
74    Draw();
75}
76
77void ChildImpl::Draw() {
78  id_ = allocator_->GenerateId();
79  surface_->CreateSurface(mojo::surfaces::SurfaceId::From(id_),
80                          mojo::Size::From(size_));
81  gfx::Rect rect(size_);
82  RenderPass::Id id(1, 1);
83  scoped_ptr<RenderPass> pass = RenderPass::Create();
84  pass->SetNew(id, rect, rect, gfx::Transform());
85
86  CreateAndAppendSimpleSharedQuadState(pass.get(), gfx::Transform(), size_);
87
88  SolidColorDrawQuad* color_quad =
89      pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
90  bool force_anti_aliasing_off = false;
91  color_quad->SetNew(pass->shared_quad_state_list.back(),
92                     rect,
93                     rect,
94                     color_,
95                     force_anti_aliasing_off);
96
97  scoped_ptr<DelegatedFrameData> delegated_frame_data(new DelegatedFrameData);
98  delegated_frame_data->render_pass_list.push_back(pass.Pass());
99
100  scoped_ptr<CompositorFrame> frame(new CompositorFrame);
101  frame->delegated_frame_data = delegated_frame_data.Pass();
102
103  surface_->SubmitFrame(mojo::surfaces::SurfaceId::From(id_),
104                        mojo::surfaces::Frame::From(*frame));
105  produce_callback_.Run(surfaces::SurfaceId::From(id_));
106}
107
108}  // namespace examples
109}  // namespace mojo
110