Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Unified Diff: cc/layers/painted_scrollbar_layer.cc

Issue 150603004: Fixed rounding issue on scrollbar rasterization. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed Test Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cc/layers/painted_scrollbar_layer.h ('k') | cc/layers/scrollbar_layer_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/layers/painted_scrollbar_layer.cc
diff --git a/cc/layers/painted_scrollbar_layer.cc b/cc/layers/painted_scrollbar_layer.cc
index 71a37d48883f8f07d592ff561e288e344917ed65..8c2f84ca63e8a38d06ff6dd5c36217b61025cb19 100644
--- a/cc/layers/painted_scrollbar_layer.cc
+++ b/cc/layers/painted_scrollbar_layer.cc
@@ -170,7 +170,7 @@ gfx::Rect PaintedScrollbarLayer::ScrollbarLayerRectToContentRect(
// Don't intersect with the bounds as in LayerRectToContentRect() because
// layer_rect here might be in coordinates of the containing layer.
gfx::Rect expanded_rect = gfx::ScaleToEnclosingRect(
- layer_rect, contents_scale_y(), contents_scale_y());
+ layer_rect, contents_scale_x(), contents_scale_y());
// We should never return a rect bigger than the content_bounds().
gfx::Size clamped_size = expanded_rect.size();
clamped_size.SetToMin(content_bounds());
@@ -187,7 +187,7 @@ gfx::Rect PaintedScrollbarLayer::OriginThumbRect() const {
thumb_size =
gfx::Size(scrollbar_->ThumbThickness(), scrollbar_->ThumbLength());
}
- return ScrollbarLayerRectToContentRect(gfx::Rect(thumb_size));
+ return gfx::Rect(thumb_size);
}
void PaintedScrollbarLayer::UpdateThumbAndTrackGeometry() {
@@ -205,8 +205,9 @@ bool PaintedScrollbarLayer::Update(ResourceUpdateQueue* queue,
const OcclusionTracker* occlusion) {
UpdateThumbAndTrackGeometry();
+ gfx::Rect track_layer_rect = gfx::Rect(location_, bounds());
gfx::Rect scaled_track_rect = ScrollbarLayerRectToContentRect(
- gfx::Rect(location_, bounds()));
+ track_layer_rect);
if (track_rect_.IsEmpty() || scaled_track_rect.IsEmpty())
return false;
@@ -221,12 +222,16 @@ bool PaintedScrollbarLayer::Update(ResourceUpdateQueue* queue,
return false;
track_resource_ = ScopedUIResource::Create(
- layer_tree_host(), RasterizeScrollbarPart(scaled_track_rect, TRACK));
+ layer_tree_host(),
+ RasterizeScrollbarPart(track_layer_rect, scaled_track_rect, TRACK));
- gfx::Rect thumb_rect = OriginThumbRect();
- if (has_thumb_ && !thumb_rect.IsEmpty()) {
+ gfx::Rect thumb_layer_rect = OriginThumbRect();
+ gfx::Rect scaled_thumb_rect =
+ ScrollbarLayerRectToContentRect(thumb_layer_rect);
+ if (has_thumb_ && !scaled_thumb_rect.IsEmpty()) {
thumb_resource_ = ScopedUIResource::Create(
- layer_tree_host(), RasterizeScrollbarPart(thumb_rect, THUMB));
+ layer_tree_host(),
+ RasterizeScrollbarPart(thumb_layer_rect, scaled_thumb_rect, THUMB));
}
// UI resources changed so push properties is needed.
@@ -234,22 +239,68 @@ bool PaintedScrollbarLayer::Update(ResourceUpdateQueue* queue,
return true;
}
+float PaintedScrollbarLayer::calculateIdealScale(
danakj 2014/02/14 20:38:31 CalculateScaleToBitmap? make this a file-static m
+ float content_scale,
+ int layer_edge,
+ int layer_size,
+ int target_size) const {
danakj 2014/02/14 20:38:31 bitmap_size?
+ const float EPSILLON = 0.1f;
danakj 2014/02/14 20:38:31 kEpsilon
+
+ DCHECK(layer_size);
+ DCHECK(target_size);
+
+ // Because skia rounds to the nearest pixel when scaling, scaling by the
+ // content scale can lead to painting thinner than the bitmap rect (e.g. if
+ // the left edge gets rounded up and the right edge is rounded down). We
+ // calculate an "ideal scale" that will give us scale the layer rect to the
+ // same size as the bitmap.
+
+ // Of course, due to the precission loss of floating point arithmetic, we'll
danakj 2014/02/14 20:38:31 precision
+ // likely be off by some epsillon which leads to edge cases when we're
danakj 2014/02/14 20:38:31 epsilon
+ // right around 0.5. To be safe, we adjust the scale by an epsillon depending
danakj 2014/02/14 20:38:31 epsilon
+ // on which direction the leading edge was rounded in. This ensures we're
+ // always rounding both edges such that the width after rounding is exactly
+ // equal to the target width.
+
+ 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
+ bool rounding_up = round(scaled_layer_edge) > scaled_layer_edge;
+ float adjusted_target_size = rounding_up ? target_size + EPSILLON
+ : target_size - EPSILLON;
+
+ return adjusted_target_size / layer_size;
+}
+
UIResourceBitmap PaintedScrollbarLayer::RasterizeScrollbarPart(
- const gfx::Rect& rect,
+ const gfx::Rect& layer_rect,
+ const gfx::Rect& content_rect,
ScrollbarPart part) {
- DCHECK(!rect.size().IsEmpty());
+ DCHECK(!content_rect.size().IsEmpty());
+ DCHECK(!layer_rect.size().IsEmpty());
SkBitmap skbitmap;
- skbitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height());
+ skbitmap.setConfig(SkBitmap::kARGB_8888_Config,
+ content_rect.width(),
+ content_rect.height());
skbitmap.allocPixels();
SkCanvas skcanvas(skbitmap);
- skcanvas.translate(SkFloatToScalar(-rect.x()), SkFloatToScalar(-rect.y()));
- skcanvas.scale(SkFloatToScalar(contents_scale_x()),
- SkFloatToScalar(contents_scale_y()));
- gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
- rect, 1.f / contents_scale_x(), 1.f / contents_scale_y());
+ float ideal_scale_x = calculateIdealScale(contents_scale_x(),
+ layer_rect.x(),
+ layer_rect.width(),
+ content_rect.width());
+ float ideal_scale_y = calculateIdealScale(contents_scale_y(),
+ layer_rect.y(),
+ layer_rect.height(),
+ content_rect.height());
+
+ // Skia rounds scaled coordinates.
+ float rounded_x = round(layer_rect.x() * ideal_scale_x);
+ float rounded_y = round(layer_rect.y() * ideal_scale_y);
+ 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
+ skcanvas.scale(SkFloatToScalar(ideal_scale_x),
+ SkFloatToScalar(ideal_scale_y));
+
SkRect layer_skrect = RectToSkRect(layer_rect);
SkPaint paint;
paint.setAntiAlias(false);
« no previous file with comments | « cc/layers/painted_scrollbar_layer.h ('k') | cc/layers/scrollbar_layer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698