chore: Extract to convertMTLTextureToSkImage

This commit is contained in:
Marc Rousavy 2023-07-03 12:41:26 +02:00
parent 820db3ca9e
commit 82eaf9594f
3 changed files with 52 additions and 40 deletions

View File

@ -11,6 +11,8 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h> #import <AVFoundation/AVFoundation.h>
#import <MetalKit/MetalKit.h>
#import <include/gpu/GrRecordingContext.h> #import <include/gpu/GrRecordingContext.h>
#import "SkImage.h" #import "SkImage.h"
@ -26,6 +28,10 @@ public:
Convert a CMSampleBuffer to an SkImage. Format has to be RGB. Convert a CMSampleBuffer to an SkImage. Format has to be RGB.
*/ */
static sk_sp<SkImage> convertCMSampleBufferToSkImage(GrRecordingContext* context, CMSampleBufferRef sampleBuffer); static sk_sp<SkImage> convertCMSampleBufferToSkImage(GrRecordingContext* context, CMSampleBufferRef sampleBuffer);
/**
Convert a MTLTexture to an SkImage. Format has to be RGB.
*/
static sk_sp<SkImage> convertMTLTextureToSkImage(GrRecordingContext* context, id<MTLTexture> mtlTexture);
/** /**
Creates a Center Crop Transformation Rect so that the source rect fills (aspectRatio: cover) the destination rect. Creates a Center Crop Transformation Rect so that the source rect fills (aspectRatio: cover) the destination rect.
The return value should be passed as a sourceRect to a canvas->draw...Rect(..) function, destinationRect should stay the same. The return value should be passed as a sourceRect to a canvas->draw...Rect(..) function, destinationRect should stay the same.

View File

@ -86,6 +86,20 @@ sk_sp<SkImage> SkImageHelpers::convertCMSampleBufferToSkImage(GrRecordingContext
return image; return image;
} }
sk_sp<SkImage> SkImageHelpers::convertMTLTextureToSkImage(GrRecordingContext* context, id<MTLTexture> texture) {
// Convert the rendered MTLTexture to an SkImage
GrMtlTextureInfo textureInfo;
textureInfo.fTexture.retain((__bridge void*)texture);
GrBackendTexture backendTexture(texture.width, texture.height, GrMipmapped::kNo, textureInfo);
auto image = SkImages::AdoptTextureFrom(context,
backendTexture,
kTopLeft_GrSurfaceOrigin,
kBGRA_8888_SkColorType,
kOpaque_SkAlphaType,
SkColorSpace::MakeSRGB());
return image;
}
SkRect SkImageHelpers::createCenterCropRect(SkRect sourceRect, SkRect destinationRect) { SkRect SkImageHelpers::createCenterCropRect(SkRect sourceRect, SkRect destinationRect) {
SkSize src; SkSize src;
if (destinationRect.width() / destinationRect.height() > sourceRect.width() / sourceRect.height()) { if (destinationRect.width() / destinationRect.height() > sourceRect.width() / sourceRect.height()) {

View File

@ -117,15 +117,7 @@ void SkiaMetalCanvasProvider::render() {
canvas->translate(offsetX, offsetY); canvas->translate(offsetX, offsetY);
// Convert the rendered MTLTexture to an SkImage // Convert the rendered MTLTexture to an SkImage
GrMtlTextureInfo textureInfo; auto image = SkImageHelpers::convertMTLTextureToSkImage(context, texture);
textureInfo.fTexture.retain((__bridge void*)texture);
GrBackendTexture backendTexture(texture.width, texture.height, GrMipmapped::kNo, textureInfo);
auto image = SkImage::MakeFromTexture(context,
backendTexture,
kTopLeft_GrSurfaceOrigin,
kBGRA_8888_SkColorType,
kOpaque_SkAlphaType,
SkColorSpace::MakeSRGB());
// Draw the Texture (Frame) to the Canvas // Draw the Texture (Frame) to the Canvas
canvas->drawImage(image, 0, 0); canvas->drawImage(image, 0, 0);