OLD | NEW |
| (Empty) |
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 "sky/compositor/layer_host.h" | |
6 | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "base/trace_event/trace_event.h" | |
9 #include "mojo/converters/geometry/geometry_type_converters.h" | |
10 #include "mojo/gpu/gl_context.h" | |
11 #include "mojo/services/surfaces/public/cpp/surfaces_utils.h" | |
12 #include "mojo/skia/ganesh_context.h" | |
13 #include "sky/compositor/layer.h" | |
14 | |
15 namespace sky { | |
16 | |
17 LayerHost::LayerHost(LayerHostClient* client) | |
18 : client_(client), | |
19 state_(kReadyForFrame), | |
20 frame_requested_(false), | |
21 surface_holder_(this, client->GetShell()), | |
22 gl_context_owner_(client->GetShell()), | |
23 ganesh_context_(gl_context()), | |
24 resource_manager_(gl_context()), | |
25 weak_factory_(this) { | |
26 } | |
27 | |
28 LayerHost::~LayerHost() { | |
29 } | |
30 | |
31 void LayerHost::SetNeedsAnimate() { | |
32 if (frame_requested_) | |
33 return; | |
34 frame_requested_ = true; | |
35 if (state_ == kReadyForFrame) | |
36 BeginFrameSoon(); | |
37 } | |
38 | |
39 void LayerHost::SetRootLayer(scoped_refptr<Layer> layer) { | |
40 DCHECK(!root_layer_.get()); | |
41 root_layer_ = layer; | |
42 } | |
43 | |
44 void LayerHost::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) { | |
45 client_->OnSurfaceIdAvailable(surface_id.Pass()); | |
46 } | |
47 | |
48 void LayerHost::ReturnResources( | |
49 mojo::Array<mojo::ReturnedResourcePtr> resources) { | |
50 resource_manager_.ReturnResources(resources.Pass()); | |
51 } | |
52 | |
53 void LayerHost::BeginFrameSoon() { | |
54 base::MessageLoop::current()->PostTask( | |
55 FROM_HERE, | |
56 base::Bind(&LayerHost::BeginFrame, weak_factory_.GetWeakPtr())); | |
57 } | |
58 | |
59 void LayerHost::BeginFrame() { | |
60 TRACE_EVENT0("sky", "LayerHost::BeginFrame"); | |
61 | |
62 DCHECK(frame_requested_); | |
63 frame_requested_ = false; | |
64 | |
65 DCHECK_EQ(state_, kReadyForFrame); | |
66 state_ = kWaitingForFrameAcknowldgement; | |
67 | |
68 client_->BeginFrame(base::TimeTicks::Now()); | |
69 | |
70 // If the root layer is empty, there's no reason to draw into it. (In fact, | |
71 // Ganesh will get upset if we try.) Instead, we just schedule the ack that | |
72 // the frame is complete. | |
73 if (root_layer_->size().IsEmpty()) { | |
74 base::MessageLoop::current()->PostTask( | |
75 FROM_HERE, | |
76 base::Bind(&LayerHost::DidCompleteFrame, weak_factory_.GetWeakPtr())); | |
77 return; | |
78 } | |
79 | |
80 { | |
81 mojo::GaneshContext::Scope scope(&ganesh_context_); | |
82 ganesh_context_.gr()->resetContext(); | |
83 root_layer_->Display(); | |
84 } | |
85 | |
86 Upload(root_layer_.get()); | |
87 } | |
88 | |
89 void LayerHost::Upload(Layer* layer) { | |
90 TRACE_EVENT0("sky", "LayerHost::Upload"); | |
91 | |
92 gfx::Size size = layer->size(); | |
93 surface_holder_.SetSize(size); | |
94 | |
95 mojo::FramePtr frame = mojo::Frame::New(); | |
96 frame->resources.resize(0u); | |
97 | |
98 mojo::Rect bounds; | |
99 bounds.width = size.width(); | |
100 bounds.height = size.height(); | |
101 mojo::PassPtr pass = mojo::CreateDefaultPass(1, bounds); | |
102 pass->quads.resize(0u); | |
103 pass->shared_quad_states.push_back(mojo::CreateDefaultSQS( | |
104 mojo::TypeConverter<mojo::Size, gfx::Size>::Convert(size))); | |
105 | |
106 mojo::TransferableResourcePtr resource = | |
107 resource_manager_.CreateTransferableResource(layer); | |
108 | |
109 mojo::QuadPtr quad = mojo::Quad::New(); | |
110 quad->material = mojo::MATERIAL_TEXTURE_CONTENT; | |
111 | |
112 mojo::RectPtr rect = mojo::Rect::New(); | |
113 rect->width = size.width(); | |
114 rect->height = size.height(); | |
115 quad->rect = rect.Clone(); | |
116 quad->opaque_rect = rect.Clone(); | |
117 quad->visible_rect = rect.Clone(); | |
118 quad->needs_blending = true; | |
119 quad->shared_quad_state_index = 0u; | |
120 | |
121 mojo::TextureQuadStatePtr texture_state = mojo::TextureQuadState::New(); | |
122 texture_state->resource_id = resource->id; | |
123 texture_state->premultiplied_alpha = true; | |
124 texture_state->uv_top_left = mojo::PointF::New(); | |
125 texture_state->uv_bottom_right = mojo::PointF::New(); | |
126 texture_state->uv_bottom_right->x = 1.f; | |
127 texture_state->uv_bottom_right->y = 1.f; | |
128 texture_state->background_color = mojo::Color::New(); | |
129 texture_state->background_color->rgba = 0; | |
130 for (int i = 0; i < 4; ++i) | |
131 texture_state->vertex_opacity.push_back(1.f); | |
132 texture_state->flipped = false; | |
133 | |
134 frame->resources.push_back(resource.Pass()); | |
135 quad->texture_quad_state = texture_state.Pass(); | |
136 pass->quads.push_back(quad.Pass()); | |
137 | |
138 frame->passes.push_back(pass.Pass()); | |
139 surface_holder_.SubmitFrame( | |
140 frame.Pass(), | |
141 base::Bind(&LayerHost::DidCompleteFrame, weak_factory_.GetWeakPtr())); | |
142 } | |
143 | |
144 void LayerHost::DidCompleteFrame() { | |
145 DCHECK_EQ(state_, kWaitingForFrameAcknowldgement); | |
146 state_ = kReadyForFrame; | |
147 if (frame_requested_) | |
148 BeginFrame(); | |
149 } | |
150 | |
151 } // namespace sky | |
OLD | NEW |