Skip to content

Commit a38f7ec

Browse files
committed
Support GL_TEXTURE_EXTERNAL_OES embedder textures in Impeller
Impeller wrapped all OpenGL external textures as GL_TEXTURE_2D, so textures with a GL_TEXTURE_EXTERNAL_OES target (e.g. from a GTK FlTextureGL) failed to draw. Skia handles these, so this is a regression on Linux now Impeller is the default. GL_TEXTURE_RECTANGLE textures are also unsupported by Impeller and are not fixed here; they are now rejected with an error instead of failing to draw. Fixes flutter#195543
1 parent 418d48f commit a38f7ec

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

‎engine/src/flutter/shell/platform/embedder/embedder_external_texture_gl.cc‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ sk_sp<DlImage> EmbedderExternalTextureGL::ResolveTextureImpeller(
158158
impeller::TextureDescriptor desc;
159159
desc.size = impeller::ISize(texture->width, texture->height);
160160
desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt;
161+
switch (texture->target) {
162+
case GL_TEXTURE_2D:
163+
desc.type = impeller::TextureType::kTexture2D;
164+
break;
165+
case GL_TEXTURE_EXTERNAL_OES:
166+
desc.type = impeller::TextureType::kTextureExternalOES;
167+
break;
168+
default:
169+
FML_LOG(ERROR) << "Unsupported texture target " << texture->target;
170+
return nullptr;
171+
}
161172

162173
impeller::ContextGLES& context =
163174
impeller::ContextGLES::Cast(*aiks_context->GetContext());

‎engine/src/flutter/shell/platform/embedder/tests/embedder_gl_unittests.cc‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
#include <string>
1010
#include <vector>
1111

12+
#include "EGL/egl.h"
13+
#include "EGL/eglext.h"
14+
#include "GLES2/gl2.h"
15+
#include "GLES2/gl2ext.h"
1216
#include "GLES3/gl3.h"
17+
1318
#include "flutter/display_list/image/dl_image_skia.h"
1419
#include "flutter/flow/raster_cache.h"
1520
#include "flutter/fml/file.h"
@@ -4900,6 +4905,111 @@ TEST_F(EmbedderTest, RenderTextureWithImpellerOpenGL) {
49004905
}
49014906
}
49024907

