// Decreases refcount and deallocates when it reaches 0. public void Release(TextureId id) { if (id.index < 0 || id.index >= m_Textures.Count) { Debug.LogError($"Attempted to release an invalid texture (index={id.index})."); return; } TextureInfo info = m_Textures[id.index]; if (info.refCount < 1) { Debug.LogError($"Attempted to release a texture (index={id.index}) that is not allocated."); return; } --info.refCount; if (info.refCount == 0) { if (!info.dynamic) { m_TextureToId.Remove(info.texture); } info.texture = null; info.dynamic = false; m_FreeIds.Push(id); } m_Textures[id.index] = info; }
TextureId AllocAndAcquire(Texture texture, bool dynamic) { TextureId id; TextureInfo info = new TextureInfo { texture = texture, dynamic = dynamic, refCount = 1 }; if (m_FreeIds.Count > 0) { id = m_FreeIds.Pop(); m_Textures[id.index] = info; } else { if (m_Textures.Count == maxTextures) { Debug.LogError($"Failed to allocate a {nameof(TextureId)} because the limit of {maxTextures} textures is reached."); return(TextureId.invalid); } id = new TextureId(m_Textures.Count); m_Textures.Add(info); } if (!dynamic) { m_TextureToId[texture] = id; } return(id); }
public void UpdateDynamic(TextureId id, Texture texture) { if (id.index < 0 || id.index >= m_Textures.Count) { Debug.LogError($"Attempted to update an invalid dynamic texture (index={id.index})."); return; } TextureInfo info = m_Textures[id.index]; if (!info.dynamic) { Debug.LogError($"Attempted to update a texture (index={id.index}) that is not dynamic."); return; } if (info.refCount < 1) { Debug.LogError($"Attempted to update a dynamic texture (index={id.index}) that is not allocated."); return; } info.texture = texture; m_Textures[id.index] = info; }
public override bool TryGetAtlas(VisualElement ve, Texture2D src, out TextureId atlas, out RectInt atlasRect) { if (m_Panels.Count == 0 || src == null) { atlas = TextureId.invalid; atlasRect = new RectInt(); return(false); } if (!isInitialized) { InitPages(); } if (m_Database.TryGetValue(src, out TextureInfo info)) { atlas = info.page.textureId; atlasRect = info.rect; ++info.counter; return(true); } // For the time being, we don't have a trilinear page. If users keep the filterMode check enabled, a // mip-mapped texture will NOT enter any of our pages. However, if they disable this check, we should try // to put it in the bilinear atlas first, because it will provide some interpolation, unlike the Point page. Allocator2D.Alloc2D alloc; if (IsTextureValid(src, FilterMode.Bilinear) && m_BilinearPage.TryAdd(src, out alloc, out atlasRect)) { info = TextureInfo.pool.Get(); info.alloc = alloc; info.counter = 1; info.page = m_BilinearPage; info.rect = atlasRect; m_Database[src] = info; atlas = m_BilinearPage.textureId; return(true); } if (IsTextureValid(src, FilterMode.Point) && m_PointPage.TryAdd(src, out alloc, out atlasRect)) { info = TextureInfo.pool.Get(); info.alloc = alloc; info.counter = 1; info.page = m_PointPage; info.rect = atlasRect; m_Database[src] = info; atlas = m_PointPage.textureId; return(true); } atlas = TextureId.invalid; atlasRect = new RectInt(); return(false); }
public override void ReturnAtlas(VisualElement ve, Texture2D src, TextureId atlas) { if (m_Database.TryGetValue(src, out TextureInfo info)) { --info.counter; if (info.counter == 0) { info.page.Remove(info.alloc); m_Database.Remove(src); TextureInfo.pool.Return(info); } } }
public Texture GetTexture(TextureId id) { if (id.index < 0 || id.index >= m_Textures.Count) { Debug.LogError($"Attempted to get an invalid texture (index={id.index})."); return(null); } TextureInfo info = m_Textures[id.index]; if (info.refCount < 1) { Debug.LogError($"Attempted to get a texture (index={id.index}) that is not allocated."); return(null); } return(info.texture); }
// Increases refcount public void Acquire(TextureId id) { if (id.index < 0 || id.index >= m_Textures.Count) { Debug.LogError($"Attempted to acquire an invalid texture (index={id.index})."); return; } TextureInfo info = m_Textures[id.index]; if (info.refCount < 1) { Debug.LogError($"Attempted to acquire a texture (index={id.index}) that is not allocated."); return; } ++info.refCount; m_Textures[id.index] = info; }
public bool Equals(TextureId other) { return(m_Index == other.m_Index); }
public virtual bool TryGetAtlas(VisualElement ctx, Texture2D src, out TextureId atlas, out RectInt atlasRect) { atlas = TextureId.invalid; atlasRect = new RectInt(); return(false); }
protected void SetDynamicTexture(TextureId id, Texture texture) { textureRegistry.UpdateDynamic(id, texture); }
protected void FreeDynamicTexture(TextureId id) { textureRegistry.Release(id); }
public virtual void ReturnAtlas(VisualElement ctx, Texture2D src, TextureId atlas) { }