How to Import Self-Rendered Content into Flutter
Background Introduction
It supports importing externally self-rendered content (whether drawn via OHOS CAPI such as NativeDrawing or via OpenGL-related interfaces) into Flutter's Texture component for display through external textures. The following is the specific implementation method.
Prerequisites
Flutter version 3.22 or above is required
Implementation Steps
1. Dart-Side Operations
1.1 Create a Texture Widget
- Create a Texture widget and bind the
textureId.
1.2 Register the Texture
-
Create a
MethodChannel:final MethodChannel _channel = const MethodChannel('PictureChannel'); -
Register the texture in the
initStatefunction:_textureId = await _channel.invokeMethod("registerStarTexture");
1.3 Unregister the Texture
-
Unregister the texture in the
disposefunction:_channel.invokeMethod('unregisterTexture', {'textureId': _textureId});
2. OHOS-Side Operations
2.1 Import the Utility Class
You need to import the following utility class:
import { TextureRegistry, SurfaceTextureEntry } from '@ohos/flutter_ohos/src/main/ets/view/TextureRegistry';
2.2 Provided Implementation Methods
Two implementations are provided for importing self-rendered content into Flutter textures.
Method 1: Draw to a Buffer via getNativeWindowId
Obtain the engine-level OHNativeWindow pointer corresponding to the Texture by using getNativeWindowId, and then draw self-rendered content to the buffer associated with the window.
ArkTS-Side Implementation Steps
- Obtain the relevant information when registering the texture:
this.textureId = this.textureRegistry!.getTextureId();
this.surfaceTextureEntry = this.textureRegistry!.registerTexture(this.textureId);
this.windowId = this.surfaceTextureEntry.getNativeWindowId();
drawNapi.drawPatternWindow(this.windowId, 300, 300); // Needs to be implemented in the cpp layer, receiving the window pointer
-
Call the
cpplayer function to draw self-rendered content to thewindow:drawNapi.drawPatternWindow(this.windowId, 300, 300); // Needs to be implemented in the cpp layer
CPP-Side Implementation Steps
- Use
OH_NativeWindow_NativeWindowHandleOptto set thewindow'sUSAGE, width, height, and other information. - Call
OH_NativeWindow_NativeWindowRequestBufferto get the buffer of thewindow. - Use
OH_NativeWindow_GetBufferHandleFromNativeto get thebufferHandle. - Use
OH_NativeWindow_NativeWindowFlushBufferto put the produced buffer into the buffer queue for consumption.
Method 2: Use setExternalNativeImage
Directly pass the NativeImage pointer used for content production to the Flutter engine layer. The engine layer uses the NativeImage and registers a frame-available callback using OH_NativeImage_SetOnFrameAvailableListener for content consumption.
ArkTS-Side Implementation Steps
- Obtain the relevant information when registering the texture:
this.textureId = this.textureRegistry!.getTextureId();
this.surfaceTextureEntry = this.textureRegistry!.registerTexture(this.textureId);
drawNapi.drawPatternWindow(1000, 1000);
this.nativeImageId = drawNapi.getNativeImage(); // Get the NativeImage used for content production
this.textureRegistry!.setExternalNativeImage(this.textureId, this.nativeImageId); // Pass the NativeImage to the engine layer
CPP-Side Implementation Steps
Here are the steps for a simple drawing implementation based on OpenGL:
- Use
OH_NativeImage_Createto create aNativeImage. - Use
OH_NativeImage_AcquireNativeWindowto get thewindowassociated with theNativeImage. - Use
OH_NativeWindow_NativeWindowHandleOptto setUSAGEand width/height. - Convert
NativeWindowto the typeEGLNativeWindowType. - Call the following OpenGL-related commands to complete the rendering:
eglCreateWindowSurfaceeglCreateContexteglMakeCurrent- OpenGL drawing commands
eglSwapBuffers
Texture Unregistration
No matter which method is used above, the following function needs to be called to unregister the texture:
this.textureRegistry!.unregisterTexture(textureId);
Reference Implementations
Reference
Code Location Description
-
Dart-Side Code
Method 1: The relevant code is located inlib/PicturePage1.dart. The texture component added at the bottom is used to import self-rendered content.
Method 2: The relevant code is located inlib/PicturePage2.dart. -
ArkTS-Side Code
Method 1: The relevant code is located inohos/entry/src/main/ets/entryability/PicturePlugin1.ets, which includes functions for registering and unregistering textures.
Method 2: The relevant code is located inohos/entry/src/main/ets/entryability/PicturePlugin2.ets. -
Native Rendering Code
The relevant code is located inohos/entry/src/main/cpp.
Notes
- When using Method 2, developers need to manage the lifecycle of
NativeImageby themselves and ensure that the object is not destroyed beforeunregister.