Fix WeakPtrFactory members in Android media classes
[chromium-blink-merge.git] / cc / surfaces / surface_aggregator.cc
blob9456dd29c4be152ec529a03888e9ec1ef562af9d
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/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"
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 const RenderPassList& referenced_passes = render_pass_list;
232 size_t passes_to_copy =
233 merge_pass ? referenced_passes.size() - 1 : referenced_passes.size();
234 for (size_t j = 0; j < passes_to_copy; ++j) {
235 const RenderPass& source = *referenced_passes[j];
237 size_t sqs_size = source.shared_quad_state_list.size();
238 size_t dq_size = source.quad_list.size();
239 scoped_ptr<RenderPass> copy_pass(RenderPass::Create(sqs_size, dq_size));
241 RenderPassId remapped_pass_id = RemapPassId(source.id, surface_id);
243 copy_pass->SetAll(remapped_pass_id,
244 source.output_rect,
245 source.damage_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 // TODO(jamesr): Make sure this is sufficient for surfaces nested several
255 // levels deep and add tests.
256 copy_pass->transform_to_root_target.ConcatTransform(
257 surface_quad->quadTransform());
258 copy_pass->transform_to_root_target.ConcatTransform(
259 content_to_target_transform);
261 CopyQuadsToPass(source.quad_list, source.shared_quad_state_list,
262 gfx::Transform(), ClipData(), copy_pass.get(), surface_id);
264 dest_pass_list_->push_back(copy_pass.Pass());
267 const RenderPass& last_pass = *render_pass_list.back();
268 if (merge_pass) {
269 // TODO(jamesr): Clean up last pass special casing.
270 const QuadList& quads = last_pass.quad_list;
272 gfx::Transform surface_transform = surface_quad->quadTransform();
273 surface_transform.ConcatTransform(content_to_target_transform);
275 // Intersect the transformed visible rect and the clip rect to create a
276 // smaller cliprect for the quad.
277 ClipData surface_quad_clip_rect(
278 true, MathUtil::MapEnclosingClippedRect(surface_quad->quadTransform(),
279 surface_quad->visible_rect));
280 if (surface_quad->isClipped())
281 surface_quad_clip_rect.rect.Intersect(surface_quad->clipRect());
283 ClipData quads_clip = CalculateClipRect(clip_rect, surface_quad_clip_rect,
284 content_to_target_transform);
286 CopyQuadsToPass(quads, last_pass.shared_quad_state_list, surface_transform,
287 quads_clip, dest_pass, surface_id);
288 } else {
289 RenderPassId remapped_pass_id = RemapPassId(last_pass.id, surface_id);
291 CopySharedQuadState(surface_quad->shared_quad_state,
292 content_to_target_transform, clip_rect, dest_pass);
294 SharedQuadState* shared_quad_state =
295 dest_pass->shared_quad_state_list.back();
296 RenderPassDrawQuad* quad =
297 dest_pass->CreateAndAppendDrawQuad<RenderPassDrawQuad>();
298 quad->SetNew(shared_quad_state,
299 surface_quad->rect,
300 surface_quad->visible_rect,
301 remapped_pass_id,
303 gfx::Vector2dF(),
304 gfx::Size(),
305 FilterOperations(),
306 gfx::Vector2dF(),
307 FilterOperations());
309 dest_pass->damage_rect =
310 gfx::UnionRects(dest_pass->damage_rect,
311 MathUtil::MapEnclosingClippedRect(
312 surface_quad->quadTransform(),
313 DamageRectForSurface(surface, last_pass,
314 surface_quad->visible_rect)));
316 referenced_surfaces_.erase(it);
319 void SurfaceAggregator::CopySharedQuadState(
320 const SharedQuadState* source_sqs,
321 const gfx::Transform& content_to_target_transform,
322 const ClipData& clip_rect,
323 RenderPass* dest_render_pass) {
324 SharedQuadState* copy_shared_quad_state =
325 dest_render_pass->CreateAndAppendSharedQuadState();
326 copy_shared_quad_state->CopyFrom(source_sqs);
327 // content_to_target_transform contains any transformation that may exist
328 // between the context that these quads are being copied from (i.e. the
329 // surface's draw transform when aggregated from within a surface) to the
330 // target space of the pass. This will be identity except when copying the
331 // root draw pass from a surface into a pass when the surface draw quad's
332 // transform is not identity.
333 copy_shared_quad_state->content_to_target_transform.ConcatTransform(
334 content_to_target_transform);
336 ClipData new_clip_rect = CalculateClipRect(
337 clip_rect, ClipData(source_sqs->is_clipped, source_sqs->clip_rect),
338 content_to_target_transform);
339 copy_shared_quad_state->is_clipped = new_clip_rect.is_clipped;
340 copy_shared_quad_state->clip_rect = new_clip_rect.rect;
343 void SurfaceAggregator::CopyQuadsToPass(
344 const QuadList& source_quad_list,
345 const SharedQuadStateList& source_shared_quad_state_list,
346 const gfx::Transform& content_to_target_transform,
347 const ClipData& clip_rect,
348 RenderPass* dest_pass,
349 SurfaceId surface_id) {
350 const SharedQuadState* last_copied_source_shared_quad_state = NULL;
352 SharedQuadStateList::ConstIterator sqs_iter =
353 source_shared_quad_state_list.begin();
354 for (const auto& quad : source_quad_list) {
355 while (quad->shared_quad_state != *sqs_iter) {
356 ++sqs_iter;
357 DCHECK(sqs_iter != source_shared_quad_state_list.end());
359 DCHECK_EQ(quad->shared_quad_state, *sqs_iter);
361 if (quad->material == DrawQuad::SURFACE_CONTENT) {
362 const SurfaceDrawQuad* surface_quad = SurfaceDrawQuad::MaterialCast(quad);
363 HandleSurfaceQuad(surface_quad, content_to_target_transform, clip_rect,
364 dest_pass);
365 } else {
366 if (quad->shared_quad_state != last_copied_source_shared_quad_state) {
367 CopySharedQuadState(quad->shared_quad_state,
368 content_to_target_transform, clip_rect, dest_pass);
369 last_copied_source_shared_quad_state = quad->shared_quad_state;
371 if (quad->material == DrawQuad::RENDER_PASS) {
372 const RenderPassDrawQuad* pass_quad =
373 RenderPassDrawQuad::MaterialCast(quad);
374 RenderPassId original_pass_id = pass_quad->render_pass_id;
375 RenderPassId remapped_pass_id =
376 RemapPassId(original_pass_id, surface_id);
378 dest_pass->CopyFromAndAppendRenderPassDrawQuad(
379 pass_quad,
380 dest_pass->shared_quad_state_list.back(),
381 remapped_pass_id);
382 } else {
383 dest_pass->CopyFromAndAppendDrawQuad(
384 quad, dest_pass->shared_quad_state_list.back());
390 void SurfaceAggregator::CopyPasses(const DelegatedFrameData* frame_data,
391 Surface* surface) {
392 RenderPassList source_pass_list;
394 // The root surface is allowed to have copy output requests, so grab them
395 // off its render passes.
396 std::multimap<RenderPassId, CopyOutputRequest*> copy_requests;
397 surface->TakeCopyOutputRequests(&copy_requests);
399 bool invalid_frame = TakeResources(surface, frame_data, &source_pass_list);
400 DCHECK(!invalid_frame);
402 for (size_t i = 0; i < source_pass_list.size(); ++i) {
403 const RenderPass& source = *source_pass_list[i];
405 size_t sqs_size = source.shared_quad_state_list.size();
406 size_t dq_size = source.quad_list.size();
407 scoped_ptr<RenderPass> copy_pass(RenderPass::Create(sqs_size, dq_size));
409 MoveMatchingRequests(source.id, &copy_requests, &copy_pass->copy_requests);
411 RenderPassId remapped_pass_id =
412 RemapPassId(source.id, surface->surface_id());
414 copy_pass->SetAll(remapped_pass_id, source.output_rect,
415 DamageRectForSurface(surface, source, source.output_rect),
416 source.transform_to_root_target,
417 source.has_transparent_background);
419 CopyQuadsToPass(source.quad_list, source.shared_quad_state_list,
420 gfx::Transform(), ClipData(), copy_pass.get(),
421 surface->surface_id());
423 dest_pass_list_->push_back(copy_pass.Pass());
427 void SurfaceAggregator::RemoveUnreferencedChildren() {
428 for (const auto& surface : previous_contained_surfaces_) {
429 if (!contained_surfaces_.count(surface.first)) {
430 SurfaceToResourceChildIdMap::iterator it =
431 surface_id_to_resource_child_id_.find(surface.first);
432 if (it != surface_id_to_resource_child_id_.end()) {
433 provider_->DestroyChild(it->second);
434 surface_id_to_resource_child_id_.erase(it);
440 scoped_ptr<CompositorFrame> SurfaceAggregator::Aggregate(SurfaceId surface_id) {
441 Surface* surface = manager_->GetSurfaceForId(surface_id);
442 DCHECK(surface);
443 contained_surfaces_[surface_id] = surface->frame_index();
444 const CompositorFrame* root_surface_frame = surface->GetEligibleFrame();
445 if (!root_surface_frame)
446 return nullptr;
447 TRACE_EVENT0("cc", "SurfaceAggregator::Aggregate");
449 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
450 frame->delegated_frame_data = make_scoped_ptr(new DelegatedFrameData);
452 DCHECK(root_surface_frame->delegated_frame_data);
454 SurfaceSet::iterator it = referenced_surfaces_.insert(surface_id).first;
456 dest_resource_list_ = &frame->delegated_frame_data->resource_list;
457 dest_pass_list_ = &frame->delegated_frame_data->render_pass_list;
459 CopyPasses(root_surface_frame->delegated_frame_data.get(), surface);
461 referenced_surfaces_.erase(it);
462 DCHECK(referenced_surfaces_.empty());
464 if (dest_pass_list_->empty())
465 return nullptr;
467 dest_pass_list_ = NULL;
468 RemoveUnreferencedChildren();
469 contained_surfaces_.swap(previous_contained_surfaces_);
470 contained_surfaces_.clear();
472 for (SurfaceIndexMap::iterator it = previous_contained_surfaces_.begin();
473 it != previous_contained_surfaces_.end();
474 ++it) {
475 Surface* surface = manager_->GetSurfaceForId(it->first);
476 if (surface)
477 surface->TakeLatencyInfo(&frame->metadata.latency_info);
480 // TODO(jamesr): Aggregate all resource references into the returned frame's
481 // resource list.
483 return frame.Pass();
486 void SurfaceAggregator::ReleaseResources(SurfaceId surface_id) {
487 SurfaceToResourceChildIdMap::iterator it =
488 surface_id_to_resource_child_id_.find(surface_id);
489 if (it != surface_id_to_resource_child_id_.end()) {
490 provider_->DestroyChild(it->second);
491 surface_id_to_resource_child_id_.erase(it);
495 } // namespace cc