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

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

Issue 2785103003: [cc] CompositorFrameSinkSupport: Defer BeginFrameAck of pending frames. (Closed)
Patch Set: add some comments, plumb SurfaceDiscarded. Created 3 years, 8 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"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 45
46 // For display root surfaces, the surface is no longer going to be visible 46 // For display root surfaces, the surface is no longer going to be visible
47 // so make it unreachable from the top-level root. 47 // so make it unreachable from the top-level root.
48 if (surface_manager_->using_surface_references() && is_root_ && 48 if (surface_manager_->using_surface_references() && is_root_ &&
49 reference_tracker_.current_surface_id().is_valid()) 49 reference_tracker_.current_surface_id().is_valid())
50 RemoveTopLevelRootReference(reference_tracker_.current_surface_id()); 50 RemoveTopLevelRootReference(reference_tracker_.current_surface_id());
51 51
52 // SurfaceFactory's destructor will attempt to return resources which will 52 // SurfaceFactory's destructor will attempt to return resources which will
53 // call back into here and access |client_| so we should destroy 53 // call back into here and access |client_| so we should destroy
54 // |surface_factory_|'s resources early on. 54 // |surface_factory_|'s resources early on.
55 surface_factory_.EvictSurface(); 55 EvictFrame();
56 surface_manager_->UnregisterSurfaceFactoryClient(frame_sink_id_); 56 surface_manager_->UnregisterSurfaceFactoryClient(frame_sink_id_);
57 if (handles_frame_sink_id_invalidation_) 57 if (handles_frame_sink_id_invalidation_)
58 surface_manager_->InvalidateFrameSinkId(frame_sink_id_); 58 surface_manager_->InvalidateFrameSinkId(frame_sink_id_);
59 } 59 }
60 60
61 void CompositorFrameSinkSupport::EvictFrame() { 61 void CompositorFrameSinkSupport::EvictFrame() {
62 surface_factory_.EvictSurface(); 62 surface_factory_.EvictSurface();
63 } 63 }
64 64
65 void CompositorFrameSinkSupport::SetNeedsBeginFrame(bool needs_begin_frame) { 65 void CompositorFrameSinkSupport::SetNeedsBeginFrame(bool needs_begin_frame) {
66 needs_begin_frame_ = needs_begin_frame; 66 client_needs_begin_frame_ = needs_begin_frame;
67 bool was_observing = added_frame_observer_;
67 UpdateNeedsBeginFramesInternal(); 68 UpdateNeedsBeginFramesInternal();
69
70 // If we are observing because of a pending CompositorFrame, we may have
71 // previously received a BeginFrame that we should now forward to the client.
72 // BackToBackBeginFrameSource sends a new BeginFrame in a separate task
73 // provided we weren't observing before, so we special-case it here.
74 if (needs_begin_frame && !last_begin_frame_sent_to_client_ &&
75 (was_observing || begin_frame_source_->IsThrottled())) {
76 last_begin_frame_sent_to_client_ = true;
77 client_->OnBeginFrame(last_begin_frame_args_);
78 }
68 } 79 }
69 80
70 void CompositorFrameSinkSupport::BeginFrameDidNotSwap( 81 void CompositorFrameSinkSupport::BeginFrameDidNotSwap(
71 const BeginFrameAck& ack) { 82 const BeginFrameAck& ack) {
72 // TODO(eseckler): While a pending CompositorFrame exists (see TODO below), we
73 // should not acknowledge immediately. Instead, we should update the ack that
74 // will be sent to DisplayScheduler when the pending frame is activated.
75 DCHECK_LE(BeginFrameArgs::kStartingFrameNumber, ack.sequence_number); 83 DCHECK_LE(BeginFrameArgs::kStartingFrameNumber, ack.sequence_number);
84
85 if (ack.sequence_number < BeginFrameArgs::kStartingFrameNumber) {
86 DLOG(ERROR) << "Received BeginFrameDidNotSwap with invalid BeginFrameAck.";
87 return;
88 }
76 // |has_damage| is not transmitted, but false by default. 89 // |has_damage| is not transmitted, but false by default.
77 DCHECK(!ack.has_damage); 90 DCHECK(!ack.has_damage);
78 if (begin_frame_source_) 91
79 begin_frame_source_->DidFinishFrame(this, ack); 92 // Client can continue to request BeginFrames after submitting a
93 // CompositorFrame, so we may end up with a pending frame here.
94 if (has_pending_frame_) {
95 // We will acknowledge the BeginFrame once the pending frame activates.
96 begin_frame_ack_for_pending_frame_.source_id = ack.source_id;
Fady Samuel 2017/03/31 00:09:02 I wonder if we can modify the pending CompositorFr
97 begin_frame_ack_for_pending_frame_.latest_confirmed_sequence_number =
98 ack.latest_confirmed_sequence_number;
99 return;
100 }
101
102 AcknowledgeBeginFrame(ack);
80 } 103 }
81 104
82 void CompositorFrameSinkSupport::SubmitCompositorFrame( 105 void CompositorFrameSinkSupport::SubmitCompositorFrame(
83 const LocalSurfaceId& local_surface_id, 106 const LocalSurfaceId& local_surface_id,
84 CompositorFrame frame) { 107 CompositorFrame frame) {
85 ++ack_pending_count_; 108 ++ack_pending_count_;
86 109
87 if (frame.metadata.begin_frame_ack.sequence_number < 110 if (frame.metadata.begin_frame_ack.sequence_number <
88 BeginFrameArgs::kStartingFrameNumber) { 111 BeginFrameArgs::kStartingFrameNumber) {
89 DLOG(ERROR) << "Received CompositorFrame with invalid BeginFrameAck."; 112 DLOG(ERROR) << "Received CompositorFrame with invalid BeginFrameAck.";
90 frame.metadata.begin_frame_ack.source_id = BeginFrameArgs::kManualSourceId; 113 frame.metadata.begin_frame_ack.source_id = BeginFrameArgs::kManualSourceId;
91 frame.metadata.begin_frame_ack.sequence_number = 114 frame.metadata.begin_frame_ack.sequence_number =
92 BeginFrameArgs::kStartingFrameNumber; 115 BeginFrameArgs::kStartingFrameNumber;
93 } 116 }
94 // |has_damage| is not transmitted. 117 // |has_damage| is not transmitted.
95 frame.metadata.begin_frame_ack.has_damage = true; 118 frame.metadata.begin_frame_ack.has_damage = true;
96 119
120 // The CompositorFrame submitted below might not be activated right away b/c
121 // of surface synchronization. We only forward the CompositorFrame's
122 // BeginFrameAck to DisplayScheduler once it is activated. While waiting for
123 // activation, we continue to observe BeginFrames. If the CompositorFrame is
124 // not activated before we receive the next BeginFrame, we will acknowledge
125 // the current one without updates.
126 has_pending_frame_ = true;
Fady Samuel 2017/03/31 00:09:02 Surface::HasPendingFrame exists. Would it make se
127 surface_id_for_pending_frame_ = local_surface_id;
128 // If there was a previous pending frame that didn't get activated, this
129 // overrides the pending BeginFrameAck.
130 begin_frame_ack_for_pending_frame_ = frame.metadata.begin_frame_ack;
131 // Ensure that we continue observing BeginFrames while the frame is pending.
132 UpdateNeedsBeginFramesInternal();
133
97 surface_factory_.SubmitCompositorFrame( 134 surface_factory_.SubmitCompositorFrame(
98 local_surface_id, std::move(frame), 135 local_surface_id, std::move(frame),
99 base::Bind(&CompositorFrameSinkSupport::DidReceiveCompositorFrameAck, 136 base::Bind(&CompositorFrameSinkSupport::DidReceiveCompositorFrameAck,
100 weak_factory_.GetWeakPtr())); 137 weak_factory_.GetWeakPtr()));
101
102 // TODO(eseckler): The CompositorFrame submitted below might not be activated
103 // right away b/c of surface synchronization. We should only send the
104 // BeginFrameAck to DisplayScheduler when it is activated. This also means
105 // that we need to stay an active BFO while a CompositorFrame is pending.
106 // See https://crbug.com/703079.
107 if (begin_frame_source_)
108 begin_frame_source_->DidFinishFrame(this, frame.metadata.begin_frame_ack);
109 } 138 }
110 139
111 void CompositorFrameSinkSupport::UpdateSurfaceReferences( 140 void CompositorFrameSinkSupport::UpdateSurfaceReferences(
112 const SurfaceId& last_surface_id, 141 const SurfaceId& last_surface_id,
113 const LocalSurfaceId& local_surface_id) { 142 const LocalSurfaceId& local_surface_id) {
114 const bool surface_id_changed = 143 const bool surface_id_changed =
115 last_surface_id.local_surface_id() != local_surface_id; 144 last_surface_id.local_surface_id() != local_surface_id;
116 145
117 // If this is a display root surface and the SurfaceId is changing, make the 146 // If this is a display root surface and the SurfaceId is changing, make the
118 // new SurfaceId reachable from the top-level root. 147 // new SurfaceId reachable from the top-level root.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 // We return the resources before sending an ack so they can be reused in 192 // We return the resources before sending an ack so they can be reused in
164 // making the next CompositorFrame. 193 // making the next CompositorFrame.
165 if (!surface_returned_resources_.empty()) { 194 if (!surface_returned_resources_.empty()) {
166 client_->ReclaimResources(surface_returned_resources_); 195 client_->ReclaimResources(surface_returned_resources_);
167 surface_returned_resources_.clear(); 196 surface_returned_resources_.clear();
168 } 197 }
169 client_->DidReceiveCompositorFrameAck(); 198 client_->DidReceiveCompositorFrameAck();
170 } 199 }
171 200
172 void CompositorFrameSinkSupport::ForceReclaimResources() { 201 void CompositorFrameSinkSupport::ForceReclaimResources() {
202 PendingFrameDiscarded();
173 surface_factory_.ClearSurface(); 203 surface_factory_.ClearSurface();
174 } 204 }
175 205
176 void CompositorFrameSinkSupport::ClaimTemporaryReference( 206 void CompositorFrameSinkSupport::ClaimTemporaryReference(
177 const SurfaceId& surface_id) { 207 const SurfaceId& surface_id) {
178 surface_manager_->AssignTemporaryReference(surface_id, frame_sink_id_); 208 surface_manager_->AssignTemporaryReference(surface_id, frame_sink_id_);
179 } 209 }
180 210
181 void CompositorFrameSinkSupport::ReferencedSurfacesChanged( 211 void CompositorFrameSinkSupport::ReferencedSurfacesChanged(
182 const LocalSurfaceId& local_surface_id, 212 const LocalSurfaceId& local_surface_id,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 UpdateNeedsBeginFramesInternal(); 252 UpdateNeedsBeginFramesInternal();
223 } 253 }
224 254
225 void CompositorFrameSinkSupport::WillDrawSurface( 255 void CompositorFrameSinkSupport::WillDrawSurface(
226 const LocalSurfaceId& local_surface_id, 256 const LocalSurfaceId& local_surface_id,
227 const gfx::Rect& damage_rect) { 257 const gfx::Rect& damage_rect) {
228 if (client_) 258 if (client_)
229 client_->WillDrawSurface(local_surface_id, damage_rect); 259 client_->WillDrawSurface(local_surface_id, damage_rect);
230 } 260 }
231 261
262 void CompositorFrameSinkSupport::PendingFrameActivated(
263 const LocalSurfaceId& local_surface_id) {
264 // has_pending_frame_ may be false because ClearSurface() submits
265 // CompositorFrames we don't track.
266 if (!has_pending_frame_)
267 return;
268 DCHECK_EQ(surface_id_for_pending_frame_, local_surface_id);
269 has_pending_frame_ = false;
270
271 if (last_begin_frame_args_.IsValid()) {
272 if (last_begin_frame_args_.source_id !=
273 begin_frame_ack_for_pending_frame_.source_id) {
274 // BeginFrameSource has changed, we cannot confirm any sequence number.
275 begin_frame_ack_for_pending_frame_.source_id =
276 last_begin_frame_args_.source_id;
277 begin_frame_ack_for_pending_frame_.latest_confirmed_sequence_number =
278 BeginFrameArgs::kInvalidFrameNumber;
279 }
280
281 begin_frame_ack_for_pending_frame_.sequence_number =
282 last_begin_frame_args_.sequence_number;
283 }
284
285 DCHECK(begin_frame_ack_for_pending_frame_.has_damage);
286 AcknowledgeBeginFrame(begin_frame_ack_for_pending_frame_);
287 // We may no longer need BeginFrames, since has_pending_frame_ changed.
288 UpdateNeedsBeginFramesInternal();
289 }
290
291 void CompositorFrameSinkSupport::SurfaceDiscarded(
292 const LocalSurfaceId& local_surface_id) {
293 // Don't call PendingFrameDiscarded() if an older surface was discarded and a
294 // CompositorFrame may still be pending on the new surface.
295 if (local_surface_id != surface_id_for_pending_frame_)
296 return;
297
298 PendingFrameDiscarded();
299 }
300
301 void CompositorFrameSinkSupport::PendingFrameDiscarded() {
302 if (!has_pending_frame_)
303 return;
304
305 has_pending_frame_ = false;
306 AcknowledgeLastBeginFrameWithoutUpdates();
307 UpdateNeedsBeginFramesInternal();
308 }
309
232 void CompositorFrameSinkSupport::OnBeginFrame(const BeginFrameArgs& args) { 310 void CompositorFrameSinkSupport::OnBeginFrame(const BeginFrameArgs& args) {
233 UpdateNeedsBeginFramesInternal(); 311 UpdateNeedsBeginFramesInternal();
312
313 // If we are observing BeginFrames while a pending frame exists, we are
314 // deferring acknowledging until the pending frame activates. Thus, we may not
315 // have acknowledged the last BeginFrame yet. Do that now.
316 if (has_pending_frame_)
317 AcknowledgeLastBeginFrameWithoutUpdates();
318
234 last_begin_frame_args_ = args; 319 last_begin_frame_args_ = args;
235 if (client_) 320 if (client_ && client_needs_begin_frame_) {
321 last_begin_frame_sent_to_client_ = true;
236 client_->OnBeginFrame(args); 322 client_->OnBeginFrame(args);
323 } else {
324 last_begin_frame_sent_to_client_ = false;
325 }
326 }
327
328 void CompositorFrameSinkSupport::AcknowledgeLastBeginFrameWithoutUpdates() {
329 // Not all clients wait for a BeginFrame before submitting CompositorFrames.
330 if (!last_begin_frame_args_.IsValid())
331 return;
332
333 uint64_t confirmed_sequence_number = BeginFrameArgs::kInvalidFrameNumber;
334 if (last_begin_frame_args_.source_id ==
335 latest_confirmed_begin_frame_source_id_) {
336 confirmed_sequence_number = latest_confirmed_begin_frame_sequence_number_;
337 }
338 AcknowledgeBeginFrame(BeginFrameAck(last_begin_frame_args_.source_id,
339 last_begin_frame_args_.sequence_number,
340 confirmed_sequence_number, false));
341 }
342
343 void CompositorFrameSinkSupport::AcknowledgeBeginFrame(
344 const BeginFrameAck& ack) {
345 latest_confirmed_begin_frame_source_id_ =
346 ack.latest_confirmed_sequence_number;
347 latest_confirmed_begin_frame_sequence_number_ =
348 ack.latest_confirmed_sequence_number;
349 if (begin_frame_source_)
350 begin_frame_source_->DidFinishFrame(this, ack);
237 } 351 }
238 352
239 const BeginFrameArgs& CompositorFrameSinkSupport::LastUsedBeginFrameArgs() 353 const BeginFrameArgs& CompositorFrameSinkSupport::LastUsedBeginFrameArgs()
240 const { 354 const {
241 return last_begin_frame_args_; 355 return last_begin_frame_args_;
242 } 356 }
243 357
244 void CompositorFrameSinkSupport::OnBeginFrameSourcePausedChanged(bool paused) {} 358 void CompositorFrameSinkSupport::OnBeginFrameSourcePausedChanged(bool paused) {}
245 359
246 void CompositorFrameSinkSupport::UpdateNeedsBeginFramesInternal() { 360 void CompositorFrameSinkSupport::UpdateNeedsBeginFramesInternal() {
247 if (!begin_frame_source_) 361 if (!begin_frame_source_)
248 return; 362 return;
249 363
250 if (needs_begin_frame_ == added_frame_observer_) 364 // We continue observing BeginFrames while a frame is pending to be activated,
365 // so that we can eventually forward the corresponding BeginFrameAck to the
366 // source.
367 bool needs_begin_frame = client_needs_begin_frame_ || has_pending_frame_;
368
369 if (needs_begin_frame == added_frame_observer_)
251 return; 370 return;
252 371
253 added_frame_observer_ = needs_begin_frame_; 372 added_frame_observer_ = needs_begin_frame;
254 if (needs_begin_frame_) 373 if (added_frame_observer_)
255 begin_frame_source_->AddObserver(this); 374 begin_frame_source_->AddObserver(this);
256 else 375 else
257 begin_frame_source_->RemoveObserver(this); 376 begin_frame_source_->RemoveObserver(this);
258 } 377 }
259 378
260 void CompositorFrameSinkSupport::RequestCopyOfSurface( 379 void CompositorFrameSinkSupport::RequestCopyOfSurface(
261 std::unique_ptr<CopyOutputRequest> request) { 380 std::unique_ptr<CopyOutputRequest> request) {
262 surface_factory_.RequestCopyOfSurface(std::move(request)); 381 surface_factory_.RequestCopyOfSurface(std::move(request));
263 } 382 }
264 383
265 } // namespace cc 384 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698