Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resource_manager.hpp « graphics - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75eda9b086cb3f67eff09dbee4f248ae0c32becf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#pragma once

#include "opengl/storage.hpp"
#include "opengl/program_manager.hpp"
#include "glyph_cache.hpp"
#include "data_formats.hpp"
#include "defines.hpp"

//#include "../base/mutex.hpp"
#include "../base/resource_pool.hpp"

#include "../std/shared_ptr.hpp"
#include "../std/map.hpp"
#include "../std/string.hpp"
#include "../std/vector.hpp"


namespace graphics
{
  class ResourceCache;

  namespace gl
  {
    class BaseTexture;
    class Storage;
    class ProgramManager;
  }

  struct GlyphInfo;

  struct TTextureFactory : BasePoolElemFactory
  {
    size_t m_w;
    size_t m_h;
    graphics::DataFormat m_format;
    TTextureFactory(size_t w,
                    size_t h,
                    graphics::DataFormat format,
                    char const * resName,
                    size_t batchSize);
    shared_ptr<gl::BaseTexture> const Create();
  };

  struct TStorageFactory : BasePoolElemFactory
  {
    size_t m_vbSize;
    size_t m_ibSize;
    bool m_useSingleThreadedOGL;
    TStorageFactory(size_t vbSize,
                    size_t ibSize,
                    bool useSingleThreadedOGL,
                    char const * resName,
                    size_t batchSize);
    gl::Storage const Create();
    void BeforeMerge(gl::Storage const & e);
  };

  /// ---- Texture Pools ----

  /// Basic texture pool traits
  typedef BasePoolTraits<shared_ptr<gl::BaseTexture>, TTextureFactory> TBaseTexturePoolTraits;

  /// Fixed-Size texture pool
  typedef FixedSizePoolTraits<TTextureFactory, TBaseTexturePoolTraits > TFixedSizeTexturePoolTraits;
  typedef ResourcePoolImpl<TFixedSizeTexturePoolTraits> TFixedSizeTexturePoolImpl;

  /// On-Demand multi-threaded texture pool

  typedef AllocateOnDemandMultiThreadedPoolTraits<TTextureFactory, TBaseTexturePoolTraits> TOnDemandMultiThreadedTexturePoolTraits;
  typedef ResourcePoolImpl<TOnDemandMultiThreadedTexturePoolTraits> TOnDemandMultiThreadedTexturePoolImpl;

  /// On-Demand single-threaded texture pool
  /// (with postponed resource allocation)
  typedef AllocateOnDemandSingleThreadedPoolTraits<TTextureFactory, TBaseTexturePoolTraits> TOnDemandSingleThreadedTexturePoolTraits;
  typedef ResourcePoolImpl<TOnDemandSingleThreadedTexturePoolTraits> TOnDemandSingleThreadedTexturePoolImpl;

  /// Interface for texture pool
  typedef ResourcePool<shared_ptr<gl::BaseTexture> > TTexturePool;

  /// ---- Storage Pools ----

  /// Basic storage traits
  typedef BasePoolTraits<gl::Storage, TStorageFactory> TBaseStoragePoolTraits;

  /// Fixed-Size mergeable storage pool
  typedef SeparateFreePoolTraits<TStorageFactory, TBaseStoragePoolTraits> TSeparateFreeStoragePoolTraits;
  typedef FixedSizePoolTraits<TStorageFactory, TSeparateFreeStoragePoolTraits> TFixedSizeMergeableStoragePoolTraits;
  typedef ResourcePoolImpl<TFixedSizeMergeableStoragePoolTraits> TFixedSizeMergeableStoragePoolImpl;

  /// Fixed-Size non-mergeable storage pool
  typedef FixedSizePoolTraits<TStorageFactory, TBaseStoragePoolTraits> TFixedSizeNonMergeableStoragePoolTraits;
  typedef ResourcePoolImpl<TFixedSizeNonMergeableStoragePoolTraits> TFixedSizeNonMergeableStoragePoolImpl;

  /// On-Demand single-threaded storage pool
  /// (with postponed resource allocation and separate list of freed resources)
  typedef AllocateOnDemandSingleThreadedPoolTraits<TStorageFactory, TSeparateFreeStoragePoolTraits> TOnDemandSingleThreadedStoragePoolTraits;
  typedef ResourcePoolImpl<TOnDemandSingleThreadedStoragePoolTraits> TOnDemandSingleThreadedStoragePoolImpl;

  /// On-Demand multi-threaded storage pool
  typedef AllocateOnDemandMultiThreadedPoolTraits<TStorageFactory, TBaseStoragePoolTraits> TOnDemandMultiThreadedStoragePoolTraits;
  typedef ResourcePoolImpl<TOnDemandMultiThreadedStoragePoolTraits> TOnDemandMultiThreadedStoragePoolImpl;

