Files
OpenSpace/modules/globebrowsing/tile/asynctiledataprovider.cpp
T
Kalle Bladin 4f903ac030 Feature/globebrowsing (#334)
Layer support for globe browsing:

Add layers using the function openspace.globebrowsing.addLayer
Delete layers using openspace.globebrowsing.deleteLayer
Layer type does not necessarily have to be of tile type. For example solidcolor does not use tiles
Blend modes for layers are Normal, Add, Subtract, Multiply, Color
Layer adjustments to affect layers. The current only active one is chroma key to cut out a color from the layer. Transfer functions or clipping masks are examples of layer adjustments for the future.
Support for adding layer specifications for quickly accessing GIBS layers:
openspace.globebrowsing.createGibsGdalXml
openspace.globebrowsing.createTemporalGibsGdalXml
The arguments for these functions are currently strings. Would it be better to use a lua dictionary?
No data values for height layers are correctly regarded (can be seen on Earth. No longer bumps on the poles)
Other minor things:

Worked a bit on point globe to render globes at large distances. Currently not in use and doesn't have anything to do with the other things.
Concurrent job manager takes a thread pool as argument and not a pointer to one. This is because the concurrent job manager needs to have ownership of the thread pool for correct deinitialization. Will cause breaking change for users of concurrent job manager if merged in to master.


* Add ability to add layers programatically.

* Clean up

* Fix order of deletion in concurrent job manager and clean up

* Can create by level tile provider with empty dictionary.

* Add script to add GIBS datasets.

* Start working with layer adjustment

* Update mod files

* More work on point globe

* Add script to create temporal GIBS datasets.

* Update temporal tile provider to be able to take gdal descriptions without file path.

* Add adjustment property to layers.

* Rename adjustment layer

* Add adjustment code to all layer groups

* Remove caching of gdal datasets due to cluttering of folders

* Document layer support

* Update Mars mod

* Make Mercury great again.

* Cleanup and add blend mode Color

* Enable setting of layeradjustment and blend mode from mod files.

* No more use for grayscale color overlays. Use grayscale layer with color blend mode instead.

* Clean up mod files

* Clean up

* Clean up

* No need for grayscale layers. Reading grayscale in to rgb instead for color layers.

* Remove unused layer groups

* Correctly read to grayscale layers

* Update globe mod files

* Rename ColorOverlays to Overlays.

* Clean up

* Clean up

* Solve compilation error
2017-07-10 20:34:39 +02:00

270 lines
11 KiB
C++

/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/globebrowsing/tile/asynctiledataprovider.h>
#include <modules/globebrowsing/other/lruthreadpool.h>
#include <modules/globebrowsing/tile/tileloadjob.h>
#include <modules/globebrowsing/tile/rawtiledatareader/rawtiledatareader.h>
#include <modules/globebrowsing/tile/tiletextureinitdata.h>
#include <modules/globebrowsing/cache/memoryawaretilecache.h>
#include <openspace/engine/openspaceengine.h>
#include <openspace/engine/moduleengine.h>
#include <ghoul/logging/logmanager.h>
namespace openspace {
namespace globebrowsing {
namespace {
const char* _loggerCat = "AsyncTileDataProvider";
}
AsyncTileDataProvider::AsyncTileDataProvider(const std::string& name,
const std::shared_ptr<RawTileDataReader> rawTileDataReader)
: _name(name)
, _rawTileDataReader(rawTileDataReader)
, _concurrentJobManager(LRUThreadPool<TileIndex::TileHashKey>(1, 10))
, _pboContainer(nullptr)
, _resetMode(ResetMode::ShouldResetAllButRawTileDataReader)
, _shouldBeDeleted(false)
{
_globeBrowsingModule = OsEng.moduleEngine().module<GlobeBrowsingModule>();
performReset(ResetRawTileDataReader::No);
}
std::shared_ptr<RawTileDataReader> AsyncTileDataProvider::getRawTileDataReader() const {
return _rawTileDataReader;
}
bool AsyncTileDataProvider::enqueueTileIO(const TileIndex& tileIndex) {
if (_resetMode == ResetMode::ShouldNotReset && satisfiesEnqueueCriteria(tileIndex)) {
if (_pboContainer) {
char* dataPtr = static_cast<char*>(_pboContainer->mapBuffer(
tileIndex.hashKey(), PixelBuffer::Access::WriteOnly));
if (dataPtr) {
auto job = std::make_shared<TileLoadJob>(_rawTileDataReader, tileIndex,
dataPtr);
_concurrentJobManager.enqueueJob(job, tileIndex.hashKey());
_enqueuedTileRequests.insert(tileIndex.hashKey());
}
else {
return false;
}
}
else {
auto job = std::make_shared<TileLoadJob>(_rawTileDataReader, tileIndex);
_concurrentJobManager.enqueueJob(job, tileIndex.hashKey());
_enqueuedTileRequests.insert(tileIndex.hashKey());
}
return true;
}
return false;
}
std::vector<std::shared_ptr<RawTile>> AsyncTileDataProvider::getRawTiles() {
std::vector<std::shared_ptr<RawTile>> readyResults;
std::shared_ptr<RawTile> finishedJob = popFinishedRawTile();
while (finishedJob) {
readyResults.push_back(finishedJob);
finishedJob = popFinishedRawTile();
}
return readyResults;
}
std::shared_ptr<RawTile> AsyncTileDataProvider::popFinishedRawTile() {
if (_concurrentJobManager.numFinishedJobs() > 0) {
// Now the tile load job looses ownerwhip of the data pointer
std::shared_ptr<RawTile> product =
_concurrentJobManager.popFinishedJob()->product();
TileIndex::TileHashKey key = product->tileIndex.hashKey();
// No longer enqueued. Remove from set of enqueued tiles
_enqueuedTileRequests.erase(key);
// Pbo is still mapped. Set the id for the raw tile
if (_pboContainer) {
product->pbo = _pboContainer->idOfMappedBuffer(key);
// Now we are finished with the mapping of this pbo
_pboContainer->unMapBuffer(key);
}
else {
product->pbo = 0;
if (product->error != RawTile::ReadError::None) {
delete [] product->imageData;
return nullptr;
}
}
return product;
}
else
return nullptr;
}
bool AsyncTileDataProvider::satisfiesEnqueueCriteria(const TileIndex& tileIndex) {
// Only satisfies if it is not already enqueued. Also bumps the request to the top.
bool alreadyEnqueued = _concurrentJobManager.touch(tileIndex.hashKey());
// Concurrent job manager can start jobs which will pop them from enqueued, however
// they are still in _enqueuedTileRequests until finished
bool notFoundAmongEnqueued =
_enqueuedTileRequests.find(tileIndex.hashKey()) == _enqueuedTileRequests.end();
return !alreadyEnqueued && notFoundAmongEnqueued;
}
void AsyncTileDataProvider::endUnfinishedJobs() {
std::vector<TileIndex::TileHashKey> unfinishedJobs =
_concurrentJobManager.getKeysToUnfinishedJobs();
for (TileIndex::TileHashKey unfinishedJob : unfinishedJobs) {
// Unmap unfinished jobs
if (_pboContainer) {
_pboContainer->unMapBuffer(unfinishedJob);
}
// When erasing the job before
_enqueuedTileRequests.erase(unfinishedJob);
}
}
void AsyncTileDataProvider::endEnqueuedJobs() {
std::vector<TileIndex::TileHashKey> enqueuedJobs =
_concurrentJobManager.getKeysToEnqueuedJobs();
for (const TileIndex::TileHashKey& enqueuedJob : enqueuedJobs) {
// Unmap unfinished jobs
if (_pboContainer) {
_pboContainer->unMapBuffer(enqueuedJob);
}
// When erasing the job before
_enqueuedTileRequests.erase(enqueuedJob);
}
}
void AsyncTileDataProvider::updatePboUsage() {
bool usingPbo = _pboContainer != nullptr;
bool shouldUsePbo = _globeBrowsingModule->tileCache()->shouldUsePbo();
// If changed, we need to reset the async tile data provider.
// No need to reset the raw tile data reader when changing PBO usage.
if (usingPbo != shouldUsePbo &&
_resetMode != ResetMode::ShouldResetAllButRawTileDataReader) {
_resetMode = ResetMode::ShouldResetAllButRawTileDataReader;
LINFO(std::string("PBO usage updated, prepairing for resetting of tile reader ") +
"'" + _name + "'");
}
}
void AsyncTileDataProvider::update() {
endUnfinishedJobs();
// May reset
switch (_resetMode) {
case ResetMode::ShouldResetAll: {
// Clean all finished jobs
getRawTiles();
// Only allow resetting if there are no jobs currently running
if (_enqueuedTileRequests.size() == 0) {
performReset(ResetRawTileDataReader::Yes);
LINFO(std::string("Tile data reader ") + "'" + _name + "'" +
" reset successfully.");
}
break;
}
case ResetMode::ShouldResetAllButRawTileDataReader: {
// Clean all finished jobs
getRawTiles();
// Only allow resetting if there are no jobs currently running
if (_enqueuedTileRequests.size() == 0) {
performReset(ResetRawTileDataReader::No);
LINFO(std::string("Tile data reader ") + "'" + _name + "'" +
" reset successfully.");
}
break;
}
case ResetMode::ShouldBeDeleted: {
// Clean all finished jobs
getRawTiles();
// Only allow resetting if there are no jobs currently running
if (_enqueuedTileRequests.size() == 0) {
_shouldBeDeleted = true;
}
break;
}
case ResetMode::ShouldNotReset: {
updatePboUsage();
break;
}
default:
break;
}
}
void AsyncTileDataProvider::reset() {
// Can not clear concurrent job manager in case there are threads running. therefore
// we need to wait until _enqueuedTileRequests is empty before finishing up.
_resetMode = ResetMode::ShouldResetAll;
endEnqueuedJobs();
LINFO(std::string("Prepairing for resetting of tile reader ") +
"'" + _name + "'");
}
void AsyncTileDataProvider::prepairToBeDeleted() {
_resetMode = ResetMode::ShouldBeDeleted;
endEnqueuedJobs();
}
bool AsyncTileDataProvider::shouldBeDeleted() {
return _shouldBeDeleted;
}
void AsyncTileDataProvider::performReset(ResetRawTileDataReader resetRawTileDataReader) {
ghoul_assert(_enqueuedTileRequests.size() == 0, "No enqueued requests left");
// Re-initialize PBO container
if (_globeBrowsingModule->tileCache()->shouldUsePbo()) {
size_t pboNumBytes = _rawTileDataReader->tileTextureInitData().totalNumBytes();
_pboContainer = std::make_unique<PixelBufferContainer<TileIndex::TileHashKey>>(
pboNumBytes, PixelBuffer::Usage::StreamDraw, 10
);
}
else {
_pboContainer = nullptr;
}
// Reset raw tile data reader
if (resetRawTileDataReader == ResetRawTileDataReader::Yes) {
_rawTileDataReader->reset();
}
// Finished resetting
_resetMode = ResetMode::ShouldNotReset;
}
float AsyncTileDataProvider::noDataValueAsFloat() const {
return _rawTileDataReader->noDataValueAsFloat();
}
} // namespace globebrowsing
} // namespace openspace