Automated Commit: Committing new LKGM version 6953.0.0 for chromeos.
[chromium-blink-merge.git] / cc / surfaces / surface_aggregator.cc
blob21ec6dbdb3802051aabf82246c9f84aac0098f43
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 surface_id_to_resource_child_id_[surface->surface_id()] = child_id;
124 return child_id;
125 } else {
126 return it->second;
130 static ResourceProvider::ResourceId ResourceRemapHelper(
131 bool* invalid_frame,
132 const ResourceProvider::ResourceIdMap& child_to_parent_map,
133 ResourceProvider::ResourceIdArray* resources_in_frame,
134 ResourceProvider::ResourceId id) {
135 ResourceProvider::ResourceIdMap::const_iterator it =
136 child_to_parent_map.find(id);
137 if (it == child_to_parent_map.end()) {
138 *invalid_frame = true;
139 return 0;
142 DCHECK_EQ(it->first, id);
143 ResourceProvider::ResourceId remapped_id = it->second;
144 resources_in_frame->push_back(id);
145 return remapped_id;
148 bool SurfaceAggregator::TakeResources(Surface* surface,
149 const DelegatedFrameData* frame_data,
150 RenderPassList* render_pass_list) {
151 RenderPass::CopyAll(frame_data->render_pass_list, render_pass_list);
152 if (!provider_) // TODO(jamesr): hack for unit tests that don't set up rp
153 return false;
155 int child_id = ChildIdForSurface(surface);
156 if (surface->factory())
157 surface->factory()->RefResources(frame_data->resource_list);
158 provider_->ReceiveFromChild(child_id, frame_data->resource_list);
160 typedef ResourceProvider::ResourceIdArray IdArray;
161 IdArray referenced_resources;
163 bool invalid_frame = false;
164 DrawQuad::ResourceIteratorCallback remap =
165 base::Bind(&ResourceRemapHelper,
166 &invalid_frame,
167 provider_->GetChildToParentMap(child_id),
168 &referenced_resources);
169 for (const auto& render_pass : *render_pass_list) {
170 for (const auto& quad : render_pass->quad_list)
171 quad->IterateResources(remap);
174 if (!invalid_frame)
175 provider_->DeclareUsedResourcesFromChild(child_id, referenced_resources);
177 return invalid_frame;
180 gfx::Rect SurfaceAggregator::DamageRectForSurface(const Surface* surface,
181 const RenderPass& source,
182 const gfx::Rect& full_rect) {
183 int previous_index = previous_contained_surfaces_[surface->surface_id()];
184 if (previous_index == surface->frame_index())
185 return gfx::Rect();
186 else if (previous_index == surface->frame_index() - 1)
187 return source.damage_rect;
188 return full_rect;
191 void SurfaceAggregator::HandleSurfaceQuad(
192 const SurfaceDrawQuad* surface_quad,
193 const gfx::Transform& content_to_target_transform,
194 const ClipData& clip_rect,
195 RenderPass* dest_pass) {
196 SurfaceId surface_id = surface_quad->surface_id;
197 // If this surface's id is already in our referenced set then it creates
198 // a cycle in the graph and should be dropped.
199 if (referenced_surfaces_.count(surface_id))
200 return;
201 Surface* surface = manager_->GetSurfaceForId(surface_id);
202 if (!surface) {
203 contained_surfaces_[surface_id] = 0;
204 return;
206 contained_surfaces_[surface_id] = surface->frame_index();
207 const CompositorFrame* frame = surface->GetEligibleFrame();
208 if (!frame)
209 return;
210 const DelegatedFrameData* frame_data = frame->delegated_frame_data.get();
211 if (!frame_data)
212 return;
214 std::multimap<RenderPassId, CopyOutputRequest*> copy_requests;
215 surface->TakeCopyOutputRequests(&copy_requests);
217 RenderPassList render_pass_list;
218 bool invalid_frame = TakeResources(surface, frame_data, &render_pass_list);
219 if (invalid_frame) {
220 for (auto& request : copy_requests) {
221 request.second->SendEmptyResult();
222 delete request.second;
224 return;
227 SurfaceSet::iterator it = referenced_surfaces_.insert(surface_id).first;
229 bool merge_pass = surface_quad->opacity() == 1.f && copy_requests.empty();
231 gfx::Rect surface_damage = DamageRectForSurface(
232 surface, *render_pass_list.back(), surface_quad->visible_rect);
233 const RenderPassList& referenced_passes = render_pass_list;
234 size_t passes_to_copy =
235 merge_pass ? referenced_passes.size() - 1 : referenced_passes.size();
236 for (size_t j = 0; j < passes_to_copy; ++j) {
237 const RenderPass& source = *referenced_passes[j];
239 size_t sqs_size = source.shared_quad_state_list.size();
240 size_t dq_size = source.quad_list.size();
241 scoped_ptr<RenderPass> copy_pass(RenderPass::Create(sqs_size, dq_size));
243 RenderPassId remapped_pass_id = RemapPassId(source.id, surface_id);
245 copy_pass->SetAll(remapped_pass_id, source.output_rect, gfx::Rect(),
246 source.transform_to_root_target,
247 source.has_transparent_background);
249 MoveMatchingRequests(source.id, &copy_requests, &copy_pass->copy_requests);
251 // Contributing passes aggregated in to the pass list need to take the
252 // transform of the surface quad into account to update their transform to
253 // the root surface.
254 copy_pass->transform_to_root_target.ConcatTransform(
255 surface_quad->quadTransform());
256 copy_pass->transform_to_root_target.ConcatTransform(
257 content_to_target_transform);
258 copy_pass->transform_to_root_target.ConcatTransform(
259 dest_pass->transform_to_root_target);
261 CopyQuadsToPass(source.quad_list, source.shared_quad_state_list,
262 gfx::Transform(), ClipData(), copy_pass.get(), surface_id);
264 if (j == referenced_passes.size() - 1)
265 surface_damage = gfx::UnionRects(surface_damage, copy_pass->damage_rect);
267 dest_pass_list_->push_back(copy_pass.Pass());
270 const RenderPass& last_pass = *render_pass_list.back();
271 if (merge_pass) {
272 // TODO(jamesr): Clean up last pass special casing.
273 const QuadList& quads = last_pass.quad_list;
275 gfx::Transform surface_transform = surface_quad->quadTransform();
276 surface_transform.ConcatTransform(content_to_target_transform);
278 // Intersect the transformed visible rect and the clip rect to create a
279 // smaller cliprect for the quad.
280 ClipData surface_quad_clip_rect(
281 true, MathUtil::MapEnclosingClippedRect(surface_quad->quadTransform(),
282 surface_quad->visible_rect));
283 if (surface_quad->isClipped())
284 surface_quad_clip_rect.rect.Intersect(surface_quad->clipRect());
286 ClipData quads_clip = CalculateClipRect(clip_rect, surface_quad_clip_rect,
287 content_to_target_transform);
289 CopyQuadsToPass(quads, last_pass.shared_quad_state_list, surface_transform,
290 quads_clip, dest_pass, surface_id);
291 } else {
292 RenderPassId remapped_pass_id = RemapPassId(last_pass.id, surface_id);
294 CopySharedQuadState(surface_quad->shared_quad_state,
295 content_to_target_transform, clip_rect, dest_pass);
297 SharedQuadState* shared_quad_state =
298 dest_pass->shared_quad_state_list.back();
299 RenderPassDrawQuad* quad =
300 dest_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
301 quad->SetNew(shared_quad_state,
302 surface_quad->rect,
303 surface_quad->visible_rect,
304 remapped_pass_id,
306 gfx::Vector2dF(),
307 gfx::Size(),
308 FilterOperations(),
309 gfx::Vector2dF(),
310 FilterOperations());
312 dest_pass->damage_rect =
313 gfx::UnionRects(dest_pass->damage_rect,
314 MathUtil::MapEnclosingClippedRect(
315 surface_quad->quadTransform(), surface_damage));
317 referenced_surfaces_.erase(it);
320 void SurfaceAggregator::CopySharedQuadState(
321 const SharedQuadState* source_sqs,
322 const gfx::Transform& content_to_target_transform,
323 const ClipData& clip_rect,
324 RenderPass* dest_render_pass) {
325 SharedQuadState* copy_shared_quad_state =
326 dest_render_pass->CreateAndAppendSharedQuadState();
327 copy_shared_quad_state->CopyFrom(source_sqs);
328 // content_to_target_transform contains any transformation that may exist
329 // between the context that these quads are being copied from (i.e. the
330 // surface's draw transform when aggregated from within a surface) to the
331 // target space of the pass. This will be identity except when copying the
332 // root draw pass from a surface into a pass when the surface draw quad's
333 // transform is not identity.
334 copy_shared_quad_state->content_to_target_transform.ConcatTransform(
335 content_to_target_transform);
337 ClipData new_clip_rect = CalculateClipRect(
338 clip_rect, ClipData(source_sqs->is_clipped, source_sqs->clip_rect),
339 content_to_target_transform);
340 copy_shared_quad_state->is_clipped = new_clip_rect.is_clipped;
341 copy_shared_quad_state->clip_rect = new_clip_rect.rect;
344 void SurfaceAggregator::CopyQuadsToPass(
345 const QuadList& source_quad_list,
346 const SharedQuadStateList& source_shared_quad_state_list,
347 const gfx::Transform& content_to_target_transform,
348 const ClipData& clip_rect,
349 RenderPass* dest_pass,
350 SurfaceId surface_id) {
351 const SharedQuadState* last_copied_source_shared_quad_state = NULL;
353 SharedQuadStateList::ConstIterator sqs_iter =
354 source_shared_quad_state_list.begin();
355 for (const auto& quad : source_quad_list) {
356 while (quad->shared_quad_state != *sqs_iter) {
357 ++sqs_iter;
358 DCHECK(sqs_iter != source_shared_quad_state_list.end());
360 DCHECK_EQ(quad->shared_quad_state, *sqs_iter);
362 if (quad->material == DrawQuad::SURFACE_CONTENT) {
363 const SurfaceDrawQuad* surface_quad = SurfaceDrawQuad::MaterialCast(quad);
364 HandleSurfaceQuad(surface_quad, content_to_target_transform, clip_rect,
365 dest_pass);
366 } else {
367 if (quad->shared_quad_state != last_copied_source_shared_quad_state) {
368 CopySharedQuadState(quad->shared_quad_state,
369 content_to_target_transform, clip_rect, dest_pass);
370 last_copied_source_shared_quad_state = quad->shared_quad_state;
372 if (quad->material == DrawQuad::RENDER_PASS) {
373 const RenderPassDrawQuad* pass_quad =
374 RenderPassDrawQuad::MaterialCast(quad);
375 RenderPassId original_pass_id = pass_quad->render_pass_id;
376 RenderPassId remapped_pass_id =
377 RemapPassId(original_pass_id, surface_id);
379 gfx::Rect pass_damage;
380 for (const auto* pass : *dest_pass_list_) {
381 if (pass->id == remapped_pass_id) {
382 pass_damage = pass->damage_rect;
383 break;
387 DrawQuad* rpdq = dest_pass->CopyFromAndAppendRenderPassDrawQuad(
388 pass_quad, dest_pass->shared_quad_state_list.back(),
389 remapped_pass_id);
390 dest_pass->damage_rect = gfx::UnionRects(
391 dest_pass->damage_rect, MathUtil::MapEnclosingClippedRect(
392 rpdq->quadTransform(), pass_damage));
393 } else {
394 dest_pass->CopyFromAndAppendDrawQuad(
395 quad, dest_pass->shared_quad_state_list.back());
401 void SurfaceAggregator::CopyPasses(const DelegatedFrameData* frame_data,
402 Surface* surface) {
403 RenderPassList source_pass_list;
405 // The root surface is allowed to have copy output requests, so grab them
406 // off its render passes.
407 std::multimap<RenderPassId, CopyOutputRequest*> copy_requests;
408 surface->TakeCopyOutputRequests(&copy_requests);
410 bool invalid_frame = TakeResources(surface, frame_data, &source_pass_list);
411 DCHECK(!invalid_frame);
413 for (size_t i = 0; i < source_pass_list.size(); ++i) {
414 const RenderPass& source = *source_pass_list[i];
416 size_t sqs_size = source.shared_quad_state_list.size();
417 size_t dq_size = source.quad_list.size();
418 scoped_ptr<RenderPass> copy_pass(RenderPass::Create(sqs_size, dq_size));
420 MoveMatchingRequests(source.id, &copy_requests, &copy_pass->copy_requests);
422 RenderPassId remapped_pass_id =
423 RemapPassId(source.id, surface->surface_id());
425 gfx::Rect damage_rect =
426 (i < source_pass_list.size() - 1)
427 ? gfx::Rect()
428 : DamageRectForSurface(surface, source, source.output_rect);
429 copy_pass->SetAll(remapped_pass_id, source.output_rect, damage_rect,
430 source.transform_to_root_target,
431 source.has_transparent_background);
433 CopyQuadsToPass(source.quad_list, source.shared_quad_state_list,
434 gfx::Transform(), ClipData(), copy_pass.get(),
435 surface->surface_id());
437 dest_pass_list_->push_back(copy_pass.Pass());
441 void SurfaceAggregator::RemoveUnreferencedChildren() {
442 for (const auto& surface : previous_contained_surfaces_) {
443 if (!contained_surfaces_.count(surface.first)) {
444 SurfaceToResourceChildIdMap::iterator it =
445 surface_id_to_resource_child_id_.find(surface.first);
446 if (it != surface_id_to_resource_child_id_.end()) {
447 provider_->DestroyChild(it->second);
448 surface_id_to_resource_child_id_.erase(it);
451 Surface* surface_ptr = manager_->GetSurfaceForId(surface.first);
452 if (surface_ptr)
453 surface_ptr->RunDrawCallbacks(SurfaceDrawStatus::DRAW_SKIPPED);
458 scoped_ptr<CompositorFrame> SurfaceAggregator::Aggregate(SurfaceId surface_id) {
459 Surface* surface = manager_->GetSurfaceForId(surface_id);
460 DCHECK(surface);
461 contained_surfaces_[surface_id] = surface->frame_index();
462 const CompositorFrame* root_surface_frame = surface->GetEligibleFrame();
463 if (!root_surface_frame)
464 return nullptr;
465 TRACE_EVENT0("cc", "SurfaceAggregator::Aggregate");
467 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
468 frame->delegated_frame_data = make_scoped_ptr(new DelegatedFrameData);
470 DCHECK(root_surface_frame->delegated_frame_data);
472 SurfaceSet::iterator it = referenced_surfaces_.insert(surface_id).first;
474 dest_resource_list_ = &frame->delegated_frame_data->resource_list;
475 dest_pass_list_ = &frame->delegated_frame_data->render_pass_list;
477 CopyPasses(root_surface_frame->delegated_frame_data.get(), surface);
479 referenced_surfaces_.erase(it);
480 DCHECK(referenced_surfaces_.empty());
482 if (dest_pass_list_->empty())
483 return nullptr;
485 dest_pass_list_ = NULL;
486 RemoveUnreferencedChildren();
487 contained_surfaces_.swap(previous_contained_surfaces_);
488 contained_surfaces_.clear();
490 for (SurfaceIndexMap::iterator it = previous_contained_surfaces_.begin();
491 it != previous_contained_surfaces_.end();
492 ++it) {
493 Surface* surface = manager_->GetSurfaceForId(it->first);
494 if (surface)
495 surface->TakeLatencyInfo(&frame->metadata.latency_info);
498 // TODO(jamesr): Aggregate all resource references into the returned frame's
499 // resource list.
501 return frame.Pass();
504 void SurfaceAggregator::ReleaseResources(SurfaceId surface_id) {
505 SurfaceToResourceChildIdMap::iterator it =
506 surface_id_to_resource_child_id_.find(surface_id);
507 if (it != surface_id_to_resource_child_id_.end()) {
508 provider_->DestroyChild(it->second);
509 surface_id_to_resource_child_id_.erase(it);
513 } // namespace cc