Skia4Delphi - RAD Studio Exclusive Features

From RAD Studio
Jump to: navigation, search
Attention: These are exclusive features not found in the open source project.

Vulkan Backend

The Skia with RAD Studio offers Vulkan backend support for Android and optionally for Windows. When using Skia Canvas with RAD Studio, it automatically utilizes Vulkan on Android if supported, resulting in enhanced graphical performance and energy efficiency compared to OpenGLES. To enable Vulkan on Windows when supported, set the boolean FMX.Types.GlobalUseVulkan to True and the boolean FMX.Skia.GlobalUseSkiaRasterWhenAvailable to False in the initialization section. See an example:

uses
  System.StartUpCopy,
  FMX.Forms,
  FMX.Types,
  FMX.Skia,
  Unit1 in 'Unit1.pas' {Form1};
 
{$R *.res}
 
begin
  GlobalUseSkia := True;
  GlobalUseSkiaRasterWhenAvailable := False;
  GlobalUseVulkan := True;
  Application.Initialize;
  ...
Note: The preference for the Vulkan backend on Android can be disabled using the same boolean GlobalUseVulkan employed for enabling it on Windows, but it should be set to False to deactivate.

Effects and Filters with Skia Shading Language (SKSL)

When using Skia app rendering with GPU acceleration instead of rasterization, Firemonkey leverages the powerful SKSL to render effects and filters. This approach not only provides superior performance compared to the current method, but also enhances Skia Canvas's efficiency.

Moreover, one of the main advantages of using SKSL for creating effects and filters is simplified maintenance. A single shader codebase works consistently across all platforms, eliminating the need for multiple codes specific to each backend API and ensuring uniform results.

Animated Codec: WebP Encoder

The Skia version integrated with RAD Studio supports encoding of animated WebP. With Firemonkey's new animated codec foundation, it is possible to register new encoders capable of encoding and/or decoding.

WebP is one of the most widely used formats on the web due to its superior compression compared to JPEG, resulting in smaller file sizes while maintaining higher quality. It also offers animation capabilities similar to GIFs but with lossless frames, meaning there is no loss of quality and/or some compression. Additionally, it supports transparency, making it a highly versatile format.

To provide an example, assume there are five 400x400 PNG images (frames) named frame1.png, frame2.png, frame3.png, frame4.png, and frame5.png, with each frame having an 80-millisecond interval. To create an animated WebP, first include the units "FMX.Graphics" and "FMX.Skia," then utilize the following code:

uses
  FMX.Graphics;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  var LAnimatedCodec := TAnimatedCodecManager.CreateAnimatedCodec('.webp');
  try
    for var I := 1 to 5 do
    begin
      var LBitmap := TBitmap.Create;
      try
        LBitmap.LoadFromFile(Format('frame%d.png', [I]));
        LAnimatedCodec.AddFrame(LBitmap, 80);
      finally
        LBitmap.Free;
      end;
    end;
    if not LAnimatedCodec.SaveToFile('image.webp') then
      raise Exception.Create('Could not save animated WebP.');
  finally
    LAnimatedCodec.Free;
  end;
end;

Printer

When Skia is enabled, Firemonkey's native Printer for Windows will use Skia as its backend for the physical printer, and ensure support for all other platforms through PDF document printing, with the optional XPS printing also available when the target is Windows. On iOS and Android, the printer will generate a printable file which will be automatically shared upon completion.

To use Firemonkey's printer, follow the simple example below:

uses
  FMX.Printer;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Printer.BeginDoc;
  try
    if Printer.Canvas.BeginScene then
      try
        Form1.PaintTo(Printer.Canvas);
      finally
        Printer.Canvas.EndScene;
      end;
  finally
    Printer.EndDoc;
  end;
end;
Note: For Android targets, a specific permission must be granted through the Project Options. Right-click your project, select "Options...", navigate to "Application" > "Entitlement List", choose the Android target (All configurations), and finally, check the "Secure File Sharing" option.

You can select custom paper sizes or predefined sizes. By default, A4 paper is configured, but it can be easily changed as demonstrated in the example below:

uses
  FMX.Printer;
 
initialization
  // Alter the document's paper size
  TPrinterDocument.SetDocumentPaper(TDocumentPaper.Letter);
end.

Depending on the target devices, document size, and complexity, the UI may become unresponsive due to the resource-intensive operation. In such cases, you can use background thread printing on all platforms to print directly to a stream, as shown in the example below:

uses
  FMX.Printer;
 
procedure TThread1.Execute;
begin
  var LStream := TMemoryStream.Create;
  try
    var LPrinter := PrinterDocumentClass.Create;
    try
      LPrinter.BeginDoc(LStream);
      try
        if LPrinter.Canvas.BeginScene then
        begin
          try
            { TODO: Draw page 1 }
          finally
            LPrinter.Canvas.EndScene;
          end;
        end;
 
        LPrinter.NewPage;
        if LPrinter.Canvas.BeginScene then
        begin
          try
            { TODO: Draw page 2 }
          finally
            LPrinter.Canvas.EndScene;
          end;
        end;
      finally
        LPrinter.EndDoc;
      end;
    finally
      LPrinter.Free;
    end;
    { TODO: Something with LStream }
  finally
    LStream.Free;
  end;
end;

C++ Builder Support

RAD Studio offers Skia4Delphi support for C++ Builder, including the project's main demo written in C++ to assist users in its implementation. To access the demo during RAD Studio installation, opt to install the Samples, or visit Embarcadero's GitHub repository to download it.


See Also