Debian Codegraph @

gdk/broadway/gdkcairocontext-broadway.c

raw symbols SCIP
1 /* GDK - The GIMP Drawing Kit
2 *
3 * Copyright © 2018 Benjamin Otte
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20
21 #include "gdkconfig.h"
22
23 #include "gdkcairocontext-broadway.h"
24 #include "gdktextureprivate.h"
25 #include "gdkprivate-broadway.h"
26
27 G_DEFINE_TYPE (GdkBroadwayCairoContext, gdk_broadway_cairo_context, GDK_TYPE_CAIRO_CONTEXT)
28
29 static void
30 gdk_broadway_cairo_context_dispose (GObject *object)
31 {
32 G_OBJECT_CLASS (gdk_broadway_cairo_context_parent_class)->dispose (object);
33 }
34
35 static void
36 gdk_broadway_cairo_context_begin_frame (GdkDrawContext *draw_context,
37 gpointer context_data,
38 GdkMemoryDepth depth,
39 cairo_region_t *region,
40 GdkColorState **out_color_state,
41 GdkMemoryDepth *out_depth)
42 {
43 GdkBroadwayCairoContext *self = GDK_BROADWAY_CAIRO_CONTEXT (draw_context);
44 cairo_t *cr;
45 cairo_region_t *repaint_region;
46 guint width, height;
47
48 gdk_draw_context_get_buffer_size (draw_context, &width, &height);
49 self->paint_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
50
51 repaint_region = cairo_region_create_rectangle (&(cairo_rectangle_int_t) { 0, 0, width, height });
52 cairo_region_union (region, repaint_region);
53 cairo_region_destroy (repaint_region);
54
55 /* clear the repaint area */
56 cr = cairo_create (self->paint_surface);
57 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
58 cairo_fill (cr);
59 cairo_destroy (cr);
60
61 *out_color_state = GDK_COLOR_STATE_SRGB;
62 *out_depth = gdk_color_state_get_depth (GDK_COLOR_STATE_SRGB);
63 }
64
65 static void
66 add_uint32 (GArray *nodes, guint32 v)
67 {
68 g_array_append_val (nodes, v);
69 }
70
71 static void
72 add_float (GArray *nodes, float f)
73 {
74 gint32 i = (gint32) (f * 256.0f);
75 guint u = (guint32) i;
76 g_array_append_val (nodes, u);
77 }
78
79 static void
80 gdk_broadway_cairo_context_end_frame (GdkDrawContext *draw_context,
81 gpointer context_data,
82 cairo_region_t *painted)
83 {
84 GdkBroadwayCairoContext *self = GDK_BROADWAY_CAIRO_CONTEXT (draw_context);
85 GdkDisplay *display = gdk_draw_context_get_display (draw_context);
86 GdkSurface *surface = gdk_draw_context_get_surface (draw_context);
87 GdkTexture *texture;
88 GPtrArray *node_textures;
89 GArray *nodes;
90 guint32 texture_id;
91
92 nodes = g_array_new (FALSE, FALSE, sizeof(guint32));
93 node_textures = g_ptr_array_new_with_free_func (g_object_unref);
94
95 texture = gdk_texture_new_for_surface ((cairo_surface_t *)self->paint_surface);
96 g_ptr_array_add (node_textures, g_object_ref (texture)); /* Transfers ownership to node_textures */
97 texture_id = gdk_broadway_display_ensure_texture (display, texture);
98
99 add_uint32 (nodes, BROADWAY_NODE_TEXTURE);
100 add_float (nodes, 0);
101 add_float (nodes, 0);
102 add_float (nodes, cairo_image_surface_get_width (self->paint_surface));
103 add_float (nodes, cairo_image_surface_get_height (self->paint_surface));
104 add_uint32 (nodes, texture_id);
105
106 gdk_broadway_surface_set_nodes (surface, nodes, node_textures);
107 g_array_unref (nodes);
108 g_ptr_array_unref (node_textures);
109
110 cairo_surface_destroy (self->paint_surface);
111 self->paint_surface = NULL;
112 }
113
114 static void
115 gdk_broadway_cairo_context_surface_resized (GdkDrawContext *draw_context)
116 {
117 }
118
119 static cairo_t *
120 gdk_broadway_cairo_context_cairo_create (GdkCairoContext *context)
121 {
122 GdkBroadwayCairoContext *self = GDK_BROADWAY_CAIRO_CONTEXT (context);
123
124 return cairo_create (self->paint_surface);
125 }
126
127 static void
128 gdk_broadway_cairo_context_class_init (GdkBroadwayCairoContextClass *klass)
129 {
130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131 GdkDrawContextClass *draw_context_class = GDK_DRAW_CONTEXT_CLASS (klass);
132 GdkCairoContextClass *cairo_context_class = GDK_CAIRO_CONTEXT_CLASS (klass);
133
134 gobject_class->dispose = gdk_broadway_cairo_context_dispose;
135
136 draw_context_class->begin_frame = gdk_broadway_cairo_context_begin_frame;
137 draw_context_class->end_frame = gdk_broadway_cairo_context_end_frame;
138 draw_context_class->surface_resized = gdk_broadway_cairo_context_surface_resized;
139
140 cairo_context_class->cairo_create = gdk_broadway_cairo_context_cairo_create;
141 }
142
143 static void
144 gdk_broadway_cairo_context_init (GdkBroadwayCairoContext *self)
145 {
146 }