 //
 extension SNTrimController: SNTrimColorPickerDelegate {
     func updateMaskColor(color:UIColor?, fPlus:Bool) {
-        guard let device = MTLCreateSystemDefaultDevice() else {
+        guard let device = SNTrimController.device,
+              let function = self.function else {
             print("SNTrim No Metal. User CPU")
             return updateMaskColorCPU(color, fPlus: fPlus)
         }

            encoder.setBytes(&uz0, length: sizeofValue(uz0), atIndex: 5)
            encoder.setBytes(&inv, length: sizeofValue(inv), atIndex: 6)

            let function = device.newDefaultLibrary()!.newFunctionWithName("maskImage")!
            let pipeline = try! device.newComputePipelineStateWithFunction(function)
            encoder.setComputePipelineState(pipeline)
            let threadExeWidth = pipeline.threadExecutionWidth

    
    var image:UIImage!
    var delegate:SNTrimControllerDelegate!
    
    // Metal
    static let device = MTLCreateSystemDefaultDevice()
    let function = SNTrimController.device?.newDefaultLibrary()?.newFunctionWithName("maskImage")

    private let borderView:UIView = {
        let borderView = UIView()
