Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import <Metal/Metal.h>

#include "flutter/common/graphics/texture.h"
#include "flutter/display_list/image/dl_image.h"
#include "flutter/impeller/aiks/aiks_context.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImage.h"
Expand All @@ -29,6 +31,18 @@

@end

@interface FlutterDarwinExternalTextureImpellerImageWrapper : NSObject

+ (sk_sp<flutter::DlImage>)wrapYUVATexture:(nonnull id<MTLTexture>)yTex
UVTex:(nonnull id<MTLTexture>)uvTex
YUVColorSpace:(impeller::YUVColorSpace)colorSpace
aiksContext:(nonnull impeller::AiksContext*)aiksContext;

+ (sk_sp<flutter::DlImage>)wrapRGBATexture:(nonnull id<MTLTexture>)rgbaTex
aiksContext:(nonnull impeller::AiksContext*)aiks_context;

@end

@interface FlutterDarwinExternalTextureMetal : NSObject

- (nullable instancetype)initWithTextureCache:(nonnull CVMetalTextureCacheRef)textureCache
Expand Down
74 changes: 47 additions & 27 deletions shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#import "flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h"
#include "flutter/display_list/image/dl_image.h"
#include "impeller/aiks/aiks_context.h"
#include "impeller/base/validation.h"
#include "impeller/display_list/dl_image_impeller.h"
#include "impeller/renderer/backend/metal/texture_mtl.h"
Expand Down Expand Up @@ -196,29 +197,14 @@ - (void)onTextureUnregistered {
CVBufferRelease(uvMetalTexture);

if (_enableImpeller) {
impeller::TextureDescriptor yDesc;
yDesc.storage_mode = impeller::StorageMode::kHostVisible;
yDesc.format = impeller::PixelFormat::kR8UNormInt;
yDesc.size = {textureSize.width(), textureSize.height()};
yDesc.mip_count = 1;
auto yTexture = impeller::TextureMTL::Wrapper(yDesc, yTex);
yTexture->SetCoordinateSystem(impeller::TextureCoordinateSystem::kUploadFromHost);

impeller::TextureDescriptor uvDesc;
uvDesc.storage_mode = impeller::StorageMode::kHostVisible;
uvDesc.format = impeller::PixelFormat::kR8G8UNormInt;
uvDesc.size = {textureSize.width() / 2, textureSize.height() / 2};
uvDesc.mip_count = 1;
auto uvTexture = impeller::TextureMTL::Wrapper(uvDesc, uvTex);
uvTexture->SetCoordinateSystem(impeller::TextureCoordinateSystem::kUploadFromHost);

impeller::YUVColorSpace yuvColorSpace =
_pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
? impeller::YUVColorSpace::kBT601LimitedRange
: impeller::YUVColorSpace::kBT601FullRange;

return impeller::DlImageImpeller::MakeFromYUVTextures(context.aiks_context, yTexture, uvTexture,
yuvColorSpace);
return [FlutterDarwinExternalTextureImpellerImageWrapper wrapYUVATexture:yTex
UVTex:uvTex
YUVColorSpace:yuvColorSpace
aiksContext:context.aiks_context];
}

SkYUVColorSpace colorSpace = _pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
Expand Down Expand Up @@ -263,14 +249,8 @@ - (void)onTextureUnregistered {
CVBufferRelease(metalTexture);

if (_enableImpeller) {
impeller::TextureDescriptor desc;
desc.storage_mode = impeller::StorageMode::kHostVisible;
desc.format = impeller::PixelFormat::kB8G8R8A8UNormInt;
desc.size = {textureSize.width(), textureSize.height()};
desc.mip_count = 1;
auto texture = impeller::TextureMTL::Wrapper(desc, rgbaTex);
texture->SetCoordinateSystem(impeller::TextureCoordinateSystem::kUploadFromHost);
return impeller::DlImageImpeller::Make(texture);
return [FlutterDarwinExternalTextureImpellerImageWrapper wrapRGBATexture:rgbaTex
aiksContext:context.aiks_context];
}

auto skImage = [FlutterDarwinExternalTextureSkImageWrapper wrapRGBATexture:rgbaTex
Expand Down Expand Up @@ -342,3 +322,43 @@ GrYUVABackendTextures yuvaBackendTextures(yuvaInfo, skiaBackendTextures,
#endif // SLIMPELLER
}
@end

@implementation FlutterDarwinExternalTextureImpellerImageWrapper

+ (sk_sp<flutter::DlImage>)wrapYUVATexture:(id<MTLTexture>)yTex
UVTex:(id<MTLTexture>)uvTex
YUVColorSpace:(impeller::YUVColorSpace)colorSpace
aiksContext:(nonnull impeller::AiksContext*)aiks_context {
impeller::TextureDescriptor yDesc;
yDesc.storage_mode = impeller::StorageMode::kDevicePrivate;
yDesc.format = impeller::PixelFormat::kR8UNormInt;
yDesc.size = impeller::ISize(yTex.width, yTex.height);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this code was hitting asserts running the AV video player example app because the sizes supplied didn't match the actual texture sizes. I changed the logic to read the texture descriptor size from the textures themselves so this can't get out of sync.

yDesc.mip_count = 1;
auto yTexture = impeller::TextureMTL::Wrapper(yDesc, yTex);
yTexture->SetCoordinateSystem(impeller::TextureCoordinateSystem::kUploadFromHost);

impeller::TextureDescriptor uvDesc;
uvDesc.storage_mode = impeller::StorageMode::kDevicePrivate;
uvDesc.format = impeller::PixelFormat::kR8G8UNormInt;
uvDesc.size = impeller::ISize(uvTex.width, uvTex.height);
uvDesc.mip_count = 1;
auto uvTexture = impeller::TextureMTL::Wrapper(uvDesc, uvTex);
uvTexture->SetCoordinateSystem(impeller::TextureCoordinateSystem::kUploadFromHost);
;

return impeller::DlImageImpeller::MakeFromYUVTextures(aiks_context, yTexture, uvTexture,
colorSpace);
}

+ (sk_sp<flutter::DlImage>)wrapRGBATexture:(id<MTLTexture>)rgbaTex
aiksContext:(nonnull impeller::AiksContext*)aiks_context {
impeller::TextureDescriptor desc;
desc.storage_mode = impeller::StorageMode::kDevicePrivate;
desc.format = impeller::PixelFormat::kB8G8R8A8UNormInt;
desc.size = impeller::ISize(rgbaTex.width, rgbaTex.height);
desc.mip_count = 1;
auto texture = impeller::TextureMTL::Wrapper(desc, rgbaTex);
texture->SetCoordinateSystem(impeller::TextureCoordinateSystem::kUploadFromHost);
return impeller::DlImageImpeller::Make(texture);
}
@end
Loading