source: mainline/uspace/lib/guigfx/src/canvas.c@ cea9f0c

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

Guigfx can use memory GC

  • Property mode set to 100644
File size: 6.7 KB
Line 
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 libguigfx
30 * @{
31 */
32/**
33 * @file GFX canvas backend
34 *
35 * This implements a graphics context over a libgui canvas.
36 * This is just for experimentation purposes and its kind of backwards.
37 */
38
39#include <draw/drawctx.h>
40#include <draw/source.h>
41#include <gfx/color.h>
42#include <gfx/context.h>
43#include <gfx/render.h>
44#include <guigfx/canvas.h>
45#include <io/pixel.h>
46#include <stdlib.h>
47#include <transform.h>
48#include "../private/canvas.h"
49//#include "../../private/color.h"
50
51static errno_t canvas_gc_set_color(void *, gfx_color_t *);
52static errno_t canvas_gc_fill_rect(void *, gfx_rect_t *);
53static errno_t canvas_gc_bitmap_create(void *, gfx_bitmap_params_t *,
54 gfx_bitmap_alloc_t *, void **);
55static errno_t canvas_gc_bitmap_destroy(void *);
56static errno_t canvas_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
57static errno_t canvas_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
58
59gfx_context_ops_t canvas_gc_ops = {
60 .set_color = canvas_gc_set_color,
61 .fill_rect = canvas_gc_fill_rect,
62 .bitmap_create = canvas_gc_bitmap_create,
63 .bitmap_destroy = canvas_gc_bitmap_destroy,
64 .bitmap_render = canvas_gc_bitmap_render,
65 .bitmap_get_alloc = canvas_gc_bitmap_get_alloc
66};
67
68/** Set color on canvas GC.
69 *
70 * Set drawing color on canvas GC.
71 *
72 * @param arg Canvas GC
73 * @param color Color
74 *
75 * @return EOK on success or an error code
76 */
77static errno_t canvas_gc_set_color(void *arg, gfx_color_t *color)
78{
79 canvas_gc_t *cgc = (canvas_gc_t *) arg;
80
81 return gfx_set_color(cgc->mbgc, color);
82}
83
84/** Fill rectangle on canvas GC.
85 *
86 * @param arg Canvas GC
87 * @param rect Rectangle
88 *
89 * @return EOK on success or an error code
90 */
91static errno_t canvas_gc_fill_rect(void *arg, gfx_rect_t *rect)
92{
93 canvas_gc_t *cgc = (canvas_gc_t *) arg;
94 errno_t rc;
95
96 rc = gfx_fill_rect(cgc->mbgc, rect);
97 if (rc == EOK)
98 update_canvas(cgc->canvas, cgc->surface);
99
100 return rc;
101}
102
103/** Create canvas GC.
104 *
105 * Create graphics context for rendering into a canvas.
106 *
107 * @param canvas Canvas object
108 * @param fout File to which characters are written (canvas)
109 * @param rgc Place to store pointer to new GC.
110 *
111 * @return EOK on success or an error code
112 */
113errno_t canvas_gc_create(canvas_t *canvas, surface_t *surface,
114 canvas_gc_t **rgc)
115{
116 canvas_gc_t *cgc = NULL;
117 gfx_context_t *gc = NULL;
118 surface_coord_t w, h;
119 gfx_rect_t rect;
120 gfx_bitmap_alloc_t alloc;
121 errno_t rc;
122
123 surface_get_resolution(surface, &w, &h);
124
125 cgc = calloc(1, sizeof(canvas_gc_t));
126 if (cgc == NULL) {
127 rc = ENOMEM;
128 goto error;
129 }
130
131 rc = gfx_context_new(&canvas_gc_ops, cgc, &gc);
132 if (rc != EOK)
133 goto error;
134
135 rect.p0.x = 0;
136 rect.p0.y = 0;
137 rect.p1.x = w;
138 rect.p1.y = h;
139
140 alloc.pitch = w * sizeof(uint32_t);
141 alloc.off0 = 0;
142 alloc.pixels = surface_direct_access(surface);
143
144 rc = mem_gc_create(&rect, &alloc, &cgc->mgc);
145 if (rc != EOK)
146 goto error;
147
148 cgc->mbgc = mem_gc_get_ctx(cgc->mgc);
149
150 cgc->gc = gc;
151 cgc->canvas = canvas;
152 cgc->surface = surface;
153 *rgc = cgc;
154 return EOK;
155error:
156 if (gc != NULL)
157 gfx_context_delete(gc);
158 if (cgc != NULL)
159 free(cgc);
160 return rc;
161}
162
163/** Delete canvas GC.
164 *
165 * @param cgc Canvas GC
166 */
167errno_t canvas_gc_delete(canvas_gc_t *cgc)
168{
169 errno_t rc;
170
171 mem_gc_delete(cgc->mgc);
172
173 rc = gfx_context_delete(cgc->gc);
174 if (rc != EOK)
175 return rc;
176
177 free(cgc);
178 return EOK;
179}
180
181/** Get generic graphic context from canvas GC.
182 *
183 * @param cgc Canvas GC
184 * @return Graphic context
185 */
186gfx_context_t *canvas_gc_get_ctx(canvas_gc_t *cgc)
187{
188 return cgc->gc;
189}
190
191/** Create bitmap in canvas GC.
192 *
193 * @param arg Canvas GC
194 * @param params Bitmap params
195 * @param alloc Bitmap allocation info or @c NULL
196 * @param rbm Place to store pointer to new bitmap
197 * @return EOK on success or an error code
198 */
199errno_t canvas_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
200 gfx_bitmap_alloc_t *alloc, void **rbm)
201{
202 canvas_gc_t *cgc = (canvas_gc_t *) arg;
203 canvas_gc_bitmap_t *cbm = NULL;
204 errno_t rc;
205
206 cbm = calloc(1, sizeof(canvas_gc_bitmap_t));
207 if (cbm == NULL)
208 return ENOMEM;
209
210 rc = gfx_bitmap_create(cgc->mbgc, params, alloc, &cbm->mbitmap);
211 if (rc != EOK)
212 goto error;
213
214 cbm->cgc = cgc;
215 *rbm = (void *)cbm;
216 return EOK;
217error:
218 if (cbm != NULL)
219 free(cbm);
220 return rc;
221}
222
223/** Destroy bitmap in canvas GC.
224 *
225 * @param bm Bitmap
226 * @return EOK on success or an error code
227 */
228static errno_t canvas_gc_bitmap_destroy(void *bm)
229{
230 canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm;
231 errno_t rc;
232
233 rc = gfx_bitmap_destroy(cbm->mbitmap);
234 if (rc != EOK)
235 return rc;
236
237 free(cbm);
238 return EOK;
239}
240
241/** Render bitmap in canvas GC.
242 *
243 * @param bm Bitmap
244 * @param srect0 Source rectangle or @c NULL
245 * @param offs0 Offset or @c NULL
246 * @return EOK on success or an error code
247 */
248static errno_t canvas_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
249 gfx_coord2_t *offs0)
250{
251 canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm;
252 errno_t rc;
253
254 rc = gfx_bitmap_render(cbm->mbitmap, srect0, offs0);
255 if (rc == EOK)
256 update_canvas(cbm->cgc->canvas, cbm->cgc->surface);
257
258 return rc;
259}
260
261/** Get allocation info for bitmap in canvas GC.
262 *
263 * @param bm Bitmap
264 * @param alloc Place to store allocation info
265 * @return EOK on success or an error code
266 */
267static errno_t canvas_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
268{
269 canvas_gc_bitmap_t *cbm = (canvas_gc_bitmap_t *)bm;
270
271 return gfx_bitmap_get_alloc(cbm->mbitmap, alloc);
272}
273
274/** @}
275 */
Note: See TracBrowser for help on using the repository browser.