summaryrefslogtreecommitdiff
path: root/src/compositor/meta-compositor-native.c
blob: a156be7da24f9fb9148a5620ec219dd362860403 (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
/*
 * Copyright (C) 2019 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 */

#include "config.h"

#include "compositor/meta-compositor-native.h"

#include "backends/meta-logical-monitor.h"
#include "backends/native/meta-crtc-kms.h"
#include "compositor/meta-compositor-view-native.h"
#include "compositor/meta-surface-actor-wayland.h"

struct _MetaCompositorNative
{
  MetaCompositorServer parent;

  MetaWaylandSurface *current_scanout_candidate;
};

G_DEFINE_TYPE (MetaCompositorNative, meta_compositor_native,
               META_TYPE_COMPOSITOR_SERVER)

#ifdef HAVE_WAYLAND

static MetaRendererView *
get_window_view (MetaRenderer *renderer,
                 MetaWindow   *window)
{
  GList *l;
  MetaRendererView *view_found = NULL;

  for (l = meta_renderer_get_views (renderer); l; l = l->next)
    {
      ClutterStageView *stage_view = l->data;
      MetaRectangle view_layout;

      clutter_stage_view_get_layout (stage_view, &view_layout);

      if (meta_rectangle_equal (&window->buffer_rect,
                                &view_layout))
        {
          if (view_found)
            return NULL;
          view_found = META_RENDERER_VIEW (stage_view);
        }
    }

  return view_found;
}

static void
maybe_assign_primary_plane (MetaCompositor *compositor)
{
  MetaCompositorNative *compositor_native = META_COMPOSITOR_NATIVE (compositor);
  MetaBackend *backend = meta_get_backend ();
  MetaRenderer *renderer = meta_backend_get_renderer (backend);
  MetaWindowActor *window_actor;
  MetaWindow *window;
  MetaRendererView *view;
  MetaCrtc *crtc;
  CoglFramebuffer *framebuffer;
  CoglOnscreen *onscreen;
  MetaSurfaceActor *surface_actor;
  MetaSurfaceActorWayland *surface_actor_wayland;
  MetaWaylandSurface *surface;
  int geometry_scale;
  MetaWaylandSurface *old_candidate =
    compositor_native->current_scanout_candidate;
  MetaWaylandSurface *new_candidate = NULL;
  g_autoptr (CoglScanout) scanout = NULL;

  if (meta_compositor_is_unredirect_inhibited (compositor))
    goto done;

  window_actor = meta_compositor_get_top_window_actor (compositor);
  if (!window_actor)
    goto done;

  if (meta_window_actor_effect_in_progress (window_actor))
    goto done;

  if (clutter_actor_has_transitions (CLUTTER_ACTOR (window_actor)))
    goto done;

  window = meta_window_actor_get_meta_window (window_actor);
  if (!window)
    goto done;

  view = get_window_view (renderer, window);
  if (!view)
    goto done;

  crtc = meta_renderer_view_get_crtc (META_RENDERER_VIEW (view));
  if (!META_IS_CRTC_KMS (crtc))
    goto done;

  framebuffer = clutter_stage_view_get_framebuffer (CLUTTER_STAGE_VIEW (view));
  if (!COGL_IS_ONSCREEN (framebuffer))
    goto done;

  surface_actor = meta_window_actor_get_scanout_candidate (window_actor);
  if (!surface_actor)
    goto done;

  surface_actor_wayland = META_SURFACE_ACTOR_WAYLAND (surface_actor);
  surface = meta_surface_actor_wayland_get_surface (surface_actor_wayland);
  if (!surface)
    goto done;

  geometry_scale = meta_window_actor_get_geometry_scale (window_actor);
  if (!meta_wayland_surface_can_scanout_untransformed (surface, view,
                                                       geometry_scale))
    goto done;

  new_candidate = surface;

  onscreen = COGL_ONSCREEN (framebuffer);
  scanout = meta_surface_actor_wayland_try_acquire_scanout (surface_actor_wayland,
                                                            onscreen);
  if (!scanout)
    goto done;

  clutter_stage_view_assign_next_scanout (CLUTTER_STAGE_VIEW (view), scanout);

done:

  if (old_candidate && old_candidate != new_candidate)
    {
      meta_wayland_surface_set_scanout_candidate (old_candidate, NULL);
      g_clear_weak_pointer (&compositor_native->current_scanout_candidate);
    }

  if (new_candidate)
    {
      meta_wayland_surface_set_scanout_candidate (surface, crtc);
      g_set_weak_pointer (&compositor_native->current_scanout_candidate,
                          surface);
    }
}
#endif /* HAVE_WAYLAND */

static void
meta_compositor_native_before_paint (MetaCompositor     *compositor,
                                     MetaCompositorView *compositor_view)
{
  MetaCompositorClass *parent_class;

#ifdef HAVE_WAYLAND
  maybe_assign_primary_plane (compositor);
#endif

  parent_class = META_COMPOSITOR_CLASS (meta_compositor_native_parent_class);
  parent_class->before_paint (compositor, compositor_view);
}

static MetaCompositorView *
meta_compositor_native_create_view (MetaCompositor   *compositor,
                                    ClutterStageView *stage_view)
{
  MetaCompositorViewNative *compositor_view_native;

  compositor_view_native = meta_compositor_view_native_new (stage_view);

  return META_COMPOSITOR_VIEW (compositor_view_native);
}

MetaCompositorNative *
meta_compositor_native_new (MetaDisplay *display,
                            MetaBackend *backend)
{
  return g_object_new (META_TYPE_COMPOSITOR_NATIVE,
                       "display", display,
                       "backend", backend,
                       NULL);
}

static void
meta_compositor_native_finalize (GObject *object)
{
  MetaCompositorNative *compositor_native = META_COMPOSITOR_NATIVE (object);

  g_clear_weak_pointer (&compositor_native->current_scanout_candidate);

  G_OBJECT_CLASS (meta_compositor_native_parent_class)->finalize (object);
}

static void
meta_compositor_native_init (MetaCompositorNative *compositor_native)
{
}

static void
meta_compositor_native_class_init (MetaCompositorNativeClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  MetaCompositorClass *compositor_class = META_COMPOSITOR_CLASS (klass);

  object_class->finalize = meta_compositor_native_finalize;

  compositor_class->before_paint = meta_compositor_native_before_paint;
  compositor_class->create_view = meta_compositor_native_create_view;
}