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

[Impeller] add mechanism for sharing bdf inputs. #55701

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

jonahwilliams
Copy link
Member

@jonahwilliams jonahwilliams commented Oct 7, 2024

Introduces a mechanism to allow backdrop filters to 1) share backdrop inputs and 2) fuse filter applications for faster blurs.

This is a proposed solution to flutter/flutter#131568

Implemented:

  • Developer can specify a "backdrop id" which indicates that a backdrop layer should share the input texture and potentially cached filter for a layer.
  • Removes second save layer for each backdrop filter
  • Removes save layer trace event for backdrop filter
  • Can fuse backdrop filters if there is more than one identical filter

TBD:

  • Adjust heruristic to avoid applying bdf filter to entire screen

Suggestions: applying a bdf should be a distinct operation from a save layer in the DL builder/dispatcher. The saveLayer implmenentation in the impeller dispatcher is super convoluted because it needs to handle both.

Video

Video starts with normal bdf then I hot reload to specify that the bdfs share inputs/filters. This is running on a pixel 8 pro

Change to the macrobenchmark app is just:

  Widget build(BuildContext context) {
    Widget addBlur(Widget child, bool shouldBlur) {
      if (shouldBlur) {
        return ClipRect(
          child: BackdropFilter(
            filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
            backdropId: 1, // Added ID
            child: child,
          ),
        );
      } else {
        return child;
      }
    }
demo.mp4

Requires framework changes in https://github.com/jonahwilliams/flutter/pull/new/backdrop_id

@jonahwilliams
Copy link
Member Author

This code isn't ready for review yet, but adding @flar and @gaaclarke for high level input. This is the doc that I said I was going to write that I dragged my feet on...

@jonahwilliams
Copy link
Member Author

Also I notice in the video that there is a slight pixel shift when switching modes...

@jonahwilliams jonahwilliams marked this pull request as ready for review October 10, 2024 22:22
@jonahwilliams
Copy link
Member Author

Currently the transforms are incorrect if the same bdf id is used in different save layers.

Copy link
Contributor

@flar flar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reviewed the first half - the front end - on this pass.

One thought - why 64-bit signed integer for ID? Do we anticipate having this many BDFs? An optional value feels more appropriate at the Dart level than a magic value (or declaring that valid IDs must be positive). Making the parameter nullable at the Dart level seems simple. std::optional seems like overkill at the C++ level, but these don't get used all that much compared to all of the other work going on.

FML_UNREACHABLE();
}

virtual void saveLayer(const DlRect& bounds,
const SaveLayerOptions& options,
uint32_t total_content_depth,
DlBlendMode max_content_blend_mode,
const DlImageFilter* backdrop = nullptr) {
const DlImageFilter* backdrop = nullptr,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are implementations, aren't they? Do they need the default values?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Or all of these virtual tags?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not overrides so I think they need the default values. overrides of a virtual method don't need to re-specify them.

@@ -51,7 +51,8 @@ class DisplayListBuilder final : public virtual DlCanvas,
// |DlCanvas|
void SaveLayer(const SkRect* bounds,
const DlPaint* paint = nullptr,
const DlImageFilter* backdrop = nullptr) override;
const DlImageFilter* backdrop = nullptr,
int64_t backdrop_id = -1) override;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto - default values probably unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, in this case there is a lot of code calling directly to a DlBuilder not through the DlCanvas interface so perhaps it needs them for ease of use. That need would disappear if we did something more like Flutter and Skia and have the Builder simply represent the recording process and provide an actual DlCanvas implementation via a getter rather than via direct implementation...?

@@ -143,15 +141,17 @@ class DlOpReceiver {
// layer before further rendering happens.
virtual void saveLayer(const DlRect& bounds,
const SaveLayerOptions options,
const DlImageFilter* backdrop = nullptr) = 0;
const DlImageFilter* backdrop = nullptr,
int64_t backdrop_id = -1) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to be the front end and back end for DLs. Now that it is just the backend I think all default values throughout this interface are obsolete. But, maybe there are a couple of unit tests that still call these directly? I'll watch for this as I de-skiafy...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The DlOp records used during dispatch shouldn't need any defaults for the calls they make.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed flutter/flutter#156617 to remind me to clean this up.

@@ -32,7 +32,8 @@ class DlSkCanvasAdapter final : public virtual DlCanvas {
void Save() override;
void SaveLayer(const SkRect* bounds,
const DlPaint* paint = nullptr,
const DlImageFilter* backdrop = nullptr) override;
const DlImageFilter* backdrop = nullptr,
int64_t backdrop_id = -1) override;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use this directly (as in on an instance of this class as opposed to casting it to a DlCanvas where the defaults already exist)? Actually we might in the embedders or other platform code.

@jonahwilliams
Copy link
Member Author

Re: the 64 bit integer. Dart ints are 64 bits and signed, so using a smaller int means we'd need to do a runtime check or throw an error (folks could reasonably use Dart max int has an input). We could remove the -1 and make it nullable instead, I need to look into how to pass that through dart::ffi.

