2015-03-12 09:18:35 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2015-03-12 09:18:35 -05:00
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <iosfwd>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <memory>
|
2017-06-22 07:58:50 -05:00
|
|
|
#include <thread>
|
2015-05-05 06:57:51 -05:00
|
|
|
#include <string>
|
2019-02-15 14:55:47 -06:00
|
|
|
#include <unordered_map>
|
2015-03-12 09:18:35 -05:00
|
|
|
|
2018-05-31 13:20:09 -05:00
|
|
|
#include <Rectangle.hpp>
|
2016-05-15 16:50:32 -05:00
|
|
|
|
2016-05-21 08:41:23 -05:00
|
|
|
#include "TileDesc.hpp"
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-05-16 19:49:36 -05:00
|
|
|
class ClientSession;
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2019-02-15 14:55:47 -06:00
|
|
|
class TileCacheDesc : public TileDesc
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TileCacheDesc(const TileDesc ©)
|
|
|
|
: TileDesc(copy)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// The cache cares about only some properties.
|
|
|
|
bool operator==(const TileCacheDesc& other) const
|
|
|
|
{
|
|
|
|
return _part == other._part &&
|
|
|
|
_width == other._width &&
|
|
|
|
_height == other._height &&
|
|
|
|
_tilePosX == other._tilePosX &&
|
|
|
|
_tilePosY == other._tilePosY &&
|
|
|
|
_tileWidth == other._tileWidth &&
|
2019-11-09 23:09:00 -06:00
|
|
|
_tileHeight == other._tileHeight &&
|
|
|
|
_normalizedViewId == other._normalizedViewId;
|
2019-02-15 14:55:47 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The cache cares about only some properties.
|
|
|
|
struct TileCacheDescCompareEqual
|
|
|
|
{
|
|
|
|
bool operator()(const TileCacheDesc &l, const TileCacheDesc &r) const { return l == r; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// The cache cares about only some properties.
|
|
|
|
struct TileCacheDescHasher
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const TileCacheDesc &t) const
|
|
|
|
{
|
|
|
|
size_t hash = t.getPart();
|
|
|
|
|
|
|
|
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();
|
2019-11-09 23:09:00 -06:00
|
|
|
hash = (hash << 5) + hash + t.getNormalizedViewId();
|
2019-02-15 14:55:47 -06:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-21 08:41:23 -05:00
|
|
|
/// Handles the caching of tiles of one document.
|
2015-03-12 18:34:42 -05:00
|
|
|
class TileCache
|
2015-03-12 09:18:35 -05:00
|
|
|
{
|
2016-04-25 04:04:25 -05:00
|
|
|
struct TileBeingRendered;
|
|
|
|
|
2019-02-15 14:55:47 -06:00
|
|
|
bool hasTileBeingRendered(const std::shared_ptr<TileCache::TileBeingRendered>& tileBeingRendered);
|
2016-05-15 17:47:08 -05:00
|
|
|
std::shared_ptr<TileBeingRendered> findTileBeingRendered(const TileDesc& tile);
|
2016-04-25 04:04:25 -05:00
|
|
|
|
2015-03-12 18:34:42 -05:00
|
|
|
public:
|
2019-02-14 13:01:43 -06:00
|
|
|
typedef std::shared_ptr<std::vector<char>> Tile;
|
|
|
|
|
2015-08-04 13:37:05 -05:00
|
|
|
/// 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.
|
2019-08-30 12:37:55 -05:00
|
|
|
TileCache(const std::string& docURL, const std::chrono::system_clock::time_point& modifiedTime, bool dontCache = false);
|
2016-03-26 06:50:13 -05:00
|
|
|
~TileCache();
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2019-02-14 15:40:33 -06:00
|
|
|
/// Completely clear the cache contents.
|
|
|
|
void clear();
|
2017-04-12 13:04:30 -05:00
|
|
|
|
2016-03-25 21:56:18 -05:00
|
|
|
TileCache(const TileCache&) = delete;
|
2019-10-28 08:26:15 -05:00
|
|
|
TileCache& operator=(const TileCache&) = delete;
|
2016-03-25 21:56:18 -05:00
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
/// Subscribes if no subscription exists and returns the version number.
|
|
|
|
/// Otherwise returns 0 to signify a subscription exists.
|
2016-10-29 20:15:00 -05:00
|
|
|
void subscribeToTileRendering(const TileDesc& tile, const std::shared_ptr<ClientSession>& subscriber);
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2018-09-26 15:15:26 -05:00
|
|
|
/// Create the TileBeingRendered object for the given tile indicating that the tile was sent to
|
|
|
|
/// the kit for rendering. Note: subscribeToTileRendering calls this internally, so you don't need
|
2020-04-26 15:55:16 -05:00
|
|
|
/// to call this method if you need also to subscribe for the rendered tile.
|
2018-09-26 15:15:26 -05:00
|
|
|
void registerTileBeingRendered(const TileDesc& tile);
|
|
|
|
|
2016-08-30 22:15:44 -05:00
|
|
|
/// Cancels all tile requests by the given subscriber.
|
2016-10-29 20:15:00 -05:00
|
|
|
std::string cancelTiles(const std::shared_ptr<ClientSession>& subscriber);
|
2016-08-30 22:15:44 -05:00
|
|
|
|
2019-02-14 13:01:43 -06:00
|
|
|
/// Find the tile with this description
|
|
|
|
Tile lookupTile(const TileDesc& tile);
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
void saveTileAndNotify(const TileDesc& tile, const char* data, const size_t size);
|
2016-04-23 11:11:11 -05:00
|
|
|
|
2019-02-15 14:55:47 -06:00
|
|
|
enum StreamType {
|
|
|
|
Font,
|
|
|
|
Style,
|
|
|
|
CmdValues,
|
|
|
|
Last
|
|
|
|
};
|
|
|
|
|
2017-01-17 09:42:31 -06:00
|
|
|
/// Get the content of a cache file.
|
|
|
|
/// @param content Valid only when the call returns true.
|
|
|
|
/// @return true when the file actually exists
|
2019-02-15 14:55:47 -06:00
|
|
|
bool getTextStream(StreamType type, const std::string& fileName, std::string& content);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
// Save some text into a file in the cache directory
|
2019-02-15 14:55:47 -06:00
|
|
|
void saveTextStream(StreamType type, const std::string& text, const std::string& fileName);
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-11-27 08:12:44 -06:00
|
|
|
// Saves a font / style / etc rendering
|
2019-02-15 14:55:47 -06:00
|
|
|
void saveStream(StreamType type, const std::string& name, const char* data, size_t size);
|
2015-11-27 08:12:44 -06:00
|
|
|
|
2019-02-14 13:01:43 -06:00
|
|
|
/// Return the tile data if we have it, or nothing.
|
2019-02-15 14:55:47 -06:00
|
|
|
Tile lookupCachedStream(StreamType type, const std::string& name);
|
2015-11-27 08:12:44 -06:00
|
|
|
|
2015-05-28 10:39:05 -05:00
|
|
|
// The tiles parameter is an invalidatetiles: message as sent by the child process
|
2019-10-15 07:35:35 -05:00
|
|
|
void invalidateTiles(const std::string& tiles, int normalizedViewId);
|
2015-05-28 10:39:05 -05:00
|
|
|
|
2018-05-31 13:20:09 -05:00
|
|
|
/// Parse invalidateTiles message to a part number and a rectangle of the invalidated area
|
|
|
|
static std::pair<int, Util::Rectangle> parseInvalidateMsg(const std::string& tiles);
|
|
|
|
|
2019-03-02 12:26:08 -06:00
|
|
|
void forgetTileBeingRendered(const std::shared_ptr<TileCache::TileBeingRendered>& tileBeingRendered);
|
2019-02-15 14:55:47 -06:00
|
|
|
double getTileBeingRenderedElapsedTimeMs(const TileDesc &tileDesc) const;
|
2019-03-02 12:26:08 -06:00
|
|
|
|
2019-02-15 14:55:47 -06:00
|
|
|
size_t countTilesBeingRenderedForSession(const std::shared_ptr<ClientSession>& session);
|
|
|
|
bool hasTileBeingRendered(const TileDesc& tileDesc);
|
|
|
|
int getTileBeingRenderedVersion(const TileDesc& tileDesc);
|
2018-09-26 15:15:26 -05:00
|
|
|
|
2019-10-28 08:26:15 -05:00
|
|
|
/// Set the high watermark for tilecache size
|
|
|
|
void setMaxCacheSize(size_t cacheSize);
|
|
|
|
|
|
|
|
/// Get the current memory use.
|
|
|
|
size_t getMemorySize() const { return _cacheSize; }
|
|
|
|
|
2019-02-15 13:42:00 -06:00
|
|
|
// Debugging bits ...
|
|
|
|
void dumpState(std::ostream& os);
|
2017-06-22 07:58:50 -05:00
|
|
|
void setThreadOwner(const std::thread::id &id) { _owner = id; }
|
|
|
|
void assertCorrectThread();
|
2019-10-28 08:26:15 -05:00
|
|
|
void assertCacheSize();
|
2017-06-22 07:58:50 -05:00
|
|
|
|
2016-05-01 08:30:31 -05:00
|
|
|
private:
|
2019-10-28 08:26:15 -05:00
|
|
|
void ensureCacheSize();
|
|
|
|
static size_t itemCacheSize(const Tile &tile);
|
|
|
|
|
2019-10-15 07:35:35 -05:00
|
|
|
void invalidateTiles(int part, int x, int y, int width, int height, int normalizedViewId);
|
2015-05-28 10:39:05 -05:00
|
|
|
|
2019-02-14 15:40:33 -06:00
|
|
|
/// Lookup tile in our cache.
|
2019-02-15 14:55:47 -06:00
|
|
|
TileCache::Tile findTile(const TileDesc &desc);
|
|
|
|
|
|
|
|
/// Lookup tile in our stream cache.
|
|
|
|
TileCache::Tile findStreamTile(StreamType type, const std::string &fileName);
|
2019-02-14 15:40:33 -06:00
|
|
|
|
2019-02-15 14:55:47 -06:00
|
|
|
/// Removes the named stream from the cache
|
|
|
|
void removeStream(StreamType type, const std::string& fileName);
|
2016-02-10 11:51:24 -06:00
|
|
|
|
2019-02-15 14:55:47 -06:00
|
|
|
static std::string cacheFileName(const TileDesc& tileDesc);
|
2019-10-15 07:35:35 -05:00
|
|
|
static bool parseCacheFileName(const std::string& fileName, int& part, int& width, int& height, int& tilePosX, int& tilePosY, int& tileWidth, int& tileHeight, int& nviewid);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
|
|
|
/// Extract location from fileName, and check if it intersects with [x, y, width, height].
|
2019-10-15 07:35:35 -05:00
|
|
|
static bool intersectsTile(const TileDesc &tileDesc, int part, int x, int y, int width, int height, int normalizedViewId);
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2019-02-15 14:55:47 -06:00
|
|
|
void saveDataToCache(const TileDesc &desc, const char *data, const size_t size);
|
|
|
|
void saveDataToStreamCache(StreamType type, const std::string &fileName, const char *data, const size_t size);
|
2015-08-04 13:37:05 -05:00
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
const std::string _docURL;
|
2015-06-24 15:05:49 -05:00
|
|
|
|
2017-06-22 07:58:50 -05:00
|
|
|
std::thread::id _owner;
|
2016-04-15 10:24:00 -05:00
|
|
|
|
2019-02-14 15:40:33 -06:00
|
|
|
bool _dontCache;
|
2019-10-28 08:26:15 -05:00
|
|
|
|
|
|
|
/// Approximate size of tilecache in bytes
|
|
|
|
size_t _cacheSize;
|
|
|
|
|
|
|
|
/// Maximum (high watermark) size of the tilecache in bytes
|
|
|
|
size_t _maxCacheSize;
|
|
|
|
|
2019-02-15 14:55:47 -06:00
|
|
|
// FIXME: should we have a tile-desc to WID map instead and a simpler lookup ?
|
|
|
|
std::unordered_map<TileCacheDesc, Tile,
|
|
|
|
TileCacheDescHasher,
|
|
|
|
TileCacheDescCompareEqual> _cache;
|
|
|
|
// FIXME: TileBeingRendered contains TileDesc too ...
|
|
|
|
std::unordered_map<TileCacheDesc, std::shared_ptr<TileBeingRendered>,
|
|
|
|
TileCacheDescHasher,
|
|
|
|
TileCacheDescCompareEqual> _tilesBeingRendered;
|
|
|
|
|
|
|
|
// old-style file-name to data grab-bag.
|
|
|
|
std::map<std::string, Tile> _streamCache[(int)StreamType::Last];
|
2015-03-12 09:18:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|