1 | /*
|
---|
2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libui
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Dummy graphic context
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <gfx/context.h>
|
---|
37 | #include <gfx/coord.h>
|
---|
38 | #include <mem.h>
|
---|
39 | #include <stdbool.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include "../private/dummygc.h"
|
---|
42 |
|
---|
43 | static errno_t dummygc_set_color(void *, gfx_color_t *);
|
---|
44 | static errno_t dummygc_fill_rect(void *, gfx_rect_t *);
|
---|
45 | static errno_t dummygc_update(void *);
|
---|
46 | static errno_t dummygc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
47 | gfx_bitmap_alloc_t *, void **);
|
---|
48 | static errno_t dummygc_bitmap_destroy(void *);
|
---|
49 | static errno_t dummygc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
50 | static errno_t dummygc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
51 |
|
---|
52 | /** Dummy GC operations */
|
---|
53 | gfx_context_ops_t dummygc_ops = {
|
---|
54 | .set_color = dummygc_set_color,
|
---|
55 | .fill_rect = dummygc_fill_rect,
|
---|
56 | .update = dummygc_update,
|
---|
57 | .bitmap_create = dummygc_bitmap_create,
|
---|
58 | .bitmap_destroy = dummygc_bitmap_destroy,
|
---|
59 | .bitmap_render = dummygc_bitmap_render,
|
---|
60 | .bitmap_get_alloc = dummygc_bitmap_get_alloc
|
---|
61 | };
|
---|
62 |
|
---|
63 | /** Create dummy GC.
|
---|
64 | *
|
---|
65 | * @param rdgc Place to store pointer to new dummy GC
|
---|
66 | * @return EOK on success, ENOMEM if out of memory
|
---|
67 | */
|
---|
68 | errno_t dummygc_create(dummy_gc_t **rdgc)
|
---|
69 | {
|
---|
70 | dummy_gc_t *dgc;
|
---|
71 | gfx_context_t *gc;
|
---|
72 | errno_t rc;
|
---|
73 |
|
---|
74 | dgc = calloc(1, sizeof(dummy_gc_t));
|
---|
75 | if (dgc == NULL)
|
---|
76 | return ENOMEM;
|
---|
77 |
|
---|
78 | rc = gfx_context_new(&dummygc_ops, dgc, &gc);
|
---|
79 | if (rc != EOK) {
|
---|
80 | free(dgc);
|
---|
81 | return rc;
|
---|
82 | }
|
---|
83 |
|
---|
84 | dgc->gc = gc;
|
---|
85 | *rdgc = dgc;
|
---|
86 | return EOK;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Destroy dummy GC.
|
---|
90 | *
|
---|
91 | * @param dgc Dummy GC
|
---|
92 | */
|
---|
93 | void dummygc_destroy(dummy_gc_t *dgc)
|
---|
94 | {
|
---|
95 | gfx_context_delete(dgc->gc);
|
---|
96 | free(dgc);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Get generic graphic context from dummy GC.
|
---|
100 | *
|
---|
101 | * @param dgc Dummy GC
|
---|
102 | * @return Graphic context
|
---|
103 | */
|
---|
104 | gfx_context_t *dummygc_get_ctx(dummy_gc_t *dgc)
|
---|
105 | {
|
---|
106 | return dgc->gc;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Set color on dummy GC
|
---|
110 | *
|
---|
111 | * @param arg Argument (dummy_gc_t)
|
---|
112 | * @param color Color
|
---|
113 | * @return EOK on success or an error code
|
---|
114 | */
|
---|
115 | static errno_t dummygc_set_color(void *arg, gfx_color_t *color)
|
---|
116 | {
|
---|
117 | (void) arg;
|
---|
118 | (void) color;
|
---|
119 | return EOK;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Fill rectangle on dummy GC
|
---|
123 | *
|
---|
124 | * @param arg Argument (dummy_gc_t)
|
---|
125 | * @param rect Rectangle
|
---|
126 | * @return EOK on success or an error code
|
---|
127 | */
|
---|
128 | static errno_t dummygc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
129 | {
|
---|
130 | (void) arg;
|
---|
131 | (void) rect;
|
---|
132 | return EOK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Update dummy GC
|
---|
136 | *
|
---|
137 | * @param arg Argument (dummy_gc_t)
|
---|
138 | * @return EOK on success or an error code
|
---|
139 | */
|
---|
140 | static errno_t dummygc_update(void *arg)
|
---|
141 | {
|
---|
142 | (void) arg;
|
---|
143 | return EOK;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** Create bitmap on dummy GC
|
---|
147 | *
|
---|
148 | * @param arg Argument (dummy_gc_t)
|
---|
149 | * @param params Bitmap parameters
|
---|
150 | * @param alloc Bitmap allocation info or @c NULL
|
---|
151 | * @param rbm Place to store pointer to new bitmap
|
---|
152 | * @return EOK on success or an error code
|
---|
153 | */
|
---|
154 | static errno_t dummygc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
155 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
156 | {
|
---|
157 | dummy_gc_t *dgc = (dummy_gc_t *) arg;
|
---|
158 | dummygc_bitmap_t *tbm;
|
---|
159 |
|
---|
160 | tbm = calloc(1, sizeof(dummygc_bitmap_t));
|
---|
161 | if (tbm == NULL)
|
---|
162 | return ENOMEM;
|
---|
163 |
|
---|
164 | if (alloc == NULL) {
|
---|
165 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
166 | sizeof(uint32_t);
|
---|
167 | tbm->alloc.off0 = 0;
|
---|
168 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
169 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
170 | (params->rect.p1.y - params->rect.p0.y));
|
---|
171 | tbm->myalloc = true;
|
---|
172 | if (tbm->alloc.pixels == NULL) {
|
---|
173 | free(tbm);
|
---|
174 | return ENOMEM;
|
---|
175 | }
|
---|
176 | } else {
|
---|
177 | tbm->alloc = *alloc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | tbm->dgc = dgc;
|
---|
181 | dgc->bm_created = true;
|
---|
182 | dgc->bm_params = *params;
|
---|
183 | dgc->bm_pixels = tbm->alloc.pixels;
|
---|
184 | *rbm = (void *)tbm;
|
---|
185 | return EOK;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** Destroy bitmap on dummy GC
|
---|
189 | *
|
---|
190 | * @param bm Bitmap
|
---|
191 | * @return EOK on success or an error code
|
---|
192 | */
|
---|
193 | static errno_t dummygc_bitmap_destroy(void *bm)
|
---|
194 | {
|
---|
195 | dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
|
---|
196 | if (tbm->myalloc)
|
---|
197 | free(tbm->alloc.pixels);
|
---|
198 | tbm->dgc->bm_destroyed = true;
|
---|
199 | free(tbm);
|
---|
200 | return EOK;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /** Render bitmap on dummy GC
|
---|
204 | *
|
---|
205 | * @param bm Bitmap
|
---|
206 | * @param srect Source rectangle or @c NULL
|
---|
207 | * @param offs Offset or @c NULL
|
---|
208 | * @return EOK on success or an error code
|
---|
209 | */
|
---|
210 | static errno_t dummygc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
211 | gfx_coord2_t *offs)
|
---|
212 | {
|
---|
213 | dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
|
---|
214 |
|
---|
215 | tbm->dgc->bm_rendered = true;
|
---|
216 |
|
---|
217 | tbm->dgc->bm_srect.p0.x = 0;
|
---|
218 | tbm->dgc->bm_srect.p0.y = 0;
|
---|
219 | tbm->dgc->bm_srect.p1.x = 0;
|
---|
220 | tbm->dgc->bm_srect.p1.y = 0;
|
---|
221 |
|
---|
222 | tbm->dgc->bm_offs.x = 0;
|
---|
223 | tbm->dgc->bm_offs.y = 0;
|
---|
224 |
|
---|
225 | if (srect != NULL)
|
---|
226 | tbm->dgc->bm_srect = *srect;
|
---|
227 |
|
---|
228 | if (offs != NULL)
|
---|
229 | tbm->dgc->bm_offs = *offs;
|
---|
230 |
|
---|
231 | return EOK;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /** Get bitmap allocation info on dummy GC
|
---|
235 | *
|
---|
236 | * @param bm Bitmap
|
---|
237 | * @param alloc Place to store allocation info
|
---|
238 | * @return EOK on success or an error code
|
---|
239 | */
|
---|
240 | static errno_t dummygc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
241 | {
|
---|
242 | dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
|
---|
243 | *alloc = tbm->alloc;
|
---|
244 | tbm->dgc->bm_got_alloc = true;
|
---|
245 | return EOK;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /** @}
|
---|
249 | */
|
---|