Fix dcheck in message port code on Mandoline shutdown.
[chromium-blink-merge.git] / cc / surfaces / surface.cc
blob16987cb84e49b356bd2cf4afa0fd255f9ddf82a6
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.h"
7 #include <algorithm>
9 #include "cc/output/compositor_frame.h"
10 #include "cc/output/copy_output_request.h"
11 #include "cc/surfaces/surface_factory.h"
12 #include "cc/surfaces/surface_id_allocator.h"
13 #include "cc/surfaces/surface_manager.h"
15 namespace cc {
17 // The frame index starts at 2 so that empty frames will be treated as
18 // completely damaged the first time they're drawn from.
19 static const int kFrameIndexStart = 2;
21 Surface::Surface(SurfaceId id, SurfaceFactory* factory)
22 : surface_id_(id),
23 factory_(factory->AsWeakPtr()),
24 frame_index_(kFrameIndexStart),
25 destroyed_(false) {}
27 Surface::~Surface() {
28 ClearCopyRequests();
29 if (current_frame_ && factory_) {
30 ReturnedResourceArray current_resources;
31 TransferableResource::ReturnResources(
32 current_frame_->delegated_frame_data->resource_list,
33 &current_resources);
34 factory_->UnrefResources(current_resources);
36 if (!draw_callback_.is_null())
37 draw_callback_.Run(SurfaceDrawStatus::DRAW_SKIPPED);
40 void Surface::QueueFrame(scoped_ptr<CompositorFrame> frame,
41 const DrawCallback& callback) {
42 DCHECK(factory_);
43 ClearCopyRequests();
45 if (frame) {
46 TakeLatencyInfo(&frame->metadata.latency_info);
49 scoped_ptr<CompositorFrame> previous_frame = current_frame_.Pass();
50 current_frame_ = frame.Pass();
52 if (current_frame_) {
53 factory_->ReceiveFromChild(
54 current_frame_->delegated_frame_data->resource_list);
57 // Empty frames shouldn't be drawn and shouldn't contribute damage, so don't
58 // increment frame index for them.
59 if (current_frame_ &&
60 !current_frame_->delegated_frame_data->render_pass_list.empty())
61 ++frame_index_;
63 std::vector<SurfaceId> new_referenced_surfaces;
64 if (current_frame_) {
65 for (auto& render_pass :
66 current_frame_->delegated_frame_data->render_pass_list) {
67 new_referenced_surfaces.insert(new_referenced_surfaces.end(),
68 render_pass->referenced_surfaces.begin(),
69 render_pass->referenced_surfaces.end());
73 if (previous_frame) {
74 ReturnedResourceArray previous_resources;
75 TransferableResource::ReturnResources(
76 previous_frame->delegated_frame_data->resource_list,
77 &previous_resources);
78 factory_->UnrefResources(previous_resources);
80 if (!draw_callback_.is_null())
81 draw_callback_.Run(SurfaceDrawStatus::DRAW_SKIPPED);
82 draw_callback_ = callback;
84 bool referenced_surfaces_changed =
85 (referenced_surfaces_ != new_referenced_surfaces);
86 referenced_surfaces_ = new_referenced_surfaces;
87 std::vector<uint32_t> satisfies_sequences;
88 if (current_frame_)
89 current_frame_->metadata.satisfies_sequences.swap(satisfies_sequences);
90 if (referenced_surfaces_changed || !satisfies_sequences.empty()) {
91 // Notify the manager that sequences were satisfied either if some new
92 // sequences were satisfied, or if the set of referenced surfaces changed
93 // to force a GC to happen.
94 factory_->manager()->DidSatisfySequences(
95 SurfaceIdAllocator::NamespaceForId(surface_id_), &satisfies_sequences);
99 void Surface::RequestCopyOfOutput(scoped_ptr<CopyOutputRequest> copy_request) {
100 if (current_frame_ &&
101 !current_frame_->delegated_frame_data->render_pass_list.empty())
102 current_frame_->delegated_frame_data->render_pass_list.back()
103 ->copy_requests.push_back(copy_request.Pass());
104 else
105 copy_request->SendEmptyResult();
108 void Surface::TakeCopyOutputRequests(
109 std::multimap<RenderPassId, CopyOutputRequest*>* copy_requests) {
110 DCHECK(copy_requests->empty());
111 if (current_frame_) {
112 for (const auto& render_pass :
113 current_frame_->delegated_frame_data->render_pass_list) {
114 while (!render_pass->copy_requests.empty()) {
115 scoped_ptr<CopyOutputRequest> request =
116 render_pass->copy_requests.take_back();
117 render_pass->copy_requests.pop_back();
118 copy_requests->insert(
119 std::make_pair(render_pass->id, request.release()));
125 const CompositorFrame* Surface::GetEligibleFrame() {
126 return current_frame_.get();
129 void Surface::TakeLatencyInfo(std::vector<ui::LatencyInfo>* latency_info) {
130 if (!current_frame_)
131 return;
132 if (latency_info->empty()) {
133 current_frame_->metadata.latency_info.swap(*latency_info);
134 return;
136 std::copy(current_frame_->metadata.latency_info.begin(),
137 current_frame_->metadata.latency_info.end(),
138 std::back_inserter(*latency_info));
139 current_frame_->metadata.latency_info.clear();
142 void Surface::RunDrawCallbacks(SurfaceDrawStatus drawn) {
143 if (!draw_callback_.is_null()) {
144 DrawCallback callback = draw_callback_;
145 draw_callback_ = DrawCallback();
146 callback.Run(drawn);
150 void Surface::AddDestructionDependency(SurfaceSequence sequence) {
151 destruction_dependencies_.push_back(sequence);
154 void Surface::SatisfyDestructionDependencies(
155 base::hash_set<SurfaceSequence>* sequences,
156 base::hash_set<uint32_t>* valid_id_namespaces) {
157 destruction_dependencies_.erase(
158 std::remove_if(destruction_dependencies_.begin(),
159 destruction_dependencies_.end(),
160 [sequences, valid_id_namespaces](SurfaceSequence seq) {
161 return (!!sequences->erase(seq) ||
162 !valid_id_namespaces->count(seq.id_namespace));
164 destruction_dependencies_.end());
167 void Surface::ClearCopyRequests() {
168 if (current_frame_) {
169 for (const auto& render_pass :
170 current_frame_->delegated_frame_data->render_pass_list) {
171 for (const auto& copy_request : render_pass->copy_requests)
172 copy_request->SendEmptyResult();
177 } // namespace cc