• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ContextOptions.h"
12#include "include/gpu/graphite/TextureInfo.h"
13#include "src/gpu/graphite/Caps.h"
14#include "src/gpu/graphite/GlobalCache.h"
15#include "src/gpu/graphite/Log.h"
16#include "src/gpu/graphite/mtl/MtlCommandBuffer.h"
17#include "src/gpu/graphite/mtl/MtlResourceProvider.h"
18#include "src/gpu/graphite/mtl/MtlTexture.h"
19#include "src/gpu/mtl/MtlMemoryAllocatorImpl.h"
20
21namespace skgpu::graphite {
22
23sk_sp<SharedContext> MtlSharedContext::Make(const MtlBackendContext& context,
24                                            const ContextOptions& options) {
25    if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) {
26        // no warning needed
27    } else {
28        SKGPU_LOG_E("Skia's Graphite backend no longer supports this OS version.");
29#ifdef SK_BUILD_FOR_IOS
30        SKGPU_LOG_E("Minimum supported version is iOS/tvOS 13.0.");
31#else
32        SKGPU_LOG_E("Minimum supported version is MacOS 10.15.");
33#endif
34        return nullptr;
35    }
36
37    sk_cfp<id<MTLDevice>> device = sk_ret_cfp((id<MTLDevice>)(context.fDevice.get()));
38
39    std::unique_ptr<const MtlCaps> caps(new MtlCaps(device.get(), options));
40
41    // TODO: Add memory allocator to context once we figure out synchronization
42    sk_sp<MtlMemoryAllocator> memoryAllocator = skgpu::MtlMemoryAllocatorImpl::Make(device.get());
43    if (!memoryAllocator) {
44        SkDEBUGFAIL("No supplied Metal memory allocator and unable to create one internally.");
45        return nullptr;
46    }
47
48    return sk_sp<SharedContext>(new MtlSharedContext(std::move(device),
49                                                     std::move(memoryAllocator),
50                                                     std::move(caps),
51                                                     options.fUserDefinedKnownRuntimeEffects));
52}
53
54MtlSharedContext::MtlSharedContext(sk_cfp<id<MTLDevice>> device,
55                                   sk_sp<skgpu::MtlMemoryAllocator> memoryAllocator,
56                                   std::unique_ptr<const MtlCaps> caps,
57                                   SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects)
58        : SharedContext(std::move(caps), BackendApi::kMetal, userDefinedKnownRuntimeEffects)
59        , fMemoryAllocator(std::move(memoryAllocator))
60        , fDevice(std::move(device)) {}
61
62MtlSharedContext::~MtlSharedContext() {
63    // need to clear out resources before the allocator (if any) is removed
64    this->globalCache()->deleteResources();
65}
66
67std::unique_ptr<ResourceProvider> MtlSharedContext::makeResourceProvider(
68        SingleOwner* singleOwner,
69        uint32_t recorderID,
70        size_t resourceBudget) {
71    return std::unique_ptr<ResourceProvider>(new MtlResourceProvider(this,
72                                                                     singleOwner,
73                                                                     recorderID,
74                                                                     resourceBudget));
75}
76
77} // namespace skgpu::graphite
78