summaryrefslogtreecommitdiff
path: root/src/egl.h
blob: 0f16f88b58cdfce987b3ca98831c325d5469f03d (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2021 Benjamin Barenblat
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// An idiomatic C++ interface to EGL 1.5.

#ifndef GLPLANET_SRC_EGL_H_
#define GLPLANET_SRC_EGL_H_

#include <EGL/egl.h>
#include <X11/Xlib.h>

#include <utility>
#include <vector>

#include "src/undo_xlib_dot_h_namespace_pollution.h"
//

#include "src/util.h"
#include "third_party/abseil/absl/types/optional.h"

namespace egl {

class Context;
class Display;
class Surface;

// Makes a Context active in the current thread.
void BindContext(Surface&, Context&);

// Configuration attributes.
enum Attribute : EGLint {
  kBufferSize = EGL_BUFFER_SIZE,

  kRedSize = EGL_RED_SIZE,
  kGreenSize = EGL_GREEN_SIZE,
  kBlueSize = EGL_BLUE_SIZE,
  kLuminanceSize = EGL_LUMINANCE_SIZE,
  kAlphaSize = EGL_ALPHA_SIZE,
  kAlphaMaskSize = EGL_ALPHA_MASK_SIZE,

  kBindToTextureRgb = EGL_BIND_TO_TEXTURE_RGB,
  kBindToTextureRgba = EGL_BIND_TO_TEXTURE_RGBA,
  kColorBufferType = EGL_COLOR_BUFFER_TYPE,
  kConfigCaveat = EGL_CONFIG_CAVEAT,
  kConfigId = EGL_CONFIG_ID,
  kConformantMask = EGL_CONFORMANT,
  kDepthSize = EGL_DEPTH_SIZE,
  kLevel = EGL_LEVEL,
  kMatchNativePixmap = EGL_MATCH_NATIVE_PIXMAP,
  kMaxSwapInterval = EGL_MAX_SWAP_INTERVAL,
  kMinSwapInterval = EGL_MIN_SWAP_INTERVAL,
  kNativeRenderable = EGL_NATIVE_RENDERABLE,
  kNativeVisualId = EGL_NATIVE_VISUAL_ID,
  kNativeVisualType = EGL_NATIVE_VISUAL_TYPE,
  kSampleBuffers = EGL_SAMPLE_BUFFERS,
  kSamples = EGL_SAMPLES,
  kStencilSize = EGL_STENCIL_SIZE,
  kSurfaceTypeMask = EGL_SURFACE_TYPE,
  kTransparentType = EGL_TRANSPARENT_TYPE,
  kTransparentRedValue = EGL_TRANSPARENT_RED_VALUE,
  kTransparentGreenValue = EGL_TRANSPARENT_GREEN_VALUE,
  kTransparentBlueValue = EGL_TRANSPARENT_BLUE_VALUE,

  kRenderableTypeMask = EGL_RENDERABLE_TYPE,
};

// A "don't care" value for use in attribute lists.
constexpr int kDontCare = EGL_DONT_CARE;

enum class Api : EGLenum {
  kOpenglEs = EGL_OPENGL_ES_API,
  kOpenvg = EGL_OPENVG_API,
  kOpengl = EGL_OPENGL_API,
};

enum ApiMask : EGLint {
  kOpenglEsBit = EGL_OPENGL_ES_BIT,
  kOpenVgBit = EGL_OPENVG_BIT,
  kOpenglEs2Bit = EGL_OPENGL_ES2_BIT,
  kOpenglBit = EGL_OPENGL_BIT,
};

// The format, type, and size of the buffers for a drawable.
//
// This class is thread-safe.
class Configuration final {
 public:
  Configuration(const Configuration&) = default;
  Configuration& operator=(const Configuration&) = default;
  Configuration(Configuration&&) noexcept = default;
  Configuration& operator=(Configuration&&) noexcept = default;

  int Get(Attribute attribute) const;

 private:
  friend class Display;

  explicit Configuration(EGLDisplay display, EGLConfig config) noexcept
      : display_(display), config_(config) {}

  EGLDisplay display_;
  EGLConfig config_;
};

// A rendering surface.
//
// This class inherits the thread-safety properties of the Display that
// constructed it.
class Surface final {
 public:
  Surface(const Surface&) = default;
  Surface& operator=(const Surface&) = default;
  Surface(Surface&&) noexcept = default;
  Surface& operator=(Surface&&) noexcept = default;

  void SwapBuffers();

 private:
  friend class Display;
  friend void BindContext(Surface&, Context&);

  explicit Surface(EGLDisplay display, EGLSurface surface) noexcept
      : display_(display), surface_(surface) {}

  EGLDisplay display_;
  EGLSurface surface_;
};

// A rendering context.
//
// This class inherits the thread-safety properties of the Display that
// constructed it.
class Context final {
 public:
  Context(Context&&) noexcept = default;
  Context& operator=(Context&&) noexcept = default;

  ~Context() noexcept {
    // This should always succeed, since display_ and context_ are guaranteed
    // valid.
    DCHECK(eglDestroyContext(display_, context_));
  }

 private:
  friend class Display;
  friend void BindContext(Surface&, Context&);

  explicit Context(EGLDisplay display, EGLContext context) noexcept
      : display_(display), context_(context) {}

  EGLDisplay display_;
  EGLContext context_;
};

// The abstract display on which graphics are drawn.
//
// This class inherits the thread-safety properties of the underlying Xlib
// Display with which it is constructed.
class Display final {
 public:
  explicit Display(::Display* xlib_display);

  Display(Display&&) noexcept = default;
  Display& operator=(Display&&) noexcept = default;

  ~Display() noexcept;

  // Gets Configurations that match the specified attributes. If a limit is
  // specified, no more than that many Configurations will be returned.
  //
  // The returned Configurations hold references into this object and must not
  // outlive it.
  std::vector<Configuration> GetConfigurations(
      const std::vector<std::pair<Attribute, int>>& filter_attributes,
      absl::optional<int> limit = absl::nullopt) const;

  // Creates a rendering surface on a window.
  //
  // The returned Surface holds a reference into this Display and must not
  // outlive it.
  Surface CreateWindowSurface(const Configuration& config, ::Window window);

  // Creates an OpenGL context.
  //
  // The returned Context holds a reference into this Display and must not
  // outlive it.
  Context CreateContext(const Configuration& config, Api api, int major,
                        int minor);

 private:
  EGLDisplay display_;
};

}  // namespace egl

#endif  // GLPLANET_SRC_EGL_H_