Files
react-native-svg/windows/RNSVG/ImageView.h
Andrew Coates d02c997352 fix: react-native-windows implementation for new architecture (#2527)
# Summary
There are two main things going on with the PR. Both involve a reworking
of the new arch implementation of rn-svg for windows.
The current implementation attempts to implement a svg renderer from
scratch. There are numerous edge cases that it wasn't handling
correctly, and the performance had some serious issues. This
implementation switches to use the svg rendering path built into
Direct2D. This brings significant performance improvements.

The 2nd issue is there have been various breaking changes in
react-native-windows for how new arch native components are implemented.
This brings the rn-svg implementation in line with those latest changes.

## Test Plan

Primary testing right now is loading up the example app in the repo.
New arch on react-native-windows is still in somewhat early days - so
there are not really current users of this code. I am integrating this
code into Microsoft Office, where I have tested some scenarios. But we
will get expanded testing as we roll out the new arch. I expect there to
be some follow-ups as we expand our usage. The version of rn-svg before
this PR doesn't build with the latest new arch react-native-windows
versions. - So its hard to get worse than that.

### What's required for testing (prerequisites)?

### What are the steps to reproduce (after prerequisites)?

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |    N/A  |
| MacOS   |    N/A      |
| Android |    N/A      |
| Web     |          |

## Checklist

- [x] I have tested this on a device and a simulator
- [ ] I added documentation in `README.md`
- [ ] I updated the typed files (typescript)
- [ ] I added a test for the API in the `__tests__` folder
2024-11-15 12:17:12 +01:00

68 lines
2.3 KiB
C++

#pragma once
#include "ImageView.g.h"
#include "RenderableView.h"
#include <wincodec.h>
namespace winrt::RNSVG::implementation {
enum class ImageSourceType { Uri = 0, Download = 1, InlineData = 2 };
enum class ImageSourceFormat { Bitmap = 0, Svg = 1 };
struct ImageSource {
std::string uri{""};
std::string method{""};
std::vector<std::pair<hstring, hstring>> headers{};
float width{0.0f};
float height{0.0f};
float scale{1.0f};
bool packagerAsset{false};
ImageSourceType type{ImageSourceType::Uri};
ImageSourceFormat format{ImageSourceFormat::Bitmap};
};
struct ImageView : ImageViewT<ImageView, RNSVG::implementation::RenderableView> {
public:
ImageView() = default;
// IRenderablePaper
void UpdateProperties(Microsoft::ReactNative::IJSValueReader const &reader, bool forceUpdate, bool invalidate);
// IRenderable
void Draw(RNSVG::D2DDeviceContext const &deviceContext, Windows::Foundation::Size const &size);
void CreateResources();
void Unload();
private:
RNSVG::SVGLength m_x{};
RNSVG::SVGLength m_y{};
RNSVG::SVGLength m_width{};
RNSVG::SVGLength m_height{};
ImageSource m_source{};
ImageSourceType m_type{ImageSourceType::Uri};
ImageSourceFormat m_format{ImageSourceFormat::Bitmap};
// preserveAspectRatio
std::string m_align{""};
RNSVG::MeetOrSlice m_meetOrSlice{RNSVG::MeetOrSlice::Meet};
com_ptr<IWICBitmap> m_wicbitmap;
Windows::Foundation::IAsyncAction LoadImageSourceAsync(bool invalidate);
Windows::Foundation::IAsyncOperation<Windows::Storage::Streams::InMemoryRandomAccessStream>
GetImageMemoryStreamAsync(ImageSource source);
Windows::Foundation::IAsyncOperation<Windows::Storage::Streams::InMemoryRandomAccessStream>
GetImageStreamAsync(ImageSource source);
Windows::Foundation::IAsyncOperation<Windows::Storage::Streams::InMemoryRandomAccessStream>
GetImageInlineDataAsync(ImageSource source);
com_ptr<IWICBitmapSource> wicBitmapSourceFromStream(
Windows::Storage::Streams::InMemoryRandomAccessStream const &stream);
void generateBitmap(
Windows::Storage::Streams::InMemoryRandomAccessStream const &results);
};
} // namespace winrt::RNSVG::implementation
namespace winrt::RNSVG::factory_implementation {
struct ImageView : ImageViewT<ImageView, implementation::ImageView> {};
} // namespace winrt::RNSVG::factory_implementation