Skia

From Free Pascal wiki
Jump to navigationJump to search

Overview

Skiais an open source cross platform 2D graphics library used for example by Chrome and Firefox.

The skia4delphiproject provides shared and static libraries and Pascal units to access Skia with FPC and Delphi.

In lazarus-ccr there are Lazarus packages and LCL components.

Setup

Requirements

  • FPC 3.3.1
  • If you want to install the design time package, the IDE must be compiled with fpc 3.3.1.

Download sk4d dynamic library

The sk4d dynamic library is provided by the skia4delphi project. Their installer is for Windows Delphi users. For Lazarus download the sources or clone the git:

  1. Go to https://github.com/skia4delphi/skia4delphi/releases
  2. Download the Source code (zip)
  3. Unpack the skia4delphi-6.1.0.zip
  4. You will find the sk4d library in Binary/Shared/YourPlatform

Your project must be able to find the library:

  • Linux: Set the LD_LIBRARY_PATH variable:
bash: export LD_LIBRARY_PATH=/home/user/skia4delphi-6.1.0/Binary/Shared/Linux64/
  • MacOS: Copy the skia4delphi-6.1.0/Binary/Shared/OSX64/libsk4d.dylib to the project folder or create a symlink
  • Windows: Copy the skia4delphi-6.1.0/Binary/Shared/Win64/sk4d.dll to the project folder

Download the Lazarus packages

Download lazarus-ccr via svn:

svn checkout https://svn.code.sf.net/p/lazarus-ccr/svn/ lazarus-ccr-svn

The skia files are in the directory lazarus-ccr-svn/components/skia .

There are three Lazarus packages:

  • design/Skia.LCL.Design.lpk - the IDE addon
  • src/Skia.LCL.lpk - contains the LCL controls like TSkPaintBox and TSkCustomWinControl
  • skia4d_package/Skia.lpk - the units from skia4delphi

Install Skia.LCL.Design

This is optional.

Requirements:

  • The IDE is compiled with fpc 3.3.1
  • the sk4d library can be found by the Lazarus executable, otherwise the IDE will not start.

Install the package Skia.LCL.Design.lpk in the IDE:

  • Open the design/Skia.LCL.Design.lpk via IDE menu Package / Open Package File (lpk)
  • Then click the button Use and Install
  • Restart the IDE

You will find a new page Skia in the component palette.

TSkPaintBox

This is a TGraphicControl - a lcl control without its own handle. Useful if you just want to draw something with skia. Set the OnDraw event

procedure TForm1.FormCreate(Sender: TObject);
begin
  MySkPaintBox:=TSkPaintBox.Create(Self);
  with MySkPaintBox do begin
    Name:='MySkPaintBox';
    OnDraw:=@MySkPaintBoxDraw;
    SetBounds(10,10,50,50);
    Parent:=Self;
  end;
end;

procedure TForm1.MySkPaintBoxDraw(Sender: TObject; const aCanvas: ISkCanvas; const aDest: TRectF; const aOpacity: Single);
begin
  aCanvas.Clear(TAlphaColors.Red);
end;

Note: the Opacity is not yet supported.

TSkCustomWinControl

This is TCustomControl - a lcl base class for custom controls with own handle and scrollbars. Override the DrawContent and Invalidate methods:

type
  TMySkiaControl = class(TSkCustomWinControl)
  public
    procedure DrawContent(const ACanvas: ISkCanvas; const {%H-}ADest: TRectF;
      const AOpacity: Single); override;
    procedure Invalidate; override;
  end;
...

procedure TMySkiaControl.DrawContent(const ACanvas: ISkCanvas; const ADest: TRectF; const AOpacity: Single);
begin
  // your drawing code
  aCanvas.Clear(TAlphaColors.White);
end;

procedure TMySkiaControl.Invalidate;
begin
  inherited Invalidate;
  Render.Redraw; // force a redraw
end;

Note: the Opacity is not yet supported.