Skip to content

Commit

Permalink
Merge pull request #567 from openmobilemaps/bugfix/performance-fixes
Browse files Browse the repository at this point in the history
Bugfix/performance fixes
  • Loading branch information
maurhofer-ubique authored Jan 22, 2024
2 parents 73ca61d + b4442ea commit 1d1fbee
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ void ColorCircleShaderOpenGl::preRender(const std::shared_ptr<::RenderingContext

void ColorCircleShaderOpenGl::setColor(float red, float green, float blue, float alpha) {
std::lock_guard<std::mutex> lock(dataMutex);
color.at(0) = red;
color.at(1) = green;
color.at(2) = blue;
color.at(3) = alpha;
color[0] = red;
color[1] = green;
color[2] = blue;
color[3] = alpha;
}

std::string ColorCircleShaderOpenGl::getFragmentShader() {
Expand Down
8 changes: 4 additions & 4 deletions android/src/main/cpp/graphics/shader/ColorShaderOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ void ColorShaderOpenGl::preRender(const std::shared_ptr<::RenderingContextInterf

void ColorShaderOpenGl::setColor(float red, float green, float blue, float alpha) {
std::lock_guard<std::mutex> lock(dataMutex);
color.at(0) = red;
color.at(1) = green;
color.at(2) = blue;
color.at(3) = alpha;
color[0] = red;
color[1] = green;
color[2] = blue;
color[3] = alpha;
}

std::string ColorShaderOpenGl::getVertexShader() {
Expand Down
20 changes: 10 additions & 10 deletions android/src/main/cpp/graphics/shader/TextShaderOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ const std::string TextShaderOpenGl::programName = "UBMAP_TextShaderOpenGl";

std::string TextShaderOpenGl::getProgramName() { return programName; }

void TextShaderOpenGl::setColor(const ::Color & color) {
void TextShaderOpenGl::setColor(const ::Color & c) {
std::lock_guard<std::mutex> lock(dataMutex);
this->color.at(0) = color.r;
this->color.at(1) = color.g;
this->color.at(2) = color.b;
this->color.at(3) = color.a;
color[0] = c.r;
color[1] = c.g;
color[2] = c.b;
color[3] = c.a;
}

void TextShaderOpenGl::setHaloColor(const ::Color & color, double width) {
std::lock_guard<std::mutex> lock(dataMutex);
haloColor.at(0) = color.r;
haloColor.at(1) = color.g;
haloColor.at(2) = color.b;
haloColor.at(3) = color.a;
this->haloWidth = width;
haloColor[0] = color.r;
haloColor[1] = color.g;
haloColor[2] = color.b;
haloColor[3] = color.a;
haloWidth = width;
}

void TextShaderOpenGl::setOpacity(float opacity) { this->opacity = opacity; }
Expand Down
28 changes: 14 additions & 14 deletions shared/public/CollisionGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct IndexRange {
}
}

bool isValid(int16_t maxX, int16_t maxY) {
bool isValid(int16_t maxX, int16_t maxY) const {
return xMin <= maxX && xMax >= 0 && yMin <= maxY && yMax >= 0;
}
};
Expand Down Expand Up @@ -72,8 +72,8 @@ class CollisionGrid {
* return true (1) if collision, or true (2) if outside of bounds
*/
uint8_t addAndCheckCollisionAlignedRect(const CollisionRectF &rectangle) {
RectF projectedRectangle = getProjectedRectangle(rectangle);
IndexRange indexRange = getIndexRangeForRectangle(projectedRectangle);
const RectF &projectedRectangle = getProjectedRectangle(rectangle);
const IndexRange &indexRange = getIndexRangeForRectangle(projectedRectangle);
if (!indexRange.isValid(numCellsX - 1, numCellsY - 1)) {
return 2; // Fully outside of bounds - not relevant
}
Expand Down Expand Up @@ -215,22 +215,22 @@ class CollisionGrid {
temp2[2] = 0.0;
temp2[3] = 1.0;
Matrix::multiply(vpMatrix, temp2, temp1);
float originX = ((temp1.at(0) / temp1.at(3)) * halfWidth + halfWidth);
float originY = ((temp1.at(1) / temp1.at(3)) * halfHeight + halfHeight);
float originX = ((temp1[0] / temp1[3]) * halfWidth + halfWidth);
float originY = ((temp1[1] / temp1[3]) * halfHeight + halfHeight);
temp2[0] = rectangle.width * cosNegGridAngle;
temp2[1] = rectangle.width * sinNegGridAngle;
temp2[2] = 0.0;
temp2[3] = 0.0;
Matrix::multiply(vpMatrix, temp2, temp1);
float w = temp1.at(0);
float h = temp1.at(1);
float w = temp1[0];
float h = temp1[1];
temp2[0] = -rectangle.height * sinNegGridAngle;
temp2[1] = rectangle.height * cosNegGridAngle;
temp2[2] = 0.0;
temp2[3] = 0.0;
Matrix::multiply(vpMatrix, temp2, temp1);
w += temp1.at(0);
h += temp1.at(1);
w += temp1[0];
h += temp1[1];
float width = (w * halfWidth); // by assumption aligned with projected space
float height = (h * halfHeight); // by assumption aligned with projected space
originX = std::min(originX, originX + width);
Expand All @@ -245,16 +245,16 @@ class CollisionGrid {
temp2[2] = 0.0;
temp2[3] = 1.0;
Matrix::multiply(vpMatrix, temp2, temp1);
float originX = ((temp1.at(0) / temp1.at(3)) * halfWidth + halfWidth);
float originY = ((temp1.at(1) / temp1.at(3)) * halfHeight + halfHeight);
float originX = ((temp1[0] / temp1[3]) * halfWidth + halfWidth);
float originY = ((temp1[1] / temp1[3]) * halfHeight + halfHeight);
temp2[0] = circle.radius;
temp2[1] = circle.radius;
temp2[2] = 0.0;
temp2[3] = 0.0;
Matrix::multiply(vpMatrix, temp2, temp1);
temp1.at(0) = temp1.at(0) * halfWidth;
temp1.at(1) = temp1.at(1) * halfHeight;
float iRadius = std::abs((std::sqrt(temp1.at(0) * temp1.at(0) + temp1.at(1) + temp1.at(1))));
temp1[0] = temp1[0] * halfWidth;
temp1[1] = temp1[1] * halfHeight;
float iRadius = std::sqrt(temp1[0] * temp1[0] + temp1[1] * temp1[1]);
return {originX, originY, iRadius};
}

Expand Down
4 changes: 2 additions & 2 deletions shared/public/LoaderHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class LoaderHelper {
if (loaderIndex >= loaders.size()) {
promise->setValue(std::move(TextureLoaderResult(nullptr, std::nullopt, LoaderStatus::NOOP, std::nullopt)));
} else {
loaders.at(loaderIndex)->loadTextureAsnyc(url, etag).then([url, etag, &loaders, loaderIndex, promise](::djinni::Future<::TextureLoaderResult> result) {
loaders[loaderIndex]->loadTextureAsnyc(url, etag).then([url, etag, &loaders, loaderIndex, promise](::djinni::Future<::TextureLoaderResult> result) {
const auto textureResult = result.get();
if (textureResult.status != LoaderStatus::NOOP || loaderIndex == loaders.size() - 1) {
promise->setValue(std::move(textureResult));
Expand All @@ -72,7 +72,7 @@ class LoaderHelper {
if (loaderIndex >= loaders.size()) {
promise->setValue(DataLoaderResult(std::nullopt, std::nullopt, LoaderStatus::NOOP, std::nullopt));
} else {
loaders.at(loaderIndex)->loadDataAsync(url, etag).then([url, etag, &loaders, loaderIndex, promise](::djinni::Future<::DataLoaderResult> result) {
loaders[loaderIndex]->loadDataAsync(url, etag).then([url, etag, &loaders, loaderIndex, promise](::djinni::Future<::DataLoaderResult> result) {
const auto dataResult = result.get();
if (dataResult.status != LoaderStatus::NOOP || loaderIndex == loaders.size() - 1) {
promise->setValue(std::move(dataResult));
Expand Down
2 changes: 1 addition & 1 deletion shared/public/TextLayerObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class TextLayerObject : public LayerObjectInterface {

std::pair<int, double> findReferencePointIndices();
Coord pointAtIndex(const std::pair<int, double> &index, bool useRender = true);
std::pair<int, double> indexAtDistance(const std::pair<int, double> &index, double distance);
std::pair<int, double> indexAtDistance(const std::pair<int, double> &index, double distance, const std::optional<Coord> &indexCoord);

private:
std::shared_ptr<TextInterface> text;
Expand Down
8 changes: 4 additions & 4 deletions shared/public/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,15 +629,15 @@ class Value {
template<class ResultType>
class ValueEvaluator {
public:
inline ResultType getResult(const std::shared_ptr<Value> &value, const EvaluationContext &context, const ResultType defaultValue) {
inline ResultType getResult(const std::shared_ptr<Value> &value, const EvaluationContext &context, const ResultType &defaultValue) {
std::lock_guard<std::mutex> lock(mutex);
if (!value) {
return defaultValue;
}
if (lastValuePtr != value.get()) {
lastResults.clear();
staticValue = std::nullopt;
const auto usedKeysCollection = value->getUsedKeys();
const auto &usedKeysCollection = value->getUsedKeys();
isStatic = usedKeysCollection.empty();
if (isStatic) {
staticValue = value->evaluateOr(context, defaultValue);
Expand All @@ -652,12 +652,12 @@ class ValueEvaluator {
return *staticValue;
}

if(isStateDependant && isZoomDependent && !context.featureStateManager->empty()) {
if((isStateDependant && !context.featureStateManager->empty()) || isZoomDependent) {
return value->evaluateOr(context, defaultValue);
}


auto identifier = (context.feature->identifier << 12) | (uint64_t)((isZoomDependent ? context.zoomLevel : 0.f) * 100);
auto identifier = context.feature->identifier;
if(isStateDependant && !context.featureStateManager->empty()) {
identifier = (context.feature->identifier << 32) | (uint64_t)(context.featureStateManager->getCurrentState());
}
Expand Down
8 changes: 4 additions & 4 deletions shared/src/graphics/helpers/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ std::vector<float> Matrix::multiply(const std::vector<float> &M, const std::vect
}

void Matrix::multiply(const std::vector<float> &M, const std::vector<float> &x, std::vector<float> &result) {
result[0] = M[I(0, 0)] * x[0] + M[I(1, 0)] * x[1] + M[I(2, 0)] * x[2] + M[I(3, 0)] * x[3];
result[1] = M[I(0, 1)] * x[0] + M[I(1, 1)] * x[1] + M[I(2, 1)] * x[2] + M[I(3, 1)] * x[3];
result[2] = M[I(0, 2)] * x[0] + M[I(1, 2)] * x[1] + M[I(2, 2)] * x[2] + M[I(3, 2)] * x[3];
result[3] = M[I(0, 3)] * x[0] + M[I(1, 3)] * x[1] + M[I(2, 3)] * x[2] + M[I(3, 3)] * x[3];
result[0] = M[0] * x[0] + M[4] * x[1] + M[8] * x[2] + M[12] * x[3];
result[1] = M[1] * x[0] + M[5] * x[1] + M[9] * x[2] + M[13] * x[3];
result[2] = M[2] * x[0] + M[6] * x[1] + M[10] * x[2] + M[14] * x[3];
result[3] = M[3] * x[0] + M[7] * x[1] + M[11] * x[2] + M[15] * x[3];
}
31 changes: 16 additions & 15 deletions shared/src/map/layers/text/TextLayerObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ float TextLayerObject::layoutLine(float scale, bool updateObject) {
}
}

currentIndex = indexAtDistance(currentIndex, -size * 0.5);
currentIndex = indexAtDistance(currentIndex, -size * 0.5, std::nullopt);

int total = 0;
int rotated = 0;
Expand All @@ -403,7 +403,7 @@ float TextLayerObject::layoutLine(float scale, bool updateObject) {

for(auto &i : splittedTextInfo) {
if(i.glyphIndex < 0) {
currentIndex = indexAtDistance(currentIndex, spaceAdvance * fontSize * i.scale);
currentIndex = indexAtDistance(currentIndex, spaceAdvance * fontSize * i.scale, std::nullopt);
lastAngle = 0;
preLastAngle = 0;
index = 0;
Expand All @@ -417,8 +417,8 @@ float TextLayerObject::layoutLine(float scale, bool updateObject) {
auto p = pointAtIndex(currentIndex);

// get before and after to calculate angle
auto before = pointAtIndex(indexAtDistance(currentIndex, -size.x * 0.5), false);
auto after = pointAtIndex(indexAtDistance(currentIndex, size.x * 0.5), false);
auto before = pointAtIndex(indexAtDistance(currentIndex, -size.x * 0.5, p), false);
auto after = pointAtIndex(indexAtDistance(currentIndex, size.x * 0.5, p), false);

double angle = atan2((before.y - after.y), -(before.x - after.x));
angle *= (180.0 / M_PI);
Expand Down Expand Up @@ -454,7 +454,7 @@ float TextLayerObject::layoutLine(float scale, bool updateObject) {
auto yh = y + size.y;

auto lastIndex = currentIndex;
currentIndex = indexAtDistance(currentIndex, advance.x * (1.0 + letterSpacing));
currentIndex = indexAtDistance(currentIndex, advance.x * (1.0 + letterSpacing), p);

// if we are at the end, and we were at the end (lastIndex), then clear and skip
if(currentIndex.first == renderLineCoordinates.size() - 1 && lastIndex.first == currentIndex.first && (lastIndex.second == currentIndex.second)) {
Expand Down Expand Up @@ -553,19 +553,20 @@ std::pair<int, double> TextLayerObject::findReferencePointIndices() {
int iMin = 0;

for(int i=1; i<renderLineCoordinates.size(); ++i) {
auto start = renderLineCoordinates[i-1];
auto end = renderLineCoordinates[i];
const auto& start = renderLineCoordinates[i-1];
const auto& end = renderLineCoordinates[i];

auto length = Vec2DHelper::distance(Vec2D(start.x, start.y), Vec2D(end.x, end.y));
auto lengthSquared = Vec2DHelper::distanceSquared(Vec2D(start.x, start.y), Vec2D(end.x, end.y));
auto dir = Vec2D(end.x - start.x, end.y - start.y);

double t = 0.0;
if(length > 0) {
auto dot = Vec2D(point.x - start.x, point.y - start.y) * Vec2D(end.x - start.x, end.y - start.y);
t = dot / (length * length);
if(lengthSquared > 0) {
auto dot = Vec2D(point.x - start.x, point.y - start.y) * dir;
t = dot / lengthSquared;
}

auto proj = Vec2D(start.x + t * (end.x - start.x), start.y + t * (end.y - start.y));
auto dist = Vec2DHelper::distance(proj, Vec2D(point.x, point.y));
auto proj = Vec2D(start.x + t * dir.x, start.y + t * dir.y);
auto dist = Vec2DHelper::distanceSquared(proj, Vec2D(point.x, point.y));

if(dist < distance && t >= 0.0 && t <= 1.0) {
tMin = t;
Expand All @@ -583,8 +584,8 @@ Coord TextLayerObject::pointAtIndex(const std::pair<int, double> &index, bool us
return Coord(s.systemIdentifier, s.x + (e.x - s.x) * index.second, s.y + (e.y - s.y) * index.second, s.z + (e.z - s.z) * index.second);
}

std::pair<int, double> TextLayerObject::indexAtDistance(const std::pair<int, double> &index, double distance) {
auto current = pointAtIndex(index);
std::pair<int, double> TextLayerObject::indexAtDistance(const std::pair<int, double> &index, double distance, const std::optional<Coord> &indexCoord) {
auto current = indexCoord ? *indexCoord : pointAtIndex(index);
auto currentIndex = index;
auto dist = std::abs(distance);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ Tiled2dMapVectorLayerParserResult Tiled2dMapVectorLayerParserHelper::parseStyleJ
tmpBounds.push_back(el.value().get<double>());
}
if (tmpBounds.size() == 4) {
const auto topLeft = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds.at(0), tmpBounds.at(1), 0);
const auto bottomRight = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds.at(2), tmpBounds.at(3), 0);
const auto topLeft = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds[0], tmpBounds[1], 0);
const auto bottomRight = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds[2], tmpBounds[3], 0);
bounds = RectCoord(topLeft, bottomRight);
}
}
Expand All @@ -135,8 +135,8 @@ Tiled2dMapVectorLayerParserResult Tiled2dMapVectorLayerParserHelper::parseStyleJ
tmpBounds.push_back(el.value().get<double>());
}
if (tmpBounds.size() == 4) {
const auto topLeft = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds.at(0), tmpBounds.at(1), 0);
const auto bottomRight = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds.at(2), tmpBounds.at(3), 0);
const auto topLeft = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds[0], tmpBounds[1], 0);
const auto bottomRight = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds[2], tmpBounds[3], 0);
bounds = RectCoord(topLeft, bottomRight);
}
}
Expand Down Expand Up @@ -246,7 +246,7 @@ Tiled2dMapVectorLayerParserResult Tiled2dMapVectorLayerParserHelper::parseStyleJ
layers.push_back(layerDesc);

} else if (val["type"] == "raster" && rasterLayerMap.count(val["source"]) != 0) {
auto layer = rasterLayerMap.at(val["source"]);
auto layer = rasterLayerMap[val["source"]];
RasterVectorStyle style = RasterVectorStyle(parser.parseValue(val["paint"]["raster-opacity"]),
parser.parseValue(val["paint"]["raster-brightness-min"]),
parser.parseValue(val["paint"]["raster-brightness-max"]),
Expand Down Expand Up @@ -387,8 +387,8 @@ Tiled2dMapVectorLayerParserResult Tiled2dMapVectorLayerParserHelper::parseStyleJ
tmpBounds.push_back(d);
}
if (tmpBounds.size() == 4) {
const auto topLeft = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds.at(0), tmpBounds.at(1), 0);
const auto bottomRight = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds.at(2), tmpBounds.at(3), 0);
const auto topLeft = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds[0], tmpBounds[1], 0);
const auto bottomRight = Coord(CoordinateSystemIdentifiers::EPSG4326(), tmpBounds[2], tmpBounds[3], 0);
bounds = RectCoord(topLeft, bottomRight);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class SymbolObjectCollisionWrapper {
size_t symbolTileIndex;
bool isColliding;

SymbolObjectCollisionWrapper(const std::shared_ptr<Tiled2dMapVectorSymbolObject> object)
SymbolObjectCollisionWrapper(const std::shared_ptr<Tiled2dMapVectorSymbolObject> &object)
: symbolObject(object), symbolSortKey(object->symbolSortKey), symbolTileIndex(object->symbolTileIndex),
isColliding(object->animationCoordinator->isColliding()) {};

SymbolObjectCollisionWrapper(SymbolObjectCollisionWrapper&& other) noexcept
: symbolObject(std::move(other.symbolObject)),
SymbolObjectCollisionWrapper(const SymbolObjectCollisionWrapper& other) noexcept
: symbolObject(other.symbolObject),
symbolSortKey(other.symbolSortKey),
symbolTileIndex(other.symbolTileIndex),
isColliding(other.isColliding) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,25 +627,26 @@ void Tiled2dMapVectorSourceSymbolDataManager::collisionDetection(std::vector<std
if (objectsIt != symbolGroupsMap.end()) {
for (auto &symbolGroup: std::get<1>(objectsIt->second)) {
symbolGroup.syncAccess([&allObjects, zoomIdentifier, persistingPlacement = persistingSymbolPlacement](auto group){
auto objects = group->getSymbolObjectsForCollision();
const auto &objects = group->getSymbolObjectsForCollision();
if (persistingPlacement) {
for (auto &object : objects) {
if (object.symbolObject->largestCollisionZoom == -1 || object.symbolObject->largestCollisionZoom < zoomIdentifier) {
allObjects.push_back(std::move(object));
allObjects.push_back(object);
}
else {
object.symbolObject->setHideFromCollision(true);
}
}
} else {
allObjects.reserve(allObjects.size() + objects.size());
allObjects.insert(allObjects.end(), std::make_move_iterator(objects.begin()),
std::make_move_iterator(objects.end()));
for(auto& o : objects) {
allObjects.push_back(o);
}
}
});
}
}
}

std::stable_sort(allObjects.rbegin(), allObjects.rend());

for (const auto &objectWrapper: allObjects) {
Expand Down
Loading

0 comments on commit 1d1fbee

Please sign in to comment.