Skip to content

Commit 511ee36

Browse files
authored
[Impeller] libImpeller: Allow fetching OpenGL texture handle. (flutter/engine#55753)
Fixes flutter#156359 cc @lyceel
1 parent b1c6343 commit 511ee36

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

engine/src/flutter/impeller/toolkit/interop/impeller.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ ImpellerTexture ImpellerTextureCreateWithOpenGLTextureHandleNew(
544544
return nullptr;
545545
}
546546
texture->SetCoordinateSystem(TextureCoordinateSystem::kUploadFromHost);
547-
return Create<Texture>(std::move(texture)).Leak();
547+
return Create<Texture>(impeller::Context::BackendType::kOpenGLES,
548+
std::move(texture))
549+
.Leak();
548550
}
549551

550552
IMPELLER_EXTERN_C
@@ -557,6 +559,19 @@ void ImpellerTextureRelease(ImpellerTexture texture) {
557559
ObjectBase::SafeRelease(texture);
558560
}
559561

562+
IMPELLER_EXTERN_C
563+
uint64_t ImpellerTextureGetOpenGLHandle(ImpellerTexture texture) {
564+
auto interop_texture = GetPeer(texture);
565+
if (interop_texture->GetBackendType() !=
566+
impeller::Context::BackendType::kOpenGLES) {
567+
VALIDATION_LOG << "Can only fetch the texture handle of an OpenGL texture.";
568+
return 0u;
569+
}
570+
return TextureGLES::Cast(*interop_texture->GetTexture())
571+
.GetGLHandle()
572+
.value_or(0u);
573+
}
574+
560575
IMPELLER_EXTERN_C
561576
void ImpellerDisplayListRetain(ImpellerDisplayList display_list) {
562577
ObjectBase::SafeRetain(display_list);

engine/src/flutter/impeller/toolkit/interop/impeller.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ void ImpellerTextureRetain(ImpellerTexture IMPELLER_NULLABLE texture);
493493
IMPELLER_EXPORT
494494
void ImpellerTextureRelease(ImpellerTexture IMPELLER_NULLABLE texture);
495495

496+
IMPELLER_EXPORT
497+
uint64_t ImpellerTextureGetOpenGLHandle(
498+
ImpellerTexture IMPELLER_NONNULL texture);
499+
496500
//------------------------------------------------------------------------------
497501
// Color Sources
498502
//------------------------------------------------------------------------------

engine/src/flutter/impeller/toolkit/interop/impeller_unittests.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ TEST_P(InteropPlaygroundTest, CanCreateOpenGLImage) {
165165
external_texture //
166166
));
167167
ASSERT_TRUE(texture);
168+
169+
ASSERT_EQ(ImpellerTextureGetOpenGLHandle(texture.GetC()), external_texture);
170+
168171
auto builder =
169172
Adopt<DisplayListBuilder>(ImpellerDisplayListBuilderNew(nullptr));
170173
ImpellerPoint point = {100, 100};

engine/src/flutter/impeller/toolkit/interop/texture.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ Texture::Texture(const Context& context, const TextureDescriptor& descriptor) {
1515
if (!texture || !texture->IsValid()) {
1616
return;
1717
}
18+
backend_ = context.GetContext()->GetBackendType();
1819
texture_ = std::move(texture);
1920
}
2021

21-
Texture::Texture(std::shared_ptr<impeller::Texture> texture)
22-
: texture_(std::move(texture)) {}
22+
Texture::Texture(impeller::Context::BackendType backend,
23+
std::shared_ptr<impeller::Texture> texture)
24+
: backend_(backend), texture_(std::move(texture)) {}
2325

2426
Texture::~Texture() = default;
2527

@@ -45,4 +47,12 @@ sk_sp<DlImageImpeller> Texture::MakeImage() const {
4547
return DlImageImpeller::Make(texture_);
4648
}
4749

50+
impeller::Context::BackendType Texture::GetBackendType() const {
51+
return backend_;
52+
}
53+
54+
const std::shared_ptr<impeller::Texture>& Texture::GetTexture() const {
55+
return texture_;
56+
}
57+
4858
} // namespace impeller::interop

engine/src/flutter/impeller/toolkit/interop/texture.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class Texture final
1818
public:
1919
explicit Texture(const Context& context, const TextureDescriptor& descriptor);
2020

21-
explicit Texture(std::shared_ptr<impeller::Texture> texture);
21+
explicit Texture(impeller::Context::BackendType backend,
22+
std::shared_ptr<impeller::Texture> texture);
2223

2324
~Texture() override;
2425

@@ -34,7 +35,13 @@ class Texture final
3435

3536
sk_sp<DlImageImpeller> MakeImage() const;
3637

38+
impeller::Context::BackendType GetBackendType() const;
39+
40+
const std::shared_ptr<impeller::Texture>& GetTexture() const;
41+
3742
private:
43+
impeller::Context::BackendType backend_ =
44+
impeller::Context::BackendType::kMetal;
3845
std::shared_ptr<impeller::Texture> texture_;
3946
};
4047

0 commit comments

Comments
 (0)