Skip to content

Commit

Permalink
Use moved GrBackendTexture factories for Skia's Metal backend (#51828)
Browse files Browse the repository at this point in the history
In https://skia-review.googlesource.com/c/skia/+/833302, Skia moved the
constructors for Metal versions of GrBackendTexture (and other
functions/methods). This updates uses in Flutter to use the new APIs.

## 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].
- [ ] 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/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
  • Loading branch information
kjlubick authored Apr 2, 2024
1 parent 87c6127 commit 6883f73
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 37 deletions.
5 changes: 4 additions & 1 deletion shell/gpu/gpu_surface_metal_skia.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "third_party/skia/include/gpu/GpuTypes.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlTypes.h"
#include "third_party/skia/include/ports/SkCFObject.h"

static_assert(!__has_feature(objc_arc), "ARC must be disabled.");
Expand All @@ -45,7 +47,8 @@
SkSurface::ReleaseContext release_context) {
GrMtlTextureInfo info;
info.fTexture.reset([texture retain]);
GrBackendTexture backend_texture(texture.width, texture.height, skgpu::Mipmapped::kNo, info);
GrBackendTexture backend_texture =
GrBackendTextures::MakeMtl(texture.width, texture.height, skgpu::Mipmapped::kNo, info);
return SkSurfaces::WrapBackendTexture(
context, backend_texture, origin, static_cast<int>(sample_cnt), color_type,
std::move(color_space), props, release_proc, release_context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "third_party/skia/include/gpu/GrDirectContext.h"
#include "third_party/skia/include/gpu/GrYUVABackendTextures.h"
#include "third_party/skia/include/gpu/ganesh/SkImageGanesh.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlTypes.h"
#include "third_party/skia/include/ports/SkCFObject.h"

FLUTTER_ASSERT_ARC
Expand Down Expand Up @@ -294,18 +296,14 @@ @implementation FlutterDarwinExternalTextureSkImageWrapper
ySkiaTextureInfo.fTexture = sk_cfp<const void*>{(__bridge_retained const void*)yTex};

GrBackendTexture skiaBackendTextures[2];
skiaBackendTextures[0] = GrBackendTexture(/*width=*/width,
/*height=*/height,
/*mipMapped=*/skgpu::Mipmapped::kNo,
/*mtlInfo=*/ySkiaTextureInfo);
skiaBackendTextures[0] =
GrBackendTextures::MakeMtl(width, height, skgpu::Mipmapped::kNo, ySkiaTextureInfo);

GrMtlTextureInfo uvSkiaTextureInfo;
uvSkiaTextureInfo.fTexture = sk_cfp<const void*>{(__bridge_retained const void*)uvTex};

skiaBackendTextures[1] = GrBackendTexture(/*width=*/width,
/*height=*/height,
/*mipMapped=*/skgpu::Mipmapped::kNo,
/*mtlInfo=*/uvSkiaTextureInfo);
skiaBackendTextures[1] =
GrBackendTextures::MakeMtl(width, height, skgpu::Mipmapped::kNo, uvSkiaTextureInfo);
SkYUVAInfo yuvaInfo(skiaBackendTextures[0].dimensions(), SkYUVAInfo::PlaneConfig::kY_UV,
SkYUVAInfo::Subsampling::k444, colorSpace);
GrYUVABackendTextures yuvaBackendTextures(yuvaInfo, skiaBackendTextures,
Expand All @@ -323,10 +321,8 @@ GrYUVABackendTextures yuvaBackendTextures(yuvaInfo, skiaBackendTextures,
GrMtlTextureInfo skiaTextureInfo;
skiaTextureInfo.fTexture = sk_cfp<const void*>{(__bridge_retained const void*)rgbaTex};

GrBackendTexture skiaBackendTexture(/*width=*/width,
/*height=*/height,
/*mipMapped=*/skgpu::Mipmapped ::kNo,
/*mtlInfo=*/skiaTextureInfo);
GrBackendTexture skiaBackendTexture =
GrBackendTextures::MakeMtl(width, height, skgpu::Mipmapped ::kNo, skiaTextureInfo);

return SkImages::BorrowTextureFrom(grContext, skiaBackendTexture, kTopLeft_GrSurfaceOrigin,
kBGRA_8888_SkColorType, kPremul_SkAlphaType,
Expand Down
21 changes: 11 additions & 10 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ extern const intptr_t kPlatformStrongDillSize;

#ifdef SHELL_ENABLE_METAL
#include "flutter/shell/platform/embedder/embedder_surface_metal.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlTypes.h"
#include "third_party/skia/include/ports/SkCFObject.h"
#ifdef IMPELLER_SUPPORTS_RENDERING
#include "flutter/shell/platform/embedder/embedder_render_target_impeller.h" // nogncheck
Expand Down Expand Up @@ -747,11 +749,9 @@ static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
texture_info.fID = texture->name;
texture_info.fFormat = texture->format;

auto backend_texture = GrBackendTextures::MakeGL(config.size.width, //
config.size.height, //
skgpu::Mipmapped::kNo, //
texture_info //
);
GrBackendTexture backend_texture =
GrBackendTextures::MakeGL(config.size.width, config.size.height,
skgpu::Mipmapped::kNo, texture_info);

SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);

Expand Down Expand Up @@ -922,11 +922,12 @@ static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
sk_cfp<FlutterMetalTextureHandle> mtl_texture;
mtl_texture.retain(metal->texture.texture);
texture_info.fTexture = mtl_texture;
GrBackendTexture backend_texture(config.size.width, //
config.size.height, //
skgpu::Mipmapped::kNo, //
texture_info //
);
GrBackendTexture backend_texture =
GrBackendTextures::MakeMtl(config.size.width, //
config.size.height, //
skgpu::Mipmapped::kNo, //
texture_info //
);

SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);

Expand Down
6 changes: 4 additions & 2 deletions shell/platform/embedder/tests/embedder_metal_unittests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "third_party/skia/include/gpu/GpuTypes.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlTypes.h"

// CREATE_NATIVE_ENTRY is leaky by design
// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
Expand Down Expand Up @@ -64,8 +66,8 @@
void* texture) {
GrMtlTextureInfo info;
info.fTexture.reset([(id<MTLTexture>)texture retain]);
GrBackendTexture backend_texture(texture_size.width(), texture_size.height(),
skgpu::Mipmapped::kNo, info);
GrBackendTexture backend_texture = GrBackendTextures::MakeMtl(
texture_size.width(), texture_size.height(), skgpu::Mipmapped::kNo, info);

return SkSurfaces::WrapBackendTexture(skia_context.get(), backend_texture,
kTopLeft_GrSurfaceOrigin, 1, kBGRA_8888_SkColorType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include "third_party/skia/include/gpu/ganesh/vk/GrVkBackendSurface.h"
#endif // SHELL_ENABLE_VULKAN

#ifdef SHELL_ENABLE_METAL
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlTypes.h"
#endif

// TODO(zanderso): https://github.com/flutter/flutter/issues/127701
// NOLINTBEGIN(bugprone-unchecked-optional-access)

Expand Down Expand Up @@ -281,8 +286,9 @@ bool EmbedderTestBackingStoreProducer::CreateMTLTexture(

GrMtlTextureInfo skia_texture_info;
skia_texture_info.fTexture.reset(SkCFSafeRetain(texture_info.texture));
GrBackendTexture backend_texture(surface_size.width(), surface_size.height(),
skgpu::Mipmapped::kNo, skia_texture_info);
GrBackendTexture backend_texture =
GrBackendTextures::MakeMtl(surface_size.width(), surface_size.height(),
skgpu::Mipmapped::kNo, skia_texture_info);

sk_sp<SkSurface> surface = SkSurfaces::WrapBackendTexture(
context_.get(), backend_texture, kTopLeft_GrSurfaceOrigin, 1,
Expand Down
9 changes: 1 addition & 8 deletions skia/flutter_defines.gni
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ flutter_defines = [

# Staging
"SK_LEGACY_IGNORE_DRAW_VERTICES_BLEND_WITH_NO_SHADER",
"SK_DISABLE_LEGACY_GRDIRECTCONTEXT_BOOLS",
"SK_DISABLE_LEGACY_GRDIRECTCONTEXT_FLUSH",
"SK_RESOLVE_FILTERS_BEFORE_RESTORE",
"SK_DISABLE_LEGACY_METAL_BACKEND_SURFACE",

# Fast low-precision software rendering isn't a priority for Flutter.
"SK_DISABLE_LEGACY_SHADERCONTEXT",
Expand All @@ -23,12 +22,6 @@ flutter_defines = [
# When running Metal, ensure that command buffers are scheduled before
# returning from submit.
"SK_METAL_WAIT_UNTIL_SCHEDULED",

# Staging for b/305780908
"SK_DEFAULT_TYPEFACE_IS_EMPTY",
"SK_DISABLE_LEGACY_DEFAULT_TYPEFACE",
"SK_DISABLE_LEGACY_FONTMGR_FACTORY",
"SK_DISABLE_LEGACY_FONTMGR_REFDEFAULT",
]

if (!is_fuchsia) {
Expand Down
6 changes: 4 additions & 2 deletions testing/test_metal_surface_impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "third_party/skia/include/gpu/GpuTypes.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendSurface.h"
#include "third_party/skia/include/gpu/ganesh/mtl/GrMtlTypes.h"

namespace flutter {

Expand All @@ -27,8 +29,8 @@

GrMtlTextureInfo skia_texture_info;
skia_texture_info.fTexture.reset([texture retain]);
GrBackendTexture backend_texture(surface_size.width(), surface_size.height(),
skgpu::Mipmapped::kNo, skia_texture_info);
GrBackendTexture backend_texture = GrBackendTextures::MakeMtl(
surface_size.width(), surface_size.height(), skgpu::Mipmapped::kNo, skia_texture_info);

sk_sp<SkSurface> surface = SkSurfaces::WrapBackendTexture(
test_metal_context_.GetSkiaContext().get(), backend_texture, kTopLeft_GrSurfaceOrigin, 1,
Expand Down

0 comments on commit 6883f73

Please sign in to comment.