7#define IMPELLER_PLAYGROUND_SUPPORTS_ANGLE FML_OS_MACOSX
9#if IMPELLER_PLAYGROUND_SUPPORTS_ANGLE
13#define GLFW_INCLUDE_NONE
14#include "third_party/glfw/include/GLFW/glfw3.h"
18#include "impeller/entity/gles/entity_shaders_gles.h"
19#include "impeller/entity/gles/framebuffer_blend_shaders_gles.h"
20#include "impeller/entity/gles/modern_shaders_gles.h"
21#include "impeller/entity/gles3/entity_shaders_gles.h"
22#include "impeller/entity/gles3/framebuffer_blend_shaders_gles.h"
23#include "impeller/entity/gles3/modern_shaders_gles.h"
24#include "impeller/fixtures/gles/fixtures_shaders_gles.h"
25#include "impeller/fixtures/gles/modern_fixtures_shaders_gles.h"
26#include "impeller/fixtures/gles3/fixtures_shaders_gles.h"
27#include "impeller/fixtures/gles3/modern_fixtures_shaders_gles.h"
28#include "impeller/playground/imgui/gles/imgui_shaders_gles.h"
29#include "impeller/playground/imgui/gles3/imgui_shaders_gles.h"
43 auto found = reactions_allowed_.find(std::this_thread::get_id());
44 if (found == reactions_allowed_.end()) {
52 reactions_allowed_[std::this_thread::get_id()] = allowed;
56 mutable RWMutex mutex_;
57 std::map<std::thread::id, bool> reactions_allowed_
IPLR_GUARDED_BY(mutex_);
67 std::shared_ptr<ReactorWorker>
worker,
68 std::shared_ptr<ContextGLES>
context,
77 ::glfwMakeContextCurrent(
window.get());
89 UniqueHandle
window = {
nullptr, &DestroyWindowHandle};
91 std::shared_ptr<ReactorWorker>
worker;
96std::unique_ptr<PlaygroundImplGLES::ShareableContext>
97 PlaygroundImplGLES::shared_context_msaa_;
98std::unique_ptr<PlaygroundImplGLES::ShareableContext>
99 PlaygroundImplGLES::shared_context_sdf_;
102 shared_context_msaa_.reset();
103 shared_context_sdf_.reset();
106void PlaygroundImplGLES::DestroyWindowHandle(WindowHandle handle) {
110 ::glfwDestroyWindow(
reinterpret_cast<GLFWwindow*
>(handle));
113static std::vector<std::shared_ptr<fml::Mapping>>
118 handle_(nullptr, &DestroyWindowHandle),
119 use_angle_(switches.use_angle) {
121#if IMPELLER_PLAYGROUND_SUPPORTS_ANGLE
122 angle_glesv2_ = dlopen(
"libGLESv2.dylib", RTLD_LAZY);
127 std::unique_ptr<PlaygroundImplGLES::ShareableContext>& shared_context =
128 GetShareableContext();
129 if (!shared_context) {
130 shared_context = MakeShareableContext(
switches_);
131 if (!shared_context) {
132 FML_LOG(ERROR) <<
"Could not create GLES context.";
137 context_ = shared_context->context;
142 shared_context->context->GetGPUTracer()->Reset();
145std::unique_ptr<PlaygroundImplGLES::ShareableContext>&
146PlaygroundImplGLES::GetShareableContext() {
149 return unique_context_;
152 std::unique_ptr<PlaygroundImplGLES::ShareableContext>& shared_context =
157 if (shared_context && (shared_context->switches !=
switches_)) {
158 shared_context.reset();
163 return shared_context;
166GLFWwindow* PlaygroundImplGLES::CreateGLWindow(
167 const PlaygroundSwitches& switches,
168 GLFWwindow* share_window) {
169 ::glfwDefaultWindowHints();
172 FML_CHECK(switches.use_angle) <<
"Must use Angle on macOS for OpenGL ES.";
173 ::glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
178 ::glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
180 ::glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
181 ::glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
182 ::glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
183 ::glfwWindowHint(GLFW_RED_BITS, 8);
184 ::glfwWindowHint(GLFW_GREEN_BITS, 8);
185 ::glfwWindowHint(GLFW_BLUE_BITS, 8);
186 ::glfwWindowHint(GLFW_ALPHA_BITS, 8);
187 ::glfwWindowHint(GLFW_DEPTH_BITS, 32);
188 ::glfwWindowHint(GLFW_STENCIL_BITS, 8);
189 ::glfwWindowHint(GLFW_SAMPLES, 4);
193 ::glfwWindowHint(GLFW_CONTEXT_DEBUG,
GLFW_TRUE);
196 auto window = ::glfwCreateWindow(1, 1,
"Test",
nullptr, share_window);
197 ::glfwMakeContextCurrent(
window);
201std::unique_ptr<PlaygroundImplGLES::ShareableContext>
202PlaygroundImplGLES::MakeShareableContext(
const PlaygroundSwitches& switches) {
204 UniqueHandle(CreateGLWindow(switches,
nullptr), &DestroyWindowHandle);
206 FML_LOG(ERROR) <<
"Could not create GLES window.";
211 std::make_unique<ProcTableGLES>(CreateGLProcAddressResolver(switches));
212 if (!gl->IsValid()) {
213 FML_LOG(ERROR) <<
"Proc table when creating a playground was invalid.";
217 if (gl->GetDescription()->HasDebugExtension()) {
218 gl->DebugMessageCallbackKHR(
219 +[](GLenum , GLenum message_type, GLuint ,
220 GLenum , GLsizei ,
const GLchar*
message,
222 switch (message_type) {
223 case GL_DEBUG_TYPE_ERROR_KHR:
233 gl->Enable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR);
236 bool is_gles3 = gl->GetDescription()->GetGlVersion().IsAtLeast(Version(3));
241 FML_LOG(ERROR) <<
"Could not create context.";
245 auto worker = std::make_shared<ReactorWorker>();
246 worker->SetReactionsAllowedOnCurrentThread(
true);
249 auto worker_id = context_gles->AddReactorWorker(worker);
250 if (!worker_id.has_value()) {
251 FML_LOG(ERROR) <<
"Could not add reactor worker.";
255 return std::make_unique<ShareableContext>(std::move(
window), worker,
256 context_gles, switches);
260 if (context_ && handle_) {
261 ::glfwMakeContextCurrent(handle_.get());
262 [[maybe_unused]]
auto result = context_->FlushCommandBuffers();
266static std::vector<std::shared_ptr<fml::Mapping>>
270 std::make_shared<fml::NonOwnedMapping>(
271 impeller_entity_shaders_gles3_data,
272 impeller_entity_shaders_gles3_length),
273 std::make_shared<fml::NonOwnedMapping>(
274 impeller_modern_shaders_gles3_data,
275 impeller_modern_shaders_gles3_length),
276 std::make_shared<fml::NonOwnedMapping>(
277 impeller_framebuffer_blend_shaders_gles3_data,
278 impeller_framebuffer_blend_shaders_gles3_length),
279 std::make_shared<fml::NonOwnedMapping>(
280 impeller_fixtures_shaders_gles3_data,
281 impeller_fixtures_shaders_gles3_length),
282 std::make_shared<fml::NonOwnedMapping>(
283 impeller_modern_fixtures_shaders_gles3_data,
284 impeller_modern_fixtures_shaders_gles3_length),
285 std::make_shared<fml::NonOwnedMapping>(
286 impeller_imgui_shaders_gles3_data,
287 impeller_imgui_shaders_gles3_length),
291 std::make_shared<fml::NonOwnedMapping>(
292 impeller_entity_shaders_gles_data,
293 impeller_entity_shaders_gles_length),
294 std::make_shared<fml::NonOwnedMapping>(
295 impeller_modern_shaders_gles_data,
296 impeller_modern_shaders_gles_length),
297 std::make_shared<fml::NonOwnedMapping>(
298 impeller_framebuffer_blend_shaders_gles_data,
299 impeller_framebuffer_blend_shaders_gles_length),
300 std::make_shared<fml::NonOwnedMapping>(
301 impeller_fixtures_shaders_gles_data,
302 impeller_fixtures_shaders_gles_length),
303 std::make_shared<fml::NonOwnedMapping>(
304 impeller_modern_fixtures_shaders_gles_data,
305 impeller_modern_fixtures_shaders_gles_length),
306 std::make_shared<fml::NonOwnedMapping>(
307 impeller_imgui_shaders_gles_data, impeller_imgui_shaders_gles_length),
312std::shared_ptr<Context> PlaygroundImplGLES::GetContext()
const {
318PlaygroundImplGLES::CreateGLProcAddressResolver()
const {
319 return CreateGLProcAddressResolver(
switches_);
323PlaygroundImplGLES::CreateGLProcAddressResolver(
324 const PlaygroundSwitches& switches) {
325 return switches.use_angle ? [](
const char*
name) ->
void* {
326 void* symbol =
nullptr;
327#if IMPELLER_PLAYGROUND_SUPPORTS_ANGLE
328 void* angle_glesv2 = dlopen(
"libGLESv2.dylib", RTLD_LAZY);
329 symbol = dlsym(angle_glesv2,
name);
334 : [](
const char*
name) ->
void* {
335 return reinterpret_cast<void*
>(::glfwGetProcAddress(
name));
341 return handle_.get();
345std::unique_ptr<Surface> PlaygroundImplGLES::AcquireSurfaceFrame(
346 std::shared_ptr<Context>
context) {
347 auto window =
reinterpret_cast<GLFWwindow*
>(GetWindowHandle());
355 ::glfwSwapBuffers(
window);
367 const std::shared_ptr<Capabilities>& capabilities) {
370 "PlaygroundImplGLES doesn't support setting the capabilities.");
375 std::make_unique<ProcTableGLES>(CreateGLProcAddressResolver());
376 if (!gl->IsValid()) {
377 FML_LOG(ERROR) <<
"Proc table was invalid. Assuming baseline OpenGL ES";
380 bool is_gles3 = gl->GetDescription()->GetGlVersion().IsAtLeast(Version(3));
static std::shared_ptr< ContextGLES > Create(const Flags &flags, std::unique_ptr< ProcTableGLES > gl, const std::vector< std::shared_ptr< fml::Mapping > > &shader_libraries, bool enable_gpu_tracing, std::shared_ptr< fml::BasicTaskRunner > io_task_runner=nullptr)
std::function< void *(const char *proc_name)> GLProcAddressResolver
bool CanReactorReactOnCurrentThreadNow(const ReactorGLES &reactor) const override
Determines the ability of the worker to service a reaction on the current thread. The OpenGL context ...
void SetReactionsAllowedOnCurrentThread(bool allowed)
fml::Status SetCapabilities(const std::shared_ptr< Capabilities > &capabilities) override
PlaygroundImplGLES(const PlaygroundSwitches &switches)
static void OnTearDownTestEnvironment()
const PlaygroundSwitches switches_
A delegate implemented by a thread on which an OpenGL context is current. There may be multiple worke...
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about.
static std::unique_ptr< Surface > WrapFBO(const std::shared_ptr< Context > &context, SwapCallback swap_callback, GLuint fbo, PixelFormat color_format, ISize fbo_size)
std::function< bool(void)> SwapCallback
#define FML_LOG(severity)
#define FML_CHECK(condition)
static std::vector< std::shared_ptr< fml::Mapping > > ShaderLibraryMappingsForPlayground()
std::shared_ptr< ReactorGLES > reactor
std::shared_ptr< ContextGLES > context
bool use_sdfs
Use SDFs for rendering.
const PlaygroundSwitches switches
std::shared_ptr< ContextGLES > context
ShareableContext(UniqueHandle window, std::shared_ptr< ReactorWorker > worker, std::shared_ptr< ContextGLES > context, const PlaygroundSwitches &switches)
std::shared_ptr< ReactorWorker > worker
static constexpr TSize MakeWH(Type width, Type height)
#define IPLR_GUARDED_BY(x)