Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / cc / surfaces / surface_aggregator.cc
blobb4123d01d48c3c376d6f7a19dd2ccd741c4dcab7
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"
7 #include <map>
9 #include "base/bind.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"
25 namespace cc {
26 namespace {
28 void MoveMatchingRequests(
29 RenderPassId id,
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) {
34 DCHECK(it->second);
35 output_requests->push_back(scoped_ptr<CopyOutputRequest>(it->second));
36 it->second = nullptr;
38 copy_requests->erase(request_range.first, request_range.second);
41 } // namespace
43 SurfaceAggregator::SurfaceAggregator(SurfaceManager* manager,
44 ResourceProvider* provider)
45 : manager_(manager), provider_(provider), next_render_pass_id_(1) {
46 DCHECK(manager_);
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& content_to_target_transform) {
57 ClipData out_clip;
58 if (surface_clip.is_clipped)
59 out_clip = surface_clip;
61 if (quad_clip.is_clipped) {
62 // TODO(jamesr): This only works if content_to_target_transform maps integer
63 // rects to integer rects.
64 gfx::Rect final_clip = MathUtil::MapEnclosingClippedRect(
65 content_to_target_transform, quad_clip.rect);
66 if (out_clip.is_clipped)
67 out_clip.rect.Intersect(final_clip);
68 else
69 out_clip.rect = final_clip;
70 out_clip.is_clipped = true;
73 return out_clip;
76 class SurfaceAggregator::RenderPassIdAllocator {
77 public:
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())
83 return;
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]);
92 private:
93 base::hash_map<RenderPassId, int> id_to_index_map_;
94 int* next_index_;
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) {
102 if (surface_factory)
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);
109 if (!allocator) {
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()) {
121 int child_id =
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;
128 return child_id;
129 } else {
130 return it->second;
134 static ResourceProvider::ResourceId ResourceRemapHelper(
135 const ResourceProvider::ResourceIdMap& child_to_parent_map,
136 ResourceProvider::ResourceId id) {
137 ResourceProvider::ResourceIdMap::const_iterator it =
138 child_to_parent_map.find(id);
139 DCHECK(it != child_to_parent_map.end());
141 DCHECK_EQ(it->first, id);
142 ResourceProvider::ResourceId remapped_id = it->second;
143 return remapped_id;
146 static ResourceProvider::ResourceId ValidateResourceHelper(
147 bool* invalid_frame,
148 const ResourceProvider::ResourceIdMap& child_to_parent_map,
149 ResourceProvider::ResourceIdSet* resources_in_frame,
150 ResourceProvider::ResourceId id) {
151 ResourceProvider::ResourceIdMap::const_iterator it =
152 child_to_parent_map.find(id);
153 if (it == child_to_parent_map.end()) {
154 *invalid_frame = true;
155 return id;
157 resources_in_frame->insert(id);
158 return id;
161 bool SurfaceAggregator::ValidateResources(
162 Surface* surface,
163 const DelegatedFrameData* frame_data) {
164 if (!provider_) // TODO(jamesr): hack for unit tests that don't set up rp
165 return false;
167 int child_id = ChildIdForSurface(surface);
168 if (surface->factory())
169 surface->factory()->RefResources(frame_data->resource_list);
170 provider_->ReceiveFromChild(child_id, frame_data->resource_list);
172 ResourceProvider::ResourceIdSet referenced_resources;
174 bool invalid_frame = false;
175 DrawQuad::ResourceIteratorCallback remap =
176 base::Bind(&ValidateResourceHelper, &invalid_frame,
177 base::ConstRef(provider_->GetChildToParentMap(child_id)),
178 &referenced_resources);
179 for (const auto& render_pass : frame_data->render_pass_list) {
180 for (const auto& quad : render_pass->quad_list)
181 quad->IterateResources(remap);
184 if (!invalid_frame)
185 provider_->DeclareUsedResourcesFromChild(child_id, referenced_resources);
187 return invalid_frame;
190 gfx::Rect SurfaceAggregator::DamageRectForSurface(const Surface* surface,
191 const RenderPass& source,
192 const gfx::Rect& full_rect) {
193 int previous_index = previous_contained_surfaces_[surface->surface_id()];
194 if (previous_index == surface->frame_index())
195 return gfx::Rect();
196 else if (previous_index == surface->frame_index() - 1)
197 return source.damage_rect;
198 return full_rect;
201 void SurfaceAggregator::HandleSurfaceQuad(
202 const SurfaceDrawQuad* surface_quad,
203 const gfx::Transform& content_to_target_transform,
204 const ClipData& clip_rect,
205 RenderPass* dest_pass) {
206 SurfaceId surface_id = surface_quad->surface_id;
207 // If this surface's id is already in our referenced set then it creates
208 // a cycle in the graph and should be dropped.
209 if (referenced_surfaces_.count(surface_id))
210 return;
211 Surface* surface = manager_->GetSurfaceForId(surface_id);
212 if (!surface) {
213 contained_surfaces_[surface_id] = 0;
214 return;
216 contained_surfaces_[surface_id] = surface->frame_index();
217 const CompositorFrame* frame = surface->GetEligibleFrame();
218 if (!frame)
219 return;
220 const DelegatedFrameData* frame_data = frame->delegated_frame_data.get();
221 if (!frame_data)
222 return;
224 std::multimap<RenderPassId, CopyOutputRequest*> copy_requests;
225 surface->TakeCopyOutputRequests(&copy_requests);
227 const RenderPassList& render_pass_list = frame_data->render_pass_list;
228 bool invalid_frame = ValidateResources(surface, frame_data);
229 if (invalid_frame) {
230 for (auto& request : copy_requests) {
231 request.second->SendEmptyResult();
232 delete request.second;
234 return;
237 SurfaceSet::iterator it = referenced_surfaces_.insert(surface_id).first;
238 DrawQuad::ResourceIteratorCallback remap;
239 if (provider_) {
240 int child_id = ChildIdForSurface(surface);
241 remap =
242 base::Bind(&ResourceRemapHelper,
243 base::ConstRef(provider_->GetChildToParentMap(child_id)));
246 bool merge_pass = surface_quad->opacity() == 1.f && copy_requests.empty();
248 gfx::Rect surface_damage = DamageRectForSurface(
249 surface, *render_pass_list.back(), surface_quad->visible_rect);
250 const RenderPassList& referenced_passes = render_pass_list;
251 size_t passes_to_copy =
252 merge_pass ? referenced_passes.size() - 1 : referenced_passes.size();
253 for (size_t j = 0; j < passes_to_copy; ++j) {
254 const RenderPass& source = *referenced_passes[j];
256 size_t sqs_size = source.shared_quad_state_list.size();
257 size_t dq_size = source.quad_list.size();
258 scoped_ptr<RenderPass> copy_pass(RenderPass::Create(sqs_size, dq_size));
260 RenderPassId remapped_pass_id = RemapPassId(source.id, surface_id);
262 copy_pass->SetAll(remapped_pass_id, source.output_rect, gfx::Rect(),
263 source.transform_to_root_target,
264 source.has_transparent_background);
266 MoveMatchingRequests(source.id, &copy_requests, &copy_pass->copy_requests);
268 // Contributing passes aggregated in to the pass list need to take the
269 // transform of the surface quad into account to update their transform to
270 // the root surface.
271 copy_pass->transform_to_root_target.ConcatTransform(
272 surface_quad->quadTransform());
273 copy_pass->transform_to_root_target.ConcatTransform(
274 content_to_target_transform);
275 copy_pass->transform_to_root_target.ConcatTransform(
276 dest_pass->transform_to_root_target);
278 CopyQuadsToPass(source.quad_list, source.shared_quad_state_list, remap,
279 gfx::Transform(), ClipData(), copy_pass.get(), surface_id);
281 if (j == referenced_passes.size() - 1)
282 surface_damage = gfx::UnionRects(surface_damage, copy_pass->damage_rect);
284 dest_pass_list_->push_back(copy_pass.Pass());
287 const RenderPass& last_pass = *render_pass_list.back();
288 if (merge_pass) {
289 // TODO(jamesr): Clean up last pass special casing.
290 const QuadList& quads = last_pass.quad_list;
292 gfx::Transform surface_transform = surface_quad->quadTransform();
293 surface_transform.ConcatTransform(content_to_target_transform);
295 // Intersect the transformed visible rect and the clip rect to create a
296 // smaller cliprect for the quad.
297 ClipData surface_quad_clip_rect(
298 true, MathUtil::MapEnclosingClippedRect(surface_quad->quadTransform(),
299 surface_quad->visible_rect));
300 if (surface_quad->isClipped())
301 surface_quad_clip_rect.rect.Intersect(surface_quad->clipRect());
303 ClipData quads_clip = CalculateClipRect(clip_rect, surface_quad_clip_rect,
304 content_to_target_transform);
306 CopyQuadsToPass(quads, last_pass.shared_quad_state_list, remap,
307 surface_transform, quads_clip, dest_pass, surface_id);
308 } else {
309 RenderPassId remapped_pass_id = RemapPassId(last_pass.id, surface_id);
311 CopySharedQuadState(surface_quad->shared_quad_state,
312 content_to_target_transform, clip_rect, dest_pass);
314 SharedQuadState* shared_quad_state =
315 dest_pass->shared_quad_state_list.back();
316 RenderPassDrawQuad* quad =
317 dest_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
318 quad->SetNew(shared_quad_state,
319 surface_quad->rect,
320 surface_quad->visible_rect,
321 remapped_pass_id,
323 gfx::Vector2dF(),
324 gfx::Size(),
325 FilterOperations(),
326 gfx::Vector2dF(),
327 FilterOperations());
329 dest_pass->damage_rect =
330 gfx::UnionRects(dest_pass->damage_rect,
331 MathUtil::MapEnclosingClippedRect(
332 surface_quad->quadTransform(), surface_damage));
334 referenced_surfaces_.erase(it);
337 void SurfaceAggregator::CopySharedQuadState(
338 const SharedQuadState* source_sqs,
339 const gfx::Transform& content_to_target_transform,
340 const ClipData& clip_rect,
341 RenderPass* dest_render_pass) {
342 SharedQuadState* copy_shared_quad_state =
343 dest_render_pass->CreateAndAppendSharedQuadState();
344 copy_shared_quad_state->CopyFrom(source_sqs);
345 // content_to_target_transform contains any transformation that may exist
346 // between the context that these quads are being copied from (i.e. the
347 // surface's draw transform when aggregated from within a surface) to the
348 // target space of the pass. This will be identity except when copying the
349 // root draw pass from a surface into a pass when the surface draw quad's
350 // transform is not identity.
351 copy_shared_quad_state->content_to_target_transform.ConcatTransform(
352 content_to_target_transform);
354 ClipData new_clip_rect = CalculateClipRect(
355 clip_rect, ClipData(source_sqs->is_clipped, source_sqs->clip_rect),
356 content_to_target_transform);
357 copy_shared_quad_state->is_clipped = new_clip_rect.is_clipped;
358 copy_shared_quad_state->clip_rect = new_clip_rect.rect;
361 void SurfaceAggregator::CopyQuadsToPass(
362 const QuadList& source_quad_list,
363 const SharedQuadStateList& source_shared_quad_state_list,
364 const DrawQuad::ResourceIteratorCallback& remap,
365 const gfx::Transform& content_to_target_transform,
366 const ClipData& clip_rect,
367 RenderPass* dest_pass,
368 SurfaceId surface_id) {
369 const SharedQuadState* last_copied_source_shared_quad_state = NULL;
371 SharedQuadStateList::ConstIterator sqs_iter =
372 source_shared_quad_state_list.begin();
373 for (const auto& quad : source_quad_list) {
374 while (quad->shared_quad_state != *sqs_iter) {
375 ++sqs_iter;
376 DCHECK(sqs_iter != source_shared_quad_state_list.end());
378 DCHECK_EQ(quad->shared_quad_state, *sqs_iter);
380 if (quad->material == DrawQuad::SURFACE_CONTENT) {
381 const SurfaceDrawQuad* surface_quad = SurfaceDrawQuad::MaterialCast(quad);
382 HandleSurfaceQuad(surface_quad, content_to_target_transform, clip_rect,
383 dest_pass);
384 } else {
385 if (quad->shared_quad_state != last_copied_source_shared_quad_state) {
386 CopySharedQuadState(quad->shared_quad_state,
387 content_to_target_transform, clip_rect, dest_pass);
388 last_copied_source_shared_quad_state = quad->shared_quad_state;
390 DrawQuad* dest_quad;
391 if (quad->material == DrawQuad::RENDER_PASS) {
392 const RenderPassDrawQuad* pass_quad =
393 RenderPassDrawQuad::MaterialCast(quad);
394 RenderPassId original_pass_id = pass_quad->render_pass_id;
395 RenderPassId remapped_pass_id =
396 RemapPassId(original_pass_id, surface_id);
398 gfx::Rect pass_damage;
399 for (const auto* pass : *dest_pass_list_) {
400 if (pass->id == remapped_pass_id) {
401 pass_damage = pass->damage_rect;
402 break;
406 dest_quad = dest_pass->CopyFromAndAppendRenderPassDrawQuad(
407 pass_quad, dest_pass->shared_quad_state_list.back(),
408 remapped_pass_id);
409 dest_pass->damage_rect =
410 gfx::UnionRects(dest_pass->damage_rect,
411 MathUtil::MapEnclosingClippedRect(
412 dest_quad->quadTransform(), pass_damage));
413 } else {
414 dest_quad = dest_pass->CopyFromAndAppendDrawQuad(
415 quad, dest_pass->shared_quad_state_list.back());
417 if (!remap.is_null())
418 dest_quad->IterateResources(remap);
423 void SurfaceAggregator::CopyPasses(const DelegatedFrameData* frame_data,
424 Surface* surface) {
425 // The root surface is allowed to have copy output requests, so grab them
426 // off its render passes.
427 std::multimap<RenderPassId, CopyOutputRequest*> copy_requests;
428 surface->TakeCopyOutputRequests(&copy_requests);
430 const RenderPassList& source_pass_list = frame_data->render_pass_list;
431 bool invalid_frame = ValidateResources(surface, frame_data);
432 DCHECK(!invalid_frame);
433 if (invalid_frame)
434 return;
436 DrawQuad::ResourceIteratorCallback remap;
437 if (provider_) {
438 int child_id = ChildIdForSurface(surface);
439 remap =
440 base::Bind(&ResourceRemapHelper,
441 base::ConstRef(provider_->GetChildToParentMap(child_id)));
444 for (size_t i = 0; i < source_pass_list.size(); ++i) {
445 const RenderPass& source = *source_pass_list[i];
447 size_t sqs_size = source.shared_quad_state_list.size();
448 size_t dq_size = source.quad_list.size();
449 scoped_ptr<RenderPass> copy_pass(RenderPass::Create(sqs_size, dq_size));
451 MoveMatchingRequests(source.id, &copy_requests, &copy_pass->copy_requests);
453 RenderPassId remapped_pass_id =
454 RemapPassId(source.id, surface->surface_id());
456 gfx::Rect damage_rect =
457 (i < source_pass_list.size() - 1)
458 ? gfx::Rect()
459 : DamageRectForSurface(surface, source, source.output_rect);
460 copy_pass->SetAll(remapped_pass_id, source.output_rect, damage_rect,
461 source.transform_to_root_target,
462 source.has_transparent_background);
464 CopyQuadsToPass(source.quad_list, source.shared_quad_state_list, remap,
465 gfx::Transform(), ClipData(), copy_pass.get(),
466 surface->surface_id());
468 dest_pass_list_->push_back(copy_pass.Pass());
472 void SurfaceAggregator::RemoveUnreferencedChildren() {
473 for (const auto& surface : previous_contained_surfaces_) {
474 if (!contained_surfaces_.count(surface.first)) {
475 SurfaceToResourceChildIdMap::iterator it =
476 surface_id_to_resource_child_id_.find(surface.first);
477 if (it != surface_id_to_resource_child_id_.end()) {
478 provider_->DestroyChild(it->second);
479 surface_id_to_resource_child_id_.erase(it);
482 Surface* surface_ptr = manager_->GetSurfaceForId(surface.first);
483 if (surface_ptr)
484 surface_ptr->RunDrawCallbacks(SurfaceDrawStatus::DRAW_SKIPPED);
489 scoped_ptr<CompositorFrame> SurfaceAggregator::Aggregate(SurfaceId surface_id) {
490 Surface* surface = manager_->GetSurfaceForId(surface_id);
491 DCHECK(surface);
492 contained_surfaces_[surface_id] = surface->frame_index();
493 const CompositorFrame* root_surface_frame = surface->GetEligibleFrame();
494 if (!root_surface_frame)
495 return nullptr;
496 TRACE_EVENT0("cc", "SurfaceAggregator::Aggregate");
498 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
499 frame->delegated_frame_data = make_scoped_ptr(new DelegatedFrameData);
501 DCHECK(root_surface_frame->delegated_frame_data);
503 SurfaceSet::iterator it = referenced_surfaces_.insert(surface_id).first;
505 dest_resource_list_ = &frame->delegated_frame_data->resource_list;
506 dest_pass_list_ = &frame->delegated_frame_data->render_pass_list;
508 CopyPasses(root_surface_frame->delegated_frame_data.get(), surface);
510 referenced_surfaces_.erase(it);
511 DCHECK(referenced_surfaces_.empty());
513 if (dest_pass_list_->empty())
514 return nullptr;
516 dest_pass_list_ = NULL;
517 RemoveUnreferencedChildren();
518 contained_surfaces_.swap(previous_contained_surfaces_);
519 contained_surfaces_.clear();
521 for (SurfaceIndexMap::iterator it = previous_contained_surfaces_.begin();
522 it != previous_contained_surfaces_.end();
523 ++it) {
524 Surface* surface = manager_->GetSurfaceForId(it->first);
525 if (surface)
526 surface->TakeLatencyInfo(&frame->metadata.latency_info);
529 // TODO(jamesr): Aggregate all resource references into the returned frame's
530 // resource list.
532 return frame.Pass();
535 void SurfaceAggregator::ReleaseResources(SurfaceId surface_id) {
536 SurfaceToResourceChildIdMap::iterator it =
537 surface_id_to_resource_child_id_.find(surface_id);
538 if (it != surface_id_to_resource_child_id_.end()) {
539 provider_->DestroyChild(it->second);
540 surface_id_to_resource_child_id_.erase(it);
544 void SurfaceAggregator::SetFullDamageForSurface(SurfaceId surface_id) {
545 auto it = previous_contained_surfaces_.find(surface_id);
546 if (it == previous_contained_surfaces_.end())
547 return;
548 // Set the last drawn index as 0 to ensure full damage next time it's drawn.
549 it->second = 0;
552 } // namespace cc