summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/paint/scrollbar_display_item.cc
blob: eeb5932afc5d286193f6d8cbc5fb1e7ea1cb3f88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/graphics/paint/scrollbar_display_item.h"

#include "base/trace_event/traced_value.h"
#include "cc/input/scrollbar.h"
#include "cc/layers/painted_overlay_scrollbar_layer.h"
#include "cc/layers/painted_scrollbar_layer.h"
#include "cc/layers/solid_color_scrollbar_layer.h"
#include "third_party/blink/renderer/platform/graphics/graphics_context.h"
#include "third_party/blink/renderer/platform/graphics/paint/drawing_recorder.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_controller.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_record_builder.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_recorder.h"

namespace blink {

ScrollbarDisplayItem::ScrollbarDisplayItem(
    const DisplayItemClient& client,
    Type type,
    scoped_refptr<cc::Scrollbar> scrollbar,
    const IntRect& rect,
    const TransformPaintPropertyNode* scroll_translation,
    CompositorElementId element_id)
    : DisplayItem(client, type, sizeof(*this), /*draws_content*/ true),
      scrollbar_(std::move(scrollbar)),
      rect_(rect),
      scroll_translation_(scroll_translation),
      element_id_(element_id) {
  DCHECK(IsScrollbar());
  DCHECK(!scroll_translation || scroll_translation->ScrollNode());
}

sk_sp<const PaintRecord> ScrollbarDisplayItem::Paint() const {
  // We are painting a non-composited scrollbar, so we don't need layer_.
  layer_ = nullptr;

  if (record_) {
    DCHECK(!scrollbar_->NeedsRepaintPart(cc::TRACK_BUTTONS_TICKMARKS));
    DCHECK(!scrollbar_->NeedsRepaintPart(cc::THUMB));
    return record_;
  }

  PaintRecorder recorder;
  recorder.beginRecording(rect_);
  auto* canvas = recorder.getRecordingCanvas();
  scrollbar_->PaintPart(canvas, cc::TRACK_BUTTONS_TICKMARKS, rect_);
  gfx::Rect thumb_rect = scrollbar_->ThumbRect();
  thumb_rect.Offset(rect_.X(), rect_.Y());
  scrollbar_->PaintPart(canvas, cc::THUMB, thumb_rect);

  record_ = recorder.finishRecordingAsPicture();
  return record_;
}

scoped_refptr<cc::ScrollbarLayerBase> ScrollbarDisplayItem::GetLayer() const {
  // This function is called when the scrollbar is composited. We don't need
  // record_ which is for non-composited scrollbars.
  record_ = nullptr;

  if (!layer_ || layer_->is_left_side_vertical_scrollbar() !=
                     scrollbar_->IsLeftSideVerticalScrollbar())
    layer_ = CreateLayer();

  layer_->SetOffsetToTransformParent(
      gfx::Vector2dF(FloatPoint(rect_.Location())));
  layer_->SetBounds(gfx::Size(rect_.Size()));

  if (scrollbar_->NeedsRepaintPart(cc::THUMB) ||
      scrollbar_->NeedsRepaintPart(cc::TRACK_BUTTONS_TICKMARKS))
    layer_->SetNeedsDisplay();
  return layer_;
}

scoped_refptr<cc::ScrollbarLayerBase> ScrollbarDisplayItem::CreateLayer()
    const {
  scoped_refptr<cc::ScrollbarLayerBase> layer;
  if (scrollbar_->IsSolidColor()) {
    DCHECK(scrollbar_->IsOverlay());
    bool is_horizontal = scrollbar_->Orientation() == cc::HORIZONTAL;
    gfx::Rect thumb_rect = scrollbar_->ThumbRect();
    int thumb_thickness =
        is_horizontal ? thumb_rect.height() : thumb_rect.width();
    gfx::Rect track_rect = scrollbar_->TrackRect();
    int track_start = is_horizontal ? track_rect.x() : track_rect.y();
    layer = cc::SolidColorScrollbarLayer::Create(
        scrollbar_->Orientation(), thumb_thickness, track_start,
        scrollbar_->IsLeftSideVerticalScrollbar());
  } else if (scrollbar_->UsesNinePatchThumbResource()) {
    DCHECK(scrollbar_->IsOverlay());
    layer = cc::PaintedOverlayScrollbarLayer::Create(scrollbar_);
  } else {
    layer = cc::PaintedScrollbarLayer::Create(scrollbar_);
  }

  layer->SetIsDrawable(true);
  layer->SetElementId(element_id_);
  layer->SetScrollElementId(
      scroll_translation_
          ? scroll_translation_->ScrollNode()->GetCompositorElementId()
          : CompositorElementId());
  return layer;
}

bool ScrollbarDisplayItem::Equals(const DisplayItem& other) const {
  if (!DisplayItem::Equals(other))
    return false;

  // Don't check scrollbar_ because it's always newly created when we repaint
  // a scrollbar (including forced repaint for PaintUnderInvalidationChecking).
  // Don't check record_ because it's lazily created, and the DCHECKs in Paint()
  // can catch most under-invalidation cases.
  const auto& other_scrollbar_item =
      static_cast<const ScrollbarDisplayItem&>(other);
  return rect_ == other_scrollbar_item.rect_ &&
         scroll_translation_ == other_scrollbar_item.scroll_translation_ &&
         element_id_ == other_scrollbar_item.element_id_;
}

#if DCHECK_IS_ON()
void ScrollbarDisplayItem::PropertiesAsJSON(JSONObject& json) const {
  DisplayItem::PropertiesAsJSON(json);
  json.SetString("rect", rect_.ToString());
  json.SetString("scrollTranslation",
                 String::Format("%p", scroll_translation_));
}
#endif

void ScrollbarDisplayItem::Record(
    GraphicsContext& context,
    const DisplayItemClient& client,
    DisplayItem::Type type,
    scoped_refptr<cc::Scrollbar> scrollbar,
    const IntRect& rect,
    const TransformPaintPropertyNode* scroll_translation,
    CompositorElementId element_id) {
  PaintController& paint_controller = context.GetPaintController();
  // Must check PaintController::UseCachedItemIfPossible before this function.
  DCHECK(RuntimeEnabledFeatures::PaintUnderInvalidationCheckingEnabled() ||
         !paint_controller.UseCachedItemIfPossible(client, type));

  paint_controller.CreateAndAppend<ScrollbarDisplayItem>(
      client, type, std::move(scrollbar), rect, scroll_translation, element_id);
}

}  // namespace blink