Fix screen flicker
[apps/agl-service-windowmanager-2017.git] / src / wayland.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef WM_WAYLAND_HPP
18 #define WM_WAYLAND_HPP
19
20 #include "controller_hooks.hpp"
21 #include "ivi-controller-client-protocol.h"
22 #include "util.hpp"
23
24 #include <functional>
25 #include <memory>
26 #include <unordered_map>
27 #include <vector>
28
29 /**
30  * @struct wayland_proxy
31  */
32 template <typename ProxyT>
33 struct wayland_proxy {
34    std::unique_ptr<ProxyT, std::function<void(ProxyT *)>> proxy;
35    wayland_proxy(wayland_proxy const &) = delete;
36    wayland_proxy &operator=(wayland_proxy const &) = delete;
37    wayland_proxy(void *p)
38       : wayland_proxy(p,
39                       reinterpret_cast<void (*)(ProxyT *)>(wl_proxy_destroy)) {}
40    wayland_proxy(void *p, std::function<void(ProxyT *)> &&p_del)
41       : proxy(std::unique_ptr<ProxyT, std::function<void(ProxyT *)>>(
42            static_cast<ProxyT *>(p), p_del)) {}
43 };
44
45 /**
46  * namespace wl
47  */
48 namespace wl {
49
50 /**
51  * @struct registry
52  */
53 struct registry : public wayland_proxy<struct wl_registry> {
54    typedef std::function<void(struct wl_registry *, uint32_t, uint32_t)> binder;
55    std::unordered_map<std::string, binder> bindings;
56
57    registry(registry const &) = delete;
58    registry &operator=(registry const &) = delete;
59    registry(struct wl_display *d);
60
61    void add_global_handler(char const *iface, binder bind);
62
63    // Events
64    void global(uint32_t name, char const *iface, uint32_t v);
65    void global_remove(uint32_t name);
66 };
67
68 /**
69  * @struct display
70  */
71 struct display {
72    std::unique_ptr<struct wl_display, void (*)(struct wl_display *)> d;
73    struct registry r;
74
75    display(display const &) = delete;
76    display &operator=(display const &) = delete;
77    display();
78    bool ok() const;
79    void roundtrip();
80    int dispatch();
81    int dispatch_pending();
82    int read_events();
83    void flush();
84    int get_fd() const;
85    int get_error();
86
87    // Lets just proxy this for the registry
88    inline void add_global_handler(char const *iface, registry::binder bind) {
89       this->r.add_global_handler(iface, bind);
90    }
91 };
92
93 /**
94  * @struct output
95  */
96 struct output : wayland_proxy<struct wl_output> {
97    int width{};
98    int height{};
99    int refresh{};
100    int transform{};
101
102    output(output const &) = delete;
103    output &operator=(output const &) = delete;
104    output(struct wl_registry *r, uint32_t name, uint32_t v);
105
106    // Events
107    void geometry(int32_t x, int32_t y, int32_t pw, int32_t ph, int32_t subpel,
108                  char const *make, char const *model, int32_t tx);
109    void mode(uint32_t flags, int32_t w, int32_t h, int32_t r);
110    void done();
111    void scale(int32_t factor);
112 };
113 }  // namespace wl
114
115 /**
116  * namespace compositor
117  */
118 namespace compositor {
119
120 struct size {
121    uint32_t w, h;
122 };
123
124 struct rect {
125    int32_t w, h;
126    int32_t x, y;
127 };
128
129 static const constexpr rect full_rect = rect{-1, -1, 0, 0};
130
131 inline bool operator==(struct rect a, struct rect b) {
132    return a.w == b.w && a.h == b.h && a.x == b.x && a.y == b.y;
133 }
134
135 struct controller;
136
137 struct controller_child {
138    struct controller *parent;
139    uint32_t id;
140
141    controller_child(controller_child const &) = delete;
142    controller_child &operator=(controller_child const &) = delete;
143    controller_child(struct controller *c, uint32_t i) : parent(c), id(i) {}
144    virtual ~controller_child() {}
145 };
146
147 struct surface_properties {
148    uint32_t id;  // let's just save an ID here too
149    struct rect dst_rect;
150    struct rect src_rect;
151    struct size size;
152    int32_t orientation;
153    int32_t visibility;
154    float opacity;
155 };
156
157 /**
158  * @struct surface
159  */
160 struct surface : public wayland_proxy<struct ivi_controller_surface>,
161                  controller_child {
162    surface(surface const &) = delete;
163    surface &operator=(surface const &) = delete;
164    surface(uint32_t i, struct controller *c);
165
166    // Requests
167    void set_visibility(uint32_t visibility);
168    void set_opacity(wl_fixed_t opacity);
169    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
170                              int32_t height);
171    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
172                                   int32_t height);
173    void set_configuration(int32_t width, int32_t height);
174    void set_orientation(int32_t orientation);
175    void screenshot(const char *filename);
176    void send_stats();
177    void destroy(int32_t destroy_scene_object);
178 };
179
180 /**
181  * @struct layer
182  */
183 struct layer : public wayland_proxy<struct ivi_controller_layer>,
184                controller_child {
185    layer(layer const &) = delete;
186    layer &operator=(layer const &) = delete;
187    layer(uint32_t i, struct controller *c);
188    layer(uint32_t i, int32_t w, int32_t h, struct controller *c);
189
190    // Requests
191    void set_visibility(uint32_t visibility);
192    void set_opacity(wl_fixed_t opacity);
193    void set_source_rectangle(int32_t x, int32_t y, int32_t width,
194                              int32_t height);
195    void set_destination_rectangle(int32_t x, int32_t y, int32_t width,
196                                   int32_t height);
197    void set_configuration(int32_t width, int32_t height);
198    void set_orientation(int32_t orientation);
199    void screenshot(const char *filename);
200    void clear_surfaces();
201    void add_surface(struct surface *surface);
202    void remove_surface(struct surface *surface);
203    void set_render_order(std::vector<uint32_t> const &ro);
204 };
205
206 /**
207  * @struct screen
208  */
209 struct screen : public wayland_proxy<struct ivi_controller_screen>,
210                 controller_child {
211    screen(screen const &) = delete;
212    screen &operator=(screen const &) = delete;
213    screen(uint32_t i, struct controller *c, struct ivi_controller_screen *p);
214    void clear();
215    void add_layer(layer *l);
216    void set_render_order(std::vector<uint32_t> const &ro);
217 };
218
219 /**
220  * @struct controller
221  */
222 struct controller : public wayland_proxy<struct ivi_controller> {
223    // This controller is still missing ivi-input
224
225    typedef std::unordered_map<uintptr_t, uint32_t> proxy_to_id_map_type;
226    typedef std::unordered_map<uint32_t, std::unique_ptr<struct surface>>
227       surface_map_type;
228    typedef std::unordered_map<uint32_t, std::unique_ptr<struct layer>>
229       layer_map_type;
230    typedef std::unordered_map<uint32_t, std::unique_ptr<struct screen>>
231       screen_map_type;
232
233    typedef std::unordered_map<uint32_t, struct surface_properties> props_map;
234
235    // HACK:
236    // The order of these member is mandatory, as when objects are destroyed
237    // they will call their parent (that's us right here!) and remove their
238    // proxy-to-id mapping. I.e. the *_proxy_to_id members need to be valid
239    // when the surfaces/layers/screens maps are destroyed. This sucks, but
240    // I cannot see a better solution w/o globals or some other horrible
241    // call-our-parent construct.
242    proxy_to_id_map_type surface_proxy_to_id;
243    proxy_to_id_map_type layer_proxy_to_id;
244    proxy_to_id_map_type screen_proxy_to_id;
245
246    props_map sprops;
247    props_map lprops;
248
249    surface_map_type surfaces;
250    layer_map_type layers;
251    screen_map_type screens;
252
253    size output_size;
254
255    wm::controller_hooks *chooks;
256
257    bool is_configured;
258
259    void add_proxy_to_id_mapping(struct ivi_controller_surface *p, uint32_t id);
260    void remove_proxy_to_id_mapping(struct ivi_controller_surface *p);
261    void add_proxy_to_id_mapping(struct ivi_controller_layer *p, uint32_t id);
262    void remove_proxy_to_id_mapping(struct ivi_controller_layer *p);
263    void add_proxy_to_id_mapping(struct wl_output *p, uint32_t id);
264    void remove_proxy_to_id_mapping(struct wl_output *p);
265
266    bool surface_exists(uint32_t id) const {
267       return this->surfaces.find(id) != this->surfaces.end();
268    }
269
270    bool layer_exists(uint32_t id) const {
271       return this->layers.find(id) != this->layers.end();
272    }
273
274    controller(struct wl_registry *r, uint32_t name, uint32_t version);
275
276    // Requests
277    void commit_changes() const {
278       ivi_controller_commit_changes(this->proxy.get());
279    }
280    void layer_create(uint32_t id, int32_t w, int32_t h);
281    void surface_create(uint32_t id);
282
283    // Events
284    // controller
285    void controller_screen(uint32_t id, struct ivi_controller_screen *screen);
286    void controller_layer(uint32_t id);
287    void controller_surface(uint32_t id);
288    void controller_error(int32_t object_id, int32_t object_type,
289                          int32_t error_code, char const *error_text);
290
291    // surface
292    void surface_visibility(struct surface *s, int32_t visibility);
293    void surface_opacity(struct surface *s, float opacity);
294    void surface_source_rectangle(struct surface *s, int32_t x, int32_t y,
295                                  int32_t width, int32_t height);
296    void surface_destination_rectangle(struct surface *s, int32_t x, int32_t y,
297                                       int32_t width, int32_t height);
298    void surface_configuration(struct surface *s, int32_t width, int32_t height);
299    void surface_orientation(struct surface *s, int32_t orientation);
300    void surface_pixelformat(struct surface *s, int32_t pixelformat);
301    void surface_layer(struct surface *s, struct ivi_controller_layer *layer);
302    void surface_stats(struct surface *s, uint32_t redraw_count,
303                       uint32_t frame_count, uint32_t update_count, uint32_t pid,
304                       const char *process_name);
305    void surface_destroyed(struct surface *s);
306    void surface_content(struct surface *s, int32_t content_state);
307
308    // layer
309    void layer_visibility(struct layer *l, int32_t visibility);
310    void layer_opacity(struct layer *l, float opacity);
311    void layer_source_rectangle(struct layer *l, int32_t x, int32_t y,
312                                int32_t width, int32_t height);
313    void layer_destination_rectangle(struct layer *l, int32_t x, int32_t y,
314                                     int32_t width, int32_t height);
315    void layer_configuration(struct layer *l, int32_t width, int32_t height);
316    void layer_orientation(struct layer *l, int32_t orientation);
317    void layer_screen(struct layer *l, struct wl_output *screen);
318    void layer_destroyed(struct layer *l);
319 };
320 }  // namespace compositor
321
322 #endif  // !WM_WAYLAND_HPP