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.
5 #include "cc/trees/single_thread_proxy.h"
7 #include "base/auto_reset.h"
8 #include "base/debug/trace_event.h"
9 #include "cc/base/thread.h"
10 #include "cc/output/context_provider.h"
11 #include "cc/output/output_surface.h"
12 #include "cc/quads/draw_quad.h"
13 #include "cc/resources/prioritized_resource_manager.h"
14 #include "cc/resources/resource_update_controller.h"
15 #include "cc/trees/layer_tree_host.h"
16 #include "cc/trees/layer_tree_impl.h"
20 scoped_ptr
<Proxy
> SingleThreadProxy::Create(LayerTreeHost
* layer_tree_host
) {
21 return make_scoped_ptr(
22 new SingleThreadProxy(layer_tree_host
)).PassAs
<Proxy
>();
25 SingleThreadProxy::SingleThreadProxy(LayerTreeHost
* layer_tree_host
)
26 : Proxy(scoped_ptr
<Thread
>(NULL
)),
27 layer_tree_host_(layer_tree_host
),
28 output_surface_lost_(false),
29 created_offscreen_context_provider_(false),
30 renderer_initialized_(false),
31 next_frame_is_newly_committed_frame_(false),
33 TRACE_EVENT0("cc", "SingleThreadProxy::SingleThreadProxy");
34 DCHECK(Proxy::IsMainThread());
35 DCHECK(layer_tree_host
);
37 // Impl-side painting not supported without threaded compositing.
38 CHECK(!layer_tree_host
->settings().impl_side_painting
)
39 << "Threaded compositing must be enabled to use impl-side painting.";
42 void SingleThreadProxy::Start() {
43 DebugScopedSetImplThread
impl(this);
44 layer_tree_host_impl_
= layer_tree_host_
->CreateLayerTreeHostImpl(this);
47 SingleThreadProxy::~SingleThreadProxy() {
48 TRACE_EVENT0("cc", "SingleThreadProxy::~SingleThreadProxy");
49 DCHECK(Proxy::IsMainThread());
50 DCHECK(!layer_tree_host_impl_
.get() &&
51 !layer_tree_host_
); // make sure Stop() got called.
54 bool SingleThreadProxy::CompositeAndReadback(void* pixels
, gfx::Rect rect
) {
55 TRACE_EVENT0("cc", "SingleThreadProxy::CompositeAndReadback");
56 DCHECK(Proxy::IsMainThread());
58 gfx::Rect device_viewport_damage_rect
= rect
;
60 LayerTreeHostImpl::FrameData frame
;
61 if (!CommitAndComposite(base::TimeTicks::Now(),
62 device_viewport_damage_rect
,
67 DebugScopedSetImplThread
impl(this);
68 layer_tree_host_impl_
->Readback(pixels
, rect
);
70 if (layer_tree_host_impl_
->IsContextLost())
73 layer_tree_host_impl_
->SwapBuffers(frame
);
80 void SingleThreadProxy::FinishAllRendering() {
81 DCHECK(Proxy::IsMainThread());
83 DebugScopedSetImplThread
impl(this);
84 layer_tree_host_impl_
->FinishAllRendering();
88 bool SingleThreadProxy::IsStarted() const {
89 DCHECK(Proxy::IsMainThread());
90 return layer_tree_host_impl_
;
93 bool SingleThreadProxy::InitializeOutputSurface() {
94 DCHECK(Proxy::IsMainThread());
95 scoped_ptr
<OutputSurface
> output_surface
=
96 layer_tree_host_
->CreateOutputSurface();
99 output_surface_before_initialization_
= output_surface
.Pass();
103 void SingleThreadProxy::SetSurfaceReady() {
104 // Scheduling is controlled by the embedder in the single thread case, so
108 void SingleThreadProxy::SetVisible(bool visible
) {
109 DebugScopedSetImplThread
impl(this);
110 layer_tree_host_impl_
->SetVisible(visible
);
113 bool SingleThreadProxy::InitializeRenderer() {
114 DCHECK(Proxy::IsMainThread());
115 DCHECK(output_surface_before_initialization_
.get());
117 DebugScopedSetImplThread
impl(this);
118 bool ok
= layer_tree_host_impl_
->InitializeRenderer(
119 output_surface_before_initialization_
.Pass());
121 renderer_initialized_
= true;
122 renderer_capabilities_for_main_thread_
=
123 layer_tree_host_impl_
->GetRendererCapabilities();
130 bool SingleThreadProxy::RecreateOutputSurface() {
131 TRACE_EVENT0("cc", "SingleThreadProxy::RecreateContext");
132 DCHECK(Proxy::IsMainThread());
133 DCHECK(output_surface_lost_
);
135 scoped_ptr
<OutputSurface
> output_surface
=
136 layer_tree_host_
->CreateOutputSurface();
139 scoped_refptr
<cc::ContextProvider
> offscreen_context_provider
;
140 if (created_offscreen_context_provider_
) {
141 offscreen_context_provider
=
142 layer_tree_host_
->client()->OffscreenContextProviderForMainThread();
143 if (!offscreen_context_provider
)
149 DebugScopedSetMainThreadBlocked
mainThreadBlocked(this);
150 DebugScopedSetImplThread
impl(this);
151 layer_tree_host_
->DeleteContentsTexturesOnImplThread(
152 layer_tree_host_impl_
->resource_provider());
154 layer_tree_host_impl_
->InitializeRenderer(output_surface
.Pass());
156 renderer_capabilities_for_main_thread_
=
157 layer_tree_host_impl_
->GetRendererCapabilities();
158 layer_tree_host_impl_
->resource_provider()->
159 set_offscreen_context_provider(offscreen_context_provider
);
160 } else if (offscreen_context_provider
) {
161 offscreen_context_provider
->VerifyContexts();
166 output_surface_lost_
= false;
171 const RendererCapabilities
& SingleThreadProxy::GetRendererCapabilities() const {
172 DCHECK(renderer_initialized_
);
173 // Note: this gets called during the commit by the "impl" thread.
174 return renderer_capabilities_for_main_thread_
;
177 void SingleThreadProxy::SetNeedsAnimate() {
178 // Thread-only feature.
182 void SingleThreadProxy::DoCommit(scoped_ptr
<ResourceUpdateQueue
> queue
) {
183 DCHECK(Proxy::IsMainThread());
184 // Commit immediately.
186 DebugScopedSetMainThreadBlocked
mainThreadBlocked(this);
187 DebugScopedSetImplThread
impl(this);
189 RenderingStatsInstrumentation
* stats_instrumentation
=
190 layer_tree_host_
->rendering_stats_instrumentation();
191 base::TimeTicks start_time
= stats_instrumentation
->StartRecording();
193 layer_tree_host_impl_
->BeginCommit();
195 layer_tree_host_
->contents_texture_manager()->
196 PushTexturePrioritiesToBackings();
197 layer_tree_host_
->BeginCommitOnImplThread(layer_tree_host_impl_
.get());
199 scoped_ptr
<ResourceUpdateController
> update_controller
=
200 ResourceUpdateController::Create(
204 layer_tree_host_impl_
->resource_provider());
205 update_controller
->Finalize();
207 layer_tree_host_
->FinishCommitOnImplThread(layer_tree_host_impl_
.get());
209 layer_tree_host_impl_
->CommitComplete();
212 // In the single-threaded case, the scroll deltas should never be
213 // touched on the impl layer tree.
214 scoped_ptr
<ScrollAndScaleSet
> scroll_info
=
215 layer_tree_host_impl_
->ProcessScrollDeltas();
216 DCHECK(!scroll_info
->scrolls
.size());
219 base::TimeDelta duration
= stats_instrumentation
->EndRecording(start_time
);
220 stats_instrumentation
->AddCommit(duration
);
222 layer_tree_host_
->CommitComplete();
223 next_frame_is_newly_committed_frame_
= true;
226 void SingleThreadProxy::SetNeedsCommit() {
227 DCHECK(Proxy::IsMainThread());
228 layer_tree_host_
->ScheduleComposite();
231 void SingleThreadProxy::SetNeedsRedraw(gfx::Rect damage_rect
) {
232 SetNeedsRedrawRectOnImplThread(damage_rect
);
235 void SingleThreadProxy::OnHasPendingTreeStateChanged(bool have_pending_tree
) {
236 // Thread-only feature.
240 void SingleThreadProxy::SetDeferCommits(bool defer_commits
) {
241 // Thread-only feature.
245 bool SingleThreadProxy::CommitRequested() const { return false; }
247 size_t SingleThreadProxy::MaxPartialTextureUpdates() const {
248 return std::numeric_limits
<size_t>::max();
251 void SingleThreadProxy::Stop() {
252 TRACE_EVENT0("cc", "SingleThreadProxy::stop");
253 DCHECK(Proxy::IsMainThread());
255 DebugScopedSetMainThreadBlocked
mainThreadBlocked(this);
256 DebugScopedSetImplThread
impl(this);
258 layer_tree_host_
->DeleteContentsTexturesOnImplThread(
259 layer_tree_host_impl_
->resource_provider());
260 layer_tree_host_impl_
.reset();
262 layer_tree_host_
= NULL
;
265 void SingleThreadProxy::OnCanDrawStateChanged(bool can_draw
) {
266 DCHECK(Proxy::IsImplThread());
267 layer_tree_host_impl_
->UpdateBackgroundAnimateTicking(!ShouldComposite());
270 void SingleThreadProxy::SetNeedsRedrawOnImplThread() {
271 layer_tree_host_
->ScheduleComposite();
274 void SingleThreadProxy::SetNeedsRedrawRectOnImplThread(gfx::Rect damage_rect
) {
275 // FIXME: Once we move render_widget scheduling into this class, we can
276 // treat redraw requests more efficiently than CommitAndRedraw requests.
277 layer_tree_host_impl_
->SetViewportDamage(damage_rect
);
281 void SingleThreadProxy::DidInitializeVisibleTileOnImplThread() {
282 // Impl-side painting only.
286 void SingleThreadProxy::SetNeedsCommitOnImplThread() {
287 layer_tree_host_
->ScheduleComposite();
290 void SingleThreadProxy::SetNeedsManageTilesOnImplThread() {
291 layer_tree_host_
->ScheduleComposite();
294 void SingleThreadProxy::PostAnimationEventsToMainThreadOnImplThread(
295 scoped_ptr
<AnimationEventsVector
> events
,
296 base::Time wall_clock_time
) {
297 DCHECK(Proxy::IsImplThread());
298 DebugScopedSetMainThread
main(this);
299 layer_tree_host_
->SetAnimationEvents(events
.Pass(), wall_clock_time
);
302 bool SingleThreadProxy::ReduceContentsTextureMemoryOnImplThread(
304 int priority_cutoff
) {
305 DCHECK(IsImplThread());
306 if (!layer_tree_host_
->contents_texture_manager())
309 return layer_tree_host_
->contents_texture_manager()->ReduceMemoryOnImplThread(
310 limit_bytes
, priority_cutoff
, layer_tree_host_impl_
->resource_provider());
313 void SingleThreadProxy::ReduceWastedContentsTextureMemoryOnImplThread() {
314 // Impl-side painting only.
318 void SingleThreadProxy::SendManagedMemoryStats() {
319 DCHECK(Proxy::IsImplThread());
320 if (!layer_tree_host_impl_
)
322 if (!layer_tree_host_
->contents_texture_manager())
325 PrioritizedResourceManager
* contents_texture_manager
=
326 layer_tree_host_
->contents_texture_manager();
327 layer_tree_host_impl_
->SendManagedMemoryStats(
328 contents_texture_manager
->MemoryVisibleBytes(),
329 contents_texture_manager
->MemoryVisibleAndNearbyBytes(),
330 contents_texture_manager
->MemoryUseBytes());
333 bool SingleThreadProxy::IsInsideDraw() { return inside_draw_
; }
335 void SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() {
336 // Cause a commit so we can notice the lost context.
337 SetNeedsCommitOnImplThread();
340 // Called by the legacy scheduling path (e.g. where render_widget does the
342 void SingleThreadProxy::CompositeImmediately(base::TimeTicks frame_begin_time
) {
343 gfx::Rect device_viewport_damage_rect
;
345 LayerTreeHostImpl::FrameData frame
;
346 if (CommitAndComposite(frame_begin_time
,
347 device_viewport_damage_rect
,
349 layer_tree_host_impl_
->SwapBuffers(frame
);
354 scoped_ptr
<base::Value
> SingleThreadProxy::AsValue() const {
355 scoped_ptr
<base::DictionaryValue
> state(new base::DictionaryValue());
357 // The following line casts away const modifiers because it is just
358 // setting debug state. We still want the AsValue() function and its
359 // call chain to be const throughout.
360 DebugScopedSetImplThread
impl(const_cast<SingleThreadProxy
*>(this));
362 state
->Set("layer_tree_host_impl",
363 layer_tree_host_impl_
->AsValue().release());
365 return state
.PassAs
<base::Value
>();
368 void SingleThreadProxy::ForceSerializeOnSwapBuffers() {
370 DebugScopedSetImplThread
impl(this);
371 if (renderer_initialized_
)
372 layer_tree_host_impl_
->renderer()->DoNoOp();
376 bool SingleThreadProxy::CommitAndComposite(
377 base::TimeTicks frame_begin_time
,
378 gfx::Rect device_viewport_damage_rect
,
379 LayerTreeHostImpl::FrameData
* frame
) {
380 DCHECK(Proxy::IsMainThread());
382 if (!layer_tree_host_
->InitializeRendererIfNeeded())
385 scoped_refptr
<cc::ContextProvider
> offscreen_context_provider
;
386 if (renderer_capabilities_for_main_thread_
.using_offscreen_context3d
&&
387 layer_tree_host_
->needs_offscreen_context()) {
388 offscreen_context_provider
=
389 layer_tree_host_
->client()->OffscreenContextProviderForMainThread();
390 if (offscreen_context_provider
)
391 created_offscreen_context_provider_
= true;
394 layer_tree_host_
->contents_texture_manager()->UnlinkAndClearEvictedBackings();
396 scoped_ptr
<ResourceUpdateQueue
> queue
=
397 make_scoped_ptr(new ResourceUpdateQueue
);
398 layer_tree_host_
->UpdateLayers(
399 queue
.get(), layer_tree_host_impl_
->memory_allocation_limit_bytes());
401 layer_tree_host_
->WillCommit();
402 DoCommit(queue
.Pass());
403 bool result
= DoComposite(offscreen_context_provider
,
405 device_viewport_damage_rect
,
407 layer_tree_host_
->DidBeginFrame();
411 bool SingleThreadProxy::ShouldComposite() const {
412 DCHECK(Proxy::IsImplThread());
413 return layer_tree_host_impl_
->visible() &&
414 layer_tree_host_impl_
->CanDraw();
417 bool SingleThreadProxy::DoComposite(
418 scoped_refptr
<cc::ContextProvider
> offscreen_context_provider
,
419 base::TimeTicks frame_begin_time
,
420 gfx::Rect device_viewport_damage_rect
,
421 LayerTreeHostImpl::FrameData
* frame
) {
422 DCHECK(!output_surface_lost_
);
424 DebugScopedSetImplThread
impl(this);
425 base::AutoReset
<bool> mark_inside(&inside_draw_
, true);
427 layer_tree_host_impl_
->resource_provider()->
428 set_offscreen_context_provider(offscreen_context_provider
);
430 // We guard PrepareToDraw() with CanDraw() because it always returns a valid
431 // frame, so can only be used when such a frame is possible. Since
432 // DrawLayers() depends on the result of PrepareToDraw(), it is guarded on
433 // CanDraw() as well.
434 if (!ShouldComposite()) {
435 layer_tree_host_impl_
->UpdateBackgroundAnimateTicking(true);
439 layer_tree_host_impl_
->Animate(
440 layer_tree_host_impl_
->CurrentFrameTimeTicks(),
441 layer_tree_host_impl_
->CurrentFrameTime());
442 layer_tree_host_impl_
->UpdateBackgroundAnimateTicking(false);
444 layer_tree_host_impl_
->PrepareToDraw(frame
, device_viewport_damage_rect
);
445 layer_tree_host_impl_
->DrawLayers(frame
, frame_begin_time
);
446 layer_tree_host_impl_
->DidDrawAllLayers(*frame
);
447 output_surface_lost_
= layer_tree_host_impl_
->IsContextLost();
449 bool start_ready_animations
= true;
450 layer_tree_host_impl_
->UpdateAnimationState(start_ready_animations
);
452 layer_tree_host_impl_
->BeginNextFrame();
455 if (output_surface_lost_
) {
456 cc::ContextProvider
* offscreen_contexts
= layer_tree_host_impl_
->
457 resource_provider()->offscreen_context_provider();
458 if (offscreen_contexts
)
459 offscreen_contexts
->VerifyContexts();
460 layer_tree_host_
->DidLoseOutputSurface();
467 void SingleThreadProxy::DidSwapFrame() {
468 if (next_frame_is_newly_committed_frame_
) {
469 next_frame_is_newly_committed_frame_
= false;
470 layer_tree_host_
->DidCommitAndDrawFrame();
474 bool SingleThreadProxy::CommitPendingForTesting() { return false; }
476 skia::RefPtr
<SkPicture
> SingleThreadProxy::CapturePicture() {
477 // Impl-side painting only.
479 return skia::RefPtr
<SkPicture
>();