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

Allow external texture sources when using the Metal backend. #17154

Merged
merged 1 commit into from
Mar 23, 2020

Conversation

chinmaygarde
Copy link
Member

@chinmaygarde chinmaygarde commented Mar 14, 2020

Allow external texture sources when using the Metal backend.

This patch moves the responsibility of vending external texture proxies to the
client rendering API specific IOS context subclasses.

Some key changes in the Metal backend compared to OpenGL.

  • The Metal backend has a single texture cache for all external textures as
    opposed to a separate cache for each texture proxy in the OpenGL backend. This
    use is more desirable as cached entries may be reused by multiple proxies for
    better cache utilization. This implementation is harder in the OpenGL backend
    because the cache cannot be created upfront as thread local state is needed to
    (re)initialize the cache. This update can be made to the OpenGL backend as
    well but it is unrelated to the primary purpose of this patch.
  • Attempting to use external textures with the software backend now logs a
    single message to the console that this feature is only available when using
    the Metal or OpenGL backend on devices. Previously, an OpenGL texture proxy
    creation would be attempted and it would fail in an undefined spot internally
    with a confusing message.
  • The OpenGL backend seems to conflate rendering size with paint size. This puts
    the unnecessary limitation that the embedders must provide textures at the
    size at which they will be rendered. This limitation has been lifted in the
    Metal backend and should probably be lifted in the OpenGL backend as well (in
    a later patch).

There are opportunities to dry up the implementations of the
IOSExternalTextureMetal and IOSExternalTextureGL but that refactor can be
attempted separately.

Fixes flutter/flutter#41819

@chinmaygarde chinmaygarde added the Work in progress (WIP) Not ready (yet) for review! label Mar 14, 2020
@auto-assign auto-assign bot requested a review from cbracken March 14, 2020 22:51
@chinmaygarde chinmaygarde force-pushed the external_textures branch 3 times, most recently from 72a015e to ead5edd Compare March 19, 2020 02:51
@chinmaygarde chinmaygarde force-pushed the external_textures branch 2 times, most recently from e353f09 to 520383c Compare March 23, 2020 02:26
@chinmaygarde chinmaygarde added waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. and removed Work in progress (WIP) Not ready (yet) for review! labels Mar 23, 2020
@chinmaygarde
Copy link
Member Author

As c0deb23 has landed, this is good to go. All existing plugins that composite external textures have been tested to work with the Metal backend without modification.

@fluttergithubbot
Copy link
Contributor

This pull request is not suitable for automatic merging in its current state.

  • Please get at least one approved review before re-applying this label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@fluttergithubbot fluttergithubbot removed the waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. label Mar 23, 2020
@@ -36,14 +51,20 @@ class CFRef {
instance_ = instance;
}

[[nodiscard]] T Release() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we on C++ 17 everywhere we have to compile? Particularly google3?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes.

@@ -34,6 +35,19 @@
return;
}

CVMetalTextureCacheRef texture_cache_raw = NULL;
auto cv_return = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
NULL, // cache attributes (NULL default)
Copy link
Contributor

Choose a reason for hiding this comment

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

clang format doesn't line these up?

Copy link
Member Author

Choose a reason for hiding this comment

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

Apparently not. I don't format code so its got to be clang-format. I think its because the comments are pushed up against the column limit.

&texture_cache_raw // [out] cache
);
if (cv_return != kCVReturnSuccess) {
FML_LOG(ERROR) << "Could not create Metal texture cache.";
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this mean for a Flutter developer? What action should be taken if any?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hah. Docs are woefully lacking here, apparently it just documents that when it succeeds it returns this and nothing else... https://developer.apple.com/documentation/corevideo/1456774-cvmetaltexturecachecreate?language=objc

Copy link
Contributor

Choose a reason for hiding this comment

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

should this really be FML_DLOG?

Copy link
Member Author

Choose a reason for hiding this comment

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

What action should be taken if any?

If I saw that in the a bug report, I'd know immediately where what went wrong and where. If a user filed an error for this with no logs, I probably wouldn't find the cause unless I reproduced the issue and set various breakpoint (if the issue caught my attention at all).

I am on the fence about its use. Probably means I should just get rid of it. Patching.

Comment on lines +121 to +123
// External images in this backend have no thread affinity and are not tied to the context in any
// way. Instead, they are tied to the Metal device which is associated with the cache already and
// is consistent throughout the shell run.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is exciting.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

LGTM with nit about logging. If a developer can take action on it,w e should clarify what action to take - otherwise we should probably make a DLOG.

This patch moves the responsibility of vending external texture proxies to the
client rendering API specific IOS context subclasses.

Some key changes in the Metal backend compared to OpenGL.
* The Metal backend has a single texture cache for all external textures as
  opposed to a separate cache for each texture proxy in the OpenGL backend. This
  use is more desirable as cached entries may be reused by multiple proxies for
  better cache utilization. This implementation is harder in the OpenGL backend
  because the cache cannot be created upfront as thread local state is needed to
  (re)initialize the cache. This update can be made to the OpenGL backend as
  well but it is unrelated to the primary purpose of this patch.
* Attempting to use external textures with the software backend now logs a
  single message to the console that this feature is only available when using
  the Metal or OpenGL backend on devices. Previously, an OpenGL texture proxy
  creation would be attempted and it would fail in an undefined spot internally
  with a confusing message.
* The OpenGL backend seems to conflate rendering size with paint size. This puts
  the unnecessary limitation that the embedders must provide textures at the
  size at which they will be rendered. This limitation has been lifted in the
  Metal backend and should probably be lifted in the OpenGL backend as well (in
  a later patch).

There are opportunities to dry up the implementations of the
`IOSExternalTextureMetal` and `IOSExternalTextureGL` but that refactor can be
attempted separately.

Fixes flutter/flutter#41819
@chinmaygarde
Copy link
Member Author

If a developer can take action on it

This is unlikely for anything in these components. I converted everything to DLOG.

@chinmaygarde chinmaygarde deleted the external_textures branch July 7, 2022 22:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add external texture support when using the Metal rendering backend
4 participants