Chromium Code Reviews| 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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 } | 163 } |
| 164 | 164 |
| 165 ContentsScalingLayer::SetLayerTreeHost(host); | 165 ContentsScalingLayer::SetLayerTreeHost(host); |
| 166 } | 166 } |
| 167 | 167 |
| 168 gfx::Rect PaintedScrollbarLayer::ScrollbarLayerRectToContentRect( | 168 gfx::Rect PaintedScrollbarLayer::ScrollbarLayerRectToContentRect( |
| 169 const gfx::Rect& layer_rect) const { | 169 const gfx::Rect& layer_rect) const { |
| 170 // Don't intersect with the bounds as in LayerRectToContentRect() because | 170 // Don't intersect with the bounds as in LayerRectToContentRect() because |
| 171 // layer_rect here might be in coordinates of the containing layer. | 171 // layer_rect here might be in coordinates of the containing layer. |
| 172 gfx::Rect expanded_rect = gfx::ScaleToEnclosingRect( | 172 gfx::Rect expanded_rect = gfx::ScaleToEnclosingRect( |
| 173 layer_rect, contents_scale_y(), contents_scale_y()); | 173 layer_rect, contents_scale_x(), contents_scale_y()); |
| 174 // We should never return a rect bigger than the content_bounds(). | 174 // We should never return a rect bigger than the content_bounds(). |
| 175 gfx::Size clamped_size = expanded_rect.size(); | 175 gfx::Size clamped_size = expanded_rect.size(); |
| 176 clamped_size.SetToMin(content_bounds()); | 176 clamped_size.SetToMin(content_bounds()); |
| 177 expanded_rect.set_size(clamped_size); | 177 expanded_rect.set_size(clamped_size); |
| 178 return expanded_rect; | 178 return expanded_rect; |
| 179 } | 179 } |
| 180 | 180 |
| 181 gfx::Rect PaintedScrollbarLayer::OriginThumbRect() const { | 181 gfx::Rect PaintedScrollbarLayer::OriginThumbRect() const { |
| 182 gfx::Size thumb_size; | 182 gfx::Size thumb_size; |
| 183 if (orientation() == HORIZONTAL) { | 183 if (orientation() == HORIZONTAL) { |
| 184 thumb_size = | 184 thumb_size = |
| 185 gfx::Size(scrollbar_->ThumbLength(), scrollbar_->ThumbThickness()); | 185 gfx::Size(scrollbar_->ThumbLength(), scrollbar_->ThumbThickness()); |
| 186 } else { | 186 } else { |
| 187 thumb_size = | 187 thumb_size = |
| 188 gfx::Size(scrollbar_->ThumbThickness(), scrollbar_->ThumbLength()); | 188 gfx::Size(scrollbar_->ThumbThickness(), scrollbar_->ThumbLength()); |
| 189 } | 189 } |
| 190 return ScrollbarLayerRectToContentRect(gfx::Rect(thumb_size)); | 190 return gfx::Rect(thumb_size); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void PaintedScrollbarLayer::UpdateThumbAndTrackGeometry() { | 193 void PaintedScrollbarLayer::UpdateThumbAndTrackGeometry() { |
| 194 UpdateProperty(scrollbar_->TrackRect(), &track_rect_); | 194 UpdateProperty(scrollbar_->TrackRect(), &track_rect_); |
| 195 UpdateProperty(scrollbar_->Location(), &location_); | 195 UpdateProperty(scrollbar_->Location(), &location_); |
| 196 UpdateProperty(scrollbar_->IsOverlay(), &is_overlay_); | 196 UpdateProperty(scrollbar_->IsOverlay(), &is_overlay_); |
| 197 UpdateProperty(scrollbar_->HasThumb(), &has_thumb_); | 197 UpdateProperty(scrollbar_->HasThumb(), &has_thumb_); |
| 198 if (has_thumb_) { | 198 if (has_thumb_) { |
| 199 UpdateProperty(scrollbar_->ThumbThickness(), &thumb_thickness_); | 199 UpdateProperty(scrollbar_->ThumbThickness(), &thumb_thickness_); |
| 200 UpdateProperty(scrollbar_->ThumbLength(), &thumb_length_); | 200 UpdateProperty(scrollbar_->ThumbLength(), &thumb_length_); |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 | 203 |
| 204 bool PaintedScrollbarLayer::Update(ResourceUpdateQueue* queue, | 204 bool PaintedScrollbarLayer::Update(ResourceUpdateQueue* queue, |
| 205 const OcclusionTracker* occlusion) { | 205 const OcclusionTracker* occlusion) { |
| 206 UpdateThumbAndTrackGeometry(); | 206 UpdateThumbAndTrackGeometry(); |
| 207 | 207 |
| 208 gfx::Rect track_layer_rect = gfx::Rect(location_, bounds()); | |
| 208 gfx::Rect scaled_track_rect = ScrollbarLayerRectToContentRect( | 209 gfx::Rect scaled_track_rect = ScrollbarLayerRectToContentRect( |
| 209 gfx::Rect(location_, bounds())); | 210 track_layer_rect); |
| 210 | 211 |
| 211 if (track_rect_.IsEmpty() || scaled_track_rect.IsEmpty()) | 212 if (track_rect_.IsEmpty() || scaled_track_rect.IsEmpty()) |
| 212 return false; | 213 return false; |
| 213 | 214 |
| 214 { | 215 { |
| 215 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_, | 216 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_, |
| 216 true); | 217 true); |
| 217 ContentsScalingLayer::Update(queue, occlusion); | 218 ContentsScalingLayer::Update(queue, occlusion); |
| 218 } | 219 } |
| 219 | 220 |
| 220 if (update_rect_.IsEmpty() && track_resource_) | 221 if (update_rect_.IsEmpty() && track_resource_) |
| 221 return false; | 222 return false; |
| 222 | 223 |
| 223 track_resource_ = ScopedUIResource::Create( | 224 track_resource_ = ScopedUIResource::Create( |
| 224 layer_tree_host(), RasterizeScrollbarPart(scaled_track_rect, TRACK)); | 225 layer_tree_host(), |
| 226 RasterizeScrollbarPart(track_layer_rect, scaled_track_rect, TRACK)); | |
| 225 | 227 |
| 226 gfx::Rect thumb_rect = OriginThumbRect(); | 228 gfx::Rect thumb_layer_rect = OriginThumbRect(); |
| 227 if (has_thumb_ && !thumb_rect.IsEmpty()) { | 229 gfx::Rect scaled_thumb_rect = |
| 230 ScrollbarLayerRectToContentRect(thumb_layer_rect); | |
| 231 if (has_thumb_ && !scaled_thumb_rect.IsEmpty()) { | |
| 228 thumb_resource_ = ScopedUIResource::Create( | 232 thumb_resource_ = ScopedUIResource::Create( |
| 229 layer_tree_host(), RasterizeScrollbarPart(thumb_rect, THUMB)); | 233 layer_tree_host(), |
| 234 RasterizeScrollbarPart(thumb_layer_rect, scaled_thumb_rect, THUMB)); | |
| 230 } | 235 } |
| 231 | 236 |
| 232 // UI resources changed so push properties is needed. | 237 // UI resources changed so push properties is needed. |
| 233 SetNeedsPushProperties(); | 238 SetNeedsPushProperties(); |
| 234 return true; | 239 return true; |
| 235 } | 240 } |
| 236 | 241 |
| 242 float PaintedScrollbarLayer::calculateIdealScale( | |
|
danakj
2014/02/14 20:38:31
CalculateScaleToBitmap?
make this a file-static m
| |
| 243 float content_scale, | |
| 244 int layer_edge, | |
| 245 int layer_size, | |
| 246 int target_size) const { | |
|
danakj
2014/02/14 20:38:31
bitmap_size?
| |
| 247 const float EPSILLON = 0.1f; | |
|
danakj
2014/02/14 20:38:31
kEpsilon
| |
| 248 | |
| 249 DCHECK(layer_size); | |
| 250 DCHECK(target_size); | |
| 251 | |
| 252 // Because skia rounds to the nearest pixel when scaling, scaling by the | |
| 253 // content scale can lead to painting thinner than the bitmap rect (e.g. if | |
| 254 // the left edge gets rounded up and the right edge is rounded down). We | |
| 255 // calculate an "ideal scale" that will give us scale the layer rect to the | |
| 256 // same size as the bitmap. | |
| 257 | |
| 258 // Of course, due to the precission loss of floating point arithmetic, we'll | |
|
danakj
2014/02/14 20:38:31
precision
| |
| 259 // likely be off by some epsillon which leads to edge cases when we're | |
|
danakj
2014/02/14 20:38:31
epsilon
| |
| 260 // right around 0.5. To be safe, we adjust the scale by an epsillon depending | |
|
danakj
2014/02/14 20:38:31
epsilon
| |
| 261 // on which direction the leading edge was rounded in. This ensures we're | |
| 262 // always rounding both edges such that the width after rounding is exactly | |
| 263 // equal to the target width. | |
| 264 | |
| 265 float scaled_layer_edge = layer_edge * content_scale; | |
|
danakj
2014/02/14 20:38:31
Can you explain this bit here? I'm a little confus
bokan
2014/02/17 17:11:50
Sorry, I've been massively over-thinking this. The
| |
| 266 bool rounding_up = round(scaled_layer_edge) > scaled_layer_edge; | |
| 267 float adjusted_target_size = rounding_up ? target_size + EPSILLON | |
| 268 : target_size - EPSILLON; | |
| 269 | |
| 270 return adjusted_target_size / layer_size; | |
| 271 } | |
| 272 | |
| 237 UIResourceBitmap PaintedScrollbarLayer::RasterizeScrollbarPart( | 273 UIResourceBitmap PaintedScrollbarLayer::RasterizeScrollbarPart( |
| 238 const gfx::Rect& rect, | 274 const gfx::Rect& layer_rect, |
| 275 const gfx::Rect& content_rect, | |
| 239 ScrollbarPart part) { | 276 ScrollbarPart part) { |
| 240 DCHECK(!rect.size().IsEmpty()); | 277 DCHECK(!content_rect.size().IsEmpty()); |
| 278 DCHECK(!layer_rect.size().IsEmpty()); | |
| 241 | 279 |
| 242 SkBitmap skbitmap; | 280 SkBitmap skbitmap; |
| 243 skbitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height()); | 281 skbitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 282 content_rect.width(), | |
| 283 content_rect.height()); | |
| 244 skbitmap.allocPixels(); | 284 skbitmap.allocPixels(); |
| 245 | 285 |
| 246 SkCanvas skcanvas(skbitmap); | 286 SkCanvas skcanvas(skbitmap); |
| 247 skcanvas.translate(SkFloatToScalar(-rect.x()), SkFloatToScalar(-rect.y())); | |
| 248 skcanvas.scale(SkFloatToScalar(contents_scale_x()), | |
| 249 SkFloatToScalar(contents_scale_y())); | |
| 250 | 287 |
| 251 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect( | 288 float ideal_scale_x = calculateIdealScale(contents_scale_x(), |
| 252 rect, 1.f / contents_scale_x(), 1.f / contents_scale_y()); | 289 layer_rect.x(), |
| 290 layer_rect.width(), | |
| 291 content_rect.width()); | |
| 292 float ideal_scale_y = calculateIdealScale(contents_scale_y(), | |
| 293 layer_rect.y(), | |
| 294 layer_rect.height(), | |
| 295 content_rect.height()); | |
| 296 | |
| 297 // Skia rounds scaled coordinates. | |
| 298 float rounded_x = round(layer_rect.x() * ideal_scale_x); | |
| 299 float rounded_y = round(layer_rect.y() * ideal_scale_y); | |
| 300 skcanvas.translate(SkFloatToScalar(-rounded_x), SkFloatToScalar(-rounded_y)); | |
|
danakj
2014/02/14 20:38:31
Ok so, I'm not sure why you need this translate. T
bokan
2014/02/17 17:11:50
The PaintPart method for the track draws directly
| |
| 301 skcanvas.scale(SkFloatToScalar(ideal_scale_x), | |
| 302 SkFloatToScalar(ideal_scale_y)); | |
| 303 | |
| 253 SkRect layer_skrect = RectToSkRect(layer_rect); | 304 SkRect layer_skrect = RectToSkRect(layer_rect); |
| 254 SkPaint paint; | 305 SkPaint paint; |
| 255 paint.setAntiAlias(false); | 306 paint.setAntiAlias(false); |
| 256 paint.setXfermodeMode(SkXfermode::kClear_Mode); | 307 paint.setXfermodeMode(SkXfermode::kClear_Mode); |
| 257 skcanvas.drawRect(layer_skrect, paint); | 308 skcanvas.drawRect(layer_skrect, paint); |
| 258 skcanvas.clipRect(layer_skrect); | 309 skcanvas.clipRect(layer_skrect); |
| 259 | 310 |
| 260 scrollbar_->PaintPart(&skcanvas, part, layer_rect); | 311 scrollbar_->PaintPart(&skcanvas, part, layer_rect); |
| 261 // Make sure that the pixels are no longer mutable to unavoid unnecessary | 312 // Make sure that the pixels are no longer mutable to unavoid unnecessary |
| 262 // allocation and copying. | 313 // allocation and copying. |
| 263 skbitmap.setImmutable(); | 314 skbitmap.setImmutable(); |
| 264 | 315 |
| 265 return UIResourceBitmap(skbitmap); | 316 return UIResourceBitmap(skbitmap); |
| 266 } | 317 } |
| 267 | 318 |
| 268 } // namespace cc | 319 } // namespace cc |
| OLD | NEW |