1/* 2 * Copyright 2024 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#import "ganesh_metal_context_helper.h" 9 10#import "include/gpu/ganesh/mtl/GrMtlTypes.h" 11#import "include/ports/SkCFObject.h" 12 13#import <Metal/Metal.h> 14 15GrMtlBackendContext GetMetalContext() { 16 GrMtlBackendContext backendContext = {}; 17 sk_cfp<id<MTLDevice>> device; 18#if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) 19 device.reset(MTLCreateSystemDefaultDevice()); 20#else 21 sk_cfp<NSArray<id <MTLDevice>>*> availableDevices(MTLCopyAllDevices()); 22 // Choose the non-integrated CPU if available 23 for (id<MTLDevice> dev in availableDevices.get()) { 24 if (!dev.isLowPower) { 25 device.retain(dev); 26 break; 27 } 28 if (dev.isRemovable) { 29 device.retain(dev); 30 break; 31 } 32 } 33 if (!device) { 34 device.reset(MTLCreateSystemDefaultDevice()); 35 } 36#endif 37 38 backendContext.fDevice.retain((GrMTLHandle)device.get()); 39 sk_cfp<id<MTLCommandQueue>> queue([*device newCommandQueue]); 40 backendContext.fQueue.retain((GrMTLHandle)queue.get()); 41 return backendContext; 42} 43