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.
5 #include "mojo/examples/surfaces_app/child_impl.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"
25 using cc::RenderPassId
;
27 using cc::SolidColorDrawQuad
;
28 using cc::DelegatedFrameData
;
29 using cc::CompositorFrame
;
31 ChildImpl::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()));
38 ChildImpl::~ChildImpl() {
40 surface_
->DestroySurface(mojo::SurfaceId::From(id_
));
43 void ChildImpl::ProduceFrame(
46 const mojo::Callback
<void(SurfaceIdPtr id
)>& callback
) {
47 color_
= color
.To
<SkColor
>();
48 size_
= size
.To
<gfx::Size
>();
49 produce_callback_
= callback
;
54 void 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())
63 void ChildImpl::ReturnResources(
64 Array
<ReturnedResourcePtr
> resources
) {
65 DCHECK(!resources
.size());
68 void 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());
77 CreateAndAppendSimpleSharedQuadState(pass
.get(), gfx::Transform(), size_
);
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(),
86 force_anti_aliasing_off
);
88 scoped_ptr
<DelegatedFrameData
> delegated_frame_data(new DelegatedFrameData
);
89 delegated_frame_data
->render_pass_list
.push_back(pass
.Pass());
91 scoped_ptr
<CompositorFrame
> frame(new CompositorFrame
);
92 frame
->delegated_frame_data
= delegated_frame_data
.Pass();
94 surface_
->SubmitFrame(mojo::SurfaceId::From(id_
),
95 mojo::Frame::From(*frame
));
96 produce_callback_
.Run(SurfaceId::From(id_
));
99 } // namespace examples