File: test_shm_buffer.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (379 lines) | stat: -rw-r--r-- 10,820 bytes parent folder | download
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright © 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 2 or 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/>.
 */

#include "src/platforms/common/server/shm_buffer.h"
#include "mir/graphics/egl_context_executor.h"
#include "mir/renderer/gl/context.h"

#include "mir/test/doubles/mock_gl.h"
#include "mir/test/doubles/mock_egl.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <endian.h>
#include <boost/throw_exception.hpp>

namespace mg = mir::graphics;
namespace mgc = mir::graphics::common;
namespace mtd = mir::test::doubles;
namespace geom = mir::geometry;
using namespace testing;

namespace
{

class DumbGLContext : public mir::renderer::gl::Context
{
public:
    DumbGLContext(EGLContext ctx)
        : ctx{ctx}
    {
    }

    void make_current() const override
    {
        eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx);
    }

    void release_current() const override
    {
        eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    }

    auto make_share_context() const -> std::unique_ptr<Context> override
    {
        return std::make_unique<DumbGLContext>(ctx);
    }

    explicit operator EGLContext() override
    {
        return ctx;
    }

private:
    EGLDisplay const dpy{reinterpret_cast<void*>(0xdeebbeed)};
    EGLContext const ctx;
};

struct PlatformlessShmBuffer : mgc::MemoryBackedShmBuffer
{
    PlatformlessShmBuffer(
        geom::Size const& size,
        MirPixelFormat const& pixel_format,
        std::shared_ptr<mgc::EGLContextExecutor> egl_delegate)
        : MemoryBackedShmBuffer(
            size,
            pixel_format,
            std::move(egl_delegate))
    {
    }

    auto pixel_buffer() -> unsigned char const*
    {
        // This uses the fact that MemoryBackedShmBuffer always returns the same backing store when mapping
        auto const read_mapping = map_readable();
        return read_mapping->data();
    }
};

struct ShmBufferTest : public testing::Test
{
    ShmBufferTest()
        : size{150, 340},
          pixel_format{mir_pixel_format_bgr_888},
          egl_delegate{
            std::make_shared<mgc::EGLContextExecutor>(
                std::make_unique<DumbGLContext>(dummy))}
    {
    }

    testing::NiceMock<mtd::MockEGL> mock_egl;
    testing::NiceMock<mtd::MockGL> mock_gl;

    geom::Size const size;
    MirPixelFormat const pixel_format;
    EGLContext const dummy{reinterpret_cast<void*>(0x0011223344)};
    std::shared_ptr<mgc::EGLContextExecutor> const egl_delegate;
};

}

TEST_F(ShmBufferTest, has_correct_properties)
{
    size_t const bytes_per_pixel = MIR_BYTES_PER_PIXEL(pixel_format);
    size_t const expected_stride{bytes_per_pixel * size.width.as_uint32_t()};

    PlatformlessShmBuffer shm_buffer(size, pixel_format, egl_delegate);

    EXPECT_EQ(size, shm_buffer.size());
    EXPECT_EQ(geom::Stride{expected_stride}, shm_buffer.map_readable()->stride());
    EXPECT_EQ(pixel_format, shm_buffer.pixel_format());
}

TEST_F(ShmBufferTest, cant_upload_bgr_888)
{
    PlatformlessShmBuffer buf(size, mir_pixel_format_bgr_888, egl_delegate);
    EXPECT_CALL(mock_gl, glTexImage2D(GL_TEXTURE_2D, 0, _,
                                      size.width.as_int(), size.height.as_int(),
                                      0, _, _,
                                      buf.pixel_buffer()))
                .Times(0);

    buf.bind();
}

struct BufferUploadDesc
{
    geom::Size size;
    // TODO: Test uploads with stride_in_px != size.width
    int stride_in_px;
    MirPixelFormat format;
    GLenum gl_format;
    GLenum gl_type;
};

class UploadTest :
    public ShmBufferTest,
    public WithParamInterface<BufferUploadDesc>
{
};

TEST_P(UploadTest, uploads_correctly)
{
    auto const desc = GetParam();

    PlatformlessShmBuffer buf(
        desc.size, desc.format, egl_delegate);

    ExpectationSet gl_setup;
    gl_setup +=
        EXPECT_CALL(mock_gl, glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
    gl_setup +=
        EXPECT_CALL(mock_gl, glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, desc.stride_in_px));

    Expectation gl_use = EXPECT_CALL(
        mock_gl,
        glTexImage2D(
            GL_TEXTURE_2D, 0,
            desc.gl_format,
            desc.size.width.as_int(), desc.size.height.as_int(),
            0,
            desc.gl_format, desc.gl_type,
            buf.pixel_buffer()));

    // Ensure we reset GL state to be polite to other users.
    EXPECT_CALL(mock_gl, glPixelStorei(GL_UNPACK_ALIGNMENT, 4))
        .After(gl_use);
    EXPECT_CALL(mock_gl, glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0))
        .After(gl_use);

    buf.bind();

}

