Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread safety to WIC initialization using call_once #807

Open
wants to merge 2 commits into
base: uwp/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions winrt/lib/images/CanvasBitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas

WicBitmapSource DefaultBitmapAdapter::CreateWicBitmapSource(ICanvasDevice* device, IStream* fileStream, bool tryEnableIndexing)
{
auto factory = m_wicAdapter->GetFactory();
ComPtr<IWICBitmapDecoder> wicBitmapDecoder;
ThrowIfFailed(m_wicAdapter->GetFactory()->CreateDecoderFromStream(
ThrowIfFailed(factory->CreateDecoderFromStream(
fileStream,
nullptr,
WICDecodeMetadataCacheOnDemand,
Expand Down Expand Up @@ -303,7 +304,7 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
auto transformOptions = GetTransformOptionsFromPhotoOrientation(orientation);

ComPtr<IWICFormatConverter> wicFormatConverter;
ThrowIfFailed(m_wicAdapter->GetFactory()->CreateFormatConverter(&wicFormatConverter));
ThrowIfFailed(factory->CreateFormatConverter(&wicFormatConverter));

auto targetPixelFormat = GUID_WICPixelFormat32bppPBGRA;

Expand Down
5 changes: 2 additions & 3 deletions winrt/lib/images/CanvasImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,11 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas

ComPtr<IWICImagingFactory2> const& DefaultCanvasImageAdapter::GetFactory()
{
if (!m_wicAdapter)
{
std::call_once(m_initWicAdapterFlag, [this] {
// Hold on to the wic adapter so it doesn't keep getting destroyed
// and recreated.
m_wicAdapter = WicAdapter::GetInstance();
}
});

return m_wicAdapter->GetFactory();
}
Expand Down
1 change: 1 addition & 0 deletions winrt/lib/images/CanvasImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ namespace ABI { namespace Microsoft { namespace Graphics { namespace Canvas
class DefaultCanvasImageAdapter : public CanvasImageAdapter
{
std::shared_ptr<WicAdapter> m_wicAdapter;
std::once_flag m_initWicAdapterFlag;

public:
virtual ComPtr<IAsyncAction> RunAsync(std::function<void()>&& fn) override;
Expand Down
8 changes: 4 additions & 4 deletions winrt/lib/images/WicAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class WicAdapter : public Singleton<WicAdapter, DefaultWicAdapter>
class DefaultWicAdapter : public WicAdapter
{
ComPtr<IWICImagingFactory2> m_wicFactory;
std::once_flag m_initFactoryFlag;

public:
DefaultWicAdapter()
Expand All @@ -29,15 +30,14 @@ class DefaultWicAdapter : public WicAdapter

virtual ComPtr<IWICImagingFactory2> const& GetFactory() override
{
if (!m_wicFactory)
{
std::call_once(m_initFactoryFlag, [this] {
ThrowIfFailed(
CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&m_wicFactory)));
}
IID_PPV_ARGS(&m_wicFactory)));
});

return m_wicFactory;
}
Expand Down