main
wld-private.h
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
54struct wld_font_context {
55 FT_Library library;
56};
57
58struct 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
72struct font {
73 struct wld_font base;
74
75 struct wld_font_context *context;
76 FT_Face face;
77 struct glyph **glyphs;
78};
79
80struct wld_context_impl {
81 struct wld_renderer *(*create_renderer)(struct wld_context *context);
82 struct buffer *(*create_buffer)(struct wld_context *context,
83 uint32_t width, uint32_t height,
84 uint32_t format, uint32_t flags);
85 struct buffer *(*import_buffer)(struct wld_context *context,
86 uint32_t type, union wld_object object,
87 uint32_t width, uint32_t height,
88 uint32_t format, uint32_t pitch);
89 struct wld_surface *(*create_surface)(struct wld_context *context,
90 uint32_t width, uint32_t height,
91 uint32_t format, uint32_t flags);
92 void (*destroy)(struct wld_context *context);
93};
94
95struct wld_renderer_impl {
96 uint32_t (*capabilities)(struct wld_renderer *renderer,
97 struct buffer *buffer);
98 bool (*set_target)(struct wld_renderer *renderer, struct buffer *buffer);
99 void (*fill_rectangle)(struct wld_renderer *renderer,
100 uint32_t color, int32_t x, int32_t y,
101 uint32_t width, uint32_t height);
102 void (*fill_region)(struct wld_renderer *renderer,
103 uint32_t color, pixman_region32_t *region);
104 void (*copy_rectangle)(struct wld_renderer *renderer, struct buffer *src,
105 int32_t dst_x, int32_t dst_y,
106 int32_t src_x, int32_t src_y,
107 uint32_t width, uint32_t height);
108 void (*copy_region)(struct wld_renderer *renderer, struct buffer *src,
109 int32_t dst_x, int32_t dst_y,
110 pixman_region32_t *region);
111 void (*draw_circle)(struct wld_renderer *renderer, uint32_t color,
112 int32_t x, int32_t y, uint32_t r, bool fill);
113 void (*draw_line)(struct wld_renderer *renderer, uint32_t color,
114 int32_t x1, int32_t y1, int32_t x2, int32_t y2);
115 void (*draw_text)(struct wld_renderer *renderer,
116 struct font *font, uint32_t color,
117 int32_t x, int32_t y, const char *text, uint32_t length,
118 struct wld_extents *extents);
119 void (*flush)(struct wld_renderer *renderer);
120 void (*destroy)(struct wld_renderer *renderer);
121};
122
123struct buffer {
124 struct wld_buffer base;
125
126 unsigned ref, map_ref;
127 struct wld_exporter *exporters;
128 struct wld_destructor *destructors;
129};
130
131struct wld_buffer_impl {
132 bool (*map)(struct buffer *buffer);
133 bool (*unmap)(struct buffer *buffer);
134 void (*destroy)(struct buffer *buffer);
135};
136
137struct wld_surface_impl {
138 pixman_region32_t *(*damage)(struct wld_surface *surface,
139 pixman_region32_t *damage);
140 struct buffer *(*back)(struct wld_surface *surface);
141 struct buffer *(*take)(struct wld_surface *surface);
142 bool (*release)(struct wld_surface *surface, struct buffer *buffer);
143 bool (*swap)(struct wld_surface *surface);
144 void (*destroy)(struct wld_surface *surface);
145};
146
147struct buffer_socket {
148 const struct buffer_socket_impl *impl;
149};
150
151struct buffer_socket_impl {
152 bool (*attach)(struct buffer_socket *socket, struct buffer *buffer);
153 void (*process)(struct buffer_socket *socket);
154 void (*destroy)(struct buffer_socket *socket);
155};
156
157bool font_ensure_glyph(struct font *font, FT_UInt glyph_index);
158
159/**
160 * Returns the number of bytes per pixel for the given format.
161 */
162static inline uint8_t
163format_bytes_per_pixel(enum wld_format format)
164{
165 switch (format) {
166 case WLD_FORMAT_ARGB8888:
167 case WLD_FORMAT_XRGB8888:
168 return 4;
169 default:
170 return 0;
171 }
172}
173
174static inline pixman_format_code_t
175format_wld_to_pixman(uint32_t format)
176{
177 switch (format) {
178 case WLD_FORMAT_ARGB8888:
179 return PIXMAN_a8r8g8b8;
180 case WLD_FORMAT_XRGB8888:
181 return PIXMAN_x8r8g8b8;
182 default:
183 return 0;
184 }
185}
186
187static inline uint32_t
188format_pixman_to_wld(pixman_format_code_t format)
189{
190 switch (format) {
191 case PIXMAN_a8r8g8b8:
192 return WLD_FORMAT_ARGB8888;
193 case PIXMAN_x8r8g8b8:
194 return WLD_FORMAT_XRGB8888;
195 default:
196 return 0;
197 }
198}
199
200/**
201 * This default fill_region method is implemented in terms of fill_rectangle.
202 */
203void default_fill_region(struct wld_renderer *renderer, uint32_t color,
204 pixman_region32_t *region);
205
206/**
207 * This default copy_region method is implemented in terms of copy_rectangle.
208 */
209void default_copy_region(struct wld_renderer *renderer, struct buffer *buffer,
210 int32_t dst_x, int32_t dst_y,
211 pixman_region32_t *region);
212
213struct wld_surface *default_create_surface(struct wld_context *context,
214 uint32_t width, uint32_t height,
215 uint32_t format, uint32_t flags);
216
217struct wld_surface *buffered_surface_create(struct wld_context *context,
218 uint32_t width, uint32_t height,
219 uint32_t format, uint32_t flags,
220 struct buffer_socket *socket);
221
222void context_initialize(struct wld_context *context,
223 const struct wld_context_impl *impl);
224
225void renderer_initialize(struct wld_renderer *renderer,
226 const struct wld_renderer_impl *impl);
227
228void buffer_initialize(struct buffer *buffer,
229 const struct wld_buffer_impl *impl,
230 uint32_t width, uint32_t height,
231 uint32_t format, uint32_t pitch);
232
233void surface_initialize(struct wld_surface *surface,
234 const struct wld_surface_impl *impl);
235
236#endif