Flutter Impeller
texture_source_vk.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_RENDERER_BACKEND_VULKAN_TEXTURE_SOURCE_VK_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEXTURE_SOURCE_VK_H_
7 
8 #include <cstdint>
9 #include <vector>
10 
11 #include "flutter/fml/status.h"
12 #include "impeller/core/formats.h"
19 
20 namespace impeller {
21 
22 // These methods should only be used by render_pass_vk.h
26 };
27 
28 //------------------------------------------------------------------------------
29 /// @brief Abstract base class that represents a vkImage and an
30 /// vkImageView.
31 ///
32 /// This is intended to be used with an impeller::TextureVK. Example
33 /// implementations represent swapchain images, uploaded textures,
34 /// Android Hardware Buffer backend textures, etc...
35 ///
37  public:
38  virtual ~TextureSourceVK();
39 
40  //----------------------------------------------------------------------------
41  /// @brief Gets the texture descriptor for this image source.
42  ///
43  /// @warning Texture descriptors from texture sources whose capabilities
44  /// are a superset of those that can be expressed with Vulkan
45  /// (like Android Hardware Buffer) are inferred. Stuff like size,
46  /// mip-counts, types is reliable. So use these descriptors as
47  /// advisory. Creating copies of texture sources from these
48  /// descriptors is usually not possible and depends on the
49  /// allocator used.
50  ///
51  /// @return The texture descriptor.
52  ///
54 
55  //----------------------------------------------------------------------------
56  /// @brief Get the image handle for this texture source.
57  ///
58  /// @return The image.
59  ///
60  virtual vk::Image GetImage() const = 0;
61 
62  //----------------------------------------------------------------------------
63  /// @brief Retrieve the image view used for sampling/blitting/compute
64  /// with this texture source.
65  ///
66  /// @return The image view.
67  ///
68  virtual vk::ImageView GetImageView() const = 0;
69 
70  //----------------------------------------------------------------------------
71  /// @brief Retrieve the image view used to attach a specific
72  /// subresource of this texture as a render target.
73  ///
74  /// The returned view covers a single mip level and a single
75  /// array layer (or cube map face), since attachment views cannot
76  /// span multiple levels or layers.
77  ///
78  /// @param[in] mip_level The mip level to attach.
79  /// @param[in] array_layer The array layer or cube map face to attach.
80  ///
81  /// @return The render target view.
82  ///
83  virtual vk::ImageView GetRenderTargetView(uint32_t mip_level,
84  uint32_t array_layer) const = 0;
85 
86  //----------------------------------------------------------------------------
87  /// @brief Encodes the layout transition `barrier` to
88  /// `barrier.cmd_buffer` for the image.
89  ///
90  /// The transition is from the layout stored via
91  /// `SetLayoutWithoutEncoding` to `barrier.new_layout`.
92  ///
93  /// @param[in] barrier The barrier.
94  ///
95  /// @return If the layout transition was successfully made.
96  ///
97  fml::Status SetLayout(const BarrierVK& barrier) const;
98 
99  //----------------------------------------------------------------------------
100  /// @brief Store the layout of the image.
101  ///
102  /// This just is bookkeeping on the CPU, to actually set the
103  /// layout use `SetLayout`.
104  ///
105  /// @param[in] layout The new layout.
106  ///
107  /// @return The old layout.
108  ///
109  vk::ImageLayout SetLayoutWithoutEncoding(vk::ImageLayout layout) const;
110 
111  //----------------------------------------------------------------------------
112  /// @brief Get the last layout assigned to the TextureSourceVK.
113  ///
114  /// This value is synchronized with the GPU via SetLayout so it
115  /// may not reflect the actual layout.
116  ///
117  /// @return The last known layout of the texture source.
118  ///
119  vk::ImageLayout GetLayout() const;
120 
121  //----------------------------------------------------------------------------
122  /// @brief When sampling from textures whose formats are not known to
123  /// Vulkan, a custom conversion is necessary to setup custom
124  /// samplers. This accessor provides this conversion if one is
125  /// present. Most texture source have none.
126  ///
127  /// @return The sampler conversion.
128  ///
129  virtual std::shared_ptr<YUVConversionVK> GetYUVConversion() const;
130 
131  //----------------------------------------------------------------------------
132  /// @brief Determines if swapchain image. That is, an image used as the
133  /// root render target.
134  ///
135  /// @return Whether or not this is a swapchain image.
136  ///
137  virtual bool IsSwapchainImage() const = 0;
138 
139  // These methods should only be used by render_pass_vk.h
140 
141  /// Store the framebuffer and render pass last used to render into the
142  /// `(sample_count, mip_level, slice)` subresource of this texture.
143  ///
144  /// This is only called when this texture is being used as the resolve (or
145  /// non-MSAA color) target of a render pass. By construction, the cached
146  /// objects are compatible with any future render pass that targets the
147  /// same subresource.
149  SampleCount sample_count,
150  uint32_t mip_level = 0u,
151  uint32_t slice = 0u);
152 
153  /// Retrieve the cached framebuffer and render pass for the given
154  /// `(sample_count, mip_level, slice)` subresource.
155  ///
156  /// An empty `FramebufferAndRenderPass` is returned when no cached entry
157  /// exists for that key. Entries are populated lazily on first use and
158  /// live for the lifetime of the texture.
160  uint32_t mip_level = 0u,
161  uint32_t slice = 0u) const;
162 
163  protected:
165 
166  explicit TextureSourceVK(TextureDescriptor desc);
167 
168  private:
169  struct CachedFrameDataEntry {
170  SampleCount sample_count;
171  uint32_t mip_level;
172  uint32_t slice;
174  };
175  // Linear-scanned because N is typically 1 and bounded by
176  // `sample_counts * mip_count * layer_count` for the rare textures that
177  // are rendered to across many subresources (e.g. a fully populated cube
178  // mip chain).
179  std::vector<CachedFrameDataEntry> frame_data_;
180  mutable vk::ImageLayout layout_ = vk::ImageLayout::eUndefined;
181 };
182 
183 } // namespace impeller
184 
185 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEXTURE_SOURCE_VK_H_
Abstract base class that represents a vkImage and an vkImageView.
TextureSourceVK(TextureDescriptor desc)
virtual std::shared_ptr< YUVConversionVK > GetYUVConversion() const
When sampling from textures whose formats are not known to Vulkan, a custom conversion is necessary t...
void SetCachedFrameData(const FramebufferAndRenderPass &data, SampleCount sample_count, uint32_t mip_level=0u, uint32_t slice=0u)
virtual bool IsSwapchainImage() const =0
Determines if swapchain image. That is, an image used as the root render target.
const TextureDescriptor & GetTextureDescriptor() const
Gets the texture descriptor for this image source.
FramebufferAndRenderPass GetCachedFrameData(SampleCount sample_count, uint32_t mip_level=0u, uint32_t slice=0u) const
virtual vk::ImageView GetImageView() const =0
Retrieve the image view used for sampling/blitting/compute with this texture source.
virtual vk::ImageView GetRenderTargetView(uint32_t mip_level, uint32_t array_layer) const =0
Retrieve the image view used to attach a specific subresource of this texture as a render target.
virtual vk::Image GetImage() const =0
Get the image handle for this texture source.
vk::ImageLayout GetLayout() const
Get the last layout assigned to the TextureSourceVK.
vk::ImageLayout SetLayoutWithoutEncoding(vk::ImageLayout layout) const
Store the layout of the image.
const TextureDescriptor desc_
fml::Status SetLayout(const BarrierVK &barrier) const
Encodes the layout transition barrier to barrier.cmd_buffer for the image.
std::shared_ptr< SharedObjectVKT< T > > SharedHandleVK
SampleCount
Definition: formats.h:483
Defines an operations and memory access barrier on a resource.
Definition: barrier_vk.h:27
SharedHandleVK< vk::Framebuffer > framebuffer
SharedHandleVK< vk::RenderPass > render_pass
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...