summaryrefslogtreecommitdiff
path: root/gdk/mir/gdkmirglcontext.c
blob: ca04255a58eefbea7de63ae9596aa59901ea028d (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
/* GDK - The GIMP Drawing Kit
 *
 * gdkmirglcontext.c: Mir specific OpenGL wrappers
 *
 * Copyright © 2014 Canonical Ltd
 *
 * 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 "gdkmir-private.h"
#include "gdkinternals.h"
#include "gdkintl.h"

G_DEFINE_TYPE (GdkMirGLContext, gdk_mir_gl_context, GDK_TYPE_GL_CONTEXT)

#define N_EGL_ATTRS 16

static gboolean
gdk_mir_gl_context_realize (GdkGLContext *context,
                            GError      **error)
{
  GdkMirGLContext *context_mir = GDK_MIR_GL_CONTEXT (context);
  GdkDisplay *display = gdk_gl_context_get_display (context);
  GdkGLContext *share = gdk_gl_context_get_shared_context (context);
  EGLContext ctx;
  EGLint context_attribs[N_EGL_ATTRS];
  int major, minor, flags;
  gboolean debug_bit, forward_bit;
  int i = 0;

  if (!_gdk_mir_display_init_egl_display (display))
    {
      g_set_error_literal (error, GDK_GL_ERROR,
                           GDK_GL_ERROR_NOT_AVAILABLE,
                           _("No GL implementation is available"));
      return FALSE;
    }

  gdk_gl_context_get_required_version (context, &major, &minor);
  debug_bit = gdk_gl_context_get_debug_enabled (context);
  forward_bit = gdk_gl_context_get_forward_compatible (context);

  flags = 0;

  if (debug_bit)
    flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
  if (forward_bit)
    flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;

  /* We want a core profile */
  context_attribs[i++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
  context_attribs[i++] = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;

  /* Specify the version */
  context_attribs[i++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
  context_attribs[i++] = major;
  context_attribs[i++] = EGL_CONTEXT_MINOR_VERSION_KHR;
  context_attribs[i++] = minor;

  /* Specify the flags */
  context_attribs[i++] = EGL_CONTEXT_FLAGS_KHR;
  context_attribs[i++] = flags;

  context_attribs[i++] = EGL_NONE;
  g_assert (i < N_EGL_ATTRS);

  ctx = eglCreateContext (_gdk_mir_display_get_egl_display (display),
                          context_mir->egl_config,
                          share != NULL ? GDK_MIR_GL_CONTEXT (share)->egl_context
                                        : EGL_NO_CONTEXT,
                          context_attribs);
  if (ctx == NULL)
    {
      g_set_error_literal (error, GDK_GL_ERROR,
                           GDK_GL_ERROR_NOT_AVAILABLE,
                           _("Unable to create a GL context"));
      return FALSE;
    }

  GDK_NOTE (OPENGL, g_print ("Created EGL context[%p]\n", ctx));

  context_mir->egl_context = ctx;

  return TRUE;
}

static cairo_region_t *
gdk_mir_gl_context_get_damage (GdkGLContext *context)
{
  GdkDisplay *display = gdk_draw_context_get_display (GDK_DRAW_CONTEXT (context));
  EGLSurface egl_surface;
  GdkWindow *window = gdk_draw_context_get_window (GDK_DRAW_CONTEXT (context));
  int buffer_age = 0;

  if (_gdk_mir_display_have_egl_buffer_age (display))
    {
      GdkGLContext *shared;
      GdkMirGLContext *shared_mir;
     
      shared = gdk_gl_context_get_shared_context (context);
      if (shared == NULL)
        shared = context;
      shared_mir = GDK_MIR_GL_CONTEXT (shared);

      egl_surface = _gdk_mir_window_get_egl_surface (window, shared_mir->egl_config);
      gdk_gl_context_make_current (shared);
      eglQuerySurface (_gdk_mir_display_get_egl_display (display), egl_surface,
                       EGL_BUFFER_AGE_EXT, &buffer_age);

      if (buffer_age >= 2)
        {
          if (window->old_updated_area[0])
            return cairo_region_copy (window->old_updated_area[0]);
        }
      else if (buffer_age >= 3)
        {
          if (window->old_updated_area[0] &&
              window->old_updated_area[1])
            {
              cairo_region_t *damage = cairo_region_copy (window->old_updated_area[0]);
              cairo_region_union (damage, window->old_updated_area[1]);
              return damage;
            }
        }
    }

  return GDK_GL_CONTEXT_CLASS (gdk_mir_gl_context_parent_class)->get_damage (context);
}

static void
gdk_mir_gl_context_end_frame (GdkDrawContext *context,
                              cairo_region_t *painted,
                              cairo_region_t *damage)
{
  GdkGLContext *gl_context = GDK_GL_CONTEXT (context);
  GdkMirGLContext *context_mir = GDK_MIR_GL_CONTEXT (gl_context);
  GdkWindow *window = gdk_gl_context_get_window (gl_context);
  GdkDisplay *display = gdk_window_get_display (window);
  EGLDisplay egl_display = _gdk_mir_display_get_egl_display (display);
  EGLSurface egl_surface;

  GDK_DRAW_CONTEXT_CLASS (gdk_mir_gl_context_parent_class)->end_frame (context, painted, damage);
  if (gdk_gl_context_get_shared_context (gl_context))
    return;

  gdk_gl_context_make_current (gl_context);

  egl_surface = _gdk_mir_window_get_egl_surface (window,
                                                 context_mir->egl_config);

  if (_gdk_mir_display_have_egl_swap_buffers_with_damage (display))
    {
      int i, j, n_rects = cairo_region_num_rectangles (damage);
      EGLint *rects = g_new (EGLint, n_rects * 4);
      cairo_rectangle_int_t rect;
      int window_height = gdk_window_get_height (window);

      for (i = 0, j = 0; i < n_rects; i++)
        {
          cairo_region_get_rectangle (damage, i, &rect);
          rects[j++] = rect.x;
          rects[j++] = window_height - rect.height - rect.y;
          rects[j++] = rect.width;
          rects[j++] = rect.height;
        }
      eglSwapBuffersWithDamageEXT (egl_display, egl_surface, rects, n_rects);
      g_free (rects);
    }
  else
    {
      eglSwapBuffers (egl_display, egl_surface);
    }
}

static void
gdk_mir_gl_context_dispose (GObject *gobject)
{
  GdkMirGLContext *context_mir = GDK_MIR_GL_CONTEXT (gobject);

  if (context_mir->egl_context != NULL)
    {
      GdkGLContext *context = GDK_GL_CONTEXT (gobject);
      GdkWindow *window = gdk_gl_context_get_window (context);
      GdkDisplay *display = gdk_window_get_display (window);
      EGLDisplay egl_display = _gdk_mir_display_get_egl_display (display);

      if (eglGetCurrentContext () == context_mir->egl_context)
        eglMakeCurrent (egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

      GDK_NOTE (OPENGL, g_print ("Destroying EGL context\n"));

      eglDestroyContext (egl_display, context_mir->egl_context);
      context_mir->egl_context = NULL;
    }

  G_OBJECT_CLASS (gdk_mir_gl_context_parent_class)->dispose (gobject);
}

static void
gdk_mir_gl_context_class_init (GdkMirGLContextClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GdkDrawContextClass *draw_context_class = GDK_DRAW_CONTEXT_CLASS (klass);
  GdkGLContextClass *gl_context_class = GDK_GL_CONTEXT_CLASS (klass);

  gl_context_class->realize = gdk_mir_gl_context_realize;
  gl_context_class->get_damage = gdk_mir_gl_context_get_damage;
  draw_context_class->end_frame = gdk_mir_gl_context_end_frame;
  gobject_class->dispose = gdk_mir_gl_context_dispose;
}

static void
gdk_mir_gl_context_init (GdkMirGLContext *self)
{
}