• 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 "tools/graphite/mtl/GraphiteMtlTestContext.h"
9
10#include "experimental/graphite/include/Context.h"
11#include "experimental/graphite/include/mtl/MtlTypes.h"
12
13#ifdef SK_METAL
14
15#import <Metal/Metal.h>
16
17namespace skiatest::graphite::mtl {
18
19std::unique_ptr<GraphiteTestContext> TestContext::Make() {
20    sk_cfp<id<MTLDevice>> device;
21#ifdef SK_BUILD_FOR_MAC
22    sk_cfp<NSArray<id <MTLDevice>>*> availableDevices(MTLCopyAllDevices());
23    // Choose the non-integrated CPU if available
24    for (id<MTLDevice> dev in availableDevices.get()) {
25        if (!dev.isLowPower) {
26            // This retain is necessary because when the NSArray goes away it will delete the
27            // device entry otherwise.
28            device.retain(dev);
29            break;
30        }
31        if (dev.isRemovable) {
32            device.retain(dev);
33            break;
34        }
35    }
36    if (!device) {
37        device.reset(MTLCreateSystemDefaultDevice());
38    }
39#else
40    device.reset(MTLCreateSystemDefaultDevice());
41#endif
42
43    skgpu::mtl::BackendContext backendContext = {};
44    backendContext.fDevice.retain(device.get());
45    backendContext.fQueue.reset([*device newCommandQueue]);
46
47    return std::unique_ptr<GraphiteTestContext>(new TestContext(backendContext));
48}
49
50sk_sp<skgpu::Context> TestContext::makeContext() {
51    return skgpu::Context::MakeMetal(fMtl);
52}
53
54}  // namespace skiatest::graphite::mtl
55
56#endif // SK_METAL
57