Skip to content

Commit

Permalink
Add very strict resource creation mutex
Browse files Browse the repository at this point in the history
The mutex is very strict for now and therefore causes a noticeable slowdown. We will ease the restrictions as we find out where exactly it is needed. (Sounds familiar?)
  • Loading branch information
kb173 committed Apr 15, 2020
1 parent 887d082 commit 2419613
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/geodot.cpp
Expand Up @@ -2,6 +2,10 @@
#include "RasterTileExtractor.h"
#include <algorithm> // For std::clamp
#include <functional> // For std::hash
#include <mutex> // For std::mutex

std::mutex resource_creation_mutex;


using namespace godot;

Expand Down Expand Up @@ -36,6 +40,8 @@ Ref<GeoImage> Geodot::get_image(String path, String file_ending,
load_mutex->unlock();
return image_cache[image_hash];
} else {
resource_creation_mutex.lock();

// This strange __internal_constructor call is required to prevent a memory leak
// See https://github.com/GodotNativeTools/godot-cpp/issues/215
Ref<GeoImage> image = Ref<GeoImage>::__internal_constructor(GeoImage::_new());
Expand All @@ -46,16 +52,18 @@ Ref<GeoImage> Geodot::get_image(String path, String file_ending,
top_left_x, top_left_y, size_meters,
img_size, interpolation_type);

load_mutex->unlock();

if (raster == nullptr) {
resource_creation_mutex.unlock();
load_mutex->unlock();
Godot::print_error("No valid data was available for the requested path and position!", "Geodot::get_image", "geodot.cpp", 26);
return image;
}

image->set_raster(raster, interpolation_type);

image_cache[image_hash] = image;

resource_creation_mutex.unlock();
load_mutex->unlock();

return image;
Expand All @@ -67,13 +75,17 @@ Array Geodot::get_lines_near_position(String path, double pos_x, double pos_y, d

std::list<LineFeature *> linefeatures = VectorExtractor::get_lines_near_position(path.utf8().get_data(), pos_x, pos_y, radius, max_lines);

resource_creation_mutex.lock();

for (LineFeature *linefeature : linefeatures) {
Ref<GeoLine> line = GeoLine::_new();
line->set_line(linefeature);

lines.push_back(line);
}

resource_creation_mutex.unlock();

return lines;
}

Expand All @@ -82,13 +94,17 @@ Array Geodot::get_points_near_position(String path, double pos_x, double pos_y,

std::list<PointFeature *> pointfeatures = VectorExtractor::get_points_near_position(path.utf8().get_data(), pos_x, pos_y, radius, max_points);

resource_creation_mutex.lock();

for (PointFeature *pointfeature : pointfeatures) {
Ref<GeoPoint> point = GeoPoint::_new();
point->set_point(pointfeature);

points.push_back(point);
}

resource_creation_mutex.unlock();

return points;
}

Expand All @@ -97,13 +113,17 @@ Array Geodot::crop_lines_to_square(String path, double top_left_x, double top_le

std::list<LineFeature *> linefeatures = VectorExtractor::crop_lines_to_square(path.utf8().get_data(), top_left_x, top_left_y, size_meters, max_lines);

resource_creation_mutex.lock();

for (LineFeature *linefeature : linefeatures) {
Ref<GeoLine> line = GeoLine::_new();
line->set_line(linefeature);

lines.push_back(line);
}

resource_creation_mutex.unlock();

return lines;
}

Expand Down Expand Up @@ -319,15 +339,19 @@ Ref<Image> GeoImage::get_normalmap_for_heightmap(float scale) {

Ref<ImageTexture> GeoImage::get_normalmap_texture_for_heightmap(float scale) {
// Create an ImageTexture wrapping the Image
resource_creation_mutex.lock();
ImageTexture *imgTex = ImageTexture::_new();
imgTex->set_storage(ImageTexture::STORAGE_RAW);
imgTex->create_from_image(get_normalmap_for_heightmap(scale), ImageTexture::FLAG_FILTER);
resource_creation_mutex.unlock();

return Ref<ImageTexture>(imgTex);
}

Ref<ImageTexture> GeoImage::get_image_texture() {
// Create an ImageTexture wrapping the Image
resource_creation_mutex.lock();

ImageTexture *imgTex = ImageTexture::_new();
imgTex->set_storage(ImageTexture::STORAGE_RAW);

Expand All @@ -342,6 +366,8 @@ Ref<ImageTexture> GeoImage::get_image_texture() {

imgTex->create_from_image(Ref<Image>(image), flag);

resource_creation_mutex.unlock();

return Ref<ImageTexture>(imgTex);
}

Expand Down Expand Up @@ -388,6 +414,7 @@ String GeoLine::get_attribute(String name) {
}

Ref<Curve3D> GeoLine::get_offset_curve3d(int offset_x, int offset_y, int offset_z) {
resource_creation_mutex.lock();
Ref<Curve3D> curve = Curve3D::_new();

int point_count = line->get_point_count();
Expand All @@ -401,6 +428,8 @@ Ref<Curve3D> GeoLine::get_offset_curve3d(int offset_x, int offset_y, int offset_
curve->add_point(Vector3(x, y, z));
}

resource_creation_mutex.unlock();

return curve;
}

Expand Down

0 comments on commit 2419613

Please sign in to comment.