Skia Canvas (GlobalUseSkia)

FireMonkey can draw all 2D content with Skia instead of Direct2D (GlobalUseSkia := True). Gorilla3D works with the Skia canvas, but the combination used to cost a lot of performance: depending on the form, the frame rate dropped to a fraction of the Direct2D value and the main thread was fully loaded even at 60 FPS.

Gorilla3D v1.5.1 addresses this. This page explains where the cost comes from, what v1.5.1 changes, and which settings you have.

Units: Gorilla.Viewport, Gorilla.Layers Platforms: Windows (all improvements), other platforms (repaint deferral and Skia animations in layers only)

Table of Contents

The Problem

How the viewport reaches the screen

TGorillaViewport does not draw onto the FMX form canvas directly. The 3D scene is rendered by Gorilla3D's own graphics context into an offscreen image. FMX then draws this image in TGorillaViewport.Paint, like any other bitmap, together with all 2D controls of the form. In fixed frame rate mode (UseFixedFrameRate, the default on all platforms except iOS) this repeats every frame: render the scene, then invalidate the viewport so that FMX paints the new image.

Why Skia repaints the whole form

A FireMonkey canvas declares its capabilities in GetCanvasStyle. The Direct2D canvas supports clip rects (TCanvasStyle.SupportClipRects): when a control invalidates its area, FMX repaints only that area.

The Skia canvases do not declare SupportClipRects. Without it, TCustomForm.AddUpdateRect widens every invalidation to the full client area of the form. The form clears its background completely and paints all of its controls again.

For a 3D viewport, which invalidates itself every frame, this means:

  • every 3D frame repaints the entire form, including every 2D control, even controls that do not overlap the viewport,
  • the cost grows with the number and complexity of the 2D controls on the form, not with the 3D scene,
  • every independent source of invalidation causes its own full repaint: the 3D frame, every tick of a Lottie animation (TSkAnimatedImage), FMX animations, a camera controller. At 60 FPS the form was repainted 150-180 times per second.

With Direct2D the same form repaints only the viewport rectangle, and the 2D controls around it are not touched.

Note: This is a property of the Skia canvas in FireMonkey, not of Gorilla3D. Any FMX control that invalidates frequently (video, game view, animation) is affected the same way. Gorilla3D v1.5.1 reduces how often the full repaint happens. The cost of one full repaint itself remains.

What Changed in v1.5.1

All changes are active automatically, but only when the active canvas cannot repaint partial regions (no SupportClipRects) and is not a GPU canvas that shares the render context. In practice this means the Skia canvases. Direct2D, the FMX GPU canvas (GlobalUseGPUCanvas), design time and event-based rendering (UseFixedFrameRate = False) behave as before.

1. Canvas detection

The viewport checks the capabilities of the active canvas instead of GlobalUseSkia. This also covers the case where GlobalUseSkia is set but Skia could not be activated.

Class function Returns True if
TGorillaViewport.CanvasRepaintsWholeForm the canvas has no SupportClipRects, so FMX repaints the whole form for every invalidation
TGorillaViewport.CanvasSharesRenderContext the canvas is a GPU canvas (TCustomCanvasGpu) that shares the graphics context with the renderer
TGorillaViewport.UsesWholeFormRepaintHandling the first is True and the second is False - the v1.5.1 handling applies

The GPU canvas is excluded on purpose: it relies on the canvas scene during rendering.

2. Repaint requests are collected until the next frame

Every change of a 3D object requests a repaint of the viewport: moving a mesh, an animated skeleton, particles, a rotation set in a timer, a camera controller update. In fixed frame rate mode such a repaint only draws the last rendered image again, because the next frame renders and invalidates anyway. On Skia each of these requests was a full form repaint.

Since v1.5.1 these requests are only recorded. The next render frame invalidates exactly once. This covers NeedRender (changes of 3D objects) as well as BeginUpdate / EndUpdate on the viewport.

3. FMX paint synchronized with the display refresh (Windows, Delphi 13)

The Gorilla3D render loop keeps running at FixedFrameRate on its own timer. What changes is the moment the new image is handed to FMX:

  • after each rendered frame the viewport only marks "new image available",
  • on the next tick of the FMX display link (IFMXDisplayLinkService, the same clock FMX and Lottie animations run on) the viewport invalidates, if a new image is available,
  • Windows merges this invalidation and the ones from the animations of the same refresh into one paint of the form.

Result: one full repaint per display refresh, no matter how many animations are running, while the 3D scene is still rendered at FixedFrameRate. With FixedFrameRate above the display refresh rate, the additional frames are rendered, but only the latest one per refresh is shown.

