summaryrefslogtreecommitdiff
path: root/gdk/macos/gdkmacoscairocontext.c
blob: 31eecd5df682ab2bd94c48af28007709463677a8 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright © 2016 Benjamin Otte
 * Copyright © 2020 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "gdkconfig.h"

#include <cairo.h>
#include <QuartzCore/QuartzCore.h>
#include <CoreGraphics/CoreGraphics.h>

#include "gdkmacosbuffer-private.h"
#include "gdkmacoscairocontext-private.h"
#include "gdkmacossurface-private.h"

struct _GdkMacosCairoContext
{
  GdkCairoContext parent_instance;
};

struct _GdkMacosCairoContextClass
{
  GdkCairoContextClass parent_class;
};

G_DEFINE_TYPE (GdkMacosCairoContext, _gdk_macos_cairo_context, GDK_TYPE_CAIRO_CONTEXT)

static cairo_t *
_gdk_macos_cairo_context_cairo_create (GdkCairoContext *cairo_context)
{
  GdkMacosCairoContext *self = (GdkMacosCairoContext *)cairo_context;
  const cairo_region_t *damage;
  cairo_surface_t *image_surface;
  GdkMacosBuffer *buffer;
  GdkSurface *surface;
  NSWindow *nswindow;
  cairo_t *cr;
  gpointer data;
  double scale;
  guint width;
  guint height;
  guint stride;
  gboolean opaque;

  g_assert (GDK_IS_MACOS_CAIRO_CONTEXT (self));

  surface = gdk_draw_context_get_surface (GDK_DRAW_CONTEXT (self));
  nswindow = _gdk_macos_surface_get_native (GDK_MACOS_SURFACE (surface));
  opaque = [nswindow isOpaque];

  buffer = _gdk_macos_surface_get_buffer (GDK_MACOS_SURFACE (surface));
  damage = _gdk_macos_buffer_get_damage (buffer);
  width = _gdk_macos_buffer_get_width (buffer);
  height = _gdk_macos_buffer_get_height (buffer);
  scale = _gdk_macos_buffer_get_device_scale (buffer);
  stride = _gdk_macos_buffer_get_stride (buffer);
  data = _gdk_macos_buffer_get_data (buffer);

  /* Instead of forcing cairo to do everything through a CGContext,
   * we just use an image surface backed by an IOSurfaceRef mapped
   * into user-space. We can then use pixman which is quite fast as
   * far as software rendering goes.
   *
   * Additionally, cairo_quartz_surface_t can't handle a number of
   * tricks that the GSK cairo renderer does with border nodes and
   * shadows, so an image surface is necessary for that.
   *
   * Since our IOSurfaceRef is width*scale-by-height*scale, we undo
   * the scaling using cairo_surface_set_device_scale() so the renderer
   * just thinks it's on a 2x scale surface for HiDPI.
   */
  image_surface = cairo_image_surface_create_for_data (data,
                                                       CAIRO_FORMAT_ARGB32,
                                                       width,
                                                       height,
                                                       stride);
  cairo_surface_set_device_scale (image_surface, scale, scale);

  /* The buffer should already be locked at this point, and will
   * be unlocked as part of end_frame.
   */

  if (!(cr = cairo_create (image_surface)))
    goto failure;

  /* Clip to the current damage region */
  if (damage != NULL)
    {
      gdk_cairo_region (cr, damage);
      cairo_clip (cr);
    }

  /* If we have some exposed transparent area in the damage region,
   * we need to clear the existing content first to leave an transparent
   * area for cairo. We use (surface_bounds or damage)-(opaque) to get
   * the smallest set of rectangles we need to clear as it's expensive.
   */
  if (!opaque)
    {
      cairo_region_t *transparent;
      cairo_rectangle_int_t r = { 0, 0, width/scale, height/scale };

      cairo_save (cr);

      if (damage != NULL)
        cairo_region_get_extents (damage, &r);
      transparent = cairo_region_create_rectangle (&r);
      if (surface->opaque_region)
        cairo_region_subtract (transparent, surface->opaque_region);

      if (!cairo_region_is_empty (transparent))
        {
          gdk_cairo_region (cr, transparent);
          cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
          cairo_fill (cr);
        }

      cairo_region_destroy (transparent);
      cairo_restore (cr);
    }

failure:
  cairo_surface_destroy (image_surface);

  return cr;
}

