wld.h (9961B)
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 #ifdef WLD_FONTCONFIG 28 #include <fontconfig/fontconfig.h> 29 #endif 30 #include <pixman.h> 31 #include <stdbool.h> 32 #include <stdint.h> 33 34 #define WLD_USER_ID (0xff << 24) 35 36 #define __WLD_FOURCC(a, b, c, d) ((a) \ 37 | ((b) << 8) \ 38 | ((c) << 16) \ 39 | ((d) << 24)) 40 41 /** 42 * Supported pixel formats. 43 * 44 * These formats can safely be interchanged with GBM and wl_drm formats. 45 */ 46 enum wld_format { 47 WLD_FORMAT_XRGB8888 = __WLD_FOURCC('X', 'R', '2', '4'), 48 WLD_FORMAT_ARGB8888 = __WLD_FOURCC('A', 'R', '2', '4') 49 }; 50 51 enum wld_flags { 52 WLD_FLAG_MAP = 1 << 16, 53 WLD_FLAG_CURSOR = 1 << 17, 54 }; 55 56 bool wld_lookup_named_color(const char *name, uint32_t *color); 57 58 /**** WLD Context ****/ 59 60 enum wld_object_type { 61 WLD_OBJECT_DATA 62 }; 63 64 union wld_object { 65 void *ptr; 66 uint32_t u32; 67 int i; 68 }; 69 70 struct wld_context { 71 const struct wld_context_impl *const impl; 72 }; 73 74 struct wld_renderer *wld_create_renderer(struct wld_context *context); 75 76 struct wld_buffer *wld_create_buffer(struct wld_context *context, 77 uint32_t width, uint32_t height, 78 uint32_t format, uint32_t flags); 79 80 struct wld_buffer *wld_import_buffer(struct wld_context *context, 81 uint32_t type, union wld_object object, 82 uint32_t width, uint32_t height, 83 uint32_t format, uint32_t pitch); 84 85 struct wld_surface *wld_create_surface(struct wld_context *context, 86 uint32_t width, uint32_t height, 87 uint32_t format, uint32_t flags); 88 89 void wld_destroy_context(struct wld_context *context); 90 91 /**** Font Handling ****/ 92 93 struct wld_extents { 94 uint32_t advance; 95 96 /** 97 * The box the glyphs cover, from the pen origin on the baseline. 98 * Only wld_font_text_extents sets it. 99 */ 100 int32_t ink_x, ink_y; 101 uint32_t ink_width, ink_height; 102 }; 103 104 struct wld_font { 105 uint32_t ascent, descent; 106 uint32_t height; 107 uint32_t max_advance; 108 }; 109 110 /** 111 * Create a new font context. 112 * 113 * This sets up the underlying FreeType library. 114 */ 115 struct wld_font_context *wld_font_create_context(); 116 117 /** 118 * Destroy a font context. 119 */ 120 void wld_font_destroy_context(struct wld_font_context *context); 121 122 /** 123 * Open a font file, face index of it (0 unless the file is a collection), 124 * pixel_size pixels tall. It needs no font matcher: this is how a font is 125 * opened where there is no fontconfig. 126 */ 127 struct wld_font *wld_font_open_file(struct wld_font_context *context, 128 const char *path, int index, 129 double pixel_size); 130 131 #ifdef WLD_FONTCONFIG 132 /** 133 * Open a new font from the given fontconfig match. 134 */ 135 struct wld_font *wld_font_open_pattern(struct wld_font_context *context, 136 FcPattern *match); 137 138 /** 139 * Open a new font from a fontconfig pattern string. 140 */ 141 struct wld_font *wld_font_open_name(struct wld_font_context *context, 142 const char *name); 143 #endif 144 145 /** 146 * Close a font. 147 */ 148 void wld_font_close(struct wld_font *font); 149 150 /** 151 * Check if the given font has a particular character (in UTF-32), and if so, 152 * load the glyph. 153 */ 154 bool wld_font_ensure_char(struct wld_font *font, uint32_t character); 155 156 /** 157 * The character that draws the glyph called name, or 0. 158 */ 159 uint32_t wld_font_char_by_name(struct wld_font *font, const char *name); 160 161 /** 162 * Calculate the text extents of the given UTF-8 string. 163 * 164 * @param length The maximum number of bytes in the string to process 165 */ 166 void wld_font_text_extents_n(struct wld_font *font, 167 const char *text, int32_t length, 168 struct wld_extents *extents); 169 170 static inline void 171 wld_font_text_extents(struct wld_font *font, 172 const char *text, 173 struct wld_extents *extents) 174 { 175 wld_font_text_extents_n(font, text, INT32_MAX, extents); 176 } 177 178 /**** Buffers ****/ 179 180 struct wld_exporter { 181 bool (*export)(struct wld_exporter *exporter, struct wld_buffer *buffer, 182 uint32_t type, union wld_object *object); 183 struct wld_exporter *next; 184 }; 185 186 struct wld_destructor { 187 void (*destroy)(struct wld_destructor *destructor); 188 struct wld_destructor *next; 189 }; 190 191 struct wld_buffer { 192 const struct wld_buffer_impl *const impl; 193 194 uint32_t width, height, pitch; 195 enum wld_format format; 196 pixman_region32_t damage; 197 void *map; 198 }; 199 200 bool wld_map(struct wld_buffer *buffer); 201 bool wld_unmap(struct wld_buffer *buffer); 202 203 bool wld_export(struct wld_buffer *buffer, 204 uint32_t type, union wld_object *object); 205 206 void wld_buffer_add_exporter(struct wld_buffer *buffer, 207 struct wld_exporter *exporter); 208 209 void wld_buffer_add_destructor(struct wld_buffer *buffer, 210 struct wld_destructor *destructor); 211 212 /** 213 * Increase the reference count of a buffer. 214 */ 215 void wld_buffer_reference(struct wld_buffer *buffer); 216 217 /** 218 * Decrease the reference count of a buffer. 219 * 220 * When the reference count drops to zero, the buffer will be destroyed. 221 */ 222 void wld_buffer_unreference(struct wld_buffer *buffer); 223 224 /**** Surfaces ****/ 225 226 struct wld_surface { 227 const struct wld_surface_impl *const impl; 228 }; 229 230 pixman_region32_t *wld_surface_damage(struct wld_surface *surface, 231 pixman_region32_t *new_damage); 232 233 struct wld_buffer *wld_surface_take(struct wld_surface *surface); 234 235 void wld_surface_release(struct wld_surface *surface, 236 struct wld_buffer *buffer); 237 238 bool wld_swap(struct wld_surface *surface); 239 240 void wld_destroy_surface(struct wld_surface *surface); 241 242 /**** Renderers ****/ 243 244 struct wld_renderer { 245 const struct wld_renderer_impl *const impl; 246 struct wld_buffer *target; 247 }; 248 249 enum wld_capability { 250 WLD_CAPABILITY_READ = 1 << 0, 251 WLD_CAPABILITY_WRITE = 1 << 1, 252 }; 253 254 void wld_destroy_renderer(struct wld_renderer *renderer); 255 256 uint32_t wld_capabilities(struct wld_renderer *renderer, 257 struct wld_buffer *buffer); 258 259 bool wld_set_target_buffer(struct wld_renderer *renderer, 260 struct wld_buffer *buffer); 261 262 bool wld_set_target_surface(struct wld_renderer *renderer, 263 struct wld_surface *surface); 264 265 void wld_fill_rectangle(struct wld_renderer *renderer, uint32_t color, 266 int32_t x, int32_t y, uint32_t width, uint32_t height); 267 268 void wld_fill_region(struct wld_renderer *renderer, uint32_t color, 269 pixman_region32_t *region); 270 271 void wld_copy_rectangle(struct wld_renderer *renderer, 272 struct wld_buffer *buffer, 273 int32_t dst_x, int32_t dst_y, 274 int32_t src_x, int32_t src_y, 275 uint32_t width, uint32_t height); 276 277 void wld_copy_region(struct wld_renderer *renderer, 278 struct wld_buffer *buffer, 279 int32_t dst_x, int32_t dst_y, pixman_region32_t *region); 280 281 void wld_blend_region(struct wld_renderer *renderer, 282 struct wld_buffer *buffer, 283 int32_t dst_x, int32_t dst_y, 284 pixman_region32_t *region); 285 286 /** 287 * The pixels of the renderer's target, for the CPU to read and write: what 288 * has been drawn so far, after every pending operation has finished. NULL 289 * when the target cannot be mapped. Unmap it before drawing again. 290 */ 291 void *wld_target_map(struct wld_renderer *renderer, uint32_t *pitch, 292 uint32_t *format); 293 void wld_target_unmap(struct wld_renderer *renderer); 294 295 void wld_draw_circle(struct wld_renderer *renderer, uint32_t color, 296 int32_t x, int32_t y, uint32_t r, bool fill); 297 298 void wld_draw_line(struct wld_renderer *renderer, uint32_t color, 299 int32_t x1, int32_t y1, int32_t x2, int32_t y2); 300 301 /** 302 * Draw a UTF-8 text string to the given buffer. 303 * 304 * @param length The maximum number of bytes in the string to process. If 305 * length is -1, then draw until a NULL byte is found. 306 * @param extents If not NULL, will be initialized to the extents of the 307 * drawn text 308 */ 309 /** 310 * Limit drawing to a rectangle of the target. Setting a target resets it. 311 * Returns false when the renderer cannot clip. 312 */ 313 bool wld_set_clip(struct wld_renderer *renderer, int32_t x, int32_t y, 314 uint32_t width, uint32_t height); 315 316 void wld_draw_text(struct wld_renderer *renderer, 317 struct wld_font *font, uint32_t color, 318 int32_t x, int32_t y, const char *text, uint32_t length, 319 struct wld_extents *extents); 320 321 void wld_flush(struct wld_renderer *renderer); 322 323 #endif