summaryrefslogtreecommitdiffstats
path: root/cc/scrollbar_layer.cc
blob: ded7bf12a9a4f3f7c99066ef7cc33565ab0dc89c (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
354
355
// 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/scrollbar_layer.h"

#include "base/basictypes.h"
#include "base/debug/trace_event.h"
#include "cc/caching_bitmap_content_layer_updater.h"
#include "cc/layer_painter.h"
#include "cc/layer_tree_host.h"
#include "cc/prioritized_resource.h"
#include "cc/resource_update_queue.h"
#include "cc/scrollbar_layer_impl.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
#include "ui/gfx/rect_conversions.h"

namespace cc {

scoped_ptr<LayerImpl> ScrollbarLayer::createLayerImpl(LayerTreeImpl* treeImpl)
{
    return ScrollbarLayerImpl::create(treeImpl, id(), ScrollbarGeometryFixedThumb::create(make_scoped_ptr(m_geometry->clone()))).PassAs<LayerImpl>();
}

scoped_refptr<ScrollbarLayer> ScrollbarLayer::create(
    scoped_ptr<WebKit::WebScrollbar> scrollbar,
    scoped_ptr<ScrollbarThemePainter> painter,
    scoped_ptr<WebKit::WebScrollbarThemeGeometry> geometry,
    int scrollLayerId)
{
    return make_scoped_refptr(new ScrollbarLayer(scrollbar.Pass(), painter.Pass(), geometry.Pass(), scrollLayerId));
}

ScrollbarLayer::ScrollbarLayer(
    scoped_ptr<WebKit::WebScrollbar> scrollbar,
    scoped_ptr<ScrollbarThemePainter> painter,
    scoped_ptr<WebKit::WebScrollbarThemeGeometry> geometry,
    int scrollLayerId)
    : m_scrollbar(scrollbar.Pass())
    , m_painter(painter.Pass())
    , m_geometry(geometry.Pass())
    , m_scrollLayerId(scrollLayerId)
    , m_textureFormat(GL_INVALID_ENUM)
{
    if (!m_scrollbar->isOverlay())
        setShouldScrollOnMainThread(true);
}

ScrollbarLayer::~ScrollbarLayer()
{
}

void ScrollbarLayer::setScrollLayerId(int id)
{
    if (id == m_scrollLayerId)
        return;

    m_scrollLayerId = id;
    setNeedsFullTreeSync();
}

WebKit::WebScrollbar::Orientation ScrollbarLayer::orientation() const
{
    return m_scrollbar->orientation();
}

int ScrollbarLayer::maxTextureSize() {
    DCHECK(layerTreeHost());
    return layerTreeHost()->rendererCapabilities().maxTextureSize;
}

float ScrollbarLayer::clampScaleToMaxTextureSize(float scale) {
    if (layerTreeHost()->settings().solidColorScrollbars)
        return scale;

    // If the scaled contentBounds() is bigger than the max texture size of the
    // device, we need to clamp it by rescaling, since contentBounds() is used
    // below to set the texture size.
    gfx::Size scaledBounds = computeContentBoundsForScale(scale, scale);
    if (scaledBounds.width() > maxTextureSize() || scaledBounds.height() > maxTextureSize()) {
         if (scaledBounds.width() > scaledBounds.height())
             return (maxTextureSize() - 1) / static_cast<float>(bounds().width());
         else
             return (maxTextureSize() - 1) / static_cast<float>(bounds().height());
   }
    return scale;
}

void ScrollbarLayer::calculateContentsScale(
  float idealContentsScale,
  bool animatingTransformToScreen,
  float* contentsScaleX,
  float* contentsScaleY,
  gfx::Size* contentBounds)
{
    ContentsScalingLayer::calculateContentsScale(
        clampScaleToMaxTextureSize(idealContentsScale),
        animatingTransformToScreen,
        contentsScaleX,
        contentsScaleY,
        contentBounds);
    DCHECK_LE(contentBounds->width(), maxTextureSize());
    DCHECK_LE(contentBounds->height(), maxTextureSize());
}

void ScrollbarLayer::pushPropertiesTo(LayerImpl* layer)
{
    ContentsScalingLayer::pushPropertiesTo(layer);

    ScrollbarLayerImpl* scrollbarLayer = static_cast<ScrollbarLayerImpl*>(layer);

    scrollbarLayer->setScrollbarData(m_scrollbar.get());
    scrollbarLayer->setThumbSize(m_thumbSize);

    if (m_backTrack && m_backTrack->texture()->haveBackingTexture())
        scrollbarLayer->setBackTrackResourceId(m_backTrack->texture()->resourceId());
    else
        scrollbarLayer->setBackTrackResourceId(0);

    if (m_foreTrack && m_foreTrack->texture()->haveBackingTexture())
        scrollbarLayer->setForeTrackResourceId(m_foreTrack->texture()->resourceId());
    else
        scrollbarLayer->setForeTrackResourceId(0);

    if (m_thumb && m_thumb->texture()->haveBackingTexture())
        scrollbarLayer->setThumbResourceId(m_thumb->texture()->resourceId());
    else
        scrollbarLayer->setThumbResourceId(0);
}

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

class ScrollbarBackgroundPainter : public LayerPainter {
public:
    static scoped_ptr<ScrollbarBackgroundPainter> create(WebKit::WebScrollbar* scrollbar, ScrollbarThemePainter *painter, WebKit::WebScrollbarThemeGeometry* geometry, WebKit::WebScrollbar::ScrollbarPart trackPart)
    {
        return make_scoped_ptr(new ScrollbarBackgroundPainter(scrollbar, painter, geometry, trackPart));
    }

    virtual void paint(SkCanvas* canvas, gfx::Rect contentRect, gfx::RectF&) OVERRIDE
    {
        // The following is a simplification of ScrollbarThemeComposite::paint.
        m_painter->PaintScrollbarBackground(canvas, contentRect);

        if (m_geometry->hasButtons(m_scrollbar)) {
            gfx::Rect backButtonStartPaintRect = m_geometry->backButtonStartRect(m_scrollbar);
            m_painter->PaintBackButtonStart(canvas, backButtonStartPaintRect);

            gfx::Rect backButtonEndPaintRect = m_geometry->backButtonEndRect(m_scrollbar);
            m_painter->PaintBackButtonEnd(canvas, backButtonEndPaintRect);

            gfx::Rect forwardButtonStartPaintRect = m_geometry->forwardButtonStartRect(m_scrollbar);
            m_painter->PaintForwardButtonStart(canvas, forwardButtonStartPaintRect);

            gfx::Rect forwardButtonEndPaintRect = m_geometry->forwardButtonEndRect(m_scrollbar);
            m_painter->PaintForwardButtonEnd(canvas, forwardButtonEndPaintRect);
        }

        gfx::Rect trackPaintRect = m_geometry->trackRect(m_scrollbar);
        m_painter->PaintTrackBackground(canvas, trackPaintRect);

        bool thumbPresent = m_geometry->hasThumb(m_scrollbar);
        if (thumbPresent) {
            if (m_trackPart == WebKit::WebScrollbar::ForwardTrackPart)
                m_painter->PaintForwardTrackPart(canvas, trackPaintRect);
            else
                m_painter->PaintBackTrackPart(canvas, trackPaintRect);
        }

        m_painter->PaintTickmarks(canvas, trackPaintRect);
    }
private:
    ScrollbarBackgroundPainter(WebKit::WebScrollbar* scrollbar, ScrollbarThemePainter *painter, WebKit::WebScrollbarThemeGeometry* geometry, WebKit::WebScrollbar::ScrollbarPart trackPart)
        : m_scrollbar(scrollbar)
        , m_painter(painter)
        , m_geometry(geometry)
        , m_trackPart(trackPart)
    {
    }

    WebKit::WebScrollbar* m_scrollbar;
    ScrollbarThemePainter* m_painter;
    WebKit::WebScrollbarThemeGeometry* m_geometry;
    WebKit::WebScrollbar::ScrollbarPart m_trackPart;

    DISALLOW_COPY_AND_ASSIGN(ScrollbarBackgroundPainter);
};

class ScrollbarThumbPainter : public LayerPainter {
public:
    static scoped_ptr<ScrollbarThumbPainter> create(WebKit::WebScrollbar* scrollbar, ScrollbarThemePainter* painter, WebKit::WebScrollbarThemeGeometry* geometry)
    {
        return make_scoped_ptr(new ScrollbarThumbPainter(scrollbar, painter, geometry));
    }

    virtual void paint(SkCanvas* canvas, gfx::Rect contentRect, gfx::RectF& opaque) OVERRIDE
    {
        // Consider the thumb to be at the origin when painting.
        gfx::Rect thumbRect = m_geometry->thumbRect(m_scrollbar);
        m_painter->PaintThumb(canvas, gfx::Rect(thumbRect.size()));
    }

private:
    ScrollbarThumbPainter(WebKit::WebScrollbar* scrollbar, ScrollbarThemePainter* painter, WebKit::WebScrollbarThemeGeometry* geometry)
        : m_scrollbar(scrollbar)
        , m_painter(painter)
        , m_geometry(geometry)
    {
    }

    WebKit::WebScrollbar* m_scrollbar;
    ScrollbarThemePainter* m_painter;
    WebKit::WebScrollbarThemeGeometry* m_geometry;

    DISALLOW_COPY_AND_ASSIGN(ScrollbarThumbPainter);
};

void ScrollbarLayer::setLayerTreeHost(LayerTreeHost* host)
{
    if (!host || host != layerTreeHost()) {
        m_backTrackUpdater = NULL;
        m_backTrack.reset();
        m_thumbUpdater = NULL;
        m_thumb.reset();
    }

    ContentsScalingLayer::setLayerTreeHost(host);
}

void ScrollbarLayer::createUpdaterIfNeeded()
{
    if (layerTreeHost()->settings().solidColorScrollbars)
        return;

    m_textureFormat = layerTreeHost()->rendererCapabilities().bestTextureFormat;

    if (!m_backTrackUpdater)
        m_backTrackUpdater = CachingBitmapContentLayerUpdater::Create(ScrollbarBackgroundPainter::create(m_scrollbar.get(), m_painter.get(), m_geometry.get(), WebKit::WebScrollbar::BackTrackPart).PassAs<LayerPainter>());
    if (!m_backTrack)
        m_backTrack = m_backTrackUpdater->createResource(layerTreeHost()->contentsTextureManager());

    // Only create two-part track if we think the two parts could be different in appearance.
    if (m_scrollbar->isCustomScrollbar()) {
        if (!m_foreTrackUpdater)
            m_foreTrackUpdater = CachingBitmapContentLayerUpdater::Create(ScrollbarBackgroundPainter::create(m_scrollbar.get(), m_painter.get(), m_geometry.get(), WebKit::WebScrollbar::ForwardTrackPart).PassAs<LayerPainter>());
        if (!m_foreTrack)
            m_foreTrack = m_foreTrackUpdater->createResource(layerTreeHost()->contentsTextureManager());
    }

    if (!m_thumbUpdater)
        m_thumbUpdater = CachingBitmapContentLayerUpdater::Create(ScrollbarThumbPainter::create(m_scrollbar.get(), m_painter.get(), m_geometry.get()).PassAs<LayerPainter>());
    if (!m_thumb)
        m_thumb = m_thumbUpdater->createResource(layerTreeHost()->contentsTextureManager());
}

void ScrollbarLayer::updatePart(CachingBitmapContentLayerUpdater* painter, LayerUpdater::Resource* resource, const gfx::Rect& rect, ResourceUpdateQueue& queue, RenderingStats* stats)
{
    if (layerTreeHost()->settings().solidColorScrollbars)
        return;

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

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

    // Paint and upload the entire part.
    gfx::Rect paintedOpaqueRect;
    painter->prepareToUpdate(rect, rect.size(), contentsScaleX(), contentsScaleY(), paintedOpaqueRect, stats);
    if (!painter->pixelsDidChange() && resource->texture()->haveBackingTexture()) {
        TRACE_EVENT_INSTANT0("cc","ScrollbarLayer::updatePart no texture upload needed");
        return;
    }

    bool partialUpdatesAllowed = layerTreeHost()->settings().maxPartialTextureUpdates > 0;
    if (!partialUpdatesAllowed)
        resource->texture()->returnBackingTexture();

    gfx::Vector2d destOffset(0, 0);
    resource->update(queue, rect, destOffset, partialUpdatesAllowed, stats);
}

gfx::Rect ScrollbarLayer::scrollbarLayerRectToContentRect(const gfx::Rect& layerRect) const
{
    // Don't intersect with the bounds as in layerRectToContentRect() because
    // layerRect here might be in coordinates of the containing layer.
    gfx::RectF contentRect = gfx::ScaleRect(layerRect, contentsScaleX(), contentsScaleY());
    return gfx::ToEnclosingRect(contentRect);
}

void ScrollbarLayer::setTexturePriorities(const PriorityCalculator&)
{
    if (layerTreeHost()->settings().solidColorScrollbars)
        return;

    if (contentBounds().IsEmpty())
        return;
    DCHECK_LE(contentBounds().width(), maxTextureSize());
    DCHECK_LE(contentBounds().height(), maxTextureSize());

    createUpdaterIfNeeded();

    bool drawsToRoot = !renderTarget()->parent();
    if (m_backTrack) {
        m_backTrack->texture()->setDimensions(contentBounds(), m_textureFormat);
        m_backTrack->texture()->setRequestPriority(PriorityCalculator::uiPriority(drawsToRoot));
    }
    if (m_foreTrack) {
        m_foreTrack->texture()->setDimensions(contentBounds(), m_textureFormat);
        m_foreTrack->texture()->setRequestPriority(PriorityCalculator::uiPriority(drawsToRoot));
    }
    if (m_thumb) {
        gfx::Size thumbSize = scrollbarLayerRectToContentRect(m_geometry->thumbRect(m_scrollbar.get())).size();
        m_thumb->texture()->setDimensions(thumbSize, m_textureFormat);
        m_thumb->texture()->setRequestPriority(PriorityCalculator::uiPriority(drawsToRoot));
    }
}

void ScrollbarLayer::update(ResourceUpdateQueue& queue, const OcclusionTracker* occlusion, RenderingStats* stats)
{
    ContentsScalingLayer::update(queue, occlusion, stats);

    m_dirtyRect.Union(m_updateRect);
    if (contentBounds().IsEmpty())
        return;
    if (visibleContentRect().IsEmpty())
        return;

    createUpdaterIfNeeded();

    gfx::Rect contentRect = scrollbarLayerRectToContentRect(gfx::Rect(m_scrollbar->location(), bounds()));
    updatePart(m_backTrackUpdater.get(), m_backTrack.get(), contentRect, queue, stats);
    if (m_foreTrack && m_foreTrackUpdater)
        updatePart(m_foreTrackUpdater.get(), m_foreTrack.get(), contentRect, queue, stats);

    // Consider the thumb to be at the origin when painting.
    gfx::Rect thumbRect = m_geometry->thumbRect(m_scrollbar.get());
    m_thumbSize = thumbRect.size();
    gfx::Rect originThumbRect = scrollbarLayerRectToContentRect(gfx::Rect(thumbRect.size()));
    if (!originThumbRect.IsEmpty())
        updatePart(m_thumbUpdater.get(), m_thumb.get(), originThumbRect, queue, stats);

    m_dirtyRect = gfx::RectF();
}

}  // namespace cc