text: remove load/unload helpers for text-font backends
[platform/upstream/kmscon.git] / src / text.h
1 /*
2  * kmscon - Text Renderer
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * Text Renderer
28  * The Text-Renderer subsystem provides a simple way to draw text into a
29  * framebuffer. The system is modular and several different backends are
30  * available that can be used.
31  * The system is split into:
32  *  - Font renderer: The font renderer allows selecting fonts and rendering
33  *    single glyphs into memory-buffers
34  *  - Text renderer: The text renderer uses the font renderer to draw a whole
35  *    console into a framebuffer.
36  */
37
38 #ifndef KMSCON_TEXT_H
39 #define KMSCON_TEXT_H
40
41 #include <errno.h>
42 #include <stdlib.h>
43 #include "tsm_screen.h"
44 #include "uterm.h"
45
46 /* fonts */
47
48 struct kmscon_font_attr;
49 struct kmscon_glyph;
50 struct kmscon_font;
51 struct kmscon_font_ops;
52
53 #define KMSCON_FONT_MAX_NAME 128
54 #define KMSCON_FONT_DEFAULT_NAME "monospace"
55 #define KMSCON_FONT_DEFAULT_PPI 72
56
57 struct kmscon_font_attr {
58         char name[KMSCON_FONT_MAX_NAME];
59         unsigned int ppi;
60         unsigned int points;
61         bool bold;
62         bool italic;
63         unsigned int height;
64         unsigned int width;
65 };
66
67 void kmscon_font_attr_normalize(struct kmscon_font_attr *attr);
68 bool kmscon_font_attr_match(const struct kmscon_font_attr *a1,
69                             const struct kmscon_font_attr *a2);
70
71 struct kmscon_glyph {
72         struct uterm_video_buffer buf;
73         unsigned int width;
74         void *data;
75 };
76
77 struct kmscon_font {
78         unsigned long ref;
79         struct shl_register_record *record;
80         const struct kmscon_font_ops *ops;
81         struct kmscon_font_attr attr;
82         unsigned int baseline;
83         void *data;
84 };
85
86 struct kmscon_font_ops {
87         const char *name;
88         int (*init) (struct kmscon_font *out,
89                      const struct kmscon_font_attr *attr);
90         void (*destroy) (struct kmscon_font *font);
91         int (*render) (struct kmscon_font *font,
92                        uint32_t id, const uint32_t *ch, size_t len,
93                        const struct kmscon_glyph **out);
94         int (*render_empty) (struct kmscon_font *font,
95                              const struct kmscon_glyph **out);
96         int (*render_inval) (struct kmscon_font *font,
97                              const struct kmscon_glyph **out);
98 };
99
100 int kmscon_font_register(const struct kmscon_font_ops *ops);
101 void kmscon_font_unregister(const char *name);
102
103 int kmscon_font_find(struct kmscon_font **out,
104                      const struct kmscon_font_attr *attr,
105                      const char *backend);
106 void kmscon_font_ref(struct kmscon_font *font);
107 void kmscon_font_unref(struct kmscon_font *font);
108
109 int kmscon_font_render(struct kmscon_font *font,
110                        uint32_t id, const uint32_t *ch, size_t len,
111                        const struct kmscon_glyph **out);
112 int kmscon_font_render_empty(struct kmscon_font *font,
113                              const struct kmscon_glyph **out);
114 int kmscon_font_render_inval(struct kmscon_font *font,
115                              const struct kmscon_glyph **out);
116
117 /* text renderer */
118
119 struct kmscon_text;
120 struct kmscon_text_ops;
121
122 struct kmscon_text {
123         unsigned long ref;
124         struct shl_register_record *record;
125         const struct kmscon_text_ops *ops;
126         void *data;
127
128         struct kmscon_font *font;
129         struct kmscon_font *bold_font;
130         struct uterm_display *disp;
131         unsigned int cols;
132         unsigned int rows;
133         bool rendering;
134 };
135
136 struct kmscon_text_ops {
137         const char *name;
138         int (*init) (struct kmscon_text *txt);
139         void (*destroy) (struct kmscon_text *txt);
140         int (*set) (struct kmscon_text *txt);
141         void (*unset) (struct kmscon_text *txt);
142         int (*prepare) (struct kmscon_text *txt);
143         int (*draw) (struct kmscon_text *txt,
144                      uint32_t id, const uint32_t *ch, size_t len,
145                      unsigned int width,
146                      unsigned int posx, unsigned int posy,
147                      const struct tsm_screen_attr *attr);
148         int (*render) (struct kmscon_text *txt);
149         void (*abort) (struct kmscon_text *txt);
150 };
151
152 int kmscon_text_register(const struct kmscon_text_ops *ops);
153 void kmscon_text_unregister(const char *name);
154
155 int kmscon_text_new(struct kmscon_text **out, const char *backend);
156 void kmscon_text_ref(struct kmscon_text *txt);
157 void kmscon_text_unref(struct kmscon_text *txt);
158
159 int kmscon_text_set(struct kmscon_text *txt,
160                     struct kmscon_font *font,
161                     struct kmscon_font *bold_font,
162                     struct uterm_display *disp);
163 void kmscon_text_unset(struct kmscon_text *txt);
164 unsigned int kmscon_text_get_cols(struct kmscon_text *txt);
165 unsigned int kmscon_text_get_rows(struct kmscon_text *txt);
166
167 int kmscon_text_prepare(struct kmscon_text *txt);
168 int kmscon_text_draw(struct kmscon_text *txt,
169                      uint32_t id, const uint32_t *ch, size_t len,
170                      unsigned int width,
171                      unsigned int posx, unsigned int posy,
172                      const struct tsm_screen_attr *attr);
173 int kmscon_text_render(struct kmscon_text *txt);
174 void kmscon_text_abort(struct kmscon_text *txt);
175
176 int kmscon_text_prepare_cb(struct tsm_screen *con, void *data);
177 int kmscon_text_draw_cb(struct tsm_screen *con,
178                         uint32_t id, const uint32_t *ch, size_t len,
179                         unsigned int width,
180                         unsigned int posx, unsigned int posy,
181                         const struct tsm_screen_attr *attr, void *data);
182 int kmscon_text_render_cb(struct tsm_screen *con, void *data);
183
184 /* modularized backends */
185
186 extern const struct kmscon_font_ops kmscon_font_8x16_ops;
187 extern const struct kmscon_font_ops kmscon_font_unifont_ops;
188 extern const struct kmscon_font_ops kmscon_font_freetype2_ops;
189 extern const struct kmscon_font_ops kmscon_font_pango_ops;
190
191 #ifdef BUILD_ENABLE_RENDERER_BBLIT
192
193 int kmscon_text_bblit_load(void);
194 void kmscon_text_bblit_unload(void);
195
196 #else
197
198 static inline int kmscon_text_bblit_load(void)
199 {
200         return -EOPNOTSUPP;
201 }
202
203 static inline void kmscon_text_bblit_unload(void)
204 {
205 }
206
207 #endif
208
209 #ifdef BUILD_ENABLE_RENDERER_BBULK
210
211 int kmscon_text_bbulk_load(void);
212 void kmscon_text_bbulk_unload(void);
213
214 #else
215
216 static inline int kmscon_text_bbulk_load(void)
217 {
218         return -EOPNOTSUPP;
219 }
220
221 static inline void kmscon_text_bbulk_unload(void)
222 {
223 }
224
225 #endif
226
227 #ifdef BUILD_ENABLE_RENDERER_GLTEX
228
229 int kmscon_text_gltex_load(void);
230 void kmscon_text_gltex_unload(void);
231
232 #else
233
234 static inline int kmscon_text_gltex_load(void)
235 {
236         return -EOPNOTSUPP;
237 }
238
239 static inline void kmscon_text_gltex_unload(void)
240 {
241 }
242
243 #endif
244
245 static inline void kmscon_text_load_all(void)
246 {
247         kmscon_text_bbulk_load();
248         kmscon_text_bblit_load();
249         kmscon_text_gltex_load();
250 }
251
252 static inline void kmscon_text_unload_all(void)
253 {
254         kmscon_text_gltex_unload();
255         kmscon_text_bblit_unload();
256         kmscon_text_bbulk_unload();
257 }
258
259 #endif /* KMSCON_TEXT_H */