/*
 * Copyright 2021 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "experimental/graphite/src/mtl/MtlCommandBuffer.h"

namespace skgpu::mtl {

std::unique_ptr<CommandBuffer> CommandBuffer::Make(id<MTLCommandQueue> queue) {
    sk_cfp<MTLCommandBufferDescriptor*> desc([[MTLCommandBufferDescriptor alloc] init]);
    (*desc).retainedReferences = NO;
#ifdef SK_ENABLE_MTL_DEBUG_INFO
    (*desc).errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus;
#endif

    // We add a retain here because the command buffer is set to autorelease (not alloc or copy)
    sk_cfp<id<MTLCommandBuffer>> cmdBuffer([[queue commandBufferWithDescriptor:desc.get()] retain]);
    if (cmdBuffer == nil) {
        return nullptr;
    }

#ifdef SK_ENABLE_MTL_DEBUG_INFO
     (*cmdBuffer).label = @"CommandBuffer::Make";
#endif

    return std::unique_ptr<CommandBuffer>(new CommandBuffer(std::move(cmdBuffer)));
}

} // namespace skgpu::mtl

/*
 * Copyright 2021 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "experimental/graphite/src/mtl/MtlCommandBuffer.h"

namespace skgpu::mtl {

std::unique_ptr<CommandBuffer> CommandBuffer::Make(id<MTLCommandQueue> queue) {
    sk_cfp<id<MTLCommandBuffer>> cmdBuffer;
#if GR_METAL_SDK_VERSION >= 230
    if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) {
        sk_cfp<MTLCommandBufferDescriptor*> desc([[MTLCommandBufferDescriptor alloc] init]);
        (*desc).retainedReferences = NO;
#ifdef SK_ENABLE_MTL_DEBUG_INFO
        (*desc).errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus;
#endif
        // We add a retain here because the command buffer is set to autorelease (not alloc or copy)
        cmdBuffer.reset([[queue commandBufferWithDescriptor:desc.get()] retain]);
    } else {
#endif
        // We add a retain here because the command buffer is set to autorelease (not alloc or copy)
        cmdBuffer.reset([[queue commandBufferWithUnretainedReferences] retain]);
#if GR_METAL_SDK_VERSION >= 230
    }
#endif
    if (cmdBuffer == nil) {
        return nullptr;
    }

#ifdef SK_ENABLE_MTL_DEBUG_INFO
     (*cmdBuffer).label = @"CommandBuffer::Make";
#endif

    return std::unique_ptr<CommandBuffer>(new CommandBuffer(std::move(cmdBuffer)));
}

} // namespace skgpu::mtl

Fix creation of command buffer for different SDK/OS versions
[graphite] Fix creation of command buffer for different SDK/OS versions

Bug: skia:12466
Change-Id: I9828c59f0406e457e723f78940b1f42ff343fa2e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/455500
Auto-Submit: Jim Van Verth <509afb0bfbb23ecaaf1fb3323209f531b45e75a6@google.com>
Reviewed-by: Greg Daniel <39df0a804564ccb6cf75f18db79653821f37c1c5@google.com>
