aboutsummaryrefslogtreecommitdiffhomepage
path: root/obsolete/SkTextureCache.h
blob: aac3af4d1b5ac5a6b9688e0265d79dc943cec5c3 (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

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef SkTextureCache_DEFINED
#define SkTextureCache_DEFINED

#include "SkBitmap.h"
#include "SkPoint.h"
#include "SkGL.h"
#include "SkTDArray.h"

class SkTextureCache {
public:
    SkTextureCache(size_t maxCount, size_t maxSize);
    ~SkTextureCache();
    
    size_t getMaxCount() { return fTexCountMax; }
    size_t getMaxSize() { return fTexSizeMax; }

    void setMaxCount(size_t count);
    void setMaxSize(size_t size);
    
    /** Deletes all the caches. Pass true if the texture IDs are still valid,
        and if so, it will call glDeleteTextures. Pass false if the texture IDs
        are invalid (e.g. the gl-context has changed), in which case they will
        just be abandoned.
    */
    void deleteAllCaches(bool texturesAreValid);
    
    static int HashMask() { return kHashMask; }
    
    class Key {
    public:
        Key(const SkBitmap& bm) {
            fGenID = bm.getGenerationID();
            fOffset = bm.pixelRefOffset();
            fWH = (bm.width() << 16) | bm.height();
            this->computeHash();
        }
        
        int getHashIndex() const { return fHashIndex; }
        
        friend bool operator==(const Key& a, const Key& b) {
            return  a.fHash ==   b.fHash &&
                    a.fGenID ==  b.fGenID &&
                    a.fOffset == b.fOffset &&
                    a.fWH ==     b.fWH;
        }
        
        friend bool operator<(const Key& a, const Key& b) {
            if (a.fHash < b.fHash) {
                return true;
            } else if (a.fHash > b.fHash) {
                return false;
            }
            
            if (a.fGenID < b.fGenID) {
                return true;
            } else if (a.fGenID > b.fGenID) {
                return false;
            }
            
            if (a.fOffset < b.fOffset) {
                return true;
            } else if (a.fOffset > b.fOffset) {
                return false;
            }
            
            return a.fWH < b.fWH;
        }
        
    private:
        void computeHash() {
            uint32_t hash = fGenID ^ fOffset ^ fWH;
            fHash = hash;
            hash ^= hash >> 16;
            fHashIndex = hash & SkTextureCache::HashMask();
        }
        
        uint32_t    fHash;  // computed from the other fields
        uint32_t    fGenID;
        size_t      fOffset;
        uint32_t    fWH;
        // for indexing into the texturecache's fHash
        int fHashIndex;
    };

    class Entry {
    public:
        GLuint name() const { return fName; }
        SkPoint texSize() const { return fTexSize; }
        size_t memSize() const { return fMemSize; }
        const Key& getKey() const { return fKey; }

        // call this to clear the texture name, in case the context has changed
        // in which case we should't reference or delete this texture in GL
        void abandonTexture() { fName = 0; }

    private:
        Entry(const SkBitmap& bitmap);
        ~Entry();

        int lockCount() const { return fLockCount; }
        bool isLocked() const { return fLockCount > 0; }

        void lock() { fLockCount += 1; }
        void unlock() {
            SkASSERT(fLockCount > 0);
            fLockCount -= 1;
        }

    private:
        GLuint  fName;
        SkPoint fTexSize;
        Key     fKey;
        size_t  fMemSize;
        int     fLockCount;
        
        Entry*  fPrev;
        Entry*  fNext;
        
        friend class SkTextureCache;
    };
    
    Entry* lock(const SkBitmap&);
    void unlock(Entry*);
    
private:
    void purgeIfNecessary(size_t extraSize);
    
#ifdef SK_DEBUG
    void validate() const;
#else
    void validate() const {}
#endif

    Entry* fHead;
    Entry* fTail;
    
    // limits for the cache
    size_t  fTexCountMax;
    size_t  fTexSizeMax;
    
    // current values for the cache
    size_t  fTexCount;
    size_t  fTexSize;
    
    enum {
        kHashBits = 6,
        kHashCount = 1 << kHashBits,
        kHashMask = kHashCount - 1
    };
    mutable Entry* fHash[kHashCount];
    SkTDArray<Entry*> fSorted;
    
    /*  If we find the key, return the entry and ignore index. If we don't,
        return NULL and set index to the place to insert the entry in fSorted
    */
    Entry* find(const Key&, int* index) const;
    // returns index or <0 if not found. Does NOT update hash
    int findInSorted(const Key& key) const;
};

#endif