text: remove old init/deinit helpers
[platform/upstream/kmscon.git] / src / text_bbulk.c
1 /*
2  * kmscon - Bit-Blitting Bulk Text Renderer Backend
3  *
4  * Copyright (c) 2012-2013 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  * SECTION:text_bbulk.c
28  * @short_description: Bit-Blitting Bulk Text Renderer Backend
29  * @include: text.h
30  *
31  * Similar to the bblit renderer but assembles an array of blit-requests and
32  * pushes all of them at once to the video device.
33  */
34
35 #include <errno.h>
36 #include <stdbool.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "log.h"
40 #include "text.h"
41 #include "uterm.h"
42
43 #define LOG_SUBSYSTEM "text_bbulk"
44
45 struct bbulk {
46         struct uterm_video_blend_req *reqs;
47 };
48
49 #define FONT_WIDTH(txt) ((txt)->font->attr.width)
50 #define FONT_HEIGHT(txt) ((txt)->font->attr.height)
51
52 static int bbulk_init(struct kmscon_text *txt)
53 {
54         struct bbulk *bb;
55
56         bb = malloc(sizeof(*bb));
57         if (!bb)
58                 return -ENOMEM;
59
60         txt->data = bb;
61         return 0;
62 }
63
64 static void bbulk_destroy(struct kmscon_text *txt)
65 {
66         struct bbulk *bb = txt->data;
67
68         free(bb);
69 }
70
71 static int bbulk_set(struct kmscon_text *txt)
72 {
73         struct bbulk *bb = txt->data;
74         unsigned int sw, sh, i, j;
75         struct uterm_video_blend_req *req;
76         struct uterm_mode *mode;
77
78         memset(bb, 0, sizeof(*bb));
79
80         mode = uterm_display_get_current(txt->disp);
81         if (!mode)
82                 return -EINVAL;
83         sw = uterm_mode_get_width(mode);
84         sh = uterm_mode_get_height(mode);
85
86         txt->cols = sw / FONT_WIDTH(txt);
87         txt->rows = sh / FONT_HEIGHT(txt);
88
89         bb->reqs = malloc(sizeof(*bb->reqs) * txt->cols * txt->rows);
90         if (!bb->reqs)
91                 return -ENOMEM;
92         memset(bb->reqs, 0, sizeof(*bb->reqs) * txt->cols * txt->rows);
93
94         for (i = 0; i < txt->rows; ++i) {
95                 for (j = 0; j < txt->cols; ++j) {
96                         req = &bb->reqs[i * txt->cols + j];
97                         req->x = j * FONT_WIDTH(txt);
98                         req->y = i * FONT_HEIGHT(txt);
99                 }
100         }
101
102         return 0;
103 }
104
105 static void bbulk_unset(struct kmscon_text *txt)
106 {
107         struct bbulk *bb = txt->data;
108
109         free(bb->reqs);
110         bb->reqs = NULL;
111 }
112
113 static int bbulk_draw(struct kmscon_text *txt,
114                       uint32_t id, const uint32_t *ch, size_t len,
115                       unsigned int width,
116                       unsigned int posx, unsigned int posy,
117                       const struct tsm_screen_attr *attr)
118 {
119         struct bbulk *bb = txt->data;
120         const struct kmscon_glyph *glyph;
121         int ret;
122         struct uterm_video_blend_req *req;
123         struct kmscon_font *font;
124
125         if (!width) {
126                 bb->reqs[posy * txt->cols + posx].buf = NULL;
127                 return 0;
128         }
129
130         if (attr->bold)
131                 font = txt->bold_font;
132         else
133                 font = txt->font;
134
135         if (!len) {
136                 ret = kmscon_font_render_empty(font, &glyph);
137         } else {
138                 ret = kmscon_font_render(font, id, ch, len, &glyph);
139         }
140
141         if (ret) {
142                 ret = kmscon_font_render_inval(font, &glyph);
143                 if (ret)
144                         return ret;
145         }
146
147         req = &bb->reqs[posy * txt->cols + posx];
148         req->buf = &glyph->buf;
149         if (attr->inverse) {
150                 req->fr = attr->br;
151                 req->fg = attr->bg;
152                 req->fb = attr->bb;
153                 req->br = attr->fr;
154                 req->bg = attr->fg;
155                 req->bb = attr->fb;
156         } else {
157                 req->fr = attr->fr;
158                 req->fg = attr->fg;
159                 req->fb = attr->fb;
160                 req->br = attr->br;
161                 req->bg = attr->bg;
162                 req->bb = attr->bb;
163         }
164
165         return 0;
166 }
167
168 static int bbulk_render(struct kmscon_text *txt)
169 {
170         struct bbulk *bb = txt->data;
171
172         return uterm_display_fake_blendv(txt->disp, bb->reqs,
173                                          txt->cols * txt->rows);
174 }
175
176 struct kmscon_text_ops kmscon_text_bbulk_ops = {
177         .name = "bbulk",
178         .owner = NULL,
179         .init = bbulk_init,
180         .destroy = bbulk_destroy,
181         .set = bbulk_set,
182         .unset = bbulk_unset,
183         .prepare = NULL,
184         .draw = bbulk_draw,
185         .render = bbulk_render,
186         .abort = NULL,
187 };