source: mainline/uspace/lib/ui/src/dummygc.c@ 901b302

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 901b302 was 7470d97, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Add GC operation to set clipping rectangle

The number of changed files is due to the proliferation of GC
implementations, mostly these are just dummies in unit tests.
Definitely need to tame those in the future.

  • Property mode set to 100644
File size: 6.5 KB
Line 
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
43static errno_t dummygc_set_clip_rect(void *, gfx_rect_t *);
44static errno_t dummygc_set_color(void *, gfx_color_t *);
45static errno_t dummygc_fill_rect(void *, gfx_rect_t *);
46static errno_t dummygc_update(void *);
47static errno_t dummygc_bitmap_create(void *, gfx_bitmap_params_t *,
48 gfx_bitmap_alloc_t *, void **);
49static errno_t dummygc_bitmap_destroy(void *);
50static errno_t dummygc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
51static errno_t dummygc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
52
53/** Dummy GC operations */
54gfx_context_ops_t dummygc_ops = {
55 .set_clip_rect = dummygc_set_clip_rect,
56 .set_color = dummygc_set_color,
57 .fill_rect = dummygc_fill_rect,
58 .update = dummygc_update,
59 .bitmap_create = dummygc_bitmap_create,
60 .bitmap_destroy = dummygc_bitmap_destroy,
61 .bitmap_render = dummygc_bitmap_render,
62 .bitmap_get_alloc = dummygc_bitmap_get_alloc
63};
64
65/** Create dummy GC.
66 *
67 * @param rdgc Place to store pointer to new dummy GC
68 * @return EOK on success, ENOMEM if out of memory
69 */
70errno_t dummygc_create(dummy_gc_t **rdgc)
71{
72 dummy_gc_t *dgc;
73 gfx_context_t *gc;
74 errno_t rc;
75
76 dgc = calloc(1, sizeof(dummy_gc_t));
77 if (dgc == NULL)
78 return ENOMEM;
79
80 rc = gfx_context_new(&dummygc_ops, dgc, &gc);
81 if (rc != EOK) {
82 free(dgc);
83 return rc;
84 }
85
86 dgc->gc = gc;
87 *rdgc = dgc;
88 return EOK;
89}
90
91/** Destroy dummy GC.
92 *
93 * @param dgc Dummy GC
94 */
95void dummygc_destroy(dummy_gc_t *dgc)
96{
97 gfx_context_delete(dgc->gc);
98 free(dgc);
99}
100
101/** Get generic graphic context from dummy GC.
102 *
103 * @param dgc Dummy GC
104 * @return Graphic context
105 */
106gfx_context_t *dummygc_get_ctx(dummy_gc_t *dgc)
107{
108 return dgc->gc;
109}
110
111/** Set clipping rectangle on dummy GC
112 *
113 * @param arg Argument (dummy_gc_t)
114 * @param rect Rectangle
115 * @return EOK on success or an error code
116 */
117static errno_t dummygc_set_clip_rect(void *arg, gfx_rect_t *rect)
118{
119 (void) arg;
120 (void) rect;
121 return EOK;
122}
123
124/** Set color on dummy GC
125 *
126 * @param arg Argument (dummy_gc_t)
127 * @param color Color
128 * @return EOK on success or an error code
129 */
130static errno_t dummygc_set_color(void *arg, gfx_color_t *color)
131{
132 (void) arg;
133 (void) color;
134 return EOK;
135}
136
137/** Fill rectangle on dummy GC
138 *
139 * @param arg Argument (dummy_gc_t)
140 * @param rect Rectangle
141 * @return EOK on success or an error code
142 */
143static errno_t dummygc_fill_rect(void *arg, gfx_rect_t *rect)
144{
145 (void) arg;
146 (void) rect;
147 return EOK;
148}
149
150/** Update dummy GC
151 *
152 * @param arg Argument (dummy_gc_t)
153 * @return EOK on success or an error code
154 */
155static errno_t dummygc_update(void *arg)
156{
157 (void) arg;
158 return EOK;
159}
160
161/** Create bitmap on dummy GC
162 *
163 * @param arg Argument (dummy_gc_t)
164 * @param params Bitmap parameters
165 * @param alloc Bitmap allocation info or @c NULL
166 * @param rbm Place to store pointer to new bitmap
167 * @return EOK on success or an error code
168 */
169static errno_t dummygc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
170 gfx_bitmap_alloc_t *alloc, void **rbm)
171{
172 dummy_gc_t *dgc = (dummy_gc_t *) arg;
173 dummygc_bitmap_t *tbm;
174
175 tbm = calloc(1, sizeof(dummygc_bitmap_t));
176 if (tbm == NULL)
177 return ENOMEM;
178
179 if (alloc == NULL) {
180 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
181 sizeof(uint32_t);
182 tbm->alloc.off0 = 0;
183 tbm->alloc.pixels = calloc(sizeof(uint32_t),
184 (params->rect.p1.x - params->rect.p0.x) *
185 (params->rect.p1.y - params->rect.p0.y));
186 tbm->myalloc = true;
187 if (tbm->alloc.pixels == NULL) {
188 free(tbm);
189 return ENOMEM;
190 }
191 } else {
192 tbm->alloc = *alloc;
193 }
194
195 tbm->dgc = dgc;
196 dgc->bm_created = true;
197 dgc->bm_params = *params;
198 dgc->bm_pixels = tbm->alloc.pixels;
199 *rbm = (void *)tbm;
200 return EOK;
201}
202
203/** Destroy bitmap on dummy GC
204 *
205 * @param bm Bitmap
206 * @return EOK on success or an error code
207 */
208static errno_t dummygc_bitmap_destroy(void *bm)
209{
210 dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
211 if (tbm->myalloc)
212 free(tbm->alloc.pixels);
213 tbm->dgc->bm_destroyed = true;
214 free(tbm);
215 return EOK;
216}
217
218/** Render bitmap on dummy GC
219 *
220 * @param bm Bitmap
221 * @param srect Source rectangle or @c NULL
222 * @param offs Offset or @c NULL
223 * @return EOK on success or an error code
224 */
225static errno_t dummygc_bitmap_render(void *bm, gfx_rect_t *srect,
226 gfx_coord2_t *offs)
227{
228 dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
229
230 tbm->dgc->bm_rendered = true;
231
232 tbm->dgc->bm_srect.p0.x = 0;
233 tbm->dgc->bm_srect.p0.y = 0;
234 tbm->dgc->bm_srect.p1.x = 0;
235 tbm->dgc->bm_srect.p1.y = 0;
236
237 tbm->dgc->bm_offs.x = 0;
238 tbm->dgc->bm_offs.y = 0;
239
240 if (srect != NULL)
241 tbm->dgc->bm_srect = *srect;
242
243 if (offs != NULL)
244 tbm->dgc->bm_offs = *offs;
245
246 return EOK;
247}
248
249/** Get bitmap allocation info on dummy GC
250 *
251 * @param bm Bitmap
252 * @param alloc Place to store allocation info
253 * @return EOK on success or an error code
254 */
255static errno_t dummygc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
256{
257 dummygc_bitmap_t *tbm = (dummygc_bitmap_t *)bm;
258 *alloc = tbm->alloc;
259 tbm->dgc->bm_got_alloc = true;
260 return EOK;
261}
262
263/** @}
264 */
Note: See TracBrowser for help on using the repository browser.