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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
// Copyright 2013 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 "cc/layers/painted_scrollbar_layer.h"
#include "base/auto_reset.h"
#include "base/basictypes.h"
#include "base/debug/trace_event.h"
#include "cc/layers/painted_scrollbar_layer_impl.h"
#include "cc/resources/ui_resource_bitmap.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_impl.h"
#include "skia/ext/platform_canvas.h"
#include "skia/ext/refptr.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSize.h"
#include "ui/gfx/skia_util.h"
namespace cc {
scoped_ptr<LayerImpl> PaintedScrollbarLayer::CreateLayerImpl(
LayerTreeImpl* tree_impl) {
return PaintedScrollbarLayerImpl::Create(
tree_impl, id(), scrollbar_->Orientation()).PassAs<LayerImpl>();
}
scoped_refptr<PaintedScrollbarLayer> PaintedScrollbarLayer::Create(
scoped_ptr<Scrollbar> scrollbar,
int scroll_layer_id) {
return make_scoped_refptr(
new PaintedScrollbarLayer(scrollbar.Pass(), scroll_layer_id));
}
PaintedScrollbarLayer::PaintedScrollbarLayer(
scoped_ptr<Scrollbar> scrollbar,
int scroll_layer_id)
: scrollbar_(scrollbar.Pass()),
scroll_layer_id_(scroll_layer_id) {
if (!scrollbar_->IsOverlay())
SetShouldScrollOnMainThread(true);
}
PaintedScrollbarLayer::~PaintedScrollbarLayer() {}
void PaintedScrollbarLayer::SetScrollLayerId(int id) {
if (id == scroll_layer_id_)
return;
scroll_layer_id_ = id;
SetNeedsFullTreeSync();
}
bool PaintedScrollbarLayer::OpacityCanAnimateOnImplThread() const {
return scrollbar_->IsOverlay();
}
ScrollbarOrientation PaintedScrollbarLayer::Orientation() const {
return scrollbar_->Orientation();
}
int PaintedScrollbarLayer::MaxTextureSize() {
DCHECK(layer_tree_host());
return layer_tree_host()->GetRendererCapabilities().max_texture_size;
}
float PaintedScrollbarLayer::ClampScaleToMaxTextureSize(float scale) {
if (layer_tree_host()->settings().solid_color_scrollbars)
return scale;
// If the scaled content_bounds() is bigger than the max texture size of the
// device, we need to clamp it by rescaling, since content_bounds() is used
// below to set the texture size.
gfx::Size scaled_bounds = ComputeContentBoundsForScale(scale, scale);
if (scaled_bounds.width() > MaxTextureSize() ||
scaled_bounds.height() > MaxTextureSize()) {
if (scaled_bounds.width() > scaled_bounds.height())
return (MaxTextureSize() - 1) / static_cast<float>(bounds().width());
else
return (MaxTextureSize() - 1) / static_cast<float>(bounds().height());
}
return scale;
}
void PaintedScrollbarLayer::CalculateContentsScale(
float ideal_contents_scale,
float device_scale_factor,
float page_scale_factor,
bool animating_transform_to_screen,
float* contents_scale_x,
float* contents_scale_y,
gfx::Size* content_bounds) {
ContentsScalingLayer::CalculateContentsScale(
ClampScaleToMaxTextureSize(ideal_contents_scale),
device_scale_factor,
page_scale_factor,
animating_transform_to_screen,
contents_scale_x,
contents_scale_y,
content_bounds);
}
void PaintedScrollbarLayer::PushPropertiesTo(LayerImpl* layer) {
ContentsScalingLayer::PushPropertiesTo(layer);
PaintedScrollbarLayerImpl* scrollbar_layer =
static_cast<PaintedScrollbarLayerImpl*>(layer);
if (layer_tree_host() &&
layer_tree_host()->settings().solid_color_scrollbars) {
int thickness_override =
layer_tree_host()->settings().solid_color_scrollbar_thickness_dip;
if (thickness_override != -1) {
scrollbar_layer->SetThumbThickness(thickness_override);
} else {
if (Orientation() == HORIZONTAL)
scrollbar_layer->SetThumbThickness(bounds().height());
else
scrollbar_layer->SetThumbThickness(bounds().width());
}
} else {
scrollbar_layer->SetThumbThickness(thumb_thickness_);
}
scrollbar_layer->SetThumbLength(thumb_length_);
if (Orientation() == HORIZONTAL) {
scrollbar_layer->SetTrackStart(
track_rect_.x() - location_.x());
scrollbar_layer->SetTrackLength(track_rect_.width());
} else {
scrollbar_layer->SetTrackStart(
track_rect_.y() - location_.y());
scrollbar_layer->SetTrackLength(track_rect_.height());
}
if (track_resource_.get())
scrollbar_layer->set_track_ui_resource_id(track_resource_->id());
if (thumb_resource_.get())
scrollbar_layer->set_thumb_ui_resource_id(thumb_resource_->id());
scrollbar_layer->set_is_overlay_scrollbar(scrollbar_->IsOverlay());
// PaintedScrollbarLayer must push properties every frame. crbug.com/259095
needs_push_properties_ = true;
}
PaintedScrollbarLayer* PaintedScrollbarLayer::ToScrollbarLayer() {
return this;
}
void PaintedScrollbarLayer::SetLayerTreeHost(LayerTreeHost* host) {
// When the LTH is set to null or has changed, then this layer should remove
// all of its associated resources.
if (!host || host != layer_tree_host()) {
track_resource_.reset();
thumb_resource_.reset();
}
ContentsScalingLayer::SetLayerTreeHost(host);
}
gfx::Rect PaintedScrollbarLayer::ScrollbarLayerRectToContentRect(
gfx::Rect layer_rect) const {
// 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());
// We should never return a rect bigger than the content_bounds().
gfx::Size clamped_size = expanded_rect.size();
clamped_size.SetToMin(content_bounds());
expanded_rect.set_size(clamped_size);
return expanded_rect;
}
gfx::Rect PaintedScrollbarLayer::OriginThumbRect() const {
gfx::Size thumb_size;
if (Orientation() == HORIZONTAL) {
thumb_size =
gfx::Size(scrollbar_->ThumbLength(), scrollbar_->ThumbThickness());
} else {
thumb_size =
gfx::Size(scrollbar_->ThumbThickness(), scrollbar_->ThumbLength());
}
return ScrollbarLayerRectToContentRect(gfx::Rect(thumb_size));
}
void PaintedScrollbarLayer::UpdateThumbAndTrackGeometry() {
track_rect_ = scrollbar_->TrackRect();
location_ = scrollbar_->Location();
if (scrollbar_->HasThumb()) {
thumb_thickness_ = scrollbar_->ThumbThickness();
thumb_length_ = scrollbar_->ThumbLength();
}
}
bool PaintedScrollbarLayer::Update(ResourceUpdateQueue* queue,
const OcclusionTracker* occlusion) {
UpdateThumbAndTrackGeometry();
gfx::Rect scaled_track_rect = ScrollbarLayerRectToContentRect(
gfx::Rect(location_, bounds()));
if (layer_tree_host()->settings().solid_color_scrollbars ||
track_rect_.IsEmpty() || scaled_track_rect.IsEmpty())
return false;
{
base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
true);
ContentsScalingLayer::Update(queue, occlusion);
}
track_resource_ = ScopedUIResource::Create(
layer_tree_host(), RasterizeScrollbarPart(scaled_track_rect, TRACK));
gfx::Rect thumb_rect = OriginThumbRect();
if (scrollbar_->HasThumb() && !thumb_rect.IsEmpty()) {
thumb_resource_ = ScopedUIResource::Create(
layer_tree_host(), RasterizeScrollbarPart(thumb_rect, THUMB));
}
return true;
}
scoped_refptr<UIResourceBitmap> PaintedScrollbarLayer::RasterizeScrollbarPart(
gfx::Rect rect,
ScrollbarPart part) {
DCHECK(!layer_tree_host()->settings().solid_color_scrollbars);
DCHECK(!rect.size().IsEmpty());
scoped_refptr<UIResourceBitmap> bitmap =
UIResourceBitmap::Create(new uint8_t[rect.width() * rect.height() * 4],
UIResourceBitmap::RGBA8,
rect.size());
SkBitmap skbitmap;
skbitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height());
skbitmap.setPixels(bitmap->GetPixels());
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());
SkRect layer_skrect = RectToSkRect(layer_rect);
SkPaint paint;
paint.setAntiAlias(false);
paint.setXfermodeMode(SkXfermode::kClear_Mode);
skcanvas.drawRect(layer_skrect, paint);
skcanvas.clipRect(layer_skrect);
scrollbar_->PaintPart(&skcanvas, part, layer_rect);
return bitmap;
}
} // namespace cc
|