Skip to content

Commit dee5e3c

Browse files
authored
Make color cube app write to logcat on Android (#68)
* Make color cube app write to logcat on Android The color cube test app does all its error reporting and logging using std::cout. This works fine on desktop platforms but not so well on Android. Android apps typically write to logcat, as the standard handles are not easily accessible. This introduces a AMDVulkanDemoVkUtils::Log() which writes to logcat on Android, and stdout everywhere else. This also changes all std::cout writes to use the new utility method. Finally, the code that tries to create a Vulkan android surface was neglecting to check if the operation succeeded. It wrote the result code to stdout, but assumed success. It now checks and aborts if the creation failed. Change-Id: I9c731a3363227e3fc14289445f112526e1882689 * Avoid memory leak in error case
1 parent 15d2348 commit dee5e3c

File tree

4 files changed

+186
-119
lines changed

4 files changed

+186
-119
lines changed

source/examples/vulkan/vk_color_cube/gpa_helper.cc

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//==============================================================================
77

88
#include "examples/vulkan/vk_color_cube/gpa_helper.h"
9+
#include "examples/vulkan/vk_color_cube/vk_util.h"
910

1011
#include <assert.h>
1112

@@ -80,29 +81,29 @@ void GpaHelper::PrintGPACounterInfo(GpaContextId context_id) const
8081

8182
if (kGpaStatusOk != gpa_status)
8283
{
83-
std::cout << "ERROR: Failed to get device and revision id.\n";
84+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get device and revision id.");
8485
return;
8586
}
8687

8788
gpa_status = gpa_function_table_->GpaGetDeviceName(context_id, &device_name_ptr);
8889

8990
if (kGpaStatusOk != gpa_status)
9091
{
91-
std::cout << "ERROR: Failed to get the device name.\n";
92+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get the device name.");
9293
return;
9394
}
9495
std::string device_name_string(device_name_ptr);
9596

96-
std::cout << "Device Id: " << std::showbase << std::hex << device_id << std::endl;
97-
std::cout << "Revision Id: " << FormatRevisionId(revision_id) << std::endl;
98-
std::cout << "Device Name: " << device_name_string.c_str() << std::endl;
97+
AMDVulkanDemoVkUtils::Log("Device Id: 0x%04X", device_id);
98+
AMDVulkanDemoVkUtils::Log("Revision Id: %s", FormatRevisionId(revision_id).c_str());
99+
AMDVulkanDemoVkUtils::Log("Device Name: %s", device_name_string.c_str());
99100

100101
GpaUInt32 num_counters = 0;
101102
gpa_status = gpa_function_table_->GpaGetNumCounters(context_id, &num_counters);
102103

103104
if (kGpaStatusOk != gpa_status)
104105
{
105-
std::cout << "ERROR: Failed to get the number of available counters." << std::endl;
106+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get the number of available counters.");
106107
return;
107108
}
108109

@@ -119,11 +120,11 @@ void GpaHelper::PrintGPACounterInfo(GpaContextId context_id) const
119120

120121
if (kGpaStatusOk == name_status && kGpaStatusOk == group_status && kGpaStatusOk == description_status)
121122
{
122-
std::cout << counter_index << ": " << name << " \"" << group << "\" - " << description << std::endl;
123+
AMDVulkanDemoVkUtils::Log("%d: %s \"%s\" - %s", counter_index, name, group, description);
123124
}
124125
else
125126
{
126-
std::cout << "ERROR: Failed to get counter name, group, or description." << std::endl;
127+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get counter name, group, or description.");
127128
}
128129
}
129130
}
@@ -233,6 +234,11 @@ void GpaHelper::gpaLoggingCallback(GpaLoggingType type, const char* msg)
233234
}
234235

235236
gpa_log_file_stream << log_message << std::endl;
237+
238+
#ifdef ANDROID
239+
// Write the message to logcat
240+
AMDVulkanDemoVkUtils::Log("%s", log_message.c_str());
241+
#endif
236242
}
237243

