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 != layer_tree_host()) |
| 114 thumb_resource_ = nullptr; |
| 115 |
| 116 Layer::SetLayerTreeHost(host); |
| 117 } |
| 118 |
| 119 gfx::Rect PaintedOverlayScrollbarLayer::OriginThumbRectForPainting() const { |
| 120 return gfx::Rect(gfx::Point(), scrollbar_->NinePatchThumbCanvasSize()); |
| 121 } |
| 122 |
| 123 bool PaintedOverlayScrollbarLayer::Update() { |
| 124 bool updated = false; |
| 125 updated |= Layer::Update(); |
| 126 |
| 127 DCHECK(scrollbar_->HasThumb()); |
| 128 DCHECK(scrollbar_->IsOverlay()); |
| 129 DCHECK(scrollbar_->UsesNinePatchThumbResource()); |
| 130 updated |= UpdateProperty(scrollbar_->TrackRect(), &track_rect_); |
| 131 updated |= UpdateProperty(scrollbar_->Location(), &location_); |
| 132 updated |= UpdateProperty(scrollbar_->ThumbThickness(), &thumb_thickness_); |
| 133 updated |= UpdateProperty(scrollbar_->ThumbLength(), &thumb_length_); |
| 134 updated |= PaintThumbIfNeeded(); |
| 135 |
| 136 return updated; |
| 137 } |
| 138 |
| 139 bool PaintedOverlayScrollbarLayer::PaintThumbIfNeeded() { |
| 140 if (!scrollbar_->NeedsPaintPart(THUMB)) |
| 141 return false; |
| 142 |
| 143 gfx::Rect paint_rect = OriginThumbRectForPainting(); |
| 144 aperture_ = scrollbar_->NinePatchThumbAperture(); |
| 145 |
| 146 DCHECK(!paint_rect.size().IsEmpty()); |
| 147 DCHECK(paint_rect.origin().IsOrigin()); |
| 148 |
| 149 SkBitmap skbitmap; |
| 150 skbitmap.allocN32Pixels(paint_rect.width(), paint_rect.height()); |
| 151 SkCanvas skcanvas(skbitmap); |
| 152 |
| 153 SkRect content_skrect = RectToSkRect(paint_rect); |
| 154 SkPaint paint; |
| 155 paint.setAntiAlias(false); |
| 156 paint.setBlendMode(SkBlendMode::kClear); |
| 157 skcanvas.drawRect(content_skrect, paint); |
| 158 skcanvas.clipRect(content_skrect); |
| 159 |
| 160 scrollbar_->PaintPart(&skcanvas, THUMB, paint_rect); |
| 161 // Make sure that the pixels are no longer mutable to unavoid unnecessary |
| 162 // allocation and copying. |
| 163 skbitmap.setImmutable(); |
| 164 |
| 165 thumb_resource_ = ScopedUIResource::Create( |
| 166 layer_tree_host()->GetUIResourceManager(), UIResourceBitmap(skbitmap)); |
| 167 |
| 168 SetNeedsPushProperties(); |
| 169 |
| 170 return true; |
| 171 } |
| 172 |
| 173 } // namespace cc |
OLD | NEW |