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 ConnectionSurfaceMap;
64 class DisplayConfiguration;
65 class EventHandlerRegister;
66 class AsyncBufferFactory;
67 class MirBuffer;
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 register_error_callback(mir_error_callback callback, void* context);
139 
140  void populate(MirPlatformPackage& platform_package);
141  void populate_graphics_module(MirModuleProperties& properties) override;
142  MirDisplayConfiguration* create_copy_of_display_config();
143  std::unique_ptr<mir::protobuf::DisplayConfiguration> snapshot_display_configuration() const;
144  void available_surface_formats(MirPixelFormat* formats,
145  unsigned int formats_size, unsigned int& valid_formats);
146 
147  std::shared_ptr<mir::client::ClientBufferStream> make_consumer_stream(
148  mir::protobuf::BufferStream const& protobuf_bs);
149 
150  MirWaitHandle* create_client_buffer_stream(
151  int width, int height,
152  MirPixelFormat format,
153  MirBufferUsage buffer_usage,
155  void *context);
156  MirWaitHandle* release_buffer_stream(
159  void *context);
160 
161  void create_presentation_chain(
163  void *context);
164  void release_presentation_chain(MirPresentationChain* context);
165 
166  void release_consumer_stream(mir::client::ClientBufferStream*);
167 
168  static bool is_valid(MirConnection *connection);
169 
170  EGLNativeDisplayType egl_native_display();
171  MirPixelFormat egl_pixel_format(EGLDisplay, EGLConfig) const;
172 
173  void on_stream_created(int id, mir::client::ClientBufferStream* stream);
174 
175  MirWaitHandle* configure_display(MirDisplayConfiguration* configuration);
176  void done_display_configure();
177 
178  MirWaitHandle* set_base_display_configuration(MirDisplayConfiguration const* configuration);
179  void preview_base_display_configuration(
180  mir::protobuf::DisplayConfiguration const& configuration,
181  std::chrono::seconds timeout);
182  void confirm_base_display_configuration(
183  mir::protobuf::DisplayConfiguration const& configuration);
184  void done_set_base_display_configuration();
185 
186  std::shared_ptr<mir::client::rpc::MirBasicRpcChannel> rpc_channel() const
187  {
188  return channel;
189  }
190 
191  mir::client::rpc::DisplayServer& display_server();
192  mir::client::rpc::DisplayServerDebug& debug_display_server();
193  std::shared_ptr<mir::input::InputDevices> const& the_input_devices() const
194  {
195  return input_devices;
196  }
197 
198  void allocate_buffer(
200  mir_buffer_callback callback, void* context);
201  void release_buffer(mir::client::MirBuffer* buffer);
202 
203 private:
204  //google cant have callbacks with more than 2 args
205  struct SurfaceCreationRequest
206  {
207  SurfaceCreationRequest(mir_surface_callback cb, void* context, MirSurfaceSpec const& spec) :
208  cb(cb), context(context), spec(spec),
209  response(std::make_shared<mir::protobuf::Surface>()),
210  wh(std::make_shared<MirWaitHandle>())
211  {
212  }
214  void* context;
215  MirSurfaceSpec const spec;
216  std::shared_ptr<mir::protobuf::Surface> response;
217  std::shared_ptr<MirWaitHandle> wh;
218  };
219  std::vector<std::shared_ptr<SurfaceCreationRequest>> surface_requests;
220  void surface_created(SurfaceCreationRequest*);
221 
222  struct StreamCreationRequest
223  {
224  StreamCreationRequest(
225  mir_buffer_stream_callback cb, void* context, mir::protobuf::BufferStreamParameters const& params) :
226  callback(cb), context(context), parameters(params), response(std::make_shared<mir::protobuf::BufferStream>()),
227  wh(std::make_shared<MirWaitHandle>())
228  {
229  }
231  void* context;
232  mir::protobuf::BufferStreamParameters const parameters;
233  std::shared_ptr<mir::protobuf::BufferStream> response;
234  std::shared_ptr<MirWaitHandle> const wh;
235  };
236  std::vector<std::shared_ptr<StreamCreationRequest>> stream_requests;
237  void stream_created(StreamCreationRequest*);
238  void stream_error(std::string const& error_msg, std::shared_ptr<StreamCreationRequest> const& request);
239 
240  struct ChainCreationRequest
241  {
242  ChainCreationRequest(mir_presentation_chain_callback cb, void* context) :
243  callback(cb), context(context),
244  response(std::make_shared<mir::protobuf::BufferStream>())
245  {
246  }
247 
249  void* context;
250  std::shared_ptr<mir::protobuf::BufferStream> response;
251  };
252  std::vector<std::shared_ptr<ChainCreationRequest>> context_requests;
253  void context_created(ChainCreationRequest*);
254  void chain_error(std::string const& error_msg, std::shared_ptr<ChainCreationRequest> const& request);
255 
256  void populate_server_package(MirPlatformPackage& platform_package) override;
257  // MUST be first data member so it is destroyed last.
258  struct Deregisterer
259  { MirConnection* const self; ~Deregisterer(); } deregisterer;
260 
261  mutable std::mutex mutex; // Protects all members of *this (except release_wait_handles)
262 
263  std::shared_ptr<mir::client::ClientPlatform> platform;
264  std::shared_ptr<mir::client::ConnectionSurfaceMap> surface_map;
265  std::shared_ptr<mir::client::AsyncBufferFactory> buffer_factory;
266  std::shared_ptr<mir::client::rpc::MirBasicRpcChannel> const channel;
269  std::shared_ptr<mir::logging::Logger> const logger;
270  std::unique_ptr<mir::protobuf::Void> void_response;
271  std::unique_ptr<mir::protobuf::Connection> connect_result;
272  std::atomic<bool> connect_done;
273  std::unique_ptr<mir::protobuf::Void> ignored;
274  std::unique_ptr<mir::protobuf::ConnectParameters> connect_parameters;
275  std::unique_ptr<mir::protobuf::PlatformOperationMessage> platform_operation_reply;
276  std::unique_ptr<mir::protobuf::DisplayConfiguration> display_configuration_response;
277  std::unique_ptr<mir::protobuf::Void> set_base_display_configuration_response;
278  std::atomic<bool> disconnecting{false};
279 
280  mir::frontend::SurfaceId next_error_id(std::unique_lock<std::mutex> const&);
281  int surface_error_id{-1};
282 
283  std::shared_ptr<mir::client::ClientPlatformFactory> const client_platform_factory;
284  std::shared_ptr<mir::client::ClientBufferFactory> client_buffer_factory;
285  std::shared_ptr<EGLNativeDisplayType> native_display;
286 
287  std::shared_ptr<mir::input::receiver::InputPlatform> const input_platform;
288 
289  std::string error_message;
290 
291  MirWaitHandle connect_wait_handle;
292  MirWaitHandle disconnect_wait_handle;
293  MirWaitHandle platform_operation_wait_handle;
294  MirWaitHandle configure_display_wait_handle;
295  MirWaitHandle set_base_display_configuration_wait_handle;
296 
297  std::mutex release_wait_handle_guard;
298  std::vector<MirWaitHandle*> release_wait_handles;
299 
300  std::shared_ptr<mir::client::DisplayConfiguration> const display_configuration;
301  std::shared_ptr<mir::input::InputDevices> const input_devices;
302 
303  std::shared_ptr<mir::client::LifecycleControl> const lifecycle_control;
304 
305  std::shared_ptr<mir::client::PingHandler> const ping_handler;
306 
307 
308  std::shared_ptr<mir::client::EventHandlerRegister> const event_handler_register;
309 
310  std::unique_ptr<google::protobuf::Closure> const pong_callback;
311 
312  std::unique_ptr<mir::dispatch::ThreadedDispatcher> const eventloop;
313 
315 
316  struct SurfaceRelease;
317  struct StreamRelease;
318 
319  MirConnection* next_valid{nullptr};
320 
321  void set_error_message(std::string const& error);
322  void done_disconnect();
323  void connected(mir_connected_callback callback, void * context);
324  void released(SurfaceRelease);
325  void released(StreamRelease);
326  void done_platform_operation(mir_platform_operation_callback, void* context);
327  bool validate_user_display_config(MirDisplayConfiguration const* config);
328 
329  int const nbuffers;
330 };
331 
332 #endif /* MIR_CLIENT_MIR_CONNECTION_H_ */
Definition: size.h:30
Definition: mir_buffer.h:34
All things Mir.
Definition: atomic_callback.h:25
Definition: client_types.h:207
Definition: client_types.h:353
Definition: mir_surface.h:86
Definition: mir_surface.h:147
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:115
struct MirBuffer MirBuffer
Definition: client_types_nbs.h:33
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:96
Retrieved information about a loadable module.
Definition: client_types.h:223
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:125
struct MirPlatformMessage MirPlatformMessage
Definition: client_types.h:68
Definition: mir_display_server_debug.h:32
std::shared_ptr< mir::input::InputDevices > const & the_input_devices() const
Definition: mir_connection.h:193
void(* mir_platform_operation_callback)(MirConnection *connection, MirPlatformMessage *reply, void *context)
Callback called when a platform operation completes.
Definition: client_types.h:439
struct MirConnection MirConnection
Definition: client_types.h:40
Definition: mir_presentation_chain.h:26
unsigned int width
Definition: touchspot_image.c:4
void(* mir_error_callback)(MirConnection *connection, MirError const *error, void *context)
Definition: client_types.h:477
MirBufferUsage
MirBufferUsage specifies how a surface can and will be used.
Definition: client_types.h:159
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:87
Definition: client_context.h:29
std::shared_ptr< mir::client::rpc::MirBasicRpcChannel > rpc_channel() const
Definition: mir_connection.h:186
void(* mir_connected_callback)(MirConnection *connection, void *client_context)
Callback to be passed when issuing a mir_connect request.
Definition: client_types.h:76
Definition: client_buffer_stream.h:68
error
Definition: connector_report_tp.h:56
unsigned int height
Definition: touchspot_image.c:5
Definition: mir_connection.h:96
void(* mir_buffer_callback)(MirBuffer *, void *context)
Definition: client_types_nbs.h:36
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:134
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 Thu Sep 8 14:50:19 UTC 2016