Flutter Engine
FlutterEmbedderGLFW.cc File Reference
#include <embedder.h>
#include <glfw3.h>
#include <cassert>
#include <chrono>
#include <iostream>

Go to the source code of this file.

Functions

void GLFWcursorPositionCallbackAtPhase (GLFWwindow *window, FlutterPointerPhase phase, double x, double y)
 
void GLFWcursorPositionCallback (GLFWwindow *window, double x, double y)
 
void GLFWmouseButtonCallback (GLFWwindow *window, int key, int action, int mods)
 
static void GLFWKeyCallback (GLFWwindow *window, int key, int scancode, int action, int mods)
 
void GLFWwindowSizeCallback (GLFWwindow *window, int width, int height)
 
bool RunFlutter (GLFWwindow *window, const std::string &project_path, const std::string &icudtl_path)
 
void printUsage ()
 
void GLFW_ErrorCallback (int error, const char *description)
 
int main (int argc, const char *argv[])
 

Variables

static double g_pixelRatio = 1.0
 
static const size_t kInitialWindowWidth = 800
 
static const size_t kInitialWindowHeight = 600
 

Function Documentation

◆ GLFW_ErrorCallback()

void GLFW_ErrorCallback ( int  error,
const char *  description 
)

Definition at line 132 of file FlutterEmbedderGLFW.cc.

Referenced by main().

132  {
133  std::cout << "GLFW Error: (" << error << ") " << description << std::endl;
134 }
FlMethodResponse GError ** error

◆ GLFWcursorPositionCallback()

void GLFWcursorPositionCallback ( GLFWwindow *  window,
double  x,
double  y 
)

Definition at line 41 of file FlutterEmbedderGLFW.cc.

References GLFWcursorPositionCallbackAtPhase(), and kMove.

Referenced by GLFWmouseButtonCallback().

41  {
43 }
void GLFWcursorPositionCallbackAtPhase(GLFWwindow *window, FlutterPointerPhase phase, double x, double y)

◆ GLFWcursorPositionCallbackAtPhase()

void GLFWcursorPositionCallbackAtPhase ( GLFWwindow *  window,
FlutterPointerPhase  phase,
double  x,
double  y 
)

Definition at line 23 of file FlutterEmbedderGLFW.cc.

References event, FlutterEngineSendPointerEvent(), g_pixelRatio, phase, and FlutterPointerEvent::struct_size.

Referenced by GLFWcursorPositionCallback(), and GLFWmouseButtonCallback().

26  {
27  FlutterPointerEvent event = {};
28  event.struct_size = sizeof(event);
29  event.phase = phase;
30  event.x = x * g_pixelRatio;
31  event.y = y * g_pixelRatio;
32  event.timestamp =
33  std::chrono::duration_cast<std::chrono::microseconds>(
34  std::chrono::high_resolution_clock::now().time_since_epoch())
35  .count();
37  reinterpret_cast<FlutterEngine>(glfwGetWindowUserPointer(window)), &event,
38  1);
39 }
static double g_pixelRatio
size_t struct_size
The size of this struct. Must be sizeof(FlutterPointerEvent).
Definition: embedder.h:532
FlutterPointerPhase phase
Definition: fl_view.cc:78
GdkEventButton * event
Definition: fl_view.cc:62
FlutterEngineResult FlutterEngineSendPointerEvent(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPointerEvent *pointers, size_t events_count)
Definition: embedder.cc:1323

◆ GLFWKeyCallback()

static void GLFWKeyCallback ( GLFWwindow *  window,
int  key,
int  scancode,
int  action,
int  mods 
)
static

Definition at line 64 of file FlutterEmbedderGLFW.cc.

References GLFW_TRUE.

Referenced by main().

68  {
69  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
70  glfwSetWindowShouldClose(window, GLFW_TRUE);
71  }
72 }
#define GLFW_TRUE
Definition: flutter_glfw.cc:30
SemanticsAction action

◆ GLFWmouseButtonCallback()

void GLFWmouseButtonCallback ( GLFWwindow *  window,
int  key,
int  action,
int  mods 
)

Definition at line 45 of file FlutterEmbedderGLFW.cc.