static void
copy_surface_data (GdkMacosBuffer       *from,
                   GdkMacosBuffer       *to,
                   const cairo_region_t *region,
                   int                   scale)
{
  const guint8 *from_base;
  guint8 *to_base;
  guint from_stride;
  guint to_stride;
  guint n_rects;

  g_assert (GDK_IS_MACOS_BUFFER (from));
  g_assert (GDK_IS_MACOS_BUFFER (to));
  g_assert (region != NULL);
  g_assert (!cairo_region_is_empty (region));

  from_base = _gdk_macos_buffer_get_data (from);
  from_stride = _gdk_macos_buffer_get_stride (from);

  to_base = _gdk_macos_buffer_get_data (to);
  to_stride = _gdk_macos_buffer_get_stride (to);

  n_rects = cairo_region_num_rectangles (region);

  for (guint i = 0; i < n_rects; i++)
    {
      cairo_rectangle_int_t rect;
      int y2;

      cairo_region_get_rectangle (region, i, &rect);

      rect.y *= scale;
      rect.height *= scale;
      rect.x *= scale;
      rect.width *= scale;

      y2 = rect.y + rect.height;

      for (int y = rect.y; y < y2; y++)
        memcpy (&to_base[y * to_stride + rect.x * 4],
                &from_base[y * from_stride + rect.x * 4],
                rect.width * 4);
    }
}

static void
_gdk_macos_cairo_context_begin_frame (GdkDrawContext *draw_context,
                                      gboolean        prefers_high_depth,
                                      cairo_region_t *region)
{
  GdkMacosCairoContext *self = (GdkMacosCairoContext *)draw_context;
  GdkMacosBuffer *buffer;
  GdkMacosSurface *surface;

  g_assert (GDK_IS_MACOS_CAIRO_CONTEXT (self));

  [CATransaction begin];
  [CATransaction setDisableActions:YES];

  surface = GDK_MACOS_SURFACE (gdk_draw_context_get_surface (draw_context));
  buffer = _gdk_macos_surface_get_buffer (surface);

  _gdk_macos_buffer_set_damage (buffer, region);
  _gdk_macos_buffer_set_flipped (buffer, FALSE);

  _gdk_macos_buffer_lock (buffer);

  /* If there is damage that was on the previous frame that is not on
   * this frame, we need to copy that rendered region over to the back
   * buffer so that when swapping buffers, we still have that content.
   * This is done with a read-only lock on the IOSurface to avoid
   * invalidating the buffer contents.
   */
  if (surface->front != NULL)
    {
      const cairo_region_t *previous = _gdk_macos_buffer_get_damage (surface->front);

      if (previous != NULL)
        {
          cairo_region_t *copy;

          copy = cairo_region_copy (previous);
          cairo_region_subtract (copy, region);

          if (!cairo_region_is_empty (copy))
            {
              int scale = gdk_surface_get_scale_factor (GDK_SURFACE (surface));

              _gdk_macos_buffer_read_lock (surface->front);
              copy_surface_data (surface->front, buffer, copy, scale);
              _gdk_macos_buffer_read_unlock (surface->front);
            }

          cairo_region_destroy (copy);
        }
    }
}

static void
_gdk_macos_cairo_context_end_frame (GdkDrawContext *draw_context,
                                    cairo_region_t *painted)
{
  GdkMacosCairoContext *self = (GdkMacosCairoContext *)draw_context;
  GdkMacosSurface *surface;
  GdkMacosBuffer *buffer;

  g_assert (GDK_IS_MACOS_CAIRO_CONTEXT (self));

  surface = GDK_MACOS_SURFACE (gdk_draw_context_get_surface (draw_context));
  buffer = _gdk_macos_surface_get_buffer (surface);

  _gdk_macos_buffer_unlock (buffer);

  _gdk_macos_surface_swap_buffers (surface, painted);

  [CATransaction commit];
}

static void
_gdk_macos_cairo_context_surface_resized (GdkDrawContext *draw_context)
{
  g_assert (GDK_IS_MACOS_CAIRO_CONTEXT (draw_context));

  /* Do nothing, next begin_frame will get new buffer */
}

static void
_gdk_macos_cairo_context_class_init (GdkMacosCairoContextClass *klass)
{
  GdkCairoContextClass *cairo_context_class = GDK_CAIRO_CONTEXT_CLASS (klass);
  GdkDrawContextClass *draw_context_class = GDK_DRAW_CONTEXT_CLASS (klass);

  draw_context_class->begin_frame = _gdk_macos_cairo_context_begin_frame;
  draw_context_class->end_frame = _gdk_macos_cairo_context_end_frame;
  draw_context_class->surface_resized = _gdk_macos_cairo_context_surface_resized;

  cairo_context_class->cairo_create = _gdk_macos_cairo_context_cairo_create;
}

static void
_gdk_macos_cairo_context_init (GdkMacosCairoContext *self)
{
}