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

Refactor the WIC factory creation to use the format outlined in D2DWrapper #1357

Merged
Merged
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
21 changes: 15 additions & 6 deletions Frameworks/CoreGraphics/CGImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ CGImageRef CGImageCreate(size_t width,
unsigned char* data = (unsigned char*)[dataProvider bytes];

ComPtr<IWICBitmap> image;
ComPtr<IWICImagingFactory> imageFactory = _GetWICFactory();
ComPtr<IWICImagingFactory> imageFactory;
RETURN_NULL_IF_FAILED(_CGGetWICFactory(&imageFactory));

REFGUID pixelFormat = _CGImageGetWICPixelFormatFromImageProperties(bitsPerComponent, bitsPerPixel, colorSpace, bitmapInfo);

Expand All @@ -93,7 +94,9 @@ CGImageRef CGImageCreate(size_t width,
CGImageRef CGImageCreateWithImageInRect(CGImageRef ref, CGRect rect) {
RETURN_NULL_IF(!ref);

ComPtr<IWICImagingFactory> imageFactory = _GetWICFactory();
ComPtr<IWICImagingFactory> imageFactory;
RETURN_NULL_IF_FAILED(_CGGetWICFactory(&imageFactory));

ComPtr<IWICBitmap> rectImage;
RETURN_NULL_IF_FAILED(imageFactory->CreateBitmapFromSourceRect(
ref->ImageSource().Get(), rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, &rectImage));
Expand All @@ -113,7 +116,9 @@ CGImageRef CGImageCreateWithImageInRect(CGImageRef ref, CGRect rect) {
CGImageRef CGImageCreateCopy(CGImageRef ref) {
RETURN_NULL_IF(!ref);

ComPtr<IWICImagingFactory> imageFactory = _GetWICFactory();
ComPtr<IWICImagingFactory> imageFactory;
RETURN_NULL_IF_FAILED(_CGGetWICFactory(&imageFactory));

ComPtr<IWICBitmap> image;

RETURN_NULL_IF_FAILED(imageFactory->CreateBitmapFromSource(ref->ImageSource().Get(), WICBitmapCacheOnDemand, &image));
Expand Down Expand Up @@ -147,7 +152,8 @@ CGImageRef CGImageMaskCreate(size_t width,
unsigned char* data = (unsigned char*)[dataProvider bytes];

ComPtr<IWICBitmap> image;
ComPtr<IWICImagingFactory> imageFactory = _GetWICFactory();
ComPtr<IWICImagingFactory> imageFactory;
RETURN_NULL_IF_FAILED(_CGGetWICFactory(&imageFactory));

woc::unique_cf<CGColorSpaceRef> colorSpace(CGColorSpaceCreateDeviceGray());
REFGUID pixelFormat =
Expand Down Expand Up @@ -408,7 +414,9 @@ CGImageRef _CGImageCreateCopyWithPixelFormat(CGImageRef image, WICPixelFormatGUI
return image;
}

ComPtr<IWICImagingFactory> imageFactory = _GetWICFactory();
ComPtr<IWICImagingFactory> imageFactory;
RETURN_NULL_IF_FAILED(_CGGetWICFactory(&imageFactory));

ComPtr<IWICFormatConverter> converter;
RETURN_NULL_IF_FAILED(imageFactory->CreateFormatConverter(&converter));

Expand Down Expand Up @@ -449,7 +457,8 @@ CGImageRef _CGImageLoadJPEG(void* bytes, int length) {
}

CGImageRef _CGImageLoadImageWithWICDecoder(REFGUID decoderCls, void* bytes, int length) {
ComPtr<IWICImagingFactory> imageFactory = _GetWICFactory();
ComPtr<IWICImagingFactory> imageFactory;
RETURN_NULL_IF_FAILED(_CGGetWICFactory(&imageFactory));

ComPtr<IWICBitmapDecoder> pDecoder;
ComPtr<IWICStream> spStream;
Expand Down
14 changes: 5 additions & 9 deletions Frameworks/CoreGraphics/D2DWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ HRESULT _CGGetD2DFactory(ID2D1Factory** factory) {
RETURN_HR(sHr);
}

static ComPtr<IWICImagingFactory> __createWICFactory() {
ComPtr<IWICImagingFactory> wicFactory;
FAIL_FAST_IF_FAILED(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wicFactory)));
return wicFactory;
}

ComPtr<IWICImagingFactory> _GetWICFactory() {
static ComPtr<IWICImagingFactory> s_WICFactory = __createWICFactory();
return s_WICFactory;
HRESULT _CGGetWICFactory(IWICImagingFactory** factory) {
static ComPtr<IWICImagingFactory> sWicFactory;
static HRESULT sHr = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&sWicFactory));
sWicFactory.CopyTo(factory);
RETURN_HR(sHr);
}
2 changes: 1 addition & 1 deletion Frameworks/include/CoreGraphics/D2DWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

HRESULT _CGGetD2DFactory(ID2D1Factory** factory);

Microsoft::WRL::ComPtr<IWICImagingFactory> _GetWICFactory();
HRESULT _CGGetWICFactory(IWICImagingFactory** factory);

inline D2D_POINT_2F _CGPointToD2D_F(CGPoint point) {
return { point.x, point.y };
Expand Down