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

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

Communicate memory GC updates via callback function

This is what we want in most use cases. Allows us to expose memory GC
ops directly without interposing on them (greatly simplifying the code).
The previous behavior is easily achieved by supplying the right callback
function.

  • Property mode set to 100644
File size: 3.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 void canvas_gc_update_cb(void *, gfx_rect_t *);
52
53/** Create canvas GC.
54 *
55 * Create graphics context for rendering into a canvas.
56 *
57 * @param canvas Canvas object
58 * @param fout File to which characters are written (canvas)
59 * @param rgc Place to store pointer to new GC.
60 *
61 * @return EOK on success or an error code
62 */
63errno_t canvas_gc_create(canvas_t *canvas, surface_t *surface,
64 canvas_gc_t **rgc)
65{
66 canvas_gc_t *cgc = NULL;
67 surface_coord_t w, h;
68 gfx_rect_t rect;
69 gfx_bitmap_alloc_t alloc;
70 errno_t rc;
71
72 surface_get_resolution(surface, &w, &h);
73
74 cgc = calloc(1, sizeof(canvas_gc_t));
75 if (cgc == NULL) {
76 rc = ENOMEM;
77 goto error;
78 }
79
80 rect.p0.x = 0;
81 rect.p0.y = 0;
82 rect.p1.x = w;
83 rect.p1.y = h;
84
85 alloc.pitch = w * sizeof(uint32_t);
86 alloc.off0 = 0;
87 alloc.pixels = surface_direct_access(surface);
88
89 rc = mem_gc_create(&rect, &alloc, canvas_gc_update_cb,
90 (void *)cgc, &cgc->mgc);
91 if (rc != EOK)
92 goto error;
93
94 cgc->gc = mem_gc_get_ctx(cgc->mgc);
95
96 cgc->canvas = canvas;
97 cgc->surface = surface;
98 *rgc = cgc;
99 return EOK;
100error:
101 if (cgc != NULL)
102 free(cgc);
103 return rc;
104}
105
106/** Delete canvas GC.
107 *
108 * @param cgc Canvas GC
109 */
110errno_t canvas_gc_delete(canvas_gc_t *cgc)
111{
112 errno_t rc;
113
114 mem_gc_delete(cgc->mgc);
115
116 rc = gfx_context_delete(cgc->gc);
117 if (rc != EOK)
118 return rc;
119
120 free(cgc);
121 return EOK;
122}
123
124/** Get generic graphic context from canvas GC.
125 *
126 * @param cgc Canvas GC
127 * @return Graphic context
128 */
129gfx_context_t *canvas_gc_get_ctx(canvas_gc_t *cgc)
130{
131 return cgc->gc;
132}
133
134/** Canvas GC update callback called by memory GC.
135 *
136 * @param arg Canvas GC
137 * @param rect Rectangle to update
138 */
139static void canvas_gc_update_cb(void *arg, gfx_rect_t *rect)
140{
141 canvas_gc_t *cgc = (canvas_gc_t *)arg;
142
143 update_canvas(cgc->canvas, cgc->surface);
144}
145
146/** @}
147 */
Note: See TracBrowser for help on using the repository browser.