Skip to content

Commit a3eae14

Browse files
[CP-stable]Makes sure impeller context's are plumbed through to LayerTree::Flatten. (#191222)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: _What is the link to the issue this cherry-pick is addressing?_ #190774 ### Impact Description: _What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping of production apps (the app crashes on launch). This information is for domain experts and release engineers to understand the consequences of saying yes or no to the cherry pick._ There is a crash without this PR. It happens in user's built apps on Windows if the app is using external textures. ### Changelog Description: _Explain this cherry pick: * In one line that is accessible to most Flutter developers. * That describes the state prior to the fix. * That includes which platforms are impacted. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples._ < Replace with changelog description here > [flutter/190774] When using external textures on Windows, a crash could occur when the engine paints them. ### Workaround: _Is there a workaround for this issue?_ Opting out of Impeller. ### Risk: _What is the risk level of this cherry-pick?_ ### Test Coverage: _Are you confident that your fix is well-tested by automated tests?_ ### Validation Steps: _What are the steps to validate that this fix works?_ The issue author had reproduction code that was run on windows with the fix ( https://github.com/NH-Mason/flutter-190774-repro ).
1 parent d989cca commit a3eae14

11 files changed

Lines changed: 121 additions & 34 deletions

‎engine/src/flutter/flow/layers/layer_tree.cc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame,
147147
sk_sp<DisplayList> LayerTree::Flatten(
148148
const DlRect& bounds,
149149
const std::shared_ptr<TextureRegistry>& texture_registry,
150-
GrDirectContext* gr_context) {
150+
GrDirectContext* gr_context,
151+
impeller::AiksContext* aiks_context) {
151152
TRACE_EVENT0("flutter", "LayerTree::Flatten");
152153

153154
DisplayListBuilder builder(bounds);
@@ -188,6 +189,8 @@ sk_sp<DisplayList> LayerTree::Flatten(
188189
#if !SLIMPELLER
189190
.raster_cache = nullptr,
190191
#endif // !SLIMPELLER
192+
.impeller_enabled = !!aiks_context,
193+
.aiks_context = aiks_context,
191194
// clang-format on
192195
};
193196

‎engine/src/flutter/flow/layers/layer_tree.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class LayerTree {
4848
sk_sp<DisplayList> Flatten(
4949
const DlRect& bounds,
5050
const std::shared_ptr<TextureRegistry>& texture_registry = nullptr,
51-
GrDirectContext* gr_context = nullptr);
51+
GrDirectContext* gr_context = nullptr,
52+
impeller::AiksContext* aiks_context = nullptr);
5253

5354
Layer* root_layer() const { return root_layer_.get(); }
5455
const DlISize& frame_size() const { return frame_size_; }

‎engine/src/flutter/flow/layers/layer_tree_unittests.cc‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flutter/fml/macros.h"
1313
#include "flutter/testing/canvas_test.h"
1414
#include "flutter/testing/display_list_testing.h"
15+
#include "gmock/gmock.h"
1516
#include "gtest/gtest.h"
1617

1718
namespace flutter {
@@ -277,5 +278,43 @@ TEST_F(LayerTreeTest, PaintContextInitialization) {
277278
expect_defaults(context);
278279
}
279280

281+
namespace {
282+
283+
class MockFlattenLayer : public Layer {
284+
public:
285+
MockFlattenLayer() { set_paint_bounds(DlRect::MakeWH(100, 100)); }
286+
287+
MOCK_METHOD(void, Preroll, (PrerollContext*), (override));
288+
MOCK_METHOD(void, Paint, (PaintContext&), (const, override));
289+
};
290+
291+
} // namespace
292+
293+
TEST_F(LayerTreeTest, FlattenInitializesContexts) {
294+
auto layer = std::make_shared<MockFlattenLayer>();
295+
auto layer_tree = BuildLayerTree(layer);
296+
297+
EXPECT_CALL(*layer, Preroll(::testing::_)).Times(2);
298+
299+
// Default: null contexts
300+
EXPECT_CALL(*layer, Paint(::testing::_)).WillOnce([](PaintContext& context) {
301+
EXPECT_EQ(context.gr_context, nullptr);
302+
EXPECT_EQ(context.aiks_context, nullptr);
303+
EXPECT_FALSE(context.impeller_enabled);
304+
});
305+
layer_tree->Flatten(DlRect::MakeWH(100, 100));
306+
307+
// With a fake AiksContext pointer
308+
auto fake_aiks = reinterpret_cast<impeller::AiksContext*>(0x1234);
309+
EXPECT_CALL(*layer, Paint(::testing::_))
310+
.WillOnce([fake_aiks](PaintContext& context) {
311+
EXPECT_EQ(context.gr_context, nullptr);
312+
EXPECT_EQ(context.aiks_context, fake_aiks);
313+
EXPECT_TRUE(context.impeller_enabled);
314+
});
315+
layer_tree->Flatten(DlRect::MakeWH(100, 100), /*texture_registry=*/nullptr,
316+
/*gr_context=*/nullptr, fake_aiks);
317+
}
318+
280319
} // namespace testing
281320
} // namespace flutter

