Skip to content

Commit 87c0219

Browse files
[wimp] Turn on multithreading for wimp. (#191747)
This turns multithreaded wimp on. This fixes a few issues that popped up when diagnosing problems related to multithreading: * Instead of directly transferring `VideoFrame` objects to the web worker, make an `ImageBitmap` out of it. This normalizes the EXIF rotation of the image so that we don't end up with problems transferrring it to a texture. * Use globals for the raster thread rather than function-local statics. Emscripten uses some complex locking schemes for function-local statics that can sometimes cause compilcations and deadlocks, and they are overkill for what we're doing here. Using globals simplifies any locking schemes. * Pass the `ContentContext` when generating the impeller texture. This makes sure that paths like the picture-to-image code path can share resources like the typographer context and cached runtime effects. * Because we're sharing the `ContentContext` when rendering pictures to images now, we do a pre-pass that renders pictures to their image representations before starting the rendering of the frame and puts them in the image cache. This avoids having nested render passes inside of a frame, which can cause problems for the state of the glyph atlas in the `ContentContext`
1 parent 5b9af9a commit 87c0219

30 files changed

Lines changed: 349 additions & 106 deletions

‎engine/src/flutter/impeller/display_list/aiks_dl_atlas_unittests.cc‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ CreateTestData(const AiksTest* test) {
3838
// Draws the image as four squares stiched together.
3939
auto atlas =
4040
DlImageImpeller::Make(test->CreateTextureForFixture("bay_bridge.jpg"));
41-
auto size = atlas->GetImpellerTexture(test->GetContext())->GetSize();
41+
ContentContext content_context(test->GetContext(), nullptr);
42+
auto size = atlas->GetImpellerTexture(content_context)->GetSize();
4243
// Divide image into four quadrants.
4344
Scalar half_width = size.width / 2;
4445
Scalar half_height = size.height / 2;
@@ -119,7 +120,8 @@ TEST_P(AiksTest, DrawAtlasWithOpacity) {
119120

120121
TEST_P(AiksTest, DrawAtlasNoColorFullSize) {
121122
auto atlas = DlImageImpeller::Make(CreateTextureForFixture("bay_bridge.jpg"));
122-
auto size = atlas->GetImpellerTexture(GetContext())->GetSize();
123+
ContentContext content_context(GetContext(), nullptr);
124+
auto size = atlas->GetImpellerTexture(content_context)->GetSize();
123125
std::vector<DlRect> texture_coordinates = {
124126
DlRect::MakeLTRB(0, 0, size.width, size.height)};
125127
std::vector<RSTransform> transforms = {MakeTranslation(0, 0)};
@@ -189,15 +191,14 @@ TEST_P(AiksTest, DrawAtlasPlusWideGamut) {
189191
TEST_P(AiksTest, DlAtlasGeometryNoBlendRenamed) {
190192
auto [texture_coordinates, transforms, atlas] = CreateTestData(this);
191193

192-
DlAtlasGeometry geom(atlas->GetImpellerTexture(GetContext()),
193-
transforms.data(), texture_coordinates.data(), nullptr,
194-
transforms.size(), BlendMode::kSrcOver, {},
195-
std::nullopt);
194+
ContentContext context(GetContext(), nullptr);
195+
DlAtlasGeometry geom(atlas->GetImpellerTexture(context), transforms.data(),
196+
texture_coordinates.data(), nullptr, transforms.size(),
197+
BlendMode::kSrcOver, {}, std::nullopt);
196198

197199
EXPECT_FALSE(geom.ShouldUseBlend());
198200
EXPECT_FALSE(geom.ShouldSkip());
199201

200-
ContentContext context(GetContext(), nullptr);
201202
auto vertex_buffer =
202203
geom.CreateSimpleVertexBuffer(context.GetTransientsDataBuffer());
203204

@@ -213,15 +214,15 @@ TEST_P(AiksTest, DlAtlasGeometryBlend) {
213214
for (auto i = 0u; i < texture_coordinates.size(); i++) {
214215
colors.push_back(DlColor::ARGB(0.5, 1, 1, 1));
215216
}
216-
DlAtlasGeometry geom(atlas->GetImpellerTexture(GetContext()),
217-
transforms.data(), texture_coordinates.data(),
218-
colors.data(), transforms.size(), BlendMode::kSrcOver,
219-
{}, std::nullopt);
217+
ContentContext context(GetContext(), nullptr);
218+
DlAtlasGeometry geom(atlas->GetImpellerTexture(context), transforms.data(),
219+
texture_coordinates.data(), colors.data(),
220+
transforms.size(), BlendMode::kSrcOver, {},
221+
std::nullopt);
220222

221223
EXPECT_TRUE(geom.ShouldUseBlend());
222224
EXPECT_FALSE(geom.ShouldSkip());
223225

224-
ContentContext context(GetContext(), nullptr);
225226
auto vertex_buffer =
226227
geom.CreateBlendVertexBuffer(context.GetTransientsDataBuffer());
227228

@@ -237,10 +238,10 @@ TEST_P(AiksTest, DlAtlasGeometryColorButNoBlend) {
237238
for (auto i = 0u; i < texture_coordinates.size(); i++) {
238239
colors.push_back(DlColor::ARGB(0.5, 1, 1, 1));
239240
}
240-
DlAtlasGeometry geom(atlas->GetImpellerTexture(GetContext()),
241-
transforms.data(), texture_coordinates.data(),
242-
colors.data(), transforms.size(), BlendMode::kSrc, {},
243-
std::nullopt);
241+
ContentContext context(GetContext(), nullptr);
242+
DlAtlasGeometry geom(atlas->GetImpellerTexture(context), transforms.data(),
243+
texture_coordinates.data(), colors.data(),
244+
transforms.size(), BlendMode::kSrc, {}, std::nullopt);
244245

245246
// Src blend mode means that colors would be ignored, even if provided.
246247
EXPECT_FALSE(geom.ShouldUseBlend());
@@ -255,10 +256,10 @@ TEST_P(AiksTest, DlAtlasGeometrySkip) {
255256
for (auto i = 0u; i < texture_coordinates.size(); i++) {
256257
colors.push_back(DlColor::ARGB(0.5, 1, 1, 1));
257258
}
258-
DlAtlasGeometry geom(atlas->GetImpellerTexture(GetContext()),
259-
transforms.data(), texture_coordinates.data(),
260-
colors.data(), transforms.size(), BlendMode::kClear, {},
261-
std::nullopt);
259+
ContentContext context(GetContext(), nullptr);
260+
DlAtlasGeometry geom(atlas->GetImpellerTexture(context), transforms.data(),
261+
texture_coordinates.data(), colors.data(),
262+
transforms.size(), BlendMode::kClear, {}, std::nullopt);
262263
EXPECT_TRUE(geom.ShouldSkip());
263264
}
264265

‎engine/src/flutter/impeller/display_list/aiks_dl_vertices_unittests.cc‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "impeller/display_list/dl_dispatcher.h"
1717
#include "impeller/display_list/dl_image_impeller.h"
1818
#include "impeller/display_list/dl_runtime_effect_impeller.h"
19+
#include "impeller/entity/contents/content_context.h"
1920
#include "third_party/abseil-cpp/absl/status/status_matchers.h"
2021

2122
namespace impeller {
@@ -53,7 +54,8 @@ TEST_P(AiksTest, VerticesGeometryUVPositionData) {
5354
DlPaint paint;
5455
auto image =
5556
DlImageImpeller::Make(CreateTextureForFixture("table_mountain_nx.png"));
56-
auto size = image->GetImpellerTexture(GetContext())->GetSize();
57+
ContentContext content_context(GetContext(), nullptr);
58+
auto size = image->GetImpellerTexture(content_context)->GetSize();
5759

5860
paint.setColorSource(
5961
DlColorSource::MakeImage(image, DlTileMode::kClamp, DlTileMode::kClamp));
@@ -76,7 +78,8 @@ TEST_P(AiksTest, VerticesGeometryUVPositionDataWithTranslate) {
7678
DlPaint paint;
7779
auto image =
7880
DlImageImpeller::Make(CreateTextureForFixture("table_mountain_nx.png"));
79-
auto size = image->GetImpellerTexture(GetContext())->GetSize();
81+
ContentContext content_context(GetContext(), nullptr);
82+
auto size = image->GetImpellerTexture(content_context)->GetSize();
8083

8184
DlMatrix matrix = DlMatrix::MakeTranslation({100, 100});
8285
paint.setColorSource(
@@ -101,7 +104,8 @@ TEST_P(AiksTest, VerticesGeometryColorUVPositionData) {
101104
DlPaint paint;
102105
auto image =
103106
DlImageImpeller::Make(CreateTextureForFixture("table_mountain_nx.png"));
104-
auto size = image->GetImpellerTexture(GetContext())->GetSize();
107+
ContentContext content_context(GetContext(), nullptr);
108+
auto size = image->GetImpellerTexture(content_context)->GetSize();
105109

106110
paint.setColorSource(
107111
DlColorSource::MakeImage(image, DlTileMode::kClamp, DlTileMode::kClamp));
@@ -129,7 +133,8 @@ TEST_P(AiksTest, VerticesGeometryColorUVPositionDataAdvancedBlend) {
129133
DlPaint paint;
130134
auto image =
131135
DlImageImpeller::Make(CreateTextureForFixture("table_mountain_nx.png"));
132-
auto size = image->GetImpellerTexture(GetContext())->GetSize();
136+
ContentContext content_context(GetContext(), nullptr);
137+
auto size = image->GetImpellerTexture(content_context)->GetSize();
133138

134139
paint.setColorSource(
135140
DlColorSource::MakeImage(image, DlTileMode::kClamp, DlTileMode::kClamp));

‎engine/src/flutter/impeller/display_list/canvas.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static void ApplyFramebufferBlend(Entity& entity) {
163163
/// @brief Create the subpass restore contents, appling any filters or opacity
164164
/// from the provided paint object.
165165
static std::shared_ptr<Contents> CreateContentsForSubpassTarget(
166-
const ContentContext& renderer,
166+
ContentContext& renderer,
167167
const Paint& paint,
168168
const std::shared_ptr<Texture>& target,
169169
const Matrix& effect_transform) {

‎engine/src/flutter/impeller/display_list/dl_dispatcher.cc‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ Canvas& CanvasDlDispatcher::GetCanvas() {
955955
return canvas_;
956956
}
957957

958-
const ContentContext& CanvasDlDispatcher::GetContentContext() const {
958+
ContentContext& CanvasDlDispatcher::GetContentContext() const {
959959
return renderer_;
960960
}
961961

@@ -1228,7 +1228,7 @@ FirstPassDispatcher::TakeBackdropData() {
12281228
std::shared_ptr<Texture> DisplayListToTexture(
12291229
const sk_sp<flutter::DisplayList>& display_list,
12301230
ISize size,
1231-
AiksContext& context,
1231+
ContentContext& context,
12321232
bool reset_host_buffer,
12331233
bool generate_mips,
12341234
std::optional<PixelFormat> target_pixel_format) {
@@ -1276,11 +1276,11 @@ std::shared_ptr<Texture> DisplayListToTexture(
12761276
}
12771277

12781278
DlIRect cull_rect = DlIRect::MakeWH(size.width, size.height);
1279-
impeller::FirstPassDispatcher collector(
1280-
context.GetContentContext(), impeller::Matrix(), Rect::MakeSize(size));
1279+
impeller::FirstPassDispatcher collector(context, impeller::Matrix(),
1280+
Rect::MakeSize(size));
12811281
display_list->Dispatch(collector, cull_rect);
12821282
impeller::CanvasDlDispatcher impeller_dispatcher(
1283-
context.GetContentContext(), //
1283+
context, //
12841284
target, //
12851285
/*is_onscreen=*/false, //
12861286
display_list->root_has_backdrop_filter(), //
@@ -1289,14 +1289,14 @@ std::shared_ptr<Texture> DisplayListToTexture(
12891289
);
12901290
const auto& [data, count] = collector.TakeBackdropData();
12911291
impeller_dispatcher.SetBackdropData(data, count);
1292-
context.GetContentContext().GetTextShadowCache().MarkFrameStart();
1292+
context.GetTextShadowCache().MarkFrameStart();
12931293
fml::ScopedCleanupClosure cleanup([&] {
12941294
if (reset_host_buffer) {
1295-
context.GetContentContext().GetTransientsDataBuffer().Reset();
1296-
context.GetContentContext().GetTransientsIndexesBuffer().Reset();
1295+
context.GetTransientsDataBuffer().Reset();
1296+
context.GetTransientsIndexesBuffer().Reset();
12971297
}
1298-
context.GetContentContext().GetTextShadowCache().MarkFrameEnd();
1299-
context.GetContentContext().GetLazyGlyphAtlas()->ResetTextFrames();
1298+
context.GetTextShadowCache().MarkFrameEnd();
1299+
context.GetLazyGlyphAtlas()->ResetTextFrames();
13001300
context.GetContext()->DisposeThreadLocalCachedResources();
13011301
});
13021302

@@ -1306,6 +1306,18 @@ std::shared_ptr<Texture> DisplayListToTexture(
13061306
return target.GetRenderTargetTexture();
13071307
}
13081308

1309+
std::shared_ptr<Texture> DisplayListToTexture(
1310+
const sk_sp<flutter::DisplayList>& display_list,
1311+
ISize size,
1312+
AiksContext& context,
1313+
bool reset_host_buffer,
1314+
bool generate_mips,
1315+
std::optional<PixelFormat> target_pixel_format) {
1316+
return DisplayListToTexture(display_list, size, context.GetContentContext(),
1317+
reset_host_buffer, generate_mips,
1318+
target_pixel_format);
1319+
}
1320+
13091321
bool RenderToTarget(ContentContext& context,
13101322
RenderTarget render_target,
13111323
const sk_sp<flutter::DisplayList>& display_list,

‎engine/src/flutter/impeller/display_list/dl_dispatcher.h‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class DlDispatcherBase : public flutter::DlOpReceiver {
289289

290290
virtual Canvas& GetCanvas() = 0;
291291

292-
virtual const ContentContext& GetContentContext() const = 0;
292+
virtual ContentContext& GetContentContext() const = 0;
293293

294294
std::shared_ptr<Texture> GetTexture(const sk_sp<flutter::DlImage>& image);
295295

@@ -347,10 +347,10 @@ class CanvasDlDispatcher : public DlDispatcherBase {
347347

348348
private:
349349
Canvas canvas_;
350-
const ContentContext& renderer_;
350+
ContentContext& renderer_;
351351

352352
Canvas& GetCanvas() override;
353-
const ContentContext& GetContentContext() const override;
353+
ContentContext& GetContentContext() const override;
354354
};
355355

356356
/// Performs a first pass over the display list to collect information
@@ -445,6 +445,15 @@ class FirstPassDispatcher : public flutter::IgnoreAttributeDispatchHelper,
445445
Paint paint_;
446446
};
447447

448+
/// Render the provided display list to a texture with the given size.
449+
std::shared_ptr<Texture> DisplayListToTexture(
450+
const sk_sp<flutter::DisplayList>& display_list,
451+
ISize size,
452+
ContentContext& context,
453+
bool reset_host_buffer = true,
454+
bool generate_mips = false,
455+
std::optional<PixelFormat> target_pixel_format = std::nullopt);
456+
448457
/// Render the provided display list to a texture with the given size.
449458
std::shared_ptr<Texture> DisplayListToTexture(
450459
const sk_sp<flutter::DisplayList>& display_list,

‎engine/src/flutter/impeller/display_list/dl_image_impeller.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ sk_sp<DlImageImpeller> DlImageImpeller::MakeFromYUVTextures(
5252
}
5353

5454
std::shared_ptr<Texture> DlImageImpeller::GetCachedTexture(
55-
const ContentContext& renderer) const {
55+
ContentContext& renderer) const {
5656
auto texture = renderer.GetCachedTexture(this);
5757
if (texture) {
5858
return texture;
5959
}
60-
texture = GetImpellerTexture(renderer.GetContext());
60+
texture = GetImpellerTexture(renderer);
6161
renderer.SetCachedTexture(this, texture);
6262
return texture;
6363
}
@@ -71,7 +71,7 @@ DlImageImpellerTexture::~DlImageImpellerTexture() = default;
7171

7272
// |DlImage|
7373
std::shared_ptr<impeller::Texture> DlImageImpellerTexture::GetImpellerTexture(
74-
const std::shared_ptr<impeller::Context>& context) const {
74+
ContentContext& renderer) const {
7575
return texture_;
7676
}
7777

‎engine/src/flutter/impeller/display_list/dl_image_impeller.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ class DlImageImpeller : public flutter::DlImage {
2626
bool isTextureBacked() const override { return true; }
2727

2828
virtual std::shared_ptr<Texture> GetImpellerTexture(
29-
const std::shared_ptr<Context>& context) const = 0;
29+
ContentContext& renderer) const = 0;
3030

31-
std::shared_ptr<Texture> GetCachedTexture(
32-
const ContentContext& renderer) const;
31+
std::shared_ptr<Texture> GetCachedTexture(ContentContext& renderer) const;
3332

3433
static sk_sp<DlImageImpeller> Make(
3534
std::shared_ptr<Texture> texture,
@@ -51,7 +50,7 @@ class DlImageImpellerTexture final : public DlImageImpeller {
5150

5251
// |DlImageImpeller|
5352
std::shared_ptr<Texture> GetImpellerTexture(
54-
const std::shared_ptr<Context>& context) const override;
53+
ContentContext& renderer) const override;
5554

5655
flutter::DlColorSpace GetColorSpace() const override;
5756
bool isOpaque() const override;

‎engine/src/flutter/impeller/display_list/image_filter.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace impeller {
1919

20-
std::shared_ptr<FilterContents> WrapInput(const ContentContext& renderer,
20+
std::shared_ptr<FilterContents> WrapInput(ContentContext& renderer,
2121
const flutter::DlImageFilter* filter,
2222
const FilterInput::Ref& input) {
2323
FML_DCHECK(filter);

‎engine/src/flutter/impeller/display_list/image_filter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace impeller {
1313

1414
/// @brief Generate a new FilterContents using this filter's configuration.
1515
///
16-
std::shared_ptr<FilterContents> WrapInput(const ContentContext& renderer,
16+
std::shared_ptr<FilterContents> WrapInput(ContentContext& renderer,
1717
const flutter::DlImageFilter* filter,
1818
const FilterInput::Ref& input);
1919

‎engine/src/flutter/impeller/display_list/paint.cc‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void Paint::ConvertStops(const flutter::DlGradientColorSourceBase* gradient,
6262
}
6363

6464
std::shared_ptr<ColorSourceContents> Paint::CreateContents(
65-
const ContentContext& renderer,
65+
ContentContext& renderer,
6666
const Geometry* geometry,
6767
const std::optional<Matrix>& geometry_transform) const {
6868
if (color_source == nullptr) {
@@ -284,7 +284,7 @@ std::shared_ptr<ColorSourceContents> Paint::CreateContents(
284284
}
285285

286286
std::shared_ptr<Contents> Paint::WithFilters(
287-
const ContentContext& renderer,
287+
ContentContext& renderer,
288288
std::shared_ptr<Contents> input) const {
289289
input = WithColorFilter(input, ColorFilterContents::AbsorbOpacity::kYes);
290290
auto image_filter = WithImageFilter(renderer, input, Matrix(),
@@ -296,7 +296,7 @@ std::shared_ptr<Contents> Paint::WithFilters(
296296
}
297297

298298
std::shared_ptr<Contents> Paint::WithFiltersForSubpassTarget(
299-
const ContentContext& renderer,
299+
ContentContext& renderer,
300300
std::shared_ptr<Contents> input,
301301
const Matrix& effect_transform) const {
302302
auto image_filter =
@@ -320,7 +320,7 @@ std::shared_ptr<Contents> Paint::WithMaskBlur(std::shared_ptr<Contents> input,
320320
}
321321

322322
std::shared_ptr<FilterContents> Paint::WithImageFilter(
323-
const ContentContext& renderer,
323+
ContentContext& renderer,
324324
const FilterInput::Variant& input,
325325
const Matrix& effect_transform,
326326
Entity::RenderingMode rendering_mode) const {
@@ -404,7 +404,7 @@ std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
404404

405405
std::shared_ptr<Contents> Paint::MaskBlurDescriptor::CreateMaskBlur(
406406
const Paint& paint,
407-
const ContentContext& renderer,
407+
ContentContext& renderer,
408408
const Geometry* geometry,
409409
std::shared_ptr<ColorSourceContents> contents,
410410
bool needs_color_filter,

0 commit comments

Comments
 (0)