Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(413)

Side by Side Diff: cc/surfaces/compositor_frame_sink_support.cc

Issue 2802023002: Remove SurfaceFactory And SurfaceFactoryClient (Closed)
Patch Set: Address Comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/surfaces/compositor_frame_sink_support.h" 5 #include "cc/surfaces/compositor_frame_sink_support.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "cc/output/compositor_frame.h" 10 #include "cc/output/compositor_frame.h"
11 #include "cc/scheduler/begin_frame_source.h" 11 #include "cc/scheduler/begin_frame_source.h"
12 #include "cc/surfaces/compositor_frame_sink_support_client.h" 12 #include "cc/surfaces/compositor_frame_sink_support_client.h"
13 #include "cc/surfaces/display.h" 13 #include "cc/surfaces/display.h"
14 #include "cc/surfaces/surface.h" 14 #include "cc/surfaces/surface.h"
15 #include "cc/surfaces/surface_info.h"
15 #include "cc/surfaces/surface_manager.h" 16 #include "cc/surfaces/surface_manager.h"
16 #include "cc/surfaces/surface_reference.h" 17 #include "cc/surfaces/surface_reference.h"
17 18
18 namespace cc { 19 namespace cc {
19 20
20 // static 21 // static
21 std::unique_ptr<CompositorFrameSinkSupport> CompositorFrameSinkSupport::Create( 22 std::unique_ptr<CompositorFrameSinkSupport> CompositorFrameSinkSupport::Create(
22 CompositorFrameSinkSupportClient* client, 23 CompositorFrameSinkSupportClient* client,
23 SurfaceManager* surface_manager, 24 SurfaceManager* surface_manager,
24 const FrameSinkId& frame_sink_id, 25 const FrameSinkId& frame_sink_id,
25 bool is_root, 26 bool is_root,
26 bool handles_frame_sink_id_invalidation, 27 bool handles_frame_sink_id_invalidation,
27 bool needs_sync_points) { 28 bool needs_sync_points) {
28 std::unique_ptr<CompositorFrameSinkSupport> support = 29 std::unique_ptr<CompositorFrameSinkSupport> support =
29 base::WrapUnique(new CompositorFrameSinkSupport( 30 base::WrapUnique(new CompositorFrameSinkSupport(
30 client, frame_sink_id, is_root, handles_frame_sink_id_invalidation)); 31 client, frame_sink_id, is_root, handles_frame_sink_id_invalidation,
31 support->Init(surface_manager, needs_sync_points); 32 needs_sync_points));
33 support->Init(surface_manager);
32 return support; 34 return support;
33 } 35 }
34 36
35 CompositorFrameSinkSupport::~CompositorFrameSinkSupport() { 37 CompositorFrameSinkSupport::~CompositorFrameSinkSupport() {
36 // Unregister |this| as a BeginFrameObserver so that the BeginFrameSource does 38 // Unregister |this| as a BeginFrameObserver so that the BeginFrameSource does
37 // not call into |this| after it's deleted. 39 // not call into |this| after it's deleted.
38 SetNeedsBeginFrame(false); 40 SetNeedsBeginFrame(false);
39 41
40 // For display root surfaces, the surface is no longer going to be visible 42 // For display root surfaces, the surface is no longer going to be visible
41 // so make it unreachable from the top-level root. 43 // so make it unreachable from the top-level root.
42 if (surface_manager_->using_surface_references() && is_root_ && 44 if (surface_manager_->using_surface_references() && is_root_ &&
43 reference_tracker_.current_surface_id().is_valid()) 45 reference_tracker_.current_surface_id().is_valid())
44 RemoveTopLevelRootReference(reference_tracker_.current_surface_id()); 46 RemoveTopLevelRootReference(reference_tracker_.current_surface_id());
45 47
46 // SurfaceFactory's destructor will attempt to return resources which will 48 EvictFrame();
47 // call back into here and access |client_| so we should destroy
48 // |surface_factory_|'s resources early on.
49 surface_factory_->EvictSurface();
50 surface_manager_->UnregisterFrameSinkManagerClient(frame_sink_id_); 49 surface_manager_->UnregisterFrameSinkManagerClient(frame_sink_id_);
51 if (handles_frame_sink_id_invalidation_) 50 if (handles_frame_sink_id_invalidation_)
52 surface_manager_->InvalidateFrameSinkId(frame_sink_id_); 51 surface_manager_->InvalidateFrameSinkId(frame_sink_id_);
53 } 52 }
54 53
55 void CompositorFrameSinkSupport::ReferencedSurfacesChanged(
56 const LocalSurfaceId& local_surface_id,
57 const std::vector<SurfaceId>* active_referenced_surfaces) {
58 if (!surface_manager_->using_surface_references())
59 return;
60
61 SurfaceId last_surface_id = reference_tracker_.current_surface_id();
62
63 // Populate list of surface references to add and remove based on reference
64 // surfaces in current frame compared with the last frame. The list of
65 // surface references includes references from both the pending and active
66 // frame if any.
67 reference_tracker_.UpdateReferences(local_surface_id,
68 active_referenced_surfaces);
69
70 UpdateSurfaceReferences(last_surface_id, local_surface_id);
71 }
72
73 void CompositorFrameSinkSupport::ReturnResources( 54 void CompositorFrameSinkSupport::ReturnResources(
74 const ReturnedResourceArray& resources) { 55 const ReturnedResourceArray& resources) {
75 if (resources.empty()) 56 if (resources.empty())
76 return; 57 return;
77 if (!ack_pending_count_ && client_) { 58 if (!ack_pending_count_ && client_) {
78 client_->ReclaimResources(resources); 59 client_->ReclaimResources(resources);
79 return; 60 return;
80 } 61 }
81 62
82 std::copy(resources.begin(), resources.end(), 63 std::copy(resources.begin(), resources.end(),
83 std::back_inserter(surface_returned_resources_)); 64 std::back_inserter(surface_returned_resources_));
84 } 65 }
85 66
86 void CompositorFrameSinkSupport::SetBeginFrameSource( 67 void CompositorFrameSinkSupport::SetBeginFrameSource(
87 BeginFrameSource* begin_frame_source) { 68 BeginFrameSource* begin_frame_source) {
88 if (begin_frame_source_ && added_frame_observer_) { 69 if (begin_frame_source_ && added_frame_observer_) {
89 begin_frame_source_->RemoveObserver(this); 70 begin_frame_source_->RemoveObserver(this);
90 added_frame_observer_ = false; 71 added_frame_observer_ = false;
91 } 72 }
92 begin_frame_source_ = begin_frame_source; 73 begin_frame_source_ = begin_frame_source;
93 UpdateNeedsBeginFramesInternal(); 74 UpdateNeedsBeginFramesInternal();
94 } 75 }
95 76
96 void CompositorFrameSinkSupport::EvictFrame() { 77 void CompositorFrameSinkSupport::EvictFrame() {
97 DCHECK(surface_factory_); 78 if (!current_surface_)
98 surface_factory_->EvictSurface(); 79 return;
80 DestroyCurrentSurface();
99 } 81 }
100 82
101 void CompositorFrameSinkSupport::SetNeedsBeginFrame(bool needs_begin_frame) { 83 void CompositorFrameSinkSupport::SetNeedsBeginFrame(bool needs_begin_frame) {
102 needs_begin_frame_ = needs_begin_frame; 84 needs_begin_frame_ = needs_begin_frame;
103 UpdateNeedsBeginFramesInternal(); 85 UpdateNeedsBeginFramesInternal();
104 } 86 }
105 87
106 void CompositorFrameSinkSupport::BeginFrameDidNotSwap( 88 void CompositorFrameSinkSupport::BeginFrameDidNotSwap(
107 const BeginFrameAck& ack) { 89 const BeginFrameAck& ack) {
108 // TODO(eseckler): While a pending CompositorFrame exists (see TODO below), we 90 // TODO(eseckler): While a pending CompositorFrame exists (see TODO below), we
109 // should not acknowledge immediately. Instead, we should update the ack that 91 // should not acknowledge immediately. Instead, we should update the ack that
110 // will be sent to DisplayScheduler when the pending frame is activated. 92 // will be sent to DisplayScheduler when the pending frame is activated.
111 DCHECK_GE(ack.sequence_number, BeginFrameArgs::kStartingFrameNumber); 93 DCHECK_GE(ack.sequence_number, BeginFrameArgs::kStartingFrameNumber);
112 94
113 // |has_damage| is not transmitted, but false by default. 95 // |has_damage| is not transmitted, but false by default.
114 DCHECK(!ack.has_damage); 96 DCHECK(!ack.has_damage);
115 if (begin_frame_source_) 97 if (begin_frame_source_)
116 begin_frame_source_->DidFinishFrame(this, ack); 98 begin_frame_source_->DidFinishFrame(this, ack);
117 } 99 }
118 100
119 void CompositorFrameSinkSupport::SubmitCompositorFrame( 101 void CompositorFrameSinkSupport::SubmitCompositorFrame(
120 const LocalSurfaceId& local_surface_id, 102 const LocalSurfaceId& local_surface_id,
121 CompositorFrame frame) { 103 CompositorFrame frame) {
122 DCHECK(surface_factory_); 104 TRACE_EVENT0("cc", "CompositorFrameSinkSupport::SubmitCompositorFrame");
105 DCHECK(local_surface_id.is_valid());
123 ++ack_pending_count_; 106 ++ack_pending_count_;
124 107
125 DCHECK_GE(frame.metadata.begin_frame_ack.sequence_number, 108 DCHECK_GE(frame.metadata.begin_frame_ack.sequence_number,
126 BeginFrameArgs::kStartingFrameNumber); 109 BeginFrameArgs::kStartingFrameNumber);
127 110
128 // |has_damage| is not transmitted. 111 // |has_damage| is not transmitted.
129 frame.metadata.begin_frame_ack.has_damage = true; 112 frame.metadata.begin_frame_ack.has_damage = true;
130 113
131 BeginFrameAck ack = frame.metadata.begin_frame_ack; 114 BeginFrameAck ack = frame.metadata.begin_frame_ack;
132 surface_factory_->SubmitCompositorFrame( 115 for (ui::LatencyInfo& latency : frame.metadata.latency_info) {
133 local_surface_id, std::move(frame), 116 if (latency.latency_components().size() > 0) {
117 latency.AddLatencyNumber(ui::DISPLAY_COMPOSITOR_RECEIVED_FRAME_COMPONENT,
118 0, 0);
119 }
120 }
121
122 std::unique_ptr<Surface> surface;
123 bool create_new_surface =
124 (!current_surface_ ||
125 local_surface_id != current_surface_->surface_id().local_surface_id());
126 if (!create_new_surface) {
127 surface = std::move(current_surface_);
128 } else {
129 surface = CreateSurface(local_surface_id);
130 }
131
132 // Receive and track the resources referenced from the CompositorFrame
133 // regardless of whether it's pending or active.
134 surface_resource_holder_.ReceiveFromChild(frame.resource_list);
135 surface->QueueFrame(
136 std::move(frame),
134 base::Bind(&CompositorFrameSinkSupport::DidReceiveCompositorFrameAck, 137 base::Bind(&CompositorFrameSinkSupport::DidReceiveCompositorFrameAck,
135 weak_factory_.GetWeakPtr()), 138 weak_factory_.GetWeakPtr()),
136 base::BindRepeating(&CompositorFrameSinkSupport::WillDrawSurface, 139 base::BindRepeating(&CompositorFrameSinkSupport::WillDrawSurface,
137 weak_factory_.GetWeakPtr())); 140 weak_factory_.GetWeakPtr()));
138 141
142 if (current_surface_) {
143 surface->SetPreviousFrameSurface(current_surface_.get());
144 DestroyCurrentSurface();
145 }
146 current_surface_ = std::move(surface);
147
139 // TODO(eseckler): The CompositorFrame submitted below might not be activated 148 // TODO(eseckler): The CompositorFrame submitted below might not be activated
140 // right away b/c of surface synchronization. We should only send the 149 // right away b/c of surface synchronization. We should only send the
141 // BeginFrameAck to DisplayScheduler when it is activated. This also means 150 // BeginFrameAck to DisplayScheduler when it is activated. This also means
142 // that we need to stay an active BFO while a CompositorFrame is pending. 151 // that we need to stay an active BFO while a CompositorFrame is pending.
143 // See https://crbug.com/703079. 152 // See https://crbug.com/703079.
144 if (begin_frame_source_) 153 if (begin_frame_source_)
145 begin_frame_source_->DidFinishFrame(this, ack); 154 begin_frame_source_->DidFinishFrame(this, ack);
146 } 155 }
147 156
148 void CompositorFrameSinkSupport::UpdateSurfaceReferences( 157 void CompositorFrameSinkSupport::UpdateSurfaceReferences(
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 SurfaceReference reference(surface_manager_->GetRootSurfaceId(), surface_id); 192 SurfaceReference reference(surface_manager_->GetRootSurfaceId(), surface_id);
184 surface_manager_->AddSurfaceReferences({reference}); 193 surface_manager_->AddSurfaceReferences({reference});
185 } 194 }
186 195
187 void CompositorFrameSinkSupport::RemoveTopLevelRootReference( 196 void CompositorFrameSinkSupport::RemoveTopLevelRootReference(
188 const SurfaceId& surface_id) { 197 const SurfaceId& surface_id) {
189 SurfaceReference reference(surface_manager_->GetRootSurfaceId(), surface_id); 198 SurfaceReference reference(surface_manager_->GetRootSurfaceId(), surface_id);
190 surface_manager_->RemoveSurfaceReferences({reference}); 199 surface_manager_->RemoveSurfaceReferences({reference});
191 } 200 }
192 201
202 void CompositorFrameSinkSupport::ReferencedSurfacesChanged(
203 const LocalSurfaceId& local_surface_id,
204 const std::vector<SurfaceId>* active_referenced_surfaces) {
205 if (!surface_manager_->using_surface_references())
206 return;
207
208 SurfaceId last_surface_id = reference_tracker_.current_surface_id();
209
210 // Populate list of surface references to add and remove based on reference
211 // surfaces in current frame compared with the last frame. The list of
212 // surface references includes references from both the pending and active
213 // frame if any.
214 reference_tracker_.UpdateReferences(local_surface_id,
215 active_referenced_surfaces);
216
217 UpdateSurfaceReferences(last_surface_id, local_surface_id);
218 }
219
193 void CompositorFrameSinkSupport::DidReceiveCompositorFrameAck() { 220 void CompositorFrameSinkSupport::DidReceiveCompositorFrameAck() {
194 DCHECK_GT(ack_pending_count_, 0); 221 DCHECK_GT(ack_pending_count_, 0);
195 ack_pending_count_--; 222 ack_pending_count_--;
196 if (!client_) 223 if (!client_)
197 return; 224 return;
225
198 client_->DidReceiveCompositorFrameAck(surface_returned_resources_); 226 client_->DidReceiveCompositorFrameAck(surface_returned_resources_);
199 surface_returned_resources_.clear(); 227 surface_returned_resources_.clear();
200 } 228 }
201 229
202 void CompositorFrameSinkSupport::WillDrawSurface( 230 void CompositorFrameSinkSupport::WillDrawSurface(
203 const LocalSurfaceId& local_surface_id, 231 const LocalSurfaceId& local_surface_id,
204 const gfx::Rect& damage_rect) { 232 const gfx::Rect& damage_rect) {
205 if (client_) 233 if (client_)
206 client_->WillDrawSurface(local_surface_id, damage_rect); 234 client_->WillDrawSurface(local_surface_id, damage_rect);
207 } 235 }
208 236
209 void CompositorFrameSinkSupport::ClaimTemporaryReference( 237 void CompositorFrameSinkSupport::ClaimTemporaryReference(
210 const SurfaceId& surface_id) { 238 const SurfaceId& surface_id) {
211 surface_manager_->AssignTemporaryReference(surface_id, frame_sink_id_); 239 surface_manager_->AssignTemporaryReference(surface_id, frame_sink_id_);
212 } 240 }
213 241
242 void CompositorFrameSinkSupport::RefResources(
243 const TransferableResourceArray& resources) {
244 surface_resource_holder_.RefResources(resources);
245 }
246
247 void CompositorFrameSinkSupport::UnrefResources(
248 const ReturnedResourceArray& resources) {
249 surface_resource_holder_.UnrefResources(resources);
250 }
251
214 CompositorFrameSinkSupport::CompositorFrameSinkSupport( 252 CompositorFrameSinkSupport::CompositorFrameSinkSupport(
215 CompositorFrameSinkSupportClient* client, 253 CompositorFrameSinkSupportClient* client,
216 const FrameSinkId& frame_sink_id, 254 const FrameSinkId& frame_sink_id,
217 bool is_root, 255 bool is_root,
218 bool handles_frame_sink_id_invalidation) 256 bool handles_frame_sink_id_invalidation,
257 bool needs_sync_points)
219 : client_(client), 258 : client_(client),
220 frame_sink_id_(frame_sink_id), 259 frame_sink_id_(frame_sink_id),
260 surface_resource_holder_(this),
221 reference_tracker_(frame_sink_id), 261 reference_tracker_(frame_sink_id),
222 is_root_(is_root), 262 is_root_(is_root),
263 needs_sync_points_(needs_sync_points),
223 handles_frame_sink_id_invalidation_(handles_frame_sink_id_invalidation), 264 handles_frame_sink_id_invalidation_(handles_frame_sink_id_invalidation),
224 weak_factory_(this) {} 265 weak_factory_(this) {}
225 266
226 void CompositorFrameSinkSupport::Init(SurfaceManager* surface_manager, 267 void CompositorFrameSinkSupport::Init(SurfaceManager* surface_manager) {
227 bool needs_sync_points) {
228 surface_manager_ = surface_manager; 268 surface_manager_ = surface_manager;
229 surface_factory_ = base::MakeUnique<SurfaceFactory>(
230 frame_sink_id_, surface_manager_, this, this);
231 if (handles_frame_sink_id_invalidation_) 269 if (handles_frame_sink_id_invalidation_)
232 surface_manager_->RegisterFrameSinkId(frame_sink_id_); 270 surface_manager_->RegisterFrameSinkId(frame_sink_id_);
233 surface_manager_->RegisterFrameSinkManagerClient(frame_sink_id_, this); 271 surface_manager_->RegisterFrameSinkManagerClient(frame_sink_id_, this);
234 surface_factory_->set_needs_sync_points(needs_sync_points);
235 } 272 }
236 273
237 void CompositorFrameSinkSupport::OnBeginFrame(const BeginFrameArgs& args) { 274 void CompositorFrameSinkSupport::OnBeginFrame(const BeginFrameArgs& args) {
238 UpdateNeedsBeginFramesInternal(); 275 UpdateNeedsBeginFramesInternal();
239 last_begin_frame_args_ = args; 276 last_begin_frame_args_ = args;
240 if (client_) 277 if (client_)
241 client_->OnBeginFrame(args); 278 client_->OnBeginFrame(args);
242 } 279 }
243 280
244 const BeginFrameArgs& CompositorFrameSinkSupport::LastUsedBeginFrameArgs() 281 const BeginFrameArgs& CompositorFrameSinkSupport::LastUsedBeginFrameArgs()
245 const { 282 const {
246 return last_begin_frame_args_; 283 return last_begin_frame_args_;
247 } 284 }
248 285
249 void CompositorFrameSinkSupport::OnBeginFrameSourcePausedChanged(bool paused) {} 286 void CompositorFrameSinkSupport::OnBeginFrameSourcePausedChanged(bool paused) {}
250 287
288 void CompositorFrameSinkSupport::OnSurfaceActivated(Surface* surface) {
289 DCHECK(surface->HasActiveFrame());
290 // TODO(staraz): Notify BeginFrameSource about the last activated sequence
291 // number.
292 if (!seen_first_frame_activation_) {
293 seen_first_frame_activation_ = true;
294
295 const CompositorFrame& frame = surface->GetActiveFrame();
296 // CompositorFrames might not be populated with a RenderPass in unit tests.
297 gfx::Size frame_size;
298 if (!frame.render_pass_list.empty())
299 frame_size = frame.render_pass_list.back()->output_rect.size();
300
301 // SurfaceCreated only applies for the first Surface activation. Thus,
302 // SurfaceFactory stops observing new activations after the first one.
303 surface_manager_->SurfaceCreated(SurfaceInfo(
304 surface->surface_id(), frame.metadata.device_scale_factor, frame_size));
305 }
306 // Fire SurfaceCreated first so that a temporary reference is added before it
307 // is potentially transformed into a real reference by the client.
308 ReferencedSurfacesChanged(surface->surface_id().local_surface_id(),
309 surface->active_referenced_surfaces());
310 if (!surface_manager_->SurfaceModified(surface->surface_id())) {
311 TRACE_EVENT_INSTANT0("cc", "Damage not visible.", TRACE_EVENT_SCOPE_THREAD);
312 surface->RunDrawCallback();
313 }
314 }
315
316 void CompositorFrameSinkSupport::OnSurfaceDependenciesChanged(
317 Surface* surface,
318 const SurfaceDependencies& added_dependencies,
319 const SurfaceDependencies& removed_dependencies) {}
320
321 void CompositorFrameSinkSupport::OnSurfaceDiscarded(Surface* surface) {}
322
251 void CompositorFrameSinkSupport::UpdateNeedsBeginFramesInternal() { 323 void CompositorFrameSinkSupport::UpdateNeedsBeginFramesInternal() {
252 if (!begin_frame_source_) 324 if (!begin_frame_source_)
253 return; 325 return;
254 326
255 if (needs_begin_frame_ == added_frame_observer_) 327 if (needs_begin_frame_ == added_frame_observer_)
256 return; 328 return;
257 329
258 added_frame_observer_ = needs_begin_frame_; 330 added_frame_observer_ = needs_begin_frame_;
259 if (needs_begin_frame_) 331 if (needs_begin_frame_)
260 begin_frame_source_->AddObserver(this); 332 begin_frame_source_->AddObserver(this);
261 else 333 else
262 begin_frame_source_->RemoveObserver(this); 334 begin_frame_source_->RemoveObserver(this);
263 } 335 }
264 336
337 std::unique_ptr<Surface> CompositorFrameSinkSupport::CreateSurface(
338 const LocalSurfaceId& local_surface_id) {
339 seen_first_frame_activation_ = false;
340 std::unique_ptr<Surface> surface = surface_manager_->CreateSurface(
341 weak_factory_.GetWeakPtr(), local_surface_id);
342 surface->AddObserver(this);
343 return surface;
344 }
345
346 void CompositorFrameSinkSupport::DestroyCurrentSurface() {
347 current_surface_->RemoveObserver(this);
348 surface_manager_->DestroySurface(std::move(current_surface_));
349 }
350
265 void CompositorFrameSinkSupport::RequestCopyOfSurface( 351 void CompositorFrameSinkSupport::RequestCopyOfSurface(
266 std::unique_ptr<CopyOutputRequest> request) { 352 std::unique_ptr<CopyOutputRequest> copy_request) {
267 DCHECK(surface_factory_); 353 if (!current_surface_)
268 surface_factory_->RequestCopyOfSurface(std::move(request)); 354 return;
355
356 DCHECK(current_surface_->compositor_frame_sink_support().get() == this);
357 current_surface_->RequestCopyOfOutput(std::move(copy_request));
358 surface_manager_->SurfaceModified(current_surface_->surface_id());
269 } 359 }
270 360
271 } // namespace cc 361 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698