wld-private.h (9260B)
1 /* wld: wld-private.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_PRIVATE_H 25 #define WLD_PRIVATE_H 26 27 #include "wld.h" 28 29 #include <assert.h> 30 #include <ft2build.h> 31 #include <stdbool.h> 32 #include <stdint.h> 33 #include FT_FREETYPE_H 34 #include FT_BITMAP_H 35 36 #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0]) 37 #if ENABLE_DEBUG 38 #define DEBUG(format, ...) \ 39 fprintf(stderr, "# %s: " format, __func__, ##__VA_ARGS__) 40 #else 41 #define DEBUG(format, ...) 42 #endif 43 44 #define EXPORT __attribute__((visibility("default"))) 45 #define CONTAINER_OF(ptr, type, member) \ 46 ((type *)((uintptr_t)ptr - offsetof(type, member))) 47 #define IMPL(impl_type, base_type) \ 48 static inline struct impl_type *impl_type(struct base_type *object) \ 49 { \ 50 assert(object->impl == &base_type##_impl); \ 51 return (struct impl_type *)object; \ 52 } 53 54 struct wld_font_context { 55 FT_Library library; 56 }; 57 58 struct glyph { 59 FT_Bitmap bitmap; 60 61 /** 62 * The offset from the origin to the top left corner of the bitmap. 63 */ 64 int16_t x, y; 65 66 /** 67 * The width to advance to the origin of the next character. 68 */ 69 uint16_t advance; 70 }; 71 72 /* glyph indices of recently used characters, indexed by the character's 73 * low bits: FT_Get_Char_Index searches the face's cmap on every call */ 74 #define FONT_CHAR_CACHE 256 75 76 /* One character of UTF-8 text of at most len bytes into *c: its length, 77 * 0 at the end of the text or on a byte that starts no character. */ 78 static inline int 79 wld_utf8_decode(const char *text, int len, uint32_t *c) 80 { 81 const unsigned char *s = (const unsigned char *)text; 82 int n, i; 83 84 if (len <= 0 || !s[0]) 85 return 0; 86 if (s[0] < 0x80) { 87 *c = s[0]; 88 return 1; 89 } 90 if ((s[0] & 0xe0) == 0xc0) 91 n = 2, *c = s[0] & 0x1f; 92 else if ((s[0] & 0xf0) == 0xe0) 93 n = 3, *c = s[0] & 0x0f; 94 else if ((s[0] & 0xf8) == 0xf0) 95 n = 4, *c = s[0] & 0x07; 96 else 97 return 0; 98 if (n > len) 99 return 0; 100 for (i = 1; i < n; i++) { 101 if ((s[i] & 0xc0) != 0x80) 102 return 0; 103 *c = *c << 6 | (s[i] & 0x3f); 104 } 105 return n; 106 } 107 108 struct font { 109 struct wld_font base; 110 111 struct wld_font_context *context; 112 FT_Face face; 113 struct glyph **glyphs; 114 struct { 115 uint32_t key; 116 FT_UInt index; 117 } chars[FONT_CHAR_CACHE]; 118 }; 119 120 struct wld_context_impl { 121 struct wld_renderer *(*create_renderer)(struct wld_context *context); 122 struct buffer *(*create_buffer)(struct wld_context *context, 123 uint32_t width, uint32_t height, 124 uint32_t format, uint32_t flags); 125 struct buffer *(*import_buffer)(struct wld_context *context, 126 uint32_t type, union wld_object object, 127 uint32_t width, uint32_t height, 128 uint32_t format, uint32_t pitch); 129 struct wld_surface *(*create_surface)(struct wld_context *context, 130 uint32_t width, uint32_t height, 131 uint32_t format, uint32_t flags); 132 void (*destroy)(struct wld_context *context); 133 }; 134 135 struct wld_renderer_impl { 136 uint32_t (*capabilities)(struct wld_renderer *renderer, 137 struct buffer *buffer); 138 bool (*set_target)(struct wld_renderer *renderer, struct buffer *buffer); 139 void (*fill_rectangle)(struct wld_renderer *renderer, 140 uint32_t color, int32_t x, int32_t y, 141 uint32_t width, uint32_t height); 142 void (*fill_region)(struct wld_renderer *renderer, 143 uint32_t color, pixman_region32_t *region); 144 void (*copy_rectangle)(struct wld_renderer *renderer, struct buffer *src, 145 int32_t dst_x, int32_t dst_y, 146 int32_t src_x, int32_t src_y, 147 uint32_t width, uint32_t height); 148 void (*copy_region)(struct wld_renderer *renderer, struct buffer *src, 149 int32_t dst_x, int32_t dst_y, 150 pixman_region32_t *region); 151 void (*draw_circle)(struct wld_renderer *renderer, uint32_t color, 152 int32_t x, int32_t y, uint32_t r, bool fill); 153 void (*draw_line)(struct wld_renderer *renderer, uint32_t color, 154 int32_t x1, int32_t y1, int32_t x2, int32_t y2); 155 void (*draw_text)(struct wld_renderer *renderer, 156 struct font *font, uint32_t color, 157 int32_t x, int32_t y, const char *text, uint32_t length, 158 struct wld_extents *extents); 159 void (*set_clip)(struct wld_renderer *renderer, int32_t x, int32_t y, 160 uint32_t width, uint32_t height); 161 void (*flush)(struct wld_renderer *renderer); 162 void (*destroy)(struct wld_renderer *renderer); 163 }; 164 165 struct buffer { 166 struct wld_buffer base; 167 168 unsigned ref, map_ref; 169 struct wld_exporter *exporters; 170 struct wld_destructor *destructors; 171 }; 172 173 struct wld_buffer_impl { 174 bool (*map)(struct buffer *buffer); 175 bool (*unmap)(struct buffer *buffer); 176 void (*flush)(struct buffer *buffer); 177 void (*destroy)(struct buffer *buffer); 178 }; 179 180 struct wld_surface_impl { 181 pixman_region32_t *(*damage)(struct wld_surface *surface, 182 pixman_region32_t *damage); 183 struct buffer *(*back)(struct wld_surface *surface); 184 struct buffer *(*take)(struct wld_surface *surface); 185 bool (*release)(struct wld_surface *surface, struct buffer *buffer); 186 bool (*swap)(struct wld_surface *surface); 187 void (*destroy)(struct wld_surface *surface); 188 }; 189 190 struct buffer_socket { 191 const struct buffer_socket_impl *impl; 192 }; 193 194 struct buffer_socket_impl { 195 bool (*attach)(struct buffer_socket *socket, struct buffer *buffer); 196 void (*process)(struct buffer_socket *socket); 197 void (*destroy)(struct buffer_socket *socket); 198 }; 199 200 bool font_ensure_glyph(struct font *font, FT_UInt glyph_index); 201 FT_UInt font_char_index(struct font *font, uint32_t character); 202 203 /** 204 * Returns the number of bytes per pixel for the given format. 205 */ 206 static inline uint8_t 207 format_bytes_per_pixel(enum wld_format format) 208 { 209 switch (format) { 210 case WLD_FORMAT_ARGB8888: 211 case WLD_FORMAT_XRGB8888: 212 return 4; 213 default: 214 return 0; 215 } 216 } 217 218 static inline pixman_format_code_t 219 format_wld_to_pixman(uint32_t format) 220 { 221 switch (format) { 222 case WLD_FORMAT_ARGB8888: 223 return PIXMAN_a8r8g8b8; 224 case WLD_FORMAT_XRGB8888: 225 return PIXMAN_x8r8g8b8; 226 default: 227 return 0; 228 } 229 } 230 231 static inline uint32_t 232 format_pixman_to_wld(pixman_format_code_t format) 233 { 234 switch (format) { 235 case PIXMAN_a8r8g8b8: 236 return WLD_FORMAT_ARGB8888; 237 case PIXMAN_x8r8g8b8: 238 return WLD_FORMAT_XRGB8888; 239 default: 240 return 0; 241 } 242 } 243 244 /** 245 * This default fill_region method is implemented in terms of fill_rectangle. 246 */ 247 void default_fill_region(struct wld_renderer *renderer, uint32_t color, 248 pixman_region32_t *region); 249 250 /** 251 * This default copy_region method is implemented in terms of copy_rectangle. 252 */ 253 void default_copy_region(struct wld_renderer *renderer, struct buffer *buffer, 254 int32_t dst_x, int32_t dst_y, 255 pixman_region32_t *region); 256 257 struct wld_surface *default_create_surface(struct wld_context *context, 258 uint32_t width, uint32_t height, 259 uint32_t format, uint32_t flags); 260 261 struct wld_surface *buffered_surface_create(struct wld_context *context, 262 uint32_t width, uint32_t height, 263 uint32_t format, uint32_t flags, 264 struct buffer_socket *socket); 265 266 void context_initialize(struct wld_context *context, 267 const struct wld_context_impl *impl); 268 269 void renderer_initialize(struct wld_renderer *renderer, 270 const struct wld_renderer_impl *impl); 271 272 void buffer_initialize(struct buffer *buffer, 273 const struct wld_buffer_impl *impl, 274 uint32_t width, uint32_t height, 275 uint32_t format, uint32_t pitch); 276 277 void surface_initialize(struct wld_surface *surface, 278 const struct wld_surface_impl *impl); 279 280 #endif