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 "cc/surfaces/surface_aggregator.h"
10 #include "base/containers/hash_tables.h"
11 #include "base/debug/trace_event.h"
12 #include "base/logging.h"
13 #include "cc/base/math_util.h"
14 #include "cc/output/compositor_frame.h"
15 #include "cc/output/delegated_frame_data.h"
16 #include "cc/quads/draw_quad.h"
17 #include "cc/quads/render_pass_draw_quad.h"
18 #include "cc/quads/shared_quad_state.h"
19 #include "cc/quads/surface_draw_quad.h"
20 #include "cc/surfaces/surface.h"
21 #include "cc/surfaces/surface_factory.h"
22 #include "cc/surfaces/surface_manager.h"
23 #include "cc/trees/blocking_task_runner.h"
28 void MoveMatchingRequests(
30 std::multimap
<RenderPassId
, CopyOutputRequest
*>* copy_requests
,
31 ScopedPtrVector
<CopyOutputRequest
>* output_requests
) {
32 auto request_range
= copy_requests
->equal_range(id
);
33 for (auto it
= request_range
.first
; it
!= request_range
.second
; ++it
) {
35 output_requests
->push_back(scoped_ptr
<CopyOutputRequest
>(it
->second
));
38 copy_requests
->erase(request_range
.first
, request_range
.second
);
43 SurfaceAggregator::SurfaceAggregator(SurfaceManager
* manager
,
44 ResourceProvider
* provider
)
45 : manager_(manager
), provider_(provider
), next_render_pass_id_(1) {
49 SurfaceAggregator::~SurfaceAggregator() {}
51 class SurfaceAggregator::RenderPassIdAllocator
{
53 explicit RenderPassIdAllocator(int* next_index
) : next_index_(next_index
) {}
54 ~RenderPassIdAllocator() {}
56 void AddKnownPass(RenderPassId id
) {
57 if (id_to_index_map_
.find(id
) != id_to_index_map_
.end())
59 id_to_index_map_
[id
] = (*next_index_
)++;
62 RenderPassId
Remap(RenderPassId id
) {
63 DCHECK(id_to_index_map_
.find(id
) != id_to_index_map_
.end());
64 return RenderPassId(1, id_to_index_map_
[id
]);
68 base::hash_map
<RenderPassId
, int> id_to_index_map_
;
71 DISALLOW_COPY_AND_ASSIGN(RenderPassIdAllocator
);
74 static void UnrefHelper(base::WeakPtr
<SurfaceFactory
> surface_factory
,
75 const ReturnedResourceArray
& resources
,
76 BlockingTaskRunner
* main_thread_task_runner
) {
78 surface_factory
->UnrefResources(resources
);
81 RenderPassId
SurfaceAggregator::RemapPassId(RenderPassId surface_local_pass_id
,
82 SurfaceId surface_id
) {
83 RenderPassIdAllocator
* allocator
= render_pass_allocator_map_
.get(surface_id
);
85 allocator
= new RenderPassIdAllocator(&next_render_pass_id_
);
86 render_pass_allocator_map_
.set(surface_id
, make_scoped_ptr(allocator
));
88 allocator
->AddKnownPass(surface_local_pass_id
);
89 return allocator
->Remap(surface_local_pass_id
);
92 int SurfaceAggregator::ChildIdForSurface(Surface
* surface
) {
93 SurfaceToResourceChildIdMap::iterator it
=
94 surface_id_to_resource_child_id_
.find(surface
->surface_id());
95 if (it
== surface_id_to_resource_child_id_
.end()) {
97 provider_
->CreateChild(base::Bind(&UnrefHelper
, surface
->factory()));
98 surface_id_to_resource_child_id_
[surface
->surface_id()] = child_id
;
105 static ResourceProvider::ResourceId
ResourceRemapHelper(
107 const ResourceProvider::ResourceIdMap
& child_to_parent_map
,
108 ResourceProvider::ResourceIdArray
* resources_in_frame
,
109 ResourceProvider::ResourceId id
) {
110 ResourceProvider::ResourceIdMap::const_iterator it
=
111 child_to_parent_map
.find(id
);
112 if (it
== child_to_parent_map
.end()) {
113 *invalid_frame
= true;
117 DCHECK_EQ(it
->first
, id
);
118 ResourceProvider::ResourceId remapped_id
= it
->second
;
119 resources_in_frame
->push_back(id
);
123 bool SurfaceAggregator::TakeResources(Surface
* surface
,
124 const DelegatedFrameData
* frame_data
,
125 RenderPassList
* render_pass_list
) {
126 RenderPass::CopyAll(frame_data
->render_pass_list
, render_pass_list
);
127 if (!provider_
) // TODO(jamesr): hack for unit tests that don't set up rp
130 int child_id
= ChildIdForSurface(surface
);
131 provider_
->ReceiveFromChild(child_id
, frame_data
->resource_list
);
132 if (surface
->factory())
133 surface
->factory()->RefResources(frame_data
->resource_list
);
135 typedef ResourceProvider::ResourceIdArray IdArray
;
136 IdArray referenced_resources
;
138 bool invalid_frame
= false;
139 DrawQuad::ResourceIteratorCallback remap
=
140 base::Bind(&ResourceRemapHelper
,
142 provider_
->GetChildToParentMap(child_id
),
143 &referenced_resources
);
144 for (const auto& render_pass
: *render_pass_list
) {
145 for (const auto& quad
: render_pass
->quad_list
)
146 quad
->IterateResources(remap
);
150 provider_
->DeclareUsedResourcesFromChild(child_id
, referenced_resources
);
152 return invalid_frame
;
155 gfx::Rect
SurfaceAggregator::DamageRectForSurface(const Surface
* surface
,
156 const RenderPass
& source
) {
157 int previous_index
= previous_contained_surfaces_
[surface
->surface_id()];
158 if (previous_index
== surface
->frame_index())
160 else if (previous_index
== surface
->frame_index() - 1)
161 return source
.damage_rect
;
162 return gfx::Rect(surface
->size());
165 void SurfaceAggregator::HandleSurfaceQuad(const SurfaceDrawQuad
* surface_quad
,
167 RenderPass
* dest_pass
) {
168 SurfaceId surface_id
= surface_quad
->surface_id
;
169 // If this surface's id is already in our referenced set then it creates
170 // a cycle in the graph and should be dropped.
171 if (referenced_surfaces_
.count(surface_id
))
173 Surface
* surface
= manager_
->GetSurfaceForId(surface_id
);
175 contained_surfaces_
[surface_id
] = 0;
178 contained_surfaces_
[surface_id
] = surface
->frame_index();
179 const CompositorFrame
* frame
= surface
->GetEligibleFrame();
182 const DelegatedFrameData
* frame_data
= frame
->delegated_frame_data
.get();
186 std::multimap
<RenderPassId
, CopyOutputRequest
*> copy_requests
;
187 surface
->TakeCopyOutputRequests(©_requests
);
189 RenderPassList render_pass_list
;
190 bool invalid_frame
= TakeResources(surface
, frame_data
, &render_pass_list
);
192 for (auto& request
: copy_requests
) {
193 request
.second
->SendEmptyResult();
194 delete request
.second
;
199 SurfaceSet::iterator it
= referenced_surfaces_
.insert(surface_id
).first
;
201 bool merge_pass
= copy_requests
.empty();
203 const RenderPassList
& referenced_passes
= render_pass_list
;
204 size_t passes_to_copy
=
205 merge_pass
? referenced_passes
.size() - 1 : referenced_passes
.size();
206 for (size_t j
= 0; j
< passes_to_copy
; ++j
) {
207 const RenderPass
& source
= *referenced_passes
[j
];
209 size_t sqs_size
= source
.shared_quad_state_list
.size();
210 size_t dq_size
= source
.quad_list
.size();
211 scoped_ptr
<RenderPass
> copy_pass(RenderPass::Create(sqs_size
, dq_size
));
213 RenderPassId remapped_pass_id
= RemapPassId(source
.id
, surface_id
);
215 copy_pass
->SetAll(remapped_pass_id
,
218 source
.transform_to_root_target
,
219 source
.has_transparent_background
);
221 MoveMatchingRequests(source
.id
, ©_requests
, ©_pass
->copy_requests
);
223 // Contributing passes aggregated in to the pass list need to take the
224 // transform of the surface quad into account to update their transform to
226 // TODO(jamesr): Make sure this is sufficient for surfaces nested several
227 // levels deep and add tests.
228 copy_pass
->transform_to_root_target
.ConcatTransform(
229 surface_quad
->quadTransform());
231 CopyQuadsToPass(source
.quad_list
, source
.shared_quad_state_list
,
232 gfx::Transform(), 1.f
, copy_pass
.get(), surface_id
);
234 dest_pass_list_
->push_back(copy_pass
.Pass());
237 const RenderPass
& last_pass
= *render_pass_list
.back();
239 // TODO(jamesr): Clean up last pass special casing.
240 const QuadList
& quads
= last_pass
.quad_list
;
242 // TODO(jamesr): Make sure clipping is enforced.
243 CopyQuadsToPass(quads
, last_pass
.shared_quad_state_list
,
244 surface_quad
->quadTransform(),
245 surface_quad
->opacity() * opacity
, dest_pass
, surface_id
);
247 RenderPassId remapped_pass_id
= RemapPassId(last_pass
.id
, surface_id
);
249 SharedQuadState
* shared_quad_state
=
250 dest_pass
->CreateAndAppendSharedQuadState();
251 shared_quad_state
->CopyFrom(surface_quad
->shared_quad_state
);
252 shared_quad_state
->opacity
*= opacity
;
253 RenderPassDrawQuad
* quad
=
254 dest_pass
->CreateAndAppendDrawQuad
<RenderPassDrawQuad
>();
255 quad
->SetNew(shared_quad_state
,
257 surface_quad
->visible_rect
,
266 dest_pass
->damage_rect
=
267 gfx::UnionRects(dest_pass
->damage_rect
,
268 MathUtil::MapEnclosingClippedRect(
269 surface_quad
->quadTransform(),
270 DamageRectForSurface(surface
, last_pass
)));
272 referenced_surfaces_
.erase(it
);
275 void SurfaceAggregator::CopySharedQuadState(
276 const SharedQuadState
* source_sqs
,
277 const gfx::Transform
& content_to_target_transform
,
278 RenderPass
* dest_render_pass
) {
279 SharedQuadState
* copy_shared_quad_state
=
280 dest_render_pass
->CreateAndAppendSharedQuadState();
281 copy_shared_quad_state
->CopyFrom(source_sqs
);
282 // content_to_target_transform contains any transformation that may exist
283 // between the context that these quads are being copied from (i.e. the
284 // surface's draw transform when aggregated from within a surface) to the
285 // target space of the pass. This will be identity except when copying the
286 // root draw pass from a surface into a pass when the surface draw quad's
287 // transform is not identity.
288 copy_shared_quad_state
->content_to_target_transform
.ConcatTransform(
289 content_to_target_transform
);
290 if (copy_shared_quad_state
->is_clipped
) {
291 copy_shared_quad_state
->clip_rect
= MathUtil::MapEnclosingClippedRect(
292 content_to_target_transform
, copy_shared_quad_state
->clip_rect
);
296 void SurfaceAggregator::CopyQuadsToPass(
297 const QuadList
& source_quad_list
,
298 const SharedQuadStateList
& source_shared_quad_state_list
,
299 const gfx::Transform
& content_to_target_transform
,
301 RenderPass
* dest_pass
,
302 SurfaceId surface_id
) {
303 const SharedQuadState
* last_copied_source_shared_quad_state
= NULL
;
305 SharedQuadStateList::ConstIterator sqs_iter
=
306 source_shared_quad_state_list
.begin();
307 for (const auto& quad
: source_quad_list
) {
308 while (quad
->shared_quad_state
!= *sqs_iter
) {
310 DCHECK(sqs_iter
!= source_shared_quad_state_list
.end());
312 DCHECK_EQ(quad
->shared_quad_state
, *sqs_iter
);
314 if (quad
->material
== DrawQuad::SURFACE_CONTENT
) {
315 const SurfaceDrawQuad
* surface_quad
= SurfaceDrawQuad::MaterialCast(quad
);
316 HandleSurfaceQuad(surface_quad
, opacity
, dest_pass
);
318 if (quad
->shared_quad_state
!= last_copied_source_shared_quad_state
) {
320 quad
->shared_quad_state
, content_to_target_transform
, dest_pass
);
321 dest_pass
->shared_quad_state_list
.back()->opacity
*= opacity
;
322 last_copied_source_shared_quad_state
= quad
->shared_quad_state
;
324 if (quad
->material
== DrawQuad::RENDER_PASS
) {
325 const RenderPassDrawQuad
* pass_quad
=
326 RenderPassDrawQuad::MaterialCast(quad
);
327 RenderPassId original_pass_id
= pass_quad
->render_pass_id
;
328 RenderPassId remapped_pass_id
=
329 RemapPassId(original_pass_id
, surface_id
);
331 dest_pass
->CopyFromAndAppendRenderPassDrawQuad(
333 dest_pass
->shared_quad_state_list
.back(),
336 dest_pass
->CopyFromAndAppendDrawQuad(
337 quad
, dest_pass
->shared_quad_state_list
.back());
343 void SurfaceAggregator::CopyPasses(const DelegatedFrameData
* frame_data
,
345 RenderPassList source_pass_list
;
347 // The root surface is allowed to have copy output requests, so grab them
348 // off its render passes.
349 std::multimap
<RenderPassId
, CopyOutputRequest
*> copy_requests
;
350 surface
->TakeCopyOutputRequests(©_requests
);
352 bool invalid_frame
= TakeResources(surface
, frame_data
, &source_pass_list
);
353 DCHECK(!invalid_frame
);
355 for (size_t i
= 0; i
< source_pass_list
.size(); ++i
) {
356 const RenderPass
& source
= *source_pass_list
[i
];
358 size_t sqs_size
= source
.shared_quad_state_list
.size();
359 size_t dq_size
= source
.quad_list
.size();
360 scoped_ptr
<RenderPass
> copy_pass(RenderPass::Create(sqs_size
, dq_size
));
362 MoveMatchingRequests(source
.id
, ©_requests
, ©_pass
->copy_requests
);
364 RenderPassId remapped_pass_id
=
365 RemapPassId(source
.id
, surface
->surface_id());
367 copy_pass
->SetAll(remapped_pass_id
,
369 DamageRectForSurface(surface
, source
),
370 source
.transform_to_root_target
,
371 source
.has_transparent_background
);
373 CopyQuadsToPass(source
.quad_list
, source
.shared_quad_state_list
,
374 gfx::Transform(), 1.f
, copy_pass
.get(),
375 surface
->surface_id());
377 dest_pass_list_
->push_back(copy_pass
.Pass());
381 void SurfaceAggregator::RemoveUnreferencedChildren() {
382 for (const auto& surface
: previous_contained_surfaces_
) {
383 if (!contained_surfaces_
.count(surface
.first
)) {
384 SurfaceToResourceChildIdMap::iterator it
=
385 surface_id_to_resource_child_id_
.find(surface
.first
);
386 if (it
!= surface_id_to_resource_child_id_
.end()) {
387 provider_
->DestroyChild(it
->second
);
388 surface_id_to_resource_child_id_
.erase(it
);
394 scoped_ptr
<CompositorFrame
> SurfaceAggregator::Aggregate(SurfaceId surface_id
) {
395 Surface
* surface
= manager_
->GetSurfaceForId(surface_id
);
397 contained_surfaces_
[surface_id
] = surface
->frame_index();
398 const CompositorFrame
* root_surface_frame
= surface
->GetEligibleFrame();
399 if (!root_surface_frame
)
401 TRACE_EVENT0("cc", "SurfaceAggregator::Aggregate");
403 scoped_ptr
<CompositorFrame
> frame(new CompositorFrame
);
404 frame
->delegated_frame_data
= make_scoped_ptr(new DelegatedFrameData
);
406 DCHECK(root_surface_frame
->delegated_frame_data
);
408 SurfaceSet::iterator it
= referenced_surfaces_
.insert(surface_id
).first
;
410 dest_resource_list_
= &frame
->delegated_frame_data
->resource_list
;
411 dest_pass_list_
= &frame
->delegated_frame_data
->render_pass_list
;
413 CopyPasses(root_surface_frame
->delegated_frame_data
.get(), surface
);
415 referenced_surfaces_
.erase(it
);
416 DCHECK(referenced_surfaces_
.empty());
418 dest_pass_list_
= NULL
;
419 RemoveUnreferencedChildren();
420 contained_surfaces_
.swap(previous_contained_surfaces_
);
421 contained_surfaces_
.clear();
423 for (SurfaceIndexMap::iterator it
= previous_contained_surfaces_
.begin();
424 it
!= previous_contained_surfaces_
.end();
426 Surface
* surface
= manager_
->GetSurfaceForId(it
->first
);
428 surface
->TakeLatencyInfo(&frame
->metadata
.latency_info
);
431 // TODO(jamesr): Aggregate all resource references into the returned frame's