Skip to content

Commit

Permalink
[#52832] src: gui_engine.cpp: Rebuild texture when size constraints a…
Browse files Browse the repository at this point in the history
…re not met

Signed-off-by: Illia Vysochyn <[email protected]>
  • Loading branch information
ivysochyn committed Feb 6, 2024
1 parent bca67c2 commit cdc2ae9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion include/gui_node/gui_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,13 @@ class TextureLoader
* Updates the texture with provided image data.
*
* @param image_data Vector of the image data.
* @param width Width of the image.
* @param height Height of the image.
* @param channels Number of channels in the image.
*
* @return bool True if the texture was updated successfully.
*/
bool updateTexture(std::vector<unsigned char> image_data);
bool updateTexture(std::vector<unsigned char> image_data, int width, int height, int channels);
};

/**
Expand Down
18 changes: 17 additions & 1 deletion src/gui_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,25 @@ bool TextureLoader::recordCommandBuffer(VkCommandPoolSharedPtr command_pool, con
return checkVulkanResult(result, node->get_logger(), "Failed to wait for device to become idle!");
}

bool TextureLoader::updateTexture(std::vector<unsigned char> image_data)
bool TextureLoader::updateTexture(std::vector<unsigned char> image_data, int width, int height, int channels)
{
this->image_data = image_data;

// Recreate image if size or channels constraints are not met
if (this->width != width || this->height != height || this->channels != channels)
{
this->width = width;
this->height = height;
this->channels = channels;
VkResult result = vkDeviceWaitIdle(*gui_engine->getDevice().get());
checkVulkanResult(result, node->get_logger(), "Failed to wait for device to become idle!");
createImage();
createImageView();
descriptor_set =
ImGui_ImplVulkan_AddTexture(*sampler.get(), *image_view.get(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
createUploadBuffer();
}

return uploadToBuffer() && recordCommandBuffer(gui_engine->getCommandPool(), gui_engine->getGraphicsQueue());
}

Expand Down
2 changes: 1 addition & 1 deletion src/widget/widget_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ bool BaseVideoWidget::updateTexture(const std::vector<unsigned char> &buffer, in
std::shared_ptr<TextureLoader> texture_loader = gui_engine->getTexture(ros_data_name);
if (buffer != last_image_data)
{
if (!texture_loader->updateTexture(buffer))
if (!texture_loader->updateTexture(buffer, width, height, channels))
{
return false;
}
Expand Down

0 comments on commit cdc2ae9

Please sign in to comment.