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

Side by Side Diff: cc/trees/proxy_main.cc

Issue 1417053005: cc: Split ThreadProxy into ProxyMain and ProxyImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change ThreadedChannel::GetProxyImpl to GetProxyImplForTesting Created 5 years, 1 month 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
(Empty)
1 // Copyright 2015 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.
4
5 #include "cc/trees/proxy_main.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/auto_reset.h"
11 #include "base/bind.h"
12 #include "base/trace_event/trace_event.h"
13 #include "base/trace_event/trace_event_argument.h"
14 #include "base/trace_event/trace_event_synthetic_delay.h"
15 #include "cc/debug/benchmark_instrumentation.h"
16 #include "cc/debug/devtools_instrumentation.h"
17 #include "cc/input/input_handler.h"
18 #include "cc/input/top_controls_manager.h"
19 #include "cc/output/context_provider.h"
20 #include "cc/output/output_surface.h"
21 #include "cc/output/swap_promise.h"
22 #include "cc/quads/draw_quad.h"
23 #include "cc/scheduler/commit_earlyout_reason.h"
24 #include "cc/scheduler/compositor_timing_history.h"
25 #include "cc/scheduler/scheduler.h"
26 #include "cc/trees/blocking_task_runner.h"
27 #include "cc/trees/layer_tree_host.h"
28 #include "cc/trees/layer_tree_impl.h"
29 #include "cc/trees/scoped_abort_remaining_swap_promises.h"
30 #include "gpu/command_buffer/client/gles2_interface.h"
31
32 namespace cc {
33
34 scoped_ptr<ProxyMain> ProxyMain::Create(
35 LayerTreeHost* layer_tree_host,
36 TaskRunnerProvider* task_runner_provider,
37 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
38 return make_scoped_ptr(new ProxyMain(layer_tree_host, task_runner_provider,
39 external_begin_frame_source.Pass()));
40 }
41
42 ProxyMain::ProxyMain(LayerTreeHost* layer_tree_host,
43 TaskRunnerProvider* task_runner_provider,
44 scoped_ptr<BeginFrameSource> external_begin_frame_source)
45 : layer_tree_host_(layer_tree_host),
46 task_runner_provider_(task_runner_provider),
47 layer_tree_host_id_(layer_tree_host->id()),
48 max_requested_pipeline_stage_(NO_PIPELINE_STAGE),
49 current_pipeline_stage_(NO_PIPELINE_STAGE),
50 final_pipeline_stage_(NO_PIPELINE_STAGE),
51 commit_waits_for_activation_(false),
52 started_(false),
53 prepare_tiles_pending_(false),
54 defer_commits_(false),
55 external_begin_frame_source_(external_begin_frame_source.Pass()) {
56 TRACE_EVENT0("cc", "ProxyMain::ProxyMain");
57 DCHECK(task_runner_provider_);
58 DCHECK(IsMainThread());
59 DCHECK(layer_tree_host_);
60 }
61
62 ProxyMain::~ProxyMain() {
63 TRACE_EVENT0("cc", "ProxyMain::~ProxyMain");
64 DCHECK(IsMainThread());
65 DCHECK(!started_);
66 }
67
68 void ProxyMain::SetChannel(scoped_ptr<ChannelMain> channel_main) {
69 DCHECK(!channel_main_);
70 channel_main_ = channel_main.Pass();
71 }
72
73 void ProxyMain::FinishAllRendering() {
74 DCHECK(IsMainThread());
75 DCHECK(!defer_commits_);
76
77 // Make sure all GL drawing is finished on the impl thread.
78 DebugScopedSetMainThreadBlocked main_thread_blocked(task_runner_provider_);
79 CompletionEvent completion;
80 channel_main_->FinishAllRenderingOnImpl(&completion);
81 completion.Wait();
82 }
83
84 bool ProxyMain::IsStarted() const {
85 DCHECK(IsMainThread());
86 return started_;
87 }
88
89 bool ProxyMain::CommitToActiveTree() const {
90 // With ProxyMain, we use a pending tree and activate it once it's ready to
91 // draw to allow input to modify the active tree and draw during raster.
92 return false;
93 }
94
95 void ProxyMain::SetVisible(bool visible) {
96 TRACE_EVENT1("cc", "ProxyMain::SetVisible", "visible", visible);
97 channel_main_->SetVisibleOnImpl(visible);
98 }
99
100 void ProxyMain::SetThrottleFrameProduction(bool throttle) {
101 TRACE_EVENT1("cc", "ProxyMain::SetThrottleFrameProduction", "throttle",
102 throttle);
103 channel_main_->SetThrottleFrameProductionOnImpl(throttle);
104 }
105
106 void ProxyMain::DidLoseOutputSurface() {
107 TRACE_EVENT0("cc", "ProxyMain::DidLoseOutputSurface");
108 DCHECK(IsMainThread());
109 layer_tree_host_->DidLoseOutputSurface();
110 }
111
112 void ProxyMain::RequestNewOutputSurface() {
113 DCHECK(IsMainThread());
114 layer_tree_host_->RequestNewOutputSurface();
115 }
116
117 void ProxyMain::SetOutputSurface(OutputSurface* output_surface) {
118 channel_main_->InitializeOutputSurfaceOnImpl(output_surface);
119 }
120
121 void ProxyMain::ReleaseOutputSurface() {
122 DCHECK(IsMainThread());
123 DCHECK(layer_tree_host_->output_surface_lost());
124
125 DebugScopedSetMainThreadBlocked main_thread_blocked(task_runner_provider_);
126 CompletionEvent completion;
127 channel_main_->ReleaseOutputSurfaceOnImpl(&completion);
128 completion.Wait();
129 }
130
131 void ProxyMain::DidInitializeOutputSurface(
132 bool success,
133 const RendererCapabilities& capabilities) {
134 TRACE_EVENT0("cc", "ProxyMain::DidInitializeOutputSurface");
135 DCHECK(IsMainThread());
136
137 if (!success) {
138 layer_tree_host_->DidFailToInitializeOutputSurface();
139 return;
140 }
141 renderer_capabilities_main_thread_copy_ = capabilities;
142 layer_tree_host_->DidInitializeOutputSurface();
143 }
144
145 void ProxyMain::SetRendererCapabilitiesMainCopy(
146 const RendererCapabilities& capabilities) {
147 renderer_capabilities_main_thread_copy_ = capabilities;
148 }
149
150 bool ProxyMain::SendCommitRequestToImplThreadIfNeeded(
151 CommitPipelineStage required_stage) {
152 DCHECK(IsMainThread());
153 DCHECK_NE(NO_PIPELINE_STAGE, required_stage);
154 bool already_posted = max_requested_pipeline_stage_ != NO_PIPELINE_STAGE;
155 max_requested_pipeline_stage_ =
156 std::max(max_requested_pipeline_stage_, required_stage);
157 if (already_posted)
158 return false;
159 channel_main_->SetNeedsCommitOnImpl();
160 return true;
161 }
162
163 void ProxyMain::DidCompletePageScaleAnimation() {
164 DCHECK(IsMainThread());
165 layer_tree_host_->DidCompletePageScaleAnimation();
166 }
167
168 const RendererCapabilities& ProxyMain::GetRendererCapabilities() const {
169 DCHECK(IsMainThread());
170 DCHECK(!layer_tree_host_->output_surface_lost());
171 return renderer_capabilities_main_thread_copy_;
172 }
173
174 void ProxyMain::SetNeedsAnimate() {
175 DCHECK(IsMainThread());
176 if (SendCommitRequestToImplThreadIfNeeded(ANIMATE_PIPELINE_STAGE)) {
177 TRACE_EVENT_INSTANT0("cc", "ProxyMain::SetNeedsAnimate",
178 TRACE_EVENT_SCOPE_THREAD);
179 }
180 }
181
182 void ProxyMain::SetNeedsUpdateLayers() {
183 DCHECK(IsMainThread());
184 // If we are currently animating, make sure we also update the layers.
185 if (current_pipeline_stage_ == ANIMATE_PIPELINE_STAGE) {
186 final_pipeline_stage_ =
187 std::max(final_pipeline_stage_, UPDATE_LAYERS_PIPELINE_STAGE);
188 return;
189 }
190 if (SendCommitRequestToImplThreadIfNeeded(UPDATE_LAYERS_PIPELINE_STAGE)) {
191 TRACE_EVENT_INSTANT0("cc", "ProxyMain::SetNeedsUpdateLayers",
192 TRACE_EVENT_SCOPE_THREAD);
193 }
194 }
195
196 void ProxyMain::SetNeedsCommit() {
197 DCHECK(IsMainThread());
198 // If we are currently animating, make sure we don't skip the commit. Note
199 // that requesting a commit during the layer update stage means we need to
200 // schedule another full commit.
201 if (current_pipeline_stage_ == ANIMATE_PIPELINE_STAGE) {
202 final_pipeline_stage_ =
203 std::max(final_pipeline_stage_, COMMIT_PIPELINE_STAGE);
204 return;
205 }
206 if (SendCommitRequestToImplThreadIfNeeded(COMMIT_PIPELINE_STAGE)) {
207 TRACE_EVENT_INSTANT0("cc", "ProxyMain::SetNeedsCommit",
208 TRACE_EVENT_SCOPE_THREAD);
209 }
210 }
211
212 void ProxyMain::SetNeedsRedraw(const gfx::Rect& damage_rect) {
213 TRACE_EVENT0("cc", "ProxyMain::SetNeedsRedraw");
214 DCHECK(IsMainThread());
215 channel_main_->SetNeedsRedrawOnImpl(damage_rect);
216 }
217
218 void ProxyMain::SetNextCommitWaitsForActivation() {
219 DCHECK(IsMainThread());
220 commit_waits_for_activation_ = true;
221 }
222
223 void ProxyMain::SetDeferCommits(bool defer_commits) {
224 DCHECK(IsMainThread());
225 if (defer_commits_ == defer_commits)
226 return;
227
228 defer_commits_ = defer_commits;
229 if (defer_commits_)
230 TRACE_EVENT_ASYNC_BEGIN0("cc", "ProxyMain::SetDeferCommits", this);
231 else
232 TRACE_EVENT_ASYNC_END0("cc", "ProxyMain::SetDeferCommits", this);
233
234 channel_main_->SetDeferCommitsOnImpl(defer_commits);
235 }
236
237 bool ProxyMain::CommitRequested() const {
238 DCHECK(IsMainThread());
239 // TODO(skyostil): Split this into something like CommitRequested() and
240 // CommitInProgress().
241 return current_pipeline_stage_ != NO_PIPELINE_STAGE ||
242 max_requested_pipeline_stage_ >= COMMIT_PIPELINE_STAGE;
243 }
244
245 bool ProxyMain::BeginMainFrameRequested() const {
246 DCHECK(IsMainThread());
247 return max_requested_pipeline_stage_ != NO_PIPELINE_STAGE;
248 }
249
250 void ProxyMain::MainThreadHasStoppedFlinging() {
251 DCHECK(IsMainThread());
252 channel_main_->MainThreadHasStoppedFlingingOnImpl();
253 }
254
255 void ProxyMain::NotifyInputThrottledUntilCommit() {
256 DCHECK(IsMainThread());
257 channel_main_->SetInputThrottledUntilCommitOnImpl(true);
258 }
259
260 void ProxyMain::Start() {
261 DCHECK(IsMainThread());
262 DCHECK(task_runner_provider_->HasImplThread());
263 DCHECK(channel_main_);
264
265 // Create LayerTreeHostImpl.
266 channel_main_->SynchronouslyInitializeImpl(
267 layer_tree_host_, external_begin_frame_source_.Pass());
268
269 started_ = true;
270 }
271
272 void ProxyMain::Stop() {
273 TRACE_EVENT0("cc", "ProxyMain::Stop");
274 DCHECK(IsMainThread());
275 DCHECK(started_);
276
277 channel_main_->SynchronouslyCloseImpl();
278
279 layer_tree_host_ = nullptr;
280 started_ = false;
281 }
282
283 bool ProxyMain::SupportsImplScrolling() const {
284 return true;
285 }
286
287 void ProxyMain::BeginMainFrame(
288 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) {
289 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task(
290 benchmark_instrumentation::kDoBeginFrame,
291 begin_main_frame_state->begin_frame_id);
292
293 base::TimeTicks begin_main_frame_start_time = base::TimeTicks::Now();
294
295 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame");
296 DCHECK(IsMainThread());
297 DCHECK_EQ(NO_PIPELINE_STAGE, current_pipeline_stage_);
298
299 if (defer_commits_) {
300 TRACE_EVENT_INSTANT0("cc", "EarlyOut_DeferCommit",
301 TRACE_EVENT_SCOPE_THREAD);
302 channel_main_->BeginMainFrameAbortedOnImpl(
303 CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT,
304 begin_main_frame_start_time);
305 return;
306 }
307
308 // If the commit finishes, LayerTreeHost will transfer its swap promises to
309 // LayerTreeImpl. The destructor of ScopedSwapPromiseChecker aborts the
310 // remaining swap promises.
311 ScopedAbortRemainingSwapPromises swap_promise_checker(layer_tree_host_);
312
313 final_pipeline_stage_ = max_requested_pipeline_stage_;
314 max_requested_pipeline_stage_ = NO_PIPELINE_STAGE;
315
316 if (!layer_tree_host_->visible()) {
317 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD);
318 channel_main_->BeginMainFrameAbortedOnImpl(
319 CommitEarlyOutReason::ABORTED_NOT_VISIBLE, begin_main_frame_start_time);
320 return;
321 }
322
323 if (layer_tree_host_->output_surface_lost()) {
324 TRACE_EVENT_INSTANT0("cc", "EarlyOut_OutputSurfaceLost",
325 TRACE_EVENT_SCOPE_THREAD);
326 channel_main_->BeginMainFrameAbortedOnImpl(
327 CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST,
328 begin_main_frame_start_time);
329 return;
330 }
331
332 current_pipeline_stage_ = ANIMATE_PIPELINE_STAGE;
333
334 layer_tree_host_->ApplyScrollAndScale(
335 begin_main_frame_state->scroll_info.get());
336
337 layer_tree_host_->WillBeginMainFrame();
338
339 layer_tree_host_->BeginMainFrame(begin_main_frame_state->begin_frame_args);
340 layer_tree_host_->AnimateLayers(
341 begin_main_frame_state->begin_frame_args.frame_time);
342
343 // Recreate all UI resources if there were evicted UI resources when the impl
344 // thread initiated the commit.
345 if (begin_main_frame_state->evicted_ui_resources)
346 layer_tree_host_->RecreateUIResources();
347
348 layer_tree_host_->RequestMainFrameUpdate();
349 TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame");
350
351 bool can_cancel_this_commit = final_pipeline_stage_ < COMMIT_PIPELINE_STAGE &&
352 !begin_main_frame_state->evicted_ui_resources;
353
354 current_pipeline_stage_ = UPDATE_LAYERS_PIPELINE_STAGE;
355 bool should_update_layers =
356 final_pipeline_stage_ >= UPDATE_LAYERS_PIPELINE_STAGE;
357 bool updated = should_update_layers && layer_tree_host_->UpdateLayers();
358
359 layer_tree_host_->WillCommit();
360 devtools_instrumentation::ScopedCommitTrace commit_task(
361 layer_tree_host_->id());
362
363 current_pipeline_stage_ = COMMIT_PIPELINE_STAGE;
364 if (!updated && can_cancel_this_commit) {
365 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD);
366 channel_main_->BeginMainFrameAbortedOnImpl(
367 CommitEarlyOutReason::FINISHED_NO_UPDATES, begin_main_frame_start_time);
368
369 // Although the commit is internally aborted, this is because it has been
370 // detected to be a no-op. From the perspective of an embedder, this commit
371 // went through, and input should no longer be throttled, etc.
372 current_pipeline_stage_ = NO_PIPELINE_STAGE;
373 layer_tree_host_->CommitComplete();
374 layer_tree_host_->DidBeginMainFrame();
375 layer_tree_host_->BreakSwapPromises(SwapPromise::COMMIT_NO_UPDATE);
376 return;
377 }
378
379 // Notify the impl thread that the main thread is ready to commit. This will
380 // begin the commit process, which is blocking from the main thread's
381 // point of view, but asynchronously performed on the impl thread,
382 // coordinated by the Scheduler.
383 {
384 TRACE_EVENT0("cc", "ProxyMain::BeginMainFrame::commit");
385
386 DebugScopedSetMainThreadBlocked main_thread_blocked(task_runner_provider_);
387
388 // This CapturePostTasks should be destroyed before CommitComplete() is
389 // called since that goes out to the embedder, and we want the embedder
390 // to receive its callbacks before that.
391 BlockingTaskRunner::CapturePostTasks blocked(
392 task_runner_provider_->blocking_main_thread_task_runner());
393
394 bool hold_commit_for_activation = commit_waits_for_activation_;
395 commit_waits_for_activation_ = false;
396 CompletionEvent completion;
397 channel_main_->StartCommitOnImpl(&completion, layer_tree_host_,
398 begin_main_frame_start_time,
399 hold_commit_for_activation);
400 completion.Wait();
401 }
402
403 current_pipeline_stage_ = NO_PIPELINE_STAGE;
404 layer_tree_host_->CommitComplete();
405 layer_tree_host_->DidBeginMainFrame();
406 }
407
408 void ProxyMain::BeginMainFrameNotExpectedSoon() {
409 TRACE_EVENT0("cc", "ProxyMain::BeginMainFrameNotExpectedSoon");
410 DCHECK(IsMainThread());
411 layer_tree_host_->BeginMainFrameNotExpectedSoon();
412 }
413
414 void ProxyMain::SetAuthoritativeVSyncInterval(const base::TimeDelta& interval) {
415 NOTREACHED() << "Only used by SingleProxyMain";
416 }
417
418 void ProxyMain::DidCommitAndDrawFrame() {
419 DCHECK(IsMainThread());
420 layer_tree_host_->DidCommitAndDrawFrame();
421 }
422
423 void ProxyMain::DidCompleteSwapBuffers() {
424 DCHECK(IsMainThread());
425 layer_tree_host_->DidCompleteSwapBuffers();
426 }
427
428 void ProxyMain::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) {
429 TRACE_EVENT0("cc", "ProxyMain::SetAnimationEvents");
430 DCHECK(IsMainThread());
431 layer_tree_host_->SetAnimationEvents(events.Pass());
432 }
433
434 bool ProxyMain::MainFrameWillHappenForTesting() {
435 DCHECK(IsMainThread());
436 bool main_frame_will_happen = false;
437 {
438 DebugScopedSetMainThreadBlocked main_thread_blocked(task_runner_provider_);
439 CompletionEvent completion;
440 channel_main_->MainFrameWillHappenOnImplForTesting(&completion,
441 &main_frame_will_happen);
442 completion.Wait();
443 }
444 return main_frame_will_happen;
445 }
446
447 void ProxyMain::SetChildrenNeedBeginFrames(bool children_need_begin_frames) {
448 NOTREACHED() << "Only used by SingleThreadProxy";
449 }
450
451 void ProxyMain::UpdateTopControlsState(TopControlsState constraints,
452 TopControlsState current,
453 bool animate) {
454 channel_main_->UpdateTopControlsStateOnImpl(constraints, current, animate);
455 }
456
457 void ProxyMain::PostFrameTimingEventsOnMain(
458 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
459 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) {
460 DCHECK(IsMainThread());
461 layer_tree_host_->RecordFrameTimingEvents(composite_events.Pass(),
462 main_frame_events.Pass());
463 }
464
465 bool ProxyMain::IsMainThread() const {
466 return task_runner_provider_->IsMainThread();
467 }
468
469 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698