| Flutter Impeller |

texture_descriptor.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_CORE_TEXTURE_DESCRIPTOR_H_
6 #define FLUTTER_IMPELLER_CORE_TEXTURE_DESCRIPTOR_H_
7
8 #include <cstdint>
9 #include "impeller/core/formats.h"
10 #include "impeller/geometry/size.h"
11
12 namespace impeller {
13
14 //------------------------------------------------------------------------------
15 /// @brief Additional compression to apply to a texture. This value is
16 /// ignored on platforms which do not support it.
17 ///
18 /// Lossy compression is only supported on iOS 15+ on A15 chips.
19 enum class CompressionType {
20 kLossless,
21 kLossy,
22 };
23
24 constexpr const char* CompressionTypeToString(CompressionType type) {
25 switch (type) {
26 case CompressionType::kLossless:
27 return "Lossless";
28 case CompressionType::kLossy:
29 return "Lossy";
30 }
31 FML_UNREACHABLE();
32 }
33
34 //------------------------------------------------------------------------------
35 /// @brief A lightweight object that describes the attributes of a texture
36 /// that can then used an allocator to create that texture.
37 ///
38 struct TextureDescriptor {
39 StorageMode storage_mode = StorageMode::kDeviceTransient;
40 TextureType type = TextureType::kTexture2D;
41 PixelFormat format = PixelFormat::kUnknown;
42 ISize size;
43 size_t mip_count = 1u; // Size::MipCount is usually appropriate.
44 TextureUsageMask usage = TextureUsage::kShaderRead;
45 SampleCount sample_count = SampleCount::kCount1;
46 CompressionType compression_type = CompressionType::kLossless;
47
48 constexpr size_t GetByteSizeOfBaseMipLevel() const {
49 if (!IsValid()) {
50 return 0u;
51 }
52 return size.Area() * BytesPerPixelForPixelFormat(format);
53 }
54
55 constexpr size_t GetByteSizeOfAllMipLevels() const {
56 if (!IsValid()) {
57 return 0u;
58 }
59 size_t result = 0u;
60 int64_t width = size.width;
61 int64_t height = size.height;
62 for (auto i = 0u; i < mip_count; i++) {
63 result +=
64 ISize(width, height).Area() * BytesPerPixelForPixelFormat(format);
65 width /= 2;
66 height /= 2;
67 }
68 return result;
69 }
70
71 constexpr size_t GetBytesPerRow() const {
72 if (!IsValid()) {
73 return 0u;
74 }
75 return size.width * BytesPerPixelForPixelFormat(format);
76 }
77
78 constexpr bool SamplingOptionsAreValid() const {
79 const auto count = static_cast<uint64_t>(sample_count);
80 return IsMultisampleCapable(type) ? count > 1 : count == 1;
81 }
82
83 constexpr bool operator==(const TextureDescriptor& other) const = default;
84
85 constexpr bool IsValid() const {
86 return format != PixelFormat::kUnknown && //
87 !size.IsEmpty() && //
88 mip_count >= 1u && //
89 SamplingOptionsAreValid();
90 }
91 };
92
93 std::string TextureDescriptorToString(const TextureDescriptor& desc);
94
95 } // namespace impeller
96
97 #endif // FLUTTER_IMPELLER_CORE_TEXTURE_DESCRIPTOR_H_
type
GLenum type
Definition: blit_command_gles.cc:153
formats.h
impeller
Definition: allocation.cc:12
impeller::BytesPerPixelForPixelFormat
constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format)
Definition: formats.h:469
impeller::TextureDescriptorToString
std::string TextureDescriptorToString(const TextureDescriptor &desc)
Definition: texture_descriptor.cc:11
impeller::StorageMode
StorageMode
Specified where the allocation resides and how it is used.
Definition: formats.h:32
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
impeller::PixelFormat::kUnknown
@ kUnknown
impeller::TextureType
TextureType
Definition: formats.h:265
impeller::TextureType::kTexture2D
@ kTexture2D
impeller::CompressionType
CompressionType
Additional compression to apply to a texture. This value is ignored on platforms which do not support...
Definition: texture_descriptor.h:19
impeller::CompressionType::kLossy
@ kLossy
impeller::CompressionType::kLossless
@ kLossless
impeller::ISize
ISize64 ISize
Definition: size.h:162
impeller::IsMultisampleCapable
constexpr bool IsMultisampleCapable(TextureType type)
Definition: formats.h:286
impeller::CompressionTypeToString
constexpr const char * CompressionTypeToString(CompressionType type)
Definition: texture_descriptor.h:24
impeller::TextureUsage::kShaderRead
@ kShaderRead
impeller::SampleCount
SampleCount
Definition: formats.h:298
impeller::SampleCount::kCount1
@ kCount1
size.h
impeller::Mask< TextureUsage >
impeller::TSize
Definition: size.h:25
impeller::TSize::Area
constexpr Type Area() const
Definition: size.h:120
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::width
Type width
Definition: size.h:28
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: size.h:123
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::TextureDescriptor::GetByteSizeOfBaseMipLevel
constexpr size_t GetByteSizeOfBaseMipLevel() const
Definition: texture_descriptor.h:48
impeller::TextureDescriptor::GetBytesPerRow
constexpr size_t GetBytesPerRow() const
Definition: texture_descriptor.h:71
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:44
impeller::TextureDescriptor::GetByteSizeOfAllMipLevels
constexpr size_t GetByteSizeOfAllMipLevels() const
Definition: texture_descriptor.h:55
impeller::TextureDescriptor::SamplingOptionsAreValid
constexpr bool SamplingOptionsAreValid() const
Definition: texture_descriptor.h:78
impeller::TextureDescriptor::compression_type
CompressionType compression_type
Definition: texture_descriptor.h:46
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:40
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::TextureDescriptor::IsValid
constexpr bool IsValid() const
Definition: texture_descriptor.h:85
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:45
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:43
impeller::TextureDescriptor::operator==
constexpr bool operator==(const TextureDescriptor &other) const =default
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41