From 152f369a34aea90cb1b3215e21914ce925f607ec Mon Sep 17 00:00:00 2001 From: Dan Field Date: Thu, 14 Apr 2022 10:22:39 -0700 Subject: [PATCH] Move static methods to anonymous namespace --- renderer/backend/metal/command_buffer_mtl.mm | 112 ++++++++++--------- 1 file changed, 58 insertions(+), 54 deletions(-) diff --git a/renderer/backend/metal/command_buffer_mtl.mm b/renderer/backend/metal/command_buffer_mtl.mm index 4382c6d8..7e5cacfb 100644 --- a/renderer/backend/metal/command_buffer_mtl.mm +++ b/renderer/backend/metal/command_buffer_mtl.mm @@ -7,55 +7,11 @@ #include "impeller/renderer/backend/metal/render_pass_mtl.h" namespace impeller { - -id CreateCommandBuffer(id queue) { - if (@available(iOS 14.0, macOS 11.0, *)) { - auto desc = [[MTLCommandBufferDescriptor alloc] init]; - // Degrades CPU performance slightly but is well worth the cost for typical - // Impeller workloads. - desc.errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus; - return [queue commandBufferWithDescriptor:desc]; - } - return [queue commandBuffer]; -} - -CommandBufferMTL::CommandBufferMTL(id queue) - : buffer_(CreateCommandBuffer(queue)) { - if (!buffer_) { - return; - } - is_valid_ = true; -} - -CommandBufferMTL::~CommandBufferMTL() = default; - -bool CommandBufferMTL::IsValid() const { - return is_valid_; -} - -void CommandBufferMTL::SetLabel(const std::string& label) const { - if (label.empty()) { - return; - } - - [buffer_ setLabel:@(label.data())]; -} - -static CommandBuffer::Status ToCommitResult(MTLCommandBufferStatus status) { - switch (status) { - case MTLCommandBufferStatusCompleted: - return CommandBufferMTL::Status::kCompleted; - case MTLCommandBufferStatusEnqueued: - return CommandBufferMTL::Status::kPending; - default: - break; - } - return CommandBufferMTL::Status::kError; -} - +namespace { // TODO(dnfield): remove this declaration when we no longer need to build on -// machines with lower SDK versions than 11.0.s -#if !defined(MAC_OS_VERSION_11_0) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_VERSION_11_0 +// machines with lower SDK versions than 11.0. +#if !defined(MAC_OS_VERSION_11_0) || \ + MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_VERSION_11_0 typedef NS_ENUM(NSInteger, MTLCommandEncoderErrorState) { MTLCommandEncoderErrorStateUnknown = 0, MTLCommandEncoderErrorStateCompleted = 1, @@ -65,12 +21,6 @@ typedef NS_ENUM(NSInteger, MTLCommandEncoderErrorState) { } API_AVAILABLE(macos(11.0), ios(14.0)); #endif - -#if !defined(MAC_OS_VERSION_12_0) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_VERSION_12_0 -constexpr int MTLCommandBufferErrorAccessRevoked = 4; -constexpr int MTLCommandBufferErrorStackOverflow = 12; -#endif - API_AVAILABLE(ios(14.0), macos(11.0)) NSString* MTLCommandEncoderErrorStateToString( MTLCommandEncoderErrorState state) { @@ -89,6 +39,14 @@ typedef NS_ENUM(NSInteger, MTLCommandEncoderErrorState) { return @"unknown"; } +// TODO(dnfield): This can be removed when all bots have been sufficiently +// upgraded for MAC_OS_VERSION_12_0. +#if !defined(MAC_OS_VERSION_12_0) || \ + MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_VERSION_12_0 +constexpr int MTLCommandBufferErrorAccessRevoked = 4; +constexpr int MTLCommandBufferErrorStackOverflow = 12; +#endif + static NSString* MTLCommandBufferErrorToString(MTLCommandBufferError code) { switch (code) { case MTLCommandBufferErrorNone: @@ -173,6 +131,52 @@ static void LogMTLCommandBufferErrorIfPresent(id buffer) { stream << "<<<<<<<"; VALIDATION_LOG << stream.str(); } +} // namespace + +id CreateCommandBuffer(id queue) { + if (@available(iOS 14.0, macOS 11.0, *)) { + auto desc = [[MTLCommandBufferDescriptor alloc] init]; + // Degrades CPU performance slightly but is well worth the cost for typical + // Impeller workloads. + desc.errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus; + return [queue commandBufferWithDescriptor:desc]; + } + return [queue commandBuffer]; +} + +CommandBufferMTL::CommandBufferMTL(id queue) + : buffer_(CreateCommandBuffer(queue)) { + if (!buffer_) { + return; + } + is_valid_ = true; +} + +CommandBufferMTL::~CommandBufferMTL() = default; + +bool CommandBufferMTL::IsValid() const { + return is_valid_; +} + +void CommandBufferMTL::SetLabel(const std::string& label) const { + if (label.empty()) { + return; + } + + [buffer_ setLabel:@(label.data())]; +} + +static CommandBuffer::Status ToCommitResult(MTLCommandBufferStatus status) { + switch (status) { + case MTLCommandBufferStatusCompleted: + return CommandBufferMTL::Status::kCompleted; + case MTLCommandBufferStatusEnqueued: + return CommandBufferMTL::Status::kPending; + default: + break; + } + return CommandBufferMTL::Status::kError; +} bool CommandBufferMTL::SubmitCommands(CompletionCallback callback) { if (!buffer_) {