  /// Interface for storage pool
  typedef ResourcePool<gl::Storage> TStoragePool;

  class ResourceManager
  {
  public:

    struct StoragePoolParams
    {
      size_t m_vbSize;
      size_t m_vertexSize;
      size_t m_ibSize;
      size_t m_indexSize;
      size_t m_storagesCount;
      EStorageType m_storageType;
      bool m_isDebugging;

      StoragePoolParams();
      StoragePoolParams(EStorageType storageType);
      StoragePoolParams(size_t vbSize,
                        size_t vertexSize,
                        size_t ibSize,
                        size_t indexSize,
                        size_t storagesCount,
                        EStorageType storageType,
                        bool isDebugging);

      bool isValid() const;
    };

    struct TexturePoolParams
    {
      size_t m_texWidth;
      size_t m_texHeight;
      size_t m_texCount;
      graphics::DataFormat m_format;
      ETextureType m_textureType;
      bool m_isDebugging;

      TexturePoolParams();
      TexturePoolParams(ETextureType textureType);
      TexturePoolParams(size_t texWidth,
                        size_t texHeight,
                        size_t texCount,
                        graphics::DataFormat format,
                        ETextureType textureType,
                        bool isDebugging);

      bool isValid() const;
    };

    struct GlyphCacheParams
    {
      string m_unicodeBlockFile;
      string m_whiteListFile;
      string m_blackListFile;

      size_t m_glyphCacheMemoryLimit;

      EDensity m_density;

      GlyphCacheParams();
      GlyphCacheParams(string const & unicodeBlockFile,
                       string const & whiteListFile,
                       string const & blackListFile,
                       size_t glyphCacheMemoryLimit,
                       EDensity density);
    };

    struct Params
    {
    private:

      string m_vendorName;
      string m_rendererName;
      string m_versionName;

      /// check non-strict matching upon vendorName and rendererName
      bool isGPU(char const * vendorName, char const * rendererName, bool strictMatch) const;
      bool isGPUVersion(char const * vendorName, char const * rendererName, char const * version);

    public:

      DataFormat m_rtFormat;
      DataFormat m_texFormat;
      DataFormat m_texRtFormat;
      bool m_useSingleThreadedOGL;
      bool m_useReadPixelsToSynchronize;

      size_t m_videoMemoryLimit;

      /// storages params
      vector<StoragePoolParams> m_storageParams;

      /// textures params
      vector<TexturePoolParams> m_textureParams;

      /// glyph caches params
      GlyphCacheParams m_glyphCacheParams;

      unsigned m_renderThreadsCount;
      unsigned m_threadSlotsCount;

      Params();

      void distributeFreeMemory(int freeVideoMemory);
      void checkDeviceCaps();
      int memoryUsage() const;
      int fixedMemoryUsage() const;
      void initScaleWeights();
    };

  private:

    typedef map<string, shared_ptr<gl::BaseTexture> > TStaticTextures;
    TStaticTextures m_staticTextures;
    string m_skinBuffer;

    void loadSkinInfoAndTexture(string const & skinFileName, EDensity density);

    //threads::Mutex m_mutex;

    vector<shared_ptr<TTexturePool> > m_texturePools;
    vector<shared_ptr<TStoragePool> > m_storagePools;

    struct ThreadSlot
    {
      shared_ptr<gl::ProgramManager> m_programManager;
      shared_ptr<GlyphCache> m_glyphCache;
    };

    vector<ThreadSlot> m_threadSlots;

    Params m_params;

  public:
    ResourceManager(Params const & p, string const & skinFileName, EDensity density);

    /// Use like static function with shared_ptr instead of this.
    /// @todo Check if we can remove shared_ptrs from this logic.
    static void loadSkin(shared_ptr<ResourceManager> const & rm,
                         vector<shared_ptr<ResourceCache> > & caches);

    void initThreadSlots(Params const & p);

    void initStoragePool(StoragePoolParams const & p, shared_ptr<TStoragePool> & pool);

    TStoragePool * storagePool(EStorageType type);

    void initTexturePool(TexturePoolParams const & p, shared_ptr<TTexturePool> & pool);

    TTexturePool * texturePool(ETextureType type);

    shared_ptr<gl::BaseTexture> const & getTexture(string const & name);

    Params const & params() const;

    GlyphCache * glyphCache(int threadSlot = 0);

    int renderThreadSlot(int threadNum) const;
    int guiThreadSlot() const;
    int cacheThreadSlot() const;

    void addFonts(vector<string> const & fontNames);

    /*
    void memoryWarning();
    void enterBackground();
    void enterForeground();
    */

    void updatePoolState();

    void cancel();

    bool useReadPixelsToSynchronize() const;

    shared_ptr<graphics::gl::BaseTexture> createRenderTarget(unsigned w, unsigned h);
    gl::ProgramManager * programManager(int threadSlot);
  };
}