The latest RAD Studio / C++ Builder 12 comes with fully integrated Skia graphics library support in C++ and throughout the IDE. Skia is an open-source graphic library that provides support for a wide variety of hardware and software platforms. Skia is sponsored and managed by Google but is available for use by anyone under the BSD Free Software License. Skia provides smooth 2D graphics that uses GPU efficiently. In this post we explain how to use Skia in C++ Builder.
Table of Contents
What is Skia in modern C++?
Skia is an open-source graphic library that provides support variety of hardware and software platforms. Skia is sponsored and managed by Google but is available for use by anyone under the BSD Free Software License. Mainly designed for Google Chrome and ChromeOS, Android, Flutter, Mozilla Firefox and Firefox OS, and many other products. The core components are done by the Skia development team, but they consider contributions from any source.
How can we use Skia in C++ Builder 12 project code?
First, you need C++ Builder 12.1, or you need Skia4Delphi component installed on C++ Builder 11.x. Skia is a new 2D render feature of RAD Studio / C++ Builder 12.1. It comes with Skia.Package.FMX, Skia.Package.RTL, Skia.Package.VCL. package libraries officially.
If you want to use Skia in your C++ Builder applications, you need to download the latest RAD Studio / C++ Builder. Then create a new C++ Builder FMX application or VCL application or open your project and you just need to enable Skia by right-clicking on your project in the Project window in C++ Builder. You don’t need to install any 3rd party components, after the RAD Studio 12.1 version, it is completely included in the built-in libraries.
In addition to Enabling Skia as above , it is recommended to add FMX.Skia.hpp
header to your Project.cpp
as shown below.
1 2 3 |
#include <FMX.Skia.hpp> |
It is recommended to set true GlobalUseSkia
, GlobalUseVulkan
and set false GlobalUseSkiaRasterWhenAvailable
flags inyour Project.cpp
like so:
1 2 3 4 5 |
GlobalUseSkia = true; GlobalUseVulkan = true; GlobalUseSkiaRasterWhenAvailable = false; |
Is there a simple example to Skia in C++ Builder 12?
Now, we can back to our Form to use Skia graphics. Let’s use its amazing Shader feature to create a gradient background. To do this follow these steps.
1. Drag TSkPainBox component from the Pallette window
2. Select SkPainBox1, in Object Inspector on the left side, goto Events tab and double click to OnDraw() event,
this will create OnDraw() event procedure in your code which is declared in your header.
3. Modify inside as given example below,
1 2 3 4 5 6 |
auto LPaint = SkPaint(); // create a new Skia Paint // Draw a gradient by using Skia Shader LPaint->Shader = TSkShader:: MakeGradientSweep(ADest.CenterPoint(), { 0xFF00265D, 0xFFC7CAA5, 0xFF00265D, 0xFF2EBBC1, 0xFF00265D}); ACanvas->DrawPaint(LPaint); // draw created paint to ACanvas of this PaintBox |
more securely, for any graphical issues we can save canvas state and we can use try{ }
and __finally{ }
as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
ACanvas->Save(); // Save current canvas state of this PaintBox try { auto LPaint = SkPaint(); // create a new Skia Paint // Draw a gradient by using Skia Shader LPaint->Shader = TSkShader:: MakeGradientSweep(ADest.CenterPoint(), { 0xFF00265D, 0xFFC7CAA5, 0xFF00265D, 0xFF2EBBC1, 0xFF00265D}); ACanvas->DrawPaint(LPaint); // draw created paint to ACanvas of this PaintBox } __finally { ACanvas->Restore(); // Restore to saved state if we can not try to draw }; |
then run your application, here is the result:
Is there a full example of how to use Skia in C++ Builder 12?
Here is the full code of this simple Skia example that uses Shaders of GPU to draw a gradient. Very simple and amazing!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <fmx.h> #pragma hdrstop #include "Skia_Simple_Gradient_Shader_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::SkPaintBox1Draw(TObject *ASender, ISkCanvas * const ACanvas, const TRectF &ADest, const float AOpacity) { ACanvas->Save(); // Save current canvas state of this PaintBox try { auto LPaint = SkPaint(); // create a new Skia Paint // Draw a gradient by using Skia Shader LPaint->Shader = TSkShader:: MakeGradientSweep(ADest.CenterPoint(), { 0xFF00265D, 0xFFC7CAA5, 0xFF00265D, 0xFF2EBBC1, 0xFF00265D}); ACanvas->DrawPaint(LPaint); // draw created paint to ACanvas of this PaintBox } __finally { ACanvas->Restore(); // Restore to saved state if we can not try to draw }; } |
If you want to see more examples, please install the latest RAD Studio / C++ Builder12.1 full or trial version and check this sample in Samples folder:C:\Users\Public\Documents\Embarcadero\Studio\23.0\Samples\CPP\Multi-Device Samples\Skia4Delphi
project. It has many examples that runs different Skia graphical examples.
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition