summaryrefslogtreecommitdiffstats
path: root/canvas/source/opengl/ogl_texturecache.cxx
blob: 43fb7d8e2e25049e74722585bb7b3b4957e6361c (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <sal/config.h>

#include <epoxy/gl.h>

#include <com/sun/star/geometry/IntegerSize2D.hpp>

#include "ogl_texturecache.hxx"


using namespace ::com::sun::star;

namespace oglcanvas
{
    TextureCache::TextureCache() :
        maCache(101),
        mnMissCount(0),
        mnHitCount(0)
    {}

    TextureCache::~TextureCache()
    {
        flush();
    }

    void TextureCache::flush()
    {
        // un-bind any texture
        glBindTexture(GL_TEXTURE_2D, 0);

        // delete all cached textures
        for( const auto& rCache : maCache )
        {
            glDeleteTextures( 1, &rCache.second.nTexture );
        }

        maCache.clear();
        mnMissCount = 0;
        mnHitCount  = 0;
    }

    void TextureCache::prune()
    {
        // un-bind any texture
        glBindTexture(GL_TEXTURE_2D, 0);

        // delete already "old" textures, mark "new" entries "old"
        const TextureCacheMapT::const_iterator aEnd = maCache.end();
        for( auto aCurr = maCache.begin(); aCurr != aEnd; /* increment managed in loop */)
        {
            if( aCurr->second.bOld )
            {
                glDeleteTextures( 1, &aCurr->second.nTexture );
                aCurr = maCache.erase( aCurr );
            }
            else
            {
                aCurr->second.bOld = true;
                ++aCurr;
            }
        }

        mnMissCount = 0;
        mnHitCount  = 0;
    }

    unsigned int TextureCache::getTexture( const geometry::IntegerSize2D& rPixelSize,
                                           const sal_Int8*                pPixel,
                                           sal_uInt32                     nPixelCrc32) const
    {
        unsigned int nTexture(0);

        // texture already cached?
        TextureCacheMapT::iterator aCacheEntry;
        if( (aCacheEntry=maCache.find(nPixelCrc32)) == maCache.end() )
        {
            // nope, insert new entry
            glGenTextures(1, &nTexture);
            glBindTexture(GL_TEXTURE_2D, nTexture);

            // TODO(E3): handle limited texture sizes -
            // glGetIntegerv(GL_MAX_TEXTURE_SIZE)
            glTexImage2D(GL_TEXTURE_2D,
                         0,
                         4,
                         rPixelSize.Width,
                         rPixelSize.Height,
                         0,
                         GL_RGBA,
                         GL_UNSIGNED_INT_8_8_8_8_REV,
                         pPixel);

            maCache[nPixelCrc32].nTexture = nTexture;
            ++mnMissCount;

            return nTexture;
        }
        else
        {
            nTexture = aCacheEntry->second.nTexture;
            aCacheEntry->second.bOld = false;
            ++mnHitCount;
        }

        return nTexture;
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */