File: nine_patch_thumb_scrollbar_layer_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (231 lines) | stat: -rw-r--r-- 8,020 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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/layers/nine_patch_thumb_scrollbar_layer_impl.h"

#include <memory>
#include <vector>

#include "base/memory/ptr_util.h"
#include "cc/trees/layer_tree_impl.h"
#include "components/viz/common/quads/solid_color_draw_quad.h"
#include "components/viz/common/quads/texture_draw_quad.h"

namespace cc {

std::unique_ptr<NinePatchThumbScrollbarLayerImpl>
NinePatchThumbScrollbarLayerImpl::Create(LayerTreeImpl* tree_impl,
                                         int id,
                                         ScrollbarOrientation orientation,
                                         bool is_left_side_vertical_scrollbar) {
  return base::WrapUnique(new NinePatchThumbScrollbarLayerImpl(
      tree_impl, id, orientation, is_left_side_vertical_scrollbar));
}

NinePatchThumbScrollbarLayerImpl::NinePatchThumbScrollbarLayerImpl(
    LayerTreeImpl* tree_impl,
    int id,
    ScrollbarOrientation orientation,
    bool is_left_side_vertical_scrollbar)
    : ScrollbarLayerImplBase(tree_impl,
                             id,
                             orientation,
                             is_left_side_vertical_scrollbar,
                             true) {}

NinePatchThumbScrollbarLayerImpl::~NinePatchThumbScrollbarLayerImpl() = default;

mojom::LayerType NinePatchThumbScrollbarLayerImpl::GetLayerType() const {
  return mojom::LayerType::kNinePatchThumbScrollbar;
}

std::unique_ptr<LayerImpl> NinePatchThumbScrollbarLayerImpl::CreateLayerImpl(
    LayerTreeImpl* tree_impl) const {
  return NinePatchThumbScrollbarLayerImpl::Create(
      tree_impl, id(), orientation(), is_left_side_vertical_scrollbar());
}

void NinePatchThumbScrollbarLayerImpl::PushPropertiesTo(LayerImpl* layer) {
  ScrollbarLayerImplBase::PushPropertiesTo(layer);

  NinePatchThumbScrollbarLayerImpl* scrollbar_layer =
      static_cast<NinePatchThumbScrollbarLayerImpl*>(layer);

  scrollbar_layer->SetThumbThickness(thumb_thickness_);
  scrollbar_layer->SetThumbLength(thumb_length_);
  scrollbar_layer->SetTrackStart(track_start_);
  scrollbar_layer->SetTrackLength(track_length_);

  scrollbar_layer->SetImageBounds(image_bounds_);
  scrollbar_layer->SetAperture(aperture_);

  scrollbar_layer->set_thumb_ui_resource_id(thumb_ui_resource_id_);
  scrollbar_layer->set_track_and_buttons_ui_resource_id(
      track_and_buttons_ui_resource_id_);
}

bool NinePatchThumbScrollbarLayerImpl::WillDraw(
    DrawMode draw_mode,
    viz::ClientResourceProvider* resource_provider) {
  DCHECK(draw_mode != DRAW_MODE_RESOURCELESS_SOFTWARE);
  return LayerImpl::WillDraw(draw_mode, resource_provider);
}

void NinePatchThumbScrollbarLayerImpl::AppendQuads(
    const AppendQuadsContext& context,
    viz::CompositorRenderPass* render_pass,
    AppendQuadsData* append_quads_data) {
  viz::SharedQuadState* shared_quad_state =
      render_pass->CreateAndAppendSharedQuadState();
  AppendThumbQuads(render_pass, append_quads_data, shared_quad_state);
  AppendTrackAndButtonsQuads(render_pass, append_quads_data, shared_quad_state);
}

void NinePatchThumbScrollbarLayerImpl::AppendThumbQuads(
    viz::CompositorRenderPass* render_pass,
    AppendQuadsData* append_quads_data,
    viz::SharedQuadState* shared_quad_state) {
  if (aperture_.IsEmpty())
    return;

  bool is_resource =
      thumb_ui_resource_id_ &&
      layer_tree_impl()->ResourceIdForUIResource(thumb_ui_resource_id_);
  bool are_contents_opaque =
      is_resource &&
      (layer_tree_impl()->IsUIResourceOpaque(thumb_ui_resource_id_) ||
       contents_opaque());
  PopulateSharedQuadState(shared_quad_state, are_contents_opaque);
  AppendDebugBorderQuad(render_pass, gfx::Rect(bounds()), shared_quad_state,
                        append_quads_data);

  if (!is_resource)
    return;

  // For overlay scrollbars, the border should match the inset of the aperture
  // and be symmetrical.
  gfx::Rect border(aperture_.x(), aperture_.y(), aperture_.x() * 2,
                   aperture_.y() * 2);
  gfx::Rect thumb_quad_rect(ComputeThumbQuadRect());
  gfx::Rect layer_occlusion;
  bool fill_center = true;
  bool nearest_neighbor = false;

  // Avoid drawing a scrollber in the degenerate case where the scroller is
  // smaller than the border size.
  if (thumb_quad_rect.height() < border.height() ||
      thumb_quad_rect.width() < border.width())
    return;

  const bool layout_changed = quad_generator_.SetLayout(
      image_bounds_, thumb_quad_rect.size(), aperture_, border, layer_occlusion,
      fill_center, nearest_neighbor);
  if (layout_changed) {
    quad_generator_.CheckGeometryLimitations();
    patches_ = quad_generator_.GeneratePatches();
  }

  quad_generator_.AppendQuadsForCc(this, thumb_ui_resource_id_, render_pass,
                                   shared_quad_state, patches_,
                                   thumb_quad_rect.OffsetFromOrigin());
}

void NinePatchThumbScrollbarLayerImpl::AppendTrackAndButtonsQuads(
    viz::CompositorRenderPass* render_pass,
    AppendQuadsData* append_quads_data,
    viz::SharedQuadState* shared_quad_state) {
  viz::ResourceId track_resource_id =
      layer_tree_impl()->ResourceIdForUIResource(
          track_and_buttons_ui_resource_id_);
  if (!track_resource_id)
    return;

  bool nearest_neighbor = false;

  gfx::Rect track_quad_rect(bounds());
  gfx::Rect scaled_track_quad_rect(bounds());
  gfx::Rect visible_track_quad_rect =
      draw_properties().occlusion_in_content_space.GetUnoccludedContentRect(
          track_quad_rect);
  gfx::Rect scaled_visible_track_quad_rect =
      gfx::ScaleToEnclosingRect(visible_track_quad_rect, 1.f);

  bool needs_blending = !contents_opaque();
  gfx::PointF uv_top_left(0.f, 0.f);
  gfx::PointF uv_bottom_right(1.f, 1.f);
  viz::TextureDrawQuad* quad =
      render_pass->CreateAndAppendDrawQuad<viz::TextureDrawQuad>();
  quad->SetNew(shared_quad_state, scaled_track_quad_rect,
               scaled_visible_track_quad_rect, needs_blending,
               track_resource_id, uv_top_left, uv_bottom_right,
               SkColors::kTransparent, nearest_neighbor,
               /*secure_output_only=*/false, gfx::ProtectedVideoType::kClear);
  ValidateQuadResources(quad);
}

void NinePatchThumbScrollbarLayerImpl::SetThumbThickness(int thumb_thickness) {
  if (thumb_thickness_ == thumb_thickness)
    return;
  thumb_thickness_ = thumb_thickness;
  NoteLayerPropertyChanged();
}

int NinePatchThumbScrollbarLayerImpl::ThumbThickness() const {
  return thumb_thickness_;
}

void NinePatchThumbScrollbarLayerImpl::SetThumbLength(int thumb_length) {
  if (thumb_length_ == thumb_length)
    return;
  thumb_length_ = thumb_length;
  NoteLayerPropertyChanged();
}

int NinePatchThumbScrollbarLayerImpl::ThumbLength() const {
  return thumb_length_;
}

void NinePatchThumbScrollbarLayerImpl::SetTrackStart(int track_start) {
  if (track_start_ == track_start)
    return;
  track_start_ = track_start;
  NoteLayerPropertyChanged();
}

int NinePatchThumbScrollbarLayerImpl::TrackStart() const {
  return track_start_;
}

void NinePatchThumbScrollbarLayerImpl::SetTrackLength(int track_length) {
  if (track_length_ == track_length)
    return;
  track_length_ = track_length;
  NoteLayerPropertyChanged();
}

void NinePatchThumbScrollbarLayerImpl::SetImageBounds(const gfx::Size& bounds) {
  if (image_bounds_ == bounds)
    return;
  image_bounds_ = bounds;
  NoteLayerPropertyChanged();
}

void NinePatchThumbScrollbarLayerImpl::SetAperture(const gfx::Rect& aperture) {
  if (aperture_ == aperture)
    return;
  aperture_ = aperture;
  NoteLayerPropertyChanged();
}

float NinePatchThumbScrollbarLayerImpl::TrackLength() const {
  return track_length_ + (orientation() == ScrollbarOrientation::kVertical
                              ? vertical_adjust()
                              : 0);
}

bool NinePatchThumbScrollbarLayerImpl::IsThumbResizable() const {
  return false;
}

}  // namespace cc