~hikiko/mir/mir.unity8-desktop-session

« back to all changes in this revision

Viewing changes to tests/unit-tests/graphics/mesa/test_shm_buffer.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Daniel van Vugt, Ubuntu daily release
  • Date: 2014-01-08 02:04:38 UTC
  • mfrom: (1.1.54)
  • Revision ID: package-import@ubuntu.com-20140108020438-ikbu7qqm9v2l026y
Tags: 0.1.3+14.04.20140108-0ubuntu1
[ Daniel van Vugt ]
* Preparing for release 0.1.3

[ Ubuntu daily release ]
* Automatic snapshot from revision 1170

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2013 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 
17
 */
 
18
 
 
19
#include "src/platform/graphics/mesa/shm_buffer.h"
 
20
#include "src/platform/graphics/mesa/shm_file.h"
 
21
 
 
22
#include "mir_test_doubles/mock_gl.h"
 
23
 
 
24
#include <gtest/gtest.h>
 
25
#include <gmock/gmock.h>
 
26
 
 
27
namespace mg = mir::graphics;
 
28
namespace mgm = mir::graphics::mesa;
 
29
namespace mtd = mir::test::doubles;
 
30
namespace geom = mir::geometry;
 
31
 
 
32
namespace
 
33
{
 
34
 
 
35
struct StubShmFile : public mgm::ShmFile
 
36
{
 
37
    void* base_ptr() const { return fake_mapping; }
 
38
    int fd() const { return fake_fd; }
 
39
 
 
40
    void* const fake_mapping = reinterpret_cast<void*>(0x12345678);
 
41
    int const fake_fd = 17;
 
42
};
 
43
 
 
44
struct ShmBufferTest : public testing::Test
 
45
{
 
46
    ShmBufferTest()
 
47
        : size{150,340},
 
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}
 
51
    {
 
52
    }
 
53
 
 
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;
 
59
};
 
60
 
 
61
}
 
62
 
 
63
TEST_F(ShmBufferTest, has_correct_properties)
 
64
{
 
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()};
 
67
 
 
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());
 
71
}
 
72
 
 
73
TEST_F(ShmBufferTest, native_buffer_contains_correct_data)
 
74
{
 
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()};
 
77
 
 
78
    auto native_buffer = shm_buffer.native_buffer_handle();
 
79
 
 
80
    EXPECT_EQ(1, native_buffer->fd_items);
 
81
    EXPECT_EQ(stub_shm_file->fake_fd, native_buffer->fd[0]);
 
82
 
 
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));
 
86
}
 
87
 
 
88
TEST_F(ShmBufferTest, cannot_be_used_for_bypass)
 
89
{
 
90
    EXPECT_FALSE(shm_buffer.can_bypass());
 
91
}
 
92
 
 
93
TEST_F(ShmBufferTest, uploads_pixels_to_texture)
 
94
{
 
95
    using namespace testing;
 
96
 
 
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();
 
101
}