painted_scrollbar_layer.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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
  3. // found in the LICENSE file.
  4. #include "cc/layers/painted_scrollbar_layer.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <utility>
  8. #include "base/auto_reset.h"
  9. #include "cc/layers/painted_scrollbar_layer_impl.h"
  10. #include "cc/paint/skia_paint_canvas.h"
  11. #include "cc/trees/draw_property_utils.h"
  12. #include "cc/trees/layer_tree_host.h"
  13. #include "third_party/skia/include/core/SkBitmap.h"
  14. #include "ui/gfx/geometry/transform_util.h"
  15. namespace cc {
  16. std::unique_ptr<LayerImpl> PaintedScrollbarLayer::CreateLayerImpl(
  17. LayerTreeImpl* tree_impl) const {
  18. return PaintedScrollbarLayerImpl::Create(tree_impl, id(), orientation(),
  19. is_left_side_vertical_scrollbar(),
  20. is_overlay_);
  21. }
  22. scoped_refptr<PaintedScrollbarLayer> PaintedScrollbarLayer::CreateOrReuse(
  23. scoped_refptr<Scrollbar> scrollbar,
  24. PaintedScrollbarLayer* existing_layer) {
  25. if (existing_layer &&
  26. existing_layer->scrollbar_.Read(*existing_layer)->IsSame(*scrollbar))
  27. return existing_layer;
  28. return Create(std::move(scrollbar));
  29. }
  30. scoped_refptr<PaintedScrollbarLayer> PaintedScrollbarLayer::Create(
  31. scoped_refptr<Scrollbar> scrollbar) {
  32. return base::WrapRefCounted(new PaintedScrollbarLayer(std::move(scrollbar)));
  33. }
  34. PaintedScrollbarLayer::PaintedScrollbarLayer(scoped_refptr<Scrollbar> scrollbar)
  35. : ScrollbarLayerBase(scrollbar->Orientation(),
  36. scrollbar->IsLeftSideVerticalScrollbar()),
  37. scrollbar_(std::move(scrollbar)),
  38. internal_contents_scale_(1.f),
  39. painted_opacity_(scrollbar_.Read(*this)->Opacity()),
  40. has_thumb_(scrollbar_.Read(*this)->HasThumb()),
  41. jump_on_track_click_(scrollbar_.Read(*this)->JumpOnTrackClick()),
  42. supports_drag_snap_back_(scrollbar_.Read(*this)->SupportsDragSnapBack()),
  43. is_overlay_(scrollbar_.Read(*this)->IsOverlay()) {}
  44. PaintedScrollbarLayer::~PaintedScrollbarLayer() = default;
  45. bool PaintedScrollbarLayer::OpacityCanAnimateOnImplThread() const {
  46. return is_overlay_;
  47. }
  48. void PaintedScrollbarLayer::PushPropertiesTo(
  49. LayerImpl* layer,
  50. const CommitState& commit_state,
  51. const ThreadUnsafeCommitState& unsafe_state) {
  52. ScrollbarLayerBase::PushPropertiesTo(layer, commit_state, unsafe_state);
  53. PaintedScrollbarLayerImpl* scrollbar_layer =
  54. static_cast<PaintedScrollbarLayerImpl*>(layer);
  55. scrollbar_layer->set_internal_contents_scale_and_bounds(
  56. internal_contents_scale_.Read(*this),
  57. internal_content_bounds_.Read(*this));
  58. scrollbar_layer->SetJumpOnTrackClick(jump_on_track_click_.Read(*this));
  59. scrollbar_layer->SetSupportsDragSnapBack(supports_drag_snap_back_);
  60. scrollbar_layer->SetBackButtonRect(back_button_rect_.Read(*this));
  61. scrollbar_layer->SetForwardButtonRect(forward_button_rect_.Read(*this));
  62. scrollbar_layer->SetTrackRect(track_rect_.Read(*this));
  63. if (orientation() == ScrollbarOrientation::HORIZONTAL) {
  64. scrollbar_layer->SetThumbThickness(thumb_size_.Read(*this).height());
  65. scrollbar_layer->SetThumbLength(thumb_size_.Read(*this).width());
  66. } else {
  67. scrollbar_layer->SetThumbThickness(thumb_size_.Read(*this).width());
  68. scrollbar_layer->SetThumbLength(thumb_size_.Read(*this).height());
  69. }
  70. if (track_resource_.Read(*this)) {
  71. scrollbar_layer->set_track_ui_resource_id(
  72. track_resource_.Read(*this)->id());
  73. } else {
  74. scrollbar_layer->set_track_ui_resource_id(0);
  75. }
  76. if (thumb_resource_.Read(*this)) {
  77. scrollbar_layer->set_thumb_ui_resource_id(
  78. thumb_resource_.Read(*this)->id());
  79. } else {
  80. scrollbar_layer->set_thumb_ui_resource_id(0);
  81. }
  82. scrollbar_layer->SetScrollbarPaintedOpacity(painted_opacity_.Read(*this));
  83. scrollbar_layer->set_is_overlay_scrollbar(is_overlay_);
  84. }
  85. void PaintedScrollbarLayer::SetLayerTreeHost(LayerTreeHost* host) {
  86. // When the LTH is set to null or has changed, then this layer should remove
  87. // all of its associated resources.
  88. if (!host || host != layer_tree_host()) {
  89. track_resource_.Write(*this) = nullptr;
  90. thumb_resource_.Write(*this) = nullptr;
  91. }
  92. ScrollbarLayerBase::SetLayerTreeHost(host);
  93. }
  94. gfx::Size PaintedScrollbarLayer::LayerSizeToContentSize(
  95. const gfx::Size& layer_size) const {
  96. gfx::Size content_size =
  97. gfx::ScaleToCeiledSize(layer_size, internal_contents_scale_.Read(*this));
  98. // We should never return a rect bigger than the content bounds.
  99. content_size.SetToMin(internal_content_bounds_.Read(*this));
  100. return content_size;
  101. }
  102. bool PaintedScrollbarLayer::UpdateThumbAndTrackGeometry() {
  103. // These properties should never change.
  104. DCHECK_EQ(supports_drag_snap_back_,
  105. scrollbar_.Read(*this)->SupportsDragSnapBack());
  106. DCHECK_EQ(is_left_side_vertical_scrollbar(),
  107. scrollbar_.Read(*this)->IsLeftSideVerticalScrollbar());
  108. DCHECK_EQ(is_overlay_, scrollbar_.Read(*this)->IsOverlay());
  109. DCHECK_EQ(orientation(), scrollbar_.Read(*this)->Orientation());
  110. bool updated = false;
  111. const auto& scrollbar = scrollbar_.Read(*this);
  112. updated |= UpdateProperty(scrollbar->JumpOnTrackClick(),
  113. &jump_on_track_click_.Write(*this));
  114. updated |= UpdateProperty(scrollbar->TrackRect(), &track_rect_.Write(*this));
  115. updated |= UpdateProperty(scrollbar->BackButtonRect(),
  116. &back_button_rect_.Write(*this));
  117. updated |= UpdateProperty(scrollbar->ForwardButtonRect(),
  118. &forward_button_rect_.Write(*this));
  119. updated |= UpdateProperty(scrollbar->HasThumb(), &has_thumb_.Write(*this));
  120. if (has_thumb_.Read(*this)) {
  121. // Ignore ThumbRect's location because the PaintedScrollbarLayerImpl will
  122. // compute it from scroll offset.
  123. updated |= UpdateProperty(scrollbar->ThumbRect().size(),
  124. &thumb_size_.Write(*this));
  125. } else {
  126. updated |= UpdateProperty(gfx::Size(), &thumb_size_.Write(*this));
  127. }
  128. return updated;
  129. }
  130. bool PaintedScrollbarLayer::UpdateInternalContentScale() {
  131. gfx::Transform transform;
  132. transform = draw_property_utils::ScreenSpaceTransform(
  133. this, layer_tree_host()->property_trees()->transform_tree());
  134. gfx::Vector2dF transform_scales = gfx::ComputeTransform2dScaleComponents(
  135. transform, layer_tree_host()->device_scale_factor());
  136. float scale = std::max(transform_scales.x(), transform_scales.y());
  137. // Clamp minimum scale to 1 to avoid too low scale during scale animation.
  138. // TODO(crbug.com/1009291): Move rasterization of scrollbars to the impl side
  139. // to better handle scale changes.
  140. scale = std::max(1.0f, scale);
  141. bool updated = false;
  142. updated |= UpdateProperty(scale, &internal_contents_scale_.Write(*this));
  143. updated |= UpdateProperty(
  144. gfx::ScaleToCeiledSize(bounds(), internal_contents_scale_.Read(*this)),
  145. &internal_content_bounds_.Write(*this));
  146. return updated;
  147. }
  148. bool PaintedScrollbarLayer::Update() {
  149. bool updated = false;
  150. updated |= ScrollbarLayerBase::Update();
  151. updated |= UpdateInternalContentScale();
  152. updated |= UpdateThumbAndTrackGeometry();
  153. gfx::Size size = bounds();
  154. gfx::Size scaled_size = internal_content_bounds_.Read(*this);
  155. if (scaled_size.IsEmpty()) {
  156. if (track_resource_.Read(*this)) {
  157. track_resource_.Write(*this) = nullptr;
  158. thumb_resource_.Write(*this) = nullptr;
  159. SetNeedsPushProperties();
  160. updated = true;
  161. }
  162. return updated;
  163. }
  164. if (!has_thumb_.Read(*this) && thumb_resource_.Read(*this)) {
  165. thumb_resource_.Write(*this) = nullptr;
  166. SetNeedsPushProperties();
  167. updated = true;
  168. }
  169. if (!track_resource_.Read(*this) ||
  170. scrollbar_.Read(*this)->NeedsRepaintPart(
  171. ScrollbarPart::TRACK_BUTTONS_TICKMARKS)) {
  172. track_resource_.Write(*this) = ScopedUIResource::Create(
  173. layer_tree_host()->GetUIResourceManager(),
  174. RasterizeScrollbarPart(size, scaled_size,
  175. ScrollbarPart::TRACK_BUTTONS_TICKMARKS));
  176. SetNeedsPushProperties();
  177. updated = true;
  178. }
  179. gfx::Size scaled_thumb_size = LayerSizeToContentSize(thumb_size_.Read(*this));
  180. if (has_thumb_.Read(*this) && !scaled_thumb_size.IsEmpty()) {
  181. if (!thumb_resource_.Read(*this) ||
  182. scrollbar_.Read(*this)->NeedsRepaintPart(ScrollbarPart::THUMB) ||
  183. scaled_thumb_size !=
  184. thumb_resource_.Write(*this)->GetBitmap(0, false).GetSize()) {
  185. thumb_resource_.Write(*this) = ScopedUIResource::Create(
  186. layer_tree_host()->GetUIResourceManager(),
  187. RasterizeScrollbarPart(thumb_size_.Read(*this), scaled_thumb_size,
  188. ScrollbarPart::THUMB));
  189. SetNeedsPushProperties();
  190. updated = true;
  191. }
  192. updated |= UpdateProperty(scrollbar_.Read(*this)->Opacity(),
  193. &painted_opacity_.Write(*this));
  194. }
  195. return updated;
  196. }
  197. UIResourceBitmap PaintedScrollbarLayer::RasterizeScrollbarPart(
  198. const gfx::Size& size,
  199. const gfx::Size& requested_content_size,
  200. ScrollbarPart part) {
  201. DCHECK(!requested_content_size.IsEmpty());
  202. DCHECK(!size.IsEmpty());
  203. gfx::Size content_size = requested_content_size;
  204. // Pages can end up requesting arbitrarily large scrollbars. Prevent this
  205. // from crashing due to OOM and try something smaller.
  206. SkBitmap skbitmap;
  207. bool allocation_succeeded =
  208. skbitmap.tryAllocN32Pixels(content_size.width(), content_size.height());
  209. // Assuming 4bpp, caps at 4M.
  210. constexpr int kMinScrollbarDimension = 1024;
  211. int dimension = std::max(content_size.width(), content_size.height()) / 2;
  212. while (!allocation_succeeded && dimension >= kMinScrollbarDimension) {
  213. content_size.SetToMin(gfx::Size(dimension, dimension));
  214. allocation_succeeded =
  215. skbitmap.tryAllocN32Pixels(content_size.width(), content_size.height());
  216. if (!allocation_succeeded)
  217. dimension = dimension / 2;
  218. }
  219. CHECK(allocation_succeeded)
  220. << "Failed to allocate memory for scrollbar at dimension : " << dimension;
  221. SkiaPaintCanvas canvas(skbitmap);
  222. canvas.clear(SkColors::kTransparent);
  223. float scale_x = content_size.width() / static_cast<float>(size.width());
  224. float scale_y = content_size.height() / static_cast<float>(size.height());
  225. canvas.scale(SkFloatToScalar(scale_x), SkFloatToScalar(scale_y));
  226. scrollbar_.Write(*this)->PaintPart(&canvas, part, gfx::Rect(size));
  227. // Make sure that the pixels are no longer mutable to unavoid unnecessary
  228. // allocation and copying.
  229. skbitmap.setImmutable();
  230. return UIResourceBitmap(skbitmap);
  231. }
  232. ScrollbarLayerBase::ScrollbarLayerType
  233. PaintedScrollbarLayer::GetScrollbarLayerType() const {
  234. return kPainted;
  235. }
  236. } // namespace cc