Overriding OpenGL Rendering Parameters

From RAD Studio
Jump to: navigation, search

Go Up to FireMonkey Applications Guide


To increase the performance of graphical applications, you can override the OpenGL rendering parameters on iOS and Android platforms.

For example, if you do not use 3D models, you can disable depth buffer. Likewise, if you do not use filled paths or other vector canvas shapes, it is safe to disable stencil buffer. If you prefer high performance over visual quality, then you might want to use 16-bit color depth instead of 24-bit or 32-bit.

To override the OpenGL parameters follow the next steps:

Delphi:

  1. Add FMX.Platform.pas to the USES list in your unit.
  2. Write the rendering setup procedure:
    procedure RenderingSetupCallback(const Sender, Context: TObject; var ColorBits, DepthBits: Integer;
    var Stencil: Boolean; var Multisamples: Integer);
    begin
        // Override OpenGL rendering setup to use custom values.
        ColorBits := 16; // default is 24
        DepthBits := 0; // default is 24
        Stencil := False; // default is True
        Multisamples := 0; // default depends on TForm.Quality or TForm3D.Multisample
    end;
    
  3. Write the registration procedure:
    procedure RegisterRenderingSetup;
    var
        SetupService: IFMXRenderingSetupService;
    begin
        if TPlatformServices.Current.SupportsPlatformService(IFMXRenderingSetupService, IInterface(SetupService)) then
        SetupService.Subscribe(RenderingSetupCallback);
        // There is also SetupService.Unsubscribe, which removes the hook.
    end;
    
  4. Call the RegisterRenderingSetup in the initialization section of the unit.


C++:

  1. Write the rendering and registration procedure:
    class TCppRenderingSetupCallback : public TCppInterfacedObject<TRenderingSetupCallback>
    {
    public:
      void __fastcall Invoke(System::TObject* const Sender, System::TObject* const Context, int &ColorBits,
    						 int &DepthBits, bool &Stencil, int &Multisamples)
      {
    	// Override OpenGL rendering setup to use custom values.
    	ColorBits = 16; // default is 24
    	DepthBits = 0; // default is 24
    	Stencil = false; // default is true
    	Multisamples = 0; // default depends on TForm.Quality or TForm3D.Multisample
      }
    }; 
    
    void RegisterRenderingSetup()
    {
      _di_IFMXRenderingSetupService SetupService;
      if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXRenderingSetupService), &SetupService))
      {
    	SetupService->Subscribe(new TCppRenderingSetupCallback());
      }
    }
    
  2. The function RegisterRenderingSetup() should be invoked in FMXmain, before the call to Application->Initialize, like this:
    extern "C" int FMXmain()
    {
        RegisterRenderingSetup();
    	try
    	{
    		Application->Initialize();
    		Application->CreateForm(__classid(TForm8), &Form8);
    		Application->Run();
    

    See Also