summaryrefslogtreecommitdiffstats
path: root/cc/nine_patch_layer.cc
blob: 56ae43cf215f8395b22b62d7c16b1b02dc4d9a24 (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
// 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/nine_patch_layer.h"

#include "cc/layer_tree_host.h"
#include "cc/nine_patch_layer_impl.h"
#include "cc/prioritized_resource.h"
#include "cc/resource_update.h"
#include "cc/resource_update_queue.h"

namespace cc {

scoped_refptr<NinePatchLayer> NinePatchLayer::create()
{
    return make_scoped_refptr(new NinePatchLayer());
}

NinePatchLayer::NinePatchLayer()
    : m_bitmapDirty(false)
{
}

NinePatchLayer::~NinePatchLayer()
{
}

scoped_ptr<LayerImpl> NinePatchLayer::createLayerImpl(LayerTreeImpl* treeImpl)
{
    return NinePatchLayerImpl::create(treeImpl, id()).PassAs<LayerImpl>();
}

void NinePatchLayer::setTexturePriorities(const PriorityCalculator& priorityCalc)
{
    if (m_resource && !m_resource->texture()->resourceManager()) {
        // Release the resource here, as it is no longer tied to a resource manager.
        m_resource.reset();
        if (!m_bitmap.isNull())
            createResource();
    } else if (m_needsDisplay && m_bitmapDirty && drawsContent()) {
        createResource();
    }

    if (m_resource) {
        m_resource->texture()->setRequestPriority(PriorityCalculator::uiPriority(true));
        // FIXME: Need to support swizzle in the shader for !PlatformColor::sameComponentOrder(textureFormat)
        GLenum textureFormat = layerTreeHost()->rendererCapabilities().bestTextureFormat;
        m_resource->texture()->setDimensions(gfx::Size(m_bitmap.width(), m_bitmap.height()), textureFormat);
    }
}

void NinePatchLayer::setBitmap(const SkBitmap& bitmap, const gfx::Rect& aperture) {
    m_bitmap = bitmap;
    m_imageAperture = aperture;
    m_bitmapDirty = true;
    setNeedsDisplay();
}

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

    if (m_resource && (m_bitmapDirty || m_resource->texture()->resourceId() == 0)) {
        gfx::Rect contentRect(gfx::Point(), gfx::Size(m_bitmap.width(), m_bitmap.height()));
        ResourceUpdate upload = ResourceUpdate::Create(m_resource->texture(), &m_bitmap, contentRect, contentRect, gfx::Vector2d());
        queue.appendFullUpload(upload);
        m_bitmapDirty = false;
    }
}

void NinePatchLayer::createUpdaterIfNeeded()
{
    if (m_updater)
        return;

    m_updater = ImageLayerUpdater::create();
}

void NinePatchLayer::createResource()
{
    DCHECK(!m_bitmap.isNull());
    createUpdaterIfNeeded();
    m_updater->setBitmap(m_bitmap);
    m_needsDisplay = false;

    if (!m_resource)
        m_resource = m_updater->createResource(layerTreeHost()->contentsTextureManager());
}

bool NinePatchLayer::drawsContent() const
{
    bool draws = !m_bitmap.isNull() && Layer::drawsContent() && m_bitmap.width() && m_bitmap.height();
    return draws;
}

void NinePatchLayer::pushPropertiesTo(LayerImpl* layer)
{
    Layer::pushPropertiesTo(layer);
    NinePatchLayerImpl* layerImpl = static_cast<NinePatchLayerImpl*>(layer);

    if (m_resource) {
        DCHECK(!m_bitmap.isNull());
        layerImpl->setResourceId(m_resource->texture()->resourceId());
        layerImpl->setLayout(gfx::Size(m_bitmap.width(), m_bitmap.height()), m_imageAperture);
    }
}

}