OLD | NEW |
| (Empty) |
1 // Copyright 2011 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/thread_proxy.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 namespace { | |
35 | |
36 // Measured in seconds. | |
37 const double kSmoothnessTakesPriorityExpirationDelay = 0.25; | |
38 | |
39 unsigned int nextBeginFrameId = 0; | |
40 | |
41 } // namespace | |
42 | |
43 struct ThreadProxy::SchedulerStateRequest { | |
44 CompletionEvent completion; | |
45 scoped_ptr<base::Value> state; | |
46 }; | |
47 | |
48 scoped_ptr<Proxy> ThreadProxy::Create( | |
49 LayerTreeHost* layer_tree_host, | |
50 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
51 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner, | |
52 scoped_ptr<BeginFrameSource> external_begin_frame_source) { | |
53 return make_scoped_ptr(new ThreadProxy(layer_tree_host, | |
54 main_task_runner, | |
55 impl_task_runner, | |
56 external_begin_frame_source.Pass())); | |
57 } | |
58 | |
59 ThreadProxy::ThreadProxy( | |
60 LayerTreeHost* layer_tree_host, | |
61 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
62 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner, | |
63 scoped_ptr<BeginFrameSource> external_begin_frame_source) | |
64 : Proxy(main_task_runner, impl_task_runner), | |
65 main_thread_only_vars_unsafe_(this, layer_tree_host), | |
66 compositor_thread_vars_unsafe_( | |
67 this, | |
68 layer_tree_host->id(), | |
69 layer_tree_host->rendering_stats_instrumentation(), | |
70 external_begin_frame_source.Pass()) { | |
71 TRACE_EVENT0("cc", "ThreadProxy::ThreadProxy"); | |
72 DCHECK(IsMainThread()); | |
73 DCHECK(this->main().layer_tree_host); | |
74 // TODO(khushalsagar): Move this to LayerTreeHost#InitializeThreaded once | |
75 // ThreadProxy is split. LayerTreeHost creates the channel and passes it to | |
76 // ProxyMain#SetChannel. | |
77 SetChannel(ThreadedChannel::Create(this, main_task_runner, impl_task_runner)); | |
78 } | |
79 | |
80 ThreadProxy::MainThreadOnly::MainThreadOnly(ThreadProxy* proxy, | |
81 LayerTreeHost* layer_tree_host) | |
82 : layer_tree_host_id(layer_tree_host->id()), | |
83 layer_tree_host(layer_tree_host), | |
84 max_requested_pipeline_stage(NO_PIPELINE_STAGE), | |
85 current_pipeline_stage(NO_PIPELINE_STAGE), | |
86 final_pipeline_stage(NO_PIPELINE_STAGE), | |
87 commit_waits_for_activation(false), | |
88 started(false), | |
89 prepare_tiles_pending(false), | |
90 defer_commits(false), | |
91 weak_factory(proxy) {} | |
92 | |
93 ThreadProxy::MainThreadOnly::~MainThreadOnly() {} | |
94 | |
95 ThreadProxy::BlockedMainCommitOnly::BlockedMainCommitOnly() | |
96 : layer_tree_host(nullptr) {} | |
97 | |
98 ThreadProxy::BlockedMainCommitOnly::~BlockedMainCommitOnly() {} | |
99 | |
100 ThreadProxy::CompositorThreadOnly::CompositorThreadOnly( | |
101 ThreadProxy* proxy, | |
102 int layer_tree_host_id, | |
103 RenderingStatsInstrumentation* rendering_stats_instrumentation, | |
104 scoped_ptr<BeginFrameSource> external_begin_frame_source) | |
105 : layer_tree_host_id(layer_tree_host_id), | |
106 next_commit_waits_for_activation(false), | |
107 commit_completion_event(nullptr), | |
108 next_frame_is_newly_committed_frame(false), | |
109 inside_draw(false), | |
110 input_throttled_until_commit(false), | |
111 smoothness_priority_expiration_notifier( | |
112 proxy->ImplThreadTaskRunner(), | |
113 base::Bind(&ThreadProxy::RenewTreePriority, base::Unretained(proxy)), | |
114 base::TimeDelta::FromMilliseconds( | |
115 kSmoothnessTakesPriorityExpirationDelay * 1000)), | |
116 external_begin_frame_source(external_begin_frame_source.Pass()), | |
117 rendering_stats_instrumentation(rendering_stats_instrumentation), | |
118 weak_factory(proxy) {} | |
119 | |
120 ThreadProxy::CompositorThreadOnly::~CompositorThreadOnly() {} | |
121 | |
122 ThreadProxy::~ThreadProxy() { | |
123 TRACE_EVENT0("cc", "ThreadProxy::~ThreadProxy"); | |
124 DCHECK(IsMainThread()); | |
125 DCHECK(!main().started); | |
126 } | |
127 | |
128 void ThreadProxy::SetChannel(scoped_ptr<ThreadedChannel> threaded_channel) { | |
129 threaded_channel_ = threaded_channel.Pass(); | |
130 main().channel_main = threaded_channel_.get(); | |
131 } | |
132 | |
133 void ThreadProxy::FinishAllRendering() { | |
134 DCHECK(Proxy::IsMainThread()); | |
135 DCHECK(!main().defer_commits); | |
136 | |
137 // Make sure all GL drawing is finished on the impl thread. | |
138 DebugScopedSetMainThreadBlocked main_thread_blocked(this); | |
139 CompletionEvent completion; | |
140 main().channel_main->FinishAllRenderingOnImpl(&completion); | |
141 completion.Wait(); | |
142 } | |
143 | |
144 bool ThreadProxy::IsStarted() const { | |
145 DCHECK(Proxy::IsMainThread()); | |
146 return main().started; | |
147 } | |
148 | |
149 bool ThreadProxy::CommitToActiveTree() const { | |
150 // With ThreadProxy, we use a pending tree and activate it once it's ready to | |
151 // draw to allow input to modify the active tree and draw during raster. | |
152 return false; | |
153 } | |
154 | |
155 void ThreadProxy::SetVisible(bool visible) { | |
156 TRACE_EVENT1("cc", "ThreadProxy::SetVisible", "visible", visible); | |
157 main().channel_main->SetVisibleOnImpl(visible); | |
158 } | |
159 | |
160 void ThreadProxy::SetVisibleOnImpl(bool visible) { | |
161 TRACE_EVENT1("cc", "ThreadProxy::SetVisibleOnImplThread", "visible", visible); | |
162 impl().layer_tree_host_impl->SetVisible(visible); | |
163 impl().scheduler->SetVisible(visible); | |
164 } | |
165 | |
166 void ThreadProxy::SetThrottleFrameProduction(bool throttle) { | |
167 TRACE_EVENT1("cc", "ThreadProxy::SetThrottleFrameProduction", "throttle", | |
168 throttle); | |
169 main().channel_main->SetThrottleFrameProductionOnImpl(throttle); | |
170 } | |
171 | |
172 void ThreadProxy::SetThrottleFrameProductionOnImpl(bool throttle) { | |
173 TRACE_EVENT1("cc", "ThreadProxy::SetThrottleFrameProductionOnImplThread", | |
174 "throttle", throttle); | |
175 impl().scheduler->SetThrottleFrameProduction(throttle); | |
176 } | |
177 | |
178 void ThreadProxy::DidLoseOutputSurface() { | |
179 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurface"); | |
180 DCHECK(IsMainThread()); | |
181 main().layer_tree_host->DidLoseOutputSurface(); | |
182 } | |
183 | |
184 void ThreadProxy::RequestNewOutputSurface() { | |
185 DCHECK(IsMainThread()); | |
186 main().layer_tree_host->RequestNewOutputSurface(); | |
187 } | |
188 | |
189 void ThreadProxy::SetOutputSurface(OutputSurface* output_surface) { | |
190 main().channel_main->InitializeOutputSurfaceOnImpl(output_surface); | |
191 } | |
192 | |
193 void ThreadProxy::ReleaseOutputSurface() { | |
194 DCHECK(IsMainThread()); | |
195 DCHECK(main().layer_tree_host->output_surface_lost()); | |
196 | |
197 DebugScopedSetMainThreadBlocked main_thread_blocked(this); | |
198 CompletionEvent completion; | |
199 main().channel_main->ReleaseOutputSurfaceOnImpl(&completion); | |
200 completion.Wait(); | |
201 } | |
202 | |
203 void ThreadProxy::DidInitializeOutputSurface( | |
204 bool success, | |
205 const RendererCapabilities& capabilities) { | |
206 TRACE_EVENT0("cc", "ThreadProxy::DidInitializeOutputSurface"); | |
207 DCHECK(IsMainThread()); | |
208 | |
209 if (!success) { | |
210 main().layer_tree_host->DidFailToInitializeOutputSurface(); | |
211 return; | |
212 } | |
213 main().renderer_capabilities_main_thread_copy = capabilities; | |
214 main().layer_tree_host->DidInitializeOutputSurface(); | |
215 } | |
216 | |
217 void ThreadProxy::SetRendererCapabilitiesMainCopy( | |
218 const RendererCapabilities& capabilities) { | |
219 main().renderer_capabilities_main_thread_copy = capabilities; | |
220 } | |
221 | |
222 bool ThreadProxy::SendCommitRequestToImplThreadIfNeeded( | |
223 CommitPipelineStage required_stage) { | |
224 DCHECK(IsMainThread()); | |
225 DCHECK_NE(NO_PIPELINE_STAGE, required_stage); | |
226 bool already_posted = | |
227 main().max_requested_pipeline_stage != NO_PIPELINE_STAGE; | |
228 main().max_requested_pipeline_stage = | |
229 std::max(main().max_requested_pipeline_stage, required_stage); | |
230 if (already_posted) | |
231 return false; | |
232 main().channel_main->SetNeedsCommitOnImpl(); | |
233 return true; | |
234 } | |
235 | |
236 void ThreadProxy::SetNeedsCommitOnImpl() { | |
237 SetNeedsCommitOnImplThread(); | |
238 } | |
239 | |
240 void ThreadProxy::DidCompletePageScaleAnimation() { | |
241 DCHECK(IsMainThread()); | |
242 main().layer_tree_host->DidCompletePageScaleAnimation(); | |
243 } | |
244 | |
245 const RendererCapabilities& ThreadProxy::GetRendererCapabilities() const { | |
246 DCHECK(IsMainThread()); | |
247 DCHECK(!main().layer_tree_host->output_surface_lost()); | |
248 return main().renderer_capabilities_main_thread_copy; | |
249 } | |
250 | |
251 void ThreadProxy::SetNeedsAnimate() { | |
252 DCHECK(IsMainThread()); | |
253 if (SendCommitRequestToImplThreadIfNeeded(ANIMATE_PIPELINE_STAGE)) { | |
254 TRACE_EVENT_INSTANT0("cc", "ThreadProxy::SetNeedsAnimate", | |
255 TRACE_EVENT_SCOPE_THREAD); | |
256 } | |
257 } | |
258 | |
259 void ThreadProxy::SetNeedsUpdateLayers() { | |
260 DCHECK(IsMainThread()); | |
261 // If we are currently animating, make sure we also update the layers. | |
262 if (main().current_pipeline_stage == ANIMATE_PIPELINE_STAGE) { | |
263 main().final_pipeline_stage = | |
264 std::max(main().final_pipeline_stage, UPDATE_LAYERS_PIPELINE_STAGE); | |
265 return; | |
266 } | |
267 if (SendCommitRequestToImplThreadIfNeeded(UPDATE_LAYERS_PIPELINE_STAGE)) { | |
268 TRACE_EVENT_INSTANT0("cc", "ThreadProxy::SetNeedsUpdateLayers", | |
269 TRACE_EVENT_SCOPE_THREAD); | |
270 } | |
271 } | |
272 | |
273 void ThreadProxy::SetNeedsCommit() { | |
274 DCHECK(IsMainThread()); | |
275 // If we are currently animating, make sure we don't skip the commit. Note | |
276 // that requesting a commit during the layer update stage means we need to | |
277 // schedule another full commit. | |
278 if (main().current_pipeline_stage == ANIMATE_PIPELINE_STAGE) { | |
279 main().final_pipeline_stage = | |
280 std::max(main().final_pipeline_stage, COMMIT_PIPELINE_STAGE); | |
281 return; | |
282 } | |
283 if (SendCommitRequestToImplThreadIfNeeded(COMMIT_PIPELINE_STAGE)) { | |
284 TRACE_EVENT_INSTANT0("cc", "ThreadProxy::SetNeedsCommit", | |
285 TRACE_EVENT_SCOPE_THREAD); | |
286 } | |
287 } | |
288 | |
289 void ThreadProxy::UpdateRendererCapabilitiesOnImplThread() { | |
290 DCHECK(IsImplThread()); | |
291 impl().channel_impl->SetRendererCapabilitiesMainCopy( | |
292 impl() | |
293 .layer_tree_host_impl->GetRendererCapabilities() | |
294 .MainThreadCapabilities()); | |
295 } | |
296 | |
297 void ThreadProxy::DidLoseOutputSurfaceOnImplThread() { | |
298 TRACE_EVENT0("cc", "ThreadProxy::DidLoseOutputSurfaceOnImplThread"); | |
299 DCHECK(IsImplThread()); | |
300 impl().channel_impl->DidLoseOutputSurface(); | |
301 impl().scheduler->DidLoseOutputSurface(); | |
302 } | |
303 | |
304 void ThreadProxy::CommitVSyncParameters(base::TimeTicks timebase, | |
305 base::TimeDelta interval) { | |
306 impl().scheduler->CommitVSyncParameters(timebase, interval); | |
307 } | |
308 | |
309 void ThreadProxy::SetEstimatedParentDrawTime(base::TimeDelta draw_time) { | |
310 impl().scheduler->SetEstimatedParentDrawTime(draw_time); | |
311 } | |
312 | |
313 void ThreadProxy::SetMaxSwapsPendingOnImplThread(int max) { | |
314 impl().scheduler->SetMaxSwapsPending(max); | |
315 } | |
316 | |
317 void ThreadProxy::DidSwapBuffersOnImplThread() { | |
318 impl().scheduler->DidSwapBuffers(); | |
319 } | |
320 | |
321 void ThreadProxy::DidSwapBuffersCompleteOnImplThread() { | |
322 TRACE_EVENT0("cc,benchmark", | |
323 "ThreadProxy::DidSwapBuffersCompleteOnImplThread"); | |
324 DCHECK(IsImplThread()); | |
325 impl().scheduler->DidSwapBuffersComplete(); | |
326 impl().channel_impl->DidCompleteSwapBuffers(); | |
327 } | |
328 | |
329 void ThreadProxy::WillBeginImplFrame(const BeginFrameArgs& args) { | |
330 impl().layer_tree_host_impl->WillBeginImplFrame(args); | |
331 if (impl().last_processed_begin_main_frame_args.IsValid()) { | |
332 // Last processed begin main frame args records the frame args that we sent | |
333 // to the main thread for the last frame that we've processed. If that is | |
334 // set, that means the current frame is one past the frame in which we've | |
335 // finished the processing. | |
336 impl().layer_tree_host_impl->RecordMainFrameTiming( | |
337 impl().last_processed_begin_main_frame_args, | |
338 impl().layer_tree_host_impl->CurrentBeginFrameArgs()); | |
339 impl().last_processed_begin_main_frame_args = BeginFrameArgs(); | |
340 } | |
341 } | |
342 | |
343 void ThreadProxy::OnResourcelessSoftareDrawStateChanged( | |
344 bool resourceless_draw) { | |
345 DCHECK(IsImplThread()); | |
346 impl().scheduler->SetResourcelessSoftareDraw(resourceless_draw); | |
347 } | |
348 | |
349 void ThreadProxy::OnCanDrawStateChanged(bool can_draw) { | |
350 TRACE_EVENT1( | |
351 "cc", "ThreadProxy::OnCanDrawStateChanged", "can_draw", can_draw); | |
352 DCHECK(IsImplThread()); | |
353 impl().scheduler->SetCanDraw(can_draw); | |
354 } | |
355 | |
356 void ThreadProxy::NotifyReadyToActivate() { | |
357 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToActivate"); | |
358 impl().scheduler->NotifyReadyToActivate(); | |
359 } | |
360 | |
361 void ThreadProxy::NotifyReadyToDraw() { | |
362 TRACE_EVENT0("cc", "ThreadProxy::NotifyReadyToDraw"); | |
363 impl().scheduler->NotifyReadyToDraw(); | |
364 } | |
365 | |
366 void ThreadProxy::SetNeedsCommitOnImplThread() { | |
367 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsCommitOnImplThread"); | |
368 DCHECK(IsImplThread()); | |
369 impl().scheduler->SetNeedsBeginMainFrame(); | |
370 } | |
371 | |
372 void ThreadProxy::SetVideoNeedsBeginFrames(bool needs_begin_frames) { | |
373 TRACE_EVENT1("cc", "ThreadProxy::SetVideoNeedsBeginFrames", | |
374 "needs_begin_frames", needs_begin_frames); | |
375 DCHECK(IsImplThread()); | |
376 // In tests the layer tree is destroyed after the scheduler is. | |
377 if (impl().scheduler) | |
378 impl().scheduler->SetVideoNeedsBeginFrames(needs_begin_frames); | |
379 } | |
380 | |
381 void ThreadProxy::PostAnimationEventsToMainThreadOnImplThread( | |
382 scoped_ptr<AnimationEventsVector> events) { | |
383 TRACE_EVENT0("cc", | |
384 "ThreadProxy::PostAnimationEventsToMainThreadOnImplThread"); | |
385 DCHECK(IsImplThread()); | |
386 impl().channel_impl->SetAnimationEvents(events.Pass()); | |
387 } | |
388 | |
389 bool ThreadProxy::IsInsideDraw() { return impl().inside_draw; } | |
390 | |
391 void ThreadProxy::SetNeedsRedraw(const gfx::Rect& damage_rect) { | |
392 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedraw"); | |
393 DCHECK(IsMainThread()); | |
394 main().channel_main->SetNeedsRedrawOnImpl(damage_rect); | |
395 } | |
396 | |
397 void ThreadProxy::SetNeedsRedrawOnImpl(const gfx::Rect& damage_rect) { | |
398 DCHECK(IsImplThread()); | |
399 SetNeedsRedrawRectOnImplThread(damage_rect); | |
400 } | |
401 | |
402 void ThreadProxy::SetNextCommitWaitsForActivation() { | |
403 DCHECK(IsMainThread()); | |
404 main().commit_waits_for_activation = true; | |
405 } | |
406 | |
407 void ThreadProxy::SetDeferCommits(bool defer_commits) { | |
408 DCHECK(IsMainThread()); | |
409 if (main().defer_commits == defer_commits) | |
410 return; | |
411 | |
412 main().defer_commits = defer_commits; | |
413 if (main().defer_commits) | |
414 TRACE_EVENT_ASYNC_BEGIN0("cc", "ThreadProxy::SetDeferCommits", this); | |
415 else | |
416 TRACE_EVENT_ASYNC_END0("cc", "ThreadProxy::SetDeferCommits", this); | |
417 | |
418 main().channel_main->SetDeferCommitsOnImpl(defer_commits); | |
419 } | |
420 | |
421 void ThreadProxy::SetDeferCommitsOnImpl(bool defer_commits) const { | |
422 DCHECK(IsImplThread()); | |
423 impl().scheduler->SetDeferCommits(defer_commits); | |
424 } | |
425 | |
426 bool ThreadProxy::CommitRequested() const { | |
427 DCHECK(IsMainThread()); | |
428 // TODO(skyostil): Split this into something like CommitRequested() and | |
429 // CommitInProgress(). | |
430 return main().current_pipeline_stage != NO_PIPELINE_STAGE || | |
431 main().max_requested_pipeline_stage >= COMMIT_PIPELINE_STAGE; | |
432 } | |
433 | |
434 bool ThreadProxy::BeginMainFrameRequested() const { | |
435 DCHECK(IsMainThread()); | |
436 return main().max_requested_pipeline_stage != NO_PIPELINE_STAGE; | |
437 } | |
438 | |
439 void ThreadProxy::SetNeedsRedrawOnImplThread() { | |
440 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsRedrawOnImplThread"); | |
441 DCHECK(IsImplThread()); | |
442 impl().scheduler->SetNeedsRedraw(); | |
443 } | |
444 | |
445 void ThreadProxy::SetNeedsAnimateOnImplThread() { | |
446 TRACE_EVENT0("cc", "ThreadProxy::SetNeedsAnimateOnImplThread"); | |
447 DCHECK(IsImplThread()); | |
448 impl().scheduler->SetNeedsAnimate(); | |
449 } | |
450 | |
451 void ThreadProxy::SetNeedsPrepareTilesOnImplThread() { | |
452 DCHECK(IsImplThread()); | |
453 impl().scheduler->SetNeedsPrepareTiles(); | |
454 } | |
455 | |
456 void ThreadProxy::SetNeedsRedrawRectOnImplThread(const gfx::Rect& damage_rect) { | |
457 DCHECK(IsImplThread()); | |
458 impl().layer_tree_host_impl->SetViewportDamage(damage_rect); | |
459 SetNeedsRedrawOnImplThread(); | |
460 } | |
461 | |
462 void ThreadProxy::MainThreadHasStoppedFlinging() { | |
463 DCHECK(IsMainThread()); | |
464 main().channel_main->MainThreadHasStoppedFlingingOnImpl(); | |
465 } | |
466 | |
467 void ThreadProxy::MainThreadHasStoppedFlingingOnImpl() { | |
468 DCHECK(IsImplThread()); | |
469 impl().layer_tree_host_impl->MainThreadHasStoppedFlinging(); | |
470 } | |
471 | |
472 void ThreadProxy::NotifyInputThrottledUntilCommit() { | |
473 DCHECK(IsMainThread()); | |
474 main().channel_main->SetInputThrottledUntilCommitOnImpl(true); | |
475 } | |
476 | |
477 void ThreadProxy::SetInputThrottledUntilCommitOnImpl(bool is_throttled) { | |
478 DCHECK(IsImplThread()); | |
479 if (is_throttled == impl().input_throttled_until_commit) | |
480 return; | |
481 impl().input_throttled_until_commit = is_throttled; | |
482 RenewTreePriority(); | |
483 } | |
484 | |
485 ThreadProxy::MainThreadOnly& ThreadProxy::main() { | |
486 DCHECK(IsMainThread()); | |
487 return main_thread_only_vars_unsafe_; | |
488 } | |
489 const ThreadProxy::MainThreadOnly& ThreadProxy::main() const { | |
490 DCHECK(IsMainThread()); | |
491 return main_thread_only_vars_unsafe_; | |
492 } | |
493 | |
494 ThreadProxy::BlockedMainCommitOnly& ThreadProxy::blocked_main_commit() { | |
495 DCHECK(IsMainThreadBlocked()); | |
496 DCHECK(impl().commit_completion_event); | |
497 return main_thread_blocked_commit_vars_unsafe_; | |
498 } | |
499 | |
500 ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() { | |
501 DCHECK(IsImplThread()); | |
502 return compositor_thread_vars_unsafe_; | |
503 } | |
504 | |
505 const ThreadProxy::CompositorThreadOnly& ThreadProxy::impl() const { | |
506 DCHECK(IsImplThread()); | |
507 return compositor_thread_vars_unsafe_; | |
508 } | |
509 | |
510 void ThreadProxy::Start() { | |
511 DCHECK(IsMainThread()); | |
512 DCHECK(Proxy::HasImplThread()); | |
513 | |
514 // Create LayerTreeHostImpl. | |
515 DebugScopedSetMainThreadBlocked main_thread_blocked(this); | |
516 CompletionEvent completion; | |
517 main().channel_main->InitializeImplOnImpl(&completion, | |
518 main().layer_tree_host); | |
519 completion.Wait(); | |
520 | |
521 main_thread_weak_ptr_ = main().weak_factory.GetWeakPtr(); | |
522 | |
523 main().started = true; | |
524 } | |
525 | |
526 void ThreadProxy::Stop() { | |
527 TRACE_EVENT0("cc", "ThreadProxy::Stop"); | |
528 DCHECK(IsMainThread()); | |
529 DCHECK(main().started); | |
530 | |
531 // Synchronously finishes pending GL operations and deletes the impl. | |
532 // The two steps are done as separate post tasks, so that tasks posted | |
533 // by the GL implementation due to the Finish can be executed by the | |
534 // renderer before shutting it down. | |
535 { | |
536 DebugScopedSetMainThreadBlocked main_thread_blocked(this); | |
537 CompletionEvent completion; | |
538 main().channel_main->FinishGLOnImpl(&completion); | |
539 completion.Wait(); | |
540 } | |
541 { | |
542 DebugScopedSetMainThreadBlocked main_thread_blocked(this); | |
543 | |
544 CompletionEvent completion; | |
545 main().channel_main->LayerTreeHostClosedOnImpl(&completion); | |
546 completion.Wait(); | |
547 } | |
548 | |
549 main().weak_factory.InvalidateWeakPtrs(); | |
550 main().layer_tree_host = nullptr; | |
551 main().started = false; | |
552 } | |
553 | |
554 bool ThreadProxy::SupportsImplScrolling() const { | |
555 return true; | |
556 } | |
557 | |
558 void ThreadProxy::FinishAllRenderingOnImpl(CompletionEvent* completion) { | |
559 TRACE_EVENT0("cc", "ThreadProxy::FinishAllRenderingOnImplThread"); | |
560 DCHECK(IsImplThread()); | |
561 impl().layer_tree_host_impl->FinishAllRendering(); | |
562 completion->Signal(); | |
563 } | |
564 | |
565 void ThreadProxy::ScheduledActionSendBeginMainFrame() { | |
566 unsigned int begin_frame_id = nextBeginFrameId++; | |
567 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task( | |
568 benchmark_instrumentation::kSendBeginFrame, begin_frame_id); | |
569 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state( | |
570 new BeginMainFrameAndCommitState); | |
571 begin_main_frame_state->begin_frame_id = begin_frame_id; | |
572 begin_main_frame_state->begin_frame_args = | |
573 impl().layer_tree_host_impl->CurrentBeginFrameArgs(); | |
574 begin_main_frame_state->scroll_info = | |
575 impl().layer_tree_host_impl->ProcessScrollDeltas(); | |
576 begin_main_frame_state->memory_allocation_limit_bytes = | |
577 impl().layer_tree_host_impl->memory_allocation_limit_bytes(); | |
578 begin_main_frame_state->evicted_ui_resources = | |
579 impl().layer_tree_host_impl->EvictedUIResourcesExist(); | |
580 // TODO(vmpstr): This needs to be fixed if | |
581 // main_frame_before_activation_enabled is set, since we might run this code | |
582 // twice before recording a duration. crbug.com/469824 | |
583 impl().last_begin_main_frame_args = begin_main_frame_state->begin_frame_args; | |
584 impl().channel_impl->BeginMainFrame(begin_main_frame_state.Pass()); | |
585 devtools_instrumentation::DidRequestMainThreadFrame( | |
586 impl().layer_tree_host_id); | |
587 } | |
588 | |
589 void ThreadProxy::SendBeginMainFrameNotExpectedSoon() { | |
590 impl().channel_impl->BeginMainFrameNotExpectedSoon(); | |
591 } | |
592 | |
593 void ThreadProxy::BeginMainFrame( | |
594 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state) { | |
595 benchmark_instrumentation::ScopedBeginFrameTask begin_frame_task( | |
596 benchmark_instrumentation::kDoBeginFrame, | |
597 begin_main_frame_state->begin_frame_id); | |
598 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("cc.BeginMainFrame"); | |
599 DCHECK(IsMainThread()); | |
600 DCHECK_EQ(NO_PIPELINE_STAGE, main().current_pipeline_stage); | |
601 | |
602 if (main().defer_commits) { | |
603 TRACE_EVENT_INSTANT0("cc", "EarlyOut_DeferCommit", | |
604 TRACE_EVENT_SCOPE_THREAD); | |
605 main().channel_main->BeginMainFrameAbortedOnImpl( | |
606 CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT); | |
607 return; | |
608 } | |
609 | |
610 // If the commit finishes, LayerTreeHost will transfer its swap promises to | |
611 // LayerTreeImpl. The destructor of ScopedSwapPromiseChecker aborts the | |
612 // remaining swap promises. | |
613 ScopedAbortRemainingSwapPromises swap_promise_checker(main().layer_tree_host); | |
614 | |
615 main().final_pipeline_stage = main().max_requested_pipeline_stage; | |
616 main().max_requested_pipeline_stage = NO_PIPELINE_STAGE; | |
617 | |
618 if (!main().layer_tree_host->visible()) { | |
619 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NotVisible", TRACE_EVENT_SCOPE_THREAD); | |
620 main().channel_main->BeginMainFrameAbortedOnImpl( | |
621 CommitEarlyOutReason::ABORTED_NOT_VISIBLE); | |
622 return; | |
623 } | |
624 | |
625 if (main().layer_tree_host->output_surface_lost()) { | |
626 TRACE_EVENT_INSTANT0( | |
627 "cc", "EarlyOut_OutputSurfaceLost", TRACE_EVENT_SCOPE_THREAD); | |
628 main().channel_main->BeginMainFrameAbortedOnImpl( | |
629 CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST); | |
630 return; | |
631 } | |
632 | |
633 main().current_pipeline_stage = ANIMATE_PIPELINE_STAGE; | |
634 | |
635 main().layer_tree_host->ApplyScrollAndScale( | |
636 begin_main_frame_state->scroll_info.get()); | |
637 | |
638 main().layer_tree_host->WillBeginMainFrame(); | |
639 | |
640 main().layer_tree_host->BeginMainFrame( | |
641 begin_main_frame_state->begin_frame_args); | |
642 main().layer_tree_host->AnimateLayers( | |
643 begin_main_frame_state->begin_frame_args.frame_time); | |
644 | |
645 // Recreate all UI resources if there were evicted UI resources when the impl | |
646 // thread initiated the commit. | |
647 if (begin_main_frame_state->evicted_ui_resources) | |
648 main().layer_tree_host->RecreateUIResources(); | |
649 | |
650 main().layer_tree_host->RequestMainFrameUpdate(); | |
651 TRACE_EVENT_SYNTHETIC_DELAY_END("cc.BeginMainFrame"); | |
652 | |
653 bool can_cancel_this_commit = | |
654 main().final_pipeline_stage < COMMIT_PIPELINE_STAGE && | |
655 !begin_main_frame_state->evicted_ui_resources; | |
656 | |
657 main().current_pipeline_stage = UPDATE_LAYERS_PIPELINE_STAGE; | |
658 bool should_update_layers = | |
659 main().final_pipeline_stage >= UPDATE_LAYERS_PIPELINE_STAGE; | |
660 bool updated = should_update_layers && main().layer_tree_host->UpdateLayers(); | |
661 | |
662 main().layer_tree_host->WillCommit(); | |
663 devtools_instrumentation::ScopedCommitTrace commit_task( | |
664 main().layer_tree_host->id()); | |
665 | |
666 main().current_pipeline_stage = COMMIT_PIPELINE_STAGE; | |
667 if (!updated && can_cancel_this_commit) { | |
668 TRACE_EVENT_INSTANT0("cc", "EarlyOut_NoUpdates", TRACE_EVENT_SCOPE_THREAD); | |
669 main().channel_main->BeginMainFrameAbortedOnImpl( | |
670 CommitEarlyOutReason::FINISHED_NO_UPDATES); | |
671 | |
672 // Although the commit is internally aborted, this is because it has been | |
673 // detected to be a no-op. From the perspective of an embedder, this commit | |
674 // went through, and input should no longer be throttled, etc. | |
675 main().current_pipeline_stage = NO_PIPELINE_STAGE; | |
676 main().layer_tree_host->CommitComplete(); | |
677 main().layer_tree_host->DidBeginMainFrame(); | |
678 main().layer_tree_host->BreakSwapPromises(SwapPromise::COMMIT_NO_UPDATE); | |
679 return; | |
680 } | |
681 | |
682 // Notify the impl thread that the main thread is ready to commit. This will | |
683 // begin the commit process, which is blocking from the main thread's | |
684 // point of view, but asynchronously performed on the impl thread, | |
685 // coordinated by the Scheduler. | |
686 { | |
687 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrame::commit"); | |
688 | |
689 DebugScopedSetMainThreadBlocked main_thread_blocked(this); | |
690 | |
691 // This CapturePostTasks should be destroyed before CommitComplete() is | |
692 // called since that goes out to the embedder, and we want the embedder | |
693 // to receive its callbacks before that. | |
694 BlockingTaskRunner::CapturePostTasks blocked( | |
695 blocking_main_thread_task_runner()); | |
696 | |
697 CompletionEvent completion; | |
698 main().channel_main->StartCommitOnImpl(&completion, main().layer_tree_host, | |
699 main().commit_waits_for_activation); | |
700 completion.Wait(); | |
701 main().commit_waits_for_activation = false; | |
702 } | |
703 | |
704 main().current_pipeline_stage = NO_PIPELINE_STAGE; | |
705 main().layer_tree_host->CommitComplete(); | |
706 main().layer_tree_host->DidBeginMainFrame(); | |
707 } | |
708 | |
709 void ThreadProxy::BeginMainFrameNotExpectedSoon() { | |
710 TRACE_EVENT0("cc", "ThreadProxy::BeginMainFrameNotExpectedSoon"); | |
711 DCHECK(IsMainThread()); | |
712 main().layer_tree_host->BeginMainFrameNotExpectedSoon(); | |
713 } | |
714 | |
715 void ThreadProxy::StartCommitOnImpl(CompletionEvent* completion, | |
716 LayerTreeHost* layer_tree_host, | |
717 bool hold_commit_for_activation) { | |
718 TRACE_EVENT0("cc", "ThreadProxy::StartCommitOnImplThread"); | |
719 DCHECK(!impl().commit_completion_event); | |
720 DCHECK(IsImplThread() && IsMainThreadBlocked()); | |
721 DCHECK(impl().scheduler); | |
722 DCHECK(impl().scheduler->CommitPending()); | |
723 | |
724 if (hold_commit_for_activation) { | |
725 // This commit may be aborted. Store the value for | |
726 // hold_commit_for_activation so that whenever the next commit is started, | |
727 // the main thread will be unblocked only after pending tree activation. | |
728 impl().next_commit_waits_for_activation = hold_commit_for_activation; | |
729 } | |
730 | |
731 if (!impl().layer_tree_host_impl) { | |
732 TRACE_EVENT_INSTANT0( | |
733 "cc", "EarlyOut_NoLayerTree", TRACE_EVENT_SCOPE_THREAD); | |
734 completion->Signal(); | |
735 return; | |
736 } | |
737 | |
738 // Ideally, we should inform to impl thread when BeginMainFrame is started. | |
739 // But, we can avoid a PostTask in here. | |
740 impl().scheduler->NotifyBeginMainFrameStarted(); | |
741 impl().commit_completion_event = completion; | |
742 DCHECK(!blocked_main_commit().layer_tree_host); | |
743 blocked_main_commit().layer_tree_host = layer_tree_host; | |
744 impl().scheduler->NotifyReadyToCommit(); | |
745 } | |
746 | |
747 void ThreadProxy::BeginMainFrameAbortedOnImpl(CommitEarlyOutReason reason) { | |
748 TRACE_EVENT1("cc", "ThreadProxy::BeginMainFrameAbortedOnImplThread", "reason", | |
749 CommitEarlyOutReasonToString(reason)); | |
750 DCHECK(IsImplThread()); | |
751 DCHECK(impl().scheduler); | |
752 DCHECK(impl().scheduler->CommitPending()); | |
753 DCHECK(!impl().layer_tree_host_impl->pending_tree()); | |
754 | |
755 if (CommitEarlyOutHandledCommit(reason)) { | |
756 SetInputThrottledUntilCommitOnImpl(false); | |
757 impl().last_processed_begin_main_frame_args = | |
758 impl().last_begin_main_frame_args; | |
759 } | |
760 impl().layer_tree_host_impl->BeginMainFrameAborted(reason); | |
761 impl().scheduler->BeginMainFrameAborted(reason); | |
762 } | |
763 | |
764 void ThreadProxy::ScheduledActionAnimate() { | |
765 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionAnimate"); | |
766 DCHECK(IsImplThread()); | |
767 | |
768 impl().layer_tree_host_impl->Animate(); | |
769 } | |
770 | |
771 void ThreadProxy::ScheduledActionCommit() { | |
772 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionCommit"); | |
773 DCHECK(IsImplThread()); | |
774 DCHECK(IsMainThreadBlocked()); | |
775 DCHECK(impl().commit_completion_event); | |
776 DCHECK(blocked_main_commit().layer_tree_host); | |
777 | |
778 impl().layer_tree_host_impl->BeginCommit(); | |
779 blocked_main_commit().layer_tree_host->FinishCommitOnImplThread( | |
780 impl().layer_tree_host_impl.get()); | |
781 | |
782 // Remove the LayerTreeHost reference before the completion event is signaled | |
783 // and cleared. This is necessary since blocked_main_commit() allows access | |
784 // only while we have the completion event to ensure the main thread is | |
785 // blocked for a commit. | |
786 blocked_main_commit().layer_tree_host = nullptr; | |
787 | |
788 if (impl().next_commit_waits_for_activation) { | |
789 // For some layer types in impl-side painting, the commit is held until | |
790 // the sync tree is activated. It's also possible that the | |
791 // sync tree has already activated if there was no work to be done. | |
792 TRACE_EVENT_INSTANT0("cc", "HoldCommit", TRACE_EVENT_SCOPE_THREAD); | |
793 } else { | |
794 impl().commit_completion_event->Signal(); | |
795 impl().commit_completion_event = nullptr; | |
796 } | |
797 | |
798 impl().scheduler->DidCommit(); | |
799 | |
800 // Delay this step until afer the main thread has been released as it's | |
801 // often a good bit of work to update the tree and prepare the new frame. | |
802 impl().layer_tree_host_impl->CommitComplete(); | |
803 | |
804 SetInputThrottledUntilCommitOnImpl(false); | |
805 | |
806 impl().next_frame_is_newly_committed_frame = true; | |
807 } | |
808 | |
809 void ThreadProxy::ScheduledActionActivateSyncTree() { | |
810 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionActivateSyncTree"); | |
811 DCHECK(IsImplThread()); | |
812 impl().layer_tree_host_impl->ActivateSyncTree(); | |
813 } | |
814 | |
815 void ThreadProxy::ScheduledActionBeginOutputSurfaceCreation() { | |
816 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionBeginOutputSurfaceCreation"); | |
817 DCHECK(IsImplThread()); | |
818 impl().channel_impl->RequestNewOutputSurface(); | |
819 } | |
820 | |
821 DrawResult ThreadProxy::DrawSwapInternal(bool forced_draw) { | |
822 TRACE_EVENT_SYNTHETIC_DELAY("cc.DrawAndSwap"); | |
823 DrawResult result; | |
824 | |
825 DCHECK(IsImplThread()); | |
826 DCHECK(impl().layer_tree_host_impl.get()); | |
827 | |
828 base::AutoReset<bool> mark_inside(&impl().inside_draw, true); | |
829 | |
830 if (impl().layer_tree_host_impl->pending_tree()) { | |
831 bool update_lcd_text = false; | |
832 impl().layer_tree_host_impl->pending_tree()->UpdateDrawProperties( | |
833 update_lcd_text); | |
834 } | |
835 | |
836 // This method is called on a forced draw, regardless of whether we are able | |
837 // to produce a frame, as the calling site on main thread is blocked until its | |
838 // request completes, and we signal completion here. If CanDraw() is false, we | |
839 // will indicate success=false to the caller, but we must still signal | |
840 // completion to avoid deadlock. | |
841 | |
842 // We guard PrepareToDraw() with CanDraw() because it always returns a valid | |
843 // frame, so can only be used when such a frame is possible. Since | |
844 // DrawLayers() depends on the result of PrepareToDraw(), it is guarded on | |
845 // CanDraw() as well. | |
846 | |
847 LayerTreeHostImpl::FrameData frame; | |
848 bool draw_frame = false; | |
849 | |
850 if (impl().layer_tree_host_impl->CanDraw()) { | |
851 result = impl().layer_tree_host_impl->PrepareToDraw(&frame); | |
852 draw_frame = forced_draw || result == DRAW_SUCCESS; | |
853 } else { | |
854 result = DRAW_ABORTED_CANT_DRAW; | |
855 } | |
856 | |
857 if (draw_frame) { | |
858 impl().layer_tree_host_impl->DrawLayers(&frame); | |
859 result = DRAW_SUCCESS; | |
860 } else { | |
861 DCHECK_NE(DRAW_SUCCESS, result); | |
862 } | |
863 impl().layer_tree_host_impl->DidDrawAllLayers(frame); | |
864 | |
865 bool start_ready_animations = draw_frame; | |
866 impl().layer_tree_host_impl->UpdateAnimationState(start_ready_animations); | |
867 | |
868 if (draw_frame) | |
869 impl().layer_tree_host_impl->SwapBuffers(frame); | |
870 | |
871 // Tell the main thread that the the newly-commited frame was drawn. | |
872 if (impl().next_frame_is_newly_committed_frame) { | |
873 impl().next_frame_is_newly_committed_frame = false; | |
874 impl().channel_impl->DidCommitAndDrawFrame(); | |
875 } | |
876 | |
877 DCHECK_NE(INVALID_RESULT, result); | |
878 return result; | |
879 } | |
880 | |
881 void ThreadProxy::ScheduledActionPrepareTiles() { | |
882 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionPrepareTiles"); | |
883 impl().layer_tree_host_impl->PrepareTiles(); | |
884 } | |
885 | |
886 DrawResult ThreadProxy::ScheduledActionDrawAndSwapIfPossible() { | |
887 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwap"); | |
888 | |
889 // SchedulerStateMachine::DidDrawIfPossibleCompleted isn't set up to | |
890 // handle DRAW_ABORTED_CANT_DRAW. Moreover, the scheduler should | |
891 // never generate this call when it can't draw. | |
892 DCHECK(impl().layer_tree_host_impl->CanDraw()); | |
893 | |
894 bool forced_draw = false; | |
895 return DrawSwapInternal(forced_draw); | |
896 } | |
897 | |
898 DrawResult ThreadProxy::ScheduledActionDrawAndSwapForced() { | |
899 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionDrawAndSwapForced"); | |
900 bool forced_draw = true; | |
901 return DrawSwapInternal(forced_draw); | |
902 } | |
903 | |
904 void ThreadProxy::ScheduledActionInvalidateOutputSurface() { | |
905 TRACE_EVENT0("cc", "ThreadProxy::ScheduledActionInvalidateOutputSurface"); | |
906 DCHECK(impl().layer_tree_host_impl->output_surface()); | |
907 impl().layer_tree_host_impl->output_surface()->Invalidate(); | |
908 } | |
909 | |
910 void ThreadProxy::DidFinishImplFrame() { | |
911 impl().layer_tree_host_impl->DidFinishImplFrame(); | |
912 } | |
913 | |
914 void ThreadProxy::SendBeginFramesToChildren(const BeginFrameArgs& args) { | |
915 NOTREACHED() << "Only used by SingleThreadProxy"; | |
916 } | |
917 | |
918 void ThreadProxy::SetAuthoritativeVSyncInterval( | |
919 const base::TimeDelta& interval) { | |
920 NOTREACHED() << "Only used by SingleThreadProxy"; | |
921 } | |
922 | |
923 void ThreadProxy::DidCommitAndDrawFrame() { | |
924 DCHECK(IsMainThread()); | |
925 main().layer_tree_host->DidCommitAndDrawFrame(); | |
926 } | |
927 | |
928 void ThreadProxy::DidCompleteSwapBuffers() { | |
929 DCHECK(IsMainThread()); | |
930 main().layer_tree_host->DidCompleteSwapBuffers(); | |
931 } | |
932 | |
933 void ThreadProxy::SetAnimationEvents(scoped_ptr<AnimationEventsVector> events) { | |
934 TRACE_EVENT0("cc", "ThreadProxy::SetAnimationEvents"); | |
935 DCHECK(IsMainThread()); | |
936 main().layer_tree_host->SetAnimationEvents(events.Pass()); | |
937 } | |
938 | |
939 void ThreadProxy::InitializeImplOnImpl(CompletionEvent* completion, | |
940 LayerTreeHost* layer_tree_host) { | |
941 TRACE_EVENT0("cc", "ThreadProxy::InitializeImplOnImplThread"); | |
942 DCHECK(IsImplThread()); | |
943 DCHECK(IsMainThreadBlocked()); | |
944 DCHECK(layer_tree_host); | |
945 | |
946 // TODO(khushalsagar): ThreadedChannel will create ProxyImpl here and pass a | |
947 // reference to itself. | |
948 impl().channel_impl = threaded_channel_.get(); | |
949 | |
950 impl().layer_tree_host_impl = layer_tree_host->CreateLayerTreeHostImpl(this); | |
951 | |
952 SchedulerSettings scheduler_settings( | |
953 layer_tree_host->settings().ToSchedulerSettings()); | |
954 | |
955 scoped_ptr<CompositorTimingHistory> compositor_timing_history( | |
956 new CompositorTimingHistory(CompositorTimingHistory::RENDERER_UMA, | |
957 impl().rendering_stats_instrumentation)); | |
958 | |
959 impl().scheduler = Scheduler::Create( | |
960 this, scheduler_settings, impl().layer_tree_host_id, | |
961 ImplThreadTaskRunner(), impl().external_begin_frame_source.get(), | |
962 compositor_timing_history.Pass()); | |
963 | |
964 DCHECK_EQ(impl().scheduler->visible(), | |
965 impl().layer_tree_host_impl->visible()); | |
966 impl_thread_weak_ptr_ = impl().weak_factory.GetWeakPtr(); | |
967 completion->Signal(); | |
968 } | |
969 | |
970 void ThreadProxy::InitializeOutputSurfaceOnImpl(OutputSurface* output_surface) { | |
971 TRACE_EVENT0("cc", "ThreadProxy::InitializeOutputSurfaceOnImplThread"); | |
972 DCHECK(IsImplThread()); | |
973 | |
974 LayerTreeHostImpl* host_impl = impl().layer_tree_host_impl.get(); | |
975 bool success = host_impl->InitializeRenderer(output_surface); | |
976 RendererCapabilities capabilities; | |
977 if (success) { | |
978 capabilities = | |
979 host_impl->GetRendererCapabilities().MainThreadCapabilities(); | |
980 } | |
981 | |
982 impl().channel_impl->DidInitializeOutputSurface(success, capabilities); | |
983 | |
984 if (success) | |
985 impl().scheduler->DidCreateAndInitializeOutputSurface(); | |
986 } | |
987 | |
988 void ThreadProxy::ReleaseOutputSurfaceOnImpl(CompletionEvent* completion) { | |
989 DCHECK(IsImplThread()); | |
990 | |
991 // Unlike DidLoseOutputSurfaceOnImplThread, we don't need to call | |
992 // LayerTreeHost::DidLoseOutputSurface since it already knows. | |
993 impl().scheduler->DidLoseOutputSurface(); | |
994 impl().layer_tree_host_impl->ReleaseOutputSurface(); | |
995 completion->Signal(); | |
996 } | |
997 | |
998 void ThreadProxy::FinishGLOnImpl(CompletionEvent* completion) { | |
999 TRACE_EVENT0("cc", "ThreadProxy::FinishGLOnImplThread"); | |
1000 DCHECK(IsImplThread()); | |
1001 if (impl().layer_tree_host_impl->output_surface()) { | |
1002 ContextProvider* context_provider = | |
1003 impl().layer_tree_host_impl->output_surface()->context_provider(); | |
1004 if (context_provider) | |
1005 context_provider->ContextGL()->Finish(); | |
1006 } | |
1007 completion->Signal(); | |
1008 } | |
1009 | |
1010 void ThreadProxy::LayerTreeHostClosedOnImpl(CompletionEvent* completion) { | |
1011 TRACE_EVENT0("cc", "ThreadProxy::LayerTreeHostClosedOnImplThread"); | |
1012 DCHECK(IsImplThread()); | |
1013 DCHECK(IsMainThreadBlocked()); | |
1014 impl().scheduler = nullptr; | |
1015 impl().external_begin_frame_source = nullptr; | |
1016 impl().layer_tree_host_impl = nullptr; | |
1017 impl().weak_factory.InvalidateWeakPtrs(); | |
1018 // We need to explicitly shutdown the notifier to destroy any weakptrs it is | |
1019 // holding while still on the compositor thread. This also ensures any | |
1020 // callbacks holding a ThreadProxy pointer are cancelled. | |
1021 impl().smoothness_priority_expiration_notifier.Shutdown(); | |
1022 completion->Signal(); | |
1023 } | |
1024 | |
1025 bool ThreadProxy::MainFrameWillHappenForTesting() { | |
1026 DCHECK(IsMainThread()); | |
1027 bool main_frame_will_happen = false; | |
1028 { | |
1029 DebugScopedSetMainThreadBlocked main_thread_blocked(this); | |
1030 CompletionEvent completion; | |
1031 main().channel_main->MainFrameWillHappenOnImplForTesting( | |
1032 &completion, &main_frame_will_happen); | |
1033 completion.Wait(); | |
1034 } | |
1035 return main_frame_will_happen; | |
1036 } | |
1037 | |
1038 void ThreadProxy::SetChildrenNeedBeginFrames(bool children_need_begin_frames) { | |
1039 NOTREACHED() << "Only used by SingleThreadProxy"; | |
1040 } | |
1041 | |
1042 void ThreadProxy::MainFrameWillHappenOnImplForTesting( | |
1043 CompletionEvent* completion, | |
1044 bool* main_frame_will_happen) { | |
1045 DCHECK(IsImplThread()); | |
1046 if (impl().layer_tree_host_impl->output_surface()) { | |
1047 *main_frame_will_happen = impl().scheduler->MainFrameForTestingWillHappen(); | |
1048 } else { | |
1049 *main_frame_will_happen = false; | |
1050 } | |
1051 completion->Signal(); | |
1052 } | |
1053 | |
1054 void ThreadProxy::RenewTreePriority() { | |
1055 DCHECK(IsImplThread()); | |
1056 bool smoothness_takes_priority = | |
1057 impl().layer_tree_host_impl->pinch_gesture_active() || | |
1058 impl().layer_tree_host_impl->page_scale_animation_active() || | |
1059 impl().layer_tree_host_impl->IsActivelyScrolling(); | |
1060 | |
1061 // Schedule expiration if smoothness currently takes priority. | |
1062 if (smoothness_takes_priority) | |
1063 impl().smoothness_priority_expiration_notifier.Schedule(); | |
1064 | |
1065 // We use the same priority for both trees by default. | |
1066 TreePriority priority = SAME_PRIORITY_FOR_BOTH_TREES; | |
1067 | |
1068 // Smoothness takes priority if we have an expiration for it scheduled. | |
1069 if (impl().smoothness_priority_expiration_notifier.HasPendingNotification()) | |
1070 priority = SMOOTHNESS_TAKES_PRIORITY; | |
1071 | |
1072 // New content always takes priority when there is an invalid viewport size or | |
1073 // ui resources have been evicted. | |
1074 if (impl().layer_tree_host_impl->active_tree()->ViewportSizeInvalid() || | |
1075 impl().layer_tree_host_impl->EvictedUIResourcesExist() || | |
1076 impl().input_throttled_until_commit) { | |
1077 // Once we enter NEW_CONTENTS_TAKES_PRIORITY mode, visible tiles on active | |
1078 // tree might be freed. We need to set RequiresHighResToDraw to ensure that | |
1079 // high res tiles will be required to activate pending tree. | |
1080 impl().layer_tree_host_impl->SetRequiresHighResToDraw(); | |
1081 priority = NEW_CONTENT_TAKES_PRIORITY; | |
1082 } | |
1083 | |
1084 impl().layer_tree_host_impl->SetTreePriority(priority); | |
1085 | |
1086 // Only put the scheduler in impl latency prioritization mode if we don't | |
1087 // have a scroll listener. This gives the scroll listener a better chance of | |
1088 // handling scroll updates within the same frame. The tree itself is still | |
1089 // kept in prefer smoothness mode to allow checkerboarding. | |
1090 impl().scheduler->SetImplLatencyTakesPriority( | |
1091 priority == SMOOTHNESS_TAKES_PRIORITY && | |
1092 !impl().layer_tree_host_impl->scroll_affects_scroll_handler()); | |
1093 | |
1094 // Notify the the client of this compositor via the output surface. | |
1095 // TODO(epenner): Route this to compositor-thread instead of output-surface | |
1096 // after GTFO refactor of compositor-thread (http://crbug/170828). | |
1097 if (impl().layer_tree_host_impl->output_surface()) { | |
1098 impl() | |
1099 .layer_tree_host_impl->output_surface() | |
1100 ->UpdateSmoothnessTakesPriority(priority == SMOOTHNESS_TAKES_PRIORITY); | |
1101 } | |
1102 } | |
1103 | |
1104 void ThreadProxy::PostDelayedAnimationTaskOnImplThread( | |
1105 const base::Closure& task, | |
1106 base::TimeDelta delay) { | |
1107 Proxy::ImplThreadTaskRunner()->PostDelayedTask(FROM_HERE, task, delay); | |
1108 } | |
1109 | |
1110 void ThreadProxy::DidActivateSyncTree() { | |
1111 TRACE_EVENT0("cc", "ThreadProxy::DidActivateSyncTreeOnImplThread"); | |
1112 DCHECK(IsImplThread()); | |
1113 | |
1114 if (impl().next_commit_waits_for_activation) { | |
1115 TRACE_EVENT_INSTANT0( | |
1116 "cc", "ReleaseCommitbyActivation", TRACE_EVENT_SCOPE_THREAD); | |
1117 DCHECK(impl().commit_completion_event); | |
1118 impl().commit_completion_event->Signal(); | |
1119 impl().commit_completion_event = nullptr; | |
1120 impl().next_commit_waits_for_activation = false; | |
1121 } | |
1122 | |
1123 impl().last_processed_begin_main_frame_args = | |
1124 impl().last_begin_main_frame_args; | |
1125 } | |
1126 | |
1127 void ThreadProxy::WillPrepareTiles() { | |
1128 DCHECK(IsImplThread()); | |
1129 impl().scheduler->WillPrepareTiles(); | |
1130 } | |
1131 | |
1132 void ThreadProxy::DidPrepareTiles() { | |
1133 DCHECK(IsImplThread()); | |
1134 impl().scheduler->DidPrepareTiles(); | |
1135 } | |
1136 | |
1137 void ThreadProxy::DidCompletePageScaleAnimationOnImplThread() { | |
1138 DCHECK(IsImplThread()); | |
1139 impl().channel_impl->DidCompletePageScaleAnimation(); | |
1140 } | |
1141 | |
1142 void ThreadProxy::OnDrawForOutputSurface() { | |
1143 DCHECK(IsImplThread()); | |
1144 impl().scheduler->OnDrawForOutputSurface(); | |
1145 } | |
1146 | |
1147 void ThreadProxy::UpdateTopControlsState(TopControlsState constraints, | |
1148 TopControlsState current, | |
1149 bool animate) { | |
1150 main().channel_main->UpdateTopControlsStateOnImpl(constraints, current, | |
1151 animate); | |
1152 } | |
1153 | |
1154 void ThreadProxy::UpdateTopControlsStateOnImpl(TopControlsState constraints, | |
1155 TopControlsState current, | |
1156 bool animate) { | |
1157 DCHECK(IsImplThread()); | |
1158 impl().layer_tree_host_impl->top_controls_manager()->UpdateTopControlsState( | |
1159 constraints, current, animate); | |
1160 } | |
1161 | |
1162 void ThreadProxy::PostFrameTimingEventsOnImplThread( | |
1163 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, | |
1164 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) { | |
1165 DCHECK(IsImplThread()); | |
1166 impl().channel_impl->PostFrameTimingEventsOnMain(composite_events.Pass(), | |
1167 main_frame_events.Pass()); | |
1168 } | |
1169 | |
1170 void ThreadProxy::PostFrameTimingEventsOnMain( | |
1171 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events, | |
1172 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events) { | |
1173 DCHECK(IsMainThread()); | |
1174 main().layer_tree_host->RecordFrameTimingEvents(composite_events.Pass(), | |
1175 main_frame_events.Pass()); | |
1176 } | |
1177 | |
1178 base::WeakPtr<ProxyMain> ThreadProxy::GetMainWeakPtr() { | |
1179 return main_thread_weak_ptr_; | |
1180 } | |
1181 | |
1182 base::WeakPtr<ProxyImpl> ThreadProxy::GetImplWeakPtr() { | |
1183 return impl_thread_weak_ptr_; | |
1184 } | |
1185 | |
1186 } // namespace cc | |
OLD | NEW |