File: toolbar_layer.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (297 lines) | stat: -rw-r--r-- 13,142 bytes parent folder | download | duplicates (3)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2014 The Chromium Authors
// 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/toolbar_layer.h"

#include "base/feature_list.h"
#include "cc/resources/scoped_ui_resource.h"
#include "cc/slim/layer.h"
#include "cc/slim/nine_patch_layer.h"
#include "cc/slim/solid_color_layer.h"
#include "cc/slim/ui_resource_layer.h"
#include "chrome/browser/android/compositor/resources/toolbar_resource.h"
#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "chrome/browser/ui/ui_features.h"
#include "components/viz/common/features.h"
#include "components/viz/common/quads/offset_tag.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/android/resources/nine_patch_resource.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/gfx/geometry/rounded_corners_f.h"

namespace {
// LINT.IfChange(InvalidContentOffset)
const float kInvalidContentOffset = -10001.f;
// LINT.ThenChange(//chrome/browser/ui/android/toolbar/java/src/org/chromium/chrome/browser/toolbar/top/TopToolbarOverlayMediator.java:InvalidContentOffset)
}  // namespace

namespace android {

// static
scoped_refptr<ToolbarLayer> ToolbarLayer::Create(
    ui::ResourceManager* resource_manager) {
  return base::WrapRefCounted(new ToolbarLayer(resource_manager));
}

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

void ToolbarLayer::PushResource(int toolbar_resource_id,
                                int toolbar_background_color,
                                bool anonymize,
                                int toolbar_textbox_background_color,
                                int url_bar_background_resource_id,
                                float x_offset,
                                float y_offset,
                                float legacy_content_offset,
                                bool show_debug,
                                bool clip_shadow,
                                const viz::OffsetTag& offset_tag) {
  ToolbarResource* resource =
      ToolbarResource::From(resource_manager_->GetResource(
          ui::ANDROID_RESOURCE_TYPE_DYNAMIC, toolbar_resource_id));

  // TODO(https://crbug.com/466162772): after the progress bar is decoupled from
  // the toolbar, we can freely set the visibility of the toolbar without
  // worrying about the progress bar. If AnimatedProgressBar is enabled, ensure
  // that we don't hide the parent layer so that the progress bar is still
  // visible even when we don't have a capture for the toolbar.
  if (features::IsAndroidAnimatedProgressBarInBrowserEnabled()) {
    toolbar_background_layer_->SetHideLayerAndSubtree(!resource);
    url_bar_background_layer_->SetHideLayerAndSubtree(!resource);
    bitmap_layer_->SetHideLayerAndSubtree(!resource);
  } else {
    layer_->SetHideLayerAndSubtree(!resource);
  }
  if (!resource) {
    return;
  }

  // This layer effectively draws over the space the resource takes for shadows.
  // Set the bounds to the non-shadow size so that other things can properly
  // line up.
  gfx::Size toolbar_bounds =
      gfx::Size(resource->size().width(),
                resource->size().height() - resource->shadow_height());
  layer_->SetBounds(toolbar_bounds);

  toolbar_background_layer_->SetBounds(resource->toolbar_rect().size());
  toolbar_background_layer_->SetPosition(
      gfx::PointF(resource->toolbar_rect().origin()));
  // TODO(crbug.com/40219248): Remove FromColor and make all SkColor4f.
  toolbar_background_layer_->SetBackgroundColor(
      SkColor4f::FromColor(toolbar_background_color));

  bool url_bar_visible = resource->location_bar_content_rect().width() != 0;
  url_bar_background_layer_->SetHideLayerAndSubtree(!url_bar_visible);
  if (url_bar_visible) {
    ui::NinePatchResource* url_bar_background_resource;
    if (base::FeatureList::IsEnabled(
            chrome::android::kMvcUpdateViewWhenModelChanged)) {
      // Because the ToolbarLayer is not updated every frame, even if visible,
      // we need keep the tint in the cache until the layer is destroyed.
      url_bar_background_resource = ui::NinePatchResource::From(
          resource_manager_->GetAndRetainStaticResourceWithTint(
              url_bar_background_resource_id,
              toolbar_textbox_background_color));
      DCHECK(last_url_bar_background_resource_id_ == kInvalidResourceId ||
             last_url_bar_background_resource_id_ ==
                 url_bar_background_resource_id);
      last_url_bar_background_resource_id_ = url_bar_background_resource_id;
    } else {
      url_bar_background_resource = ui::NinePatchResource::From(
          resource_manager_->GetStaticResourceWithTint(
              url_bar_background_resource_id,
              toolbar_textbox_background_color));
    }

    gfx::Size draw_size(url_bar_background_resource->DrawSize(
        resource->location_bar_content_rect().size()));
    gfx::Rect border(url_bar_background_resource->Border(draw_size));
    gfx::PointF position(url_bar_background_resource->DrawPosition(
        resource->location_bar_content_rect().origin()));

    url_bar_background_layer_->SetBounds(draw_size);
    url_bar_background_layer_->SetPosition(position);
    url_bar_background_layer_->SetBorder(border);
    url_bar_background_layer_->SetAperture(
        url_bar_background_resource->aperture());
    url_bar_background_layer_->SetUIResourceId(
        url_bar_background_resource->ui_resource()->id());
  }

  bitmap_layer_->SetUIResourceId(resource->ui_resource()->id());
  bitmap_layer_->SetBounds(resource->size());

  layer_->SetMasksToBounds(clip_shadow);

  // The location bar background doubles as the anonymize layer -- it just
  // needs to be drawn on top of the toolbar bitmap.
  int background_layer_index = GetIndexOfLayer(toolbar_background_layer_);
  scoped_refptr<cc::slim::Layer> parent = ToolbarParentLayer();
  bool needs_move_to_front =
      anonymize && parent->children().back() != url_bar_background_layer_;
  bool needs_move_to_back =
      !anonymize &&
      parent->children()[background_layer_index] != url_bar_background_layer_;

  // If the layer needs to move, remove and re-add it.
  if (needs_move_to_front) {
    parent->AddChild(url_bar_background_layer_);
  } else if (needs_move_to_back) {
    parent->InsertChild(url_bar_background_layer_, background_layer_index + 1);
  }

  debug_layer_->SetBounds(resource->size());
  if (show_debug && !debug_layer_->parent())
    layer_->AddChild(debug_layer_);
  else if (!show_debug && debug_layer_->parent())
    debug_layer_->RemoveFromParent();

  // |legacy_content_offset| represents the bottom of the toolbar, assuming it's
  // always at the bottom of the browser controls. This is no longer the case
  // as for 2025.
  // TODO(https://crbug.com/454338286): Rename / remove in favor of y_Offset.
  if (!base::FeatureList::IsEnabled(chrome::android::kTopControlsRefactor) ||
      !base::FeatureList::IsEnabled(chrome::android::kTopControlsRefactorV2) ||
      kInvalidContentOffset != legacy_content_offset) {
    y_offset = legacy_content_offset - layer_->bounds().height();
  }

  layer_->SetPosition(gfx::PointF(x_offset, y_offset));
  layer_->SetOffsetTag(offset_tag);
}

int ToolbarLayer::GetIndexOfLayer(scoped_refptr<cc::slim::Layer> layer) {
  scoped_refptr<cc::slim::Layer> parent = ToolbarParentLayer();
  for (unsigned int i = 0; i < parent->children().size(); ++i) {
    if (parent->children()[i] == layer) {
      return i;
    }
  }

  return -1;
}

scoped_refptr<cc::slim::Layer> ToolbarLayer::ToolbarParentLayer() {
  return layer_;
}

void ToolbarLayer::UpdateProgressBar(int progress_bar_x,
                                     int progress_bar_y,
                                     int progress_bar_width,
                                     int progress_bar_height,
                                     int progress_bar_color,
                                     int progress_bar_background_x,
                                     int progress_bar_background_y,
                                     int progress_bar_background_width,
                                     int progress_bar_background_height,
                                     int progress_bar_background_color,
                                     int progress_bar_static_background_x,
                                     int progress_bar_static_background_width,
                                     int progress_bar_static_background_color,
                                     float corner_radius,
                                     bool progress_bar_visual_update_available,
                                     bool visible) {
  bool is_progress_bar_visible = SkColorGetA(progress_bar_background_color);
  if (features::IsAndroidAnimatedProgressBarInBrowserEnabled()) {
    is_progress_bar_visible = visible;
  }

  progress_bar_background_layer_->SetHideLayerAndSubtree(!is_progress_bar_visible);
  progress_bar_layer_->SetHideLayerAndSubtree(!is_progress_bar_visible);
  progress_bar_static_background_layer_->SetHideLayerAndSubtree(
      !(is_progress_bar_visible && progress_bar_visual_update_available));

  if (is_progress_bar_visible) {
    progress_bar_background_layer_->SetPosition(
        gfx::PointF(progress_bar_background_x, progress_bar_background_y));
    progress_bar_background_layer_->SetBounds(
        gfx::Size(progress_bar_background_width,
                  progress_bar_background_height));
    // TODO(crbug.com/40219248): Remove FromColor and make all SkColor4f.
    progress_bar_background_layer_->SetBackgroundColor(
        SkColor4f::FromColor(progress_bar_background_color));
    progress_bar_background_layer_->SetRoundedCorner(gfx::RoundedCornersF(corner_radius));
    progress_bar_layer_->SetPosition(
        gfx::PointF(progress_bar_x, progress_bar_y));
    progress_bar_layer_->SetBounds(
        gfx::Size(progress_bar_width, progress_bar_height));
    // TODO(crbug.com/40219248): Remove FromColor and make all SkColor4f.
    progress_bar_layer_->SetBackgroundColor(
        SkColor4f::FromColor(progress_bar_color));
    progress_bar_layer_->SetRoundedCorner(gfx::RoundedCornersF(corner_radius));

    if (progress_bar_visual_update_available) {
      progress_bar_static_background_layer_->SetPosition(gfx::PointF(
          progress_bar_static_background_x, progress_bar_y));
      progress_bar_static_background_layer_->SetBounds(gfx::Size(
          progress_bar_static_background_width, progress_bar_height));
      progress_bar_static_background_layer_->SetBackgroundColor(
          SkColor4f::FromColor(progress_bar_static_background_color));
      progress_bar_static_background_layer_->SetRoundedCorner(gfx::RoundedCornersF(corner_radius));
    }
  }
}

void ToolbarLayer::SetOpacity(float opacity) {
  toolbar_background_layer_->SetOpacity(opacity);
  url_bar_background_layer_->SetOpacity(opacity);
  bitmap_layer_->SetOpacity(opacity);

  progress_bar_layer_->SetOpacity(opacity);
  progress_bar_background_layer_->SetOpacity(opacity);
  progress_bar_static_background_layer_->SetOpacity(opacity);
}

ToolbarLayer::ToolbarLayer(ui::ResourceManager* resource_manager)
    : resource_manager_(resource_manager->GetWeakPtr()),
      layer_(cc::slim::Layer::Create()),
      toolbar_layers_(cc::slim::Layer::Create()),
      progress_bar_layers_(cc::slim::Layer::Create()),
      toolbar_background_layer_(cc::slim::SolidColorLayer::Create()),
      url_bar_background_layer_(cc::slim::NinePatchLayer::Create()),
      bitmap_layer_(cc::slim::UIResourceLayer::Create()),
      progress_bar_layer_(cc::slim::SolidColorLayer::Create()),
      progress_bar_background_layer_(cc::slim::SolidColorLayer::Create()),
      progress_bar_static_background_layer_(
          cc::slim::SolidColorLayer::Create()),
      debug_layer_(cc::slim::SolidColorLayer::Create()) {
  toolbar_background_layer_->SetIsDrawable(true);
  layer_->AddChild(toolbar_background_layer_);

  url_bar_background_layer_->SetIsDrawable(true);
  url_bar_background_layer_->SetFillCenter(true);
  layer_->AddChild(url_bar_background_layer_);

  bitmap_layer_->SetIsDrawable(true);
  layer_->AddChild(bitmap_layer_);

  progress_bar_static_background_layer_->SetIsDrawable(true);
  progress_bar_static_background_layer_->SetHideLayerAndSubtree(true);
  layer_->AddChild(progress_bar_static_background_layer_);

  progress_bar_background_layer_->SetIsDrawable(true);
  progress_bar_background_layer_->SetHideLayerAndSubtree(true);
  layer_->AddChild(progress_bar_background_layer_);

  progress_bar_layer_->SetIsDrawable(true);
  progress_bar_layer_->SetHideLayerAndSubtree(true);
  layer_->AddChild(progress_bar_layer_);

  debug_layer_->SetIsDrawable(true);
  debug_layer_->SetBackgroundColor(SkColors::kGreen);
  debug_layer_->SetOpacity(0.5f);
}

ToolbarLayer::~ToolbarLayer() {
  if (resource_manager_ &&
      last_url_bar_background_resource_id_ != kInvalidResourceId) {
    resource_manager_->ReleaseStaticResource(
        last_url_bar_background_resource_id_);
  }
}

}  //  namespace android