4908+
TEST_F(EmbedderTest, RenderExternalTextureWithImpellerOpenGL) {
4909+
constexpr int kWidth = 800;
4910+
constexpr int kHeight = 600;
4911+
auto& context = GetEmbedderContext<EmbedderTestContextGL>();
4912+
EmbedderConfigBuilder builder(context);
4913+
fml::AutoResetWaitableEvent latch;
4914+
context.SetGLPresentCallback(
4915+
[&](FlutterPresentInfo present_info) { latch.Signal(); });
4916+
builder.AddCommandLineArgument("--enable-impeller");
4917+
builder.SetDartEntrypoint("render_texture_impeller_test");
4918+
builder.SetSurface(DlISize(kWidth, kHeight));
4919+
typedef void (*glGenTexturesProc)(GLsizei n, GLuint* textures);
4920+
typedef void (*glBindTextureProc)(GLenum n, GLuint texture);
4921+
typedef void (*glTexImage2DProc)(GLenum target, GLint level,
4922+
GLint internalformat, GLsizei width,
4923+
GLsizei height, GLint border, GLenum format,
4924+
GLenum type, const void* pixels);
4925+
typedef void (*glTexParameteriProc)(GLenum target, GLenum pname, GLint param);
4926+
static glGenTexturesProc glGenTextures = reinterpret_cast<glGenTexturesProc>(
4927+
context.GLGetProcAddress("glGenTextures"));
4928+
static glBindTextureProc glBindTexture = reinterpret_cast<glBindTextureProc>(
4929+
context.GLGetProcAddress("glBindTexture"));
4930+
static glTexImage2DProc glTexImage2D = reinterpret_cast<glTexImage2DProc>(
4931+
context.GLGetProcAddress("glTexImage2D"));
4932+
static glTexParameteriProc glTexParameteri =
4933+
reinterpret_cast<glTexParameteriProc>(
4934+
context.GLGetProcAddress("glTexParameteri"));
4935+
static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES =
4936+
reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(
4937+
context.GLGetProcAddress("glEGLImageTargetTexture2DOES"));
4938+
static PFNEGLGETCURRENTDISPLAYPROC eglGetCurrentDisplay =
4939+
reinterpret_cast<PFNEGLGETCURRENTDISPLAYPROC>(
4940+
context.GLGetProcAddress("eglGetCurrentDisplay"));
4941+
static PFNEGLGETCURRENTCONTEXTPROC eglGetCurrentContext =
4942+
reinterpret_cast<PFNEGLGETCURRENTCONTEXTPROC>(
4943+
context.GLGetProcAddress("eglGetCurrentContext"));
4944+
static PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR =
4945+
reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(
4946+
context.GLGetProcAddress("eglCreateImageKHR"));
4947+
4948+
static GLuint gl_texture = 0;
4949+
auto rendered_scene = context.GetNextSceneImage();
4950+
context.GetRendererConfig().open_gl.gl_external_texture_frame_callback =
4951+
[](void* user_data, int64_t texture_id, size_t width, size_t height,
4952+
FlutterOpenGLTexture* texture) -> bool {
4953+
if (gl_texture == 0) {
4954+
std::vector<uint8_t> buffer(kWidth * kHeight * 4);
4955+
for (int i = 0; i < kWidth * kHeight; ++i) {
4956+
bool top = i < kWidth * kHeight / 2;
4957+
buffer[i * 4 + 0] = top ? 255 : 0;
4958+
buffer[i * 4 + 1] = 0;
4959+
buffer[i * 4 + 2] = top ? 0 : 255;
4960+
buffer[i * 4 + 3] = 255;
4961+
}
4962+
GLuint source;
4963+
glGenTextures(1, &source);
4964+
glBindTexture(GL_TEXTURE_2D, source);
4965+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kWidth, kHeight, 0, GL_RGBA,
4966+
GL_UNSIGNED_BYTE, buffer.data());
4967+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
4968+
4969+
// Sample the texture through an EGLImage as an external texture.
4970+
EGLImageKHR image = eglCreateImageKHR(
4971+
eglGetCurrentDisplay(), eglGetCurrentContext(), EGL_GL_TEXTURE_2D_KHR,
4972+
reinterpret_cast<EGLClientBuffer>(static_cast<uintptr_t>(source)),
4973+
nullptr);
4974+
if (image == EGL_NO_IMAGE_KHR) {
4975+
return false;
4976+
}
4977+
glGenTextures(1, &gl_texture);
4978+
glBindTexture(GL_TEXTURE_EXTERNAL_OES, gl_texture);
4979+
glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image);
4980+
}
4981+
texture->target = GL_TEXTURE_EXTERNAL_OES;
4982+
texture->name = gl_texture;
4983+
texture->format = GL_RGBA8;
4984+
texture->destruction_callback = nullptr;
4985+
texture->user_data = nullptr;
4986+
texture->width = width;
4987+
texture->height = height;
4988+
return true;
4989+
};
4990+
4991+
auto engine = builder.LaunchEngine();
4992+
4993+
ASSERT_TRUE(engine.is_valid());
4994+
4995+
flutter::EmbedderEngine* embedder_engine = ToEmbedderEngine(engine.get());
4996+
4997+
constexpr int texture_id = 1;
4998+
ASSERT_TRUE(embedder_engine->RegisterTexture(texture_id));
4999+
5000+
// Send a window metrics events so frames may be scheduled.
5001+
FlutterWindowMetricsEvent event = {};
5002+
event.struct_size = sizeof(event);
5003+
event.width = kWidth;
5004+
event.height = kHeight;
5005+
event.pixel_ratio = 1.0;
5006+
ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event),
5007+
kSuccess);
5008+
latch.Wait();
5009+
ASSERT_TRUE(
5010+
ImageMatchesFixture("external_texture_impeller.png", rendered_scene));
5011+
}
5012+
49035013
TEST_F(EmbedderTest, RenderTextureWithImpellerOpenGLDestructCallback) {
49045014
constexpr int kWidth = 800;
49055015
constexpr int kHeight = 600;

0 commit comments

Comments
 (0)