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

« back to all changes in this revision

Viewing changes to src/platform/graphics/mesa/platform.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 © 2012 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as 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 Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser 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 "platform.h"
 
20
#include "buffer_allocator.h"
 
21
#include "display.h"
 
22
#include "internal_client.h"
 
23
#include "internal_native_display.h"
 
24
#include "linux_virtual_terminal.h"
 
25
#include "mir/graphics/platform_ipc_package.h"
 
26
#include "mir/graphics/buffer_ipc_packer.h"
 
27
#include "mir/options/option.h"
 
28
#include "mir/graphics/native_buffer.h"
 
29
 
 
30
#include "drm_close_threadsafe.h"
 
31
 
 
32
#include <boost/throw_exception.hpp>
 
33
#include <stdexcept>
 
34
 
 
35
#include <fcntl.h>
 
36
#include <sys/ioctl.h>
 
37
 
 
38
namespace mg = mir::graphics;
 
39
namespace mgm = mg::mesa;
 
40
namespace mo = mir::options;
 
41
 
 
42
namespace
 
43
{
 
44
 
 
45
struct MesaPlatformIPCPackage : public mg::PlatformIPCPackage
 
46
{
 
47
    MesaPlatformIPCPackage(int drm_auth_fd)
 
48
    {
 
49
        ipc_fds.push_back(drm_auth_fd);
 
50
    }
 
51
 
 
52
    ~MesaPlatformIPCPackage()
 
53
    {
 
54
        if (ipc_fds.size() > 0 && ipc_fds[0] >= 0)
 
55
            mgm::drm_close_threadsafe(ipc_fds[0]);
 
56
    }
 
57
};
 
58
 
 
59
struct RealVTFileOperations : public mgm::VTFileOperations
 
60
{
 
61
    int open(char const* pathname, int flags)
 
62
    {
 
63
        return ::open(pathname, flags);
 
64
    }
 
65
 
 
66
    int close(int fd)
 
67
    {
 
68
        return ::close(fd);
 
69
    }
 
70
 
 
71
    int ioctl(int d, int request, int val)
 
72
    {
 
73
        return ::ioctl(d, request, val);
 
74
    }
 
75
 
 
76
    int ioctl(int d, int request, void* p_val)
 
77
    {
 
78
        return ::ioctl(d, request, p_val);
 
79
    }
 
80
 
 
81
    int tcsetattr(int d, int acts, const struct termios *tcattr)
 
82
    {
 
83
        return ::tcsetattr(d, acts, tcattr);
 
84
    }
 
85
 
 
86
    int tcgetattr(int d, struct termios *tcattr)
 
87
    {
 
88
        return ::tcgetattr(d, tcattr);
 
89
    }
 
90
};
 
91
 
 
92
}
 
93
 
 
94
std::shared_ptr<mgm::InternalNativeDisplay> mgm::Platform::internal_native_display;
 
95
bool mgm::Platform::internal_display_clients_present;
 
96
mgm::Platform::Platform(std::shared_ptr<DisplayReport> const& listener,
 
97
                        std::shared_ptr<VirtualTerminal> const& vt)
 
98
    : udev{std::make_shared<UdevContext>()},
 
99
      listener{listener},
 
100
      vt{vt}
 
101
{
 
102
    drm.setup(udev);
 
103
    gbm.setup(drm);
 
104
    internal_display_clients_present = false;
 
105
}
 
106
 
 
107
mgm::Platform::~Platform()
 
108
{
 
109
    internal_native_display.reset();
 
110
    internal_display_clients_present = false;
 
111
}
 
112
 
 
113
 
 
114
std::shared_ptr<mg::GraphicBufferAllocator> mgm::Platform::create_buffer_allocator(
 
115
        const std::shared_ptr<mg::BufferInitializer>& buffer_initializer)
 
116
{
 
117
    return std::make_shared<mgm::BufferAllocator>(gbm.device, buffer_initializer);
 
118
}
 
119
 
 
120
std::shared_ptr<mg::Display> mgm::Platform::create_display(
 
121
    std::shared_ptr<DisplayConfigurationPolicy> const& initial_conf_policy)
 
122
{
 
123
    return std::make_shared<mgm::Display>(
 
124
        this->shared_from_this(),
 
125
        initial_conf_policy,
 
126
        listener);
 
127
}
 
128
 
 
129
std::shared_ptr<mg::PlatformIPCPackage> mgm::Platform::get_ipc_package()
 
130
{
 
131
    return std::make_shared<MesaPlatformIPCPackage>(drm.get_authenticated_fd());
 
132
}
 
133
 
 
134
void mgm::Platform::fill_ipc_package(BufferIPCPacker* packer, Buffer const* buffer) const
 
135
{
 
136
    auto native_handle = buffer->native_buffer_handle();
 
137
    for(auto i=0; i<native_handle->data_items; i++)
 
138
    {
 
139
        packer->pack_data(native_handle->data[i]);
 
140
    }
 
141
    for(auto i=0; i<native_handle->fd_items; i++)
 
142
    {
 
143
        packer->pack_fd(native_handle->fd[i]);
 
144
    }
 
145
 
 
146
    packer->pack_stride(buffer->stride());
 
147
    packer->pack_flags(native_handle->flags);
 
148
    packer->pack_size(buffer->size());
 
149
}
 
150
 
 
151
void mgm::Platform::drm_auth_magic(drm_magic_t magic)
 
152
{
 
153
    drm.auth_magic(magic);
 
154
}
 
155
 
 
156
std::shared_ptr<mg::InternalClient> mgm::Platform::create_internal_client()
 
157
{
 
158
    if (!internal_native_display)
 
159
        internal_native_display = std::make_shared<mgm::InternalNativeDisplay>(get_ipc_package());
 
160
    internal_display_clients_present = true;
 
161
    return std::make_shared<mgm::InternalClient>(internal_native_display);
 
162
}
 
163
 
 
164
EGLNativeDisplayType mgm::Platform::egl_native_display() const
 
165
{
 
166
    return gbm.device;
 
167
}
 
168
 
 
169
extern "C" std::shared_ptr<mg::Platform> mg::create_platform(std::shared_ptr<mo::Option> const& options, std::shared_ptr<DisplayReport> const& report)
 
170
{
 
171
    auto real_fops = std::make_shared<RealVTFileOperations>();
 
172
    auto vt = std::make_shared<mgm::LinuxVirtualTerminal>(real_fops, options->get("vt", 0), report);
 
173
    return std::make_shared<mgm::Platform>(report, vt);
 
174
}
 
175
 
 
176
extern "C" int mir_server_mesa_egl_native_display_is_valid(MirMesaEGLNativeDisplay* display)
 
177
{
 
178
    return ((mgm::Platform::internal_display_clients_present) &&
 
179
            (display == mgm::Platform::internal_native_display.get()));
 
180
}