|
9 | 9 | #include <string> |
10 | 10 | #include <vector> |
11 | 11 |
|
| 12 | +#include "EGL/egl.h" |
| 13 | +#include "EGL/eglext.h" |
| 14 | +#include "GLES2/gl2.h" |
| 15 | +#include "GLES2/gl2ext.h" |
12 | 16 | #include "GLES3/gl3.h" |
| 17 | + |
13 | 18 | #include "flutter/display_list/image/dl_image_skia.h" |
14 | 19 | #include "flutter/flow/raster_cache.h" |
15 | 20 | #include "flutter/fml/file.h" |
@@ -4900,6 +4905,111 @@ TEST_F(EmbedderTest, RenderTextureWithImpellerOpenGL) { |
4900 | 4905 | } |
4901 | 4906 | } |
4902 | 4907 |
|
| 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 | + |
4903 | 5013 | TEST_F(EmbedderTest, RenderTextureWithImpellerOpenGLDestructCallback) { |
4904 | 5014 | constexpr int kWidth = 800; |
4905 | 5015 | constexpr int kHeight = 600; |
|
0 commit comments