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