LoginSignup
4
4

More than 5 years have passed since last update.

GPUImageの連結状況をDumpする

Posted at

GPUImageで色々連結したり外したりしまくっていると、何がどうなっているか分からなくなる。
デバッグ用にDumpする仕組みを作った。

Dumpするやつ

GPUImageDumper
protocol DumpName {
  var dumpName: String? { get set }
}

extension GPUImageOutput {
  func dumpTargets() -> String {
    return ((self as? DumpName)?.dumpName ?? String(self)) + "\n" + dumpTargets("")
  }

  private func dumpTargets(prefix: String) -> String {
    let maxIndex = targets().count - 1
    var str = ""
    for (i, child) in targets().enumerate() {
      let last = (i == maxIndex)
      let name = (child as? DumpName)?.dumpName ?? String(child)
      str += prefix + (last ? "└" : "├") + name + "\n"
      if let o = child as? GPUImageOutput {
        str += o.dumpTargets(prefix + (last ? " " : "│ "))
      }
    }
    return str
  }
}

class GPUImageRootInstances {
  static let sharedInstance = GPUImageRootInstances()
  private var rootInstances = [GPUImageWeakInstance]()

  func append(instance: GPUImageOutput) {
    for i in rootInstances {
      if instance === i.instance { return }
    }
    rootInstances.append(GPUImageWeakInstance(output: instance))
  }

  func dumpAll() -> String {
    var str = ""
    for i in rootInstances {
      if let dump = i.instance?.dumpTargets() {
        str += dump + "\n"
      }
    }
    return str
  }
}

class GPUImageWeakInstance {
  weak var instance: GPUImageOutput?

  init(output: GPUImageOutput) {
    instance = output
  }
}

これで、GPUImageRootInstances.sharedInstance.append
GPUImageVideoCameraとかGPUImagePictureとかを渡せば準備OK。

GPUImageRootInstances.sharedInstance.dumpAll()で出力される。

そのあたりをDumpNameプロトコルに準拠させてあげると、設定した名前で出力させられる。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4