Skip to content

Commit 69b6725

Browse files
[FMX Render] Fix AV using Skia Metal and Vulkan as render
#413 Co-Authored-By: Paulo César Botelho Barbosa <16469061+paulocesarbot@users.noreply.github.com>
1 parent cf4b276 commit 69b6725

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

Source/FMX/FMX.Skia.Canvas.pas

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,21 @@ TSkCanvasCustomClass = class of TSkCanvasCustom;
162162

163163
TSkBitmapHandle = class
164164
private
165-
FPixels: Pointer;
165+
function GetPixels: Pointer; overload;
166+
function GetPixels(AInitializeToZero: Boolean): Pointer; overload; inline;
166167
strict private
167168
FHeight: Integer;
168169
FPixelFormat: TPixelFormat;
170+
FPixels: Pointer;
171+
FPixelsBytes: NativeUInt;
169172
FWidth: Integer;
170173
public
171174
constructor Create(const AWidth, AHeight: Integer; const APixelFormat: TPixelFormat);
172175
destructor Destroy; override;
173176
property Height: Integer read FHeight;
174177
property PixelFormat: TPixelFormat read FPixelFormat;
175-
property Pixels: Pointer read FPixels;
178+
property Pixels: Pointer read GetPixels;
179+
property PixelsBytes: NativeUInt read FPixelsBytes;
176180
property Width: Integer read FWidth;
177181
end;
178182

@@ -2460,6 +2464,8 @@ constructor TSkBitmapHandle.Create(const AWidth, AHeight: Integer;
24602464
FWidth := AWidth;
24612465
FHeight := AHeight;
24622466
FPixelFormat := APixelFormat;
2467+
if (AWidth > 0) and (AHeight > 0) and (PixelFormatBytes[APixelFormat] > 0) then
2468+
FPixelsBytes := NativeUInt(AWidth) * NativeUInt(AHeight) * NativeUInt(PixelFormatBytes[APixelFormat]);
24632469
end;
24642470

24652471
destructor TSkBitmapHandle.Destroy;
@@ -2468,6 +2474,23 @@ destructor TSkBitmapHandle.Destroy;
24682474
inherited;
24692475
end;
24702476

2477+
function TSkBitmapHandle.GetPixels(AInitializeToZero: Boolean): Pointer;
2478+
begin
2479+
if (FPixels = nil) and (FPixelsBytes > 0) then
2480+
begin
2481+
if AInitializeToZero then
2482+
FPixels := AllocMem(FPixelsBytes)
2483+
else
2484+
GetMem(FPixels, FPixelsBytes);
2485+
end;
2486+
Result := FPixels;
2487+
end;
2488+
2489+
function TSkBitmapHandle.GetPixels: Pointer;
2490+
begin
2491+
Result := GetPixels(True);
2492+
end;
2493+
24712494
{ TSkCanvasBase }
24722495

24732496
function TSkCanvasBase.BeginCanvas(const AContextHandle: THandle): ISkCanvas;
@@ -2484,8 +2507,6 @@ function TSkCanvasBase.BeginCanvas(const AContextHandle: THandle): ISkCanvas;
24842507
else if Bitmap <> nil then
24852508
begin
24862509
LBitmap := TSkBitmapHandle(Bitmap.Handle);
2487-
if LBitmap.Pixels = nil then
2488-
LBitmap.FPixels := AllocMem(NativeInt(LBitmap.Width) * LBitmap.Height * PixelFormatBytes[LBitmap.PixelFormat]);
24892510
FBitmapSurface := TSkSurface.MakeRasterDirect(TSkImageInfo.Create(LBitmap.Width, LBitmap.Height, SkFmxColorType[LBitmap.PixelFormat]), LBitmap.Pixels, LBitmap.Width * PixelFormatBytes[LBitmap.PixelFormat]);
24902511
Result := FBitmapSurface.Canvas;
24912512
end
@@ -2550,14 +2571,7 @@ class function TSkCanvasBase.DoMapBitmap(const ABitmapHandle: THandle;
25502571
LBitmap: TSkBitmapHandle;
25512572
begin
25522573
LBitmap := TSkBitmapHandle(ABitmapHandle);
2553-
if LBitmap.Pixels = nil then
2554-
begin
2555-
if AAccess = TMapAccess.Write then
2556-
GetMem(LBitmap.FPixels, LBitmap.Width * LBitmap.Height * PixelFormatBytes[LBitmap.PixelFormat])
2557-
else
2558-
LBitmap.FPixels := AllocMem(LBitmap.Width * LBitmap.Height * PixelFormatBytes[LBitmap.PixelFormat]);
2559-
end;
2560-
ABitmapData.Data := LBitmap.Pixels;
2574+
ABitmapData.Data := LBitmap.GetPixels(AAccess <> TMapAccess.Write);
25612575
ABitmapData.Pitch := LBitmap.Width * PixelFormatBytes[LBitmap.PixelFormat];
25622576
Result := True;
25632577
end;
@@ -2652,13 +2666,15 @@ function TGrSharedContext.GetGrDirectContext: TGrDirectContext;
26522666
Result := TGrDirectContext(FGrDirectContext);
26532667
end;
26542668

2655-
procedure TGrSharedContext.InitializeTextureCache(
2656-
const ABitmap: TGrBitmapHandle);
2669+
procedure TGrSharedContext.InitializeTextureCache(const ABitmap: TGrBitmapHandle);
26572670
var
26582671
LImage: ISkImage;
26592672
begin
2660-
LImage := TSkImage.MakeFromRaster(TSkImageInfo.Create(ABitmap.Width, ABitmap.Height, SkFmxColorType[ABitmap.PixelFormat]), ABitmap.Pixels, ABitmap.Width * PixelFormatBytes[ABitmap.PixelFormat]);
2661-
ABitmap.Cache := LImage.MakeTextureImage(ABitmap.SharedContext.GrDirectContext);
2673+
LImage := TSkImage.MakeFromRaster(
2674+
TSkImageInfo.Create(ABitmap.Width, ABitmap.Height, SkFmxColorType[ABitmap.PixelFormat]),
2675+
ABitmap.Pixels, ABitmap.Width * PixelFormatBytes[ABitmap.PixelFormat]);
2676+
if LImage <> nil then
2677+
ABitmap.Cache := LImage.MakeTextureImage(ABitmap.SharedContext.GrDirectContext);
26622678
end;
26632679

26642680
procedure TGrSharedContext.RefreshContext;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy