Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@gaaclarke
Copy link
Member

@gaaclarke gaaclarke commented Dec 3, 2024

issue: flutter/flutter#159745

This tweaks ReactorGLES so that one can easily opt out of it's mutex/hash map for cases where we can statically reason about the safety of doing so. The goal here was to make migration of existing code really easy to do. It may be in the future that everything is an untracked handle? We can move there in baby steps.

Potential follow up PRs:

  • Move Pipeline to use untracked handles
  • Move DeviceBufferGLES to use untracked handles
  • Add a new method to synchronously delete untracked handles
  • Start storing handles to be deleted in its own vector, so handles_ doesn't need to be used for deleting untracked handles

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or the PR is test-exempt. See testing the engine for instructions on writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

type_(GetTextureTypeFromDescriptor(GetTextureDescriptor())),
handle_(external_handle.has_value()
? external_handle.value()
: reactor_->CreateHandle(ToHandleType(type_))),
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe textures can be created on any thread. To make the texture object manage its own handle, you'd lazily initialize it in the InitializeContentsIfNecessary function

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what's happening here. CreateUntrackedHandle creates the texture synchronously.

Copy link
Contributor

Choose a reason for hiding this comment

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

If it creates it synchronously, then we have to be careful not to create texture objects on threads other than raster/IO. but if you defer construction then the existing lifecycle mechanisms manage that for you.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what the assertion that the thread reacts is maintaining. I don't think we have a use case where we have to create textures on a thread other than the io/raster thread.

Copy link
Member Author

Choose a reason for hiding this comment

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

ReactorGLES::CreateHandle is already creating these synchronously too, so there is no change in the behavior here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

It only creates it immediately if CanReactOnCurrentThread is true:

Right, but we assert CanReactOnCurrentThread so it's the same. I ran wonderous with this patch ( #56927 (comment) ) and the assert was never hit. It seems like a feature we may not even be using.

Copy link
Contributor

Choose a reason for hiding this comment

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

it might be getting used by flutter gpu, but if we're not hitting the assert anywhere that is good enough for me

@gaaclarke gaaclarke marked this pull request as ready for review December 3, 2024 23:43
@gaaclarke gaaclarke changed the title Added the ability to make untracked opengles handles. Added the ability to make untracked opengles handles (migrated textures) Dec 3, 2024
@gaaclarke
Copy link
Member Author

@jonahwilliams I'm putting this into review now. It does a portion of what we were talking about. I think this moves us in the right direction to get there. Check out the description for some idea about where we go after this PR.

@gaaclarke
Copy link
Member Author

I checked wonderous with the following additions:

diff --git a/impeller/renderer/backend/gles/device_buffer_gles.cc b/impeller/renderer/backend/gles/device_buffer_gles.cc
index f4caaf5af8..aaed494cab 100644
--- a/impeller/renderer/backend/gles/device_buffer_gles.cc
+++ b/impeller/renderer/backend/gles/device_buffer_gles.cc
@@ -17,7 +17,7 @@ DeviceBufferGLES::DeviceBufferGLES(DeviceBufferDescriptor desc,
                                    std::shared_ptr<Allocation> backing_store)
     : DeviceBuffer(desc),
       reactor_(std::move(reactor)),
-      handle_(reactor_ ? reactor_->CreateHandle(HandleType::kBuffer)
+      handle_(reactor_ ? reactor_->CreateUntrackedHandle(HandleType::kBuffer)
                        : HandleGLES::DeadHandle()),
       backing_store_(std::move(backing_store)) {}
 
diff --git a/impeller/renderer/backend/gles/unique_handle_gles.cc b/impeller/renderer/backend/gles/unique_handle_gles.cc
index ddf4081791..07b4bf00ef 100644
--- a/impeller/renderer/backend/gles/unique_handle_gles.cc
+++ b/impeller/renderer/backend/gles/unique_handle_gles.cc
@@ -11,7 +11,7 @@ namespace impeller {
 UniqueHandleGLES::UniqueHandleGLES(ReactorGLES::Ref reactor, HandleType type)
     : reactor_(std::move(reactor)) {
   if (reactor_) {
-    handle_ = reactor_->CreateHandle(type);
+    handle_ = reactor_->CreateUntrackedHandle(type);
   }
 }

Things are looking good to use this everywhere and all the overhead of reactor was eliminated from the profile.

Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

LGTM

@gaaclarke
Copy link
Member Author

Chatted with @jonahwilliams offline. This approach is looking good but I need to make sure I didn't break debug naming. We can punt on synchronous deletion the profiles after pipelines, buffers and textures are moved over is looking good.

@gaaclarke gaaclarke merged commit 8093eb3 into flutter:main Dec 4, 2024
28 checks passed
@chinmaygarde
Copy link
Member

chinmaygarde commented Dec 4, 2024

Does this put additional burden on the caller to ensure that texture handles in the OpenGL ES backend are created or destroyed on a specific thread?

@gaaclarke
Copy link
Member Author

Does this put additional burden on the caller to ensure that texture handles in the OpenGL ES backend are created or destroyed on a specific thread?

It has the burden that a call to create must happen on a thread that reacts. For deletion it goes through the same flow (so can happen on any thread). The major costs were GetHandle(), not the creation and deletion.

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 4, 2024
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Dec 4, 2024
…159808)

flutter/engine@1f7f37e...9e8fcad

2024-12-04 skia-flutter-autoroll@skia.org Manual roll Dart SDK from
470117150f34 to a2a9428e761f (1 revision) (flutter/engine#56939)
2024-12-04 30870216+gaaclarke@users.noreply.github.com Added the ability
to make untracked opengles handles (migrated textures)
(flutter/engine#56927)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC bdero@google.com,zra@google.com on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
auto-submit bot pushed a commit that referenced this pull request Dec 5, 2024
issue: flutter/flutter#159745

#56927 introduced untracked handles, but naming them didn't work.  This adds a test to make sure they work.  I kept naming thread-safe since it isn't happening often anyways.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
@chinmaygarde
Copy link
Member

In that case, this will be an issue for the OpenGL Flutter GPU runtime. I'll file a bug to sort out the threading implications for Flutter GPU like usages.

@gaaclarke
Copy link
Member Author

In that case, this will be an issue for the OpenGL Flutter GPU runtime. I'll file a bug to sort out the threading implications for Flutter GPU like usages.

Here's a model on how to do it: a289daa

You track the untracked handle only long enough to perform the operation. That makes things like naming a item require synchronization, but no hash table lookup. For creation we'd need a future so that we can make sure subsequent accesses to the name require no hash table lookup or synchronization.

@gaaclarke
Copy link
Member Author

@chinmaygarde also note I haven't closed any existing code paths yet. the slow fully tracked way is still available. We'll probably want to move everything to untracked at some point but have 2 entrypoints that are threadsafe vs not threadsafe, not tracked vs not tracked.

nick9822 pushed a commit to nick9822/flutter that referenced this pull request Dec 18, 2024
…es) (flutter/engine#56927)

issue: flutter#159745

This tweaks ReactorGLES so that one can easily opt out of it's
mutex/hash map for cases where we can statically reason about the safety
of doing so. The goal here was to make migration of existing code really
easy to do. It may be in the future that everything is an untracked
handle? We can move there in baby steps.

Potential follow up PRs:
- Move `Pipeline` to use untracked handles
- Move `DeviceBufferGLES` to use untracked handles
- Add a new method to synchronously delete untracked handles
- Start storing handles to be deleted in its own vector, so handles_
doesn't need to be used for deleting untracked handles

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I signed the [CLA].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/engine/blob/main/docs/testing/Testing-the-engine.md
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
nick9822 pushed a commit to nick9822/flutter that referenced this pull request Dec 18, 2024
issue: flutter#159745

flutter/engine#56927 introduced untracked handles, but naming them didn't work.  This adds a test to make sure they work.  I kept naming thread-safe since it isn't happening often anyways.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants