main
wld.h
1/* wld: wld.h
2 *
3 * Copyright (c) 2013, 2014 Michael Forney
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#ifndef WLD_H
25#define WLD_H
26
27#include <fontconfig/fontconfig.h>
28#include <pixman.h>
29#include <stdbool.h>
30#include <stdint.h>
31
32#define WLD_USER_ID (0xff << 24)
33
34#define __WLD_FOURCC(a, b, c, d) ((a) \
35 | ((b) << 8) \
36 | ((c) << 16) \
37 | ((d) << 24))
38
39/**
40 * Supported pixel formats.
41 *
42 * These formats can safely be interchanged with GBM and wl_drm formats.
43 */
44enum wld_format {
45 WLD_FORMAT_XRGB8888 = __WLD_FOURCC('X', 'R', '2', '4'),
46 WLD_FORMAT_ARGB8888 = __WLD_FOURCC('A', 'R', '2', '4')
47};
48
49enum wld_flags {
50 WLD_FLAG_MAP = 1 << 16,
51 WLD_FLAG_CURSOR = 1 << 17,
52};
53
54bool wld_lookup_named_color(const char *name, uint32_t *color);
55
56/**** WLD Context ****/
57
58enum wld_object_type {
59 WLD_OBJECT_DATA
60};
61
62union wld_object {
63 void *ptr;
64 uint32_t u32;
65 int i;
66};
67
68struct wld_context {
69 const struct wld_context_impl *const impl;
70};
71
72struct wld_renderer *wld_create_renderer(struct wld_context *context);
73
74struct wld_buffer *wld_create_buffer(struct wld_context *context,
75 uint32_t width, uint32_t height,
76 uint32_t format, uint32_t flags);
77
78struct wld_buffer *wld_import_buffer(struct wld_context *context,
79 uint32_t type, union wld_object object,
80 uint32_t width, uint32_t height,
81 uint32_t format, uint32_t pitch);
82
83struct wld_surface *wld_create_surface(struct wld_context *context,
84 uint32_t width, uint32_t height,
85 uint32_t format, uint32_t flags);
86
87void wld_destroy_context(struct wld_context *context);
88
89/**** Font Handling ****/
90
91struct wld_extents {
92 uint32_t advance;
93};
94
95struct wld_font {
96 uint32_t ascent, descent;
97 uint32_t height;
98 uint32_t max_advance;
99};
100
101/**
102 * Create a new font context.
103 *
104 * This sets up the underlying FreeType library.
105 */
106struct wld_font_context *wld_font_create_context();
107
108/**
109 * Destroy a font context.
110 */
111void wld_font_destroy_context(struct wld_font_context *context);
112
113/**
114 * Open a new font from the given fontconfig match.
115 */
116struct wld_font *wld_font_open_pattern(struct wld_font_context *context,
117 FcPattern *match);
118
119/**
120 * Open a new font from a fontconfig pattern string.
121 */
122struct wld_font *wld_font_open_name(struct wld_font_context *context,
123 const char *name);
124
125/**
126 * Close a font.
127 */
128void wld_font_close(struct wld_font *font);
129
130/**
131 * Check if the given font has a particular character (in UTF-32), and if so,
132 * load the glyph.
133 */
134bool wld_font_ensure_char(struct wld_font *font, uint32_t character);
135
136/**
137 * Calculate the text extents of the given UTF-8 string.
138 *
139 * @param length The maximum number of bytes in the string to process
140 */
141void wld_font_text_extents_n(struct wld_font *font,
142 const char *text, int32_t length,
143 struct wld_extents *extents);
144
145static inline void
146wld_font_text_extents(struct wld_font *font,
147 const char *text,
148 struct wld_extents *extents)
149{
150 wld_font_text_extents_n(font, text, INT32_MAX, extents);
151}
152
153/**** Buffers ****/
154
155struct wld_exporter {
156 bool (*export)(struct wld_exporter *exporter, struct wld_buffer *buffer,
157 uint32_t type, union wld_object *object);
158 struct wld_exporter *next;
159};
160
161struct wld_destructor {
162 void (*destroy)(struct wld_destructor *destructor);
163 struct wld_destructor *next;
164};
165
166struct wld_buffer {
167 const struct wld_buffer_impl *const impl;
168
169 uint32_t width, height, pitch;
170 enum wld_format format;
171 pixman_region32_t damage;
172 void *map;
173};
174
175bool wld_map(struct wld_buffer *buffer);
176bool wld_unmap(struct wld_buffer *buffer);
177
178bool wld_export(struct wld_buffer *buffer,
179 uint32_t type, union wld_object *object);
180
181void wld_buffer_add_exporter(struct wld_buffer *buffer,
182 struct wld_exporter *exporter);
183
184void wld_buffer_add_destructor(struct wld_buffer *buffer,
185 struct wld_destructor *destructor);
186
187/**
188 * Increase the reference count of a buffer.
189 */
190void wld_buffer_reference(struct wld_buffer *buffer);
191
192/**
193 * Decrease the reference count of a buffer.
194 *
195 * When the reference count drops to zero, the buffer will be destroyed.
196 */
197void wld_buffer_unreference(struct wld_buffer *buffer);
198
199/**** Surfaces ****/
200
201struct wld_surface {
202 const struct wld_surface_impl *const impl;
203};
204
205pixman_region32_t *wld_surface_damage(struct wld_surface *surface,
206 pixman_region32_t *new_damage);
207
208struct wld_buffer *wld_surface_take(struct wld_surface *surface);
209
210void wld_surface_release(struct wld_surface *surface,
211 struct wld_buffer *buffer);
212
213bool wld_swap(struct wld_surface *surface);
214
215void wld_destroy_surface(struct wld_surface *surface);
216
217/**** Renderers ****/
218
219struct wld_renderer {
220 const struct wld_renderer_impl *const impl;
221 struct wld_buffer *target;
222};
223
224enum wld_capability {
225 WLD_CAPABILITY_READ = 1 << 0,
226 WLD_CAPABILITY_WRITE = 1 << 1,
227};
228
229void wld_destroy_renderer(struct wld_renderer *renderer);
230
231uint32_t wld_capabilities(struct wld_renderer *renderer,
232 struct wld_buffer *buffer);
233
234bool wld_set_target_buffer(struct wld_renderer *renderer,
235 struct wld_buffer *buffer);
236
237bool wld_set_target_surface(struct wld_renderer *renderer,
238 struct wld_surface *surface);
239
240void wld_fill_rectangle(struct wld_renderer *renderer, uint32_t color,
241 int32_t x, int32_t y, uint32_t width, uint32_t height);
242
243void wld_fill_region(struct wld_renderer *renderer, uint32_t color,
244 pixman_region32_t *region);
245
246void wld_copy_rectangle(struct wld_renderer *renderer,
247 struct wld_buffer *buffer,
248 int32_t dst_x, int32_t dst_y,
249 int32_t src_x, int32_t src_y,
250 uint32_t width, uint32_t height);
251
252void wld_copy_region(struct wld_renderer *renderer,
253 struct wld_buffer *buffer,
254 int32_t dst_x, int32_t dst_y, pixman_region32_t *region);
255
256void wld_draw_circle(struct wld_renderer *renderer, uint32_t color,
257 int32_t x, int32_t y, uint32_t r, bool fill);
258
259void wld_draw_line(struct wld_renderer *renderer, uint32_t color,
260 int32_t x1, int32_t y1, int32_t x2, int32_t y2);
261
262/**
263 * Draw a UTF-8 text string to the given buffer.
264 *
265 * @param length The maximum number of bytes in the string to process. If
266 * length is -1, then draw until a NULL byte is found.
267 * @param extents If not NULL, will be initialized to the extents of the
268 * drawn text
269 */
270void wld_draw_text(struct wld_renderer *renderer,
271 struct wld_font *font, uint32_t color,
272 int32_t x, int32_t y, const char *text, uint32_t length,
273 struct wld_extents *extents);
274
275void wld_flush(struct wld_renderer *renderer);
276
277#endif