renderViewsToTargets method

FrameCompletion renderViewsToTargets(
  1. List<RenderViewGroup> groups, {
  2. double pixelRatio = 1.0,
})

Renders groups of views into the targets their views own, for a frame loop that is not Flutter's (an XR session handing out swapchain images, a headless capture).

Every view must carry a RenderView.externalTarget; its RenderView.viewport selects the region of that target it fills. The scene's own render-texture views (views) render first, as in renderViewGroups. Nothing here touches a canvas, Flutter's frame scheduler, or GPU pacing, so it may be called from any callback; the runtime owns the cadence. pixelRatio is the view's logical size per target pixel, which only size-dependent projections and picking read.

A target built around a surface frame (ExternalRenderTarget.surfaceFrame) is presented on the frame's last GPU submission, or discarded when the frame is skipped or throws, so the surface always gets its image back.

Returns at once. The result says whether the frame drew and completes when its GPU work has finished, which is when a runtime may release the targets. A frame whose shaders are still loading is skipped and the targets keep their previous contents.

Implementation

FrameCompletion renderViewsToTargets(
  List<RenderViewGroup> groups, {
  double pixelRatio = 1.0,
}) {
  _checkNotDisposed('renderViewsToTargets');
  final views = <RenderView>[for (final group in groups) ...group.views];
  // The distinct targets, in first-use order.
  final targets = <ExternalRenderTarget>[];
  for (final view in views) {
    final target = view.externalTarget;
    if (target != null && !targets.any((other) => identical(other, target))) {
      targets.add(target);
    }
  }
  try {
    for (final view in views) {
      if (view.externalTarget == null) {
        throw ArgumentError(
          'renderViewsToTargets needs an externalTarget on every view; use '
          'renderViews or renderViewGroups for views drawn to a canvas.',
        );
      }
    }
    for (final target in targets) {
      if (target.isSpent) {
        throw StateError(
          'An ExternalRenderTarget.surfaceFrame renders once; build a new '
          'target around the next frame the surface vends.',
        );
      }
    }
    return _renderViewsToTargets(groups, views, targets, pixelRatio);
  } finally {
    // Whatever was not presented (a skipped frame, a throw) goes back to
    // its surface.
    for (final target in targets) {
      target.discardSurfaceFrame();
    }
  }
}