Refine new editor shell host and embedded icons
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <filesystem>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
|
||||
namespace XCEngine::UI::Editor::Host {
|
||||
@@ -380,6 +381,29 @@ bool NativeRenderer::LoadTextureFromFile(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NativeRenderer::LoadTextureFromMemory(
|
||||
const std::uint8_t* data,
|
||||
std::size_t size,
|
||||
::XCEngine::UI::UITextureHandle& outTexture,
|
||||
std::string& outError) {
|
||||
outError.clear();
|
||||
ReleaseTexture(outTexture);
|
||||
|
||||
auto texture = std::make_unique<NativeTextureResource>();
|
||||
if (!DecodeTextureMemory(data, size, *texture, outError)) {
|
||||
outTexture = {};
|
||||
return false;
|
||||
}
|
||||
|
||||
outTexture.nativeHandle = reinterpret_cast<std::uintptr_t>(texture.get());
|
||||
outTexture.width = texture->width;
|
||||
outTexture.height = texture->height;
|
||||
outTexture.kind = ::XCEngine::UI::UITextureHandleKind::DescriptorHandle;
|
||||
m_liveTextures.insert(texture.get());
|
||||
texture.release();
|
||||
return true;
|
||||
}
|
||||
|
||||
void NativeRenderer::ReleaseTexture(::XCEngine::UI::UITextureHandle& texture) {
|
||||
if (!texture.IsValid()) {
|
||||
texture = {};
|
||||
@@ -742,15 +766,80 @@ bool NativeRenderer::DecodeTextureFile(
|
||||
return false;
|
||||
}
|
||||
|
||||
return DecodeTextureFrame(*frame.Get(), outTexture, outError);
|
||||
}
|
||||
|
||||
bool NativeRenderer::DecodeTextureMemory(
|
||||
const std::uint8_t* data,
|
||||
std::size_t size,
|
||||
NativeTextureResource& outTexture,
|
||||
std::string& outError) {
|
||||
outError.clear();
|
||||
if (data == nullptr || size == 0u) {
|
||||
outError = "DecodeTextureMemory rejected an empty image payload.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (size > static_cast<std::size_t>((std::numeric_limits<DWORD>::max)())) {
|
||||
outError = "DecodeTextureMemory payload exceeds WIC stream limits.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EnsureWicFactory(outError)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<IWICStream> stream;
|
||||
HRESULT hr = m_wicFactory->CreateStream(stream.ReleaseAndGetAddressOf());
|
||||
if (FAILED(hr) || !stream) {
|
||||
outError = HrToString("IWICImagingFactory::CreateStream", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
hr = stream->InitializeFromMemory(
|
||||
const_cast<BYTE*>(reinterpret_cast<const BYTE*>(data)),
|
||||
static_cast<DWORD>(size));
|
||||
if (FAILED(hr)) {
|
||||
outError = HrToString("IWICStream::InitializeFromMemory", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<IWICBitmapDecoder> decoder;
|
||||
hr = m_wicFactory->CreateDecoderFromStream(
|
||||
stream.Get(),
|
||||
nullptr,
|
||||
WICDecodeMetadataCacheOnLoad,
|
||||
decoder.ReleaseAndGetAddressOf());
|
||||
if (FAILED(hr) || !decoder) {
|
||||
outError = HrToString("IWICImagingFactory::CreateDecoderFromStream", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<IWICBitmapFrameDecode> frame;
|
||||
hr = decoder->GetFrame(0u, frame.ReleaseAndGetAddressOf());
|
||||
if (FAILED(hr) || !frame) {
|
||||
outError = HrToString("IWICBitmapDecoder::GetFrame", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
return DecodeTextureFrame(*frame.Get(), outTexture, outError);
|
||||
}
|
||||
|
||||
bool NativeRenderer::DecodeTextureFrame(
|
||||
IWICBitmapSource& source,
|
||||
NativeTextureResource& outTexture,
|
||||
std::string& outError) {
|
||||
outError.clear();
|
||||
|
||||
Microsoft::WRL::ComPtr<IWICFormatConverter> converter;
|
||||
hr = m_wicFactory->CreateFormatConverter(converter.ReleaseAndGetAddressOf());
|
||||
HRESULT hr = m_wicFactory->CreateFormatConverter(converter.ReleaseAndGetAddressOf());
|
||||
if (FAILED(hr) || !converter) {
|
||||
outError = HrToString("IWICImagingFactory::CreateFormatConverter", hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
hr = converter->Initialize(
|
||||
frame.Get(),
|
||||
&source,
|
||||
GUID_WICPixelFormat32bppPBGRA,
|
||||
WICBitmapDitherTypeNone,
|
||||
nullptr,
|
||||
|
||||
Reference in New Issue
Block a user