File: content_layer.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (246 lines) | stat: -rw-r--r-- 8,320 bytes parent folder | download
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
// Copyright 2014 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 "chrome/browser/android/compositor/layer/content_layer.h"

#include "cc/layers/layer.h"
#include "cc/layers/layer_lists.h"
#include "chrome/browser/android/compositor/layer/thumbnail_layer.h"
#include "chrome/browser/android/compositor/tab_content_manager.h"
#include "ui/gfx/geometry/size.h"

namespace chrome {
namespace android {

// static
scoped_refptr<ContentLayer> ContentLayer::Create(
    TabContentManager* tab_content_manager) {
  return make_scoped_refptr(new ContentLayer(tab_content_manager));
}

static void SetOpacityOnLeaf(scoped_refptr<cc::Layer> layer, float alpha) {
  const cc::LayerList& children = layer->children();
  if (children.size() > 0) {
    layer->SetOpacity(1.0f);
    for (uint i = 0; i < children.size(); ++i)
      SetOpacityOnLeaf(children[i], alpha);
  } else {
    layer->SetOpacity(alpha);
  }
}

static bool DoesLeafDrawContents(scoped_refptr<cc::Layer> layer) {
  if (!layer.get())
    return false;

  // TODO: Remove the need for this logic. We can't really guess from
  // an opaque layer type whether it has valid live contents, or for example
  // just a background color placeholder. Need to get this from somewhere else
  // like ContentViewCore or RWHV.
  if (layer->DrawsContent() && !layer->hide_layer_and_subtree() &&
      !layer->background_color()) {
    return true;
  }

  const cc::LayerList& children = layer->children();
  for (unsigned i = 0; i < children.size(); i++) {
    if (DoesLeafDrawContents(children[i]))
      return true;
  }
  return false;
}

static gfx::Size GetLeafBounds(scoped_refptr<cc::Layer> layer) {
  if (layer->children().size() > 0)
    return GetLeafBounds(layer->children()[0]);
  return layer->bounds();
}

void ContentLayer::SetProperties(int id,
                                 bool can_use_live_layer,
                                 bool can_use_ntp_fallback,
                                 float static_to_view_blend,
                                 bool should_override_content_alpha,
                                 float content_alpha_override,
                                 float saturation,
                                 const gfx::Rect& desired_bounds,
                                 const gfx::Size& content_size) {
  scoped_refptr<cc::Layer> content_layer =
      tab_content_manager_->GetLiveLayer(id);
  ClipContentLayer(content_layer, desired_bounds, content_size);
  bool content_layer_draws = DoesLeafDrawContents(content_layer);

  scoped_refptr<ThumbnailLayer> static_layer =
      tab_content_manager_->GetStaticLayer(id, !content_layer_draws);

  ClipStaticLayer(static_layer, desired_bounds);

  // Reset the attachment logic if the number of children doesn't match the
  // boolean flags. At some point while a tab is in the background one or more
  // layers may be removed from this tree.
  // Note that this needs to be checked *after* we access TabContentManager, as
  // that class might remove layers internally, messing up our own tracking.
  unsigned int expected_layers = 0;
  expected_layers += content_attached_ ? 1 : 0;
  expected_layers += static_attached_ ? 1 : 0;
  if (layer_->children().size() != expected_layers) {
    content_attached_ = false;
    static_attached_ = false;
    const cc::LayerList& layer_children = layer_->children();
    for (unsigned i = 0; i < layer_children.size(); i++)
      layer_children[i]->RemoveFromParent();
  }

  gfx::Size content_bounds(0, 0);
  if (!content_layer.get() || !can_use_live_layer) {
    SetContentLayer(nullptr);
    SetStaticLayer(static_layer);
    if (static_layer.get())
      content_bounds = static_layer->layer()->bounds();
    else
      content_bounds.set_width(content_size.width());
  } else {
    SetContentLayer(content_layer);
    content_bounds = content_layer->bounds();

    if (static_to_view_blend == 0.0f && !content_layer_draws)
      static_to_view_blend = 1.0f;

    if (static_to_view_blend != 0.0f && static_layer.get()) {
      static_layer->layer()->SetOpacity(static_to_view_blend);
      SetStaticLayer(static_layer);
      if (content_bounds.GetArea() == 0 || !content_layer_draws)
        content_bounds = static_layer->layer()->bounds();
    } else {
      SetStaticLayer(nullptr);
    }
  }

  if (should_override_content_alpha) {
    for (unsigned int i = 0; i < layer_->children().size(); ++i)
      SetOpacityOnLeaf(layer_->children()[i], content_alpha_override);
  }

  if (!content_layer_draws && !static_attached_)
    content_bounds = gfx::Size(0, 0);

  layer_->SetBounds(content_bounds);

  // Only worry about saturation on the static layer
  if (static_layer.get()) {
    if (saturation != saturation_) {
      saturation_ = saturation;
      cc::FilterOperations filters;
      if (saturation_ < 1.0f)
        filters.Append(cc::FilterOperation::CreateSaturateFilter(saturation_));
      static_layer->layer()->SetFilters(filters);
    }
  }
}

gfx::Size ContentLayer::GetContentSize() {
  if (content_attached_ && DoesLeafDrawContents(layer()->children()[0]))
    return layer_->children()[0]->bounds();
  return gfx::Size(0, 0);
}

scoped_refptr<cc::Layer> ContentLayer::layer() {
  return layer_;
}

ContentLayer::ContentLayer(TabContentManager* tab_content_manager)
    : layer_(cc::Layer::Create()),
      content_attached_(false),
      static_attached_(false),
      saturation_(1.0f),
      tab_content_manager_(tab_content_manager) {
}

ContentLayer::~ContentLayer() {
}

void ContentLayer::SetContentLayer(scoped_refptr<cc::Layer> layer) {
  // Check indices
  // content_attached_, expect at least 1 child.
  DCHECK(!content_attached_ || layer_->children().size() > 0);

  if (!layer.get()) {
    if (content_attached_)
      layer_->child_at(0)->RemoveFromParent();
    content_attached_ = false;
    return;
  }

  bool new_layer = false;
  if (content_attached_ && layer_->child_at(0)->id() != layer->id()) {
    layer_->ReplaceChild(layer_->child_at(0), layer);
    new_layer = true;
  } else if (!content_attached_) {
    layer_->InsertChild(layer, 0);
    new_layer = true;
  }

  // If this is a new layer, reset it's opacity.
  if (new_layer)
    SetOpacityOnLeaf(layer, 1.0f);

  content_attached_ = true;
}

void ContentLayer::SetStaticLayer(
    scoped_refptr<ThumbnailLayer> new_static_layer) {
  // Make sure child access will be valid.
  // !content_attached_ AND !static_attached_, expect 0 children.
  // content_attached_ XOR static_attached_, expect 1 child.
  // content_attached_ AND static_attached_, expect 2 children.
  DCHECK((!content_attached_ && !static_attached_) ||
         (content_attached_ != static_attached_ &&
          layer_->children().size() >= 1) ||
         (content_attached_ && static_attached_ &&
          layer_->children().size() >= 2));

  if (!new_static_layer.get()) {
    if (static_layer_.get()) {
      static_layer_->layer()->RemoveFromParent();
      static_layer_ = nullptr;
    }
    static_attached_ = false;
    return;
  }
  static_layer_ = new_static_layer;
  static_layer_->AddSelfToParentOrReplaceAt(layer_, content_attached_ ? 1 : 0);
  saturation_ = -1.0f;
  static_layer_->layer()->SetIsDrawable(true);
  static_attached_ = true;
}

void ContentLayer::ClipContentLayer(scoped_refptr<cc::Layer> content_layer,
                                    gfx::Rect clipping,
                                    gfx::Size content_size) {
  if (!content_layer.get())
    return;

  gfx::Size bounds(GetLeafBounds(content_layer));
  content_layer->SetMasksToBounds(true);
  gfx::Size clamped_bounds(bounds);
  clamped_bounds.SetToMin(clipping.size());
  content_layer->SetBounds(clamped_bounds);

  if (content_layer->children().size() > 0) {
    gfx::PointF offset(
        std::min(content_size.width() - bounds.width() - clipping.x(), 0),
        std::min(content_size.height() - bounds.height() - clipping.y(), 0));
    content_layer->children()[0]->SetPosition(offset);
  }
}

void ContentLayer::ClipStaticLayer(scoped_refptr<ThumbnailLayer> static_layer,
                                   gfx::Rect clipping) {
  if (!static_layer.get())
    return;
  static_layer->Clip(clipping);
}

}  //  namespace android
}  //  namespace chrome