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