4. No empty presents on the Skia GPU canvas

The render frame no longer opens a scene on the form canvas when a Skia canvas is active. On the Skia Vulkan canvas every BeginScene / EndScene on the form presents a swap chain image, even when nothing was drawn. That showed a stale image in between and made the 3D area flicker.

5. Skia animations inside TGorillaLayer3D (Delphi 12 and newer)

A TSkAnimatedImage (Lottie) or TSkAnimatedPaintBox inside a 3D layer never animated. TGorillaCustomLayer3D now advances them itself, see Skia Content inside the 3D Scene.

Measurements

Example measurements on Windows 11: 1600 x 900 form, a sidebar with 300 controls (TRectangle + TText) and 4 Lottie animations next to the viewport (not overlapping), FixedFrameRate = 60. CPU load is the load of the main thread in % of one core.

Canvas v1.5.0: form repaints / s v1.5.0: main thread v1.5.1: form repaints / s v1.5.1: main thread
Direct2D 121 (viewport area only) 20 % 121 (viewport area only) 20 %
Skia raster 150 (full form) 89 % 61 (full form) 40 %
Skia GPU (Vulkan) 182 (full form) 76 % 61 (full form) 30 %

With an unlimited frame rate (FixedFrameRate = 1000) the same scene renders on Skia at about 640-770 FPS in v1.5.1, compared to 94-156 FPS in v1.5.0. The form is still painted only once per display refresh.

Direct2D remains the cheapest option for forms with many 2D controls: it repaints only the areas that changed.

Enabling Skia

Skia is enabled by FireMonkey, not by Gorilla3D. Set GlobalUseSkia in the project file before Application.Initialize:

program MyProject;

uses
  Gorilla.Context.Backend, // Gorilla3D backend unit first, see Graphics Backend
  System.StartUpCopy,
  FMX.Forms,
  FMX.Skia,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  GlobalUseSkia := True;

  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

If the canvas service has already started when GlobalUseSkia is set, the setting has no effect. In debug builds FMX raises an exception telling you to move it earlier. In that case set it in the initialization section of a small unit that is listed directly behind FMX.Forms in the .dpr.

You can check which canvas is actually active at runtime:

Log.d('Canvas: %s, Skia repaint handling: %s',
  [TCanvasManager.DefaultCanvas.ClassName,
   BoolToStr(TGorillaViewport.UsesWholeFormRepaintHandling(), True)]);

Choosing the Skia Canvas

On Windows, Skia offers a CPU and a GPU canvas. GlobalUseSkiaRasterWhenAvailable (unit FMX.Skia) selects between them:

Setting Canvas Notes
True (default on Windows) TSkRasterCanvas Draws on the CPU and copies the result to the window (GDI BitBlt). The cost of a full repaint grows with window size and number of controls.
False TVkCanvas (Delphi 13.2 and newer) Draws on the GPU via Vulkan. Lower CPU load per repaint.
  GlobalUseSkia := True;
  GlobalUseSkiaRasterWhenAvailable := False; // GPU canvas

In the measurements above, the Skia GPU canvas needed about 25 % less main thread time than the raster canvas. Both work with Gorilla3D, including the v1.5.1 improvements.

Frame Rate Settings

Setting Effect with Skia (v1.5.1)
TGorillaViewport.UseFixedFrameRate Must be True (default on all platforms except iOS) for the improvements 2-4.
TGorillaViewport.FixedFrameRate Rate of the Gorilla3D render loop. Not limited by the display link.
TGorillaViewport.VSync Limits the render loop to the refresh rate of the display, as before.
GlobalPreferredFramesPerSecond (FMX, default 60) Rate of the FMX display link, and with it the rate at which the form is painted. It never exceeds the refresh rate of the display. Raise it (e.g. to 144) if you want the form painted more often on a high refresh display.

Rendering faster than the form is painted only makes sense if you need the frames for something else, e.g. physics or an input latency budget. For a typical application, FixedFrameRate equal to the display refresh rate (or VSync := True) keeps the CPU load lowest.

Opt-Out Switches

Both switches are class variables of TGorillaViewport and apply to all viewports. Set them before the first viewport starts rendering.

Switch Default True restores
TGorillaViewport.ImmediateRepaints False Every change of a 3D object and every EndUpdate repaints the viewport immediately (change 2 above is off).
TGorillaViewport.TimerPresentation False The viewport invalidates right after every render frame instead of on the display link (change 3 above is off).
  // behaviour of v1.5.0 on Skia
  TGorillaViewport.ImmediateRepaints := True;
  TGorillaViewport.TimerPresentation := True;

