forked from CollaboraOnline/online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileCache.hpp
393 lines (324 loc) · 13.5 KB
/
TileCache.hpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <iosfwd>
#include <memory>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <Rectangle.hpp>
#include "Log.hpp"
#include "Common.hpp"
#include "TileDesc.hpp"
class ClientSession;
// The cache cares about only some properties.
struct TileDescCacheCompareEq final
{
inline bool operator()(const TileDesc& l, const TileDesc& r) const
{
return l.getPart() == r.getPart() &&
l.getWidth() == r.getWidth() &&
l.getHeight() == r.getHeight() &&
l.getTilePosX() == r.getTilePosX() &&
l.getTilePosY() == r.getTilePosY() &&
l.getTileWidth() == r.getTileWidth() &&
l.getTileHeight() == r.getTileHeight() &&
l.getNormalizedViewId() == r.getNormalizedViewId() &&
l.getEditMode() == r.getEditMode();
}
};
// The cache cares about only some properties.
struct TileDescCacheHasher final
{
inline size_t operator()(const TileDesc& t) const
{
size_t hash = t.getPart();
hash = (hash << 5) + hash + t.getEditMode();
hash = (hash << 5) + hash + t.getWidth();
hash = (hash << 5) + hash + t.getHeight();
hash = (hash << 5) + hash + t.getTilePosX();
hash = (hash << 5) + hash + t.getTilePosY();
hash = (hash << 5) + hash + t.getTileWidth();
hash = (hash << 5) + hash + t.getTileHeight();
hash = (hash << 5) + hash + t.getNormalizedViewId();
return hash;
}
};
struct TileData
{
TileData(TileWireId start, const char *data, const size_t size)
{
appendBlob(start, data, size);
}
// Add a frame or delta and - return the size change
ssize_t appendBlob(TileWireId id, const char *data, const size_t dataSize)
{
size_t oldCacheSize = size();
assert (dataSize >= 1); // kit provides us a 'Z' or a 'D' or a png
if (isKeyframe(data, dataSize))
{
LOG_TRC("received key-frame - clearing tile");
_wids.clear();
_offsets.clear();
_deltas.clear();
}
else
{
LOG_TRC("received delta of size " << dataSize << " - appending to existing " << _wids.size());
// Issues #5532 and #5831 Replace assert with a log message
// Remove assert and allow delta messages to be handled even if
// there is no keyframe. Although it might make sense to skip
// delta messages if there is no keyframe, that causes some
// content, at least in Impress documents, to not render.
if (!_wids.size())
LOG_DBG("no underlying keyframe!");
}
size_t oldSize = size();
// If we have an empty delta at the end - then just
// bump the associated wid. There is no risk to sending
// an empty delta twice.x
if (dataSize == 1 && // just a 'D'
_offsets.size() > 1 &&
_offsets.back() == _deltas.size())
{
LOG_TRC("received empty delta - bumping wid from " << _wids.back() << " to " << id);
_wids.back() = id;
}
else
{
// FIXME: too many/large deltas means we should reset -
// but not here - when requesting the tiles.
_wids.push_back(id);
_offsets.push_back(_deltas.size());
if (dataSize > 1)
{
_deltas.resize(oldSize + dataSize - 1);
std::memcpy(_deltas.data() + oldSize, data + 1, dataSize - 1);
}
}
// FIXME: possible race - should store a seq. from the invalidation(s) ?
_valid = true;
return size() - oldCacheSize;
}
bool isPng() const { return (_deltas.size() > 1 &&
_deltas[0] == (char)0x89); }
static bool isKeyframe(const char *data, size_t dataSize)
{
// keyframe or png
return dataSize > 0 && (data[0] == 'Z' || data[0] == (char)0x89);
}
bool isValid() const { return _valid; }
void invalidate() { _valid = false; }
bool _valid; // not true - waiting for a new tile if in view.
std::vector<TileWireId> _wids;
std::vector<size_t> _offsets; // offset of the start of data
BlobData _deltas; // first item is a key-frame, followed by deltas at _offsets
size_t size()
{
return _deltas.size();
}
const BlobData &data()
{
return _deltas;
}
/// if we send changes since this seq - do we need to first send the keyframe ?
bool needsKeyframe(TileWireId since)
{
return since < _wids[0];
}
bool appendChangesSince(std::vector<char> &output, TileWireId since)
{
size_t i;
for (i = 0; since != 0 && i < _wids.size() && _wids[i] <= since; ++i);
if (i >= _wids.size())
{
// We don't throttle delta sending - yet the code thinks we do still.
// We just send all the deltas we have on top of the keyframe.
// LOG_WRN("odd outcome - requested for a later id " << since <<
// " than the last known: " << ((_wids.size() > 0) ? _wids.back() : -1));
return false;
}
else
{
size_t offset = _offsets[i];
if (i != _offsets.size() - 1)
LOG_TRC("appending from " << i << " to " << (_offsets.size() - 1) <<
" from wid: " << _wids[i] << " to wid: " << since <<
" from offset: " << offset << " to " << _deltas.size());
size_t extra = _deltas.size() - offset;
size_t dest = output.size();
output.resize(output.size() + extra);
std::memcpy(output.data() + dest, _deltas.data() + offset, extra);
return true;
}
}
void dumpState(std::ostream& os)
{
if (_wids.size() < 2)
os << "keyframe";
else {
os << "deltas: ";
for (size_t i = 0; i < _wids.size(); ++i)
{
os << i << ": " << _wids[i] << " -> " << _offsets[i] << " ";
}
}
}
};
using Tile = std::shared_ptr<TileData>;
/// Handles the caching of tiles of one document.
class TileCache
{
struct TileBeingRendered;
std::shared_ptr<TileBeingRendered> findTileBeingRendered(const TileDesc& tile);
public:
/// When the docURL is a non-file:// url, the timestamp has to be provided by the caller.
/// For file:// url's, it's ignored.
/// When it is missing for non-file:// url, it is assumed the document must be read, and no cached value used.
TileCache(std::string docURL, const std::chrono::system_clock::time_point& modifiedTime,
bool dontCache = false);
~TileCache();
/// Completely clear the cache contents.
void clear();
TileCache(const TileCache&) = delete;
TileCache& operator=(const TileCache&) = delete;
/// Subscribes if no subscription exists and returns true.
/// Otherwise returns false to signify a subscription already exists.
bool subscribeToTileRendering(const TileDesc& tile, const std::shared_ptr<ClientSession>& subscriber,
const std::chrono::steady_clock::time_point& now);
/// Cancels all tile requests by the given subscriber.
std::string cancelTiles(const std::shared_ptr<ClientSession>& subscriber);
/// Find the tile with this description
Tile lookupTile(const TileDesc& tile);
void saveTileAndNotify(const TileDesc& tile, const char* data, size_t size);
enum StreamType {
Font,
Style,
CmdValues,
Last
};
/// Get the content of a cache file.
/// @param content Valid only when the call returns true.
/// @return true when the file actually exists
bool getTextStream(StreamType type, const std::string& fileName, std::string& content);
// Save some text into a file in the cache directory
void saveTextStream(StreamType type, const std::string& fileName, const std::vector<char>& data);
// Saves a font / style / etc rendering
void saveStream(StreamType type, const std::string& name, const char* data, size_t size);
/// Return the data if we have it, or nothing.
Blob lookupCachedStream(StreamType type, const std::string& name);
// The tiles parameter is an invalidatetiles: message as sent by the child process
void invalidateTiles(const std::string& tiles, int normalizedViewId);
/// Parse invalidateTiles message to rectangle and associated attributes of the invalidated area
static Util::Rectangle parseInvalidateMsg(const std::string& tiles, int &part, int &mode, TileWireId &wid);
/// Forget the tile being rendered if it is the latest version we expect.
void forgetTileBeingRendered(const TileDesc& descForKitReply,
const std::shared_ptr<TileCache::TileBeingRendered>& tileBeingRendered);
size_t countTilesBeingRenderedForSession(const std::shared_ptr<ClientSession>& session,
const std::chrono::steady_clock::time_point& now);
bool hasTileBeingRendered(const TileDesc& tileDesc, const std::chrono::steady_clock::time_point *now = nullptr) const;
int getTileBeingRenderedVersion(const TileDesc& tileDesc);
/// Set the high watermark for tilecache size
void setMaxCacheSize(size_t cacheSize);
/// Get the current memory use.
size_t getMemorySize() const { return _cacheSize; }
// Debugging bits ...
void dumpState(std::ostream& os);
void setThreadOwner(const std::thread::id& id) { _owner = id; }
void assertCacheSize();
private:
void ensureCacheSize();
static size_t itemCacheSize(const Tile &tile);
void invalidateTiles(int part, int mode, int x, int y, int width, int height, int normalizedViewId);
/// Lookup tile in our cache.
Tile findTile(const TileDesc &desc);
static std::string cacheFileName(const TileDesc& tileDesc);
static bool parseCacheFileName(const std::string& fileName, int& part, int& mode,
int& width, int& height, int& tilePosX, int& tilePosY,
int& tileWidth, int& tileHeight, int& nviewid);
/// Extract location from fileName, and check if it intersects with [x, y, width, height].
static bool intersectsTile(const TileDesc &tileDesc, int part, int mode, int x, int y,
int width, int height, int normalizedViewId);
Tile saveDataToCache(const TileDesc& desc, const char* data, size_t size);
void saveDataToStreamCache(StreamType type, const std::string& fileName, const char* data,
size_t size);
const std::string _docURL;
std::thread::id _owner;
const bool _dontCache;
/// Approximate size of tilecache in bytes
size_t _cacheSize;
/// Maximum (high watermark) size of the tilecache in bytes
size_t _maxCacheSize;
// FIXME: should we have a tile-desc to WID map instead and a simpler lookup ?
std::unordered_map<TileDesc, Tile,
TileDescCacheHasher,
TileDescCacheCompareEq> _cache;
// FIXME: TileBeingRendered contains TileDesc too ...
std::unordered_map<TileDesc, std::shared_ptr<TileBeingRendered>,
TileDescCacheHasher,
TileDescCacheCompareEq> _tilesBeingRendered;
// old-style file-name to data grab-bag.
std::map<std::string, Blob> _streamCache[static_cast<int>(StreamType::Last)];
};
/// Tracks view-port area tiles to track which we last
/// sent to avoid re-sending an existing delta causing grief
class ClientDeltaTracker final {
public:
// FIXME: could be a simple 2d TileWireId array for better packing.
std::unordered_set<TileDesc,
TileDescCacheHasher,
TileDescCacheCompareEq> _cache;
ClientDeltaTracker() {
}
// FIXME: only need to store this for the current viewports
void updateViewPort( /* ... */ )
{
// copy the set to another while filtering I guess.
}
/// return wire-id of last tile sent - or 0 if not present
/// update last-tile sent wire-id to curSeq if found.
TileWireId updateTileSeq(const TileDesc &desc)
{
auto it = _cache.find(desc);
if (it == _cache.end())
{
_cache.insert(desc);
return 0;
}
const TileWireId curSeq = desc.getWireId();
TileWireId last = it->getWireId();
// id is not included in the hash.
auto pDesc = const_cast<TileDesc *>(&(*it));
pDesc->setWireId(curSeq);
return last;
}
void resetTileSeq(const TileDesc &desc)
{
auto it = _cache.find(desc);
if (it == _cache.end())
return;
// id is not included in the hash.
auto pDesc = const_cast<TileDesc *>(&(*it));
pDesc->setWireId(0);
}
};
inline std::ostream& operator<< (std::ostream& os, const Tile& tile)
{
if (!tile)
os << "nullptr";
else
os << "keyframe id " << tile->_wids[0] <<
" size: " << tile->_deltas.size() <<
" deltas: " << (tile->_wids.size() - 1);
return os;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */