Skip to content

Commit

Permalink
Normalize use of std::shared_ptr, and fix a problem with threadlocal …
Browse files Browse the repository at this point in the history
…and the GDAL driver
  • Loading branch information
gwaldron committed Nov 19, 2024
1 parent 72924cf commit ba710ad
Show file tree
Hide file tree
Showing 57 changed files with 228 additions and 207 deletions.
2 changes: 1 addition & 1 deletion src/rocky/AzureImageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ AzureImageLayer::createImageImplementation(const TileKey& key, const IOOptions&
if (image_rr.status.failed())
return image_rr.status;

shared_ptr<Image> image = image_rr.value;
auto image = image_rr.value;

if (image)
return GeoImage(image, key.extent());
Expand Down
2 changes: 1 addition & 1 deletion src/rocky/BingImageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ BingImageLayer::createImageImplementation(const TileKey& key, const IOOptions& i
return image_rr.status;
}

shared_ptr<Image> image = image_rr.value;
auto image = image_rr.value;

if (image)
return GeoImage(image, key.extent());
Expand Down
23 changes: 9 additions & 14 deletions src/rocky/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@

namespace ROCKY_NAMESPACE
{
template<class T> using shared_ptr = std::shared_ptr<T>;
template<class T> using shared_constptr = std::shared_ptr<const T>;
template<class T> using weak_ptr = std::weak_ptr<T>;
template<class T> using weak_constptr = std::weak_ptr<const T>;

//! application-wide unique ID.
using UID = std::int32_t;

Expand Down Expand Up @@ -81,28 +76,28 @@ namespace ROCKY_NAMESPACE
Inherit(const Args&... args) : PARENT(args...) { }
public:
using super = Inherit<PARENT, ME>;
using ptr = shared_ptr<ME>;
using constptr = shared_ptr<const ME>;
using weakptr = weak_ptr<ME>;
using constweakptr = weak_ptr<const ME>;
using ptr = std::shared_ptr<ME>;
using constptr = std::shared_ptr<const ME>;
using weakptr = std::weak_ptr<ME>;
using constweakptr = std::weak_ptr<const ME>;
template<typename... Args>
static shared_ptr<ME> create(Args&&... args) {
static std::shared_ptr<ME> create(Args&&... args) {
return std::make_shared<ME>(std::forward<Args>(args)...);
}
template<typename T>
static shared_ptr<ME> cast(shared_ptr<T>& rhs) {
static std::shared_ptr<ME> cast(std::shared_ptr<T>& rhs) {
return std::dynamic_pointer_cast<ME>(rhs);
}
template<typename T>
static shared_ptr<const ME> cast(shared_ptr<const T>& rhs) {
static std::shared_ptr<const ME> cast(std::shared_ptr<const T>& rhs) {
return std::dynamic_pointer_cast<const ME>(rhs);
}
template<typename T>
static shared_ptr<ME> cast(const shared_ptr<T>& rhs) {
static std::shared_ptr<ME> cast(const std::shared_ptr<T>& rhs) {
return std::dynamic_pointer_cast<ME>(rhs);
}
template<typename T>
static const shared_ptr<const ME> cast(const shared_ptr<const T>& rhs) {
static const std::shared_ptr<const ME> cast(const std::shared_ptr<const T>& rhs) {
return std::dynamic_pointer_cast<const ME>(rhs);
}
};
Expand Down
14 changes: 7 additions & 7 deletions src/rocky/ElevationLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ ElevationLayer::normalizeNoDataValues(Heightfield* hf) const
}
}

shared_ptr<Heightfield>
std::shared_ptr<Heightfield>
ElevationLayer::assembleHeightfield(const TileKey& key, const IOOptions& io) const
{
std::shared_ptr<HeightfieldMosaic> output;
Expand Down Expand Up @@ -385,7 +385,7 @@ ElevationLayer::createHeightfieldInKeyProfile(
const IOOptions& io) const
{
GeoHeightfield result;
shared_ptr<Heightfield> hf;
std::shared_ptr<Heightfield> hf;

auto my_profile = profile();
if (!my_profile.valid() || !isOpen())
Expand All @@ -411,7 +411,7 @@ ElevationLayer::createHeightfieldInKeyProfile(
else
{
// If the profiles are different, use a compositing method to assemble the tile.
shared_ptr<Heightfield> hf = assembleHeightfield(key, io);
auto hf = assembleHeightfield(key, io);
result = GeoHeightfield(hf, key.extent());
}

Expand Down Expand Up @@ -452,7 +452,7 @@ namespace
{
struct LayerData
{
shared_ptr<ElevationLayer> layer;
std::shared_ptr<ElevationLayer> layer;
TileKey key;
bool isFallback;
int index;
Expand Down Expand Up @@ -509,7 +509,7 @@ namespace

bool
ElevationLayerVector::populateHeightfield(
shared_ptr<Heightfield> hf,
std::shared_ptr<Heightfield> hf,
std::vector<float>* resolutions,
const TileKey& key,
const Profile& haeProfile,
Expand Down Expand Up @@ -877,8 +877,8 @@ ElevationLayerVector::populateHeightfield(
return realData;
}

shared_ptr<Heightfield>
ElevationLayer::decodeMapboxRGB(shared_ptr<Image> image) const
std::shared_ptr<Heightfield>
ElevationLayer::decodeMapboxRGB(std::shared_ptr<Image> image) const
{
if (!image || !image->valid())
return nullptr;
Expand Down
9 changes: 4 additions & 5 deletions src/rocky/ElevationLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace ROCKY_NAMESPACE
}

//! Decodes a mapbox RGB encoded heightfield image into a heightfield.
shared_ptr<Heightfield> decodeMapboxRGB(shared_ptr<Image> image) const;
std::shared_ptr<Heightfield> decodeMapboxRGB(std::shared_ptr<Image> image) const;

virtual ~ElevationLayer() { }

Expand All @@ -94,7 +94,7 @@ namespace ROCKY_NAMESPACE
private:
void construct(const std::string& JSON, const IOOptions& io);

shared_ptr<Heightfield> assembleHeightfield(
std::shared_ptr<Heightfield> assembleHeightfield(
const TileKey& key,
const IOOptions& io) const;

Expand All @@ -116,8 +116,7 @@ namespace ROCKY_NAMESPACE
/**
* Vector of elevation layers, with added methods.
*/
class ROCKY_EXPORT ElevationLayerVector :
public std::vector<shared_ptr<ElevationLayer>>
class ROCKY_EXPORT ElevationLayerVector : public std::vector<std::shared_ptr<ElevationLayer>>
{
public:
/**
Expand All @@ -133,7 +132,7 @@ namespace ROCKY_NAMESPACE
* @return True if "hf" was populated, false if no real data was available for key
*/
bool populateHeightfield(
shared_ptr<Heightfield> in_out_hf,
std::shared_ptr<Heightfield> in_out_hf,
std::vector<float>* resolutions,
const TileKey& key,
const Profile& hae_profile,
Expand Down
20 changes: 8 additions & 12 deletions src/rocky/GDAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ namespace ROCKY_NAMESPACE
namespace GDAL
{

Result<shared_ptr<Image>> readImage(unsigned char* data, std::size_t length, const std::string& name)
Result<std::shared_ptr<Image>> readImage(unsigned char* data, std::size_t length, const std::string& name)
{
shared_ptr<Image> result;
std::shared_ptr<Image> result;

// generate a unique name for our temporary vsimem file:
static std::atomic_int rgen(0);
Expand Down Expand Up @@ -332,13 +332,10 @@ namespace ROCKY_NAMESPACE

//...................................................................

GDAL::Driver::Driver()
{
_threadId = std::this_thread::get_id();
}

GDAL::Driver::~Driver()
{
//Log()->info("GDAL::Driver::~Driver, _warped={}, _src={}", (std::uintptr_t)_warpedDS, (std::uintptr_t)_srcDS);

if (_warpedDS)
GDALClose(_warpedDS);
else if (_srcDS)
Expand Down Expand Up @@ -572,9 +569,7 @@ GDAL::Driver::open(
SRS srs(warpedSRSWKT);
if (srs.valid())
{
_profile = Profile(
srs,
Box(minX, minY, maxX, maxY));
_profile = Profile(srs, Box(minX, minY, maxX, maxY));
}

if (!_profile.valid())
Expand Down Expand Up @@ -676,6 +671,7 @@ GDAL::Driver::open(
// Get the linear units of the SRS for scaling elevation values
_linearUnits = 1.0; // srs.getReportedLinearUnits();

_open = true;
return StatusOK;
}

Expand Down Expand Up @@ -864,7 +860,7 @@ GDAL::Driver::intersects(const TileKey& key)
return key.extent().intersects(_extents);
}

Result<shared_ptr<Image>>
Result<std::shared_ptr<Image>>
GDAL::Driver::createImage(const TileKey& key, unsigned tileSize, const IOOptions& io)
{
if (maxDataLevel.has_value() && key.levelOfDetail() > maxDataLevel)
Expand All @@ -877,7 +873,7 @@ GDAL::Driver::createImage(const TileKey& key, unsigned tileSize, const IOOptions
return Status(Status::ResourceUnavailable);
}

shared_ptr<Image> image;
std::shared_ptr<Image> image;

const bool invert = true;

Expand Down
35 changes: 31 additions & 4 deletions src/rocky/GDAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,35 @@ namespace ROCKY_NAMESPACE
optional<unsigned> maxDataLevel = 30;

//! Constructs a new driver
Driver();
Driver() = default;

virtual ~Driver();

Driver(const Driver&) = delete;

Driver(Driver&& rhs) noexcept {
_srcDS = rhs._srcDS;
_warpedDS = rhs._warpedDS;
_linearUnits = rhs._linearUnits;
_extents = rhs._extents;
_bounds = rhs._bounds;
_profile = rhs._profile;
_layer = rhs._layer;
_external = std::move(rhs._external);
_name = std::move(rhs._name);
_open = rhs._open;
rhs._srcDS = nullptr;
rhs._warpedDS = nullptr;
rhs._open = false;
rhs._profile = {};
rhs._layer = nullptr;
rhs._external = nullptr;
}

bool isOpen() const {
return _open;
}

//! Opens and initializes the connection to the dataset
Status open(
const std::string& name,
Expand All @@ -85,7 +111,7 @@ namespace ROCKY_NAMESPACE
const IOOptions& io);

//! Creates an image if possible
Result<shared_ptr<Image>> createImage(
Result<std::shared_ptr<Image>> createImage(
const TileKey& key,
unsigned tileSize,
const IOOptions& io);
Expand All @@ -103,6 +129,7 @@ namespace ROCKY_NAMESPACE
bool intersects(const TileKey&);
float getInterpolatedValue(GDALRasterBand* band, double x, double y, bool applyOffset = true);

bool _open = false;
GDALDataset* _srcDS = nullptr;
GDALDataset* _warpedDS = nullptr;
double _linearUnits = 1.0;
Expand All @@ -112,15 +139,15 @@ namespace ROCKY_NAMESPACE
Box _bounds;
Profile _profile;
const LayerBase* _layer;
shared_ptr<ExternalDataset> _external;
std::shared_ptr<ExternalDataset> _external;
std::string _name;
std::thread::id _threadId;

const std::string& getName() const { return _name; }
};

//! Reads an image from raw data using the specified GDAL driver.
extern ROCKY_EXPORT Result<shared_ptr<Image>> readImage(
extern ROCKY_EXPORT Result<std::shared_ptr<Image>> readImage(
unsigned char* data,
std::size_t len,
const std::string& gdal_driver);
Expand Down
2 changes: 1 addition & 1 deletion src/rocky/GDALElevationLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace
template<typename T>
Status openOnThisThread(
const T* layer,
shared_ptr<GDAL::Driver>& driver,
std::shared_ptr<GDAL::Driver>& driver,
Profile* profile,
DataExtentList* out_dataExtents,
const IOOptions& io)
Expand Down
2 changes: 1 addition & 1 deletion src/rocky/GDALElevationLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace ROCKY_NAMESPACE
//! Called by the constructors
void construct(const std::string& JSON, const IOOptions& io);

mutable util::ThreadLocal<shared_ptr<GDAL::Driver>> _drivers;
mutable util::ThreadLocal<std::shared_ptr<GDAL::Driver>> _drivers;
friend class GDAL::Driver;
};

Expand Down
25 changes: 16 additions & 9 deletions src/rocky/GDALImageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@ namespace
template<typename T>
Status openOnThisThread(
const T* layer,
shared_ptr<GDAL::Driver>& driver,
GDAL::Driver& driver,
//std::shared_ptr<GDAL::Driver>& driver,
Profile* profile,
DataExtentList* out_dataExtents,
const IOOptions& io)
{
driver = std::make_shared<GDAL::Driver>();
Log()->info("Opening GDAL driver on thread {}", std::hash<std::thread::id>{}(std::this_thread::get_id()));

//driver = std::make_shared<GDAL::Driver>();

if (layer->maxDataLevel().has_value())
driver->maxDataLevel = layer->maxDataLevel();
{
driver.maxDataLevel = layer->maxDataLevel();
}

Status status = driver->open(
Status status = driver.open(
layer->name(),
layer,
layer->tileSize(),
Expand All @@ -43,9 +48,11 @@ namespace
if (status.failed())
return status;

if (driver->profile().valid() && profile != nullptr)
//ROCKY_HARD_ASSERT(driver != nullptr);

if (driver.profile().valid() && profile != nullptr)
{
*profile = driver->profile();
*profile = driver.profile();
}

return StatusOK;
Expand Down Expand Up @@ -151,16 +158,16 @@ GDALImageLayer::createImageImplementation(const TileKey& key, const IOOptions& i
return status();

auto& driver = _drivers.value();
if (driver == nullptr)
if (!driver.isOpen())
{
// calling openImpl with NULL params limits the setup
// since we already called this during openImplementation
openOnThisThread(this, driver, nullptr, nullptr, io);
}

if (driver)
if (driver.isOpen())
{
auto image = driver->createImage(key, _tileSize, io);
auto image = driver.createImage(key, _tileSize, io);
if (image.value)
return GeoImage(image.value, key.extent());
}
Expand Down
3 changes: 2 additions & 1 deletion src/rocky/GDALImageLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace ROCKY_NAMESPACE
//! Called by the constructors
void construct(const std::string& JSON, const IOOptions& io);

mutable util::ThreadLocal<shared_ptr<GDAL::Driver>> _drivers;
mutable util::ThreadLocal<GDAL::Driver> _drivers;
//mutable util::ThreadLocal<std::shared_ptr<GDAL::Driver>> _drivers;
friend class GDAL::Driver;
};

Expand Down
Loading

0 comments on commit ba710ad

Please sign in to comment.