-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathWindowsImageManager.cpp
More file actions
359 lines (297 loc) · 15.4 KB
/
Copy pathWindowsImageManager.cpp
File metadata and controls
359 lines (297 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "WindowsImageManager.h"
#include <CppRuntimeOptions.h>
#include <Fabric/Composition/CompositionContextHelper.h>
#include <Fabric/Composition/ImageResponseImage.h>
#include <Fabric/Composition/UriImageManager.h>
#include <Networking/NetworkPropertyIds.h>
#include <Utils/CppWinrtLessExceptions.h>
#include <Utils/ImageUtils.h>
#include <fmt/format.h>
#include <functional/functor.h>
#include <shcore.h>
#include <wincodec.h>
#include <winrt/Microsoft.ReactNative.Composition.h>
#include <winrt/Windows.Web.Http.Headers.h>
#include <winrt/Windows.Web.Http.h>
extern "C" HRESULT WINAPI WICCreateImagingFactory_Proxy(UINT SDKVersion, IWICImagingFactory **ppIWICImagingFactory);
namespace Microsoft::ReactNative {
std::string FormatHResultError(winrt::hresult_error const &ex) {
return fmt::format("[0x{:0>8x}] {}", static_cast<uint32_t>(ex.code()), winrt::to_string(ex.message()));
}
WindowsImageManager::WindowsImageManager(winrt::Microsoft::ReactNative::ReactContext reactContext)
: m_reactContext(reactContext) {
m_uriImageManager =
winrt::Microsoft::ReactNative::Composition::implementation::UriImageManager::Get(reactContext.Properties());
// Ideally we'd just set m_httpClient.DefaultRequestHeaders().UserAgent().ParseAdd(m_defaultUserAgent), but when we do
// we start hitting E_STATE_CHANGED errors. Which appears to be this issue
// https://github.com/MicrosoftDocs/winrt-api/issues/2410 So instead we apply the header to each request
auto userAgent = Microsoft::React::GetRuntimeOptionString("Http.UserAgent");
if (userAgent.size() > 0) {
m_defaultUserAgent = winrt::to_hstring(userAgent);
} else if (auto userAgentProp = reactContext.Properties().Get(::Microsoft::React::DefaultUserAgentPropertyId())) {
m_defaultUserAgent = *userAgentProp;
}
}
std::tuple<
winrt::com_ptr<IWICBitmapSource>,
winrt::com_ptr<IWICImagingFactory>,
std::shared_ptr<facebook::react::ImageErrorInfo>>
wicBitmapSourceFromStream(const winrt::Windows::Storage::Streams::IRandomAccessStream &stream) noexcept {
try {
winrt::com_ptr<IWICImagingFactory> imagingFactory;
winrt::check_hresult(WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, imagingFactory.put()));
winrt::com_ptr<IStream> istream;
winrt::check_hresult(
CreateStreamOverRandomAccessStream(stream.as<IUnknown>().get(), __uuidof(IStream), istream.put_void()));
winrt::com_ptr<IWICBitmapDecoder> bitmapDecoder;
winrt::check_hresult(imagingFactory->CreateDecoderFromStream(
istream.get(), nullptr, WICDecodeMetadataCacheOnDemand, bitmapDecoder.put()));
if (!bitmapDecoder) {
auto errorInfo = std::make_shared<facebook::react::ImageErrorInfo>();
errorInfo->error = "Failed to decode the image.";
return {nullptr, nullptr, errorInfo};
}
winrt::com_ptr<IWICBitmapFrameDecode> decodedFrame;
winrt::check_hresult(bitmapDecoder->GetFrame(0, decodedFrame.put()));
return {decodedFrame, imagingFactory, nullptr};
} catch (winrt::hresult_error const &ex) {
auto errorInfo = std::make_shared<facebook::react::ImageErrorInfo>();
errorInfo->error = ::Microsoft::ReactNative::FormatHResultError(winrt::hresult_error(ex));
return {nullptr, nullptr, errorInfo};
}
}
winrt::Windows::Foundation::IAsyncOperation<winrt::Microsoft::ReactNative::Composition::ImageResponse>
WindowsImageManager::GetImageRandomAccessStreamAsync(
ReactImageSource source,
std::function<void(uint64_t loaded, uint64_t total)> progressCallback) const {
co_await winrt::resume_background();
winrt::Windows::Foundation::Uri uri(winrt::to_hstring(source.uri));
bool isFile = (uri.SchemeName() == L"file");
bool isAppx = (uri.SchemeName() == L"ms-appx");
if (isFile || isAppx) {
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::StorageFile> getFileSync{nullptr};
if (isFile) {
getFileSync = winrt::Windows::Storage::StorageFile::GetFileFromPathAsync(uri.AbsoluteCanonicalUri());
} else {
getFileSync = winrt::Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(uri);
}
winrt::Windows::Storage::StorageFile file(co_await getFileSync);
if (!file) {
co_return winrt::Microsoft::ReactNative::Composition::ImageFailedResponse(L"Failed to get file.");
}
co_return winrt::Microsoft::ReactNative::Composition::StreamImageResponse(co_await file.OpenReadAsync());
}
auto httpMethod{
source.method.empty() ? winrt::Windows::Web::Http::HttpMethod::Get()
: winrt::Windows::Web::Http::HttpMethod{winrt::to_hstring(source.method)}};
winrt::Windows::Web::Http::HttpRequestMessage request{httpMethod, uri};
if (!m_defaultUserAgent.empty()) {
request.Headers().Append(L"User-Agent", m_defaultUserAgent);
}
if (!source.headers.empty()) {
for (auto &header : source.headers) {
if (_stricmp(header.first.c_str(), "authorization") == 0) {
request.Headers().TryAppendWithoutValidation(winrt::to_hstring(header.first), winrt::to_hstring(header.second));
} else {
request.Headers().Append(winrt::to_hstring(header.first), winrt::to_hstring(header.second));
}
}
}
if (!source.body.empty()) {
auto bodyContent = winrt::Windows::Web::Http::HttpStringContent(
winrt::to_hstring(source.body), winrt::Windows::Storage::Streams::UnicodeEncoding::Utf8, L"application/json");
request.Content(bodyContent);
}
auto asyncOp = m_httpClient.SendRequestAsync(request);
co_await lessthrow_await_adapter<winrt::Windows::Foundation::IAsyncOperationWithProgress<
winrt::Windows::Web::Http::HttpResponseMessage,
winrt::Windows::Web::Http::HttpProgress>>{asyncOp};
if (asyncOp.Status() == winrt::Windows::Foundation::AsyncStatus::Error ||
asyncOp.Status() == winrt::Windows::Foundation::AsyncStatus::Canceled) {
auto errorMessage = FormatHResultError(winrt::hresult_error(asyncOp.ErrorCode()));
co_return winrt::Microsoft::ReactNative::Composition::ImageFailedResponse(
winrt::to_hstring("Network request failed: " + errorMessage));
}
winrt::Windows::Web::Http::HttpResponseMessage response = asyncOp.GetResults();
if (!response.IsSuccessStatusCode()) {
co_return winrt::Microsoft::ReactNative::Composition::ImageFailedResponse(
response.ReasonPhrase(), response.StatusCode(), response.Headers());
}
auto inputStream = co_await response.Content().ReadAsInputStreamAsync();
auto contentLengthRef = response.Content().Headers().ContentLength();
uint64_t total = contentLengthRef ? contentLengthRef.GetUInt64() : 0;
uint64_t loaded = 0;
winrt::Windows::Storage::Streams::InMemoryRandomAccessStream memoryStream;
winrt::Windows::Storage::Streams::DataReader reader(inputStream);
constexpr uint32_t bufferSize = 16 * 1024;
while (true) {
uint32_t loadedBuffer = co_await reader.LoadAsync(bufferSize);
if (loadedBuffer == 0)
break;
auto buffer = reader.ReadBuffer(loadedBuffer);
co_await memoryStream.WriteAsync(buffer);
loaded += loadedBuffer;
if (progressCallback) {
progressCallback(loaded, total);
}
}
memoryStream.Seek(0);
co_return winrt::Microsoft::ReactNative::Composition::StreamImageResponse(memoryStream.CloneStream());
}
facebook::react::ImageRequest WindowsImageManager::requestImage(
const facebook::react::ImageSource &imageSource,
facebook::react::SurfaceId surfaceId) const {
auto imageRequest = facebook::react::ImageRequest(imageSource, nullptr, {});
auto weakObserverCoordinator = (std::weak_ptr<const facebook::react::ImageResponseObserverCoordinator>)
imageRequest.getSharedObserverCoordinator();
// ImageResponseObserverCoordinator copies its observer list under a lock but dereferences the raw
// observer pointers after releasing it. Observers are added and removed on the UI thread (from
// ImageComponentView::setStateAndResubscribeImageResponseObserver), and that is also where the
// owning ImageComponentView - and with it the WindowsImageResponseObserver - is destroyed. Notifying
// the coordinator from the download/completion threads therefore races that teardown and can call
// into a freed observer. Marshal every notification onto the UI thread so subscription and
// notification are serialized on the same thread. Image decoding deliberately stays off the UI
// thread; only the notification itself is posted.
auto uiDispatcher = m_reactContext.UIDispatcher();
auto rnImageSource = winrt::Microsoft::ReactNative::Composition::implementation::MakeImageSource(imageSource);
auto provider = m_uriImageManager->TryGetUriImageProvider(m_reactContext.Handle(), rnImageSource);
winrt::Windows::Foundation::IAsyncOperation<winrt::Microsoft::ReactNative::Composition::ImageResponse>
imageResponseTask{nullptr};
if (provider) {
imageResponseTask = provider.GetImageResponseAsync(m_reactContext.Handle(), rnImageSource);
} else {
ReactImageSource source;
source.uri = imageSource.uri;
source.height = imageSource.size.height;
source.width = imageSource.size.width;
source.sourceType = ImageSourceType::Download;
source.body = imageSource.body;
auto progressCallback = [weakObserverCoordinator, uiDispatcher](int64_t loaded, int64_t total) {
float progress = total > 0 ? static_cast<float>(loaded) / static_cast<float>(total) : 1.0f;
uiDispatcher.Post([weakObserverCoordinator, progress, loaded, total]() {
if (auto observerCoordinator = weakObserverCoordinator.lock()) {
observerCoordinator->nativeImageResponseProgress(progress, loaded, total);
}
});
};
imageResponseTask = GetImageRandomAccessStreamAsync(source, progressCallback);
}
imageResponseTask.Completed([weakObserverCoordinator, uiDispatcher](auto asyncOp, auto status) {
if (weakObserverCoordinator.expired()) {
return;
}
auto postComplete = [weakObserverCoordinator, uiDispatcher](auto image) {
uiDispatcher.Post([weakObserverCoordinator, image = std::move(image)]() {
if (auto observerCoordinator = weakObserverCoordinator.lock()) {
observerCoordinator->nativeImageResponseComplete(facebook::react::ImageResponse(image, nullptr /*metadata*/));
}
});
};
auto postFailure = [weakObserverCoordinator,
uiDispatcher](std::shared_ptr<facebook::react::ImageErrorInfo> errorInfo) {
uiDispatcher.Post([weakObserverCoordinator, errorInfo = std::move(errorInfo)]() {
if (auto observerCoordinator = weakObserverCoordinator.lock()) {
observerCoordinator->nativeImageResponseFailed(facebook::react::ImageLoadError(errorInfo));
}
});
};
switch (status) {
case winrt::Windows::Foundation::AsyncStatus::Completed: {
auto imageResponse = asyncOp.GetResults();
auto selfImageResponse =
winrt::get_self<winrt::Microsoft::ReactNative::Composition::implementation::ImageResponse>(imageResponse);
auto imageResultOrError = selfImageResponse->ResolveImage();
if (imageResultOrError.image) {
postComplete(std::move(imageResultOrError.image));
} else {
postFailure(std::move(imageResultOrError.errorInfo));
}
break;
}
case winrt::Windows::Foundation::AsyncStatus::Canceled: {
auto errorInfo = std::make_shared<facebook::react::ImageErrorInfo>();
errorInfo->error = FormatHResultError(winrt::hresult_error(asyncOp.ErrorCode()));
postFailure(std::move(errorInfo));
break;
}
case winrt::Windows::Foundation::AsyncStatus::Error: {
auto errorInfo = std::make_shared<facebook::react::ImageErrorInfo>();
errorInfo->error = FormatHResultError(winrt::hresult_error(asyncOp.ErrorCode()));
postFailure(std::move(errorInfo));
break;
}
}
});
return imageRequest;
}
facebook::react::ImageRequest WindowsImageManager::requestImage(
const facebook::react::ImageSource &imageSource,
facebook::react::SurfaceId surfaceId,
const facebook::react::ImageRequestParams & /* imageRequestParams */,
facebook::react::Tag /* tag */) const {
return requestImage(imageSource, surfaceId);
}
} // namespace Microsoft::ReactNative
namespace winrt::Microsoft::ReactNative::Composition::implementation {
ImageResponseOrImageErrorInfo StreamImageResponse::ResolveImage() {
ImageResponseOrImageErrorInfo imageOrError;
try {
auto result = ::Microsoft::ReactNative::wicBitmapSourceFromStream(m_stream);
if (auto errorInfo = std::get<std::shared_ptr<facebook::react::ImageErrorInfo>>(result)) {
imageOrError.errorInfo = errorInfo;
return imageOrError;
}
auto imagingFactory = std::get<winrt::com_ptr<IWICImagingFactory>>(result);
auto decodedFrame = std::get<winrt::com_ptr<IWICBitmapSource>>(result);
winrt::com_ptr<IWICFormatConverter> converter;
winrt::check_hresult(imagingFactory->CreateFormatConverter(converter.put()));
winrt::check_hresult(converter->Initialize(
decodedFrame.get(),
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
nullptr,
0.0f,
WICBitmapPaletteTypeMedianCut));
imageOrError.image =
std::make_shared<winrt::Microsoft::ReactNative::Composition::implementation::ImageResponseImage>();
winrt::check_hresult(imagingFactory->CreateBitmapFromSource(
converter.get(), WICBitmapCacheOnLoad, imageOrError.image->m_wicbmp.put()));
} catch (winrt::hresult_error const &ex) {
imageOrError.errorInfo = std::make_shared<facebook::react::ImageErrorInfo>();
imageOrError.errorInfo->error = ::Microsoft::ReactNative::FormatHResultError(winrt::hresult_error(ex));
}
return imageOrError;
}
ImageResponseOrImageErrorInfo UriBrushFactoryImageResponse::ResolveImage() {
ImageResponseOrImageErrorInfo imageOrError;
imageOrError.image =
std::make_shared<winrt::Microsoft::ReactNative::Composition::implementation::ImageResponseImage>();
// Wrap the UriBrushFactory to provide the internal CompositionContext types
imageOrError.image
->m_brushFactory = [factory = m_factory](
const winrt::Microsoft::ReactNative::IReactContext &context,
const winrt::Microsoft::ReactNative::Composition::Experimental::ICompositionContext
&compositionContext) {
auto compositor =
winrt::Microsoft::ReactNative::Composition::Experimental::MicrosoftCompositionContextHelper::InnerCompositor(
compositionContext);
auto brush = factory(context, compositor);
return winrt::Microsoft::ReactNative::Composition::Experimental::implementation::MicrosoftCompositionContextHelper::
WrapBrush(brush);
};
return imageOrError;
}
} // namespace winrt::Microsoft::ReactNative::Composition::implementation
namespace winrt::Microsoft::ReactNative::Composition::Experimental::implementation {
winrt::Microsoft::ReactNative::Composition::implementation::ImageResponseOrImageErrorInfo
UriBrushFactoryImageResponse::ResolveImage() {
winrt::Microsoft::ReactNative::Composition::implementation::ImageResponseOrImageErrorInfo imageOrError;
imageOrError.image =
std::make_shared<winrt::Microsoft::ReactNative::Composition::implementation::ImageResponseImage>();
imageOrError.image->m_brushFactory = m_factory;
return imageOrError;
}
} // namespace winrt::Microsoft::ReactNative::Composition::Experimental::implementation