cid#1607171 Data race condition
[LibreOffice.git] / canvas / source / opengl / ogl_texturecache.cxx
blob43fb7d8e2e25049e74722585bb7b3b4957e6361c
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 <sal/config.h>
12 #include <epoxy/gl.h>
14 #include <com/sun/star/geometry/IntegerSize2D.hpp>
16 #include "ogl_texturecache.hxx"
19 using namespace ::com::sun::star;
21 namespace oglcanvas
23 TextureCache::TextureCache() :
24 maCache(101),
25 mnMissCount(0),
26 mnHitCount(0)
29 TextureCache::~TextureCache()
31 flush();
34 void TextureCache::flush()
36 // un-bind any texture
37 glBindTexture(GL_TEXTURE_2D, 0);
39 // delete all cached textures
40 for( const auto& rCache : maCache )
42 glDeleteTextures( 1, &rCache.second.nTexture );
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 const TextureCacheMapT::const_iterator aEnd = maCache.end();
57 for( auto aCurr = maCache.begin(); aCurr != aEnd; /* increment managed in loop */)
59 if( aCurr->second.bOld )
61 glDeleteTextures( 1, &aCurr->second.nTexture );
62 aCurr = maCache.erase( aCurr );
64 else
66 aCurr->second.bOld = true;
67 ++aCurr;
71 mnMissCount = 0;
72 mnHitCount = 0;
75 unsigned int TextureCache::getTexture( const geometry::IntegerSize2D& rPixelSize,
76 const sal_Int8* pPixel,
77 sal_uInt32 nPixelCrc32) const
79 unsigned int nTexture(0);
81 // texture already cached?
82 TextureCacheMapT::iterator aCacheEntry;
83 if( (aCacheEntry=maCache.find(nPixelCrc32)) == maCache.end() )
85 // nope, insert new entry
86 glGenTextures(1, &nTexture);
87 glBindTexture(GL_TEXTURE_2D, nTexture);
89 // TODO(E3): handle limited texture sizes -
90 // glGetIntegerv(GL_MAX_TEXTURE_SIZE)
91 glTexImage2D(GL_TEXTURE_2D,
94 rPixelSize.Width,
95 rPixelSize.Height,
97 GL_RGBA,
98 GL_UNSIGNED_INT_8_8_8_8_REV,
99 pPixel);
101 maCache[nPixelCrc32].nTexture = nTexture;
102 ++mnMissCount;
104 return nTexture;
106 else
108 nTexture = aCacheEntry->second.nTexture;
109 aCacheEntry->second.bOld = false;
110 ++mnHitCount;
113 return nTexture;
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */