summaryrefslogtreecommitdiff
path: root/gdk/gdkcairocontext.c
blob: 34d3c7fa1765d5931214db998cac4fa7c9bb2a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* GDK - The GIMP Drawing Kit
 *
 * gdkcairocontext.c: Cairo wrappers
 *
 * Copyright © 2018  Benjamin Otte
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gdkcairocontext.h"

#include "gdkcairocontextprivate.h"

#include "gdkcairo.h"
#include "gdkinternals.h"

/**
 * SECTION:gdkcairocontext
 * @Title: GdkCairoContext
 * @Short_description: Cairo draw context
 *
 * #GdkCairoContext is an object representing the platform-specific
 * draw context.
 *
 * #GdkCairoContexts are created for a #GdkDisplay using
 * gdk_surface_create_cairo_context(), and the context can then be used
 * to draw on that #GdkSurface.
 */

/**
 * GdkCairoContext:
 *
 * The GdkCairoContext struct contains only private fields and should not
 * be accessed directly.
 */

typedef struct _GdkCairoContextPrivate GdkCairoContextPrivate;

struct _GdkCairoContextPrivate {
  gpointer unused;
};

G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GdkCairoContext, gdk_cairo_context, GDK_TYPE_DRAW_CONTEXT,
                                  G_ADD_PRIVATE (GdkCairoContext))

static cairo_surface_t *
gdk_surface_ref_impl_surface (GdkSurface *surface)
{
  return GDK_SURFACE_IMPL_GET_CLASS (surface->impl)->ref_cairo_surface (surface);
}

static cairo_content_t
gdk_surface_get_content (GdkSurface *surface)
{
  cairo_surface_t *cairo_surface;
  cairo_content_t content;

  g_return_val_if_fail (GDK_IS_SURFACE (surface), 0);

  cairo_surface = gdk_surface_ref_impl_surface (surface);
  content = cairo_surface_get_content (cairo_surface);
  cairo_surface_destroy (cairo_surface);

  return content;
}

static cairo_t *
gdk_cairo_context_default_cairo_create (GdkCairoContext *self)
{
  GdkDrawContext *context;
  GdkSurface *surface;
  cairo_surface_t *cairo_surface;
  cairo_t *cr;

  g_return_val_if_fail (GDK_IS_CAIRO_CONTEXT (self), NULL);

  context = GDK_DRAW_CONTEXT (self);
  if (!gdk_draw_context_is_drawing (context))
    return NULL;

  surface = gdk_draw_context_get_surface (context);
  cairo_surface = _gdk_surface_ref_cairo_surface (surface);
  cr = cairo_create (cairo_surface);

  gdk_cairo_region (cr, gdk_draw_context_get_frame_region (context));
  cairo_clip (cr);

  cairo_surface_destroy (cairo_surface);

  return cr;
}

static void
gdk_surface_clear_backing_region (GdkSurface *surface)
{
  cairo_t *cr;

  if (GDK_SURFACE_DESTROYED (surface))
    return;

  cr = cairo_create (surface->current_paint.surface);

  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  gdk_cairo_region (cr, surface->current_paint.region);
  cairo_fill (cr);

  cairo_destroy (cr);
}

static void
gdk_surface_free_current_paint (GdkSurface *surface)
{
  cairo_surface_destroy (surface->current_paint.surface);
  surface->current_paint.surface = NULL;

  cairo_region_destroy (surface->current_paint.region);
  surface->current_paint.region = NULL;

  surface->current_paint.surface_needs_composite = FALSE;
}

static void
gdk_cairo_context_begin_frame (GdkDrawContext *draw_context,
                               cairo_region_t *region)
{
  GdkRectangle clip_box;
  GdkSurface *surface;
  GdkSurfaceImplClass *impl_class;
  double sx, sy;
  gboolean needs_surface;
  cairo_content_t surface_content;

  surface = gdk_draw_context_get_surface (draw_context);
  if (surface->current_paint.surface != NULL)
    {
      g_warning ("A paint operation on the surface is alredy in progress. "
                 "This is not allowed.");
      return;
    }

  impl_class = GDK_SURFACE_IMPL_GET_CLASS (surface->impl);

  needs_surface = TRUE;
  if (impl_class->begin_paint)
    needs_surface = impl_class->begin_paint (surface);

  surface->current_paint.region = cairo_region_copy (region);
  cairo_region_get_extents (surface->current_paint.region, &clip_box);

  surface_content = gdk_surface_get_content (surface);

  if (needs_surface)
    {
      surface->current_paint.surface = gdk_surface_create_similar_surface (surface,
                                                                           surface_content,
                                                                           MAX (clip_box.width, 1),
                                                                           MAX (clip_box.height, 1));
      sx = sy = 1;
      cairo_surface_get_device_scale (surface->current_paint.surface, &sx, &sy);
      cairo_surface_set_device_offset (surface->current_paint.surface, -clip_box.x*sx, -clip_box.y*sy);

      surface->current_paint.surface_needs_composite = TRUE;
    }
  else
    {
      surface->current_paint.surface = gdk_surface_ref_impl_surface (surface);
      surface->current_paint.surface_needs_composite = FALSE;
    }

  if (!cairo_region_is_empty (surface->current_paint.region))
    gdk_surface_clear_backing_region (surface);
}

static void
gdk_cairo_context_end_frame (GdkDrawContext *draw_context,
                             cairo_region_t *painted,
                             cairo_region_t *damage)
{
  GdkSurfaceImplClass *impl_class;
  GdkSurface *surface;
  cairo_t *cr;

  surface = gdk_draw_context_get_surface (draw_context);
  if (surface->current_paint.surface == NULL)
    {
      g_warning (G_STRLOC": no preceding call to gdk_draw_context_end_frame(), see documentation");
      return;
    }

  impl_class = GDK_SURFACE_IMPL_GET_CLASS (surface->impl);

  if (impl_class->end_paint)
    impl_class->end_paint (surface);

  if (surface->current_paint.surface_needs_composite)
    {
      cairo_surface_t *cairo_surface;

      cairo_surface = gdk_surface_ref_impl_surface (surface);
      cr = cairo_create (cairo_surface);

      cairo_set_source_surface (cr, surface->current_paint.surface, 0, 0);
      gdk_cairo_region (cr, surface->current_paint.region);
      cairo_clip (cr);

      cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
      cairo_paint (cr);

      cairo_destroy (cr);

      cairo_surface_flush (cairo_surface);
      cairo_surface_destroy (cairo_surface);
    }

  gdk_surface_free_current_paint (surface);
}

static void
gdk_cairo_context_surface_resized (GdkDrawContext *draw_context)
{
}

static void
gdk_cairo_context_class_init (GdkCairoContextClass *klass)
{
  GdkDrawContextClass *draw_context_class = GDK_DRAW_CONTEXT_CLASS (klass);

  draw_context_class->begin_frame = gdk_cairo_context_begin_frame;
  draw_context_class->end_frame = gdk_cairo_context_end_frame;
  draw_context_class->surface_resized = gdk_cairo_context_surface_resized;

  klass->cairo_create = gdk_cairo_context_default_cairo_create;
}

static void
gdk_cairo_context_init (GdkCairoContext *self)
{
}

/**
 * gdk_cairo_context_cairo_create:
 * @context: a #GdkCairoContext that is currently drawing
 *
 * Retrieves a Cairo context to be used to draw on the #GdkSurface
 * of @context. A call to gdk_draw_context_begin_frame() with this
 * @context must have been done or this function will return %NULL.
 *
 * The returned context is guaranteed to be valid until
 * gdk_draw_context_end_frame() is called.
 *
 * Returns: (transfer full) (nullable): a Cairo context to be used
 *   to draw the contents of the #GdkSurface. %NULL is returned
 *   when @contet is not drawing.
 */
cairo_t *
gdk_cairo_context_cairo_create (GdkCairoContext *self)
{
  return GDK_CAIRO_CONTEXT_GET_CLASS (self)->cairo_create (self);
}