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 "include/gpu/graphite/Context.h" 11#include "include/gpu/graphite/ContextOptions.h" 12#include "include/gpu/graphite/mtl/MtlBackendContext.h" 13#include "src/gpu/graphite/ContextOptionsPriv.h" 14#include "tools/gpu/ContextType.h" 15#include "tools/graphite/TestOptions.h" 16 17#import <Metal/Metal.h> 18 19namespace skiatest::graphite { 20 21std::unique_ptr<GraphiteTestContext> MtlTestContext::Make() { 22 sk_cfp<id<MTLDevice>> device; 23#ifdef SK_BUILD_FOR_MAC 24 sk_cfp<NSArray<id <MTLDevice>>*> availableDevices(MTLCopyAllDevices()); 25 // Choose the non-integrated CPU if available 26 for (id<MTLDevice> dev in availableDevices.get()) { 27 if (!dev.isLowPower) { 28 // This retain is necessary because when the NSArray goes away it will delete the 29 // device entry otherwise. 30 device.retain(dev); 31 break; 32 } 33 if (dev.isRemovable) { 34 device.retain(dev); 35 break; 36 } 37 } 38 if (!device) { 39 device.reset(MTLCreateSystemDefaultDevice()); 40 } 41#else 42 device.reset(MTLCreateSystemDefaultDevice()); 43#endif 44 45 skgpu::graphite::MtlBackendContext backendContext = {}; 46 backendContext.fDevice.retain(device.get()); 47 backendContext.fQueue.reset([*device newCommandQueue]); 48 49 return std::unique_ptr<GraphiteTestContext>(new MtlTestContext(backendContext)); 50} 51 52skgpu::ContextType MtlTestContext::contextType() { 53 return skgpu::ContextType::kMetal; 54} 55 56std::unique_ptr<skgpu::graphite::Context> MtlTestContext::makeContext(const TestOptions& options) { 57 SkASSERT(!options.hasDawnOptions()); 58 skgpu::graphite::ContextOptions revisedContextOptions(options.fContextOptions); 59 skgpu::graphite::ContextOptionsPriv contextOptionsPriv; 60 if (!options.fContextOptions.fOptionsPriv) { 61 revisedContextOptions.fOptionsPriv = &contextOptionsPriv; 62 } 63 // Needed to make synchronous readPixels work 64 revisedContextOptions.fOptionsPriv->fStoreContextRefInRecorder = true; 65 66 return skgpu::graphite::ContextFactory::MakeMetal(fMtl, revisedContextOptions); 67} 68 69} // namespace skiatest::graphite 70