Mir
mir_connection.h
Go to the documentation of this file.
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 #ifndef MIR_CLIENT_MIR_CONNECTION_H_
19 #define MIR_CLIENT_MIR_CONNECTION_H_
20 
21 #include "mir_wait_handle.h"
22 #include "lifecycle_control.h"
23 #include "ping_handler.h"
24 #include "rpc/mir_display_server.h"
26 
27 #include "mir/geometry/size.h"
28 #include "mir/client_platform.h"
30 #include "mir/client_context.h"
33 #include "mir_surface.h"
34 #include "display_configuration.h"
35 
36 #include <atomic>
37 #include <memory>
38 #include <mutex>
39 #include <string>
40 #include <unordered_set>
41 #include <unordered_map>
42 
43 namespace mir
44 {
45 namespace input
46 {
47 class InputDevices;
48 }
49 namespace protobuf
50 {
51 class BufferStream;
52 class Connection;
53 class ConnectParameters;
54 class PlatformOperationMessage;
55 class DisplayConfiguration;
56 }
58 namespace client
59 {
60 class ConnectionConfiguration;
61 class ClientPlatformFactory;
62 class ClientBufferStream;
63 class ClientBufferStreamFactory;
64 class ConnectionSurfaceMap;
65 class DisplayConfiguration;
66 class EventHandlerRegister;
67 class AsyncBufferFactory;
68 
69 namespace rpc
70 {
71 class DisplayServer;
72 class DisplayServerDebug;
73 class MirBasicRpcChannel;
74 }
75 }
76 
77 namespace input
78 {
79 namespace receiver
80 {
81 class InputPlatform;
82 }
83 }
84 
85 namespace logging
86 {
87 class Logger;
88 }
89 
90 namespace dispatch
91 {
92 class ThreadedDispatcher;
93 }
94 }
95 
97 {
98 public:
99  MirConnection(std::string const& error_message);
100 
102  ~MirConnection() noexcept;
103 
104  MirConnection(MirConnection const &) = delete;
105  MirConnection& operator=(MirConnection const &) = delete;
106 
107  MirWaitHandle* create_surface(
108  MirSurfaceSpec const& spec,
109  mir_surface_callback callback,
110  void * context);
111  MirWaitHandle* release_surface(
112  MirSurface *surface,
113  mir_surface_callback callback,
114  void *context);
115 
116  MirPromptSession* create_prompt_session();
117 
118  char const * get_error_message();
119 
120  MirWaitHandle* connect(
121  const char* app_name,
122  mir_connected_callback callback,
123  void * context);
124 
125  MirWaitHandle* disconnect();
126 
127  MirWaitHandle* platform_operation(
128  MirPlatformMessage const* request,
129  mir_platform_operation_callback callback, void* context);
130 
131  void register_lifecycle_event_callback(mir_lifecycle_event_callback callback, void* context);
132 
133  void register_ping_event_callback(mir_ping_event_callback callback, void* context);
134  void pong(int32_t serial);
135 
136  void register_display_change_callback(mir_display_config_callback callback, void* context);
137 
138  void populate(MirPlatformPackage& platform_package);
139  void populate_graphics_module(MirModuleProperties& properties);
140  MirDisplayConfiguration* create_copy_of_display_config();
141  std::unique_ptr<mir::protobuf::DisplayConfiguration> snapshot_display_configuration() const;
142  void available_surface_formats(MirPixelFormat* formats,
143  unsigned int formats_size, unsigned int& valid_formats);
144 
145  std::shared_ptr<mir::client::ClientBufferStream> make_consumer_stream(
146  mir::protobuf::BufferStream const& protobuf_bs, mir::geometry::Size);
147 
148  MirWaitHandle* create_client_buffer_stream(
149  int width, int height,
150  MirPixelFormat format,
151  MirBufferUsage buffer_usage,
153  void *context);
154  MirWaitHandle* release_buffer_stream(
157  void *context);
158 
159  void create_presentation_chain(
161  void *context);
162  void release_presentation_chain(MirPresentationChain* context);
163 
164  void release_consumer_stream(mir::client::ClientBufferStream*);
165 
166  static bool is_valid(MirConnection *connection);
167 
168  EGLNativeDisplayType egl_native_display();
169  MirPixelFormat egl_pixel_format(EGLDisplay, EGLConfig) const;
170 
171  void on_stream_created(int id, mir::client::ClientBufferStream* stream);
172 
173  MirWaitHandle* configure_display(MirDisplayConfiguration* configuration);
174  void done_display_configure();
175 
176  MirWaitHandle* set_base_display_configuration(MirDisplayConfiguration const* configuration);
177  void done_set_base_display_configuration();
178 
179  std::shared_ptr<mir::client::rpc::MirBasicRpcChannel> rpc_channel() const
180  {
181  return channel;
182  }
183 
184  mir::client::rpc::DisplayServer& display_server();
185  mir::client::rpc::DisplayServerDebug& debug_display_server();
186  std::shared_ptr<mir::input::InputDevices> const& the_input_devices() const
187  {
188  return input_devices;
189  }
190 
191  void allocate_buffer(
193  mir_buffer_callback callback, void* context);
194  void release_buffer(int buffer_id);
195 
196 private:
197  //google cant have callbacks with more than 2 args
198  struct SurfaceCreationRequest
199  {
200  SurfaceCreationRequest(mir_surface_callback cb, void* context, MirSurfaceSpec const& spec) :
201  cb(cb), context(context), spec(spec),
202  response(std::make_shared<mir::protobuf::Surface>()),
203  wh(std::make_shared<MirWaitHandle>())
204  {
205  }
207  void* context;
208  MirSurfaceSpec const spec;
209  std::shared_ptr<mir::protobuf::Surface> response;
210  std::shared_ptr<MirWaitHandle> wh;
211  };
212  std::vector<std::shared_ptr<SurfaceCreationRequest>> surface_requests;
213  void surface_created(SurfaceCreationRequest*);
214 
215  struct StreamCreationRequest
216  {
217  StreamCreationRequest(
218  mir_buffer_stream_callback cb, void* context, mir::protobuf::BufferStreamParameters const& params) :
219  callback(cb), context(context), parameters(params), response(std::make_shared<mir::protobuf::BufferStream>()),
220  wh(std::make_shared<MirWaitHandle>())
221  {
222  }
224  void* context;
225  mir::protobuf::BufferStreamParameters const parameters;
226  std::shared_ptr<mir::protobuf::BufferStream> response;
227  std::shared_ptr<MirWaitHandle> const wh;
228  };
229  std::vector<std::shared_ptr<StreamCreationRequest>> stream_requests;
230  void stream_created(StreamCreationRequest*);
231  void stream_error(std::string const& error_msg, std::shared_ptr<StreamCreationRequest> const& request);
232 
233  struct ChainCreationRequest
234  {
235  ChainCreationRequest(mir_presentation_chain_callback cb, void* context) :
236  callback(cb), context(context),
237  response(std::make_shared<mir::protobuf::BufferStream>())
238  {
239  }
240 
242  void* context;
243  std::shared_ptr<mir::protobuf::BufferStream> response;
244  };
245  std::vector<std::shared_ptr<ChainCreationRequest>> context_requests;
246  void context_created(ChainCreationRequest*);
247  void chain_error(std::string const& error_msg, std::shared_ptr<ChainCreationRequest> const& request);
248 
249  void populate_server_package(MirPlatformPackage& platform_package) override;
250  // MUST be first data member so it is destroyed last.
251  struct Deregisterer
252  { MirConnection* const self; ~Deregisterer(); } deregisterer;
253 
254  mutable std::mutex mutex; // Protects all members of *this (except release_wait_handles)
255 
256  std::shared_ptr<mir::client::ConnectionSurfaceMap> surface_map;
257  std::shared_ptr<mir::client::AsyncBufferFactory> buffer_factory;
258  std::shared_ptr<mir::client::rpc::MirBasicRpcChannel> const channel;
261  std::shared_ptr<mir::logging::Logger> const logger;
262  std::unique_ptr<mir::protobuf::Void> void_response;
263  std::unique_ptr<mir::protobuf::Connection> connect_result;
264  std::atomic<bool> connect_done;
265  std::unique_ptr<mir::protobuf::Void> ignored;
266  std::unique_ptr<mir::protobuf::ConnectParameters> connect_parameters;
267  std::unique_ptr<mir::protobuf::PlatformOperationMessage> platform_operation_reply;
268  std::unique_ptr<mir::protobuf::DisplayConfiguration> display_configuration_response;
269  std::unique_ptr<mir::protobuf::Void> set_base_display_configuration_response;
270  std::atomic<bool> disconnecting{false};
271 
272  mir::frontend::SurfaceId next_error_id(std::unique_lock<std::mutex> const&);
273  int surface_error_id{-1};
274 
275  std::shared_ptr<mir::client::ClientPlatformFactory> const client_platform_factory;
276  std::shared_ptr<mir::client::ClientPlatform> platform;
277  std::shared_ptr<mir::client::ClientBufferFactory> client_buffer_factory;
278  std::shared_ptr<EGLNativeDisplayType> native_display;
279 
280  std::shared_ptr<mir::input::receiver::InputPlatform> const input_platform;
281 
282  std::string error_message;
283 
284  MirWaitHandle connect_wait_handle;
285  MirWaitHandle disconnect_wait_handle;
286  MirWaitHandle platform_operation_wait_handle;
287  MirWaitHandle configure_display_wait_handle;
288  MirWaitHandle set_base_display_configuration_wait_handle;
289 
290  std::mutex release_wait_handle_guard;
291  std::vector<MirWaitHandle*> release_wait_handles;
292 
293  std::shared_ptr<mir::client::DisplayConfiguration> const display_configuration;
294  std::shared_ptr<mir::input::InputDevices> const input_devices;
295 
296  std::shared_ptr<mir::client::LifecycleControl> const lifecycle_control;
297 
298  std::shared_ptr<mir::client::PingHandler> const ping_handler;
299 
300 
301  std::shared_ptr<mir::client::EventHandlerRegister> const event_handler_register;
302 
303  std::unique_ptr<google::protobuf::Closure> const pong_callback;
304 
305  std::unique_ptr<mir::dispatch::ThreadedDispatcher> const eventloop;
306 
307  std::shared_ptr<mir::client::ClientBufferStreamFactory> buffer_stream_factory;
308 
309  struct SurfaceRelease;
310  struct StreamRelease;
311 
312  MirConnection* next_valid{nullptr};
313 
314  void set_error_message(std::string const& error);
315  void done_disconnect();
316  void connected(mir_connected_callback callback, void * context);
317  void released(SurfaceRelease);
318  void released(StreamRelease);
319  void done_platform_operation(mir_platform_operation_callback, void* context);
320  bool validate_user_display_config(MirDisplayConfiguration const* config);
321 
322  int const nbuffers;
323 };
324 
325 #endif /* MIR_CLIENT_MIR_CONNECTION_H_ */
Definition: size.h:30
All things Mir.
Definition: atomic_callback.h:25
Definition: client_types.h:203
Definition: client_types.h:347
Definition: mir_surface.h:86
Definition: mir_surface.h:146
void(* mir_lifecycle_event_callback)(MirConnection *connection, MirLifecycleState state, void *context)
Callback called when a lifecycle event/callback is requested from the running server.
Definition: client_types.h:112
Definition: mir_wait_handle.h:31
void(* mir_buffer_stream_callback)(MirBufferStream *stream, void *client_context)
Callback to be passed when calling:
Definition: client_types.h:93
void(* mir_buffer_callback)(MirPresentationChain *, MirBuffer *, void *context)
Definition: client_types_nbs.h:36
Retrieved information about a loadable module.
Definition: client_types.h:219
void(* mir_ping_event_callback)(MirConnection *connection, int32_t serial, void *context)
Callback called when the server pings for responsiveness testing.
Definition: client_types.h:122
struct MirPlatformMessage MirPlatformMessage
Definition: client_types.h:65
Definition: mir_display_server_debug.h:32
std::shared_ptr< mir::input::InputDevices > const & the_input_devices() const
Definition: mir_connection.h:186
void(* mir_platform_operation_callback)(MirConnection *connection, MirPlatformMessage *reply, void *context)
Callback called when a platform operation completes.
Definition: client_types.h:433
struct MirConnection MirConnection
Definition: client_types.h:40
Definition: mir_presentation_chain.h:26
unsigned int width
Definition: touchspot_image.c:4
MirBufferUsage
MirBufferUsage specifies how a surface can and will be used.
Definition: client_types.h:156
Definition: mir_prompt_session.h:49
MirPixelFormat
32-bit pixel formats (8888): The order of components in the enum matches the order of the components ...
Definition: common.h:134
void(* mir_surface_callback)(MirSurface *surface, void *client_context)
Callback to be passed when calling:
Definition: client_types.h:84
Definition: client_context.h:29
std::shared_ptr< mir::client::rpc::MirBasicRpcChannel > rpc_channel() const
Definition: mir_connection.h:179
void(* mir_connected_callback)(MirConnection *connection, void *client_context)
Callback to be passed when issuing a mir_connect request.
Definition: client_types.h:73
Definition: client_buffer_stream.h:69
error
Definition: connector_report_tp.h:56
unsigned int height
Definition: touchspot_image.c:5
Definition: mir_connection.h:96
Definition: connection_configuration.h:57
void(* mir_display_config_callback)(MirConnection *connection, void *context)
Callback called when a display config change has occurred.
Definition: client_types.h:131
int const size
Definition: make_socket_rpc_channel.cpp:51
void(* mir_presentation_chain_callback)(MirPresentationChain *, void *context)
Definition: client_types_nbs.h:37
Definition: mir_display_server.h:33

Copyright © 2012-2015 Canonical Ltd.
Generated on Wed Mar 30 00:29:56 UTC 2016