summaryrefslogtreecommitdiffstats
path: root/chromium/cc/layers/painted_overlay_scrollbar_layer.cc
blob: e6fcf5ff6eceed778369ca89fb60e040d8803d84 (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
// Copyright 2016 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/layers/painted_overlay_scrollbar_layer.h"

#include <algorithm>

#include "base/auto_reset.h"
#include "cc/base/math_util.h"
#include "cc/layers/painted_overlay_scrollbar_layer_impl.h"
#include "cc/paint/skia_paint_canvas.h"
#include "cc/resources/ui_resource_bitmap.h"
#include "cc/resources/ui_resource_manager.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_impl.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSize.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/skia_util.h"

namespace cc {

std::unique_ptr<LayerImpl> PaintedOverlayScrollbarLayer::CreateLayerImpl(
    LayerTreeImpl* tree_impl) {
  return PaintedOverlayScrollbarLayerImpl::Create(
      tree_impl, id(), scrollbar_->Orientation(),
      scrollbar_->IsLeftSideVerticalScrollbar());
}

scoped_refptr<PaintedOverlayScrollbarLayer>
PaintedOverlayScrollbarLayer::Create(std::unique_ptr<Scrollbar> scrollbar,
                                     ElementId scroll_element_id) {
  return base::WrapRefCounted(new PaintedOverlayScrollbarLayer(
      std::move(scrollbar), scroll_element_id));
}

PaintedOverlayScrollbarLayer::PaintedOverlayScrollbarLayer(
    std::unique_ptr<Scrollbar> scrollbar,
    ElementId scroll_element_id)
    : scrollbar_(std::move(scrollbar)),
      scroll_element_id_(scroll_element_id),
      thumb_thickness_(scrollbar_->ThumbThickness()),
      thumb_length_(scrollbar_->ThumbLength()) {
  DCHECK(scrollbar_->UsesNinePatchThumbResource());
  SetIsScrollbar(true);
}

PaintedOverlayScrollbarLayer::~PaintedOverlayScrollbarLayer() = default;

void PaintedOverlayScrollbarLayer::SetScrollElementId(ElementId element_id) {
  if (element_id == scroll_element_id_)
    return;

  scroll_element_id_ = element_id;
  SetNeedsCommit();
}

bool PaintedOverlayScrollbarLayer::OpacityCanAnimateOnImplThread() const {
  return scrollbar_->IsOverlay();
}

void PaintedOverlayScrollbarLayer::PushPropertiesTo(LayerImpl* layer) {
  Layer::PushPropertiesTo(layer);

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

  scrollbar_layer->SetScrollElementId(scroll_element_id_);

  scrollbar_layer->SetThumbThickness(thumb_thickness_);
  scrollbar_layer->SetThumbLength(thumb_length_);
  if (scrollbar_->Orientation() == HORIZONTAL) {
    scrollbar_layer->SetTrackStart(track_rect_.x());
    scrollbar_layer->SetTrackLength(track_rect_.width());
  } else {
    scrollbar_layer->SetTrackStart(track_rect_.y());
    scrollbar_layer->SetTrackLength(track_rect_.height());
  }

  if (thumb_resource_.get()) {
    scrollbar_layer->SetImageBounds(
        layer_tree_host()->GetUIResourceManager()->GetUIResourceSize(
            thumb_resource_->id()));
    scrollbar_layer->SetAperture(aperture_);
    scrollbar_layer->set_thumb_ui_resource_id(thumb_resource_->id());
  } else {
    scrollbar_layer->SetImageBounds(gfx::Size());
    scrollbar_layer->SetAperture(gfx::Rect());
    scrollbar_layer->set_thumb_ui_resource_id(0);
  }

  if (track_resource_.get())
    scrollbar_layer->set_track_ui_resource_id(track_resource_->id());
  else
    scrollbar_layer->set_track_ui_resource_id(0);
}

void PaintedOverlayScrollbarLayer::SetLayerTreeHost(LayerTreeHost* host) {
  // When the LTH is set to null or has changed, then this layer should remove
  // all of its associated resources.
  if (host != layer_tree_host()) {
    thumb_resource_.reset();
    track_resource_.reset();
  }

  Layer::SetLayerTreeHost(host);
}

gfx::Rect PaintedOverlayScrollbarLayer::OriginThumbRectForPainting() const {
  return gfx::Rect(gfx::Point(), scrollbar_->NinePatchThumbCanvasSize());
}

bool PaintedOverlayScrollbarLayer::Update() {
  bool updated = false;
  updated |= Layer::Update();

  DCHECK(scrollbar_->HasThumb());
  DCHECK(scrollbar_->IsOverlay());
  DCHECK(scrollbar_->UsesNinePatchThumbResource());
  updated |= UpdateProperty(scrollbar_->TrackRect(), &track_rect_);
  updated |= UpdateProperty(scrollbar_->Location(), &location_);
  updated |= UpdateProperty(scrollbar_->ThumbThickness(), &thumb_thickness_);
  updated |= UpdateProperty(scrollbar_->ThumbLength(), &thumb_length_);
  updated |= PaintThumbIfNeeded();
  updated |= PaintTickmarks();

  return updated;
}

bool PaintedOverlayScrollbarLayer::PaintThumbIfNeeded() {
  if (!scrollbar_->NeedsPaintPart(THUMB) && thumb_resource_)
    return false;

  gfx::Rect paint_rect = OriginThumbRectForPainting();
  aperture_ = scrollbar_->NinePatchThumbAperture();

  DCHECK(!paint_rect.size().IsEmpty());
  DCHECK(paint_rect.origin().IsOrigin());

  SkBitmap skbitmap;
  skbitmap.allocN32Pixels(paint_rect.width(), paint_rect.height());
  SkiaPaintCanvas canvas(skbitmap);

  SkRect content_skrect = RectToSkRect(paint_rect);
  PaintFlags flags;
  flags.setAntiAlias(false);
  flags.setBlendMode(SkBlendMode::kClear);
  canvas.drawRect(content_skrect, flags);
  canvas.clipRect(content_skrect);

  scrollbar_->PaintPart(&canvas, THUMB, paint_rect);
  // Make sure that the pixels are no longer mutable to unavoid unnecessary
  // allocation and copying.
  skbitmap.setImmutable();

  thumb_resource_ = ScopedUIResource::Create(
      layer_tree_host()->GetUIResourceManager(), UIResourceBitmap(skbitmap));

  SetNeedsPushProperties();

  return true;
}

bool PaintedOverlayScrollbarLayer::PaintTickmarks() {
  if (!scrollbar_->HasTickmarks()) {
    if (!track_resource_) {
      return false;
    } else {
      // Remove previous tickmarks.
      track_resource_.reset();
      SetNeedsPushProperties();
      return true;
    }
  }

  gfx::Rect paint_rect = gfx::Rect(gfx::Point(), track_rect_.size());

  DCHECK(!paint_rect.size().IsEmpty());

  SkBitmap skbitmap;
  skbitmap.allocN32Pixels(paint_rect.width(), paint_rect.height());
  SkiaPaintCanvas canvas(skbitmap);

  SkRect content_skrect = RectToSkRect(paint_rect);
  PaintFlags flags;
  flags.setAntiAlias(false);
  flags.setBlendMode(SkBlendMode::kClear);
  canvas.drawRect(content_skrect, flags);
  canvas.clipRect(content_skrect);

  scrollbar_->PaintPart(&canvas, TICKMARKS, paint_rect);
  // Make sure that the pixels are no longer mutable to unavoid unnecessary
  // allocation and copying.
  skbitmap.setImmutable();

  track_resource_ = ScopedUIResource::Create(
      layer_tree_host()->GetUIResourceManager(), UIResourceBitmap(skbitmap));

  SetNeedsPushProperties();
  return true;
}

}  // namespace cc