Debian Codegraph @

xbmc/windowing/gbm/GBMUtils.h

raw
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #pragma once
10
11 #include <memory>
12 #include <queue>
13
14 #include <gbm.h>
15
16 namespace KODI
17 {
18 namespace WINDOWING
19 {
20 namespace GBM
21 {
22
23 /**
24 * @brief A wrapper for gbm c classes to allow OOP and RAII.
25 *
26 */
27 class CGBMUtils
28 {
29 public:
30 CGBMUtils(const CGBMUtils&) = delete;
31 CGBMUtils& operator=(const CGBMUtils&) = delete;
32 CGBMUtils() = default;
33 ~CGBMUtils() = default;
34
35 /**
36 * @brief Create a gbm device for allocating buffers
37 *
38 * @param fd The file descriptor for a backend device
39 * @return true The device creation succeeded
40 * @return false The device creation failed
41 */
42 bool CreateDevice(int fd);
43
44 /**
45 * @brief A wrapper for gbm_device to allow OOP and RAII
46 *
47 */
48 class CGBMDevice
49 {
50 public:
51 CGBMDevice(const CGBMDevice&) = delete;
52 CGBMDevice& operator=(const CGBMDevice&) = delete;
53 explicit CGBMDevice(gbm_device* device);
54 ~CGBMDevice() = default;
55
56 /**
57 * @brief Create a gbm surface
58 *
59 * @param width The width to use for the surface
60 * @param height The height to use for the surface
61 * @param format The format to use for the surface
62 * @param modifiers The modifiers to use for the surface
63 * @param modifiers_count The amount of modifiers in the modifiers param
64 * @return true The surface creation succeeded
65 * @return false The surface creation failed
66 */
67 bool CreateSurface(int width,
68 int height,
69 uint32_t format,
70 const uint64_t* modifiers,
71 const int modifiers_count);
72
73 /**
74 * @brief Get the underlying gbm_device
75 *
76 * @return gbm_device* A pointer to the underlying gbm_device
77 */
78 gbm_device* Get() const { return m_device; }
79
80 /**
81 * @brief A wrapper for gbm_surface to allow OOP and RAII
82 *
83 */
84 class CGBMSurface
85 {
86 public:
87 CGBMSurface(const CGBMSurface&) = delete;
88 CGBMSurface& operator=(const CGBMSurface&) = delete;
89 explicit CGBMSurface(gbm_surface* surface);
90 ~CGBMSurface() = default;
91
92 /**
93 * @brief Get the underlying gbm_surface
94 *
95 * @return gbm_surface* A pointer to the underlying gbm_surface
96 */
97 gbm_surface* Get() const { return m_surface; }
98
99 /**
100 * @brief A wrapper for gbm_bo to allow OOP and RAII
101 *
102 */
103 class CGBMSurfaceBuffer
104 {
105 public:
106 CGBMSurfaceBuffer(const CGBMSurfaceBuffer&) = delete;
107 CGBMSurfaceBuffer& operator=(const CGBMSurfaceBuffer&) = delete;
108 explicit CGBMSurfaceBuffer(gbm_surface* surface);
109 ~CGBMSurfaceBuffer();
110
111 /**
112 * @brief Get the underlying gbm_bo
113 *
114 * @return gbm_bo* A pointer to the underlying gbm_bo
115 */
116 gbm_bo* Get() const { return m_buffer; }
117
118 private:
119 gbm_surface* m_surface{nullptr};
120 gbm_bo* m_buffer{nullptr};
121 };
122
123 /**
124 * @brief Lock the surface's current front buffer.
125 *
126 * @return CGBMSurfaceBuffer* A pointer to a CGBMSurfaceBuffer object
127 */
128 CGBMSurfaceBuffer& LockFrontBuffer();
129
130 private:
131 gbm_surface* m_surface{nullptr};
132 std::queue<std::unique_ptr<CGBMSurfaceBuffer>> m_buffers;
133 };
134
135 /**
136 * @brief Get the CGBMSurface object
137 *
138 * @return CGBMSurface* A pointer to the CGBMSurface object
139 */
140 CGBMDevice::CGBMSurface& GetSurface() const { return *m_surface; }
141
142 private:
143 gbm_device* m_device{nullptr};
144
145 struct CGBMSurfaceDeleter
146 {
147 void operator()(CGBMSurface* p) const
148 {
149 if (p)
150 gbm_surface_destroy(p->Get());
151 }
152 };
153 std::unique_ptr<CGBMSurface, CGBMSurfaceDeleter> m_surface;
154 };
155
156 /**
157 * @brief Get the CGBMDevice object
158 *
159 * @return CGBMDevice* A pointer to the CGBMDevice object
160 */
161 CGBMUtils::CGBMDevice& GetDevice() const { return *m_device; }
162
163 private:
164 struct CGBMDeviceDeleter
165 {
166 void operator()(CGBMDevice* p) const
167 {
168 if (p)
169 gbm_device_destroy(p->Get());
170 }
171 };
172 std::unique_ptr<CGBMDevice, CGBMDeviceDeleter> m_device;
173 };
174
175 }
176 }
177 }