Skip to content

Commit

Permalink
Update ios to use new YUVA texture SkImage factory (flutter#23153)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsalomon authored Dec 21, 2020
1 parent d193a60 commit 3a58179
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
35 changes: 14 additions & 21 deletions shell/platform/darwin/ios/ios_external_texture_gl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

#import "flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/core/SkYUVAIndex.h"
#include "third_party/skia/include/core/SkYUVAInfo.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"
#include "third_party/skia/src/gpu/gl/GrGLDefines.h"
#include "third_party/skia/include/gpu/GrYUVABackendTextures.h"

namespace flutter {

Expand Down Expand Up @@ -108,27 +108,20 @@

sk_sp<SkImage> IOSExternalTextureGL::CreateImageFromYUVTextures(GrDirectContext* context,
const SkRect& bounds) {
GrBackendTexture textures[2];
GrGLTextureInfo yTextureInfo = {CVOpenGLESTextureGetTarget(y_texture_ref_),
CVOpenGLESTextureGetName(y_texture_ref_), GR_GL_LUMINANCE8};
GrBackendTexture yBackendTexture(bounds.width(), bounds.height(), GrMipMapped::kNo, yTextureInfo);
CVOpenGLESTextureGetName(y_texture_ref_), GL_LUMINANCE8_EXT};
textures[0] = GrBackendTexture(bounds.width(), bounds.height(), GrMipMapped::kNo, yTextureInfo);
GrGLTextureInfo uvTextureInfo = {CVOpenGLESTextureGetTarget(uv_texture_ref_),
CVOpenGLESTextureGetName(uv_texture_ref_), GR_GL_RGBA8};
GrBackendTexture uvBackendTexture(bounds.width(), bounds.height(), GrMipMapped::kNo,
uvTextureInfo);
GrBackendTexture nv12TextureHandles[] = {yBackendTexture, uvBackendTexture};
SkYUVAIndex yuvaIndices[4] = {
SkYUVAIndex{0, SkColorChannel::kR}, // Read Y data from the red channel of the first texture
SkYUVAIndex{1, SkColorChannel::kR}, // Read U data from the red channel of the second texture
SkYUVAIndex{
1, SkColorChannel::kA}, // Read V data from the alpha channel of the second texture,
// normal NV12 data V should be taken from the green channel, but
// currently only the uv texture created by GL_LUMINANCE_ALPHA
// can be used, so the V value is taken from the alpha channel
SkYUVAIndex{-1, SkColorChannel::kA}}; //-1 means to omit the alpha data of YUVA
SkISize size{yBackendTexture.width(), yBackendTexture.height()};
sk_sp<SkImage> image = SkImage::MakeFromYUVATextures(
context, kRec601_SkYUVColorSpace, nv12TextureHandles, yuvaIndices, size,
kTopLeft_GrSurfaceOrigin, /*imageColorSpace=*/nullptr);
CVOpenGLESTextureGetName(uv_texture_ref_),
GL_LUMINANCE8_ALPHA8_EXT};
textures[1] = GrBackendTexture(bounds.width(), bounds.height(), GrMipMapped::kNo, uvTextureInfo);

SkYUVAInfo yuvaInfo(textures[0].dimensions(), SkYUVAInfo::PlaneConfig::kY_UV,
SkYUVAInfo::Subsampling::k444, kRec601_SkYUVColorSpace);
GrYUVABackendTextures yuvaBackendTextures(yuvaInfo, textures, kTopLeft_GrSurfaceOrigin);
sk_sp<SkImage> image = SkImage::MakeFromYUVATextures(context, yuvaBackendTextures,
/*imageColorSpace=*/nullptr);
return image;
}

Expand Down
37 changes: 18 additions & 19 deletions shell/platform/darwin/ios/ios_external_texture_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#import "flutter/shell/platform/darwin/ios/ios_external_texture_metal.h"

#include "flutter/fml/logging.h"
#include "third_party/skia/include/core/SkYUVAIndex.h"
#include "third_party/skia/include/core/SkYUVAInfo.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"
#include "third_party/skia/include/gpu/GrYUVABackendTextures.h"
#include "third_party/skia/include/gpu/mtl/GrMtlTypes.h"

namespace flutter {
Expand Down Expand Up @@ -132,28 +133,26 @@
y_skia_texture_info.fTexture = sk_cf_obj<const void*>{
[reinterpret_cast<NSObject*>(CVMetalTextureGetTexture(y_metal_texture)) retain]};

GrBackendTexture y_skia_backend_texture(/*width=*/texture_size.width(),
/*height=*/texture_size.height(),
/*mipMapped=*/GrMipMapped ::kNo,
/*textureInfo=*/y_skia_texture_info);
GrBackendTexture skia_backend_textures[2];
skia_backend_textures[0] = GrBackendTexture(/*width=*/texture_size.width(),
/*height=*/texture_size.height(),
/*mipMapped=*/GrMipMapped ::kNo,
/*textureInfo=*/y_skia_texture_info);

fml::CFRef<CVMetalTextureRef> uv_metal_texture(uv_metal_texture_raw);

GrMtlTextureInfo uv_skia_texture_info;
uv_skia_texture_info.fTexture = sk_cf_obj<const void*>{
[reinterpret_cast<NSObject*>(CVMetalTextureGetTexture(uv_metal_texture)) retain]};

GrBackendTexture uv_skia_backend_texture(/*width=*/texture_size.width(),
/*height=*/texture_size.height(),
/*mipMapped=*/GrMipMapped ::kNo,
/*textureInfo=*/uv_skia_texture_info);
GrBackendTexture nv12TextureHandles[] = {y_skia_backend_texture, uv_skia_backend_texture};
SkYUVAIndex yuvaIndices[4] = {
SkYUVAIndex{0, SkColorChannel::kR}, // Read Y data from the red channel of the first texture
SkYUVAIndex{1, SkColorChannel::kR}, // Read U data from the red channel of the second texture
SkYUVAIndex{1,
SkColorChannel::kG}, // Read V data from the green channel of the second texture
SkYUVAIndex{-1, SkColorChannel::kA}}; //-1 means to omit the alpha data of YUVA
skia_backend_textures[1] = GrBackendTexture(/*width=*/texture_size.width(),
/*height=*/texture_size.height(),
/*mipMapped=*/GrMipMapped ::kNo,
/*textureInfo=*/uv_skia_texture_info);
SkYUVAInfo yuva_info(skia_backend_textures[0].dimensions(), SkYUVAInfo::PlaneConfig::kY_UV,
SkYUVAInfo::Subsampling::k444, kRec601_SkYUVColorSpace);
GrYUVABackendTextures yuva_backend_textures(yuva_info, skia_backend_textures,
kTopLeft_GrSurfaceOrigin);

struct ImageCaptures {
fml::CFRef<CVPixelBufferRef> buffer;
Expand All @@ -170,9 +169,9 @@ GrBackendTexture uv_skia_backend_texture(/*width=*/texture_size.width(),
auto captures = reinterpret_cast<ImageCaptures*>(release_context);
delete captures;
};
sk_sp<SkImage> image = SkImage::MakeFromYUVATextures(
context, kRec601_SkYUVColorSpace, nv12TextureHandles, yuvaIndices, texture_size,
kTopLeft_GrSurfaceOrigin, /*imageColorSpace=*/nullptr, release_proc, captures.release());
sk_sp<SkImage> image =
SkImage::MakeFromYUVATextures(context, yuva_backend_textures, /*imageColorSpace=*/nullptr,
release_proc, captures.release());
return image;
}

Expand Down

0 comments on commit 3a58179

Please sign in to comment.