Cogs.Core
BaseRasterSource.h
1#pragma once
2
3#include "../Bridge/TerrainProviderFunctions.h"
4
5#include "Services/TaskManager.h"
6
7#include "Rendering/DataFormat.h"
8
9#include "Foundation/Collections/IntrusiveList.h"
10#include "Foundation/Collections/Pool.h"
11#include "Foundation/Platform/Threads.h"
12
13#include <glm/glm.hpp>
14
15#include <unordered_set>
16
17namespace Cogs::Core {
18 class Context;
19}
20
21namespace Cogs::Core::TerrainProvider {
22
23 struct Extent {
24 glm::dvec2 min{ 0.f };
25 glm::dvec2 max{ 0.f };
26 };
27
28 struct Tiling {
29 glm::dvec2 size;
30 unsigned width = 256;
31 unsigned height = 256;
32 unsigned overlapStart = 0;
33 unsigned overlapEnd = 0;
34 unsigned levels = 1;
35 };
36
37 struct CoordSys
38 {
39 enum struct Kind {
40 EPSG
41 } kind = Kind::EPSG;
42 unsigned id = 4326;
43 };
44
46 {
47 virtual ~BaseConfig() {}
48
49 virtual bool compatible(const BaseConfig* other) const;
50
51 Extent extent;
52 Tiling tiling;
53 CoordSys coordsys;
54 TextureFormat textureFormat = TextureFormat::Unknown;
55 MimeType mimeType = MimeType::None;
56 StringRef cacheKey = NoString;
57 StringRef name = NoString;
58 float noData = std::numeric_limits<float>::quiet_NaN();
59 bool offline = false;
60 };
61
62 struct TileId
63 {
64 unsigned level = 0;
65 unsigned i = 0;
66 unsigned j = 0;
67 };
68
69 bool equal(const TileId& id_a, const TileId& id_b);
70
72 {
73 TileLoadCallback* loadCallback = nullptr;
74 void* loadData = nullptr;
75 TileId id;
76 TaskId task = NoTask;
77 bool failure = false;
78 };
79
80 uint64_t tileKey(const TileId& id);
81
82 class ICache
83 {
84 public:
85 virtual ~ICache() {}
86
87 virtual bool getTile(Memory::MemoryBuffer& contents, MimeType& kind, const TileId& id) = 0;
88
89 virtual bool storeTile(const Memory::MemoryBuffer& contents, MimeType kind, const TileId& id, StringView debugLog) = 0;
90
91 };
92
94 {
95 friend class HandleRequestTask;
96 public:
97 BaseRasterSource(Context* context);
98 virtual ~BaseRasterSource();
99 Cogs::RasterSourceParameters getParameters() const override;
100 const CoordSys& getCoordSys() const { return coordsys; }
101 ErrorCode getErrorCode() const final;
102 void setErrorCode(ErrorCode newErrorCode);
103
104 protected:
105 bool init(const BaseConfig& conf, std::unique_ptr<ICache>&& icache);
106 void getConfig(BaseConfig& conf) const;
107 void addTile(const Cogs::Memory::MemoryBuffer& contents, MimeType kind, Request* req, StringView debugLog);
108 void addTileFailure(Request* req, StringView debugLog);
109 virtual void requestTile(Request* req) = 0;
110
111 Context* context = nullptr;
112 Extent extent;
113 Tiling tiling;
114 CoordSys coordsys;
115 TextureFormat textureFormat = TextureFormat::Unknown;
116 StringRef name = NoString;
117 float noDataValue = std::numeric_limits<float>::quiet_NaN();
118 bool emitDebugLog = true;
119
120 private:
121 std::unique_ptr<ICache> cache;
122 class StashService* stashService = nullptr;
123
124 // Request life-time:
125 //
126 // - Terrain engine invokes tileRequestCallbackFunc to request a tile:
127 // - Check requests.inflight to check if we already have a request for this tile, if yes, do nothing.
128 // - Create a new request
129 // - Insert tile id into inFlight
130 // - Insert into hasHandleTask
131 // - Issue HandleRequestTask
132 //
133 // - HandleRequestTask:
134 // - If tile is failure or tile is in cache:
135 // - Provide tile to terrain engine, data if in cache, nullptr if failure
136 // - Remove request from requests.inFlight
137 // - Remove request from requests.hasHandleTask
138 // - Destroy request and then exit
139 // - If tile is not in cache:
140 // - Remove request from requests.inFlight
141 // - Insert request into requests.waitingForProvider
142 // - Invoke provider's requestTile
143 //
144 // - Provider's request tile must either invoke addTile or addTileFailure
145 // - If addTileFailure, set failure to true
146 // - Stores tile in cache
147 // - Removes request from request.waitingForProvider
148 // - Insert into hasHandleTask
149 // - Issue HandleRequestTask
150 struct {
151 Cogs::Mutex mutex;
153 std::unordered_set<uint64_t> inFlight;
154 IntrusiveList hasHandleTask;
155 IntrusiveList waitingForProvider;
156 } requests;
157
158 struct {
159 mutable Cogs::Mutex mutex;
160 ErrorCode errorCode = ErrorCode::NoError;
161 } state;
162
163 static bool tileRequestCallbackFunc(void* userData, TileLoadCallback tileLoadCallback, int level, int x, int y);
164
165 };
166
167}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Pool used to store elements of ElementType.
Definition: Pool.h:17
Task id struct used to identify unique Task instances.
Definition: TaskManager.h:20