Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / opengl / ogl_texturecache.cxx
blob5fe3581d417902227a7716067a0cc44a69debbae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include "ogl_texturecache.hxx"
12 #include <com/sun/star/geometry/IntegerSize2D.hpp>
14 #include <GL/glew.h>
16 using namespace ::com::sun::star;
18 namespace oglcanvas
20 TextureCache::TextureCache() :
21 maCache(101),
22 mnMissCount(0),
23 mnHitCount(0)
26 TextureCache::~TextureCache()
28 flush();
31 void TextureCache::flush()
33 // un-bind any texture
34 glBindTexture(GL_TEXTURE_2D, 0);
36 // delete all cached textures
37 TextureCacheMapT::const_iterator aCurr=maCache.begin();
38 const TextureCacheMapT::const_iterator aEnd=maCache.end();
39 while( aCurr != aEnd )
41 glDeleteTextures(1, &aCurr->second.nTexture);
42 ++aCurr;
45 maCache.clear();
46 mnMissCount = 0;
47 mnHitCount = 0;
50 void TextureCache::prune()
52 // un-bind any texture
53 glBindTexture(GL_TEXTURE_2D, 0);
55 // delete already "old" textures, mark "new" entries "old"
56 TextureCacheMapT::iterator aNext;
57 TextureCacheMapT::iterator aCurr=maCache.begin();
58 const TextureCacheMapT::iterator aEnd=maCache.end();
59 while( aCurr != aEnd )
61 aNext=aCurr; ++aNext;
62 if( aCurr->second.bOld )
64 glDeleteTextures(1, &aCurr->second.nTexture);
65 maCache.erase(aCurr);
67 else
69 aCurr->second.bOld = true;
71 aCurr=aNext;
74 mnMissCount = 0;
75 mnHitCount = 0;
78 unsigned int TextureCache::getTexture( const geometry::IntegerSize2D& rPixelSize,
79 const sal_Int8* pPixel,
80 sal_uInt32 nPixelCrc32) const
82 unsigned int nTexture(0);
84 // texture already cached?
85 TextureCacheMapT::iterator aCacheEntry;
86 if( (aCacheEntry=maCache.find(nPixelCrc32)) == maCache.end() )
88 // nope, insert new entry
89 glGenTextures(1, &nTexture);
90 glBindTexture(GL_TEXTURE_2D, nTexture);
92 // TODO(E3): handle limited texture sizes -
93 // glGetIntegerv(GL_MAX_TEXTURE_SIZE)
94 glTexImage2D(GL_TEXTURE_2D,
97 rPixelSize.Width,
98 rPixelSize.Height,
100 GL_RGBA,
101 GL_UNSIGNED_INT_8_8_8_8_REV,
102 pPixel);
104 maCache[nPixelCrc32].nTexture = nTexture;
105 ++mnMissCount;
107 return nTexture;
109 else
111 nTexture = aCacheEntry->second.nTexture;
112 aCacheEntry->second.bOld = false;
113 ++mnHitCount;
116 return nTexture;
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */