~mir-team/mir/in-process-egl+input-conglomeration

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

#include "mir/graphics/platform.h"
#include "mir/graphics/display_configuration.h"
#include "mir/graphics/display_report.h"
#include "android_display.h"
#include "mir/geometry/rectangle.h"

#include <boost/throw_exception.hpp>

#include <stdexcept>

#include <GLES2/gl2.h>

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

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

class NullDisplayConfiguration : public mg::DisplayConfiguration
{
public:
    void for_each_card(std::function<void(mg::DisplayConfigurationCard const&)>)
    {
    }

    void for_each_output(std::function<void(mg::DisplayConfigurationOutput const&)>)
    {
    }
};
}

mga::AndroidDisplay::AndroidDisplay(const std::shared_ptr<AndroidFramebufferWindowQuery>& native_win,
                                    std::shared_ptr<DisplayReport> const& display_report)
    : egl_display{EGL_NO_DISPLAY},
      egl_surface{EGL_NO_SURFACE},
      native_window{native_win},
      egl_config{0},
      egl_context{EGL_NO_CONTEXT},
      egl_context_shared{EGL_NO_CONTEXT},
      egl_surface_dummy{EGL_NO_SURFACE}
{
    EGLint major, minor;

    egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (egl_display == EGL_NO_DISPLAY)
        BOOST_THROW_EXCEPTION(std::runtime_error("eglGetDisplay failed\n"));

    if (eglInitialize(egl_display, &major, &minor) == EGL_FALSE)
        BOOST_THROW_EXCEPTION(std::runtime_error("eglInitialize failure\n"));

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

    egl_config = native_window->android_display_egl_config(egl_display);
    EGLNativeWindowType native_win_type = native_window->android_native_window_type();
    egl_surface = eglCreateWindowSurface(egl_display, egl_config, native_win_type, NULL);
    if(egl_surface == EGL_NO_SURFACE)
        BOOST_THROW_EXCEPTION(std::runtime_error("could not create egl surface\n"));

    display_report->report_successful_setup_of_native_resources();

    egl_context_shared = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, default_egl_context_attr);
    if (egl_context_shared == EGL_NO_CONTEXT)
        BOOST_THROW_EXCEPTION(std::runtime_error("could not create egl context dummy\n"));

    egl_context = eglCreateContext(egl_display, egl_config, egl_context_shared, default_egl_context_attr);
    if (egl_context == EGL_NO_CONTEXT)
        BOOST_THROW_EXCEPTION(std::runtime_error("could not create egl context\n"));

    EGLint pbuffer_attribs[]{EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
    egl_surface_dummy = eglCreatePbufferSurface(egl_display, egl_config, pbuffer_attribs);
    if (egl_surface_dummy == EGL_NO_SURFACE)
        BOOST_THROW_EXCEPTION(std::runtime_error("could not create dummy egl surface\n"));

    if (eglMakeCurrent(egl_display, egl_surface_dummy, egl_surface_dummy, egl_context_shared) == EGL_FALSE)
        BOOST_THROW_EXCEPTION(std::runtime_error("could not activate dummy surface with eglMakeCurrent\n"));

    display_report->report_successful_egl_make_current_on_construction();
    display_report->report_successful_display_construction();
}

mga::AndroidDisplay::~AndroidDisplay()
{
    eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglDestroyContext(egl_display, egl_context);
    eglDestroySurface(egl_display, egl_surface);
    eglDestroyContext(egl_display, egl_context_shared);
    eglDestroySurface(egl_display, egl_surface_dummy);
    eglTerminate(egl_display);
}

geom::Rectangle mga::AndroidDisplay::view_area() const
{
    int display_width, display_height;
    eglQuerySurface(egl_display, egl_surface, EGL_WIDTH, &display_width);
    eglQuerySurface(egl_display, egl_surface, EGL_HEIGHT, &display_height);
    geom::Width w(display_width);
    geom::Height h(display_height);

    geom::Point pt { geom::X{0},
                     geom::Y{0}};
    geom::Size sz{w,h};
    geom::Rectangle rect{pt, sz};
    return rect;
}

void mga::AndroidDisplay::clear()
{
    glClear(GL_COLOR_BUFFER_BIT);
}

void mga::AndroidDisplay::post_update()
{
    if (eglSwapBuffers(egl_display, egl_surface) == EGL_FALSE)
    {
        BOOST_THROW_EXCEPTION(std::runtime_error("eglSwapBuffers failure\n"));
    }
}

void mga::AndroidDisplay::for_each_display_buffer(std::function<void(mg::DisplayBuffer&)> const& f)
{
    f(*this);
}

std::shared_ptr<mg::DisplayConfiguration> mga::AndroidDisplay::configuration()
{
    return std::make_shared<NullDisplayConfiguration>();
}

void mga::AndroidDisplay::register_pause_resume_handlers(
    MainLoop& /*main_loop*/,
    DisplayPauseHandler const& /*pause_handler*/,
    DisplayResumeHandler const& /*resume_handler*/)
{
}

void mga::AndroidDisplay::pause()
{
}

void mga::AndroidDisplay::resume()
{
}

void mga::AndroidDisplay::make_current()
{
    if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context) == EGL_FALSE)
    {
        BOOST_THROW_EXCEPTION(std::runtime_error("could not activate surface with eglMakeCurrent\n"));
    }
}

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


auto mga::AndroidDisplay::the_cursor() -> std::weak_ptr<Cursor>
{
    return std::weak_ptr<Cursor>();
}