~alan-griffiths/mir/fix-1661151

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
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
 */

#include "connection_surface_map.h"
#include "mir_surface.h"
#include "presentation_chain.h"

#include <boost/throw_exception.hpp>
#include <sstream>

namespace mcl=mir::client;
namespace mf=mir::frontend;

using mir::client::ConnectionSurfaceMap;
using mir::frontend::SurfaceId;
using mir::frontend::BufferStreamId;

std::shared_ptr<MirWindow> ConnectionSurfaceMap::surface(SurfaceId id) const
{
    std::shared_lock<decltype(guard)> lk(guard);
    auto const found = surfaces.find(id);
    return found != surfaces.end() ? found->second : nullptr;
}

void mcl::ConnectionSurfaceMap::insert(mf::SurfaceId surface_id, std::shared_ptr<MirWindow> const& surface)
{
    std::lock_guard<decltype(guard)> lk(guard);
    surfaces[surface_id] = surface;
}

void mcl::ConnectionSurfaceMap::erase(mf::SurfaceId surface_id)
{
    std::lock_guard<decltype(guard)> lk(guard);
    surfaces.erase(surface_id);
}

std::shared_ptr<MirBufferStream> ConnectionSurfaceMap::stream(BufferStreamId id) const
{
    std::shared_lock<decltype(stream_guard)> lk(stream_guard);
    auto const found = streams.find(id);
    return found != streams.end() ? found->second : nullptr;
}

void mcl::ConnectionSurfaceMap::with_all_streams_do(std::function<void(MirBufferStream*)> const& fn) const
{
    std::shared_lock<decltype(stream_guard)> lk(stream_guard);
    for(auto const& stream : streams)
        fn(stream.second.get());
}

void mcl::ConnectionSurfaceMap::insert(
    mf::BufferStreamId stream_id, std::shared_ptr<MirBufferStream> const& stream)
{
    std::lock_guard<decltype(stream_guard)> lk(stream_guard);
    streams[stream_id] = stream;
}

void mcl::ConnectionSurfaceMap::insert(
    mf::BufferStreamId stream_id, std::shared_ptr<MirPresentationChain> const& chain)
{
    std::lock_guard<decltype(stream_guard)> lk(stream_guard);
    chains[stream_id] = chain; 
}

void mcl::ConnectionSurfaceMap::erase(mf::BufferStreamId stream_id)
{
    std::lock_guard<decltype(stream_guard)> lk(stream_guard);
    auto stream_it = streams.find(stream_id);
    auto chain_it = chains.find(stream_id);
    if (stream_it != streams.end())
        streams.erase(stream_it);
    if (chain_it != chains.end())
        chains.erase(chain_it);
}

void mcl::ConnectionSurfaceMap::insert(int buffer_id, std::shared_ptr<mcl::MirBuffer> const& buffer)
{
    std::lock_guard<decltype(buffer_guard)> lk(buffer_guard);
    buffers[buffer_id] = buffer;
}

void mcl::ConnectionSurfaceMap::erase(int buffer_id)
{
    std::lock_guard<decltype(buffer_guard)> lk(buffer_guard);
    buffers.erase(buffer_id);
}

std::shared_ptr<mcl::MirBuffer> mcl::ConnectionSurfaceMap::buffer(int buffer_id) const
{
    std::shared_lock<decltype(buffer_guard)> lk(buffer_guard);
    auto const it = buffers.find(buffer_id);
    if (it != buffers.end())
        return it->second;
    else
        BOOST_THROW_EXCEPTION(std::runtime_error("could not find buffer"));
}

void mcl::ConnectionSurfaceMap::erase(void* render_surface_key)
{
    std::lock_guard<decltype(guard)> lk(stream_guard);
    auto rs_it = render_surfaces.find(render_surface_key);
    if (rs_it != render_surfaces.end())
        render_surfaces.erase(rs_it);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
void mcl::ConnectionSurfaceMap::insert(void* render_surface_key, std::shared_ptr<MirRenderSurface> const& render_surface)
{
    std::lock_guard<decltype(guard)> lk(stream_guard);
    render_surfaces[render_surface_key] = render_surface;
}

std::shared_ptr<MirRenderSurface> mcl::ConnectionSurfaceMap::render_surface(void* render_surface_key) const
{
    std::shared_lock<decltype(guard)> lk(stream_guard);
    auto const it = render_surfaces.find(render_surface_key);
    if (it != render_surfaces.end())
        return it->second;
    else
        BOOST_THROW_EXCEPTION(std::runtime_error("could not find render surface"));
}
#pragma GCC diagnostic pop