Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5843670

Browse files
committed
[Impeller] Add debug ToString methods to render targets.
Also fixes and issue where SPrintF would only consider strings 64 bytes or smaller.
1 parent 0a5ed01 commit 5843670

File tree

10 files changed

+198
-6
lines changed

10 files changed

+198
-6
lines changed

impeller/base/base_unittests.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include "flutter/testing/testing.h"
6+
#include "impeller/base/strings.h"
67
#include "impeller/base/thread.h"
78

89
namespace impeller {
@@ -68,5 +69,12 @@ TEST(ThreadTest, CanCreateRWMutexLock) {
6869
// f.mtx.UnlockReader(); <--- Static analysis error.
6970
}
7071

72+
TEST(StringsTest, CanSPrintF) {
73+
ASSERT_EQ(SPrintF("%sx%d", "Hello", 12), "Hellox12");
74+
ASSERT_EQ(SPrintF(""), "");
75+
ASSERT_EQ(SPrintF("Hello"), "Hello");
76+
ASSERT_EQ(SPrintF("%sx%.2f", "Hello", 12.122222), "Hellox12.12");
77+
}
78+
7179
} // namespace testing
7280
} // namespace impeller

impeller/base/strings.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ namespace impeller {
1010

1111
IMPELLER_PRINTF_FORMAT(1, 2)
1212
std::string SPrintF(const char* format, ...) {
13+
std::string ret_val;
1314
va_list list;
15+
va_list list2;
1416
va_start(list, format);
15-
char buffer[64] = {0};
16-
::vsnprintf(buffer, sizeof(buffer), format, list);
17+
va_copy(list2, list);
18+
if (auto string_length = ::vsnprintf(nullptr, 0, format, list);
19+
string_length >= 0) {
20+
auto buffer = reinterpret_cast<char*>(::malloc(string_length + 1));
21+
::vsnprintf(buffer, string_length + 1, format, list2);
22+
ret_val = std::string{buffer, static_cast<size_t>(string_length)};
23+
::free(buffer);
24+
}
25+
va_end(list2);
1726
va_end(list);
18-
return buffer;
27+
return ret_val;
1928
}
2029

2130
bool HasPrefix(const std::string& string, const std::string& prefix) {

impeller/core/formats.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,44 @@ std::string TextureUsageMaskToString(TextureUsageMask mask) {
101101
return stream.str();
102102
}
103103

104+
std::string AttachmentToString(const Attachment& attachment) {
105+
std::stringstream stream;
106+
if (attachment.texture) {
107+
stream << "Texture=("
108+
<< TextureDescriptorToString(
109+
attachment.texture->GetTextureDescriptor())
110+
<< "),";
111+
}
112+
if (attachment.resolve_texture) {
113+
stream << "ResolveTexture=("
114+
<< TextureDescriptorToString(
115+
attachment.resolve_texture->GetTextureDescriptor())
116+
<< "),";
117+
}
118+
stream << "LoadAction=" << LoadActionToString(attachment.load_action) << ",";
119+
stream << "StoreAction=" << StoreActionToString(attachment.store_action);
120+
return stream.str();
121+
}
122+
123+
std::string ColorAttachmentToString(const ColorAttachment& color) {
124+
std::stringstream stream;
125+
stream << AttachmentToString(color) << ",";
126+
stream << "ClearColor=(" << ColorToString(color.clear_color) << ")";
127+
return stream.str();
128+
}
129+
130+
std::string DepthAttachmentToString(const DepthAttachment& depth) {
131+
std::stringstream stream;
132+
stream << AttachmentToString(depth) << ",";
133+
stream << "ClearDepth=" << SPrintF("%.2f", depth.clear_depth);
134+
return stream.str();
135+
}
136+
137+
std::string StencilAttachmentToString(const StencilAttachment& stencil) {
138+
std::stringstream stream;
139+
stream << AttachmentToString(stencil) << ",";
140+
stream << "ClearStencil=" << stencil.clear_stencil;
141+
return stream.str();
142+
}
143+
104144
} // namespace impeller

impeller/core/formats.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,42 @@ enum class PixelFormat {
110110
kD32FloatS8UInt,
111111
};
112112

113+
constexpr const char* PixelFormatToString(PixelFormat format) {
114+
switch (format) {
115+
case PixelFormat::kUnknown:
116+
return "Unknown";
117+
case PixelFormat::kA8UNormInt:
118+
return "A8UNormInt";
119+
case PixelFormat::kR8UNormInt:
120+
return "R8UNormInt";
121+
case PixelFormat::kR8G8UNormInt:
122+
return "R8G8UNormInt";
123+
case PixelFormat::kR8G8B8A8UNormInt:
124+
return "R8G8B8A8UNormInt";
125+
case PixelFormat::kR8G8B8A8UNormIntSRGB:
126+
return "R8G8B8A8UNormIntSRGB";
127+
case PixelFormat::kB8G8R8A8UNormInt:
128+
return "B8G8R8A8UNormInt";
129+
case PixelFormat::kB8G8R8A8UNormIntSRGB:
130+
return "B8G8R8A8UNormIntSRGB";
131+
case PixelFormat::kR32G32B32A32Float:
132+
return "R32G32B32A32Float";
133+
case PixelFormat::kR16G16B16A16Float:
134+
return "R16G16B16A16Float";
135+
case PixelFormat::kB10G10R10XR:
136+
return "B10G10R10XR";
137+
case PixelFormat::kB10G10R10XRSRGB:
138+
return "B10G10R10XRSRGB";
139+
case PixelFormat::kB10G10R10A10XR:
140+
return "B10G10R10A10XR";
141+
case PixelFormat::kS8UInt:
142+
return "S8UInt";
143+
case PixelFormat::kD32FloatS8UInt:
144+
return "D32FloatS8UInt";
145+
}
146+
FML_UNREACHABLE();
147+
}
148+
113149
enum class BlendFactor {
114150
kZero,
115151
kOne,
@@ -147,6 +183,30 @@ enum class StoreAction {
147183
kStoreAndMultisampleResolve,
148184
};
149185

186+
constexpr const char* LoadActionToString(LoadAction action) {
187+
switch (action) {
188+
case LoadAction::kDontCare:
189+
return "DontCare";
190+
case LoadAction::kLoad:
191+
return "Load";
192+
case LoadAction::kClear:
193+
return "Clear";
194+
}
195+
}
196+
197+
constexpr const char* StoreActionToString(StoreAction action) {
198+
switch (action) {
199+
case StoreAction::kDontCare:
200+
return "DontCare";
201+
case StoreAction::kStore:
202+
return "Store";
203+
case StoreAction::kMultisampleResolve:
204+
return "MultisampleResolve";
205+
case StoreAction::kStoreAndMultisampleResolve:
206+
return "StoreAndMultisampleResolve";
207+
}
208+
}
209+
150210
constexpr bool CanClearAttachment(LoadAction action) {
151211
switch (action) {
152212
case LoadAction::kLoad:
@@ -530,6 +590,14 @@ struct StencilAttachment : public Attachment {
530590
uint32_t clear_stencil = 0;
531591
};
532592

593+
std::string AttachmentToString(const Attachment& attachment);
594+
595+
std::string ColorAttachmentToString(const ColorAttachment& color);
596+
597+
std::string DepthAttachmentToString(const DepthAttachment& depth);
598+
599+
std::string StencilAttachmentToString(const StencilAttachment& stencil);
600+
533601
} // namespace impeller
534602

535603
namespace std {

impeller/core/texture_descriptor.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,20 @@
44

55
#include "impeller/core/texture_descriptor.h"
66

7+
#include <sstream>
8+
79
namespace impeller {
810

9-
//
11+
std::string TextureDescriptorToString(const TextureDescriptor& desc) {
12+
std::stringstream stream;
13+
stream << "StorageMode=" << StorageModeToString(desc.storage_mode) << ",";
14+
stream << "Type=" << TextureTypeToString(desc.type) << ",";
15+
stream << "Format=" << PixelFormatToString(desc.format) << ",";
16+
stream << "Size=" << desc.size << ",";
17+
stream << "MipCount=" << desc.mip_count << ",";
18+
stream << "SampleCount=" << static_cast<size_t>(desc.sample_count) << ",";
19+
stream << "Compression=" << CompressionTypeToString(desc.compression_type);
20+
return stream.str();
21+
}
1022

1123
} // namespace impeller

impeller/core/texture_descriptor.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ enum class CompressionType {
2121
kLossless,
2222
kLossy,
2323
};
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+
2435
//------------------------------------------------------------------------------
2536
/// @brief A lightweight object that describes the attributes of a texture
2637
/// that can then used an allocator to create that texture.
@@ -63,4 +74,6 @@ struct TextureDescriptor {
6374
}
6475
};
6576

77+
std::string TextureDescriptorToString(const TextureDescriptor& desc);
78+
6679
} // namespace impeller

impeller/geometry/color.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cmath>
99
#include <sstream>
1010

11+
#include "impeller/base/strings.h"
1112
#include "impeller/geometry/constants.h"
1213
#include "impeller/geometry/scalar.h"
1314
#include "impeller/geometry/vector.h"
@@ -360,4 +361,13 @@ Color Color::BlendColor(const Color& src,
360361
}
361362
}
362363

364+
std::string ColorToString(const Color& color) {
365+
return SPrintF("R=%.1f,G=%.1f,B=%.1f,A=%.1f", //
366+
color.red, //
367+
color.green, //
368+
color.blue, //
369+
color.alpha //
370+
);
371+
}
372+
363373
} // namespace impeller