‎engine/src/flutter/lib/ui/painting/display_list_deferred_image_gpu_impeller.cc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <variant>
99

1010
#include "flutter/fml/make_copyable.h"
11+
#include "impeller/display_list/aiks_context.h"
1112

1213
// Disable a warning on Windows about use of deprecated atomic operations
1314
// on std::shared_ptr. These functions are used because libcxx does not
@@ -175,9 +176,11 @@ void DlDeferredImageGPUImpeller::ImageWrapper::SnapshotDisplayList(
175176
content)) {
176177
std::unique_ptr<LayerTree> layer_tree =
177178
std::get<std::unique_ptr<LayerTree>>(std::move(content));
179+
auto aiks_context = snapshot_delegate->GetAiksContext();
178180
display_list = layer_tree->Flatten(
179181
DlRect::MakeWH(wrapper->size_.width, wrapper->size_.height),
180-
snapshot_delegate->GetTextureRegistry());
182+
snapshot_delegate->GetTextureRegistry(),
183+
/*gr_context=*/nullptr, aiks_context.get());
181184
}
182185

183186
auto texture = snapshot_delegate->MakeImpellerSnapshotSync(

‎engine/src/flutter/lib/ui/painting/picture.cc‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,14 @@ Dart_Handle Picture::DoRasterizeToImage(const sk_sp<DisplayList>& display_list,
251251
sk_sp<DisplayList> snapshot_display_list = display_list;
252252
if (layer_tree) {
253253
FML_DCHECK(picture_bounds == layer_tree->frame_size());
254-
snapshot_display_list =
255-
layer_tree->Flatten(DlRect::MakeWH(width, height),
256-
snapshot_delegate->GetTextureRegistry(),
257-
snapshot_delegate->GetGrContext());
254+
auto aiks_context = is_impeller_enabled
255+
? snapshot_delegate->GetAiksContext()
256+
: nullptr;
257+
snapshot_display_list = layer_tree->Flatten(
258+
DlRect::MakeWH(width, height),
259+
snapshot_delegate->GetTextureRegistry(),
260+
is_impeller_enabled ? nullptr : snapshot_delegate->GetGrContext(),
261+
aiks_context.get());
258262
}
259263
if (is_impeller_enabled) {
260264
#if IMPELLER_SUPPORTS_RENDERING

‎engine/src/flutter/lib/ui/painting/testing/mocks.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class MockSnapshotDelegate : public SnapshotDelegate {
3939
(),
4040
(override));
4141
MOCK_METHOD(GrDirectContext*, GetGrContext, (), (override));
42+
MOCK_METHOD(std::shared_ptr<impeller::AiksContext>,
43+
GetAiksContext,
44+
(),
45+
(const, override));
4246
MOCK_METHOD(void,
4347
MakeSkiaSnapshot,
4448
(sk_sp<DisplayList>,

‎engine/src/flutter/lib/ui/snapshot_delegate.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define FLUTTER_LIB_UI_SNAPSHOT_DELEGATE_H_
77

88
#include <functional>
9+
#include <memory>
910
#include <string>
1011

1112
#include "flutter/common/graphics/texture.h"
@@ -19,6 +20,7 @@
1920
namespace impeller {
2021
class Texture;
2122
class RuntimeStage;
23+
class AiksContext;
2224
} // namespace impeller
2325

2426
namespace flutter {
@@ -83,6 +85,8 @@ class SnapshotDelegate {
8385

8486
virtual GrDirectContext* GetGrContext() = 0;
8587

88+
virtual std::shared_ptr<impeller::AiksContext> GetAiksContext() const = 0;
89+
8690
virtual void MakeSkiaSnapshot(sk_sp<DisplayList> display_list,
8791
DlISize picture_size,
8892
std::function<void(sk_sp<SkImage>)> callback,

‎engine/src/flutter/shell/platform/embedder/embedder_external_texture_gl.cc‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,19 @@ sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTexture(
7171
const SkISize& size) {
7272
if (!!aiks_context) {
7373
return ResolveTextureImpeller(texture_id, aiks_context, size);
74-
} else {
74+
} else if (context) {
7575
return ResolveTextureSkia(texture_id, context, size);
7676
}
77+
return nullptr;
7778
}
7879

7980
sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTextureSkia(
8081
int64_t texture_id,
8182
GrDirectContext* context,
8283
const SkISize& size) {
84+
if (!context) {
85+
return nullptr;
86+
}
8387
context->flushAndSubmit();
8488
context->resetContext(kAll_GrBackendState);
8589
std::unique_ptr<FlutterOpenGLTexture> texture =

‎engine/src/flutter/shell/platform/embedder/embedder_external_texture_gl.h‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,6 @@ class EmbedderExternalTextureGL : public flutter::Texture {
2222

2323
~EmbedderExternalTextureGL();
2424

25-
private:
26-
const ExternalTextureCallback& external_texture_callback_;
27-
sk_sp<DlImage> last_image_;
28-
29-
sk_sp<DlImage> ResolveTexture(int64_t texture_id,
30-
GrDirectContext* context,
31-
impeller::AiksContext* aiks_context,
32-
const SkISize& size);
33-
34-
sk_sp<DlImage> ResolveTextureSkia(int64_t texture_id,
35-
GrDirectContext* context,
36-
const SkISize& size);
37-
38-
sk_sp<DlImage> ResolveTextureImpeller(int64_t texture_id,
39-
impeller::AiksContext* aiks_context,
40-
const SkISize& size);
41-
4225
// |flutter::Texture|
4326
void Paint(PaintContext& context,
4427
const DlRect& bounds,
@@ -57,6 +40,23 @@ class EmbedderExternalTextureGL : public flutter::Texture {
5740
// |flutter::Texture|
5841
void OnTextureUnregistered() override;
5942

43+
private:
44+
const ExternalTextureCallback& external_texture_callback_;
45+
sk_sp<DlImage> last_image_;
46+
47+
sk_sp<DlImage> ResolveTexture(int64_t texture_id,
48+
GrDirectContext* context,
49+
impeller::AiksContext* aiks_context,
50+
const SkISize& size);
51+
52+
sk_sp<DlImage> ResolveTextureSkia(int64_t texture_id,
53+
GrDirectContext* context,
54+
const SkISize& size);
55+
56+
sk_sp<DlImage> ResolveTextureImpeller(int64_t texture_id,
57+
impeller::AiksContext* aiks_context,
58+
const SkISize& size);
59+
6060
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureGL);
6161
};
6262

‎engine/src/flutter/shell/platform/embedder/embedder_external_texture_metal.h‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ class EmbedderExternalTextureMetal : public flutter::Texture {
2424

2525
~EmbedderExternalTextureMetal();
2626

27-
private:
28-
const ExternalTextureCallback& external_texture_callback_;
29-
sk_sp<DlImage> last_image_;
30-
31-
sk_sp<DlImage> ResolveTexture(int64_t texture_id,
32-
GrDirectContext* context,
33-
impeller::AiksContext* aiks_context,
34-
const SkISize& size);
35-
3627
// |flutter::Texture|
3728
void Paint(PaintContext& context,
3829
const DlRect& bounds,
@@ -51,6 +42,15 @@ class EmbedderExternalTextureMetal : public flutter::Texture {
5142
// |flutter::Texture|
5243
void OnTextureUnregistered() override;
5344

45+
private:
46+
const ExternalTextureCallback& external_texture_callback_;
47+
sk_sp<DlImage> last_image_;
48+
49+
sk_sp<DlImage> ResolveTexture(int64_t texture_id,
50+
GrDirectContext* context,
51+
impeller::AiksContext* aiks_context,
52+
const SkISize& size);
53+
5454
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureMetal);
5555
};
5656

0 commit comments

Comments
 (0)