namespace
{
geom::Size const default_size{245, 553};

BufferUploadDesc const test_cases[] = {
#if __BYTE_ORDER == __LITTLE_ENDIAN
    BufferUploadDesc{
        default_size,
        default_size.width.as_int(),
        mir_pixel_format_xrgb_8888,
        GL_BGRA_EXT,
        GL_UNSIGNED_BYTE
    },
    BufferUploadDesc{
        default_size,
        default_size.width.as_int(),
        mir_pixel_format_argb_8888,
        GL_BGRA_EXT,
        GL_UNSIGNED_BYTE
    },
    BufferUploadDesc{
        default_size,
        default_size.width.as_int(),
        mir_pixel_format_xbgr_8888,
        GL_RGBA,
        GL_UNSIGNED_BYTE
    },
    BufferUploadDesc{
        default_size,
        default_size.width.as_int(),
        mir_pixel_format_abgr_8888,
        GL_RGBA,
        GL_UNSIGNED_BYTE
    },
#endif
    BufferUploadDesc {
        default_size,
        default_size.width.as_int(),
        mir_pixel_format_rgb_888,
        GL_RGB,
        GL_UNSIGNED_BYTE
    },
    BufferUploadDesc{
        default_size,
        default_size.width.as_int(),
        mir_pixel_format_rgb_565,
        GL_RGB,
        GL_UNSIGNED_SHORT_5_6_5
    },
    BufferUploadDesc{
        default_size,
        default_size.width.as_int(),
        mir_pixel_format_rgba_5551,
        GL_RGBA,
        GL_UNSIGNED_SHORT_5_5_5_1
    },
    BufferUploadDesc{
        default_size,
        default_size.width.as_int(),
        mir_pixel_format_rgba_4444,
        GL_RGBA,
        GL_UNSIGNED_SHORT_4_4_4_4
    },
};
}

namespace std
{
std::string to_string(MirPixelFormat format)
{
    // TODO: Wouldn't it be nice if these were much more usable?
    switch(format)
    {
#define ENUM_TO_STR(value) \
    case value: return #value

        ENUM_TO_STR(mir_pixel_format_invalid);
        ENUM_TO_STR(mir_pixel_format_abgr_8888);
        ENUM_TO_STR(mir_pixel_format_xbgr_8888);
        ENUM_TO_STR(mir_pixel_format_argb_8888);
        ENUM_TO_STR(mir_pixel_format_xrgb_8888);
        ENUM_TO_STR(mir_pixel_format_bgr_888);
        ENUM_TO_STR(mir_pixel_format_rgb_888);
        ENUM_TO_STR(mir_pixel_format_rgb_565);
        ENUM_TO_STR(mir_pixel_format_rgba_5551);
        ENUM_TO_STR(mir_pixel_format_rgba_4444);
#undef ENUM_TO_STR
    default:
        return "UNKNOWN MirPixelFormat";
    }
}
}

INSTANTIATE_TEST_SUITE_P(
    ShmBuffer,
    UploadTest,
    ValuesIn(test_cases),
    [](const testing::TestParamInfo<UploadTest::ParamType>& info)
    {
        return std::to_string(info.param.format);
    });

namespace
{
void wait_for_egl_thread(mgc::EGLContextExecutor& egl_delegate)
{
    auto egl_thread_started_promise = std::make_shared<std::promise<void>>();
    auto const egl_thread_started = egl_thread_started_promise->get_future();

    egl_delegate.spawn(
        [promise = std::move(egl_thread_started_promise)]()
        {
            promise->set_value();
        });

    if (egl_thread_started.wait_for(std::chrono::seconds{10}) != std::future_status::ready)
    {
        BOOST_THROW_EXCEPTION((std::runtime_error{"Timeout waiting for EGLContextDelegate"}));
    }
}
}

TEST_F(ShmBufferTest, texture_is_destroyed_on_thread_with_current_context)
{
    GLint const tex_id{0x8086};
    ON_CALL(mock_gl, glGenTextures(1,_))
        .WillByDefault(SetArgPointee<1>(tex_id));
    ON_CALL(mock_gl, glDeleteTextures(1,Pointee(Eq(tex_id))))
        .WillByDefault(InvokeWithoutArgs(
            []()
            {
                EXPECT_THAT(
                    eglGetCurrentContext(),
                    Ne(EGL_NO_CONTEXT));
            }));

    EGLContext const dummy_ctx{reinterpret_cast<EGLContext>(0x66221144)};
    EGLDisplay const dummy_dpy{reinterpret_cast<EGLDisplay>(0xaabbccdd)};

    {
        /* Use a locally-scoped EGLContextDelegate to make use of
         * the fact that the destructor ensures the work-queue is drained.
         */
        auto egl_delegate = std::make_shared<mgc::EGLContextExecutor>(
            std::make_unique<DumbGLContext>(reinterpret_cast<EGLContext>(42)));

        // Ensure that the EGL thread has actually spun up…
        wait_for_egl_thread(*egl_delegate);

        // Ensure we have a “context” current for creation and bind
        eglMakeCurrent(dummy_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, dummy_ctx);

        PlatformlessShmBuffer buffer{size, pixel_format, egl_delegate};

        buffer.bind();

        // Ensure this thread does *not* have a “context” current for destruction
        eglMakeCurrent(dummy_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    }
}

TEST_F(ShmBufferTest, tex_id_is_generated_on_thread_with_current_egl_context)
{
    GLint const tex_id{0x8086};
    EGLContext const dummy_ctx{reinterpret_cast<EGLContext>(0x66221144)};
    EGLDisplay const dummy_dpy{reinterpret_cast<EGLDisplay>(0xaabbccdd)};

    ON_CALL(mock_gl, glGenTextures(1,_))
        .WillByDefault(
            DoAll(
                SetArgPointee<1>(tex_id),
                InvokeWithoutArgs(
                    [dummy_ctx]()
                    {
                        EXPECT_THAT(
                            eglGetCurrentContext(),
                            Eq(dummy_ctx));
                    })
                ));

    auto egl_delegate = std::make_shared<mgc::EGLContextExecutor>(
        std::make_unique<DumbGLContext>(dummy_ctx));

    // Ensure we do not have a “context” current for creation
    eglMakeCurrent(dummy_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

    PlatformlessShmBuffer buffer{size, pixel_format, egl_delegate};

    // Ensure that the texture ID has been resolved, and to the value we expect
    EXPECT_THAT(buffer.tex_id(), Eq(tex_id));
}