~alan-griffiths/mir/fix-1654023

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
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
 */

#include "gl_context.h"
#include "framebuffer_bundle.h"
#include "android_format_conversion-inl.h"
#include "mir/graphics/display_report.h"
#include "mir/graphics/gl_config.h"
#include "mir/graphics/egl_error.h"

#include <algorithm>
#include <boost/throw_exception.hpp>
#include <stdexcept>
#include <sstream>

namespace mg=mir::graphics;
namespace mga=mir::graphics::android;

namespace
{

static EGLint const default_egl_context_attr[] =
{
    EGL_CONTEXT_CLIENT_VERSION, 2,
    EGL_NONE
};

static EGLint const dummy_pbuffer_attribs[] =
{
    EGL_WIDTH, 1,
    EGL_HEIGHT, 1,
    EGL_NONE
};

static EGLDisplay create_and_initialize_display()
{
    EGLint major, minor;

    auto egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (egl_display == EGL_NO_DISPLAY)
        BOOST_THROW_EXCEPTION(mg::egl_error("eglGetDisplay failed"));

    if (eglInitialize(egl_display, &major, &minor) == EGL_FALSE)
        BOOST_THROW_EXCEPTION(mg::egl_error("eglInitialize failure"));

    if ((major != 1) || (minor != 4))
        BOOST_THROW_EXCEPTION(std::runtime_error("must have EGL 1.4"));
    return egl_display;
}

/* the minimum requirement is to have EGL_WINDOW_BIT and EGL_OPENGL_ES2_BIT, and to select a config
   whose pixel format matches that of the framebuffer. */
EGLConfig select_egl_config_with_format(
    EGLDisplay egl_display, MirPixelFormat display_format,
    mg::GLConfig const& gl_config)
{
    EGLint const required_egl_config_attr [] =
    {
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_DEPTH_SIZE, gl_config.depth_buffer_bits(),
        EGL_STENCIL_SIZE, gl_config.stencil_buffer_bits(),
        EGL_NONE
    };

    int required_visual_id = mga::to_android_format(display_format);
    int num_potential_configs;
    EGLint num_match_configs;

    eglGetConfigs(egl_display, NULL, 0, &num_potential_configs);
    std::vector<EGLConfig> config_slots(num_potential_configs);

    eglChooseConfig(egl_display, required_egl_config_attr, config_slots.data(), num_potential_configs, &num_match_configs);
    config_slots.resize(num_match_configs);

    auto const pegl_config = std::find_if(begin(config_slots), end(config_slots),
        [&](EGLConfig& current) -> bool
        {
            int visual_id;
            eglGetConfigAttrib(egl_display, current, EGL_NATIVE_VISUAL_ID, &visual_id);
            return (visual_id == required_visual_id);
        });

    if (pegl_config == end(config_slots))
        BOOST_THROW_EXCEPTION(std::runtime_error("could not select EGL config for use with framebuffer"));

    return *pegl_config;
}

}

void mga::GLContext::make_current(EGLSurface egl_surface) const
{
    if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
    {
        BOOST_THROW_EXCEPTION(
            mg::egl_error("could not activate surface with eglMakeCurrent"));
    }
}

void mga::GLContext::release_current() const
{
    eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}


mga::GLContext::~GLContext()
{
    if (eglGetCurrentContext() == egl_context)
        release_current();
    if (own_display)
        eglTerminate(egl_display);
}

mga::GLContext::GLContext(
    MirPixelFormat display_format, mg::GLConfig const& gl_config, mg::DisplayReport& report) :
    egl_display(create_and_initialize_display()),
    egl_config(select_egl_config_with_format(egl_display, display_format, gl_config)),
    egl_context{egl_display,
                eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, default_egl_context_attr)},
    own_display(true)
{
    report.report_egl_configuration(egl_display, egl_config);
}

mga::GLContext::GLContext(GLContext const& shared_gl_context) :
    mg::GLContext(),
    egl_display(shared_gl_context.egl_display),
    egl_config(shared_gl_context.egl_config),
    egl_context{egl_display,
                eglCreateContext(egl_display, egl_config, shared_gl_context.egl_context,
                                 default_egl_context_attr)},
    own_display(false)
{
}

mga::PbufferGLContext::PbufferGLContext(
    MirPixelFormat display_format, mg::GLConfig const& gl_config, mg::DisplayReport& report) :
    GLContext(display_format, gl_config, report),
    egl_surface{egl_display,
                eglCreatePbufferSurface(egl_display, egl_config, dummy_pbuffer_attribs)}
{
}

mga::PbufferGLContext::PbufferGLContext(PbufferGLContext const& shared_gl_context) :
    GLContext(shared_gl_context),
    egl_surface{egl_display,
                eglCreatePbufferSurface(egl_display, egl_config, dummy_pbuffer_attribs)}
{
}

void mga::PbufferGLContext::make_current() const
{
    GLContext::make_current(egl_surface);
}

void mga::PbufferGLContext::release_current() const
{
    GLContext::release_current();
}

mga::FramebufferGLContext::FramebufferGLContext(
    GLContext const& shared_gl_context,
    std::shared_ptr<FramebufferBundle> const& fb_bundle,
    std::shared_ptr<ANativeWindow> const& native_window)
     : GLContext(shared_gl_context),
       fb_bundle(fb_bundle),
       egl_surface{egl_display,
                   eglCreateWindowSurface(egl_display, egl_config, native_window.get(), NULL)}
{
}

void mga::FramebufferGLContext::swap_buffers() const
{
    if (eglSwapBuffers(egl_display, egl_surface) == EGL_FALSE)
        BOOST_THROW_EXCEPTION(mg::egl_error("eglSwapBuffers failure"));
}

std::shared_ptr<mg::Buffer> mga::FramebufferGLContext::last_rendered_buffer() const
{
    return fb_bundle->last_rendered_buffer();
}

void mga::FramebufferGLContext::make_current() const
{
    GLContext::make_current(egl_surface);
}

void mga::FramebufferGLContext::release_current() const
{
    GLContext::release_current();
}