Beispiel #1
0
        FontFace AddFontFaceToCache()
        {
            FontFace fontFace       = CreateFontFace();
            FontFace bumpedFontFace = null;

            // NB: if the cache is busy, we simply return the new FontFace
            // without bothering to cache it.
            if (Interlocked.Increment(ref _mutex) == 1)
            {
                if (null == _fontFaceCache)
                {
                    _fontFaceCache = new FontFaceCacheEntry[_fontFaceCacheSize];
                }

                // Default to a slot that is not the MRU.
                _fontFaceCacheMRU = (_fontFaceCacheMRU + 1) % _fontFaceCacheSize;

                // Look for an empty slot.
                for (int i = 0; i < _fontFaceCacheSize; i++)
                {
                    if (_fontFaceCache[i].font == null)
                    {
                        _fontFaceCacheMRU = i;
                        break;
                    }
                }

                // Keep a reference to any discarded entry, clean it up after releasing
                // the mutex.
                bumpedFontFace = _fontFaceCache[_fontFaceCacheMRU].fontFace;

                // Record the new entry.
                _fontFaceCache[_fontFaceCacheMRU].font     = this;
                _fontFaceCache[_fontFaceCacheMRU].fontFace = fontFace;
                fontFace.AddRef();
            }
            Interlocked.Decrement(ref _mutex);

            // If the cache was full and we replaced an unreferenced entry, release it now.
            if (bumpedFontFace != null)
            {
                bumpedFontFace.Release();
            }

            return(fontFace);
        }
Beispiel #2
0
        FontFace LookupFontFaceSlow()
        {
            FontFace fontFace = null;

            for (int i = 0; i < _fontFaceCacheSize; i++)
            {
                if (_fontFaceCache[i].font == this)
                {
                    fontFace = _fontFaceCache[i].fontFace;
                    fontFace.AddRef();
                    _fontFaceCacheMRU = i;
                    break;
                }
            }

            return(fontFace);
        }