xref: /aosp_15_r20/external/skia/tools/gpu/mtl/MtlTestContext.mm (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1/*
2 * Copyright 2017 Google Inc.
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 "tools/gpu/mtl/MtlTestContext.h"
9
10#include "include/gpu/ganesh/GrContextOptions.h"
11#include "include/gpu/ganesh/GrDirectContext.h"
12#include "include/gpu/ganesh/mtl/GrMtlDirectContext.h"
13#include "src/gpu/ganesh/mtl/GrMtlUtil.h"
14
15#import <Metal/Metal.h>
16
17namespace {
18class MtlTestContextImpl : public sk_gpu_test::MtlTestContext {
19public:
20    static MtlTestContext* Create(MtlTestContext* sharedContext) {
21        GrMtlBackendContext backendContext = {};
22        if (sharedContext) {
23            MtlTestContextImpl* sharedContextImpl = (MtlTestContextImpl*) sharedContext;
24            backendContext = sharedContextImpl->getMtlBackendContext();
25        } else {
26            sk_cfp<id<MTLDevice>> device;
27#ifdef SK_BUILD_FOR_MAC
28            sk_cfp<NSArray<id <MTLDevice>>*> availableDevices(MTLCopyAllDevices());
29            // Choose the non-integrated CPU if available
30            for (id<MTLDevice> dev in availableDevices.get()) {
31                if (!dev.isLowPower) {
32                    device.retain(dev);
33                    break;
34                }
35                if (dev.isRemovable) {
36                    device.retain(dev);
37                    break;
38                }
39            }
40            if (!device) {
41                device.reset(MTLCreateSystemDefaultDevice());
42            }
43#else
44            device.reset(MTLCreateSystemDefaultDevice());
45#endif
46            backendContext.fDevice.retain((GrMTLHandle)device.get());
47            sk_cfp<id<MTLCommandQueue>> queue([*device newCommandQueue]);
48            backendContext.fQueue.retain((GrMTLHandle)queue.get());
49        }
50
51        return new MtlTestContextImpl(backendContext);
52    }
53
54    ~MtlTestContextImpl() override { this->teardown(); }
55
56    void testAbandon() override {}
57
58    sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override {
59        return GrDirectContexts::MakeMetal(fMtl, options);
60    }
61
62private:
63    MtlTestContextImpl(const GrMtlBackendContext& mtl)
64            : INHERITED(mtl) {
65        fFenceSupport = true;
66    }
67
68    void onPlatformMakeNotCurrent() const override {}
69    void onPlatformMakeCurrent() const override {}
70    std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; }
71
72    using INHERITED = sk_gpu_test::MtlTestContext;
73};
74
75}  // anonymous namespace
76
77namespace sk_gpu_test {
78
79MtlTestContext* CreatePlatformMtlTestContext(MtlTestContext* sharedContext) {
80    return MtlTestContextImpl::Create(sharedContext);
81}
82
83}  // namespace sk_gpu_test
84