1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/graphics/framebuffers.h"
#include "gate/atomics.h"
#include "gate/results.h"
#include "gate/times.h"
#if defined(GATE_SYS_WINSTORE)
# define GATE_GRAPHICS_FRAMEBUFFERS_DIRECTX
#elif defined(GATE_SYS_WIN16)
# define GATE_GRAPHICS_FRAMEBUFFERS_WIN16
#elif defined(GATE_SYS_WIN)
# define GATE_GRAPHICS_FRAMEBUFFERS_WIN32
#elif defined(GATE_SYS_ANDROID)
# define GATE_GRAPHICS_FRAMEBUFFERS_GL
#elif defined(GATE_SYS_OPENBSD) || defined(GATE_SYS_BEOS)
# define GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL
#elif defined(GATE_SYS_DARWIN)
# define GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL
#elif defined(GATE_SYS_POSIX)
# if defined(GATE_GRAPHICS_WAYLAND)
# define GATE_GRAPHICS_FRAMEBUFFERS_WAYLAND 1
# else
# if defined(HAVE_X11_HEADERS)
# define GATE_GRAPHICS_FRAMEBUFFERS_XLIB 1
# else
# define GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL
# endif
# endif
#elif defined(GATE_SYS_EFI)
# define GATE_GRAPHICS_FRAMEBUFFERS_UGA
#elif defined(GATE_SYS_DOS)
# define GATE_GRAPHICS_FRAMEBUFFERS_INT10
#elif defined(GATE_SYS_WASM)
# define GATE_GRAPHICS_FRAMEBUFFERS_WASM
#else
# define GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL
#endif
/**************************************************
* generic rasterimage framebuffer implementation *
**************************************************/
typedef struct rasterfb_impl
{
GATE_INTERFACE_VTBL(gate_framebuffer) const* vtbl;
gate_atomic_int_t ref_counter;
gate_rasterimage_t image_header;
gate_uint8_t image_data[8];
} rasterfb_t;
static char const* rasterfb_get_interface_name(void* obj)
{
GATE_UNUSED_ARG(obj);
return GATE_INTERFACE_NAME_FRAMEBUFFER;
}
static void rasterfb_release(void* obj)
{
rasterfb_t* const impl = (rasterfb_t*)obj;
if (0 == gate_atomic_int_dec(&impl->ref_counter))
{
gate_mem_clear(impl, sizeof(rasterfb_t));
gate_mem_dealloc(impl);
}
}
static int rasterfb_retain(void* obj)
{
rasterfb_t* const impl = (rasterfb_t*)obj;
return gate_atomic_int_inc(&impl->ref_counter);
}
static gate_result_t rasterfb_get_info(void* obj, gate_framebuffer_info_t* ptr_info)
{
rasterfb_t* const impl = (rasterfb_t*)obj;
gate_result_t ret = GATE_RESULT_INVALIDARG;
if (impl && ptr_info)
{
ptr_info->width = gate_rasterimage_width(&impl->image_header);
ptr_info->height = gate_rasterimage_height(&impl->image_header);
ptr_info->bits_per_pixel = 32;
ptr_info->format_id = 0;
ret = GATE_RESULT_OK;
}
return ret;
}
static gate_result_t rasterfb_get_property(void* obj, gate_string_t const* name, gate_property_t* prop)
{
GATE_UNUSED_ARG(obj);
GATE_UNUSED_ARG(name);
GATE_UNUSED_ARG(prop);
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t rasterfb_set_property(void* obj, gate_string_t const* name, gate_property_t const* prop)
{
GATE_UNUSED_ARG(obj);
GATE_UNUSED_ARG(name);
GATE_UNUSED_ARG(prop);
return GATE_RESULT_NOTSUPPORTED;
}
static gate_result_t rasterfb_update(void* obj)
{
/* rasterimage framebuffer is always up-to-date */
GATE_UNUSED_ARG(obj);
return GATE_RESULT_OK;
}
static gate_result_t rasterfb_get_pixel(void* obj, gate_uint32_t x, gate_uint32_t y, gate_color_t* ptr_col)
{
rasterfb_t* const impl = (rasterfb_t*)obj;
return gate_rasterimage_get_pixel(&impl->image_header, x, y, ptr_col);
}
static gate_result_t rasterfb_set_pixel(void* obj, gate_uint32_t x, gate_uint32_t y, gate_color_t col)
{
rasterfb_t* const impl = (rasterfb_t*)obj;
return gate_rasterimage_set_pixel(&impl->image_header, x, y, &col);
}
static gate_result_t rasterfb_get_image(void* obj, gate_uint32_t src_x, gate_uint32_t src_y, gate_uint32_t width, gate_uint32_t height, gate_rasterimage_t* ptr_image)
{
rasterfb_t* const impl = (rasterfb_t*)obj;
gate_result_t ret = GATE_RESULT_FAILED;
gate_uint32_t fb_width = gate_rasterimage_width(&impl->image_header);
gate_uint32_t fb_height = gate_rasterimage_height(&impl->image_header);
if ((src_x == 0) && (src_y == 0) && (((width == 0) && (height == 0)) || ((width == fb_width) && (height == fb_height))))
{
if (gate_rasterimage_copy(ptr_image, &impl->image_header))
{
ret = GATE_RESULT_OK;
}
}
else
{
do
{
if (!gate_rasterimage_create(ptr_image, GATE_IMAGE_PIXELFORMAT_BGR32, width, height, NULL))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_rasterimage_paste_image(ptr_image, &impl->image_header, 0, 0, src_x, src_y, width, height, false);
if (GATE_FAILED(ret))
{
gate_rasterimage_release(ptr_image);
}
} while (0);
}
return ret;
}
static gate_result_t rasterfb_set_image(void* obj, gate_rasterimage_t const* ptr_image, gate_uint32_t dest_x, gate_uint32_t dest_y)
{
rasterfb_t* const impl = (rasterfb_t*)obj;
gate_result_t ret = GATE_RESULT_FAILED;
gate_uint32_t width = gate_rasterimage_width(ptr_image);
gate_uint32_t height = gate_rasterimage_height(ptr_image);
ret = gate_rasterimage_paste_image(&impl->image_header, ptr_image, dest_x, dest_y, 0, 0, width, height, false);
return ret;
}
static gate_result_t rasterfb_await_event(void* obj, gate_uint32_t timeout_ms, gate_framebuffer_event_t* ptr_received_evt)
{
GATE_UNUSED_ARG(obj);
GATE_UNUSED_ARG(timeout_ms);
GATE_UNUSED_ARG(ptr_received_evt);
return GATE_RESULT_NOTSUPPORTED;
}
static GATE_INTERFACE_VTBL(gate_framebuffer) gate_rasterfb_vtbl;
static void gate_init_rasterfb_vtbl()
{
if (!gate_rasterfb_vtbl.get_interface_name)
{
GATE_INTERFACE_VTBL(gate_framebuffer) const local_vtbl =
{
&rasterfb_get_interface_name,
&rasterfb_release,
&rasterfb_retain,
&rasterfb_get_info,
&rasterfb_get_property,
&rasterfb_set_property,
&rasterfb_update,
&rasterfb_get_pixel,
&rasterfb_set_pixel,
&rasterfb_get_image,
&rasterfb_set_image,
&rasterfb_await_event
};
gate_rasterfb_vtbl = local_vtbl;
}
}
gate_result_t gate_framebuffer_create_image(gate_uint32_t width, gate_uint32_t height, gate_framebuffer_t** ptr_framebuffer)
{
rasterfb_t* impl = NULL;
gate_result_t ret = GATE_RESULT_FAILED;
gate_size_t datasize;
do
{
datasize = sizeof(gate_color_t) * width * height;
impl = (rasterfb_t*)gate_mem_alloc(sizeof(rasterfb_t) + datasize);
if (!impl)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
gate_init_rasterfb_vtbl();
impl->vtbl = &gate_rasterfb_vtbl;
gate_atomic_int_init(&impl->ref_counter, 1);
impl->image_header.width = width;
impl->image_header.height = height;
impl->image_header.line_padding = 0;
impl->image_header.bits_per_pixel = sizeof(gate_color_t) * 8;
impl->image_header.data = &impl->image_data[0];
impl->image_header.databuffer = NULL;
gate_mem_clear(&impl->image_data[0], datasize);
if (ptr_framebuffer)
{
*ptr_framebuffer = (gate_framebuffer_t*)impl;
impl = NULL;
}
ret = GATE_RESULT_OK;
} while (0);
if (impl)
{
gate_mem_dealloc(impl);
}
return ret;
}
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_DIRECTX)
#include "gate/graphics/platform/framebuffers_impl_directx.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_DIRECTX */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_WIN16)
#include "gate/graphics/platform/framebuffers_impl_win16.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_WIN16 */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_WIN32)
#include "gate/graphics/platform/framebuffers_impl_win32.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_WIN32 */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_WAYLAND)
#include "gate/graphics/platform/framebuffers_impl_wayland.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_WAYLAND */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_XLIB)
#include "gate/graphics/platform/framebuffers_impl_xlib.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_XLIB */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_GL)
#include "gate/graphics/platform/framebuffers_impl_gl.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_GL */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_UGA)
#include "gate/graphics/platform/framebuffers_impl_efigop.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_UGA */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_INT10)
#include "gate/graphics/platform/framebuffers_impl_int10.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_INT10 */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_WASM)
#include "gate/graphics/platform/framebuffers_impl_wasm.h"
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_WASM */
#if defined(GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL)
gate_size_t gate_framebuffer_enum_displays(gate_framebuffer_info_t* ptr_infos, gate_size_t infos_capacity)
{
GATE_UNUSED_ARG(ptr_infos);
GATE_UNUSED_ARG(infos_capacity);
return 0;
}
gate_result_t gate_framebuffer_find_display(gate_uint32_t required_width, gate_uint32_t required_height, gate_framebuffer_info_t* ptr_found_display_info)
{
GATE_UNUSED_ARG(required_width);
GATE_UNUSED_ARG(required_height);
GATE_UNUSED_ARG(ptr_found_display_info);
return GATE_RESULT_NOMATCH;
}
gate_result_t gate_framebuffer_open_display(gate_framebuffer_info_t const* ptr_infos, gate_framebuffer_t** ptr_framebuffer)
{
GATE_UNUSED_ARG(ptr_infos);
GATE_UNUSED_ARG(ptr_framebuffer);
return GATE_RESULT_NOMATCH;
}
#endif /* GATE_GRAPHICS_FRAMEBUFFERS_NOIMPL */
|