| 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 |
| 237 UIResourceBitmap PaintedScrollbarLayer::RasterizeScrollbarPart( | 242 UIResourceBitmap PaintedScrollbarLayer::RasterizeScrollbarPart( |
| 243 const gfx::Rect& layer_rect, |
| 238 const gfx::Rect& rect, | 244 const gfx::Rect& rect, |
| 239 ScrollbarPart part) { | 245 ScrollbarPart part) { |
| 240 DCHECK(!rect.size().IsEmpty()); | 246 DCHECK(!rect.size().IsEmpty()); |
| 247 DCHECK(!layer_rect.size().IsEmpty()); |
| 241 | 248 |
| 242 SkBitmap skbitmap; | 249 SkBitmap skbitmap; |
| 243 skbitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height()); | 250 skbitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height()); |
| 244 skbitmap.allocPixels(); | 251 skbitmap.allocPixels(); |
| 245 | 252 |
| 246 SkCanvas skcanvas(skbitmap); | 253 SkCanvas skcanvas(skbitmap); |
| 247 skcanvas.translate(SkFloatToScalar(-rect.x()), SkFloatToScalar(-rect.y())); | 254 |
| 255 // Skia will round to the nearest pixel so we need to translate by the |
| 256 // rounded origin coordinates |
| 257 float roundedX = round(layer_rect.x() * contents_scale_x()); |
| 258 float roundedY = round(layer_rect.y() * contents_scale_y()); |
| 259 skcanvas.translate(SkFloatToScalar(-roundedX), SkFloatToScalar(-roundedY)); |
| 248 skcanvas.scale(SkFloatToScalar(contents_scale_x()), | 260 skcanvas.scale(SkFloatToScalar(contents_scale_x()), |
| 249 SkFloatToScalar(contents_scale_y())); | 261 SkFloatToScalar(contents_scale_y())); |
| 250 | 262 |
| 251 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect( | |
| 252 rect, 1.f / contents_scale_x(), 1.f / contents_scale_y()); | |
| 253 SkRect layer_skrect = RectToSkRect(layer_rect); | 263 SkRect layer_skrect = RectToSkRect(layer_rect); |
| 254 SkPaint paint; | 264 SkPaint paint; |
| 255 paint.setAntiAlias(false); | 265 paint.setAntiAlias(false); |
| 256 paint.setXfermodeMode(SkXfermode::kClear_Mode); | 266 paint.setXfermodeMode(SkXfermode::kClear_Mode); |
| 257 skcanvas.drawRect(layer_skrect, paint); | 267 skcanvas.drawRect(layer_skrect, paint); |
| 258 skcanvas.clipRect(layer_skrect); | 268 skcanvas.clipRect(layer_skrect); |
| 259 | 269 |
| 260 scrollbar_->PaintPart(&skcanvas, part, layer_rect); | 270 scrollbar_->PaintPart(&skcanvas, part, layer_rect); |
| 261 // Make sure that the pixels are no longer mutable to unavoid unnecessary | 271 // Make sure that the pixels are no longer mutable to unavoid unnecessary |
| 262 // allocation and copying. | 272 // allocation and copying. |
| 263 skbitmap.setImmutable(); | 273 skbitmap.setImmutable(); |
| 264 | 274 |
| 265 return UIResourceBitmap(skbitmap); | 275 return UIResourceBitmap(skbitmap); |
| 266 } | 276 } |
| 267 | 277 |
| 268 } // namespace cc | 278 } // namespace cc |
| OLD | NEW |