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

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

Remove a ton of duplicated code in libui/libgfxfont tests

  • Property mode set to 100644
File size: 3.5 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 gfx_bitmap_params_t bm_params;
32 void *bm_pixels;
33 gfx_rect_t bm_srect;
34 gfx_coord2_t bm_offs;
35} test_gc_t;
36
37typedef struct {
38 test_gc_t *tgc;
39 gfx_bitmap_alloc_t alloc;
40 bool myalloc;
41} testgc_bitmap_t;
42
43static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
44{
45 return EOK;
46}
47
48static errno_t testgc_set_color(void *arg, gfx_color_t *color)
49{
50 return EOK;
51}
52
53static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
54{
55 return EOK;
56}
57
58static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
59 gfx_bitmap_alloc_t *alloc, void **rbm)
60{
61 test_gc_t *tgc = (test_gc_t *) arg;
62 testgc_bitmap_t *tbm;
63
64 tbm = calloc(1, sizeof(testgc_bitmap_t));
65 if (tbm == NULL)
66 return ENOMEM;
67
68 if (alloc == NULL) {
69 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
70 sizeof(uint32_t);
71 tbm->alloc.off0 = 0;
72 tbm->alloc.pixels = calloc(
73 tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y),
74 sizeof(uint32_t));
75 tbm->myalloc = true;
76 if (tbm->alloc.pixels == NULL) {
77 free(tbm);
78 return ENOMEM;
79 }
80 } else {
81 tbm->alloc = *alloc;
82 }
83
84 tbm->tgc = tgc;
85 tgc->bm_params = *params;
86 tgc->bm_pixels = tbm->alloc.pixels;
87 *rbm = (void *)tbm;
88 return EOK;
89}
90
91static errno_t testgc_bitmap_destroy(void *bm)
92{
93 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
94 if (tbm->myalloc)
95 free(tbm->alloc.pixels);
96 free(tbm);
97 return EOK;
98}
99
100static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
101 gfx_coord2_t *offs)
102{
103 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
104 tbm->tgc->bm_srect = *srect;
105 tbm->tgc->bm_offs = *offs;
106 return EOK;
107}
108
109static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
110{
111 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
112 *alloc = tbm->alloc;
113 return EOK;
114}
115
116static gfx_context_ops_t test_ops = {
117 .set_clip_rect = testgc_set_clip_rect,
118 .set_color = testgc_set_color,
119 .fill_rect = testgc_fill_rect,
120 .bitmap_create = testgc_bitmap_create,
121 .bitmap_destroy = testgc_bitmap_destroy,
122 .bitmap_render = testgc_bitmap_render,
123 .bitmap_get_alloc = testgc_bitmap_get_alloc
124};
Note: See TracBrowser for help on using the repository browser.