impeller/geometry/color.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,8 @@ struct Color {
797797
constexpr bool IsOpaque() const { return alpha == 1.0f; }
798798
};
799799

800+
std::string ColorToString(const Color& color);
801+
800802
/**
801803
* Represents a color by its constituent hue, saturation, brightness and alpha
802804
*/

impeller/renderer/render_target.cc

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "impeller/renderer/render_target.h"
66

7+
#include <sstream>
8+
79
#include "impeller/base/strings.h"
810
#include "impeller/base/validation.h"
911
#include "impeller/core/allocator.h"
@@ -59,21 +61,27 @@ bool RenderTarget::IsValid() const {
5961

6062
if (texture_type != attachment.texture->GetTextureDescriptor().type) {
6163
passes_type_validation = false;
64+
VALIDATION_LOG << "Render target has incompatible texture types: "
65+
<< TextureTypeToString(texture_type.value()) << " != "
66+
<< TextureTypeToString(
67+
attachment.texture->GetTextureDescriptor().type)
68+
<< " on target " << ToString();
6269
return false;
6370
}
6471

6572
if (sample_count !=
6673
attachment.texture->GetTextureDescriptor().sample_count) {
6774
passes_type_validation = false;
75+
VALIDATION_LOG << "Render target (" << ToString()
76+
<< ") has incompatible sample counts.";
77+
6878
return false;
6979
}
7080

7181
return true;
7282
};
7383
IterateAllAttachments(iterator);
7484
if (!passes_type_validation) {
75-
VALIDATION_LOG << "Render target texture types are not of the same type "
76-
"and sample count.";
7785
return false;
7886
}
7987
}
@@ -370,4 +378,24 @@ size_t RenderTarget::GetTotalAttachmentCount() const {
370378
return count;
371379
}
372380

381+
std::string RenderTarget::ToString() const {
382+
std::stringstream stream;
383+
384+
for (const auto& [index, color] : colors_) {
385+
stream << SPrintF("Color[%zu]=(%s)", index,
386+
ColorAttachmentToString(color).c_str());
387+
}
388+
if (depth_) {
389+
stream << ",";
390+
stream << SPrintF("Depth=(%s)",
391+
DepthAttachmentToString(depth_.value()).c_str());
392+
}
393+
if (stencil_) {
394+
stream << ",";
395+
stream << SPrintF("Stencil=(%s)",
396+
StencilAttachmentToString(stencil_.value()).c_str());
397+
}
398+
return stream.str();
399+
}
400+
373401
} // namespace impeller

impeller/renderer/render_target.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class RenderTarget final {
109109
void IterateAllAttachments(
110110
const std::function<bool(const Attachment& attachment)>& iterator) const;
111111

112+
std::string ToString() const;
113+
112114
private:
113115
std::map<size_t, ColorAttachment> colors_;
114116
std::optional<DepthAttachment> depth_;

0 commit comments

Comments
 (0)