summaryrefslogtreecommitdiffstats
path: root/cc/layer_tree_impl.cc
blob: d94d38f40aacfca39c2534b64cc55da23db5c6b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/layer_tree_impl.h"

#include "base/debug/trace_event.h"
#include "cc/layer_tree_host_common.h"
#include "cc/layer_tree_host_impl.h"
#include "ui/gfx/vector2d_conversions.h"

namespace cc {

LayerTreeImpl::LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl)
    : layer_tree_host_impl_(layer_tree_host_impl)
    , source_frame_number_(-1)
    , hud_layer_(0)
    , root_scroll_layer_(0)
    , currently_scrolling_layer_(0)
    , background_color_(0)
    , has_transparent_background_(false)
    , scrolling_layer_id_from_previous_tree_(0) {
}

LayerTreeImpl::~LayerTreeImpl() {
  // Need to explicitly clear the tree prior to destroying this so that
  // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
  root_layer_.reset();
}

static LayerImpl* findRootScrollLayer(LayerImpl* layer)
{
    if (!layer)
        return 0;

    if (layer->scrollable())
        return layer;

    for (size_t i = 0; i < layer->children().size(); ++i) {
        LayerImpl* found = findRootScrollLayer(layer->children()[i]);
        if (found)
            return found;
    }

    return 0;
}

void LayerTreeImpl::SetRootLayer(scoped_ptr<LayerImpl> layer) {
  root_layer_ = layer.Pass();
  root_scroll_layer_ = NULL;
  currently_scrolling_layer_ = NULL;

  layer_tree_host_impl_->OnCanDrawStateChangedForTree(this);
}

void LayerTreeImpl::FindRootScrollLayer() {
  root_scroll_layer_ = findRootScrollLayer(root_layer_.get());

  if (root_layer_ && scrolling_layer_id_from_previous_tree_) {
    currently_scrolling_layer_ = LayerTreeHostCommon::findLayerInSubtree(
        root_layer_.get(),
        scrolling_layer_id_from_previous_tree_);
  }

  scrolling_layer_id_from_previous_tree_ = 0;
}

scoped_ptr<LayerImpl> LayerTreeImpl::DetachLayerTree() {
  // Clear all data structures that have direct references to the layer tree.
  scrolling_layer_id_from_previous_tree_ =
    currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0;
  currently_scrolling_layer_ = NULL;

  render_surface_layer_list_.clear();
  SetNeedsUpdateDrawProperties();
  return root_layer_.Pass();
}

void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
  currently_scrolling_layer_ = NULL;
  scrolling_layer_id_from_previous_tree_ = 0;
}

void LayerTreeImpl::UpdateMaxScrollOffset() {
  if (!root_scroll_layer() || !root_scroll_layer()->children().size())
    return;

  gfx::SizeF view_bounds = device_viewport_size();
  if (LayerImpl* clip_layer = root_scroll_layer()->parent()) {
    // Compensate for non-overlay scrollbars.
    if (clip_layer->masksToBounds())
      view_bounds = gfx::ScaleSize(clip_layer->bounds(), device_scale_factor());
  }

  gfx::Size content_bounds = ContentSize();
  if (settings().pageScalePinchZoomEnabled) {
    // Pinch with pageScale scrolls entirely in layout space.  ContentSize
    // returns the bounds including the page scale factor, so calculate the
    // pre page-scale layout size here.
    float page_scale_factor = pinch_zoom_viewport().page_scale_factor();
    content_bounds.set_width(content_bounds.width() / page_scale_factor);
    content_bounds.set_height(content_bounds.height() / page_scale_factor);
  } else {
    view_bounds.Scale(1 / pinch_zoom_viewport().page_scale_delta());
  }

  gfx::Vector2dF max_scroll = gfx::Rect(content_bounds).bottom_right() -
      gfx::RectF(view_bounds).bottom_right();
  max_scroll.Scale(1 / device_scale_factor());

  // The viewport may be larger than the contents in some cases, such as
  // having a vertical scrollbar but no horizontal overflow.
  max_scroll.ClampToMin(gfx::Vector2dF());

  root_scroll_layer()->setMaxScrollOffset(gfx::ToFlooredVector2d(max_scroll));
}

void LayerTreeImpl::UpdateDrawProperties() {
  render_surface_layer_list_.clear();
  if (!RootLayer())
    return;

  if (root_scroll_layer()) {
    root_scroll_layer()->setImplTransform(
        layer_tree_host_impl_->implTransform());
  }

  {
    TRACE_EVENT0("cc", "LayerTreeImpl::UpdateDrawProperties");
    LayerTreeHostCommon::calculateDrawProperties(
        RootLayer(),
        device_viewport_size(),
        device_scale_factor(),
        pinch_zoom_viewport().page_scale_factor(),
        MaxTextureSize(),
        settings().canUseLCDText,
        render_surface_layer_list_);
  }
}

