OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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. | |
4 | |
5 #include "cc/layers/painted_overlay_scrollbar_layer.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/auto_reset.h" | |
10 #include "cc/base/math_util.h" | |
11 #include "cc/layers/painted_overlay_scrollbar_layer_impl.h" | |
12 #include "cc/resources/ui_resource_bitmap.h" | |
13 #include "cc/resources/ui_resource_manager.h" | |
14 #include "cc/trees/layer_tree_host.h" | |
15 #include "cc/trees/layer_tree_impl.h" | |
16 #include "skia/ext/platform_canvas.h" | |
17 #include "third_party/skia/include/core/SkBitmap.h" | |
18 #include "third_party/skia/include/core/SkCanvas.h" | |
19 #include "third_party/skia/include/core/SkSize.h" | |
20 #include "ui/gfx/geometry/size_conversions.h" | |
21 #include "ui/gfx/skia_util.h" | |
22 | |
23 namespace cc { | |
24 | |
25 std::unique_ptr<LayerImpl> PaintedOverlayScrollbarLayer::CreateLayerImpl( | |
26 LayerTreeImpl* tree_impl) { | |
27 return PaintedOverlayScrollbarLayerImpl::Create( | |
28 tree_impl, id(), scrollbar_->Orientation(), | |
29 scrollbar_->IsLeftSideVerticalScrollbar()); | |
30 } | |
31 | |
32 scoped_refptr<PaintedOverlayScrollbarLayer> | |
33 PaintedOverlayScrollbarLayer::Create(std::unique_ptr<Scrollbar> scrollbar, | |
34 int scroll_layer_id) { | |
35 return make_scoped_refptr( | |
36 new PaintedOverlayScrollbarLayer(std::move(scrollbar), scroll_layer_id)); | |
37 } | |
38 | |
39 PaintedOverlayScrollbarLayer::PaintedOverlayScrollbarLayer( | |
40 std::unique_ptr<Scrollbar> scrollbar, | |
41 int scroll_layer_id) | |
42 : scrollbar_(std::move(scrollbar)), | |
43 scroll_layer_id_(scroll_layer_id), | |
44 thumb_thickness_(scrollbar_->ThumbThickness()), | |
45 thumb_length_(scrollbar_->ThumbLength()) { | |
46 DCHECK(scrollbar_->UsesNinePatchThumbResource()); | |
47 } | |
48 | |
49 PaintedOverlayScrollbarLayer::~PaintedOverlayScrollbarLayer() {} | |
50 | |
51 int PaintedOverlayScrollbarLayer::ScrollLayerId() const { | |
52 return scroll_layer_id_; | |
53 } | |
54 | |
55 void PaintedOverlayScrollbarLayer::SetScrollLayer(int layer_id) { | |
56 if (layer_id == scroll_layer_id_) | |
57 return; | |
58 | |
59 scroll_layer_id_ = layer_id; | |
60 SetNeedsFullTreeSync(); | |
61 } | |
62 | |
63 bool PaintedOverlayScrollbarLayer::OpacityCanAnimateOnImplThread() const { | |
64 return scrollbar_->IsOverlay(); | |
65 } | |
66 | |
67 bool PaintedOverlayScrollbarLayer::AlwaysUseActiveTreeOpacity() const { | |
68 return true; | |
69 } | |
70 | |
71 ScrollbarOrientation PaintedOverlayScrollbarLayer::orientation() const { | |
72 return scrollbar_->Orientation(); | |
73 } | |
74 | |
75 void PaintedOverlayScrollbarLayer::PushPropertiesTo(LayerImpl* layer) { | |
76 Layer::PushPropertiesTo(layer); | |
77 | |
78 PaintedOverlayScrollbarLayerImpl* scrollbar_layer = | |
79 static_cast<PaintedOverlayScrollbarLayerImpl*>(layer); | |
80 | |
81 scrollbar_layer->SetScrollLayerId(scroll_layer_id_); | |
82 | |
83 scrollbar_layer->SetThumbThickness(thumb_thickness_); | |
84 scrollbar_layer->SetThumbLength(thumb_length_); | |
85 if (orientation() == HORIZONTAL) { | |
86 scrollbar_layer->SetTrackStart(track_rect_.x() - location_.x()); | |
87 scrollbar_layer->SetTrackLength(track_rect_.width()); | |
88 } else { | |
89 scrollbar_layer->SetTrackStart(track_rect_.y() - location_.y()); | |
90 scrollbar_layer->SetTrackLength(track_rect_.height()); | |
91 } | |
92 | |
93 if (thumb_resource_.get()) { | |
94 scrollbar_layer->SetImageBounds( | |
95 layer_tree_host()->GetUIResourceManager()->GetUIResourceSize( | |
96 thumb_resource_->id())); | |
97 scrollbar_layer->SetAperture(aperture_); | |
98 scrollbar_layer->set_thumb_ui_resource_id(thumb_resource_->id()); | |
99 } else { | |
100 scrollbar_layer->SetImageBounds(gfx::Size()); | |
101 scrollbar_layer->SetAperture(gfx::Rect()); | |
102 scrollbar_layer->set_thumb_ui_resource_id(0); | |
103 } | |
104 } | |
105 | |
106 ScrollbarLayerInterface* PaintedOverlayScrollbarLayer::ToScrollbarLayer() { | |
107 return this; | |
108 } | |
109 | |
110 void PaintedOverlayScrollbarLayer::SetLayerTreeHost(LayerTreeHost* host) { | |
111 // When the LTH is set to null or has changed, then this layer should remove | |
112 // all of its associated resources. | |
113 if (!host || host != layer_tree_host()) { | |
aelias_OOO_until_Jul13
2017/02/28 23:23:51
Nit: "!host ||" is no-op, can be removed.
bokan
2017/03/01 00:44:21
Done.
| |
114 thumb_resource_ = nullptr; | |
115 } | |
aelias_OOO_until_Jul13
2017/02/28 23:23:51
nit: no {}
bokan
2017/03/01 00:44:21
Done.
| |
116 | |
117 Layer::SetLayerTreeHost(host); | |
118 } | |
119 | |
120 gfx::Rect PaintedOverlayScrollbarLayer::OriginThumbRectForPainting() const { | |
121 return gfx::Rect(gfx::Point(), scrollbar_->NinePatchThumbCanvasSize()); | |
122 } | |
123 | |
124 bool PaintedOverlayScrollbarLayer::Update() { | |
125 bool updated = false; | |
126 { | |
127 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_, | |
aelias_OOO_until_Jul13
2017/02/28 23:23:51
It looks like this is copied from PaintedScrollbar
bokan
2017/03/01 00:44:21
This flag was added to Layer in: https://chromium.
enne (OOO)
2017/03/01 00:52:20
If you're in the middle of an Update before a comm
| |
128 true); | |
129 updated |= Layer::Update(); | |
130 } | |
131 | |
132 DCHECK(scrollbar_->HasThumb()); | |
133 DCHECK(scrollbar_->IsOverlay()); | |
134 DCHECK(scrollbar_->UsesNinePatchThumbResource()); | |
135 updated |= UpdateProperty(scrollbar_->TrackRect(), &track_rect_); | |
136 updated |= UpdateProperty(scrollbar_->Location(), &location_); | |
137 updated |= UpdateProperty(scrollbar_->ThumbThickness(), &thumb_thickness_); | |
138 updated |= UpdateProperty(scrollbar_->ThumbLength(), &thumb_length_); | |
139 updated |= PaintThumbIfNeeded(); | |
140 | |
141 return updated; | |
142 } | |
143 | |
144 bool PaintedOverlayScrollbarLayer::PaintThumbIfNeeded() { | |
145 if (!scrollbar_->NeedsPaintPart(THUMB)) | |
146 return false; | |
147 | |
148 gfx::Rect paint_rect = OriginThumbRectForPainting(); | |
149 aperture_ = scrollbar_->NinePatchThumbAperture(); | |
150 | |
151 DCHECK(!paint_rect.size().IsEmpty()); | |
152 DCHECK(paint_rect.origin().IsOrigin()); | |
153 | |
154 SkBitmap skbitmap; | |
155 skbitmap.allocN32Pixels(paint_rect.width(), paint_rect.height()); | |
156 SkCanvas skcanvas(skbitmap); | |
157 | |
158 SkRect content_skrect = RectToSkRect(paint_rect); | |
159 SkPaint paint; | |
160 paint.setAntiAlias(false); | |
161 paint.setBlendMode(SkBlendMode::kClear); | |
162 skcanvas.drawRect(content_skrect, paint); | |
163 skcanvas.clipRect(content_skrect); | |
164 | |
165 scrollbar_->PaintPart(&skcanvas, THUMB, paint_rect); | |
166 // Make sure that the pixels are no longer mutable to unavoid unnecessary | |
167 // allocation and copying. | |
168 skbitmap.setImmutable(); | |
169 | |
170 thumb_resource_ = ScopedUIResource::Create( | |
171 layer_tree_host()->GetUIResourceManager(), UIResourceBitmap(skbitmap)); | |
172 | |
173 SetNeedsPushProperties(); | |
174 | |
175 return true; | |
176 } | |
177 | |
178 } // namespace cc | |
OLD | NEW |