|
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> |
| 16 | + |
12 | 17 | #include "GLES3/gl3.h" |
| 18 | + |
13 | 19 | #include "flutter/display_list/image/dl_image_skia.h" |
14 | 20 | #include "flutter/flow/raster_cache.h" |
15 | 21 | #include "flutter/fml/file.h" |
@@ -4900,6 +4906,200 @@ TEST_F(EmbedderTest, RenderTextureWithImpellerOpenGL) { |
4900 | 4906 | } |
4901 | 4907 | } |
4902 | 4908 |
|
| 4909 | +TEST_F(EmbedderTest, RenderExternalTextureWithImpellerOpenGL) { |
| 4910 | + constexpr int kWidth = 800; |
| 4911 | + constexpr int kHeight = 600; |
| 4912 | + auto& context = GetEmbedderContext<EmbedderTestContextGL>(); |
| 4913 | + EmbedderConfigBuilder builder(context); |
| 4914 | + fml::AutoResetWaitableEvent latch; |
| 4915 | + context.SetGLPresentCallback( |
| 4916 | + [&](FlutterPresentInfo present_info) { latch.Signal(); }); |
| 4917 | + builder.AddCommandLineArgument("--enable-impeller"); |
| 4918 | + builder.SetDartEntrypoint("render_texture_impeller_test"); |
| 4919 | + builder.SetSurface(DlISize(kWidth, kHeight)); |
| 4920 | + typedef void (*glGenTexturesProc)(GLsizei n, GLuint* textures); |
| 4921 | + typedef void (*glBindTextureProc)(GLenum n, GLuint texture); |
| 4922 | + typedef void (*glTexImage2DProc)(GLenum target, GLint level, |
| 4923 | + GLint internalformat, GLsizei width, |
| 4924 | + GLsizei height, GLint border, GLenum format, |
| 4925 | + GLenum type, const void* pixels); |
| 4926 | + typedef void (*glTexParameteriProc)(GLenum target, GLenum pname, GLint param); |
| 4927 | + typedef void (*glDeleteTexturesProc)(GLsizei n, const GLuint* textures); |
| 4928 | + static glGenTexturesProc glGenTextures = reinterpret_cast<glGenTexturesProc>( |
| 4929 | + context.GLGetProcAddress("glGenTextures")); |
| 4930 | + static glBindTextureProc glBindTexture = reinterpret_cast<glBindTextureProc>( |
| 4931 | + context.GLGetProcAddress("glBindTexture")); |
| 4932 | + static glTexImage2DProc glTexImage2D = reinterpret_cast<glTexImage2DProc>( |
| 4933 | + context.GLGetProcAddress("glTexImage2D")); |
| 4934 | + static glTexParameteriProc glTexParameteri = |
| 4935 | + reinterpret_cast<glTexParameteriProc>( |
| 4936 | + context.GLGetProcAddress("glTexParameteri")); |
| 4937 | + static glDeleteTexturesProc glDeleteTextures = |
| 4938 | + reinterpret_cast<glDeleteTexturesProc>( |
| 4939 | + context.GLGetProcAddress("glDeleteTextures")); |
| 4940 | + static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES = |
| 4941 | + reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>( |
| 4942 | + context.GLGetProcAddress("glEGLImageTargetTexture2DOES")); |
| 4943 | + static PFNEGLGETCURRENTDISPLAYPROC eglGetCurrentDisplay = |
| 4944 | + reinterpret_cast<PFNEGLGETCURRENTDISPLAYPROC>( |
| 4945 | + context.GLGetProcAddress("eglGetCurrentDisplay")); |
| 4946 | + static PFNEGLGETCURRENTCONTEXTPROC eglGetCurrentContext = |
| 4947 | + reinterpret_cast<PFNEGLGETCURRENTCONTEXTPROC>( |
| 4948 | + context.GLGetProcAddress("eglGetCurrentContext")); |
| 4949 | + static PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR = |
| 4950 | + reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>( |
| 4951 | + context.GLGetProcAddress("eglCreateImageKHR")); |
| 4952 | + static PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR = |
| 4953 | + reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>( |
| 4954 | + context.GLGetProcAddress("eglDestroyImageKHR")); |
| 4955 | + if (glEGLImageTargetTexture2DOES == nullptr || eglCreateImageKHR == nullptr || |
| 4956 | + eglDestroyImageKHR == nullptr) { |
| 4957 | + GTEST_SKIP() << "EGL image extensions are not supported"; |
| 4958 | + } |
| 4959 | + |
| 4960 | + auto rendered_scene = context.GetNextSceneImage(); |
| 4961 | + context.GetRendererConfig().open_gl.gl_external_texture_frame_callback = |
| 4962 | + [](void* user_data, int64_t texture_id, size_t width, size_t height, |
| 4963 | + FlutterOpenGLTexture* texture) -> bool { |
| 4964 | + std::vector<uint8_t> buffer(kWidth * kHeight * 4); |
| 4965 | + for (int i = 0; i < kWidth * kHeight; ++i) { |
| 4966 | + bool top = i < kWidth * kHeight / 2; |
| 4967 | + buffer[i * 4 + 0] = top ? 255 : 0; |
| 4968 | + buffer[i * 4 + 1] = 0; |
| 4969 | + buffer[i * 4 + 2] = top ? 0 : 255; |
| 4970 | + buffer[i * 4 + 3] = 255; |
| 4971 | + } |
| 4972 | + GLuint source; |
| 4973 | + glGenTextures(1, &source); |
| 4974 | + glBindTexture(GL_TEXTURE_2D, source); |
| 4975 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kWidth, kHeight, 0, GL_RGBA, |
| 4976 | + GL_UNSIGNED_BYTE, buffer.data()); |
| 4977 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 4978 | + |
| 4979 | + // Sample the texture through an EGLImage as an external texture. The |
| 4980 | + // external texture keeps the image contents alive after the image and |
| 4981 | + // source texture are deleted. |
| 4982 | + EGLDisplay display = eglGetCurrentDisplay(); |
| 4983 | + EGLImageKHR image = eglCreateImageKHR( |
| 4984 | + display, eglGetCurrentContext(), EGL_GL_TEXTURE_2D_KHR, |
| 4985 | + reinterpret_cast<EGLClientBuffer>(static_cast<uintptr_t>(source)), |
| 4986 | + nullptr); |
| 4987 | + if (image == EGL_NO_IMAGE_KHR) { |
| 4988 | + glDeleteTextures(1, &source); |
| 4989 | + return false; |
| 4990 | + } |
| 4991 | + GLuint gl_texture; |
| 4992 | + glGenTextures(1, &gl_texture); |
| 4993 | + glBindTexture(GL_TEXTURE_EXTERNAL_OES, gl_texture); |
| 4994 | + glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image); |
| 4995 | + eglDestroyImageKHR(display, image); |
| 4996 | + glDeleteTextures(1, &source); |
| 4997 | + |
| 4998 | + texture->target = GL_TEXTURE_EXTERNAL_OES; |
| 4999 | + texture->name = gl_texture; |
| 5000 | + texture->format = GL_RGBA8; |
| 5001 | + texture->destruction_callback = [](void* user_data) { |
| 5002 | + GLuint gl_texture = |
| 5003 | + static_cast<GLuint>(reinterpret_cast<uintptr_t>(user_data)); |
| 5004 | + glDeleteTextures(1, &gl_texture); |
| 5005 | + }; |
| 5006 | + texture->user_data = reinterpret_cast<void*>(gl_texture); |
| 5007 | + texture->width = width; |
| 5008 | + texture->height = height; |
| 5009 | + return true; |
| 5010 | + }; |
| 5011 | + |
| 5012 | + auto engine = builder.LaunchEngine(); |
| 5013 | + |
| 5014 | + ASSERT_TRUE(engine.is_valid()); |
| 5015 | + |
| 5016 | + flutter::EmbedderEngine* embedder_engine = ToEmbedderEngine(engine.get()); |
| 5017 | + |
| 5018 | + constexpr int texture_id = 1; |
| 5019 | + ASSERT_TRUE(embedder_engine->RegisterTexture(texture_id)); |
| 5020 | + |
| 5021 | + // Send a window metrics events so frames may be scheduled. |
| 5022 | + FlutterWindowMetricsEvent event = {}; |
| 5023 | + event.struct_size = sizeof(event); |
| 5024 | + event.width = kWidth; |
| 5025 | + event.height = kHeight; |
| 5026 | + event.pixel_ratio = 1.0; |
| 5027 | + ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event), |
| 5028 | + kSuccess); |
| 5029 | + latch.Wait(); |
| 5030 | + ASSERT_TRUE( |
| 5031 | + ImageMatchesFixture("external_texture_impeller.png", rendered_scene)); |
| 5032 | +} |
| 5033 | + |
| 5034 | +TEST_F(EmbedderTest, RenderUnsupportedTextureTargetWithImpellerOpenGL) { |
| 5035 | + constexpr int kWidth = 800; |
| 5036 | + constexpr int kHeight = 600; |
| 5037 | + auto& context = GetEmbedderContext<EmbedderTestContextGL>(); |
| 5038 | + EmbedderConfigBuilder builder(context); |
| 5039 | + fml::AutoResetWaitableEvent latch; |
| 5040 | + context.SetGLPresentCallback( |
| 5041 | + [&](FlutterPresentInfo present_info) { latch.Signal(); }); |
| 5042 | + builder.AddCommandLineArgument("--enable-impeller"); |
| 5043 | + builder.SetDartEntrypoint("render_texture_impeller_test"); |
| 5044 | + builder.SetSurface(DlISize(kWidth, kHeight)); |
| 5045 | + |
| 5046 | + static std::atomic<bool> callback_called; |
| 5047 | + callback_called = false; |
| 5048 | + context.GetRendererConfig().open_gl.gl_external_texture_frame_callback = |
| 5049 | + [](void* user_data, int64_t texture_id, size_t width, size_t height, |
| 5050 | + FlutterOpenGLTexture* texture) -> bool { |
| 5051 | + callback_called = true; |
| 5052 | + |
| 5053 | + // The target is rejected before the texture is used, so no GL texture is |
| 5054 | + // needed. |
| 5055 | + texture->target = GL_TEXTURE_CUBE_MAP; |
| 5056 | + texture->format = GL_RGBA8; |
| 5057 | + texture->width = width; |
| 5058 | + texture->height = height; |
| 5059 | + return true; |
| 5060 | + }; |
| 5061 | + |
| 5062 | + auto engine = builder.LaunchEngine(); |
| 5063 | + |
| 5064 | + ASSERT_TRUE(engine.is_valid()); |
| 5065 | + |
| 5066 | + flutter::EmbedderEngine* embedder_engine = ToEmbedderEngine(engine.get()); |
| 5067 | + |
| 5068 | + constexpr int texture_id = 1; |
| 5069 | + ASSERT_TRUE(embedder_engine->RegisterTexture(texture_id)); |
| 5070 | + |
| 5071 | + // The texture is resolved on the raster thread, so capture its log there. |
| 5072 | + fml::RefPtr<fml::TaskRunner> raster_task_runner = |
| 5073 | + embedder_engine->GetShell().GetTaskRunners().GetRasterTaskRunner(); |
| 5074 | + std::ostringstream log; |
| 5075 | + fml::AutoResetWaitableEvent capture_latch; |
| 5076 | + raster_task_runner->PostTask([&] { |
| 5077 | + fml::LogMessage::CaptureNextLog(&log); |
| 5078 | + capture_latch.Signal(); |
| 5079 | + }); |
| 5080 | + capture_latch.Wait(); |
| 5081 | + |
| 5082 | + // Send a window metrics events so frames may be scheduled. |
| 5083 | + FlutterWindowMetricsEvent event = {}; |
| 5084 | + event.struct_size = sizeof(event); |
| 5085 | + event.width = kWidth; |
| 5086 | + event.height = kHeight; |
| 5087 | + event.pixel_ratio = 1.0; |
| 5088 | + ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event), |
| 5089 | + kSuccess); |
| 5090 | + latch.Wait(); |
| 5091 | + EXPECT_TRUE(callback_called); |
| 5092 | + |
| 5093 | + raster_task_runner->PostTask([&] { |
| 5094 | + fml::LogMessage::CaptureNextLog(nullptr); |
| 5095 | + capture_latch.Signal(); |
| 5096 | + }); |
| 5097 | + capture_latch.Wait(); |
| 5098 | + EXPECT_NE(log.str().find("Unsupported texture target " + |
| 5099 | + std::to_string(GL_TEXTURE_CUBE_MAP)), |
| 5100 | + std::string::npos); |
| 5101 | +} |
| 5102 | + |
4903 | 5103 | TEST_F(EmbedderTest, RenderTextureWithImpellerOpenGLDestructCallback) { |
4904 | 5104 | constexpr int kWidth = 800; |
4905 | 5105 | constexpr int kHeight = 600; |
|
0 commit comments