Delete unused downloads page asset.
[chromium-blink-merge.git] / cc / layers / painted_scrollbar_layer.cc
blobf537ffc7e079e36ab29a38b9ff49be877ad986e5
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/painted_scrollbar_layer.h"
7 #include <algorithm>
9 #include "base/auto_reset.h"
10 #include "base/basictypes.h"
11 #include "base/trace_event/trace_event.h"
12 #include "cc/base/math_util.h"
13 #include "cc/layers/painted_scrollbar_layer_impl.h"
14 #include "cc/resources/ui_resource_bitmap.h"
15 #include "cc/trees/draw_property_utils.h"
16 #include "cc/trees/layer_tree_host.h"
17 #include "cc/trees/layer_tree_impl.h"
18 #include "skia/ext/platform_canvas.h"
19 #include "skia/ext/refptr.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "third_party/skia/include/core/SkCanvas.h"
22 #include "third_party/skia/include/core/SkSize.h"
23 #include "ui/gfx/geometry/size_conversions.h"
24 #include "ui/gfx/skia_util.h"
26 namespace cc {
28 scoped_ptr<LayerImpl> PaintedScrollbarLayer::CreateLayerImpl(
29 LayerTreeImpl* tree_impl) {
30 return PaintedScrollbarLayerImpl::Create(
31 tree_impl, id(), scrollbar_->Orientation());
34 scoped_refptr<PaintedScrollbarLayer> PaintedScrollbarLayer::Create(
35 const LayerSettings& settings,
36 scoped_ptr<Scrollbar> scrollbar,
37 int scroll_layer_id) {
38 return make_scoped_refptr(
39 new PaintedScrollbarLayer(settings, scrollbar.Pass(), scroll_layer_id));
42 PaintedScrollbarLayer::PaintedScrollbarLayer(const LayerSettings& settings,
43 scoped_ptr<Scrollbar> scrollbar,
44 int scroll_layer_id)
45 : Layer(settings),
46 scrollbar_(scrollbar.Pass()),
47 scroll_layer_id_(scroll_layer_id),
48 clip_layer_id_(Layer::INVALID_ID),
49 internal_contents_scale_(1.f),
50 thumb_thickness_(scrollbar_->ThumbThickness()),
51 thumb_length_(scrollbar_->ThumbLength()),
52 is_overlay_(scrollbar_->IsOverlay()),
53 has_thumb_(scrollbar_->HasThumb()) {
54 if (!scrollbar_->IsOverlay())
55 SetShouldScrollOnMainThread(true);
58 PaintedScrollbarLayer::~PaintedScrollbarLayer() {}
60 int PaintedScrollbarLayer::ScrollLayerId() const {
61 return scroll_layer_id_;
64 void PaintedScrollbarLayer::SetScrollLayer(int layer_id) {
65 if (layer_id == scroll_layer_id_)
66 return;
68 scroll_layer_id_ = layer_id;
69 SetNeedsFullTreeSync();
72 void PaintedScrollbarLayer::SetClipLayer(int layer_id) {
73 if (layer_id == clip_layer_id_)
74 return;
76 clip_layer_id_ = layer_id;
77 SetNeedsFullTreeSync();
80 bool PaintedScrollbarLayer::OpacityCanAnimateOnImplThread() const {
81 return scrollbar_->IsOverlay();
84 ScrollbarOrientation PaintedScrollbarLayer::orientation() const {
85 return scrollbar_->Orientation();
88 int PaintedScrollbarLayer::MaxTextureSize() {
89 DCHECK(layer_tree_host());
90 return layer_tree_host()->GetRendererCapabilities().max_texture_size;
93 float PaintedScrollbarLayer::ClampScaleToMaxTextureSize(float scale) {
94 // If the scaled bounds() is bigger than the max texture size of the
95 // device, we need to clamp it by rescaling, since this is used
96 // below to set the texture size.
97 gfx::Size scaled_bounds = gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
98 if (scaled_bounds.width() > MaxTextureSize() ||
99 scaled_bounds.height() > MaxTextureSize()) {
100 if (scaled_bounds.width() > scaled_bounds.height())
101 return (MaxTextureSize() - 1) / static_cast<float>(bounds().width());
102 else
103 return (MaxTextureSize() - 1) / static_cast<float>(bounds().height());
105 return scale;
108 void PaintedScrollbarLayer::PushPropertiesTo(LayerImpl* layer) {
109 Layer::PushPropertiesTo(layer);
111 PushScrollClipPropertiesTo(layer);
113 PaintedScrollbarLayerImpl* scrollbar_layer =
114 static_cast<PaintedScrollbarLayerImpl*>(layer);
116 scrollbar_layer->set_internal_contents_scale_and_bounds(
117 internal_contents_scale_, internal_content_bounds_);
119 scrollbar_layer->SetThumbThickness(thumb_thickness_);
120 scrollbar_layer->SetThumbLength(thumb_length_);
121 if (orientation() == HORIZONTAL) {
122 scrollbar_layer->SetTrackStart(
123 track_rect_.x() - location_.x());
124 scrollbar_layer->SetTrackLength(track_rect_.width());
125 } else {
126 scrollbar_layer->SetTrackStart(
127 track_rect_.y() - location_.y());
128 scrollbar_layer->SetTrackLength(track_rect_.height());
131 if (track_resource_.get())
132 scrollbar_layer->set_track_ui_resource_id(track_resource_->id());
133 else
134 scrollbar_layer->set_track_ui_resource_id(0);
135 if (thumb_resource_.get())
136 scrollbar_layer->set_thumb_ui_resource_id(thumb_resource_->id());
137 else
138 scrollbar_layer->set_thumb_ui_resource_id(0);
140 scrollbar_layer->set_is_overlay_scrollbar(is_overlay_);
143 ScrollbarLayerInterface* PaintedScrollbarLayer::ToScrollbarLayer() {
144 return this;
147 void PaintedScrollbarLayer::PushScrollClipPropertiesTo(LayerImpl* layer) {
148 PaintedScrollbarLayerImpl* scrollbar_layer =
149 static_cast<PaintedScrollbarLayerImpl*>(layer);
151 scrollbar_layer->SetScrollLayerAndClipLayerByIds(scroll_layer_id_,
152 clip_layer_id_);
155 void PaintedScrollbarLayer::SetLayerTreeHost(LayerTreeHost* host) {
156 // When the LTH is set to null or has changed, then this layer should remove
157 // all of its associated resources.
158 if (!host || host != layer_tree_host()) {
159 track_resource_ = nullptr;
160 thumb_resource_ = nullptr;
163 Layer::SetLayerTreeHost(host);
166 gfx::Rect PaintedScrollbarLayer::ScrollbarLayerRectToContentRect(
167 const gfx::Rect& layer_rect) const {
168 // Don't intersect with the bounds as in LayerRectToContentRect() because
169 // layer_rect here might be in coordinates of the containing layer.
170 gfx::Rect expanded_rect = gfx::ScaleToEnclosingRect(
171 layer_rect, internal_contents_scale_, internal_contents_scale_);
172 // We should never return a rect bigger than the content bounds.
173 gfx::Size clamped_size = expanded_rect.size();
174 clamped_size.SetToMin(internal_content_bounds_);
175 expanded_rect.set_size(clamped_size);
176 return expanded_rect;
179 gfx::Rect PaintedScrollbarLayer::OriginThumbRect() const {
180 gfx::Size thumb_size;
181 if (orientation() == HORIZONTAL) {
182 thumb_size =
183 gfx::Size(scrollbar_->ThumbLength(), scrollbar_->ThumbThickness());
184 } else {
185 thumb_size =
186 gfx::Size(scrollbar_->ThumbThickness(), scrollbar_->ThumbLength());
188 return gfx::Rect(thumb_size);
191 void PaintedScrollbarLayer::UpdateThumbAndTrackGeometry() {
192 UpdateProperty(scrollbar_->TrackRect(), &track_rect_);
193 UpdateProperty(scrollbar_->Location(), &location_);
194 UpdateProperty(scrollbar_->IsOverlay(), &is_overlay_);
195 UpdateProperty(scrollbar_->HasThumb(), &has_thumb_);
196 if (has_thumb_) {
197 UpdateProperty(scrollbar_->ThumbThickness(), &thumb_thickness_);
198 UpdateProperty(scrollbar_->ThumbLength(), &thumb_length_);
199 } else {
200 UpdateProperty(0, &thumb_thickness_);
201 UpdateProperty(0, &thumb_length_);
205 void PaintedScrollbarLayer::UpdateInternalContentScale() {
206 float scale = layer_tree_host()->device_scale_factor();
207 if (layer_tree_host()
208 ->settings()
209 .layer_transforms_should_scale_layer_contents) {
210 gfx::Transform transform;
211 transform = DrawTransformFromPropertyTrees(
212 this, layer_tree_host()->property_trees()->transform_tree);
214 gfx::Vector2dF transform_scales =
215 MathUtil::ComputeTransform2dScaleComponents(transform, scale);
216 scale = std::max(transform_scales.x(), transform_scales.y());
218 bool changed = false;
219 changed |= UpdateProperty(ClampScaleToMaxTextureSize(scale),
220 &internal_contents_scale_);
221 changed |= UpdateProperty(
222 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), internal_contents_scale_)),
223 &internal_content_bounds_);
224 if (changed) {
225 // If the content scale or bounds change, repaint.
226 SetNeedsDisplay();
230 bool PaintedScrollbarLayer::Update() {
232 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
233 true);
234 Layer::Update();
235 UpdateInternalContentScale();
238 UpdateThumbAndTrackGeometry();
240 gfx::Rect track_layer_rect = gfx::Rect(location_, bounds());
241 gfx::Rect scaled_track_rect = ScrollbarLayerRectToContentRect(
242 track_layer_rect);
244 bool updated = false;
246 if (track_rect_.IsEmpty() || scaled_track_rect.IsEmpty()) {
247 if (track_resource_) {
248 track_resource_ = nullptr;
249 thumb_resource_ = nullptr;
250 SetNeedsPushProperties();
251 updated = true;
253 return updated;
256 if (!has_thumb_ && thumb_resource_) {
257 thumb_resource_ = nullptr;
258 SetNeedsPushProperties();
259 updated = true;
262 if (update_rect_.IsEmpty() && track_resource_)
263 return updated;
265 track_resource_ = ScopedUIResource::Create(
266 layer_tree_host(),
267 RasterizeScrollbarPart(track_layer_rect, scaled_track_rect, TRACK));
269 gfx::Rect thumb_layer_rect = OriginThumbRect();
270 gfx::Rect scaled_thumb_rect =
271 ScrollbarLayerRectToContentRect(thumb_layer_rect);
272 if (has_thumb_ && !scaled_thumb_rect.IsEmpty()) {
273 thumb_resource_ = ScopedUIResource::Create(
274 layer_tree_host(),
275 RasterizeScrollbarPart(thumb_layer_rect, scaled_thumb_rect, THUMB));
278 // UI resources changed so push properties is needed.
279 SetNeedsPushProperties();
280 updated = true;
281 return updated;
284 UIResourceBitmap PaintedScrollbarLayer::RasterizeScrollbarPart(
285 const gfx::Rect& layer_rect,
286 const gfx::Rect& content_rect,
287 ScrollbarPart part) {
288 DCHECK(!content_rect.size().IsEmpty());
289 DCHECK(!layer_rect.size().IsEmpty());
291 SkBitmap skbitmap;
292 skbitmap.allocN32Pixels(content_rect.width(), content_rect.height());
293 SkCanvas skcanvas(skbitmap);
295 float scale_x =
296 content_rect.width() / static_cast<float>(layer_rect.width());
297 float scale_y =
298 content_rect.height() / static_cast<float>(layer_rect.height());
300 skcanvas.scale(SkFloatToScalar(scale_x),
301 SkFloatToScalar(scale_y));
302 skcanvas.translate(SkFloatToScalar(-layer_rect.x()),
303 SkFloatToScalar(-layer_rect.y()));
305 SkRect layer_skrect = RectToSkRect(layer_rect);
306 SkPaint paint;
307 paint.setAntiAlias(false);
308 paint.setXfermodeMode(SkXfermode::kClear_Mode);
309 skcanvas.drawRect(layer_skrect, paint);
310 skcanvas.clipRect(layer_skrect);
312 scrollbar_->PaintPart(&skcanvas, part, layer_rect);
313 // Make sure that the pixels are no longer mutable to unavoid unnecessary
314 // allocation and copying.
315 skbitmap.setImmutable();
317 return UIResourceBitmap(skbitmap);
320 } // namespace cc