xref: /aosp_15_r20/external/skia/src/gpu/graphite/mtl/MtlSharedContext.mm (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1/*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "src/gpu/graphite/mtl/MtlSharedContext.h"
9
10#include "include/gpu/graphite/BackendTexture.h"
11#include "include/gpu/graphite/TextureInfo.h"
12#include "src/gpu/graphite/Caps.h"
13#include "src/gpu/graphite/GlobalCache.h"
14#include "src/gpu/graphite/Log.h"
15#include "src/gpu/graphite/mtl/MtlCommandBuffer.h"
16#include "src/gpu/graphite/mtl/MtlResourceProvider.h"
17#include "src/gpu/graphite/mtl/MtlTexture.h"
18#include "src/gpu/mtl/MtlMemoryAllocatorImpl.h"
19
20namespace skgpu::graphite {
21
22sk_sp<skgpu::graphite::SharedContext> MtlSharedContext::Make(const MtlBackendContext& context,
23                                                             const ContextOptions& options) {
24    if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
25        // no warning needed
26    } else {
27        SKGPU_LOG_E("Skia's Graphite backend no longer supports this OS version.");
28#ifdef SK_BUILD_FOR_IOS
29        SKGPU_LOG_E("Minimum supported version is iOS/tvOS 13.0.");
30#else
31        SKGPU_LOG_E("Minimum supported version is MacOS 10.15.");
32#endif
33        return nullptr;
34    }
35
36    sk_cfp<id<MTLDevice>> device = sk_ret_cfp((id<MTLDevice>)(context.fDevice.get()));
37
38    std::unique_ptr<const MtlCaps> caps(new MtlCaps(device.get(), options));
39
40    // TODO: Add memory allocator to context once we figure out synchronization
41    sk_sp<MtlMemoryAllocator> memoryAllocator = skgpu::MtlMemoryAllocatorImpl::Make(device.get());
42    if (!memoryAllocator) {
43        SkDEBUGFAIL("No supplied Metal memory allocator and unable to create one internally.");
44        return nullptr;
45    }
46
47    return sk_sp<skgpu::graphite::SharedContext>(new MtlSharedContext(std::move(device),
48                                                                      std::move(memoryAllocator),
49                                                                      std::move(caps)));
50}
51
52MtlSharedContext::MtlSharedContext(sk_cfp<id<MTLDevice>> device,
53                                   sk_sp<skgpu::MtlMemoryAllocator> memoryAllocator,
54                                   std::unique_ptr<const MtlCaps> caps)
55        : skgpu::graphite::SharedContext(std::move(caps), BackendApi::kMetal)
56        , fMemoryAllocator(std::move(memoryAllocator))
57        , fDevice(std::move(device)) {}
58
59MtlSharedContext::~MtlSharedContext() {
60    // need to clear out resources before the allocator (if any) is removed
61    this->globalCache()->deleteResources();
62}
63
64std::unique_ptr<ResourceProvider> MtlSharedContext::makeResourceProvider(
65        SingleOwner* singleOwner,
66        uint32_t recorderID,
67        size_t resourceBudget,
68        bool /* avoidBufferAlloc */) {
69    return std::unique_ptr<ResourceProvider>(new MtlResourceProvider(this,
70                                                                     singleOwner,
71                                                                     recorderID,
72                                                                     resourceBudget));
73}
74
75} // namespace skgpu::graphite
76