source: mainline/uspace/lib/ui/src/dummygc.c@ 8009dc27

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8009dc27 was f7a90df, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Prototype UI and window classes

It is difficult to do any useful testing on ui_window without a real
display_t. Without display, there is no window, without window no GC,
without GC no UI resource.

As a compromise, in case of unit test, create UI with display == NULL,
in which case create window with NULL dwindow and a dummy GC
(so we have valid UI resource).

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