#pragma once #ifndef __cplusplus #error This header has to be compiled with C++! #endif #import #import #import #include #include #include #include #include #include #import "VisionDisplayLink.h" #import "SkiaMetalRenderContext.h" class SkiaMetalCanvasProvider: public std::enable_shared_from_this { public: SkiaMetalCanvasProvider(); ~SkiaMetalCanvasProvider(); // Render a Camera Frame to the off-screen canvas void renderFrameToCanvas(CMSampleBufferRef sampleBuffer, const std::function& drawCallback); // Start updating the DisplayLink (runLoop @ screen refresh rate) and draw Frames to the Layer void start(); // Update the size of the View (Layer) void setSize(int width, int height); CALayer* getLayer(); private: bool _isValid = false; float _width = -1; float _height = -1; // For rendering Camera Frame -> off-screen MTLTexture OffscreenRenderContext _offscreenContext; // For rendering off-screen MTLTexture -> on-screen CAMetalLayer LayerRenderContext _layerContext; // For synchronization between the two Threads/Contexts std::mutex _textureMutex; std::atomic _hasNewFrame = false; private: void render(); id getTexture(int width, int height); float getPixelDensity(); };