Cogs.Core
RasterSource.h
1#pragma once
2
3#include "RasterLevel.h"
4#include "RasterTile.h"
5
6#include "Rendering/IGraphicsDevice.h"
7
8#include <atomic>
9#include <list>
10#include <map>
11#include <mutex>
12#include <unordered_set>
13
14namespace Cogs
15{
16 struct RasterTileIdentifier;
17 struct TileLoadRequest;
18
20 {
21 enum EInvalidationFlags
22 {
23 None = 0,
24 InvalidateParents = 1 << 0,
25 };
26 };
27
29 {
30 std::vector<uint8_t> data;
32 TextureFormat format;
33 size_t size = 0;
34 float minZ;
35 float maxZ;
36 std::atomic<size_t> generation = 0;
37 };
38
39 typedef std::lock_guard<RasterSource> ReadLock;
40 typedef std::lock_guard<RasterSource> WriteLock;
41
42 // Use after rewrite:
43 //typedef std::shared_lock<RasterSource> ReadLock;
44 //typedef std::lock_guard<RasterSource> WriteLock;
45
47 {
48 public:
49 virtual ~RasterSource();
50
51 std::vector<RasterLevel> & getLevels() { return levels; }
52 const std::vector<RasterLevel> & getLevels() const { return levels; }
53
54 int getTileWidth() const { return tileLongitudePosts; }
55 int getTileHeight() const { return tileLatitudePosts; }
56
57 virtual void requestTile(const TileLoadRequest & request) = 0;
58
59 void lock() { readyMutex.lock(); }
60 void unlock() { readyMutex.unlock(); }
61
62 TextureHandle loadTexture(const unsigned char * data, const int width, const int height, const TextureFormat format, const size_t size);
63 void releaseTexture(TextureHandle texture, const size_t size);
64
65 RasterTile * getTile(const RasterTileIdentifier & identifier);
66 RasterTile * getTile(const size_t id);
67
68 void loadTileData(RasterTile * tile, size_t requestGeneration, std::vector<uint8_t> & data, TextureHandle textureHandle, TextureFormat format, size_t size);
69
70 void invalidateTile(const RasterTileIdentifier & id, InvalidationFlags::EInvalidationFlags flags = InvalidationFlags::InvalidateParents);
71
75
77 void purgeCache();
78
80 void purgeResidentCache();
81
83 void purgeOldData();
84
86 void releaseTileData(RasterTileData & tileData);
87
88 bool tryGetTextureHandle(const RasterTile * tile, TextureHandle & handle);
89
90 void updateTileUsage(const RasterTile * tile);
91 void refTile(RasterTile * tile);
92 void unrefTile(RasterTile * tile);
93
94 void removeTileUsageData(size_t code);
95 void removeTile(RasterTile * tile);
96 void evictLruTile();
97
98 RasterTileData * getTileData(RasterTile * tile);
99
100 virtual void allocateTileStorage(const size_t textureSize, std::vector<uint8_t> & buffer) = 0;
101 virtual void deallocateTileStorage(std::vector<uint8_t> & buffer) = 0;
102
103 void addSubscriber(struct RasterSourceSubscription * handler) { subscribers.insert(handler); }
104 void removeSubscriber(struct RasterSourceSubscription * handler) { subscribers.erase(handler); }
105
106 bool hasProcessingRequests() const { return processing.getCount() > 0; }
107
109 {
110 void lock() { ++count; }
111 void unlock() { --count; }
112 size_t getCount() const { return count.load(); }
113
114 private:
115 std::atomic<size_t> count = 0;
116 } processing;
117
118 std::unordered_set<struct RasterSourceSubscription *> subscribers;
119 std::mutex subscriberMutex;
120
121 IGraphicsDevice * device = nullptr;
122
123 std::atomic<size_t> textureBufferSize = 0;
124
125 TextureFormat format = TextureFormat::Unknown;
126
127 bool isHeight = false;
128
129 float minZ = 0;
130 float maxZ = 0;
131
132 std::string name;
133
134 std::atomic<size_t> generation = 0;
135
136 protected:
137 std::vector<RasterLevel> levels;
138
139 int tileLongitudePosts = 0;
140 int tileLatitudePosts = 0;
141
142 TextureHandle invalidTileHandle;
143
144 private:
145 std::map<size_t, RasterTile> activeTiles;
146 std::map<size_t, RasterTileData> residentTiles;
147 std::vector<RasterTileData> oldTileData;
148
149 std::list<size_t> lruList;
150 std::map<size_t, std::list<size_t>::iterator> lruMap;
151
152 std::mutex readyMutex;
153 };
154}
Represents a graphics device used to manage graphics resources and issue drawing commands.
void releaseTileData(RasterTileData &tileData)
Release the given tile data.
void purgeOldData()
Purges orphaned tile data that has been replaced with new contents.
void purgeResidentCache()
Purge tile data resident in memory and on the GPU.
void purgeCache()
Purge all cache, including resident data.
void cancelActiveRequests()
Cancels all active requests.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
static const Handle_t InvalidHandle
Represents an invalid handle.
Definition: Common.h:80