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