Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS GPUImage introduction

Avatar for Bruce Tsai Bruce Tsai
February 02, 2015

iOS GPUImage introduction

An introduction to GPUImage that is an excellent image processing framework for iOS. The architecture, threading model and how to leverage OpenGL ES are addressed in the slides.

Avatar for Bruce Tsai

Bruce Tsai

February 02, 2015
Tweet

More Decks by Bruce Tsai

Other Decks in Programming

Transcript

  1. Core Image ✤ Included in iOS from 2011! ✤ 100+

    built-in filters! ✤ Multithread by CPU
  2. Drawbacks ✤ Customization limited! - Subclass CIFilter in iOS! -

    Create custom filters (shader) only in OSX! ✤ Performance not tuned! - Address tuning tips in WWDC 2012 (session 511) 3
  3. GPUImage ✤ Open source (BSD) iOS framework! ✤ GPU-based image

    and video processing! ✤ From GPU-accelerated video processing project (2010)! ✤ https://github.com/BradLarson/GPUImage 4
  4. Advantages ✤ Performance! - High FPS in real-time preview! ✤

    Customization! - OpenGL ES shader code 5
  5. Code Snippet GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack]; GPUImageFilter

    *customFilter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@“CustomShader”]; GPUImageView *filteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, viewHeight)]; [videoCamera addTarget:customFilter]; [customFilter addTarget:filteredVideoView]; [videoCamera startCameraCapture];
  6. Procedures 1. Add effect to input buffer ! 2. Save

    result to output buffer! 3. Set targets’ input buffer attributes! 4. Set targets’ input buffer! 5. Invoke targets’ operation 9
  7. Buffer Binding in FBO Frame Buffer Texture Pixel Buffer Render

    destination Actual render target Main memory buffer
  8. Conversion to and from Texture CVPixelBufferRef pxBufferRef = CMSampleBufferGetImageBuffer(imageBuffer); CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDe

    fault, videoTextureCache, pxBufferRef, NULL, GL_TEXTURE_2D, GL_RGBA, width, height, GL_BGRA, GL_UNSIGNED_BYTE, 0, &videoTextureRef); ... // renderTargetRef is corresponding to renderTexture of FBO [writerAdaptor appendPixelBuffer:renderTargetRef withPresentationTime:pts];
  9. Threading Model ✤ Grand Central Dispatch (GCD)! - Dispatch queue!

    ✤ runSynchronousOnVideoProcessingQueue! ✤ runAsynchronousOnVideoProcessingQueue! ✤ runSynchronousOnContextQueue! ✤ runAsynchronousOnContextQueue 14