Overte C++ Documentation
qml/src/qml/impl/TextureCache.h
1 //
2 // Created by Bradley Austin Davis on 2018-01-04
3 // Copyright 2013-2018 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #pragma once
9 #ifndef hifi_QmlTextureCache_h
10 #define hifi_QmlTextureCache_h
11 
12 #include <list>
13 #include <mutex>
14 #include <functional>
15 #include <unordered_map>
16 #include <utility>
17 #include <cstdint>
18 
19 #include <QtCore/QSize>
20 
21 namespace hifi { namespace qml { namespace impl {
22 
23 class TextureAndFence : public std::pair<uint32_t, void*> {
24  using Parent = std::pair<uint32_t, void*>;
25 public:
26  TextureAndFence() : Parent(0, 0) {}
27  TextureAndFence(uint32_t texture, void* sync) : Parent(texture, sync) {};
28 };
29 
30 
31 class TextureCache {
32 public:
33  using Value = TextureAndFence;
34  using ValueList = std::list<Value>;
35  using Size = uint64_t;
36 
37  struct TextureSet {
38  // The number of surfaces with this size
39  size_t clientCount { 0 };
40  ValueList returnedTextures;
41  };
42 
43  void releaseSize(const QSize& size);
44  void acquireSize(const QSize& size);
45  uint32_t acquireTexture(const QSize& size);
46  void releaseTexture(const Value& textureAndFence);
47 
48  // For debugging
49  void report();
50  size_t getUsedTextureMemory();
51 private:
52  static size_t getMemoryForSize(const QSize& size);
53 
54  uint32_t createTexture(const QSize& size);
55  void destroyTexture(uint32_t texture);
56 
57  void destroy(const Value& textureAndFence);
58  void recycle();
59 
60  using Mutex = std::mutex;
61  using Lock = std::unique_lock<Mutex>;
62  std::atomic<int> _allTextureCount;
63  std::atomic<int> _activeTextureCount;
64  std::unordered_map<Size, TextureSet> _textures;
65  std::unordered_map<uint32_t, QSize> _textureSizes;
66  Mutex _mutex;
67  std::list<Value> _returnedTextures;
68  size_t _totalTextureUsage { 0 };
69 };
70 
71 }}} // namespace hifi::qml::impl
72 
73 #endif