static void ClearRenderSurfacesOnLayerImplRecursive(LayerImpl* current)
{
    DCHECK(current);
    for (size_t i = 0; i < current->children().size(); ++i)
        ClearRenderSurfacesOnLayerImplRecursive(current->children()[i]);
    current->clearRenderSurface();
}

void LayerTreeImpl::ClearRenderSurfaces() {
  ClearRenderSurfacesOnLayerImplRecursive(RootLayer());
  render_surface_layer_list_.clear();
  SetNeedsUpdateDrawProperties();
}

const LayerTreeImpl::LayerList& LayerTreeImpl::RenderSurfaceLayerList() const {
  // If this assert triggers, then the list is dirty.
  DCHECK(!layer_tree_host_impl_->needsUpdateDrawProperties());
  return render_surface_layer_list_;
}

gfx::Size LayerTreeImpl::ContentSize() const {
  // TODO(aelias): Hardcoding the first child here is weird. Think of
  // a cleaner way to get the contentBounds on the Impl side.
  if (!root_scroll_layer() || root_scroll_layer()->children().empty())
    return gfx::Size();
  return root_scroll_layer()->children()[0]->contentBounds();
}

LayerImpl* LayerTreeImpl::LayerById(int id) {
  LayerIdMap::iterator iter = layer_id_map_.find(id);
  return iter != layer_id_map_.end() ? iter->second : NULL;
}

void LayerTreeImpl::RegisterLayer(LayerImpl* layer) {
  DCHECK(!LayerById(layer->id()));
  layer_id_map_[layer->id()] = layer;
}

void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) {
  DCHECK(LayerById(layer->id()));
  layer_id_map_.erase(layer->id());
}

void LayerTreeImpl::PushPersistedState(LayerTreeImpl* pendingTree) {
  int id = currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0;
  pendingTree->set_currently_scrolling_layer(
      LayerTreeHostCommon::findLayerInSubtree(pendingTree->RootLayer(), id));
}

const LayerTreeSettings& LayerTreeImpl::settings() const {
  return layer_tree_host_impl_->settings();
}

OutputSurface* LayerTreeImpl::output_surface() const {
  return layer_tree_host_impl_->outputSurface();
}

ResourceProvider* LayerTreeImpl::resource_provider() const {
  return layer_tree_host_impl_->resourceProvider();
}

TileManager* LayerTreeImpl::tile_manager() const {
  return layer_tree_host_impl_->tileManager();
}

FrameRateCounter* LayerTreeImpl::frame_rate_counter() const {
  return layer_tree_host_impl_->fpsCounter();
}

bool LayerTreeImpl::IsActiveTree() const {
  return layer_tree_host_impl_->activeTree() == this;
}

bool LayerTreeImpl::IsPendingTree() const {
  return layer_tree_host_impl_->pendingTree() == this;
}

LayerImpl* LayerTreeImpl::FindActiveTreeLayerById(int id) {
  LayerTreeImpl* tree = layer_tree_host_impl_->activeTree();
  if (!tree)
    return NULL;
  return tree->LayerById(id);
}

LayerImpl* LayerTreeImpl::FindPendingTreeLayerById(int id) {
  LayerTreeImpl* tree = layer_tree_host_impl_->pendingTree();
  if (!tree)
    return NULL;
  return tree->LayerById(id);
}

int LayerTreeImpl::MaxTextureSize() const {
  return layer_tree_host_impl_->rendererCapabilities().maxTextureSize;
}

bool LayerTreeImpl::PinchGestureActive() const {
  return layer_tree_host_impl_->pinchGestureActive();
}

void LayerTreeImpl::SetNeedsRedraw() {
  layer_tree_host_impl_->setNeedsRedraw();
}

void LayerTreeImpl::SetNeedsUpdateDrawProperties() {
  layer_tree_host_impl_->setNeedsUpdateDrawProperties();
}

const LayerTreeDebugState& LayerTreeImpl::debug_state() const {
  return layer_tree_host_impl_->debugState();
}

float LayerTreeImpl::device_scale_factor() const {
  return layer_tree_host_impl_->deviceScaleFactor();
}

const gfx::Size& LayerTreeImpl::device_viewport_size() const {
  return layer_tree_host_impl_->deviceViewportSize();
}

const gfx::Size& LayerTreeImpl::layout_viewport_size() const {
  return layer_tree_host_impl_->layoutViewportSize();
}

std::string LayerTreeImpl::layer_tree_as_text() const {
  return layer_tree_host_impl_->layerTreeAsText();
}

DebugRectHistory* LayerTreeImpl::debug_rect_history() const {
  return layer_tree_host_impl_->debugRectHistory();
}

AnimationRegistrar* LayerTreeImpl::animationRegistrar() const {
  return layer_tree_host_impl_->animationRegistrar();
}

const PinchZoomViewport& LayerTreeImpl::pinch_zoom_viewport() const {
  return layer_tree_host_impl_->pinchZoomViewport();
}

} // namespace cc