You normally do not need them. They are meant for comparison and as a fallback if an application relies on the old timing.

Skia Content inside the 3D Scene

Skia controls directly in the viewport

Skia controls (TSkLabel, TSkAnimatedImage, TSkSvg, ...) can be placed as 2D children of TGorillaViewport. FMX paints them on top of the 3D image. Lottie animations run normally there.

Skia controls in a TGorillaLayer3D

TGorillaLayer3D paints its 2D children into its own texture, which is shown on a quad in the 3D scene. Static Skia controls and controls that you redraw yourself (e.g. TSkPaintBox with Redraw) work as usual.

Skia animations (TSkAnimatedImage, TSkAnimatedPaintBox) cannot run inside any 3D layer by themselves: Skia only runs an animation when its scene is a form (TSkCustomAnimatedControl.CanRunAnimation), and inside a layer the scene is the layer. Since v1.5.1, TGorillaLayer3D and TGorillaTextLayer3D advance these animations themselves, once per rendered frame and at most about 80 times per second:

Animation property Supported
Enabled, Pause, Loop, Speed yes
Inverse, AutoReverse, Delay, StartProgress, StopProgress no (always plays forward)
  • Requires Delphi 12 or newer. Skia is accessed through RTTI, so Gorilla3D does not add a package dependency on Skia.
  • The animation only advances while the layer is rendered.
  • At design time the layer content does not animate.
  • A plain FMX TLayer3D does not get this. Use TGorillaLayer3D.
var
  LLayer : TGorillaLayer3D;
  LLottie : TSkAnimatedImage;
begin
  LLayer := TGorillaLayer3D.Create(Self);
  LLayer.Parent := GorillaViewport1;
  LLayer.Resolution := 200;   // pixels per 3D unit of the layer texture
  LLayer.Width  := 1.5;
  LLayer.Height := 1.8;

  LLottie := TSkAnimatedImage.Create(Self);
  LLottie.Parent := LLayer;
  LLottie.Align  := TAlignLayout.Client;
  LLottie.LoadFromFile('animation.json');
  LLottie.Animation.Loop := True;
end;

Texture resolution: the layer texture is Width x Resolution by Height x Resolution pixels. The default Resolution is 50 pixels per unit, so a large layer with the default resolution looks blurry and stretched. Raise Resolution for crisp 2D content.

Best Practices

  1. Prefer Direct2D for forms with many 2D controls next to a viewport, if you do not need Skia features for the whole form. Individual Skia controls (TSkLabel, TSkAnimatedImage, ...) also work on the Direct2D canvas.
  2. With Skia, keep the form around the viewport lean. Every paint of the form repaints all controls. Controls that are hidden or on another form do not cost anything.
  3. Prefer the Skia GPU canvas (GlobalUseSkiaRasterWhenAvailable := False, Delphi 13.2+) over the raster canvas.
  4. Do not render much faster than the form is painted unless you need the extra frames. Match FixedFrameRate to the display, or use VSync.
  5. Avoid your own per-frame Invalidate / Repaint calls on the form or on 2D controls. Under Skia each one can be a full form repaint that is not merged with the 3D frame.

Troubleshooting

Frame rate much lower with Skia than with Direct2D

Expected to some degree: Skia repaints the whole form for every paint. Check the number of 2D controls on the form, prefer the GPU canvas, and make sure UseFixedFrameRate is True so that the v1.5.1 improvements apply.

GlobalUseSkia has no effect

GlobalUseSkia was set after the canvas service started. Set it before Application.Initialize, or in the initialization section of a unit listed directly behind FMX.Forms.

The 3D area flickers with the Skia GPU canvas

Fixed in v1.5.1. On older versions this appeared as soon as the paint did not follow the render frame immediately.

Lottie animation does not play inside a 3D layer

Use TGorillaLayer3D (not FMX TLayer3D) and Delphi 12 or newer. Check that Animation.Enabled is True and Animation.Pause is False.

2D children of the viewport still cause extra repaints

2D controls placed directly in the viewport take part in BeginUpdate / EndUpdate of the viewport. Each EndUpdate repaints them, which under Skia is a full form repaint. If a camera controller or your own code brackets updates with Viewport.BeginUpdate / EndUpdate very often, this adds repaints. Move frequently updated 2D overlays to their own layout outside the viewport where possible.

See Also