HarmonyOS鸿蒙Next系统使用flutter渲染不支持在插件层更新rgba数据吗?

HarmonyOS鸿蒙Next系统使用flutter渲染不支持在插件层更新rgba数据吗?

问题

在鸿蒙Next系统使用flutter 3.22版本,开发视频渲染插件,在flutter插件层接收到底层C++ 回调上来rgba图像数据,怎么更新到纹理上?

FlutterRenderer 没有更新rgba内存数据到纹理的接口,或者有其他方式更新内存数据到纹理上吗?

import image from '@ohos.multimedia.image';
import { BusinessError } from '@ohos.base';
import { SurfaceTextureEntry, TextureRegistry } from '../../../view/TextureRegistry';
import { FlutterAbility } from '../../ohos/FlutterAbility';
import FlutterNapi from '../FlutterNapi';
import Log from '../../../util/Log';

const TAG = "FlutterRenderer"

export class FlutterRenderer implements TextureRegistry {
  private flutterNapi: FlutterNapi;
  private static globalTextureId: number = 0;

  constructor(flutterNapi: FlutterNapi) {
    this.flutterNapi = flutterNapi;
  }

  getTextureId(): number {
    let nextTextureId: number = FlutterRenderer.globalTextureId + 1;
    FlutterRenderer.globalTextureId = FlutterRenderer.globalTextureId + 1;
    Log.i(TAG, "getTextureId: " + nextTextureId)
    return nextTextureId;
  }

  registerTexture(textureId: number): SurfaceTextureEntry {
    let surfaceTextureRegistryEntry = new SurfaceTextureRegistryEntry(textureId);
    let surfaceId = this.flutterNapi.registerTexture(textureId);
    Log.i(TAG, "registerTexture, surfaceId=" + surfaceId);
    surfaceTextureRegistryEntry.setSurfaceId(surfaceId);
    let nativeWindowId = this.flutterNapi.getTextureNativeWindowId(textureId);
    surfaceTextureRegistryEntry.setNativeWindowId(nativeWindowId);
    return surfaceTextureRegistryEntry;
  }

  registerPixelMap(pixelMap: PixelMap): number {
    let nextTextureId: number = this.getTextureId();
    this.flutterNapi.registerPixelMap(nextTextureId, pixelMap);
    return nextTextureId;
  }

  setTextureBackGroundPixelMap(textureId: number, pixelMap: PixelMap): void {
    this.flutterNapi.setTextureBackGroundPixelMap(textureId, pixelMap);
  }

  setTextureBufferSize(textureId: number, width: number, height: number): void {
    this.flutterNapi.setTextureBufferSize(textureId, width, height);
  }

  notifyTextureResizing(textureId: number, width: number, height: number): void {
    this.flutterNapi.notifyTextureResizing(textureId, width, height);
  }

  setExternalNativeImage(textureId: number, native_image: number): boolean {
    return this.flutterNapi.setExternalNativeImage(textureId, native_image);
  }

  resetExternalTexture(textureId: number, need_surfaceId: boolean): number {
    return this.flutterNapi.resetExternalTexture(textureId, need_surfaceId);
  }

  unregisterTexture(textureId: number): void {
    this.flutterNapi.unregisterTexture(textureId);
  }

  onTrimMemory(level: number) {
    throw new Error('Method not implemented.');
  }
}

export class SurfaceTextureRegistryEntry implements SurfaceTextureEntry {
  private textureId: number = 0;
  private surfaceId: number = 0;
  private nativeWindowId: number = 0;
  private released: boolean = false;

  constructor(id: number) {
    this.textureId = id;
  }

  getTextureId(): number {
    return this.textureId;
  }

  getSurfaceId(): number {
    return this.surfaceId;
  }

  getNativeWindowId(): number {
    return this.nativeWindowId;
  }

  setSurfaceId(surfaceId: number): void {
    this.surfaceId = surfaceId;
  }

  setNativeWindowId(nativeWindowId: number): void {
    this.nativeWindowId = nativeWindowId;
  }

  release() {
    throw new Error('Method not implemented.');
  }
}

ref:https://gitee.com/openharmony-sig/flutter_samples/blob/master/ohos/docs/04_development/Flutter%20OHOS%E5%A4%96%E6%8E%A5%E7%BA%B9%E7%90%86%E9%80%82%E9%85%8D%E7%AE%80%E4%BB%8B.md


更多关于HarmonyOS鸿蒙Next系统使用flutter渲染不支持在插件层更新rgba数据吗?的实战教程也可以访问 https://www.itying.com/category-92-b0.html

2 回复

在HarmonyOS鸿蒙Next系统中,Flutter引擎的插件层确实存在对RGBA数据更新的限制。当前架构下,Flutter的纹理和平台视图交互机制未开放底层RGBA数据的直接写入接口。鸿蒙的图形子系统采用自有合成管线,与Flutter的Skia渲染引擎存在数据通道差异,导致插件层无法像Android/iOS平台那样直接操作像素缓冲区。这属于架构层设计约束,需等待华为官方对Flutter鸿蒙适配层的后续更新。

更多关于HarmonyOS鸿蒙Next系统使用flutter渲染不支持在插件层更新rgba数据吗?的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在HarmonyOS Next中使用Flutter 3.22进行视频渲染时,确实可以通过纹理方式处理RGBA数据。从代码来看,您需要利用FlutterNapi提供的原生能力来实现纹理更新。

关键点:

  1. 使用registerTexture获取纹理ID后,可以通过setExternalNativeImage方法更新纹理数据
  2. 对于RGBA数据,建议先在Native层转换为合适的图像格式(如PixelMap)
  3. 通过SurfaceTextureEntry获取的nativeWindowId可用于底层渲染

替代方案:

  • 考虑使用registerPixelMap直接注册PixelMap格式数据
  • 通过setTextureBackGroundPixelMap更新背景纹理

注意纹理生命周期管理,及时调用unregisterTexture释放资源。具体实现需要结合您的C++层渲染逻辑,通过JNI/NAPI桥接数据。

回到顶部