238244
bool GpaHelper::CounterValueCompare(unsigned int profile_set,
@@ -655,36 +661,35 @@ void GpaHelper::PrintGpaSampleResults(GpaContextId context_id,
655661

656662
if (kGpaStatusOk != gpa_status)
657663
{
658-
std::cout << "ERROR: Failed to get device and revision id.\n";
664+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get device and revision id.");
659665
return;
660666
}
661667

662668
gpa_status = gpa_function_table_->GpaGetDeviceName(context_id, &device_name_ptr);
663669

664670
if (kGpaStatusOk != gpa_status)
665671
{
666-
std::cout << "ERROR: Failed to get the device name.\n";
672+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get the device name.");
667673
return;
668674
}
669675

670676
GpaHwGeneration hardware_generation = kGpaHwGenerationNone;
671677
gpa_status = gpa_function_table_->GpaGetDeviceGeneration(context_id, &hardware_generation);
672678
if (kGpaStatusOk != gpa_status)
673679
{
674-
std::cout << "ERROR: Failed to get the device generation.\n";
680+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get the device generation.");
675681
return;
676682
}
677683

678684
std::string device_name_string(device_name_ptr);
679685

680686
if (output_to_console)
681687
{
682-
std::cout << "--------------------------------------------------" << std::endl;
683-
std::cout << "Device Id: " << std::showbase << std::hex << device_id << std::endl;
684-
std::cout << "Revision Id: " << FormatRevisionId(revision_id) << std::endl;
685-
std::cout << "Device Name: " << device_name_string.c_str() << std::endl;
686-
std::cout << "--------------------------------------------------" << std::endl;
687-
std::cout << "Profile " << profile_set << ", Sample ID: " << sample_id << std::endl;
688+
AMDVulkanDemoVkUtils::Log("Device Id: 0x%04X", device_id);
689+
AMDVulkanDemoVkUtils::Log("Revision Id: %s", FormatRevisionId(revision_id).c_str());
690+
AMDVulkanDemoVkUtils::Log("Device Name: %s", device_name_string.c_str());
691+
AMDVulkanDemoVkUtils::Log("--------------------------------------------------");
692+
AMDVulkanDemoVkUtils::Log("Profile %d, Sample ID: %d", profile_set, sample_id);
688693
}
689694

690695
std::stringstream csv_header;
@@ -698,23 +703,23 @@ void GpaHelper::PrintGpaSampleResults(GpaContextId context_id,
698703

699704
if (kGpaStatusOk != gpa_status)
700705
{
701-
std::cout << "ERROR: Failed to get GPA sample result size." << std::endl;
706+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get GPA sample result size.");
702707
return;
703708
}
704709

705710
GpaUInt64* results_buffer = (GpaUInt64*)malloc(sample_result_size_in_bytes);
706711

707712
if (results_buffer == nullptr)
708713
{
709-
std::cout << "ERROR: Failed to allocate memory for GPA results." << std::endl;
714+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to allocate memory for GPA results.");
710715
return;
711716
}
712717

713718
gpa_status = gpa_function_table_->GpaGetSampleResult(session_id, sample_id, sample_result_size_in_bytes, results_buffer);
714719

715720
if (kGpaStatusOk != gpa_status)
716721
{
717-
std::cout << "ERROR: Failed to get GPA sample results." << std::endl;
722+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get GPA sample results.");
718723
}
719724
else
720725
{
@@ -723,7 +728,7 @@ void GpaHelper::PrintGpaSampleResults(GpaContextId context_id,
723728

724729
if (kGpaStatusOk != gpa_status)
725730
{
726-
std::cout << "ERROR: Failed to get the number of enabled counters from GPA." << std::endl;
731+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get the number of enabled counters from GPA.");
727732
}
728733
else
729734
{
@@ -740,7 +745,7 @@ void GpaHelper::PrintGpaSampleResults(GpaContextId context_id,
740745

741746
if (kGpaStatusOk != gpa_status)
742747
{
743-
std::cout << "ERROR: Failed to get the exposed GPA counter id of the enabled counter at index " << counter_index << "." << std::endl;
748+
AMDVulkanDemoVkUtils::Log("ERROR: Failed to get the exposed GPA counter id of the enabled counter at index %d", counter_index);
744749
}
745750
else
746751
{

0 commit comments

Comments
 (0)