References GLFWcursorPositionCallback(), GLFWcursorPositionCallbackAtPhase(), kDown, and kUp.

Referenced by main().

48  {
49  if (key == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
50  double x, y;
51  glfwGetCursorPos(window, &x, &y);
53  glfwSetCursorPosCallback(window, GLFWcursorPositionCallback);
54  }
55 
56  if (key == GLFW_MOUSE_BUTTON_1 && action == GLFW_RELEASE) {
57  double x, y;
58  glfwGetCursorPos(window, &x, &y);
60  glfwSetCursorPosCallback(window, nullptr);
61  }
62 }
Definition: embedder.h:479
SemanticsAction action
void GLFWcursorPositionCallbackAtPhase(GLFWwindow *window, FlutterPointerPhase phase, double x, double y)
void GLFWcursorPositionCallback(GLFWwindow *window, double x, double y)

◆ GLFWwindowSizeCallback()

void GLFWwindowSizeCallback ( GLFWwindow *  window,
int  width,
int  height 
)

Definition at line 74 of file FlutterEmbedderGLFW.cc.

References event, FlutterEngineSendWindowMetricsEvent(), g_pixelRatio, and FlutterWindowMetricsEvent::struct_size.

Referenced by main(), and RunFlutter().

74  {
75  FlutterWindowMetricsEvent event = {};
76  event.struct_size = sizeof(event);
77  event.width = width * g_pixelRatio;
78  event.height = height * g_pixelRatio;
79  event.pixel_ratio = g_pixelRatio;
81  reinterpret_cast<FlutterEngine>(glfwGetWindowUserPointer(window)),
82  &event);
83 }
static double g_pixelRatio
int32_t height
int32_t width
size_t struct_size
The size of this struct. Must be sizeof(FlutterWindowMetricsEvent).
Definition: embedder.h:457
GdkEventButton * event
Definition: fl_view.cc:62
FlutterEngineResult FlutterEngineSendWindowMetricsEvent(FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterWindowMetricsEvent *flutter_metrics)
Definition: embedder.cc:1229

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Definition at line 136 of file FlutterEmbedderGLFW.cc.

References g_pixelRatio, GLFW_ErrorCallback(), GLFW_TRUE, GLFWKeyCallback(), GLFWmouseButtonCallback(), GLFWwindowSizeCallback(), kInitialWindowHeight, kInitialWindowWidth, printUsage(), and RunFlutter().

136  {
137  if (argc != 3) {
138  printUsage();
139  return 1;
140  }
141 
142  std::string project_path = argv[1];
143  std::string icudtl_path = argv[2];
144 
145  glfwSetErrorCallback(GLFW_ErrorCallback);
146 
147  int result = glfwInit();
148  assert(result == GLFW_TRUE);
149 
150  GLFWwindow* window = glfwCreateWindow(
151  kInitialWindowWidth, kInitialWindowHeight, "Flutter", NULL, NULL);
152  assert(window != nullptr);
153 
154  int framebuffer_width, framebuffer_height;
155  glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
156  g_pixelRatio = framebuffer_width / kInitialWindowWidth;
157 
158  bool runResult = RunFlutter(window, project_path, icudtl_path);
159  assert(runResult);
160 
161  glfwSetKeyCallback(window, GLFWKeyCallback);
162  glfwSetWindowSizeCallback(window, GLFWwindowSizeCallback);
163  glfwSetMouseButtonCallback(window, GLFWmouseButtonCallback);
164 
165  while (!glfwWindowShouldClose(window)) {
166  std::cout << "Looping..." << std::endl;
167  glfwWaitEvents();
168  }
169 
170  glfwDestroyWindow(window);
171  glfwTerminate();
172 
173  return EXIT_SUCCESS;
174 }
#define GLFW_TRUE
Definition: flutter_glfw.cc:30
void GLFWmouseButtonCallback(GLFWwindow *window, int key, int action, int mods)
static double g_pixelRatio
void GLFWwindowSizeCallback(GLFWwindow *window, int width, int height)
static void GLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
static const size_t kInitialWindowHeight
void GLFW_ErrorCallback(int error, const char *description)
static const size_t kInitialWindowWidth
void printUsage()
bool RunFlutter(GLFWwindow *window, const std::string &project_path, const std::string &icudtl_path)

◆ printUsage()

void printUsage ( )

Definition at line 127 of file FlutterEmbedderGLFW.cc.

Referenced by main().

127  {
128  std::cout << "usage: flutter_glfw <path to project> <path to icudtl.dat>"
129  << std::endl;
130 }

◆ RunFlutter()

bool RunFlutter ( GLFWwindow *  window,
const std::string &  project_path,
const std::string &  icudtl_path 
)

Definition at line 85 of file FlutterEmbedderGLFW.cc.

References args, FlutterOpenGLRendererConfig::clear_current, engine, FlutterOpenGLRendererConfig::fbo_callback, FLUTTER_ENGINE_VERSION, FlutterEngineRun(), GLFWwindowSizeCallback(), kInitialWindowHeight, kInitialWindowWidth, kOpenGL, kSuccess, FlutterOpenGLRendererConfig::make_current, FlutterRendererConfig::open_gl, FlutterOpenGLRendererConfig::present, FlutterOpenGLRendererConfig::struct_size, and FlutterRendererConfig::type.

Referenced by main().

87  {
88  FlutterRendererConfig config = {};
89  config.type = kOpenGL;
90  config.open_gl.struct_size = sizeof(config.open_gl);
91  config.open_gl.make_current = [](void* userdata) -> bool {
92  glfwMakeContextCurrent((GLFWwindow*)userdata);
93  return true;
94  };
95  config.open_gl.clear_current = [](void*) -> bool {
96  glfwMakeContextCurrent(nullptr); // is this even a thing?
97  return true;
98  };
99  config.open_gl.present = [](void* userdata) -> bool {
100  glfwSwapBuffers((GLFWwindow*)userdata);
101  return true;
102  };
103  config.open_gl.fbo_callback = [](void*) -> uint32_t {
104  return 0; // FBO0
105  };
106 
107  // This directory is generated by `flutter build bundle`.
108  std::string assets_path = project_path + "/build/flutter_assets";
110  .struct_size = sizeof(FlutterProjectArgs),
111  .assets_path = assets_path.c_str(),
112  .icu_data_path =
113  icudtl_path.c_str(), // Find this in your bin/cache directory.
114  };
115  FlutterEngine engine = nullptr;
116  FlutterEngineResult result =
117  FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, // renderer
118  &args, window, &engine);
119  assert(result == kSuccess && engine != nullptr);
120 
121  glfwSetWindowUserPointer(window, engine);
123 
124  return true;
125 }
G_BEGIN_DECLS FlValue * args
void GLFWwindowSizeCallback(GLFWwindow *window, int width, int height)
#define FLUTTER_ENGINE_VERSION
Definition: embedder.h:63
FlutterRendererType type
Definition: embedder.h:448
static const size_t kInitialWindowHeight
FlutterEngineResult FlutterEngineRun(size_t version, const FlutterRendererConfig *config, const FlutterProjectArgs *args, void *user_data, FLUTTER_API_SYMBOL(FlutterEngine) *engine_out)
Initialize and run a Flutter engine instance and return a handle to it. This is a convenience method ...
Definition: embedder.cc:718
size_t struct_size
The size of this struct. Must be sizeof(FlutterOpenGLRendererConfig).
Definition: embedder.h:379
BoolCallback clear_current
Definition: embedder.h:381
static const size_t kInitialWindowWidth
BoolCallback make_current
Definition: embedder.h:380
FlutterOpenGLRendererConfig open_gl
Definition: embedder.h:450
FlutterEngineResult
Definition: embedder.h:65
UIntCallback fbo_callback
Definition: embedder.h:391

Variable Documentation

◆ g_pixelRatio

double g_pixelRatio = 1.0
static

◆ kInitialWindowHeight

const size_t kInitialWindowHeight = 600
static

Definition at line 15 of file FlutterEmbedderGLFW.cc.

Referenced by main(), and RunFlutter().

◆ kInitialWindowWidth

const size_t kInitialWindowWidth = 800
static

Definition at line 14 of file FlutterEmbedderGLFW.cc.

Referenced by main(), and RunFlutter().