OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/layers/painted_scrollbar_layer.h" | 5 #include "cc/layers/painted_scrollbar_layer.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "cc/layers/painted_scrollbar_layer_impl.h" | 10 #include "cc/layers/painted_scrollbar_layer_impl.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 scoped_ptr<Scrollbar> scrollbar, | 37 scoped_ptr<Scrollbar> scrollbar, |
38 int scroll_layer_id) | 38 int scroll_layer_id) |
39 : scrollbar_(scrollbar.Pass()), | 39 : scrollbar_(scrollbar.Pass()), |
40 scroll_layer_id_(scroll_layer_id) { | 40 scroll_layer_id_(scroll_layer_id) { |
41 if (!scrollbar_->IsOverlay()) | 41 if (!scrollbar_->IsOverlay()) |
42 SetShouldScrollOnMainThread(true); | 42 SetShouldScrollOnMainThread(true); |
43 } | 43 } |
44 | 44 |
45 PaintedScrollbarLayer::~PaintedScrollbarLayer() {} | 45 PaintedScrollbarLayer::~PaintedScrollbarLayer() {} |
46 | 46 |
| 47 int PaintedScrollbarLayer::ScrollLayerId() const { |
| 48 return scroll_layer_id_; |
| 49 } |
| 50 |
47 void PaintedScrollbarLayer::SetScrollLayerId(int id) { | 51 void PaintedScrollbarLayer::SetScrollLayerId(int id) { |
48 if (id == scroll_layer_id_) | 52 if (id == scroll_layer_id_) |
49 return; | 53 return; |
50 | 54 |
51 scroll_layer_id_ = id; | 55 scroll_layer_id_ = id; |
52 SetNeedsFullTreeSync(); | 56 SetNeedsFullTreeSync(); |
53 } | 57 } |
54 | 58 |
55 bool PaintedScrollbarLayer::OpacityCanAnimateOnImplThread() const { | 59 bool PaintedScrollbarLayer::OpacityCanAnimateOnImplThread() const { |
56 return scrollbar_->IsOverlay(); | 60 return scrollbar_->IsOverlay(); |
57 } | 61 } |
58 | 62 |
59 ScrollbarOrientation PaintedScrollbarLayer::Orientation() const { | 63 ScrollbarOrientation PaintedScrollbarLayer::orientation() const { |
60 return scrollbar_->Orientation(); | 64 return scrollbar_->Orientation(); |
61 } | 65 } |
62 | 66 |
63 int PaintedScrollbarLayer::MaxTextureSize() { | 67 int PaintedScrollbarLayer::MaxTextureSize() { |
64 DCHECK(layer_tree_host()); | 68 DCHECK(layer_tree_host()); |
65 return layer_tree_host()->GetRendererCapabilities().max_texture_size; | 69 return layer_tree_host()->GetRendererCapabilities().max_texture_size; |
66 } | 70 } |
67 | 71 |
68 float PaintedScrollbarLayer::ClampScaleToMaxTextureSize(float scale) { | 72 float PaintedScrollbarLayer::ClampScaleToMaxTextureSize(float scale) { |
69 if (layer_tree_host()->settings().solid_color_scrollbars) | |
70 return scale; | |
71 | |
72 // If the scaled content_bounds() is bigger than the max texture size of the | 73 // If the scaled content_bounds() is bigger than the max texture size of the |
73 // device, we need to clamp it by rescaling, since content_bounds() is used | 74 // device, we need to clamp it by rescaling, since content_bounds() is used |
74 // below to set the texture size. | 75 // below to set the texture size. |
75 gfx::Size scaled_bounds = ComputeContentBoundsForScale(scale, scale); | 76 gfx::Size scaled_bounds = ComputeContentBoundsForScale(scale, scale); |
76 if (scaled_bounds.width() > MaxTextureSize() || | 77 if (scaled_bounds.width() > MaxTextureSize() || |
77 scaled_bounds.height() > MaxTextureSize()) { | 78 scaled_bounds.height() > MaxTextureSize()) { |
78 if (scaled_bounds.width() > scaled_bounds.height()) | 79 if (scaled_bounds.width() > scaled_bounds.height()) |
79 return (MaxTextureSize() - 1) / static_cast<float>(bounds().width()); | 80 return (MaxTextureSize() - 1) / static_cast<float>(bounds().width()); |
80 else | 81 else |
81 return (MaxTextureSize() - 1) / static_cast<float>(bounds().height()); | 82 return (MaxTextureSize() - 1) / static_cast<float>(bounds().height()); |
(...skipping 18 matching lines...) Expand all Loading... |
100 contents_scale_y, | 101 contents_scale_y, |
101 content_bounds); | 102 content_bounds); |
102 } | 103 } |
103 | 104 |
104 void PaintedScrollbarLayer::PushPropertiesTo(LayerImpl* layer) { | 105 void PaintedScrollbarLayer::PushPropertiesTo(LayerImpl* layer) { |
105 ContentsScalingLayer::PushPropertiesTo(layer); | 106 ContentsScalingLayer::PushPropertiesTo(layer); |
106 | 107 |
107 PaintedScrollbarLayerImpl* scrollbar_layer = | 108 PaintedScrollbarLayerImpl* scrollbar_layer = |
108 static_cast<PaintedScrollbarLayerImpl*>(layer); | 109 static_cast<PaintedScrollbarLayerImpl*>(layer); |
109 | 110 |
110 if (layer_tree_host() && | 111 scrollbar_layer->SetThumbThickness(thumb_thickness_); |
111 layer_tree_host()->settings().solid_color_scrollbars) { | |
112 scrollbar_layer->SetThumbThickness(scrollbar_->ThumbThickness()); | |
113 } else { | |
114 scrollbar_layer->SetThumbThickness(thumb_thickness_); | |
115 } | |
116 scrollbar_layer->SetThumbLength(thumb_length_); | 112 scrollbar_layer->SetThumbLength(thumb_length_); |
117 if (Orientation() == HORIZONTAL) { | 113 if (orientation() == HORIZONTAL) { |
118 scrollbar_layer->SetTrackStart( | 114 scrollbar_layer->SetTrackStart( |
119 track_rect_.x() - scrollbar_->Location().x()); | 115 track_rect_.x() - scrollbar_->Location().x()); |
120 scrollbar_layer->SetTrackLength(track_rect_.width()); | 116 scrollbar_layer->SetTrackLength(track_rect_.width()); |
121 } else { | 117 } else { |
122 scrollbar_layer->SetTrackStart( | 118 scrollbar_layer->SetTrackStart( |
123 track_rect_.y() - scrollbar_->Location().y()); | 119 track_rect_.y() - scrollbar_->Location().y()); |
124 scrollbar_layer->SetTrackLength(track_rect_.height()); | 120 scrollbar_layer->SetTrackLength(track_rect_.height()); |
125 } | 121 } |
126 | 122 |
127 if (track_resource_.get()) | 123 if (track_resource_.get()) |
128 scrollbar_layer->set_track_ui_resource_id(track_resource_->id()); | 124 scrollbar_layer->set_track_ui_resource_id(track_resource_->id()); |
129 if (thumb_resource_.get()) | 125 if (thumb_resource_.get()) |
130 scrollbar_layer->set_thumb_ui_resource_id(thumb_resource_->id()); | 126 scrollbar_layer->set_thumb_ui_resource_id(thumb_resource_->id()); |
131 | 127 |
132 scrollbar_layer->set_is_overlay_scrollbar(scrollbar_->IsOverlay()); | 128 scrollbar_layer->set_is_overlay_scrollbar(scrollbar_->IsOverlay()); |
133 | 129 |
134 // PaintedScrollbarLayer must push properties every frame. crbug.com/259095 | 130 // PaintedScrollbarLayer must push properties every frame. crbug.com/259095 |
135 needs_push_properties_ = true; | 131 needs_push_properties_ = true; |
136 } | 132 } |
137 | 133 |
138 PaintedScrollbarLayer* PaintedScrollbarLayer::ToScrollbarLayer() { | 134 ScrollbarLayerInterface* PaintedScrollbarLayer::ToScrollbarLayer() { |
139 return this; | 135 return this; |
140 } | 136 } |
141 | 137 |
142 void PaintedScrollbarLayer::SetLayerTreeHost(LayerTreeHost* host) { | 138 void PaintedScrollbarLayer::SetLayerTreeHost(LayerTreeHost* host) { |
143 // When the LTH is set to null or has changed, then this layer should remove | 139 // When the LTH is set to null or has changed, then this layer should remove |
144 // all of its associated resources. | 140 // all of its associated resources. |
145 if (!host || host != layer_tree_host()) { | 141 if (!host || host != layer_tree_host()) { |
146 track_resource_.reset(); | 142 track_resource_.reset(); |
147 thumb_resource_.reset(); | 143 thumb_resource_.reset(); |
148 } | 144 } |
149 | 145 |
150 ContentsScalingLayer::SetLayerTreeHost(host); | 146 ContentsScalingLayer::SetLayerTreeHost(host); |
151 } | 147 } |
152 | 148 |
153 gfx::Rect PaintedScrollbarLayer::ScrollbarLayerRectToContentRect( | 149 gfx::Rect PaintedScrollbarLayer::ScrollbarLayerRectToContentRect( |
154 gfx::Rect layer_rect) const { | 150 gfx::Rect layer_rect) const { |
155 // Don't intersect with the bounds as in LayerRectToContentRect() because | 151 // Don't intersect with the bounds as in LayerRectToContentRect() because |
156 // layer_rect here might be in coordinates of the containing layer. | 152 // layer_rect here might be in coordinates of the containing layer. |
157 gfx::Rect expanded_rect = gfx::ScaleToEnclosingRect( | 153 gfx::Rect expanded_rect = gfx::ScaleToEnclosingRect( |
158 layer_rect, contents_scale_y(), contents_scale_y()); | 154 layer_rect, contents_scale_y(), contents_scale_y()); |
159 // We should never return a rect bigger than the content_bounds(). | 155 // We should never return a rect bigger than the content_bounds(). |
160 gfx::Size clamped_size = expanded_rect.size(); | 156 gfx::Size clamped_size = expanded_rect.size(); |
161 clamped_size.SetToMin(content_bounds()); | 157 clamped_size.SetToMin(content_bounds()); |
162 expanded_rect.set_size(clamped_size); | 158 expanded_rect.set_size(clamped_size); |
163 return expanded_rect; | 159 return expanded_rect; |
164 } | 160 } |
165 | 161 |
166 gfx::Rect PaintedScrollbarLayer::OriginThumbRect() const { | 162 gfx::Rect PaintedScrollbarLayer::OriginThumbRect() const { |
167 gfx::Size thumb_size; | 163 gfx::Size thumb_size; |
168 if (Orientation() == HORIZONTAL) { | 164 if (orientation() == HORIZONTAL) { |
169 thumb_size = | 165 thumb_size = |
170 gfx::Size(scrollbar_->ThumbLength(), scrollbar_->ThumbThickness()); | 166 gfx::Size(scrollbar_->ThumbLength(), scrollbar_->ThumbThickness()); |
171 } else { | 167 } else { |
172 thumb_size = | 168 thumb_size = |
173 gfx::Size(scrollbar_->ThumbThickness(), scrollbar_->ThumbLength()); | 169 gfx::Size(scrollbar_->ThumbThickness(), scrollbar_->ThumbLength()); |
174 } | 170 } |
175 return ScrollbarLayerRectToContentRect(gfx::Rect(thumb_size)); | 171 return ScrollbarLayerRectToContentRect(gfx::Rect(thumb_size)); |
176 } | 172 } |
177 | 173 |
178 void PaintedScrollbarLayer::UpdateThumbAndTrackGeometry() { | 174 void PaintedScrollbarLayer::UpdateThumbAndTrackGeometry() { |
179 track_rect_ = scrollbar_->TrackRect(); | 175 track_rect_ = scrollbar_->TrackRect(); |
180 if (scrollbar_->HasThumb()) { | 176 if (scrollbar_->HasThumb()) { |
181 thumb_thickness_ = scrollbar_->ThumbThickness(); | 177 thumb_thickness_ = scrollbar_->ThumbThickness(); |
182 thumb_length_ = scrollbar_->ThumbLength(); | 178 thumb_length_ = scrollbar_->ThumbLength(); |
183 } | 179 } |
184 } | 180 } |
185 | 181 |
186 bool PaintedScrollbarLayer::Update(ResourceUpdateQueue* queue, | 182 bool PaintedScrollbarLayer::Update(ResourceUpdateQueue* queue, |
187 const OcclusionTracker* occlusion) { | 183 const OcclusionTracker* occlusion) { |
188 UpdateThumbAndTrackGeometry(); | 184 UpdateThumbAndTrackGeometry(); |
189 | 185 |
190 gfx::Rect scaled_track_rect = ScrollbarLayerRectToContentRect( | 186 gfx::Rect scaled_track_rect = ScrollbarLayerRectToContentRect( |
191 gfx::Rect(scrollbar_->Location(), bounds())); | 187 gfx::Rect(scrollbar_->Location(), bounds())); |
192 | 188 |
193 if (layer_tree_host()->settings().solid_color_scrollbars || | 189 if (track_rect_.IsEmpty() || scaled_track_rect.IsEmpty()) |
194 track_rect_.IsEmpty() || scaled_track_rect.IsEmpty()) | |
195 return false; | 190 return false; |
196 | 191 |
197 { | 192 { |
198 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_, | 193 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_, |
199 true); | 194 true); |
200 ContentsScalingLayer::Update(queue, occlusion); | 195 ContentsScalingLayer::Update(queue, occlusion); |
201 } | 196 } |
202 | 197 |
203 track_resource_ = ScopedUIResource::Create( | 198 track_resource_ = ScopedUIResource::Create( |
204 layer_tree_host(), RasterizeScrollbarPart(scaled_track_rect, TRACK)); | 199 layer_tree_host(), RasterizeScrollbarPart(scaled_track_rect, TRACK)); |
205 gfx::Rect thumb_rect = OriginThumbRect(); | 200 gfx::Rect thumb_rect = OriginThumbRect(); |
206 | 201 |
207 if (scrollbar_->HasThumb() && !thumb_rect.IsEmpty()) { | 202 if (scrollbar_->HasThumb() && !thumb_rect.IsEmpty()) { |
208 thumb_resource_ = ScopedUIResource::Create( | 203 thumb_resource_ = ScopedUIResource::Create( |
209 layer_tree_host(), RasterizeScrollbarPart(thumb_rect, THUMB)); | 204 layer_tree_host(), RasterizeScrollbarPart(thumb_rect, THUMB)); |
210 } | 205 } |
211 | 206 |
212 return true; | 207 return true; |
213 } | 208 } |
214 | 209 |
215 scoped_refptr<UIResourceBitmap> PaintedScrollbarLayer::RasterizeScrollbarPart( | 210 scoped_refptr<UIResourceBitmap> PaintedScrollbarLayer::RasterizeScrollbarPart( |
216 gfx::Rect rect, | 211 gfx::Rect rect, |
217 ScrollbarPart part) { | 212 ScrollbarPart part) { |
218 DCHECK(!layer_tree_host()->settings().solid_color_scrollbars); | |
219 DCHECK(!rect.size().IsEmpty()); | 213 DCHECK(!rect.size().IsEmpty()); |
220 | 214 |
221 scoped_refptr<UIResourceBitmap> bitmap = | 215 scoped_refptr<UIResourceBitmap> bitmap = |
222 UIResourceBitmap::Create(new uint8_t[rect.width() * rect.height() * 4], | 216 UIResourceBitmap::Create(new uint8_t[rect.width() * rect.height() * 4], |
223 UIResourceBitmap::RGBA8, | 217 UIResourceBitmap::RGBA8, |
224 rect.size()); | 218 rect.size()); |
225 | 219 |
226 SkBitmap skbitmap; | 220 SkBitmap skbitmap; |
227 skbitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height()); | 221 skbitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height()); |
228 skbitmap.setPixels(bitmap->GetPixels()); | 222 skbitmap.setPixels(bitmap->GetPixels()); |
(...skipping 11 matching lines...) Expand all Loading... |
240 paint.setXfermodeMode(SkXfermode::kClear_Mode); | 234 paint.setXfermodeMode(SkXfermode::kClear_Mode); |
241 skcanvas.drawRect(layer_skrect, paint); | 235 skcanvas.drawRect(layer_skrect, paint); |
242 skcanvas.clipRect(layer_skrect); | 236 skcanvas.clipRect(layer_skrect); |
243 | 237 |
244 scrollbar_->PaintPart(&skcanvas, part, layer_rect); | 238 scrollbar_->PaintPart(&skcanvas, part, layer_rect); |
245 | 239 |
246 return bitmap; | 240 return bitmap; |
247 } | 241 } |
248 | 242 |
249 } // namespace cc | 243 } // namespace cc |
OLD | NEW |