Skip to content

Commit

Permalink
fix(nd): reuse the terrain NanoVG texture and fix memory leak (#9351)
Browse files Browse the repository at this point in the history
* fix(nd): reuse nanovg texture

* fix(nd): clean up the decoded image on failure

* fix(nd): use fmtlib

* chore: add changelog entry
  • Loading branch information
Nufflee authored Nov 4, 2024
1 parent 70dd2b4 commit 5815870
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
1. [A32NX/CAMERA] Improved default camera position for Virtual Reality (VR) - @aguther (Andreas Guther)
1. [A380X/EFB] Fixed doors automatically opening in flight - @saschl (saschl)
1. [A380X/FMS] Fixed layouting issue on FMS/ACTIVE/PERF/T.O page for some users - @flogross89 (floridude)
1. [A380X/TELEX] Added popup for telex consent @saschl (saschl) @Maximilian-Reuter (\_chaoz)
1. [A380X/TELEX] Added popup for telex consent - @saschl (saschl) @Maximilian-Reuter (\_chaoz)
1. [ND] Fix memory leak when using TERR ON ND - @Nufflee (nufflee)

## 0.12.0

Expand Down
1 change: 1 addition & 0 deletions fbw-common/src/wasm/terronnd/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ clang++ \
-O2 \
-I "${MSFS_SDK}/WASM/include" \
-I "${MSFS_SDK}/SimConnect SDK/include" \
-I "${DIR}/../cpp-msfs-framework/lib/" \
"${DIR}/src/main.cpp" \
"${DIR}/src/nanovg/nanovg.cpp" \
"${DIR}/src/navigationdisplay/collection.cpp" \
Expand Down
39 changes: 33 additions & 6 deletions fbw-common/src/wasm/terronnd/src/navigationdisplay/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
#pragma clang diagnostic ignored "-Wsign-conversion"
#include <MSFS/Legacy/gauges.h>
#include <MSFS/Render/nanovg.h>
#include <MSFS/Render/stb_image.h>
#pragma clang diagnostic pop
#include <cstdint>
#include <string_view>
#include <vector>

#include <iostream>

#define FMT_HEADER_ONLY
#include <fmt/format.h>

#include "../simconnect/clientdataarea.hpp"
#include "../simconnect/connection.hpp"
#include "../simconnect/lvarobject.hpp"
Expand Down Expand Up @@ -113,14 +116,38 @@ class Display : public DisplayBase {
this->_frameData->defineArea(side == DisplaySide::Left ? FrameDataLeftName : FrameDataRightName);
this->_frameData->requestArea(SIMCONNECT_CLIENT_DATA_PERIOD_ON_SET);
this->_frameData->setOnChangeCallback([=]() {
this->destroyImage();

if (!this->_ignoreNextFrame && this->_configuration.terrainActive) {
this->_nanovgImage =
nvgCreateImageMem(this->_context, 0, this->_frameData->data().data(), static_cast<int>(this->_frameBufferSize));
if (this->_nanovgImage == 0) {
std::cerr << "TERR ON ND: Unable to decode the image from the stream" << std::endl;
// If we don't have an image yet, create one
this->_nanovgImage = nvgCreateImageMem(this->_context, 0, this->_frameData->data().data(), static_cast<int>(this->_frameBufferSize));
if (this->_nanovgImage == 0) {
std::cerr << fmt::format("TERR ON ND: Unable to create the image from the stream. Reason: {}", stbi_failure_reason());
}

return;
}

// Otherwise, decode the PNG manually and update the existing image
int decodedWidth, decodedHeight;
uint8_t* decodedImage = stbi_load_from_memory(this->_frameData->data().data(), static_cast<int>(this->_frameBufferSize), &decodedWidth, &decodedHeight, nullptr, 4);
if (decodedImage == nullptr) {
std::cerr << fmt::format("TERR ON ND: Unable to create the image from the stream. Reason: {}", stbi_failure_reason());
return;
}

int width, height;
nvgImageSize(this->_context, this->_nanovgImage, &width, &height);

if (decodedWidth != width || decodedHeight != height) {
// This should never happen, but bail just in case
std::cerr << fmt::format("TERR ON ND: The image size does not match the expected size. Expected: {}x{}, actual: {}x{}", width, height, decodedWidth, decodedHeight);
stbi_image_free(decodedImage);
return;
}

nvgUpdateImage(this->_context, this->_nanovgImage, decodedImage);

stbi_image_free(decodedImage);
} else {
this->resetNavigationDisplayData();
}
Expand Down

0 comments on commit 5815870

Please sign in to comment.