1 | /*
|
---|
2 | * Copyright (c) 2019 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 display
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file GFX window backend
|
---|
34 | *
|
---|
35 | * This implements a graphics context over display server window.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <gfx/bitmap.h>
|
---|
39 | #include <gfx/color.h>
|
---|
40 | #include <gfx/coord.h>
|
---|
41 | #include <gfx/context.h>
|
---|
42 | #include <gfx/render.h>
|
---|
43 | #include <io/log.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include "client.h"
|
---|
46 | #include "display.h"
|
---|
47 | #include "window.h"
|
---|
48 |
|
---|
49 | static errno_t ds_window_set_color(void *, gfx_color_t *);
|
---|
50 | static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
|
---|
51 | static errno_t ds_window_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
52 | gfx_bitmap_alloc_t *, void **);
|
---|
53 | static errno_t ds_window_bitmap_destroy(void *);
|
---|
54 | static errno_t ds_window_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
55 | static errno_t ds_window_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
56 |
|
---|
57 | gfx_context_ops_t ds_window_ops = {
|
---|
58 | .set_color = ds_window_set_color,
|
---|
59 | .fill_rect = ds_window_fill_rect,
|
---|
60 | .bitmap_create = ds_window_bitmap_create,
|
---|
61 | .bitmap_destroy = ds_window_bitmap_destroy,
|
---|
62 | .bitmap_render = ds_window_bitmap_render,
|
---|
63 | .bitmap_get_alloc = ds_window_bitmap_get_alloc
|
---|
64 | };
|
---|
65 |
|
---|
66 | /** Set color on window GC.
|
---|
67 | *
|
---|
68 | * Set drawing color on window GC.
|
---|
69 | *
|
---|
70 | * @param arg Console GC
|
---|
71 | * @param color Color
|
---|
72 | *
|
---|
73 | * @return EOK on success or an error code
|
---|
74 | */
|
---|
75 | static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
|
---|
76 | {
|
---|
77 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
78 |
|
---|
79 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_set_color gc=%p", wnd->client->display->gc);
|
---|
80 | return gfx_set_color(wnd->client->display->gc, color);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /** Fill rectangle on window GC.
|
---|
84 | *
|
---|
85 | * @param arg Window GC
|
---|
86 | * @param rect Rectangle
|
---|
87 | *
|
---|
88 | * @return EOK on success or an error code
|
---|
89 | */
|
---|
90 | static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
91 | {
|
---|
92 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
93 | gfx_rect_t drect;
|
---|
94 |
|
---|
95 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
|
---|
96 | gfx_rect_translate(&wnd->dpos, rect, &drect);
|
---|
97 | return gfx_fill_rect(wnd->client->display->gc, &drect);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Create bitmap in canvas GC.
|
---|
101 | *
|
---|
102 | * @param arg Canvas GC
|
---|
103 | * @param params Bitmap params
|
---|
104 | * @param alloc Bitmap allocation info or @c NULL
|
---|
105 | * @param rbm Place to store pointer to new bitmap
|
---|
106 | * @return EOK on success or an error code
|
---|
107 | */
|
---|
108 | errno_t ds_window_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
109 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
110 | {
|
---|
111 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
112 | ds_window_bitmap_t *cbm = NULL;
|
---|
113 | errno_t rc;
|
---|
114 |
|
---|
115 | cbm = calloc(1, sizeof(ds_window_bitmap_t));
|
---|
116 | if (cbm == NULL)
|
---|
117 | return ENOMEM;
|
---|
118 |
|
---|
119 | rc = gfx_bitmap_create(wnd->client->display->gc, params, alloc,
|
---|
120 | &cbm->bitmap);
|
---|
121 | if (rc != EOK)
|
---|
122 | goto error;
|
---|
123 |
|
---|
124 | cbm->wnd = wnd;
|
---|
125 | *rbm = (void *)cbm;
|
---|
126 | return EOK;
|
---|
127 | error:
|
---|
128 | if (cbm != NULL)
|
---|
129 | free(cbm);
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Destroy bitmap in canvas GC.
|
---|
134 | *
|
---|
135 | * @param bm Bitmap
|
---|
136 | * @return EOK on success or an error code
|
---|
137 | */
|
---|
138 | static errno_t ds_window_bitmap_destroy(void *bm)
|
---|
139 | {
|
---|
140 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
141 |
|
---|
142 | gfx_bitmap_destroy(cbm->bitmap);
|
---|
143 | free(cbm);
|
---|
144 | return EOK;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /** Render bitmap in canvas GC.
|
---|
148 | *
|
---|
149 | * @param bm Bitmap
|
---|
150 | * @param srect0 Source rectangle or @c NULL
|
---|
151 | * @param offs0 Offset or @c NULL
|
---|
152 | * @return EOK on success or an error code
|
---|
153 | */
|
---|
154 | static errno_t ds_window_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
155 | gfx_coord2_t *offs0)
|
---|
156 | {
|
---|
157 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
158 | gfx_coord2_t doffs;
|
---|
159 |
|
---|
160 | if (offs0 != NULL)
|
---|
161 | gfx_coord2_add(&cbm->wnd->dpos, offs0, &doffs);
|
---|
162 | else
|
---|
163 | doffs = cbm->wnd->dpos;
|
---|
164 |
|
---|
165 | return gfx_bitmap_render(cbm->bitmap, srect0, &doffs);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Get allocation info for bitmap in canvas GC.
|
---|
169 | *
|
---|
170 | * @param bm Bitmap
|
---|
171 | * @param alloc Place to store allocation info
|
---|
172 | * @return EOK on success or an error code
|
---|
173 | */
|
---|
174 | static errno_t ds_window_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
175 | {
|
---|
176 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
177 |
|
---|
178 | return gfx_bitmap_get_alloc(cbm->bitmap, alloc);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Create window.
|
---|
182 | *
|
---|
183 | * Create graphics context for rendering into a window.
|
---|
184 | *
|
---|
185 | * @param client Client owning the window
|
---|
186 | * @param rgc Place to store pointer to new GC.
|
---|
187 | *
|
---|
188 | * @return EOK on success or an error code
|
---|
189 | */
|
---|
190 | errno_t ds_window_create(ds_client_t *client, ds_window_t **rgc)
|
---|
191 | {
|
---|
192 | ds_window_t *wnd = NULL;
|
---|
193 | gfx_context_t *gc = NULL;
|
---|
194 | errno_t rc;
|
---|
195 |
|
---|
196 | wnd = calloc(1, sizeof(ds_window_t));
|
---|
197 | if (wnd == NULL) {
|
---|
198 | rc = ENOMEM;
|
---|
199 | goto error;
|
---|
200 | }
|
---|
201 |
|
---|
202 | rc = gfx_context_new(&ds_window_ops, wnd, &gc);
|
---|
203 | if (rc != EOK)
|
---|
204 | goto error;
|
---|
205 |
|
---|
206 | ds_client_add_window(client, wnd);
|
---|
207 | ds_display_add_window(client->display, wnd);
|
---|
208 |
|
---|
209 | wnd->gc = gc;
|
---|
210 | *rgc = wnd;
|
---|
211 | return EOK;
|
---|
212 | error:
|
---|
213 | if (wnd != NULL)
|
---|
214 | free(wnd);
|
---|
215 | gfx_context_delete(gc);
|
---|
216 | return rc;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Delete window GC.
|
---|
220 | *
|
---|
221 | * @param wnd Window GC
|
---|
222 | */
|
---|
223 | void ds_window_destroy(ds_window_t *wnd)
|
---|
224 | {
|
---|
225 | ds_client_remove_window(wnd);
|
---|
226 | ds_display_remove_window(wnd);
|
---|
227 | (void) gfx_context_delete(wnd->gc);
|
---|
228 |
|
---|
229 | free(wnd);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Get generic graphic context from window.
|
---|
233 | *
|
---|
234 | * @param wnd Window
|
---|
235 | * @return Graphic context
|
---|
236 | */
|
---|
237 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
---|
238 | {
|
---|
239 | return wnd->gc;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /** @}
|
---|
243 | */
|
---|