Flutter Engine
mock_egl.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/egl.h>
6 
7 typedef struct {
8  EGLint config_id;
9  EGLint buffer_size;
12  EGLint level;
13  EGLint red_size;
14  EGLint green_size;
15  EGLint blue_size;
16  EGLint alpha_size;
17  EGLint depth_size;
18  EGLint stencil_size;
19  EGLint samples;
24  EGLint config_caveat;
28  EGLint conformant;
29  EGLint surface_type;
35 } MockConfig;
36 
37 typedef struct {
38 } MockDisplay;
39 
40 typedef struct {
41 } MockContext;
42 
43 typedef struct {
44 } MockSurface;
45 
46 static bool display_initialized = false;
51 
52 static EGLint mock_error = EGL_SUCCESS;
53 
54 static bool check_display(EGLDisplay dpy) {
55  if (dpy == nullptr) {
56  mock_error = EGL_BAD_DISPLAY;
57  return false;
58  }
59 
60  return true;
61 }
62 
63 static bool check_initialized(EGLDisplay dpy) {
64  if (!display_initialized) {
65  mock_error = EGL_NOT_INITIALIZED;
66  return false;
67  }
68 
69  return true;
70 }
71 
72 static bool check_config(EGLConfig config) {
73  if (config == nullptr) {
74  mock_error = EGL_BAD_CONFIG;
75  return false;
76  }
77 
78  return true;
79 }
80 
81 static EGLBoolean bool_success() {
82  mock_error = EGL_SUCCESS;
83  return EGL_TRUE;
84 }
85 
86 static EGLBoolean bool_failure(EGLint error) {
87  mock_error = error;
88  return EGL_FALSE;
89 }
90 
91 EGLBoolean eglBindAPI(EGLenum api) {
92  return bool_success();
93 }
94 
95 EGLBoolean eglChooseConfig(EGLDisplay dpy,
96  const EGLint* attrib_list,
97  EGLConfig* configs,
98  EGLint config_size,
99  EGLint* num_config) {
100  if (!check_display(dpy) || !check_initialized(dpy))
101  return EGL_FALSE;
102 
103  if (configs == nullptr) {
104  if (num_config != nullptr)
105  *num_config = 1;
106  return bool_success();
107  }
108 
109  EGLint n_returned = 0;
110  if (config_size >= 1) {
111  configs[0] = &mock_config;
112  n_returned++;
113  }
114 
115  if (num_config != nullptr)
116  *num_config = n_returned;
117 
118  return bool_success();
119 }
120 
121 EGLContext eglCreateContext(EGLDisplay dpy,
122  EGLConfig config,
123  EGLContext share_context,
124  const EGLint* attrib_list) {
125  if (!check_display(dpy) || !check_initialized(dpy) || !check_config(config))
126  return EGL_NO_CONTEXT;
127 
128  mock_error = EGL_SUCCESS;
129  return &mock_context;
130 }
131 
132 EGLSurface eglCreatePbufferSurface(EGLDisplay dpy,
133  EGLConfig config,
134  const EGLint* attrib_list) {
135  if (!check_display(dpy) || !check_initialized(dpy) || !check_config(config))
136  return EGL_NO_SURFACE;
137 
138  mock_error = EGL_SUCCESS;
139  return &mock_surface;
140 }
141 
142 EGLSurface eglCreateWindowSurface(EGLDisplay dpy,
143  EGLConfig config,
144  EGLNativeWindowType win,
145  const EGLint* attrib_list) {
146  if (!check_display(dpy) || !check_initialized(dpy) || !check_config(config))
147  return EGL_NO_SURFACE;
148 
149  mock_error = EGL_SUCCESS;
150  return &mock_surface;
151 }
152 
153 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy,
154  EGLConfig config,
155  EGLint attribute,
156  EGLint* value) {
157  if (!check_display(dpy) || !check_initialized(dpy) || !check_config(config))
158  return EGL_FALSE;
159 
160  MockConfig* c = static_cast<MockConfig*>(config);
161  switch (attribute) {
162  case EGL_CONFIG_ID:
163  *value = c->config_id;
164  return bool_success();
165  case EGL_BUFFER_SIZE:
166  *value = c->buffer_size;
167  return bool_success();
168  case EGL_COLOR_BUFFER_TYPE:
169  *value = c->color_buffer_type;
170  return bool_success();
171  case EGL_TRANSPARENT_TYPE:
172  *value = c->transparent_type;
173  return bool_success();
174  case EGL_LEVEL:
175  *value = c->level;
176  return bool_success();
177  case EGL_RED_SIZE:
178  *value = c->red_size;
179  return bool_success();
180  case EGL_GREEN_SIZE:
181  *value = c->green_size;
182  return bool_success();
183  case EGL_BLUE_SIZE:
184  *value = c->blue_size;
185  return bool_success();
186  case EGL_ALPHA_SIZE:
187  *value = c->alpha_size;
188  return bool_success();
189  case EGL_DEPTH_SIZE:
190  *value = c->depth_size;
191  return bool_success();
192  case EGL_STENCIL_SIZE:
193  *value = c->stencil_size;
194  return bool_success();
195  case EGL_SAMPLES:
196  *value = c->samples;
197  return bool_success();
198  case EGL_SAMPLE_BUFFERS:
199  *value = c->sample_buffers;
200  return bool_success();
201  case EGL_NATIVE_VISUAL_ID:
202  *value = c->native_visual_id;
203  return bool_success();
204  case EGL_NATIVE_VISUAL_TYPE:
205  *value = c->native_visual_type;
206  return bool_success();
207  case EGL_NATIVE_RENDERABLE:
208  *value = c->native_renderable;
209  return bool_success();
210  case EGL_CONFIG_CAVEAT:
211  *value = c->config_caveat;
212  return bool_success();
213  case EGL_BIND_TO_TEXTURE_RGB:
214  *value = c->bind_to_texture_rgb;
215  return bool_success();
216  case EGL_BIND_TO_TEXTURE_RGBA:
217  *value = c->bind_to_texture_rgba;
218  return bool_success();
219  case EGL_RENDERABLE_TYPE:
220  *value = c->renderable_type;
221  return bool_success();
222  case EGL_CONFORMANT:
223  *value = c->conformant;
224  return bool_success();
225  case EGL_SURFACE_TYPE:
226  *value = c->surface_type;
227  return bool_success();
228  case EGL_MAX_PBUFFER_WIDTH:
229  *value = c->max_pbuffer_width;
230  return bool_success();
231  case EGL_MAX_PBUFFER_HEIGHT:
232  *value = c->max_pbuffer_height;
233  return bool_success();
234  case EGL_MAX_PBUFFER_PIXELS:
235  *value = c->max_pbuffer_pixels;
236  return bool_success();
237  case EGL_MIN_SWAP_INTERVAL:
238  *value = c->min_swap_interval;
239  return bool_success();
240  case EGL_MAX_SWAP_INTERVAL:
241  *value = c->max_swap_interval;
242  return bool_success();
243  default:
244  return bool_failure(EGL_BAD_ATTRIBUTE);
245  }
246 }
247 
248 EGLDisplay eglGetDisplay(EGLNativeDisplayType display_id) {
249  return &mock_display;
250 }
251 
252 EGLint eglGetError() {
253  EGLint error = mock_error;
254  mock_error = EGL_SUCCESS;
255  return error;
256 }
257 
258 void (*eglGetProcAddress(const char* procname))(void) {
259  mock_error = EGL_SUCCESS;
260  return nullptr;
261 }
262 
263 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint* major, EGLint* minor) {
264  if (!check_display(dpy))
265  return EGL_FALSE;
266 
267  if (!display_initialized) {
268  mock_config.config_id = 1;
269  mock_config.buffer_size = 32;
270  mock_config.color_buffer_type = EGL_RGB_BUFFER;
271  mock_config.transparent_type = EGL_NONE;
272  mock_config.level = 1;
273  mock_config.red_size = 8;
274  mock_config.green_size = 8;
275  mock_config.blue_size = 8;
276  mock_config.alpha_size = 0;
277  mock_config.depth_size = 0;
278  mock_config.stencil_size = 0;
279  mock_config.samples = 0;
280  mock_config.sample_buffers = 0;
281  mock_config.native_visual_id = 1;
282  mock_config.native_visual_type = 0;
283  mock_config.native_renderable = EGL_TRUE;
284  mock_config.config_caveat = EGL_NONE;
285  mock_config.bind_to_texture_rgb = EGL_TRUE;
286  mock_config.bind_to_texture_rgba = EGL_FALSE;
287  mock_config.renderable_type = EGL_OPENGL_ES2_BIT;
288  mock_config.conformant = EGL_OPENGL_ES2_BIT;
289  mock_config.surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
290  mock_config.max_pbuffer_width = 1024;
291  mock_config.max_pbuffer_height = 1024;
292  mock_config.max_pbuffer_pixels = 1024 * 1024;
293  mock_config.min_swap_interval = 0;
294  mock_config.max_swap_interval = 1000;
295  display_initialized = true;
296  }
297 
298  if (major != nullptr)
299  *major = 1;
300  if (minor != nullptr)
301  *major = 5;
302 
303  return bool_success();
304 }
305 
306 EGLBoolean eglMakeCurrent(EGLDisplay dpy,
307  EGLSurface draw,
308  EGLSurface read,
309  EGLContext ctx) {
310  if (!check_display(dpy) || !check_initialized(dpy))
311  return EGL_FALSE;
312 
313  return bool_success();
314 }
315 
316 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
317  if (!check_display(dpy) || !check_initialized(dpy))
318  return EGL_FALSE;
319 
320  return bool_success();
321 }
EGLint max_pbuffer_height
Definition: mock_egl.cc:31
static MockDisplay mock_display
Definition: mock_egl.cc:47
static MockSurface mock_surface
Definition: mock_egl.cc:50
EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
Definition: mock_egl.cc:153
FlMethodResponse GError ** error
static EGLBoolean bool_success()
Definition: mock_egl.cc:81
EGLint surface_type
Definition: mock_egl.cc:29
EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
Definition: mock_egl.cc:263
EGLint green_size
Definition: mock_egl.cc:14
void(*)(void) eglGetProcAddress(const char *procname)
Definition: mock_egl.cc:258
EGLint bind_to_texture_rgb
Definition: mock_egl.cc:25
EGLDisplay eglGetDisplay(EGLNativeDisplayType display_id)
Definition: mock_egl.cc:248
EGLint renderable_type
Definition: mock_egl.cc:27
EGLint alpha_size
Definition: mock_egl.cc:16
EGLint transparent_type
Definition: mock_egl.cc:11
static EGLBoolean bool_failure(EGLint error)
Definition: mock_egl.cc:86
EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
Definition: mock_egl.cc:121
EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
Definition: mock_egl.cc:142
EGLint samples
Definition: mock_egl.cc:19
EGLint buffer_size
Definition: mock_egl.cc:9
EGLBoolean eglBindAPI(EGLenum api)
Definition: mock_egl.cc:91
EGLint conformant
Definition: mock_egl.cc:28
EGLint stencil_size
Definition: mock_egl.cc:18
EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
Definition: mock_egl.cc:306
EGLint max_swap_interval
Definition: mock_egl.cc:34
static bool check_display(EGLDisplay dpy)
Definition: mock_egl.cc:54
EGLint eglGetError()
Definition: mock_egl.cc:252
uint8_t value
EGLint min_swap_interval
Definition: mock_egl.cc:33
static MockConfig mock_config
Definition: mock_egl.cc:48
EGLint config_id
Definition: mock_egl.cc:8
EGLint color_buffer_type
Definition: mock_egl.cc:10
static MockContext mock_context
Definition: mock_egl.cc:49
static bool check_config(EGLConfig config)
Definition: mock_egl.cc:72
EGLint native_visual_id
Definition: mock_egl.cc:21
EGLint native_renderable
Definition: mock_egl.cc:23
EGLint bind_to_texture_rgba
Definition: mock_egl.cc:26
static bool check_initialized(EGLDisplay dpy)
Definition: mock_egl.cc:63
EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
Definition: mock_egl.cc:316
static EGLint mock_error
Definition: mock_egl.cc:52
EGLint red_size
Definition: mock_egl.cc:13
EGLint sample_buffers
Definition: mock_egl.cc:20
EGLint max_pbuffer_width
Definition: mock_egl.cc:30
EGLint level
Definition: mock_egl.cc:12
EGLint blue_size
Definition: mock_egl.cc:15
static bool display_initialized
Definition: mock_egl.cc:46
EGLint native_visual_type
Definition: mock_egl.cc:22
EGLint max_pbuffer_pixels
Definition: mock_egl.cc:32
EGLSurface eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
Definition: mock_egl.cc:132
EGLBoolean eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
Definition: mock_egl.cc:95
EGLint depth_size
Definition: mock_egl.cc:17
EGLint config_caveat
Definition: mock_egl.cc:24