source: mainline/uspace/lib/ui/private/testgc.h

Last change on this file was 1fa6292, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 5 months ago

Remove a ton of duplicated code in libui/libgfxfont tests

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2022 Jiri Svoboda
3 * Copyright (c) 2025 Jiří Zárevúcky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30typedef struct {
31 bool bm_created;
32 bool bm_destroyed;
33 gfx_bitmap_params_t bm_params;
34 void *bm_pixels;
35 gfx_rect_t bm_srect;
36 gfx_coord2_t bm_offs;
37 bool bm_rendered;
38 bool bm_got_alloc;
39} test_gc_t;
40
41typedef struct {
42 test_gc_t *tgc;
43 gfx_bitmap_alloc_t alloc;
44 bool myalloc;
45} testgc_bitmap_t;
46
47static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
48{
49 (void) arg;
50 (void) rect;
51 return EOK;
52}
53
54static errno_t testgc_set_color(void *arg, gfx_color_t *color)
55{
56 (void) arg;
57 (void) color;
58 return EOK;
59}
60
61static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
62{
63 (void) arg;
64 (void) rect;
65 return EOK;
66}
67
68static errno_t testgc_update(void *arg)
69{
70 (void) arg;
71 return EOK;
72}
73
74static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
75 gfx_bitmap_alloc_t *alloc, void **rbm)
76{
77 test_gc_t *tgc = (test_gc_t *) arg;
78 testgc_bitmap_t *tbm;
79
80 tbm = calloc(1, sizeof(testgc_bitmap_t));
81 if (tbm == NULL)
82 return ENOMEM;
83
84 if (alloc == NULL) {
85 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
86 sizeof(uint32_t);
87 tbm->alloc.off0 = 0;
88 tbm->alloc.pixels = calloc(
89 (params->rect.p1.x - params->rect.p0.x) *
90 (params->rect.p1.y - params->rect.p0.y),
91 sizeof(uint32_t));
92 tbm->myalloc = true;
93 if (tbm->alloc.pixels == NULL) {
94 free(tbm);
95 return ENOMEM;
96 }
97 } else {
98 tbm->alloc = *alloc;
99 }
100
101 tbm->tgc = tgc;
102 tgc->bm_created = true;
103 tgc->bm_params = *params;
104 tgc->bm_pixels = tbm->alloc.pixels;
105 *rbm = (void *)tbm;
106 return EOK;
107}
108
109static errno_t testgc_bitmap_destroy(void *bm)
110{
111 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
112 if (tbm->myalloc)
113 free(tbm->alloc.pixels);
114 tbm->tgc->bm_destroyed = true;
115 free(tbm);
116 return EOK;
117}
118
119static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
120 gfx_coord2_t *offs)
121{
122 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
123 tbm->tgc->bm_rendered = true;
124 tbm->tgc->bm_srect = *srect;
125 tbm->tgc->bm_offs = *offs;
126 return EOK;
127}
128
129static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
130{
131 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
132 *alloc = tbm->alloc;
133 tbm->tgc->bm_got_alloc = true;
134 return EOK;
135}
136
137static gfx_context_ops_t ops = {
138 .set_clip_rect = testgc_set_clip_rect,
139 .set_color = testgc_set_color,
140 .fill_rect = testgc_fill_rect,
141 .update = testgc_update,
142 .bitmap_create = testgc_bitmap_create,
143 .bitmap_destroy = testgc_bitmap_destroy,
144 .bitmap_render = testgc_bitmap_render,
145 .bitmap_get_alloc = testgc_bitmap_get_alloc
146};
Note: See TracBrowser for help on using the repository browser.