1 // Copyright 2012 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/layers/delegated_renderer_layer.h"
6
7 #include "cc/layers/delegated_renderer_layer_impl.h"
8 #include "cc/output/delegated_frame_data.h"
9 #include "cc/quads/render_pass_draw_quad.h"
10 #include "cc/trees/blocking_task_runner.h"
11 #include "cc/trees/layer_tree_host.h"
12
13 namespace cc {
14
Create(const scoped_refptr<DelegatedFrameProvider> & frame_provider)15 scoped_refptr<DelegatedRendererLayer> DelegatedRendererLayer::Create(
16 const scoped_refptr<DelegatedFrameProvider>& frame_provider) {
17 return scoped_refptr<DelegatedRendererLayer>(
18 new DelegatedRendererLayer(frame_provider));
19 }
20
DelegatedRendererLayer(const scoped_refptr<DelegatedFrameProvider> & frame_provider)21 DelegatedRendererLayer::DelegatedRendererLayer(
22 const scoped_refptr<DelegatedFrameProvider>& frame_provider)
23 : Layer(),
24 frame_provider_(frame_provider),
25 should_collect_new_frame_(true),
26 frame_data_(NULL),
27 main_thread_runner_(BlockingTaskRunner::current()),
28 weak_ptrs_(this) {
29 frame_provider_->AddObserver(this);
30 }
31
~DelegatedRendererLayer()32 DelegatedRendererLayer::~DelegatedRendererLayer() {
33 frame_provider_->RemoveObserver(this);
34 }
35
CreateLayerImpl(LayerTreeImpl * tree_impl)36 scoped_ptr<LayerImpl> DelegatedRendererLayer::CreateLayerImpl(
37 LayerTreeImpl* tree_impl) {
38 return DelegatedRendererLayerImpl::Create(
39 tree_impl, layer_id_).PassAs<LayerImpl>();
40 }
41
SetLayerTreeHost(LayerTreeHost * host)42 void DelegatedRendererLayer::SetLayerTreeHost(LayerTreeHost* host) {
43 if (layer_tree_host() == host) {
44 Layer::SetLayerTreeHost(host);
45 return;
46 }
47
48 if (!host) {
49 // The active frame needs to be removed from the active tree and resources
50 // returned before the commit is called complete.
51 // TODO(danakj): Don't need to do this if the last frame commited was empty
52 // or we never commited a frame with resources.
53 SetNextCommitWaitsForActivation();
54 } else {
55 // There is no active frame in the new layer tree host to wait for so no
56 // need to call SetNextCommitWaitsForActivation().
57 should_collect_new_frame_ = true;
58 SetNeedsUpdate();
59 }
60
61 Layer::SetLayerTreeHost(host);
62 }
63
PushPropertiesTo(LayerImpl * impl)64 void DelegatedRendererLayer::PushPropertiesTo(LayerImpl* impl) {
65 Layer::PushPropertiesTo(impl);
66
67 DelegatedRendererLayerImpl* delegated_impl =
68 static_cast<DelegatedRendererLayerImpl*>(impl);
69
70 delegated_impl->CreateChildIdIfNeeded(
71 frame_provider_->GetReturnResourcesCallbackForImplThread());
72
73 if (frame_data_)
74 delegated_impl->SetFrameData(frame_data_, frame_damage_);
75 frame_data_ = NULL;
76 frame_damage_ = gfx::RectF();
77 }
78
ProviderHasNewFrame()79 void DelegatedRendererLayer::ProviderHasNewFrame() {
80 should_collect_new_frame_ = true;
81 SetNeedsUpdate();
82 // The active frame needs to be replaced and resources returned before the
83 // commit is called complete.
84 SetNextCommitWaitsForActivation();
85 }
86
Update(ResourceUpdateQueue * queue,const OcclusionTracker<Layer> * occlusion)87 bool DelegatedRendererLayer::Update(ResourceUpdateQueue* queue,
88 const OcclusionTracker<Layer>* occlusion) {
89 bool updated = Layer::Update(queue, occlusion);
90 if (!should_collect_new_frame_)
91 return updated;
92
93 frame_data_ =
94 frame_provider_->GetFrameDataAndRefResources(this, &frame_damage_);
95 should_collect_new_frame_ = false;
96
97 SetNeedsPushProperties();
98 return true;
99 }
100
101 } // namespace cc
102