Flutter Impeller
trampoline.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_TOOLKIT_GLVK_TRAMPOLINE_H_
6 #define FLUTTER_IMPELLER_TOOLKIT_GLVK_TRAMPOLINE_H_
7 
14 
15 namespace impeller::glvk {
16 
17 class AutoTrampolineContext;
18 
19 //------------------------------------------------------------------------------
20 /// @brief An object used to interoperate between OpenGL and Vulkan.
21 ///
22 /// While these are not super expensive to create, they do manage an
23 /// internal EGL context as well as some OpenGL state. For this
24 /// reason, it is recommended that callers cache these for the
25 /// duration of the lifecycle of main rendering context.
26 ///
27 class Trampoline {
28  public:
29  //----------------------------------------------------------------------------
30  /// @brief Constructs a new trampoline. It is recommended that these
31  /// objects be cached and reused for all conversion operations.
32  ///
33  /// EGL contexts on already bound to the callers thread may become
34  /// unbound after a call to this method.
35  ///
36  Trampoline();
37 
38  //----------------------------------------------------------------------------
39  /// @brief Destroys the trampoline. There are no threading restrictions.
40  /// EGL contexts on already bound to the callers thread may become
41  /// unbound after a call to this method.
42  ///
43  ~Trampoline();
44 
45  Trampoline(const Trampoline&) = delete;
46 
47  Trampoline& operator=(const Trampoline&) = delete;
48 
49  //----------------------------------------------------------------------------
50  /// @brief Determines if this is a valid trampoline. There is no error
51  /// recovery mechanism if a trampoline cannot be constructed and
52  /// an invalid trampoline must be immediately discarded.
53  ///
54  /// @return True if valid, False otherwise.
55  ///
56  bool IsValid() const;
57 
58  //----------------------------------------------------------------------------
59  /// @brief Describes an OpenGL texture along with information on how to
60  /// sample from it.
61  ///
62  struct GLTextureInfo {
63  //--------------------------------------------------------------------------
64  /// The OpenGL texture handle.
65  ///
66  GLuint texture = 0;
67  //--------------------------------------------------------------------------
68  /// The OpenGL texture target enum. For instance, GL_TEXTURE_2D or
69  /// GL_TEXTURE_EXTERNAL_OES.
70  ///
71  GLenum target = 0;
72  //--------------------------------------------------------------------------
73  /// A transformation applied to the texture coordinates in the form of (u,
74  /// v, 0, 1) when sampling from the texture.
75  ///
77  };
78 
79  //----------------------------------------------------------------------------
80  /// @brief Perform a blit operation from the source OpenGL texture to a
81  /// target Vulkan texture.
82  ///
83  /// It is the callers responsibility to ensure that the EGL
84  /// context associated with the trampoline is already current
85  /// before making this call.
86  ///
87  /// It is also the responsibility of the caller to ensure that the
88  /// destination texture is the color-attachment-optimal layout.
89  /// Failure to ensure this will lead to validation error.
90  ///
91  /// @see `MakeCurrentContext`
92  ///
93  /// @param[in] src_texture The source OpenGL texture.
94  /// @param[in] dst_texture The destination Vulkan texture.
95  ///
96  /// @return True if the blit was successful, False otherwise.
97  ///
98  bool BlitTextureOpenGLToVulkan(const GLTextureInfo& src_texture,
99  const AHBTextureSourceVK& dst_texture) const;
100 
101  //----------------------------------------------------------------------------
102  /// @brief Make the EGL context associated with this trampoline current
103  /// on the calling thread.
104  ///
105  /// @return The automatic trampoline context. The collection of this
106  /// context clears the threads EGL binding.
107  ///
108  [[nodiscard]] AutoTrampolineContext MakeCurrentContext() const;
109 
110  private:
111  friend class AutoTrampolineContext;
112 
113  std::unique_ptr<egl::Display> egl_display_;
114  std::unique_ptr<egl::Context> egl_context_;
115  std::unique_ptr<egl::Surface> egl_surface_;
116  std::unique_ptr<ProcTable> gl_;
117  GLuint program_ = GL_NONE;
118  GLint texture_uniform_location_ = 0;
119  GLint uv_transformation_location_ = 0;
120  bool is_valid_ = false;
121 };
122 
123 //------------------------------------------------------------------------------
124 /// @brief An RAII object that makes the trampolines EGL context current
125 /// when constructed and clears the EGL binding on destruction.
126 ///
128  public:
129  //----------------------------------------------------------------------------
130  /// @brief Constructs a new instance and makes the trampolines EGL
131  /// context current on the calling thread.
132  ///
133  /// @param[in] trampoline The trampoline.
134  ///
135  explicit AutoTrampolineContext(const Trampoline& trampoline);
136 
137  //----------------------------------------------------------------------------
138  /// @brief Destroys the object and clears the previous EGL binding.
139  ///
141 
143 
145 
146  private:
147  const egl::Context* context_ = nullptr;
148  const egl::Surface* surface_ = nullptr;
149 };
150 
151 } // namespace impeller::glvk
152 
153 #endif // FLUTTER_IMPELLER_TOOLKIT_GLVK_TRAMPOLINE_H_
A texture source that wraps an instance of AHardwareBuffer.
An instance of an EGL context.
Definition: context.h:30
An instance of an EGL surface. There is no ability to create surfaces directly. Instead,...
Definition: surface.h:18
An RAII object that makes the trampolines EGL context current when constructed and clears the EGL bin...
Definition: trampoline.h:127
~AutoTrampolineContext()
Destroys the object and clears the previous EGL binding.
Definition: trampoline.cc:326
AutoTrampolineContext(const Trampoline &trampoline)
Constructs a new instance and makes the trampolines EGL context current on the calling thread.
Definition: trampoline.cc:318
AutoTrampolineContext & operator=(const AutoTrampolineContext &)=delete
AutoTrampolineContext(const AutoTrampolineContext &)=delete
An object used to interoperate between OpenGL and Vulkan.
Definition: trampoline.h:27
~Trampoline()
Destroys the trampoline. There are no threading restrictions. EGL contexts on already bound to the ca...
Definition: trampoline.cc:153
bool BlitTextureOpenGLToVulkan(const GLTextureInfo &src_texture, const AHBTextureSourceVK &dst_texture) const
Perform a blit operation from the source OpenGL texture to a target Vulkan texture.
Definition: trampoline.cc:197
Trampoline & operator=(const Trampoline &)=delete
Trampoline(const Trampoline &)=delete
bool IsValid() const
Determines if this is a valid trampoline. There is no error recovery mechanism if a trampoline cannot...
Definition: trampoline.cc:161
AutoTrampolineContext MakeCurrentContext() const
Make the EGL context associated with this trampoline current on the calling thread.
Definition: trampoline.cc:313
Trampoline()
Constructs a new trampoline. It is recommended that these objects be cached and reused for all conver...
Definition: trampoline.cc:57
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
Describes an OpenGL texture along with information on how to sample from it.
Definition: trampoline.h:62