summaryrefslogtreecommitdiff
path: root/chromium/cc/layers/scrollbar_layer.cc
blob: 95eb6384732819c75bded06789c56b6cb76ee506 (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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353

// Copyright 2012 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/scrollbar_layer.h"

#include "base/auto_reset.h"
#include "base/basictypes.h"
#include "base/debug/trace_event.h"
#include "cc/layers/scrollbar_layer_impl.h"
#include "cc/resources/caching_bitmap_content_layer_updater.h"
#include "cc/resources/layer_painter.h"
#include "cc/resources/prioritized_resource.h"
#include "cc/resources/resource_update_queue.h"
#include "cc/trees/layer_tree_host.h"
#include "ui/gfx/rect_conversions.h"

namespace cc {

scoped_ptr<LayerImpl> ScrollbarLayer::CreateLayerImpl(
    LayerTreeImpl* tree_impl) {
  return ScrollbarLayerImpl::Create(
      tree_impl, id(), scrollbar_->Orientation()).PassAs<LayerImpl>();
}

scoped_refptr<ScrollbarLayer> ScrollbarLayer::Create(
    scoped_ptr<Scrollbar> scrollbar,
    int scroll_layer_id) {
  return make_scoped_refptr(new ScrollbarLayer(scrollbar.Pass(),
                                               scroll_layer_id));
}

ScrollbarLayer::ScrollbarLayer(
    scoped_ptr<Scrollbar> scrollbar,
    int scroll_layer_id)
    : scrollbar_(scrollbar.Pass()),
      scroll_layer_id_(scroll_layer_id),
      texture_format_(GL_INVALID_ENUM) {
  if (!scrollbar_->IsOverlay())
    SetShouldScrollOnMainThread(true);
}

ScrollbarLayer::~ScrollbarLayer() {}

void ScrollbarLayer::SetScrollLayerId(int id) {
  if (id == scroll_layer_id_)
    return;

  scroll_layer_id_ = id;
  SetNeedsFullTreeSync();
}

bool ScrollbarLayer::OpacityCanAnimateOnImplThread() const {
  return scrollbar_->IsOverlay();
}

ScrollbarOrientation ScrollbarLayer::Orientation() const {
  return scrollbar_->Orientation();
}

int ScrollbarLayer::MaxTextureSize() {
  DCHECK(layer_tree_host());
  return layer_tree_host()->GetRendererCapabilities().max_texture_size;
}

float ScrollbarLayer::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 ScrollbarLayer::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 ScrollbarLayer::PushPropertiesTo(LayerImpl* layer) {
  ContentsScalingLayer::PushPropertiesTo(layer);

  ScrollbarLayerImpl* scrollbar_layer = static_cast<ScrollbarLayerImpl*>(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_ && track_->texture()->have_backing_texture())
    scrollbar_layer->set_track_resource_id(track_->texture()->resource_id());
  else
    scrollbar_layer->set_track_resource_id(0);

  if (thumb_ && thumb_->texture()->have_backing_texture())
    scrollbar_layer->set_thumb_resource_id(thumb_->texture()->resource_id());
  else
    scrollbar_layer->set_thumb_resource_id(0);

  scrollbar_layer->set_is_overlay_scrollbar(scrollbar_->IsOverlay());

  // ScrollbarLayer must push properties every frame. crbug.com/259095
  needs_push_properties_ = true;
}

ScrollbarLayer* ScrollbarLayer::ToScrollbarLayer() {
  return this;
}

void ScrollbarLayer::SetLayerTreeHost(LayerTreeHost* host) {
  if (!host || host != layer_tree_host()) {
    track_updater_ = NULL;
    track_.reset();
    thumb_updater_ = NULL;
    thumb_.reset();
  }

  ContentsScalingLayer::SetLayerTreeHost(host);
}

class ScrollbarPartPainter : public LayerPainter {
 public:
  ScrollbarPartPainter(Scrollbar* scrollbar, ScrollbarPart part)
      : scrollbar_(scrollbar),
        part_(part) {}
  virtual ~ScrollbarPartPainter() {}

  // LayerPainter implementation
  virtual void Paint(SkCanvas* canvas,
                     gfx::Rect content_rect,
                     gfx::RectF* opaque) OVERRIDE {
    scrollbar_->PaintPart(canvas, part_, content_rect);
  }

 private:
  Scrollbar* scrollbar_;
  ScrollbarPart part_;
};

void ScrollbarLayer::CreateUpdaterIfNeeded() {
  if (layer_tree_host()->settings().solid_color_scrollbars)
    return;

  texture_format_ =
      layer_tree_host()->GetRendererCapabilities().best_texture_format;

  if (!track_updater_.get()) {
    track_updater_ = CachingBitmapContentLayerUpdater::Create(
        scoped_ptr<LayerPainter>(
            new ScrollbarPartPainter(scrollbar_.get(), TRACK))
            .Pass(),
        rendering_stats_instrumentation(),
        id());
  }
  if (!track_) {
    track_ = track_updater_->CreateResource(
        layer_tree_host()->contents_texture_manager());
  }

  if (!thumb_updater_.get()) {
    thumb_updater_ = CachingBitmapContentLayerUpdater::Create(
        scoped_ptr<LayerPainter>(
            new ScrollbarPartPainter(scrollbar_.get(), THUMB))
            .Pass(),
        rendering_stats_instrumentation(),
        id());
  }
  if (!thumb_ && scrollbar_->HasThumb()) {
    thumb_ = thumb_updater_->CreateResource(
        layer_tree_host()->contents_texture_manager());
  }
}

bool ScrollbarLayer::UpdatePart(CachingBitmapContentLayerUpdater* painter,
                                LayerUpdater::Resource* resource,
                                gfx::Rect rect,
                                ResourceUpdateQueue* queue) {
  if (layer_tree_host()->settings().solid_color_scrollbars)
    return false;

  // Skip painting and uploading if there are no invalidations and
  // we already have valid texture data.
  if (resource->texture()->have_backing_texture() &&
      resource->texture()->size() == rect.size() &&
      !is_dirty())
    return false;

  // We should always have enough memory for UI.
  DCHECK(resource->texture()->can_acquire_backing_texture());
  if (!resource->texture()->can_acquire_backing_texture())
    return false;

  // Paint and upload the entire part.
  gfx::Rect painted_opaque_rect;
  painter->PrepareToUpdate(rect,
                           rect.size(),
                           contents_scale_x(),
                           contents_scale_y(),
                           &painted_opaque_rect);
  if (!painter->pixels_did_change() &&
      resource->texture()->have_backing_texture()) {
    TRACE_EVENT_INSTANT0("cc",
                         "ScrollbarLayer::UpdatePart no texture upload needed",
                         TRACE_EVENT_SCOPE_THREAD);
    return false;
  }

  bool partial_updates_allowed =
      layer_tree_host()->settings().max_partial_texture_updates > 0;
  if (!partial_updates_allowed)
    resource->texture()->ReturnBackingTexture();

  gfx::Vector2d dest_offset(0, 0);
  resource->Update(queue, rect, dest_offset, partial_updates_allowed);
  return true;
}

gfx::Rect ScrollbarLayer::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;
}

void ScrollbarLayer::SetTexturePriorities(
    const PriorityCalculator& priority_calc) {
  if (layer_tree_host()->settings().solid_color_scrollbars)
    return;

  if (content_bounds().IsEmpty())
    return;
  DCHECK_LE(content_bounds().width(), MaxTextureSize());
  DCHECK_LE(content_bounds().height(), MaxTextureSize());

  CreateUpdaterIfNeeded();

  bool draws_to_root = !render_target()->parent();
  if (track_) {
    track_->texture()->SetDimensions(content_bounds(), texture_format_);
    track_->texture()->set_request_priority(
        PriorityCalculator::UIPriority(draws_to_root));
  }
  if (thumb_) {
    gfx::Size thumb_size = OriginThumbRect().size();
    thumb_->texture()->SetDimensions(thumb_size, texture_format_);
    thumb_->texture()->set_request_priority(
        PriorityCalculator::UIPriority(draws_to_root));
  }
}

bool ScrollbarLayer::Update(ResourceUpdateQueue* queue,
                            const OcclusionTracker* occlusion) {
  track_rect_ = scrollbar_->TrackRect();
  location_ = scrollbar_->Location();

  if (layer_tree_host()->settings().solid_color_scrollbars)
    return false;

  bool updated = false;

  {
    base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
                                                  true);
    updated = ContentsScalingLayer::Update(queue, occlusion);
  }

  dirty_rect_.Union(update_rect_);
  if (content_bounds().IsEmpty())
    return false;
  if (visible_content_rect().IsEmpty())
    return false;

  CreateUpdaterIfNeeded();

  gfx::Rect content_rect = ScrollbarLayerRectToContentRect(
      gfx::Rect(scrollbar_->Location(), bounds()));
  updated |= UpdatePart(track_updater_.get(), track_.get(), content_rect,
                        queue);

  if (scrollbar_->HasThumb()) {
    thumb_thickness_ = scrollbar_->ThumbThickness();
    thumb_length_ = scrollbar_->ThumbLength();
    gfx::Rect origin_thumb_rect = OriginThumbRect();
    if (!origin_thumb_rect.IsEmpty()) {
      updated |= UpdatePart(thumb_updater_.get(), thumb_.get(),
                            origin_thumb_rect, queue);
    }
  }

  dirty_rect_ = gfx::RectF();
  return updated;
}

gfx::Rect ScrollbarLayer::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));
}

}  // namespace cc