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

« back to all changes in this revision

Viewing changes to src/client/mesa/client_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: Kevin DuBois<kevin.dubois@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir_toolkit/mir_client_library.h"
 
20
#include "client_platform.h"
 
21
#include "client_buffer_factory.h"
 
22
#include "mesa_native_display_container.h"
 
23
#include "buffer_file_ops.h"
 
24
#include "native_surface.h"
 
25
#include "../mir_connection.h"
 
26
#include "../client_buffer_factory.h"
 
27
#include "../native_client_platform_factory.h"
 
28
 
 
29
#include <sys/mman.h>
 
30
#include <unistd.h>
 
31
 
 
32
namespace mcl=mir::client;
 
33
namespace mclm=mir::client::mesa;
 
34
namespace geom=mir::geometry;
 
35
 
 
36
namespace
 
37
{
 
38
 
 
39
struct RealBufferFileOps : public mclm::BufferFileOps
 
40
{
 
41
    int close(int fd) const
 
42
    {
 
43
        while (::close(fd) == -1)
 
44
        {
 
45
            // Retry on EINTR, return error on anything else
 
46
            if (errno != EINTR)
 
47
                return errno;
 
48
        }
 
49
        return 0;
 
50
    }
 
51
 
 
52
    void* map(int fd, off_t offset, size_t size) const
 
53
    {
 
54
        return mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED,
 
55
                    fd, offset);
 
56
    }
 
57
 
 
58
    void unmap(void* addr, size_t size) const
 
59
    {
 
60
        munmap(addr, size);
 
61
    }
 
62
};
 
63
 
 
64
struct NativeDisplayDeleter
 
65
{
 
66
    NativeDisplayDeleter(mcl::EGLNativeDisplayContainer& container)
 
67
    : container(container)
 
68
    {
 
69
    }
 
70
    void operator() (EGLNativeDisplayType* p)
 
71
    {
 
72
        auto display = *(reinterpret_cast<MirEGLNativeDisplayType*>(p));
 
73
        container.release(display);
 
74
        delete p;
 
75
    }
 
76
 
 
77
    mcl::EGLNativeDisplayContainer& container;
 
78
};
 
79
 
 
80
}
 
81
 
 
82
std::shared_ptr<mcl::ClientPlatform>
 
83
mcl::NativeClientPlatformFactory::create_client_platform(mcl::ClientContext* context)
 
84
{
 
85
    auto buffer_file_ops = std::make_shared<RealBufferFileOps>();
 
86
    return std::make_shared<mclm::ClientPlatform>(
 
87
        context, buffer_file_ops, mcl::EGLNativeDisplayContainer::instance());
 
88
}
 
89
 
 
90
mclm::ClientPlatform::ClientPlatform(
 
91
        ClientContext* const context,
 
92
        std::shared_ptr<BufferFileOps> const& buffer_file_ops,
 
93
        mcl::EGLNativeDisplayContainer& display_container)
 
94
    : context{context},
 
95
      buffer_file_ops{buffer_file_ops},
 
96
      display_container(display_container)
 
97
{
 
98
}
 
99
 
 
100
std::shared_ptr<mcl::ClientBufferFactory> mclm::ClientPlatform::create_buffer_factory()
 
101
{
 
102
    return std::make_shared<mclm::ClientBufferFactory>(buffer_file_ops);
 
103
}
 
104
 
 
105
namespace
 
106
{
 
107
struct NativeWindowDeleter
 
108
{
 
109
    NativeWindowDeleter(mclm::NativeSurface* window)
 
110
     : window(window) {}
 
111
 
 
112
    void operator()(EGLNativeWindowType* type)
 
113
    {
 
114
        delete type;
 
115
        delete window;
 
116
    }
 
117
 
 
118
private:
 
119
    mclm::NativeSurface* window;
 
120
};
 
121
}
 
122
 
 
123
std::shared_ptr<EGLNativeWindowType> mclm::ClientPlatform::create_egl_native_window(ClientSurface* client_surface)
 
124
{
 
125
    //TODO: this is awkward on both android and gbm...
 
126
    auto native_window = new NativeSurface(*client_surface);
 
127
    auto egl_native_window = new EGLNativeWindowType;
 
128
    *egl_native_window = native_window;
 
129
    NativeWindowDeleter deleter(native_window);
 
130
    return std::shared_ptr<EGLNativeWindowType>(egl_native_window, deleter);
 
131
}
 
132
 
 
133
std::shared_ptr<EGLNativeDisplayType> mclm::ClientPlatform::create_egl_native_display()
 
134
{
 
135
    MirEGLNativeDisplayType *mir_native_display = new MirEGLNativeDisplayType;
 
136
    *mir_native_display = display_container.create(context->mir_connection());
 
137
    auto egl_native_display = reinterpret_cast<EGLNativeDisplayType*>(mir_native_display);
 
138
 
 
139
    return std::shared_ptr<EGLNativeDisplayType>(egl_native_display, NativeDisplayDeleter(display_container));
 
140
}
 
141
 
 
142
MirPlatformType mclm::ClientPlatform::platform_type() const
 
143
{
 
144
    return mir_platform_type_gbm;
 
145
}
 
146
 
 
147
MirNativeBuffer* mclm::ClientPlatform::convert_native_buffer(graphics::NativeBuffer* buf) const
 
148
{
 
149
    //MirNativeBuffer is type-compatible with the MirNativeBuffer
 
150
    return buf;
 
151
}