Update V8 to version 4.4.64.
[chromium-blink-merge.git] / cc / surfaces / surface_aggregator.cc
blob941e2d89fab2be9a6c86a33c55bf6fd8cc170aa2
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& 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 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);
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;
173 size_t reserve_size = frame_data->resource_list.size();
174 #if defined(COMPILER_MSVC)
175 referenced_resources.reserve(reserve_size);
176 #elif defined(COMPILER_GCC)
177 // Pre-standard hash-tables only implement resize, which behaves similarly
178 // to reserve for these keys. Resizing to 0 may also be broken (particularly
179 // on stlport).
180 // TODO(jbauman): Replace with reserve when C++11 is supported everywhere.
181 if (reserve_size)
182 referenced_resources.resize(reserve_size);
183 #endif
185 bool invalid_frame = false;
186 DrawQuad::ResourceIteratorCallback remap =
187 base::Bind(&ValidateResourceHelper, &invalid_frame,
188 base::ConstRef(provider_->GetChildToParentMap(child_id)),
189 &referenced_resources);
190 for (const auto& render_pass : frame_data->render_pass_list) {
191 for (const auto& quad : render_pass->quad_list)
192 quad->IterateResources(remap);
195 if (!invalid_frame)
196 provider_->DeclareUsedResourcesFromChild(child_id, referenced_resources);
198 return invalid_frame;
201 gfx::Rect SurfaceAggregator::DamageRectForSurface(const Surface* surface,
202 const RenderPass& source,
203 const gfx::Rect& full_rect) {
204 int previous_index = previous_contained_surfaces_[surface->surface_id()];
205 if (previous_index == surface->frame_index())
206 return gfx::Rect();
207 else if (previous_index == surface->frame_index() - 1)
208 return source.damage_rect;
209 return full_rect;
212 void SurfaceAggregator::HandleSurfaceQuad(
213 const SurfaceDrawQuad* surface_quad,
214 const gfx::Transform& target_transform,
215 const ClipData& clip_rect,
216 RenderPass* dest_pass) {
217 SurfaceId surface_id = surface_quad->surface_id;
218 // If this surface's id is already in our referenced set then it creates
219 // a cycle in the graph and should be dropped.
220 if (referenced_surfaces_.count(surface_id))
221 return;
222 Surface* surface = manager_->GetSurfaceForId(surface_id);
223 if (!surface) {
224 contained_surfaces_[surface_id] = 0;
225 return;
227 contained_surfaces_[surface_id] = surface->frame_index();
228 const CompositorFrame* frame = surface->GetEligibleFrame();
229 if (!frame)
230 return;
231 const DelegatedFrameData* frame_data = frame->delegated_frame_data.get();
232 if (!frame_data)
233 return;
235 std::multimap<RenderPassId, CopyOutputRequest*> copy_requests;
236 surface->TakeCopyOutputRequests(&copy_requests);
238 const RenderPassList& render_pass_list = frame_data->render_pass_list;
239 bool invalid_frame = ValidateResources(surface, frame_data);
240 if (invalid_frame) {
241 for (auto& request : copy_requests) {
242 request.second->SendEmptyResult();
243 delete request.second;
245 return;
248 SurfaceSet::iterator it = referenced_surfaces_.insert(surface_id).first;
249 DrawQuad::ResourceIteratorCallback remap;
250 if (provider_) {
251 int child_id = ChildIdForSurface(surface);
252 remap =
253 base::Bind(&ResourceRemapHelper,
254 base::ConstRef(provider_->GetChildToParentMap(child_id)));
257 bool merge_pass = surface_quad->opacity() == 1.f && copy_requests.empty();
259 gfx::Rect surface_damage = DamageRectForSurface(
260 surface, *render_pass_list.back(), surface_quad->visible_rect);
261 const RenderPassList& referenced_passes = render_pass_list;
262 size_t passes_to_copy =
263 merge_pass ? referenced_passes.size() - 1 : referenced_passes.size();
264 for (size_t j = 0; j < passes_to_copy; ++j) {
265 const RenderPass& source = *referenced_passes[j];
267 size_t sqs_size = source.shared_quad_state_list.size();
268 size_t dq_size = source.quad_list.size();
269 scoped_ptr<RenderPass> copy_pass(RenderPass::Create(sqs_size, dq_size));
271 RenderPassId remapped_pass_id = RemapPassId(source.id, surface_id);
273 copy_pass->SetAll(remapped_pass_id, source.output_rect, gfx::Rect(),
274 source.transform_to_root_target,
275 source.has_transparent_background);
277 MoveMatchingRequests(source.id, &copy_requests, &copy_pass->copy_requests);
279 // Contributing passes aggregated in to the pass list need to take the
280 // transform of the surface quad into account to update their transform to
281 // the root surface.
282 copy_pass->transform_to_root_target.ConcatTransform(
283 surface_quad->quadTransform());
284 copy_pass->transform_to_root_target.ConcatTransform(target_transform);
285 copy_pass->transform_to_root_target.ConcatTransform(
286 dest_pass->transform_to_root_target);
288 CopyQuadsToPass(source.quad_list, source.shared_quad_state_list, remap,
289 gfx::Transform(), ClipData(), copy_pass.get(), surface_id);
291 if (j == referenced_passes.size() - 1)
292 surface_damage = gfx::UnionRects(surface_damage, copy_pass->damage_rect);
294 dest_pass_list_->push_back(copy_pass.Pass());
297 const RenderPass& last_pass = *render_pass_list.back();
298 if (merge_pass) {
299 // TODO(jamesr): Clean up last pass special casing.
300 const QuadList& quads = last_pass.quad_list;
302 gfx::Transform surface_transform = surface_quad->quadTransform();
303 surface_transform.ConcatTransform(target_transform);
305 // Intersect the transformed visible rect and the clip rect to create a
306 // smaller cliprect for the quad.
307 ClipData surface_quad_clip_rect(
308 true, MathUtil::MapEnclosingClippedRect(surface_quad->quadTransform(),
309 surface_quad->visible_rect));
310 if (surface_quad->isClipped())
311 surface_quad_clip_rect.rect.Intersect(surface_quad->clipRect());
313 ClipData quads_clip =
314 CalculateClipRect(clip_rect, surface_quad_clip_rect, target_transform);
316 CopyQuadsToPass(quads, last_pass.shared_quad_state_list, remap,
317 surface_transform, quads_clip, dest_pass, surface_id);
318 } else {
319 RenderPassId remapped_pass_id = RemapPassId(last_pass.id, surface_id);
321 CopySharedQuadState(surface_quad->shared_quad_state, target_transform,
322 clip_rect, dest_pass);
324 SharedQuadState* shared_quad_state =
325 dest_pass->shared_quad_state_list.back();
326 RenderPassDrawQuad* quad =
327 dest_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
328 quad->SetNew(shared_quad_state,
329 surface_quad->rect,
330 surface_quad->visible_rect,
331 remapped_pass_id,
333 gfx::Vector2dF(),
334 gfx::Size(),
335 FilterOperations(),
336 gfx::Vector2dF(),
337 FilterOperations());
339 dest_pass->damage_rect =
340 gfx::UnionRects(dest_pass->damage_rect,
341 MathUtil::MapEnclosingClippedRect(
342 surface_quad->quadTransform(), surface_damage));
344 referenced_surfaces_.erase(it);
347 void SurfaceAggregator::CopySharedQuadState(
348 const SharedQuadState* source_sqs,
349 const gfx::Transform& target_transform,
350 const ClipData& clip_rect,
351 RenderPass* dest_render_pass) {
352 SharedQuadState* copy_shared_quad_state =
353 dest_render_pass->CreateAndAppendSharedQuadState();
354 copy_shared_quad_state->CopyFrom(source_sqs);
355 // target_transform contains any transformation that may exist
356 // between the context that these quads are being copied from (i.e. the
357 // surface's draw transform when aggregated from within a surface) to the
358 // target space of the pass. This will be identity except when copying the
359 // root draw pass from a surface into a pass when the surface draw quad's
360 // transform is not identity.
361 copy_shared_quad_state->content_to_target_transform.ConcatTransform(
362 target_transform);
364 ClipData new_clip_rect = CalculateClipRect(
365 clip_rect, ClipData(source_sqs->is_clipped, source_sqs->clip_rect),
366 target_transform);
367 copy_shared_quad_state->is_clipped = new_clip_rect.is_clipped;
368 copy_shared_quad_state->clip_rect = new_clip_rect.rect;
371 void SurfaceAggregator::CopyQuadsToPass(
372 const QuadList& source_quad_list,
373 const SharedQuadStateList& source_shared_quad_state_list,
374 const DrawQuad::ResourceIteratorCallback& remap,
375 const gfx::Transform& target_transform,
376 const ClipData& clip_rect,
377 RenderPass* dest_pass,
378 SurfaceId surface_id) {
379 const SharedQuadState* last_copied_source_shared_quad_state = NULL;
381 SharedQuadStateList::ConstIterator sqs_iter =
382 source_shared_quad_state_list.begin();
383 for (const auto& quad : source_quad_list) {
384 while (quad->shared_quad_state != *sqs_iter) {
385 ++sqs_iter;
386 DCHECK(sqs_iter != source_shared_quad_state_list.end());
388 DCHECK_EQ(quad->shared_quad_state, *sqs_iter);
390 if (quad->material == DrawQuad::SURFACE_CONTENT) {
391 const SurfaceDrawQuad* surface_quad = SurfaceDrawQuad::MaterialCast(quad);
392 HandleSurfaceQuad(surface_quad, target_transform, clip_rect, dest_pass);
393 } else {
394 if (quad->shared_quad_state != last_copied_source_shared_quad_state) {
395 CopySharedQuadState(quad->shared_quad_state, target_transform,
396 clip_rect, dest_pass);
397 last_copied_source_shared_quad_state = quad->shared_quad_state;
399 DrawQuad* dest_quad;
400 if (quad->material == DrawQuad::RENDER_PASS) {
401 const RenderPassDrawQuad* pass_quad =
402 RenderPassDrawQuad::MaterialCast(quad);
403 RenderPassId original_pass_id = pass_quad->render_pass_id;
404 RenderPassId remapped_pass_id =
405 RemapPassId(original_pass_id, surface_id);
407 gfx::Rect pass_damage;
408 for (const auto* pass : *dest_pass_list_) {
409 if (pass->id == remapped_pass_id) {
410 pass_damage = pass->damage_rect;
411 break;
415 dest_quad = dest_pass->CopyFromAndAppendRenderPassDrawQuad(
416 pass_quad, dest_pass->shared_quad_state_list.back(),
417 remapped_pass_id);
418 dest_pass->damage_rect =
419 gfx::UnionRects(dest_pass->damage_rect,
420 MathUtil::MapEnclosingClippedRect(
421 dest_quad->quadTransform(), pass_damage));
422 } else {
423 dest_quad = dest_pass->CopyFromAndAppendDrawQuad(
424 quad, dest_pass->shared_quad_state_list.back());
426 if (!remap.is_null())
427 dest_quad->IterateResources(remap);
432 void SurfaceAggregator::CopyPasses(const DelegatedFrameData* frame_data,
433 Surface* surface) {
434 // The root surface is allowed to have copy output requests, so grab them
435 // off its render passes.
436 std::multimap<RenderPassId, CopyOutputRequest*> copy_requests;
437 surface->TakeCopyOutputRequests(&copy_requests);
439 const RenderPassList& source_pass_list = frame_data->render_pass_list;
440 bool invalid_frame = ValidateResources(surface, frame_data);
441 DCHECK(!invalid_frame);
442 if (invalid_frame)
443 return;
445 DrawQuad::ResourceIteratorCallback remap;
446 if (provider_) {
447 int child_id = ChildIdForSurface(surface);
448 remap =
449 base::Bind(&ResourceRemapHelper,
450 base::ConstRef(provider_->GetChildToParentMap(child_id)));
453 for (size_t i = 0; i < source_pass_list.size(); ++i) {
454 const RenderPass& source = *source_pass_list[i];
456 size_t sqs_size = source.shared_quad_state_list.size();
457 size_t dq_size = source.quad_list.size();
458 scoped_ptr<RenderPass> copy_pass(RenderPass::Create(sqs_size, dq_size));
460 MoveMatchingRequests(source.id, &copy_requests, &copy_pass->copy_requests);
462 RenderPassId remapped_pass_id =
463 RemapPassId(source.id, surface->surface_id());
465 gfx::Rect damage_rect =
466 (i < source_pass_list.size() - 1)
467 ? gfx::Rect()
468 : DamageRectForSurface(surface, source, source.output_rect);
469 copy_pass->SetAll(remapped_pass_id, source.output_rect, damage_rect,
470 source.transform_to_root_target,
471 source.has_transparent_background);
473 CopyQuadsToPass(source.quad_list, source.shared_quad_state_list, remap,
474 gfx::Transform(), ClipData(), copy_pass.get(),
475 surface->surface_id());
477 dest_pass_list_->push_back(copy_pass.Pass());
481 void SurfaceAggregator::RemoveUnreferencedChildren() {
482 for (const auto& surface : previous_contained_surfaces_) {
483 if (!contained_surfaces_.count(surface.first)) {
484 SurfaceToResourceChildIdMap::iterator it =
485 surface_id_to_resource_child_id_.find(surface.first);
486 if (it != surface_id_to_resource_child_id_.end()) {
487 provider_->DestroyChild(it->second);
488 surface_id_to_resource_child_id_.erase(it);
491 Surface* surface_ptr = manager_->GetSurfaceForId(surface.first);
492 if (surface_ptr)
493 surface_ptr->RunDrawCallbacks(SurfaceDrawStatus::DRAW_SKIPPED);
498 scoped_ptr<CompositorFrame> SurfaceAggregator::Aggregate(SurfaceId surface_id) {
499 Surface* surface = manager_->GetSurfaceForId(surface_id);
500 DCHECK(surface);
501 contained_surfaces_[surface_id] = surface->frame_index();
502 const CompositorFrame* root_surface_frame = surface->GetEligibleFrame();
503 if (!root_surface_frame)
504 return nullptr;
505 TRACE_EVENT0("cc", "SurfaceAggregator::Aggregate");
507 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
508 frame->delegated_frame_data = make_scoped_ptr(new DelegatedFrameData);
510 DCHECK(root_surface_frame->delegated_frame_data);
512 SurfaceSet::iterator it = referenced_surfaces_.insert(surface_id).first;
514 dest_resource_list_ = &frame->delegated_frame_data->resource_list;
515 dest_pass_list_ = &frame->delegated_frame_data->render_pass_list;
517 CopyPasses(root_surface_frame->delegated_frame_data.get(), surface);
519 referenced_surfaces_.erase(it);
520 DCHECK(referenced_surfaces_.empty());
522 if (dest_pass_list_->empty())
523 return nullptr;
525 dest_pass_list_ = NULL;
526 RemoveUnreferencedChildren();
527 contained_surfaces_.swap(previous_contained_surfaces_);
528 contained_surfaces_.clear();
530 for (SurfaceIndexMap::iterator it = previous_contained_surfaces_.begin();
531 it != previous_contained_surfaces_.end();
532 ++it) {
533 Surface* surface = manager_->GetSurfaceForId(it->first);
534 if (surface)
535 surface->TakeLatencyInfo(&frame->metadata.latency_info);
538 // TODO(jamesr): Aggregate all resource references into the returned frame's
539 // resource list.
541 return frame.Pass();
544 void SurfaceAggregator::ReleaseResources(SurfaceId surface_id) {
545 SurfaceToResourceChildIdMap::iterator it =
546 surface_id_to_resource_child_id_.find(surface_id);
547 if (it != surface_id_to_resource_child_id_.end()) {
548 provider_->DestroyChild(it->second);
549 surface_id_to_resource_child_id_.erase(it);
553 void SurfaceAggregator::SetFullDamageForSurface(SurfaceId surface_id) {
554 auto it = previous_contained_surfaces_.find(surface_id);
555 if (it == previous_contained_surfaces_.end())
556 return;
557 // Set the last drawn index as 0 to ensure full damage next time it's drawn.
558 it->second = 0;
561 } // namespace cc