1 // Copyright © 2017-2021 Rémi Thebault
2 module wayland.server.shm;
3 
4 import wayland.server.protocol : WlShm;
5 import wayland.server.core;
6 import wayland.native.server;
7 import wayland.native.util;
8 import wayland.util;
9 
10 class WlShmBuffer : Native!wl_shm_buffer
11 {
12     mixin nativeImpl!wl_shm_buffer;
13 
14     private this(wl_shm_buffer* native)
15     {
16         _native = native;
17         ObjectCache.set(native, this);
18     }
19 
20     static WlShmBuffer get(WlResource res)
21     {
22         auto natBuf = wl_shm_buffer_get(res.native);
23         if (!natBuf) return null;
24         auto buf = cast(WlShmBuffer)ObjectCache.get(natBuf);
25         if (!buf)
26         {
27             buf = new WlShmBuffer(natBuf);
28         }
29         return buf;
30     }
31 
32     void beginAccess()
33     {
34         wl_shm_buffer_begin_access(native);
35     }
36 
37     void endAccess()
38     {
39         wl_shm_buffer_end_access(native);
40     }
41 
42     @property void[] data()
43     {
44         auto dp = wl_shm_buffer_get_data(native);
45         if (!dp) return null;
46         return dp[0 .. height*stride];
47     }
48 
49     @property size_t stride()
50     {
51         return wl_shm_buffer_get_stride(native);
52     }
53 
54     @property int width()
55     {
56         return wl_shm_buffer_get_width(native);
57     }
58 
59     @property int height()
60     {
61         return wl_shm_buffer_get_height(native);
62     }
63 
64     @property WlShm.Format format()
65     {
66         return cast(WlShm.Format)wl_shm_buffer_get_format(native);
67     }
68 
69     WlShmPool refPool()
70     {
71         return new WlShmPool(wl_shm_buffer_ref_pool(native));
72     }
73 }
74 
75 class WlShmPool : Native!wl_shm_pool
76 {
77     mixin nativeImpl!wl_shm_pool;
78 
79     private this(wl_shm_pool* native)
80     {
81         _native = native;
82     }
83 
84     void unref()
85     {
86         wl_shm_pool_unref(native);
87     }
88 }