summaryrefslogtreecommitdiffstats
path: root/xbmc/windowing/gbm/GBMUtils.h
blob: 291a93a32d7dc41de7e2457a98a59a2e6d1f61e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include <memory>
#include <queue>

#include <gbm.h>

namespace KODI
{
namespace WINDOWING
{
namespace GBM
{

/**
 * @brief A wrapper for gbm c classes to allow OOP and RAII.
 *
 */
class CGBMUtils
{
public:
  CGBMUtils(const CGBMUtils&) = delete;
  CGBMUtils& operator=(const CGBMUtils&) = delete;
  CGBMUtils() = default;
  ~CGBMUtils() = default;

  /**
   * @brief Create a gbm device for allocating buffers
   *
   * @param fd The file descriptor for a backend device
   * @return true The device creation succeeded
   * @return false The device creation failed
   */
  bool CreateDevice(int fd);

  /**
   * @brief A wrapper for gbm_device to allow OOP and RAII
   *
   */
  class CGBMDevice
  {
  public:
    CGBMDevice(const CGBMDevice&) = delete;
    CGBMDevice& operator=(const CGBMDevice&) = delete;
    explicit CGBMDevice(gbm_device* device);
    ~CGBMDevice() = default;

    /**
     * @brief Create a gbm surface
     *
     * @param width The width to use for the surface
     * @param height The height to use for the surface
     * @param format The format to use for the surface
     * @param modifiers The modifiers to use for the surface
     * @param modifiers_count The amount of modifiers in the modifiers param
     * @return true The surface creation succeeded
     * @return false The surface creation failed
     */
    bool CreateSurface(int width,
                       int height,
                       uint32_t format,
                       const uint64_t* modifiers,
                       const int modifiers_count);

    /**
     * @brief Get the underlying gbm_device
     *
     * @return gbm_device* A pointer to the underlying gbm_device
     */
    gbm_device* Get() const { return m_device; }

    /**
     * @brief A wrapper for gbm_surface to allow OOP and RAII
     *
     */
    class CGBMSurface
    {
    public:
      CGBMSurface(const CGBMSurface&) = delete;
      CGBMSurface& operator=(const CGBMSurface&) = delete;
      explicit CGBMSurface(gbm_surface* surface);
      ~CGBMSurface() = default;

      /**
       * @brief Get the underlying gbm_surface
       *
       * @return gbm_surface* A pointer to the underlying gbm_surface
       */
      gbm_surface* Get() const { return m_surface; }

      /**
       * @brief A wrapper for gbm_bo to allow OOP and RAII
       *
       */
      class CGBMSurfaceBuffer
      {
      public:
        CGBMSurfaceBuffer(const CGBMSurfaceBuffer&) = delete;
        CGBMSurfaceBuffer& operator=(const CGBMSurfaceBuffer&) = delete;
        explicit CGBMSurfaceBuffer(gbm_surface* surface);
        ~CGBMSurfaceBuffer();

        /**
         * @brief Get the underlying gbm_bo
         *
         * @return gbm_bo* A pointer to the underlying gbm_bo
         */
        gbm_bo* Get() const { return m_buffer; }

      private:
        gbm_surface* m_surface{nullptr};
        gbm_bo* m_buffer{nullptr};
      };

      /**
       * @brief Lock the surface's current front buffer.
       *
       * @return CGBMSurfaceBuffer* A pointer to a CGBMSurfaceBuffer object
       */
      CGBMSurfaceBuffer* LockFrontBuffer();

    private:
      gbm_surface* m_surface{nullptr};
      std::queue<std::unique_ptr<CGBMSurfaceBuffer>> m_buffers;
    };

    /**
     * @brief Get the CGBMSurface object
     *
     * @return CGBMSurface* A pointer to the CGBMSurface object
     */
    CGBMDevice::CGBMSurface* GetSurface() const { return m_surface.get(); }

  private:
    gbm_device* m_device{nullptr};

    struct CGBMSurfaceDeleter
    {
      void operator()(CGBMSurface* p) const
      {
        if (p)
          gbm_surface_destroy(p->Get());
      }
    };
    std::unique_ptr<CGBMSurface, CGBMSurfaceDeleter> m_surface;
  };

  /**
   * @brief Get the CGBMDevice object
   *
   * @return CGBMDevice* A pointer to the CGBMDevice object
   */
  CGBMUtils::CGBMDevice* GetDevice() const { return m_device.get(); }

private:
  struct CGBMDeviceDeleter
  {
    void operator()(CGBMDevice* p) const
    {
      if (p)
        gbm_device_destroy(p->Get());
    }
  };
  std::unique_ptr<CGBMDevice, CGBMDeviceDeleter> m_device;
};

}
}
}