2
* Copyright © 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
19
#include "src/platform/graphics/mesa/shm_buffer.h"
20
#include "src/platform/graphics/mesa/shm_file.h"
22
#include "mir_test_doubles/mock_gl.h"
24
#include <gtest/gtest.h>
25
#include <gmock/gmock.h>
27
namespace mg = mir::graphics;
28
namespace mgm = mir::graphics::mesa;
29
namespace mtd = mir::test::doubles;
30
namespace geom = mir::geometry;
35
struct StubShmFile : public mgm::ShmFile
37
void* base_ptr() const { return fake_mapping; }
38
int fd() const { return fake_fd; }
40
void* const fake_mapping = reinterpret_cast<void*>(0x12345678);
41
int const fake_fd = 17;
44
struct ShmBufferTest : public testing::Test
48
pixel_format{mir_pixel_format_bgr_888},
49
stub_shm_file{std::make_shared<StubShmFile>()},
50
shm_buffer{stub_shm_file, size, pixel_format}
54
geom::Size const size;
55
MirPixelFormat const pixel_format;
56
std::shared_ptr<StubShmFile> const stub_shm_file;
57
mgm::ShmBuffer shm_buffer;
58
testing::NiceMock<mtd::MockGL> mock_gl;
63
TEST_F(ShmBufferTest, has_correct_properties)
65
size_t const bytes_per_pixel = MIR_BYTES_PER_PIXEL(pixel_format);
66
size_t const expected_stride{bytes_per_pixel * size.width.as_uint32_t()};
68
EXPECT_EQ(size, shm_buffer.size());
69
EXPECT_EQ(geom::Stride{expected_stride}, shm_buffer.stride());
70
EXPECT_EQ(pixel_format, shm_buffer.pixel_format());
73
TEST_F(ShmBufferTest, native_buffer_contains_correct_data)
75
size_t const bytes_per_pixel = MIR_BYTES_PER_PIXEL(pixel_format);
76
size_t const expected_stride{bytes_per_pixel * size.width.as_uint32_t()};
78
auto native_buffer = shm_buffer.native_buffer_handle();
80
EXPECT_EQ(1, native_buffer->fd_items);
81
EXPECT_EQ(stub_shm_file->fake_fd, native_buffer->fd[0]);
83
EXPECT_EQ(size.width.as_int(), native_buffer->width);
84
EXPECT_EQ(size.height.as_int(), native_buffer->height);
85
EXPECT_EQ(expected_stride, static_cast<size_t>(native_buffer->stride));
88
TEST_F(ShmBufferTest, cannot_be_used_for_bypass)
90
EXPECT_FALSE(shm_buffer.can_bypass());
93
TEST_F(ShmBufferTest, uploads_pixels_to_texture)
95
using namespace testing;
97
EXPECT_CALL(mock_gl, glTexImage2D(GL_TEXTURE_2D, 0, _,
98
size.width.as_int(), size.height.as_int(),
99
0, _, _, stub_shm_file->fake_mapping));
100
shm_buffer.bind_to_texture();