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/logging.h"
12 #include "base/trace_event/trace_event.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 // Create a clip rect for an aggregated quad from the original clip rect and
52 // the clip rect from the surface it's on.
53 SurfaceAggregator::ClipData
SurfaceAggregator::CalculateClipRect(
54 const ClipData
& surface_clip
,
55 const ClipData
& quad_clip
,
56 const gfx::Transform
& target_transform
) {
58 if (surface_clip
.is_clipped
)
59 out_clip
= surface_clip
;
61 if (quad_clip
.is_clipped
) {
62 // TODO(jamesr): This only works if target_transform maps integer
63 // rects to integer rects.
64 gfx::Rect final_clip
=
65 MathUtil::MapEnclosingClippedRect(target_transform
, quad_clip
.rect
);
66 if (out_clip
.is_clipped
)
67 out_clip
.rect
.Intersect(final_clip
);
69 out_clip
.rect
= final_clip
;
70 out_clip
.is_clipped
= true;
76 class SurfaceAggregator::RenderPassIdAllocator
{
78 explicit RenderPassIdAllocator(int* next_index
) : next_index_(next_index
) {}
79 ~RenderPassIdAllocator() {}
81 void AddKnownPass(RenderPassId id
) {
82 if (id_to_index_map_
.find(id
) != id_to_index_map_
.end())
84 id_to_index_map_
[id
] = (*next_index_
)++;
87 RenderPassId
Remap(RenderPassId id
) {
88 DCHECK(id_to_index_map_
.find(id
) != id_to_index_map_
.end());
89 return RenderPassId(1, id_to_index_map_
[id
]);
93 base::hash_map
<RenderPassId
, int> id_to_index_map_
;
96 DISALLOW_COPY_AND_ASSIGN(RenderPassIdAllocator
);
99 static void UnrefHelper(base::WeakPtr
<SurfaceFactory
> surface_factory
,
100 const ReturnedResourceArray
& resources
,
101 BlockingTaskRunner
* main_thread_task_runner
) {
103 surface_factory
->UnrefResources(resources
);
106 RenderPassId
SurfaceAggregator::RemapPassId(RenderPassId surface_local_pass_id
,
107 SurfaceId surface_id
) {
108 RenderPassIdAllocator
* allocator
= render_pass_allocator_map_
.get(surface_id
);
110 allocator
= new RenderPassIdAllocator(&next_render_pass_id_
);
111 render_pass_allocator_map_
.set(surface_id
, make_scoped_ptr(allocator
));
113 allocator
->AddKnownPass(surface_local_pass_id
);
114 return allocator
->Remap(surface_local_pass_id
);
117 int SurfaceAggregator::ChildIdForSurface(Surface
* surface
) {
118 SurfaceToResourceChildIdMap::iterator it
=
119 surface_id_to_resource_child_id_
.find(surface
->surface_id());
120 if (it
== surface_id_to_resource_child_id_
.end()) {
122 provider_
->CreateChild(base::Bind(&UnrefHelper
, surface
->factory()));
123 if (surface
->factory()) {
124 provider_
->SetChildNeedsSyncPoints(
125 child_id
, surface
->factory()->needs_sync_points());
127 surface_id_to_resource_child_id_
[surface
->surface_id()] = child_id
;
134 bool SurfaceAggregator::ValidateResources(
136 const DelegatedFrameData
* frame_data
) {
137 if (!provider_
) // TODO(jamesr): hack for unit tests that don't set up rp
140 int child_id
= ChildIdForSurface(surface
);
141 if (surface
->factory())
142 surface
->factory()->RefResources(frame_data
->resource_list
);
143 provider_
->ReceiveFromChild(child_id
, frame_data
->resource_list
);
145 ResourceProvider::ResourceIdSet referenced_resources
;
146 size_t reserve_size
= frame_data
->resource_list
.size();
147 #if defined(COMPILER_MSVC)
148 referenced_resources
.reserve(reserve_size
);
149 #elif defined(COMPILER_GCC)
150 // Pre-standard hash-tables only implement resize, which behaves similarly
151 // to reserve for these keys. Resizing to 0 may also be broken (particularly
153 // TODO(jbauman): Replace with reserve when C++11 is supported everywhere.
155 referenced_resources
.resize(reserve_size
);
158 bool invalid_frame
= false;
159 const ResourceProvider::ResourceIdMap
& child_to_parent_map
=
160 provider_
->GetChildToParentMap(child_id
);
161 for (const auto& render_pass
: frame_data
->render_pass_list
) {
162 for (const auto& quad
: render_pass
->quad_list
) {
163 for (ResourceId resource_id
: quad
->resources
) {
164 ResourceProvider::ResourceIdMap::const_iterator it
=
165 child_to_parent_map
.find(resource_id
);
166 if (it
== child_to_parent_map
.end()) {
167 invalid_frame
= true;
170 referenced_resources
.insert(resource_id
);
176 provider_
->DeclareUsedResourcesFromChild(child_id
, referenced_resources
);
178 return invalid_frame
;
181 gfx::Rect
SurfaceAggregator::DamageRectForSurface(const Surface
* surface
,
182 const RenderPass
& source
,
183 const gfx::Rect
& full_rect
) {
184 int previous_index
= previous_contained_surfaces_
[surface
->surface_id()];
185 if (previous_index
== surface
->frame_index())
187 else if (previous_index
== surface
->frame_index() - 1)
188 return source
.damage_rect
;
192 void SurfaceAggregator::HandleSurfaceQuad(
193 const SurfaceDrawQuad
* surface_quad
,
194 const gfx::Transform
& target_transform
,
195 const ClipData
& clip_rect
,
196 RenderPass
* dest_pass
) {
197 SurfaceId surface_id
= surface_quad
->surface_id
;
198 // If this surface's id is already in our referenced set then it creates
199 // a cycle in the graph and should be dropped.
200 if (referenced_surfaces_
.count(surface_id
))
202 Surface
* surface
= manager_
->GetSurfaceForId(surface_id
);
204 contained_surfaces_
[surface_id
] = 0;
207 contained_surfaces_
[surface_id
] = surface
->frame_index();
208 const CompositorFrame
* frame
= surface
->GetEligibleFrame();
211 const DelegatedFrameData
* frame_data
= frame
->delegated_frame_data
.get();
215 std::multimap
<RenderPassId
, CopyOutputRequest
*> copy_requests
;
216 surface
->TakeCopyOutputRequests(©_requests
);
218 const RenderPassList
& render_pass_list
= frame_data
->render_pass_list
;
219 bool invalid_frame
= ValidateResources(surface
, frame_data
);
221 for (auto& request
: copy_requests
) {
222 request
.second
->SendEmptyResult();
223 delete request
.second
;
228 SurfaceSet::iterator it
= referenced_surfaces_
.insert(surface_id
).first
;
229 // TODO(vmpstr): provider check is a hack for unittests that don't set up a
230 // resource provider.
231 ResourceProvider::ResourceIdMap empty_map
;
232 const ResourceProvider::ResourceIdMap
& child_to_parent_map
=
233 provider_
? provider_
->GetChildToParentMap(ChildIdForSurface(surface
))
236 surface_quad
->shared_quad_state
->opacity
== 1.f
&& copy_requests
.empty();
238 gfx::Rect surface_damage
= DamageRectForSurface(
239 surface
, *render_pass_list
.back(), surface_quad
->visible_rect
);
240 const RenderPassList
& referenced_passes
= render_pass_list
;
241 size_t passes_to_copy
=
242 merge_pass
? referenced_passes
.size() - 1 : referenced_passes
.size();
243 for (size_t j
= 0; j
< passes_to_copy
; ++j
) {
244 const RenderPass
& source
= *referenced_passes
[j
];
246 size_t sqs_size
= source
.shared_quad_state_list
.size();
247 size_t dq_size
= source
.quad_list
.size();
248 scoped_ptr
<RenderPass
> copy_pass(RenderPass::Create(sqs_size
, dq_size
));
250 RenderPassId remapped_pass_id
= RemapPassId(source
.id
, surface_id
);
252 copy_pass
->SetAll(remapped_pass_id
, source
.output_rect
, gfx::Rect(),
253 source
.transform_to_root_target
,
254 source
.has_transparent_background
);
256 MoveMatchingRequests(source
.id
, ©_requests
, ©_pass
->copy_requests
);
258 // Contributing passes aggregated in to the pass list need to take the
259 // transform of the surface quad into account to update their transform to
261 copy_pass
->transform_to_root_target
.ConcatTransform(
262 surface_quad
->shared_quad_state
->quad_to_target_transform
);
263 copy_pass
->transform_to_root_target
.ConcatTransform(target_transform
);
264 copy_pass
->transform_to_root_target
.ConcatTransform(
265 dest_pass
->transform_to_root_target
);
267 CopyQuadsToPass(source
.quad_list
, source
.shared_quad_state_list
,
268 child_to_parent_map
, gfx::Transform(), ClipData(),
269 copy_pass
.get(), surface_id
);
271 if (j
== referenced_passes
.size() - 1)
272 surface_damage
= gfx::UnionRects(surface_damage
, copy_pass
->damage_rect
);
274 dest_pass_list_
->push_back(copy_pass
.Pass());
277 gfx::Transform surface_transform
=
278 surface_quad
->shared_quad_state
->quad_to_target_transform
;
279 surface_transform
.ConcatTransform(target_transform
);
281 const RenderPass
& last_pass
= *render_pass_list
.back();
283 // TODO(jamesr): Clean up last pass special casing.
284 const QuadList
& quads
= last_pass
.quad_list
;
286 // Intersect the transformed visible rect and the clip rect to create a
287 // smaller cliprect for the quad.
288 ClipData
surface_quad_clip_rect(
289 true, MathUtil::MapEnclosingClippedRect(
290 surface_quad
->shared_quad_state
->quad_to_target_transform
,
291 surface_quad
->visible_rect
));
292 if (surface_quad
->shared_quad_state
->is_clipped
) {
293 surface_quad_clip_rect
.rect
.Intersect(
294 surface_quad
->shared_quad_state
->clip_rect
);
297 ClipData quads_clip
=
298 CalculateClipRect(clip_rect
, surface_quad_clip_rect
, target_transform
);
300 CopyQuadsToPass(quads
, last_pass
.shared_quad_state_list
,
301 child_to_parent_map
, surface_transform
, quads_clip
,
302 dest_pass
, surface_id
);
304 RenderPassId remapped_pass_id
= RemapPassId(last_pass
.id
, surface_id
);
306 CopySharedQuadState(surface_quad
->shared_quad_state
, target_transform
,
307 clip_rect
, dest_pass
);
309 SharedQuadState
* shared_quad_state
=
310 dest_pass
->shared_quad_state_list
.back();
311 RenderPassDrawQuad
* quad
=
312 dest_pass
->CreateAndAppendDrawQuad
<RenderPassDrawQuad
>();
313 quad
->SetNew(shared_quad_state
,
315 surface_quad
->visible_rect
,
325 dest_pass
->damage_rect
= gfx::UnionRects(
326 dest_pass
->damage_rect
,
327 MathUtil::MapEnclosingClippedRect(surface_transform
, surface_damage
));
329 referenced_surfaces_
.erase(it
);
332 void SurfaceAggregator::CopySharedQuadState(
333 const SharedQuadState
* source_sqs
,
334 const gfx::Transform
& target_transform
,
335 const ClipData
& clip_rect
,
336 RenderPass
* dest_render_pass
) {
337 SharedQuadState
* copy_shared_quad_state
=
338 dest_render_pass
->CreateAndAppendSharedQuadState();
339 copy_shared_quad_state
->CopyFrom(source_sqs
);
340 // target_transform contains any transformation that may exist
341 // between the context that these quads are being copied from (i.e. the
342 // surface's draw transform when aggregated from within a surface) to the
343 // target space of the pass. This will be identity except when copying the
344 // root draw pass from a surface into a pass when the surface draw quad's
345 // transform is not identity.
346 copy_shared_quad_state
->quad_to_target_transform
.ConcatTransform(
349 ClipData new_clip_rect
= CalculateClipRect(
350 clip_rect
, ClipData(source_sqs
->is_clipped
, source_sqs
->clip_rect
),
352 copy_shared_quad_state
->is_clipped
= new_clip_rect
.is_clipped
;
353 copy_shared_quad_state
->clip_rect
= new_clip_rect
.rect
;
356 void SurfaceAggregator::CopyQuadsToPass(
357 const QuadList
& source_quad_list
,
358 const SharedQuadStateList
& source_shared_quad_state_list
,
359 const ResourceProvider::ResourceIdMap
& child_to_parent_map
,
360 const gfx::Transform
& target_transform
,
361 const ClipData
& clip_rect
,
362 RenderPass
* dest_pass
,
363 SurfaceId surface_id
) {
364 const SharedQuadState
* last_copied_source_shared_quad_state
= NULL
;
366 SharedQuadStateList::ConstIterator sqs_iter
=
367 source_shared_quad_state_list
.begin();
368 for (const auto& quad
: source_quad_list
) {
369 while (quad
->shared_quad_state
!= *sqs_iter
) {
371 DCHECK(sqs_iter
!= source_shared_quad_state_list
.end());
373 DCHECK_EQ(quad
->shared_quad_state
, *sqs_iter
);
375 if (quad
->material
== DrawQuad::SURFACE_CONTENT
) {
376 const SurfaceDrawQuad
* surface_quad
= SurfaceDrawQuad::MaterialCast(quad
);
377 HandleSurfaceQuad(surface_quad
, target_transform
, clip_rect
, dest_pass
);
379 if (quad
->shared_quad_state
!= last_copied_source_shared_quad_state
) {
380 CopySharedQuadState(quad
->shared_quad_state
, target_transform
,
381 clip_rect
, dest_pass
);
382 last_copied_source_shared_quad_state
= quad
->shared_quad_state
;
385 if (quad
->material
== DrawQuad::RENDER_PASS
) {
386 const RenderPassDrawQuad
* pass_quad
=
387 RenderPassDrawQuad::MaterialCast(quad
);
388 RenderPassId original_pass_id
= pass_quad
->render_pass_id
;
389 RenderPassId remapped_pass_id
=
390 RemapPassId(original_pass_id
, surface_id
);
392 gfx::Rect pass_damage
;
393 for (const auto* pass
: *dest_pass_list_
) {
394 if (pass
->id
== remapped_pass_id
) {
395 pass_damage
= pass
->damage_rect
;
400 dest_quad
= dest_pass
->CopyFromAndAppendRenderPassDrawQuad(
401 pass_quad
, dest_pass
->shared_quad_state_list
.back(),
403 dest_pass
->damage_rect
= gfx::UnionRects(
404 dest_pass
->damage_rect
,
405 MathUtil::MapEnclosingClippedRect(
406 dest_quad
->shared_quad_state
->quad_to_target_transform
,
409 dest_quad
= dest_pass
->CopyFromAndAppendDrawQuad(
410 quad
, dest_pass
->shared_quad_state_list
.back());
412 if (!child_to_parent_map
.empty()) {
413 for (ResourceId
& resource_id
: dest_quad
->resources
) {
414 ResourceProvider::ResourceIdMap::const_iterator it
=
415 child_to_parent_map
.find(resource_id
);
416 DCHECK(it
!= child_to_parent_map
.end());
418 DCHECK_EQ(it
->first
, resource_id
);
419 ResourceId remapped_id
= it
->second
;
420 resource_id
= remapped_id
;
427 void SurfaceAggregator::CopyPasses(const DelegatedFrameData
* frame_data
,
429 // The root surface is allowed to have copy output requests, so grab them
430 // off its render passes.
431 std::multimap
<RenderPassId
, CopyOutputRequest
*> copy_requests
;
432 surface
->TakeCopyOutputRequests(©_requests
);
434 const RenderPassList
& source_pass_list
= frame_data
->render_pass_list
;
435 bool invalid_frame
= ValidateResources(surface
, frame_data
);
436 DCHECK(!invalid_frame
);
440 // TODO(vmpstr): provider check is a hack for unittests that don't set up a
441 // resource provider.
442 ResourceProvider::ResourceIdMap empty_map
;
443 const ResourceProvider::ResourceIdMap
& child_to_parent_map
=
444 provider_
? provider_
->GetChildToParentMap(ChildIdForSurface(surface
))
446 for (size_t i
= 0; i
< source_pass_list
.size(); ++i
) {
447 const RenderPass
& source
= *source_pass_list
[i
];
449 size_t sqs_size
= source
.shared_quad_state_list
.size();
450 size_t dq_size
= source
.quad_list
.size();
451 scoped_ptr
<RenderPass
> copy_pass(RenderPass::Create(sqs_size
, dq_size
));
453 MoveMatchingRequests(source
.id
, ©_requests
, ©_pass
->copy_requests
);
455 RenderPassId remapped_pass_id
=
456 RemapPassId(source
.id
, surface
->surface_id());
458 gfx::Rect damage_rect
=
459 (i
< source_pass_list
.size() - 1)
461 : DamageRectForSurface(surface
, source
, source
.output_rect
);
462 copy_pass
->SetAll(remapped_pass_id
, source
.output_rect
, damage_rect
,
463 source
.transform_to_root_target
,
464 source
.has_transparent_background
);
466 CopyQuadsToPass(source
.quad_list
, source
.shared_quad_state_list
,
467 child_to_parent_map
, gfx::Transform(), ClipData(),
468 copy_pass
.get(), surface
->surface_id());
470 dest_pass_list_
->push_back(copy_pass
.Pass());
474 void SurfaceAggregator::RemoveUnreferencedChildren() {
475 for (const auto& surface
: previous_contained_surfaces_
) {
476 if (!contained_surfaces_
.count(surface
.first
)) {
477 SurfaceToResourceChildIdMap::iterator it
=
478 surface_id_to_resource_child_id_
.find(surface
.first
);
479 if (it
!= surface_id_to_resource_child_id_
.end()) {
480 provider_
->DestroyChild(it
->second
);
481 surface_id_to_resource_child_id_
.erase(it
);
484 Surface
* surface_ptr
= manager_
->GetSurfaceForId(surface
.first
);
486 surface_ptr
->RunDrawCallbacks(SurfaceDrawStatus::DRAW_SKIPPED
);
491 scoped_ptr
<CompositorFrame
> SurfaceAggregator::Aggregate(SurfaceId surface_id
) {
492 Surface
* surface
= manager_
->GetSurfaceForId(surface_id
);
494 contained_surfaces_
[surface_id
] = surface
->frame_index();
495 const CompositorFrame
* root_surface_frame
= surface
->GetEligibleFrame();
496 if (!root_surface_frame
)
498 TRACE_EVENT0("cc", "SurfaceAggregator::Aggregate");
500 scoped_ptr
<CompositorFrame
> frame(new CompositorFrame
);
501 frame
->delegated_frame_data
= make_scoped_ptr(new DelegatedFrameData
);
503 DCHECK(root_surface_frame
->delegated_frame_data
);
505 SurfaceSet::iterator it
= referenced_surfaces_
.insert(surface_id
).first
;
507 dest_resource_list_
= &frame
->delegated_frame_data
->resource_list
;
508 dest_pass_list_
= &frame
->delegated_frame_data
->render_pass_list
;
510 CopyPasses(root_surface_frame
->delegated_frame_data
.get(), surface
);
512 referenced_surfaces_
.erase(it
);
513 DCHECK(referenced_surfaces_
.empty());
515 if (dest_pass_list_
->empty())
518 dest_pass_list_
= NULL
;
519 RemoveUnreferencedChildren();
520 contained_surfaces_
.swap(previous_contained_surfaces_
);
521 contained_surfaces_
.clear();
523 for (SurfaceIndexMap::iterator it
= previous_contained_surfaces_
.begin();
524 it
!= previous_contained_surfaces_
.end();
526 Surface
* surface
= manager_
->GetSurfaceForId(it
->first
);
528 surface
->TakeLatencyInfo(&frame
->metadata
.latency_info
);
531 // TODO(jamesr): Aggregate all resource references into the returned frame's
537 void SurfaceAggregator::ReleaseResources(SurfaceId surface_id
) {
538 SurfaceToResourceChildIdMap::iterator it
=
539 surface_id_to_resource_child_id_
.find(surface_id
);
540 if (it
!= surface_id_to_resource_child_id_
.end()) {
541 provider_
->DestroyChild(it
->second
);
542 surface_id_to_resource_child_id_
.erase(it
);
546 void SurfaceAggregator::SetFullDamageForSurface(SurfaceId surface_id
) {
547 auto it
= previous_contained_surfaces_
.find(surface_id
);
548 if (it
== previous_contained_surfaces_
.end())
550 // Set the last drawn index as 0 to ensure full damage next time it's drawn.