美文网首页
flutter中ios的Texture实现

flutter中ios的Texture实现

作者: ben大福 | 来源:发表于2020-08-19 14:47 被阅读0次

首先ios原生注册的地方

// shell/platform/darwin/ios/framwork/Source/FlutterEngin.mm
- (int64_t)registerTexture:(NSObject<FlutterTexture>*)texture {
  int64_t textureId = _nextTextureId++;
  self.iosPlatformView->RegisterExternalTexture(textureId, texture);
  return textureId;
}
// shell/platform/darwin/ios/platform_view_ios.mm
void PlatformViewIOS::RegisterExternalTexture(int64_t texture_id,
                                              NSObject<FlutterTexture>* texture) {
  RegisterTexture(ios_context_->CreateExternalTexture(
      texture_id, fml::scoped_nsobject<NSObject<FlutterTexture>>{[texture retain]}));
}

//shell/platform/darwin/ios/ios_external_texture_gl.mm
std::unique_ptr<Texture> IOSContextGL::CreateExternalTexture(
    int64_t texture_id,
    fml::scoped_nsobject<NSObject<FlutterTexture>> texture) {
  return std::make_unique<IOSExternalTextureGL>(texture_id, std::move(texture));
}

CreatExternalTexture最终会返回一个IOSExternalTextureGL(shell/platform/darwin/ios/ios_external_texture_gl.mm),
IOSExternalTextureGL继承于Texture(flutter/flow/texture.h)
我们回到RegisterTexture方法。

void TextureRegistry::RegisterTexture(std::shared_ptr<Texture> texture) {
  if (!texture) {
    return;
  }
  mapping_[texture->Id()] = texture;
}

这里跟android处理一样了
剩下的逻辑跟之前的android篇一样可以参考:https://www.jianshu.com/p/213d8a6f204c
直接跳转到关键方法

void IOSExternalTextureGL::Paint(SkCanvas& canvas,
                                 const SkRect& bounds,
                                 bool freeze,
                                 GrContext* context) {
  EnsureTextureCacheExists();
  if (NeedUpdateTexture(freeze)) {
    auto pixelBuffer = [external_texture_.get() copyPixelBuffer];
    if (pixelBuffer) {
      buffer_ref_.Reset(pixelBuffer);
    }
    CreateTextureFromPixelBuffer();
    new_frame_ready_ = false;
  }
  if (!texture_ref_) {
    return;
  }
  GrGLTextureInfo textureInfo = {CVOpenGLESTextureGetTarget(texture_ref_),
                                 CVOpenGLESTextureGetName(texture_ref_), GL_RGBA8_OES};
  GrBackendTexture backendTexture(bounds.width(), bounds.height(), GrMipMapped::kNo, textureInfo);
  sk_sp<SkImage> image =
      SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
                               kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
  FML_DCHECK(image) << "Failed to create SkImage from Texture.";
  if (image) {
    canvas.drawImage(image, bounds.x(), bounds.y());
  }
}

1,调用copyPixelBuffer返回当前最新帧
2, CreateTextureFromPixelBuffer 创建绘制OpenglTexture
3,将OpenglTexture封装成SKImage
2,利用Skia将buffer渲染到fultter上

参考文章:https://www.jianshu.com/p/213d8a6f204c

相关文章

网友评论

      本文标题:flutter中ios的Texture实现

      本文链接:https://www.haomeiwen.com/subject/skwrjktx.html