@@ -571,7 +572,7 @@ void DisplayListMetalComplexityCalculator::MetalHelper::drawDisplayList(
MetalHelper helper(Ceiling() - CurrentComplexityScore());
if (opacity < SK_Scalar1 && !display_list->can_apply_group_opacity()) {
auto bounds = display_list->GetBounds();
helper.saveLayer(bounds, SaveLayerOptions::kWithAttributes, nullptr);
helper.saveLayer(bounds, SaveLayerOptions::kWithAttributes, nullptr, -1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
helper.saveLayer(bounds, SaveLayerOptions::kWithAttributes, nullptr, -1);
helper.saveLayer(bounds, SaveLayerOptions::kWithAttributes, nullptr, /*backdrop_id=*/-1);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -627,7 +628,7 @@ void DisplayListGLComplexityCalculator::GLHelper::drawDisplayList(
GLHelper helper(Ceiling() - CurrentComplexityScore());
if (opacity < SK_Scalar1 && !display_list->can_apply_group_opacity()) {
auto bounds = display_list->GetBounds();
helper.saveLayer(bounds, SaveLayerOptions::kWithAttributes, nullptr);
helper.saveLayer(bounds, SaveLayerOptions::kWithAttributes, nullptr, -1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
helper.saveLayer(bounds, SaveLayerOptions::kWithAttributes, nullptr, -1);
helper.saveLayer(bounds, SaveLayerOptions::kWithAttributes, nullptr, /*backdrop_id=*/-1);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +65 to +66
const DlImageFilter* backdrop = nullptr,
std::optional<int64_t> backdrop_id = std::nullopt) = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making this a separate argument, does it make more sense to have this be a property of the backdrop itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backdrop filter argument is a DlImageFilter, which is a mirror of the dart:ui ImageFilter object. It doesn't make sense for the backdrop id to be a property of the image filter as 1) a single filter object can be used in multiple places in the same scene and 2) can be used in contexts for which the backdropId propety doesn't make sense like image filters.

I think instead it would make more sense to refactor saveLayer to group both the backdrop image filter and the backdrop id into one BackdropData struct.

callback(), //
SkIRect::MakeWH(render_target.GetRenderTargetSize().width,
render_target.GetRenderTargetSize().height), //
true //
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
true //
/*reset_host_buffer=*/true //

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -97,7 +97,8 @@ static std::shared_ptr<Texture> FlipBackdrop(
std::vector<LazyRenderingConfig>& render_passes,
Point global_pass_position,
EntityPassClipStack& clip_coverage_stack,
ContentContext& renderer) {
ContentContext& renderer,
bool should_remove_texture = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add docstring.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

bool will_cache_texture = false;
BackdropData* data = nullptr;
if (backdrop_id.has_value()) {
const auto& backdrop_data = backdrop_keys_.find(backdrop_id.value());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const auto& backdrop_data = backdrop_keys_.find(backdrop_id.value());
const auto& backdrop_data_it = backdrop_keys_.find(backdrop_id.value());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}

if (!will_cache_texture || (will_cache_texture && !data->texture_slot)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data is potentially nullptr here no? It's hard to read this code in github with comments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will_cache_texture is only true if backdrop_data wasn't nullptr, so we'd only take the first part of the OR branch here.

@@ -1084,6 +1116,42 @@ void Canvas::SaveLayer(const Paint& paint,
transform_stack_.back().transform.HasTranslation()
? Entity::RenderingMode::kSubpassPrependSnapshotTransform
: Entity::RenderingMode::kSubpassAppendSnapshotTransform);

if (will_cache_texture) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will_cache_texture is so generic, but it specifically works with BackdropData, can't it be named more appropriate for what it's used for, backdrop filters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will_cache_backdrop_texture?

backdrop_filter_contents->RenderToSnapshot(renderer_, {});
}

auto maybe_snapshot = data->filtered_input_slot;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove auto please.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


backdrop_entity.Render(
renderer_,
*render_passes_.back().inline_pass_context->GetRenderPass(0).pass);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Holy moley, maybe we can break this chain up. It's quite a bit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its bad

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a helper

@gaaclarke
Copy link
Member

Since we didn't end up writing a design doc, I know we talked about it that one day over gvc but can you write down (or link to someplace where it is already written) why we needed to have users specify this explicitly and we couldn't come up with a heuristic to do this automatically?

@jonahwilliams
Copy link
Member Author

Since we didn't end up writing a design doc..

  1. The invalidation heuristics for backdrop filters would be expensive to track. We'd need to measure dirty regions between any two backdrops and that means recording/intersecting the bounds for all draws.
  2. The huerstics fail if filters like blurs are too close as the sample region will go far out of the output region (and this cannot be controlled).

@gaaclarke
Copy link
Member

Did you consider making the Dart API look like this instead of specifying an integer? This captures a scope more succinctly and doesn't require managing ids. The downside is that it is impossible to mix and match the blur layer across the code (though that makes the code easier to follow). Under the covers you could keep the implementation the same if you wanted.

Widget build(BuildContext context) {
  return BackdropGroup(
    children:[
      BackdropFilter(...),
      BackdropFilter(...),
    ]
  );
}

@jonahwilliams
Copy link
Member Author

I discussed this a bit with @flar and we both like the idea of separating the Backdrop widget from a backdrop-reading widget. So something like:

Widget build(BuildContext context) {
  return BackdropGroup(
    children:[
      BackdropFilter(parent: BackdropGroup.of(context)),
      BackdropFilter(...),
    ]
  );
}

But that is all Dart API that would be constructed at the framework layer. The implementation should look almost identical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants