Flutter Engine
egl_utils.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "egl_utils.h"
6 
7 #include <EGL/egl.h>
8 
9 // Converts an EGL decimal value to a string.
10 static gchar* egl_decimal_to_string(EGLint value) {
11  return g_strdup_printf("%d", value);
12 }
13 
14 // Converts an EGL hexadecimal value to a string.
15 static gchar* egl_hexadecimal_to_string(EGLint value) {
16  return g_strdup_printf("0x%x", value);
17 }
18 
19 // Converts an EGL enumerated value to a string.
20 static gchar* egl_enum_to_string(EGLint value) {
21  if (value == EGL_FALSE)
22  return g_strdup("EGL_FALSE");
23  else if (value == EGL_LUMINANCE_BUFFER)
24  return g_strdup("EGL_LUMINANCE_BUFFER");
25  else if (value == EGL_NONE)
26  return g_strdup("EGL_NONE");
27  else if (value == EGL_NON_CONFORMANT_CONFIG)
28  return g_strdup("EGL_NON_CONFORMANT_CONFIG");
29  else if (value == EGL_RGB_BUFFER)
30  return g_strdup("EGL_RGB_BUFFER");
31  else if (value == EGL_SLOW_CONFIG)
32  return g_strdup("EGL_SLOW_CONFIG");
33  else if (value == EGL_TRANSPARENT_RGB)
34  return g_strdup("EGL_TRANSPARENT_RGB");
35  else if (value == EGL_TRUE)
36  return g_strdup("EGL_TRUE");
37  else
38  return nullptr;
39 }
40 
41 // Ensures the given bit is not set in a bitfield. Returns TRUE if that bit was
42 // cleared.
43 static gboolean clear_bit(EGLint* field, EGLint bit) {
44  if ((*field & bit) == 0)
45  return FALSE;
46 
47  *field ^= bit;
48  return TRUE;
49 }
50 
51 // Converts an EGL renderable type bitfield to a string.
52 static gchar* egl_renderable_type_to_string(EGLint value) {
53  EGLint v = value;
54  g_autoptr(GPtrArray) strings = g_ptr_array_new_with_free_func(g_free);
55  if (clear_bit(&v, EGL_OPENGL_ES_BIT))
56  g_ptr_array_add(strings, g_strdup("EGL_OPENGL_ES_BIT"));
57  if (clear_bit(&v, EGL_OPENVG_BIT))
58  g_ptr_array_add(strings, g_strdup("EGL_OPENVG_BIT"));
59  if (clear_bit(&v, EGL_OPENGL_ES2_BIT))
60  g_ptr_array_add(strings, g_strdup("EGL_OPENGL_ES2_BIT"));
61  if (clear_bit(&v, EGL_OPENGL_BIT))
62  g_ptr_array_add(strings, g_strdup("EGL_OPENGL_BIT"));
63  if (clear_bit(&v, EGL_OPENGL_ES3_BIT))
64  g_ptr_array_add(strings, g_strdup("EGL_OPENGL_ES3_BIT"));
65  if (v != 0)
66  g_ptr_array_add(strings, egl_hexadecimal_to_string(v));
67  g_ptr_array_add(strings, nullptr);
68 
69  return g_strjoinv("|", reinterpret_cast<gchar**>(strings->pdata));
70 }
71 
72 // Converts an EGL surface type bitfield to a string.
73 static gchar* egl_surface_type_to_string(EGLint value) {
74  EGLint v = value;
75  g_autoptr(GPtrArray) strings = g_ptr_array_new_with_free_func(g_free);
76  if (clear_bit(&v, EGL_PBUFFER_BIT))
77  g_ptr_array_add(strings, g_strdup("EGL_PBUFFER_BIT"));
78  if (clear_bit(&v, EGL_PIXMAP_BIT))
79  g_ptr_array_add(strings, g_strdup("EGL_PIXMAP_BIT"));
80  if (clear_bit(&v, EGL_WINDOW_BIT))
81  g_ptr_array_add(strings, g_strdup("EGL_WINDOW_BIT"));
82  if (v != 0)
83  g_ptr_array_add(strings, egl_hexadecimal_to_string(v));
84  g_ptr_array_add(strings, nullptr);
85 
86  return g_strjoinv("|", reinterpret_cast<gchar**>(strings->pdata));
87 }
88 
89 const gchar* egl_error_to_string(EGLint error) {
90  switch (error) {
91  case EGL_SUCCESS:
92  return "Success";
93  case EGL_NOT_INITIALIZED:
94  return "Not Initialized";
95  case EGL_BAD_ACCESS:
96  return "Bad Access";
97  case EGL_BAD_ALLOC:
98  return "Bad Allocation";
99  case EGL_BAD_ATTRIBUTE:
100  return "Bad Attribute";
101  case EGL_BAD_CONTEXT:
102  return "Bad Context";
103  case EGL_BAD_CONFIG:
104  return "Bad Configuration";
105  case EGL_BAD_CURRENT_SURFACE:
106  return "Bad Current Surface";
107  case EGL_BAD_DISPLAY:
108  return "Bad Display";
109  case EGL_BAD_SURFACE:
110  return "Bad Surface";
111  case EGL_BAD_MATCH:
112  return "Bad Match";
113  case EGL_BAD_PARAMETER:
114  return "Bad Parameter";
115  case EGL_BAD_NATIVE_PIXMAP:
116  return "Bad Native Pixmap";
117  case EGL_BAD_NATIVE_WINDOW:
118  return "Bad Native Window";
119  case EGL_CONTEXT_LOST:
120  return "Context Lost";
121  default:
122  return "Unknown Error";
123  }
124 }
125 
126 gchar* egl_config_to_string(EGLDisplay display, EGLConfig config) {
127  struct {
128  EGLint attribute;
129  const gchar* name;
130  gchar* (*to_string)(EGLint value);
131  } config_items[] = {{
132  EGL_CONFIG_ID,
133  "EGL_CONFIG_ID",
135  },
136  {
137  EGL_BUFFER_SIZE,
138  "EGL_BUFFER_SIZE",
140  },
141  {
142  EGL_COLOR_BUFFER_TYPE,
143  "EGL_COLOR_BUFFER_TYPE",
145  },
146  {
147  EGL_TRANSPARENT_TYPE,
148  "EGL_TRANSPARENT_TYPE",
150  },
151  {
152  EGL_LEVEL,
153  "EGL_LEVEL",
155  },
156  {
157  EGL_RED_SIZE,
158  "EGL_RED_SIZE",
160  },
161  {
162  EGL_GREEN_SIZE,
163  "EGL_GREEN_SIZE",
165  },
166  {
167  EGL_BLUE_SIZE,
168  "EGL_BLUE_SIZE",
170  },
171  {
172  EGL_ALPHA_SIZE,
173  "EGL_ALPHA_SIZE",
175  },
176  {
177  EGL_DEPTH_SIZE,
178  "EGL_DEPTH_SIZE",
180  },
181  {
182  EGL_STENCIL_SIZE,
183  "EGL_STENCIL_SIZE",
185  },
186  {
187  EGL_SAMPLES,
188  "EGL_SAMPLES",
190  },
191  {
192  EGL_SAMPLE_BUFFERS,
193  "EGL_SAMPLE_BUFFERS",
195  },
196  {
197  EGL_NATIVE_VISUAL_ID,
198  "EGL_NATIVE_VISUAL_ID",
200  },
201  {
202  EGL_NATIVE_VISUAL_TYPE,
203  "EGL_NATIVE_VISUAL_TYPE",
205  },
206  {
207  EGL_NATIVE_RENDERABLE,
208  "EGL_NATIVE_RENDERABLE",
210  },
211  {
212  EGL_CONFIG_CAVEAT,
213  "EGL_CONFIG_CAVEAT",
215  },
216  {
217  EGL_BIND_TO_TEXTURE_RGB,
218  "EGL_BIND_TO_TEXTURE_RGB",
220  },
221  {
222  EGL_BIND_TO_TEXTURE_RGBA,
223  "EGL_BIND_TO_TEXTURE_RGBA",
225  },
226  {
227  EGL_RENDERABLE_TYPE,
228  "EGL_RENDERABLE_TYPE",
230  },
231  {
232  EGL_CONFORMANT,
233  "EGL_CONFORMANT",
235  },
236  {
237  EGL_SURFACE_TYPE,
238  "EGL_SURFACE_TYPE",
240  },
241  {
242  EGL_MAX_PBUFFER_WIDTH,
243  "EGL_MAX_PBUFFER_WIDTH",
245  },
246  {
247  EGL_MAX_PBUFFER_HEIGHT,
248  "EGL_MAX_PBUFFER_HEIGHT",
250  },
251  {
252  EGL_MAX_PBUFFER_PIXELS,
253  "EGL_MAX_PBUFFER_PIXELS",
255  },
256  {
257  EGL_MIN_SWAP_INTERVAL,
258  "EGL_MIN_SWAP_INTERVAL",
260  },
261  {
262  EGL_MAX_SWAP_INTERVAL,
263  "EGL_MAX_SWAP_INTERVAL",
265  },
266  {EGL_NONE, nullptr, nullptr}};
267 
268  g_autoptr(GPtrArray) strings = g_ptr_array_new_with_free_func(g_free);
269  for (int i = 0; config_items[i].attribute != EGL_NONE; i++) {
270  EGLint value;
271  if (!eglGetConfigAttrib(display, config, config_items[i].attribute, &value))
272  continue;
273  g_autofree gchar* value_string = config_items[i].to_string(value);
274  if (value_string == nullptr)
275  value_string = egl_hexadecimal_to_string(value);
276  g_ptr_array_add(
277  strings, g_strdup_printf("%s=%s", config_items[i].name, value_string));
278  }
279  g_ptr_array_add(strings, nullptr);
280 
281  return g_strjoinv(" ", reinterpret_cast<gchar**>(strings->pdata));
282 }
static gchar * egl_enum_to_string(EGLint value)
Definition: egl_utils.cc:20
EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
Definition: mock_egl.cc:153
FlMethodResponse GError ** error
static gchar * egl_decimal_to_string(EGLint value)
Definition: egl_utils.cc:10
gchar * egl_config_to_string(EGLDisplay display, EGLConfig config)
Definition: egl_utils.cc:126
uint8_t value
static gchar * egl_hexadecimal_to_string(EGLint value)
Definition: egl_utils.cc:15
const gchar * egl_error_to_string(EGLint error)
Definition: egl_utils.cc:89
static gchar * egl_renderable_type_to_string(EGLint value)
Definition: egl_utils.cc:52
const char * name
Definition: fuchsia.cc:50
return TRUE
Definition: fl_view.cc:107
static gchar * egl_surface_type_to_string(EGLint value)
Definition: egl_utils.cc:73
static gboolean clear_bit(EGLint *field, EGLint bit)
Definition: egl_utils.cc:43