2023-07-20 07:30:04 -06:00
|
|
|
//
|
|
|
|
// SkiaRenderer.mm
|
|
|
|
// VisionCamera
|
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 19.07.23.
|
|
|
|
// Copyright © 2023 mrousavy. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import "SkiaRenderer.h"
|
2023-02-21 07:00:48 -07:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
#import <Metal/Metal.h>
|
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
#import "SkiaRenderContext.h"
|
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
#import <include/core/SkSurface.h>
|
|
|
|
#import <include/core/SkCanvas.h>
|
2023-07-20 07:30:04 -06:00
|
|
|
#import <include/core/SkColorSpace.h>
|
2023-07-03 04:40:07 -06:00
|
|
|
#import <include/gpu/ganesh/SkImageGanesh.h>
|
2023-02-21 07:00:48 -07:00
|
|
|
#import <include/gpu/GrDirectContext.h>
|
|
|
|
#import "SkImageHelpers.h"
|
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
#import <system_error>
|
|
|
|
#import <memory>
|
|
|
|
#import <mutex>
|
|
|
|
|
|
|
|
@implementation SkiaRenderer {
|
|
|
|
// The context we draw each Frame on
|
|
|
|
std::unique_ptr<RenderContext> _offscreenContext;
|
|
|
|
// The context the preview runs on
|
|
|
|
std::unique_ptr<RenderContext> _layerContext;
|
|
|
|
// The texture holding the drawn-to Frame
|
|
|
|
id<MTLTexture> _texture;
|
|
|
|
|
|
|
|
// For synchronization between the two Threads/Contexts
|
|
|
|
std::mutex _textureMutex;
|
|
|
|
std::atomic<bool> _hasNewFrame;
|
2023-02-21 07:00:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
|
|
|
_offscreenContext = std::make_unique<RenderContext>();
|
|
|
|
_layerContext = std::make_unique<RenderContext>();
|
|
|
|
_texture = nil;
|
|
|
|
_hasNewFrame = false;
|
|
|
|
}
|
|
|
|
return self;
|
2023-02-21 07:00:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
- (id<MTLDevice>)metalDevice {
|
|
|
|
return _layerContext->device;
|
2023-02-21 07:00:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
- (id<MTLTexture>)getTexture:(NSUInteger)width height:(NSUInteger)height {
|
|
|
|
if (_texture == nil
|
|
|
|
|| _texture.width != width
|
|
|
|
|| _texture.height != height) {
|
2023-02-21 07:00:48 -07:00
|
|
|
// Create new texture with the given width and height
|
|
|
|
MTLTextureDescriptor* textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
|
|
|
|
width:width
|
|
|
|
height:height
|
|
|
|
mipmapped:NO];
|
|
|
|
textureDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
|
2023-07-20 07:30:04 -06:00
|
|
|
_texture = [_offscreenContext->device newTextureWithDescriptor:textureDescriptor];
|
2023-02-21 07:00:48 -07:00
|
|
|
}
|
2023-07-20 07:30:04 -06:00
|
|
|
return _texture;
|
2023-02-21 07:00:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
- (void)renderCameraFrameToOffscreenCanvas:(CMSampleBufferRef)sampleBuffer withDrawCallback:(draw_callback_t)callback {
|
|
|
|
// Wrap in auto release pool since we want the system to clean up after rendering
|
|
|
|
@autoreleasepool {
|
|
|
|
// Get the Frame's PixelBuffer
|
|
|
|
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
|
|
|
|
if (pixelBuffer == nil) {
|
|
|
|
throw std::runtime_error("SkiaRenderer: Pixel Buffer is corrupt/empty.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock Mutex to block the runLoop from overwriting the _currentDrawable
|
|
|
|
std::unique_lock lock(_textureMutex);
|
|
|
|
|
|
|
|
// Get the Metal Texture we use for in-memory drawing
|
|
|
|
auto texture = [self getTexture:CVPixelBufferGetWidth(pixelBuffer)
|
|
|
|
height:CVPixelBufferGetHeight(pixelBuffer)];
|
|
|
|
|
|
|
|
// Get & Lock the writeable Texture from the Metal Drawable
|
|
|
|
GrMtlTextureInfo textureInfo;
|
|
|
|
textureInfo.fTexture.retain((__bridge void*)texture);
|
|
|
|
GrBackendRenderTarget backendRenderTarget((int)texture.width,
|
|
|
|
(int)texture.height,
|
|
|
|
1,
|
|
|
|
textureInfo);
|
|
|
|
|
|
|
|
auto context = _offscreenContext->skiaContext.get();
|
|
|
|
|
|
|
|
// Create a Skia Surface from the writable Texture
|
|
|
|
auto surface = SkSurface::MakeFromBackendRenderTarget(context,
|
|
|
|
backendRenderTarget,
|
|
|
|
kTopLeft_GrSurfaceOrigin,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
SkColorSpace::MakeSRGB(),
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
if (surface == nullptr || surface->getCanvas() == nullptr) {
|
|
|
|
throw std::runtime_error("Skia surface could not be created from parameters.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the CMSampleBuffer to an SkImage - RGB.
|
|
|
|
CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
|
|
|
|
auto image = SkImageHelpers::convertCMSampleBufferToSkImage(context, sampleBuffer);
|
|
|
|
|
|
|
|
auto canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
// Clear everything so we keep it at a clean state
|
|
|
|
canvas->clear(SkColors::kBlack);
|
|
|
|
|
|
|
|
// Draw the Image into the Frame (aspectRatio: cover)
|
|
|
|
// The Frame Processor might draw the Frame again (through render()) to pass a custom paint/shader,
|
|
|
|
// but that'll just overwrite the existing one - no need to worry.
|
|
|
|
canvas->drawImage(image, 0, 0);
|
|
|
|
|
|
|
|
// Call the draw callback - probably a JS Frame Processor.
|
|
|
|
callback(static_cast<void*>(canvas));
|
|
|
|
|
|
|
|
// Flush all appended operations on the canvas and commit it to the SkSurface
|
|
|
|
surface->flushAndSubmit();
|
|
|
|
|
|
|
|
// Set dirty & free locks
|
|
|
|
_hasNewFrame = true;
|
|
|
|
lock.unlock();
|
|
|
|
CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
|
2023-02-21 07:00:48 -07:00
|
|
|
}
|
2023-07-20 07:30:04 -06:00
|
|
|
}
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
- (void)renderLatestFrameToLayer:(CALayer* _Nonnull)layer {
|
2023-02-21 07:00:48 -07:00
|
|
|
if (!_hasNewFrame) {
|
|
|
|
// No new Frame has arrived in the meantime.
|
|
|
|
// We don't need to re-draw the texture to the screen if nothing has changed, abort.
|
|
|
|
return;
|
|
|
|
}
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
@autoreleasepool {
|
2023-07-20 07:30:04 -06:00
|
|
|
auto context = _layerContext->skiaContext.get();
|
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
// Create a Skia Surface from the CAMetalLayer (use to draw to the View)
|
|
|
|
GrMTLHandle drawableHandle;
|
|
|
|
auto surface = SkSurface::MakeFromCAMetalLayer(context,
|
2023-07-20 07:30:04 -06:00
|
|
|
(__bridge GrMTLHandle)layer,
|
2023-02-21 07:00:48 -07:00
|
|
|
kTopLeft_GrSurfaceOrigin,
|
|
|
|
1,
|
|
|
|
kBGRA_8888_SkColorType,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
&drawableHandle);
|
|
|
|
if (surface == nullptr || surface->getCanvas() == nullptr) {
|
|
|
|
throw std::runtime_error("Skia surface could not be created from parameters.");
|
|
|
|
}
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
auto canvas = surface->getCanvas();
|
2023-07-20 07:30:04 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
// Lock the Mutex so we can operate on the Texture atomically without
|
|
|
|
// renderFrameToCanvas() overwriting in between from a different thread
|
|
|
|
std::unique_lock lock(_textureMutex);
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
auto texture = _texture;
|
2023-02-21 07:00:48 -07:00
|
|
|
if (texture == nil) return;
|
2023-07-20 07:30:04 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
// Calculate Center Crop (aspectRatio: cover) transform
|
|
|
|
auto sourceRect = SkRect::MakeXYWH(0, 0, texture.width, texture.height);
|
|
|
|
auto destinationRect = SkRect::MakeXYWH(0, 0, surface->width(), surface->height());
|
|
|
|
sourceRect = SkImageHelpers::createCenterCropRect(sourceRect, destinationRect);
|
|
|
|
auto offsetX = -sourceRect.left();
|
|
|
|
auto offsetY = -sourceRect.top();
|
|
|
|
|
|
|
|
// The Canvas is equal to the View size, where-as the Frame has a different size (e.g. 4k)
|
|
|
|
// We scale the Canvas to the exact dimensions of the Frame so that the user can use the Frame as a coordinate system
|
|
|
|
canvas->save();
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
auto scaleW = static_cast<double>(surface->width()) / texture.width;
|
|
|
|
auto scaleH = static_cast<double>(surface->height()) / texture.height;
|
|
|
|
auto scale = MAX(scaleW, scaleH);
|
|
|
|
canvas->scale(scale, scale);
|
|
|
|
canvas->translate(offsetX, offsetY);
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
// Convert the rendered MTLTexture to an SkImage
|
2023-07-03 04:41:26 -06:00
|
|
|
auto image = SkImageHelpers::convertMTLTextureToSkImage(context, texture);
|
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
// Draw the Texture (Frame) to the Canvas
|
|
|
|
canvas->drawImage(image, 0, 0);
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
// Restore the scale & transform
|
|
|
|
canvas->restore();
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
surface->flushAndSubmit();
|
2023-07-03 04:41:26 -06:00
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
// Pass the drawable into the Metal Command Buffer and submit it to the GPU
|
|
|
|
id<CAMetalDrawable> drawable = (__bridge id<CAMetalDrawable>)drawableHandle;
|
2023-07-20 07:30:04 -06:00
|
|
|
id<MTLCommandBuffer> commandBuffer([_layerContext->commandQueue commandBuffer]);
|
2023-02-21 07:00:48 -07:00
|
|
|
[commandBuffer presentDrawable:drawable];
|
|
|
|
[commandBuffer commit];
|
2023-07-20 07:30:04 -06:00
|
|
|
|
|
|
|
// Set flag back to false
|
2023-02-21 07:00:48 -07:00
|
|
|
_hasNewFrame = false;
|
|
|
|
lock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
@end
|