summaryrefslogtreecommitdiffstats
path: root/cc/CCPrioritizedTexture.cpp
blob: 50db1cb432dc22ba5cf0112027cc1daeee66073a (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
// 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 "config.h"

#include "CCPrioritizedTexture.h"

#include "CCPrioritizedTextureManager.h"
#include "CCPriorityCalculator.h"
#include <algorithm>

using namespace std;

namespace cc {

CCPrioritizedTexture::CCPrioritizedTexture(CCPrioritizedTextureManager* manager, IntSize size, GC3Denum format)
    : m_size(size)
    , m_format(format)
    , m_bytes(0)
    , m_priority(CCPriorityCalculator::lowestPriority())
    , m_isAbovePriorityCutoff(false)
    , m_isSelfManaged(false)
    , m_backing(0)
    , m_manager(0)
{
    // m_manager is set in registerTexture() so validity can be checked.
    ASSERT(format || size.isEmpty());
    if (format)
        m_bytes = CCTexture::memorySizeBytes(size, format);
    if (manager)
        manager->registerTexture(this);
}

CCPrioritizedTexture::~CCPrioritizedTexture()
{
    if (m_manager)
        m_manager->unregisterTexture(this);
}

void CCPrioritizedTexture::setTextureManager(CCPrioritizedTextureManager* manager)
{
    if (m_manager == manager)
        return;
    if (m_manager)
        m_manager->unregisterTexture(this);
    if (manager)
        manager->registerTexture(this);
}

void CCPrioritizedTexture::setDimensions(IntSize size, GC3Denum format)
{
    if (m_format != format || m_size != size) {
        m_isAbovePriorityCutoff = false;
        m_format = format;
        m_size = size;
        m_bytes = CCTexture::memorySizeBytes(size, format);
        ASSERT(m_manager || !m_backing);
        if (m_manager)
            m_manager->returnBackingTexture(this);
    }
}

bool CCPrioritizedTexture::requestLate()
{
    if (!m_manager)
        return false;
    return m_manager->requestLate(this);
}

void CCPrioritizedTexture::acquireBackingTexture(CCResourceProvider* resourceProvider)
{
    ASSERT(m_isAbovePriorityCutoff);
    if (m_isAbovePriorityCutoff)
        m_manager->acquireBackingTextureIfNeeded(this, resourceProvider);
}

CCResourceProvider::ResourceId CCPrioritizedTexture::resourceId() const
{
    if (m_backing)
        return m_backing->id();
    return 0;
}

void CCPrioritizedTexture::upload(CCResourceProvider* resourceProvider,
                                  const uint8_t* image, const IntRect& imageRect,
                                  const IntRect& sourceRect, const IntSize& destOffset)
{
    ASSERT(m_isAbovePriorityCutoff);
    if (m_isAbovePriorityCutoff)
        acquireBackingTexture(resourceProvider);
    ASSERT(m_backing);
    resourceProvider->upload(resourceId(), image, imageRect, sourceRect, destOffset);
}

void CCPrioritizedTexture::link(Backing* backing)
{
    ASSERT(backing);
    ASSERT(!backing->m_owner);
    ASSERT(!m_backing);

    m_backing = backing;
    m_backing->m_owner = this;
}

void CCPrioritizedTexture::unlink()
{
    ASSERT(m_backing);
    ASSERT(m_backing->m_owner == this);

    m_backing->m_owner = 0;
    m_backing = 0;
}

void CCPrioritizedTexture::setToSelfManagedMemoryPlaceholder(size_t bytes)
{
    setDimensions(IntSize(), GraphicsContext3D::RGBA);
    setIsSelfManaged(true);
    m_bytes = bytes;
}

} // namespace cc