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::RenderPassId;
26using cc::DrawQuad;
27using cc::SolidColorDrawQuad;
28using cc::DelegatedFrameData;
29using cc::CompositorFrame;
30
31ChildImpl::ChildImpl(ApplicationConnection* surfaces_service_connection)
32    : weak_factory_(this) {
33  surfaces_service_connection->ConnectToService(&surfaces_service_);
34  surfaces_service_->CreateSurfaceConnection(base::Bind(
35      &ChildImpl::SurfaceConnectionCreated, weak_factory_.GetWeakPtr()));
36}
37
38ChildImpl::~ChildImpl() {
39  if (surface_)
40    surface_->DestroySurface(mojo::SurfaceId::From(id_));
41}
42
43void ChildImpl::ProduceFrame(
44    ColorPtr color,
45    SizePtr size,
46    const mojo::Callback<void(SurfaceIdPtr id)>& callback) {
47  color_ = color.To<SkColor>();
48  size_ = size.To<gfx::Size>();
49  produce_callback_ = callback;
50  if (allocator_)
51    Draw();
52}
53
54void ChildImpl::SurfaceConnectionCreated(SurfacePtr surface,
55                                         uint32_t id_namespace) {
56  surface_ = surface.Pass();
57  surface_.set_client(this);
58  allocator_.reset(new cc::SurfaceIdAllocator(id_namespace));
59  if (!produce_callback_.is_null())
60    Draw();
61}
62
63void ChildImpl::ReturnResources(
64    Array<ReturnedResourcePtr> resources) {
65  DCHECK(!resources.size());
66}
67
68void ChildImpl::Draw() {
69  id_ = allocator_->GenerateId();
70  surface_->CreateSurface(mojo::SurfaceId::From(id_),
71                          mojo::Size::From(size_));
72  gfx::Rect rect(size_);
73  RenderPassId id(1, 1);
74  scoped_ptr<RenderPass> pass = RenderPass::Create();
75  pass->SetNew(id, rect, rect, gfx::Transform());
76
77  CreateAndAppendSimpleSharedQuadState(pass.get(), gfx::Transform(), size_);
78
79  SolidColorDrawQuad* color_quad =
80      pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
81  bool force_anti_aliasing_off = false;
82  color_quad->SetNew(pass->shared_quad_state_list.back(),
83                     rect,
84                     rect,
85                     color_,
86                     force_anti_aliasing_off);
87
88  scoped_ptr<DelegatedFrameData> delegated_frame_data(new DelegatedFrameData);
89  delegated_frame_data->render_pass_list.push_back(pass.Pass());
90
91  scoped_ptr<CompositorFrame> frame(new CompositorFrame);
92  frame->delegated_frame_data = delegated_frame_data.Pass();
93
94  surface_->SubmitFrame(mojo::SurfaceId::From(id_),
95                        mojo::Frame::From(*frame));
96  produce_callback_.Run(SurfaceId::From(id_));
97}
98
99}  // namespace examples
100}  // namespace mojo
101