Generate texture thumbnails in a job thread (#1571)

Signed-off-by: John <jonawals@amazon.com>
This commit is contained in:
yuriy0
2021-06-25 12:43:16 -04:00
committed by John
parent 0fb23a9708
commit 9f034b90a2
2 changed files with 58 additions and 37 deletions
@@ -15,6 +15,7 @@
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/Utils/Utils.h>
#include <AzCore/Jobs/JobFunction.h>
#include <AzFramework/Asset/AssetSystemBus.h>
#include <AzFramework/StringFunc/StringFunc.h>
#include <AzToolsFramework/API/EditorAssetSystemAPI.h>
@@ -115,8 +116,7 @@ namespace ImageProcessingAtom
void ImageThumbnailSystemComponent::RenderThumbnail(
AzToolsFramework::Thumbnailer::SharedThumbnailKey thumbnailKey, int thumbnailSize)
{
auto sourceKey = azrtti_cast<const AzToolsFramework::AssetBrowser::SourceThumbnailKey*>(thumbnailKey.data());
if (sourceKey)
if (auto sourceKey = azrtti_cast<const AzToolsFramework::AssetBrowser::SourceThumbnailKey*>(thumbnailKey.data()))
{
bool foundIt = false;
AZ::Data::AssetInfo assetInfo;
@@ -129,52 +129,72 @@ namespace ImageProcessingAtom
{
AZStd::string fullPath;
AZ::StringFunc::Path::Join(watchFolder.c_str(), assetInfo.m_relativePath.c_str(), fullPath);
if (RenderThumbnailFromImage(thumbnailKey, thumbnailSize, IImageObjectPtr(LoadImageFromFile(fullPath))))
{
return;
}
RenderThumbnailFromImage(thumbnailKey, thumbnailSize,
[fullPath]() { return IImageObjectPtr(LoadImageFromFile(fullPath)); }
);
}
}
auto productKey = azrtti_cast<const AzToolsFramework::AssetBrowser::ProductThumbnailKey*>(thumbnailKey.data());
if (productKey)
else if (auto productKey = azrtti_cast<const AzToolsFramework::AssetBrowser::ProductThumbnailKey*>(thumbnailKey.data()))
{
if (RenderThumbnailFromImage(thumbnailKey, thumbnailSize, Utils::LoadImageFromImageAsset(productKey->GetAssetId())))
{
return;
}
RenderThumbnailFromImage(thumbnailKey, thumbnailSize,
[assetId = productKey->GetAssetId()]() { return Utils::LoadImageFromImageAsset(assetId); }
);
}
else
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(
thumbnailKey, &AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailFailedToRender);
}
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(
thumbnailKey, &AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailFailedToRender);
}
bool ImageThumbnailSystemComponent::RenderThumbnailFromImage(
AzToolsFramework::Thumbnailer::SharedThumbnailKey thumbnailKey, int thumbnailSize, IImageObjectPtr previewImage) const
template<class MkImageFn>
void ImageThumbnailSystemComponent::RenderThumbnailFromImage(
AzToolsFramework::Thumbnailer::SharedThumbnailKey thumbnailKey, int thumbnailSize, MkImageFn mkPreviewImage) const
{
if (!previewImage)
const auto JobRunner = [mkPreviewImage, thumbnailKey, thumbnailSize]() mutable
{
return false;
}
IImageObjectPtr previewImage = mkPreviewImage();
if (!previewImage)
{
AZ::SystemTickBus::QueueFunction(
[
thumbnailKey
]()
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(
thumbnailKey, &AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailFailedToRender);
});
ImageToProcess imageToProcess(previewImage);
imageToProcess.ConvertFormat(ePixelFormat_R8G8B8A8);
previewImage = imageToProcess.Get();
return;
}
AZ::u8* imageBuf = nullptr;
AZ::u32 mip = 0;
AZ::u32 pitch = 0;
previewImage->GetImagePointer(mip, imageBuf, pitch);
const AZ::u32 width = previewImage->GetWidth(mip);
const AZ::u32 height = previewImage->GetHeight(mip);
ImageToProcess imageToProcess(previewImage);
imageToProcess.ConvertFormat(ePixelFormat_R8G8B8A8);
previewImage = imageToProcess.Get();
QImage image(imageBuf, width, height, pitch, QImage::Format_RGBA8888);
AZ::u8* imageBuf = nullptr;
AZ::u32 mip = 0;
AZ::u32 pitch = 0;
previewImage->GetImagePointer(mip, imageBuf, pitch);
const AZ::u32 width = previewImage->GetWidth(mip);
const AZ::u32 height = previewImage->GetHeight(mip);
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(
thumbnailKey, &AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailRendered,
QPixmap::fromImage(image.scaled(QSize(thumbnailSize, thumbnailSize), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
// Note that this image holds a non-owning pointer to the `previewImage' raw data buffer
const QImage image(imageBuf, width, height, pitch, QImage::Format_RGBA8888);
return true;
// Dispatch event on main thread
AZ::SystemTickBus::QueueFunction(
[
thumbnailKey, thumbnailSize,
pixmap = QPixmap::fromImage(image.scaled(QSize(thumbnailSize, thumbnailSize), Qt::KeepAspectRatio, Qt::SmoothTransformation))
]() mutable
{
AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Event(
thumbnailKey, &AzToolsFramework::Thumbnailer::ThumbnailerRendererNotifications::ThumbnailRendered,
pixmap);
});
};
AZ::CreateJobFunction(JobRunner, true)->Start();
}
} // namespace Thumbnails
} // namespace ImageProcessingAtom
@@ -52,8 +52,9 @@ namespace ImageProcessingAtom
bool Installed() const override;
void RenderThumbnail(AzToolsFramework::Thumbnailer::SharedThumbnailKey thumbnailKey, int thumbnailSize) override;
bool RenderThumbnailFromImage(
AzToolsFramework::Thumbnailer::SharedThumbnailKey thumbnailKey, int thumbnailSize, IImageObjectPtr previewImage) const;
template<class MkImageFn>
void RenderThumbnailFromImage(
AzToolsFramework::Thumbnailer::SharedThumbnailKey thumbnailKey, int thumbnailSize, MkImageFn mkPreviewImage) const;
};
} // namespace Thumbnails
} // namespace ImageProcessingAtom