Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #define GL_GLEXT_PROTOTYPES
11 :
12 : #include "ogl_texturecache.hxx"
13 :
14 : #include <com/sun/star/geometry/IntegerSize2D.hpp>
15 :
16 : #include <GL/gl.h>
17 : #include <GL/glu.h>
18 : #include <GL/glext.h>
19 :
20 : using namespace ::com::sun::star;
21 :
22 : namespace oglcanvas
23 : {
24 0 : TextureCache::TextureCache() :
25 : maCache(101),
26 : mnMissCount(0),
27 0 : mnHitCount(0)
28 0 : {}
29 :
30 0 : TextureCache::~TextureCache()
31 : {
32 0 : flush();
33 0 : }
34 :
35 0 : void TextureCache::flush()
36 : {
37 : // un-bind any texture
38 0 : glBindTexture(GL_TEXTURE_2D, 0);
39 :
40 : // delete all cached textures
41 0 : TextureCacheMapT::const_iterator aCurr=maCache.begin();
42 0 : const TextureCacheMapT::const_iterator aEnd=maCache.end();
43 0 : while( aCurr != aEnd )
44 : {
45 0 : glDeleteTextures(1, &aCurr->second.nTexture);
46 0 : ++aCurr;
47 : }
48 :
49 0 : maCache.clear();
50 0 : mnMissCount = 0;
51 0 : mnHitCount = 0;
52 0 : }
53 :
54 0 : void TextureCache::prune()
55 : {
56 : // un-bind any texture
57 0 : glBindTexture(GL_TEXTURE_2D, 0);
58 :
59 : // delete already "old" textures, mark "new" entries "old"
60 0 : TextureCacheMapT::iterator aNext;
61 0 : TextureCacheMapT::iterator aCurr=maCache.begin();
62 0 : const TextureCacheMapT::iterator aEnd=maCache.end();
63 0 : while( aCurr != aEnd )
64 : {
65 0 : aNext=aCurr; ++aNext;
66 0 : if( aCurr->second.bOld )
67 : {
68 0 : glDeleteTextures(1, &aCurr->second.nTexture);
69 0 : maCache.erase(aCurr);
70 : }
71 : else
72 : {
73 0 : aCurr->second.bOld = true;
74 : }
75 0 : aCurr=aNext;
76 : }
77 :
78 0 : mnMissCount = 0;
79 0 : mnHitCount = 0;
80 0 : }
81 :
82 0 : unsigned int TextureCache::getTexture( const geometry::IntegerSize2D& rPixelSize,
83 : const sal_Int8* pPixel,
84 : sal_uInt32 nPixelCrc32) const
85 : {
86 0 : unsigned int nTexture(0);
87 :
88 : // texture already cached?
89 0 : TextureCacheMapT::iterator aCacheEntry;
90 0 : if( (aCacheEntry=maCache.find(nPixelCrc32)) == maCache.end() )
91 : {
92 : // nope, insert new entry
93 0 : glGenTextures(1, &nTexture);
94 0 : glBindTexture(GL_TEXTURE_2D, nTexture);
95 :
96 : // TODO(E3): handle limited texture sizes -
97 : // glGetIntegerv(GL_MAX_TEXTURE_SIZE)
98 : glTexImage2D(GL_TEXTURE_2D,
99 : 0,
100 : 4,
101 : rPixelSize.Width,
102 : rPixelSize.Height,
103 : 0,
104 : GL_RGBA,
105 : GL_UNSIGNED_INT_8_8_8_8_REV,
106 0 : pPixel);
107 :
108 0 : maCache[nPixelCrc32].nTexture = nTexture;
109 0 : ++mnMissCount;
110 :
111 0 : return nTexture;
112 : }
113 : else
114 : {
115 0 : nTexture = aCacheEntry->second.nTexture;
116 0 : aCacheEntry->second.bOld = false;
117 0 : ++mnHitCount;
118 : }
119 :
120 0 : return nTexture;
121 : }
122 : }
123 :
124 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|