Changeset 19


Ignore:
Timestamp:
Jan 21, 2018, 11:33:15 PM (7 years ago)
Author:
chronos
Message:
  • Added: Locking of video memory because it is accessed by main thread and also application threads.
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/ChronOS.lpi

    r14 r19  
    99      <ResourceType Value="res"/>
    1010      <UseXPManifest Value="True"/>
     11      <XPManifest>
     12        <DpiAware Value="True"/>
     13      </XPManifest>
    1114      <Icon Value="0"/>
    1215    </General>
     
    2427          <SearchPaths>
    2528            <IncludeFiles Value="$(ProjOutDir)"/>
    26             <OtherUnitFiles Value="Apps;Platform/Base"/>
    27             <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
     29            <OtherUnitFiles Value="Apps;Platform/Base;Forms"/>
     30            <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)-${BuildMode}"/>
    2831          </SearchPaths>
    2932          <Parsing>
     
    117120      <IncludeFiles Value="$(ProjOutDir)"/>
    118121      <OtherUnitFiles Value="Apps;Platform/Base;Forms"/>
    119       <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
     122      <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)-${BuildMode}"/>
    120123    </SearchPaths>
    121124    <Parsing>
  • trunk/Forms/UFormMain.pas

    r18 r19  
    200200  SizeY: Integer;
    201201begin
    202 {  var
    203     X, Y: Integer;
    204     PixelPtr: PInteger;
    205     PixelRowPtr: PInteger;
    206     RawImage: TRawImage;
    207     BytePerPixel: Integer;
    208     SourcePixelPtr: Integer;
    209   begin
    210     try
    211       Bitmap.BeginUpdate;
    212       RawImage := Bitmap.RawImage;
    213       PixelRowPtr := PInteger(RawImage.Data);
    214       BytePerPixel := RawImage.Description.BitsPerPixel div 8;
    215       for Y := 0 to Bitmap.Height - 1 do begin
    216         PixelPtr := PixelRowPtr;
    217         for X := 0 to Bitmap.Width - 1 do begin
    218           SourcePixelPtr := TMatter(Matter[Surface.ItemsXY[Trunc(X / Bitmap.Width * Surface.Count.X),
    219               Trunc(Y / Bitmap.Height * Surface.Count.Y)]]).Color;
    220           PixelPtr^ := SwapBRComponent(SourcePixelPtr);
    221           Inc(PByte(PixelPtr), BytePerPixel);
    222         end;
    223         Inc(PByte(PixelRowPtr), RawImage.Description.BytesPerLine);
    224       end;
    225     finally
    226       Bitmap.EndUpdate;
    227     end;
    228 }
    229202  try
    230203    TempBitmap.SetSize(VideoDevice.VideoMode.Size.X, VideoDevice.VideoMode.Size.Y);
     
    234207    DestBytePerPixel := RawImage.Description.BitsPerPixel div 8;
    235208    DestBytePerLine := RawImage.Description.BytesPerLine;
     209    VideoDevice.Lock.Acquire;
    236210    if Assigned(VideoDevice.VideoMemory) then begin
    237211      SourceBytePerPixel := VideoDevice.VideoMode.GetBytesPerPixel;
     
    252226      end;
    253227    end;
     228    VideoDevice.Lock.Release;
    254229  finally
    255230    TempBitmap.EndUpdate;
     
    266241  VideoMode.Size := TPoint.Create(Width, Height);
    267242  VideoDevice.VideoMode := VideoMode;
     243  VideoMode.Free;
    268244end;
    269245
  • trunk/Packages/Kernel/UDevice.pas

    r17 r19  
    66
    77uses
    8   Classes, SysUtils, Contnrs, UList, UGraphics;
     8  Classes, SysUtils, Contnrs, UList, UGraphics, syncobjs;
    99
    1010type
     
    3636    function GetVideoMemory: PByte; virtual;
    3737  public
     38    Lock: TCriticalSection;
    3839    constructor Create; override;
     40    destructor Destroy; override;
    3941    procedure GetSupportedModes(Modes: TObjectList); virtual;
    4042    procedure VideoMemoryChange; virtual;
     
    104106  ClassName := 'Video device';
    105107  FVideoMode := TVideoMode.Create;
     108  Lock := TCriticalSection.Create;
     109end;
     110
     111destructor TDeviceVideo.Destroy;
     112begin
     113  Lock.Free;
     114  FVideoMode.Free;
     115  inherited Destroy;
    106116end;
    107117
  • trunk/Packages/Kernel/UScreen.pas

    r17 r19  
    1414
    1515  TScreenCanvas = class(TCanvas)
     16  private
     17    procedure SetPixelInternal(P: TPoint; Color: TColor);
     18  public
    1619    Screen: TScreen;
    1720    procedure DrawText(Pos: TPoint; Text: string; Color: TColor); override;
     
    5760  I: Integer;
    5861begin
    59   if Abs(P2.X - P1.X) > Abs(P2.Y - P1.Y) then begin
    60     for I := 0 to Abs(P2.X - P1.X) - 1 do
    61       SetPixel(TPoint.Create(Trunc(P1.X + I * Sign(P2.X - P1.X)),
    62         Trunc(P1.Y + (P2.Y - P1.Y) / Abs(P2.X - P1.X) * I)), Color);
    63   end else begin
    64     for I := 0 to Abs(P2.Y - P1.Y) - 1 do
    65       SetPixel(TPoint.Create(Trunc(P1.X + (P2.X - P1.X) / Abs(P2.Y - P1.Y) * I),
    66         Trunc(P1.Y + I * Sign(P2.Y - P1.Y))), Color);
     62  TDeviceVideo(Screen.Device).Lock.Acquire;
     63  try
     64    if Abs(P2.X - P1.X) > Abs(P2.Y - P1.Y) then begin
     65      for I := 0 to Abs(P2.X - P1.X) - 1 do
     66        SetPixelInternal(TPoint.Create(Trunc(P1.X + I * Sign(P2.X - P1.X)),
     67          Trunc(P1.Y + (P2.Y - P1.Y) / Abs(P2.X - P1.X) * I)), Color);
     68    end else begin
     69      for I := 0 to Abs(P2.Y - P1.Y) - 1 do
     70        SetPixelInternal(TPoint.Create(Trunc(P1.X + (P2.X - P1.X) / Abs(P2.Y - P1.Y) * I),
     71          Trunc(P1.Y + I * Sign(P2.Y - P1.Y))), Color);
     72    end;
     73  finally
     74    TDeviceVideo(Screen.Device).Lock.Release;
    6775  end;
    6876  Screen.VideoMemoryUpdated;
     
    7381  X, Y: Integer;
    7482begin
    75   for Y := Rect.Top to Rect.Bottom do
    76   for X := Rect.Left to Rect.Right do
    77     SetPixel(TPoint.Create(X, Y), Color);
     83  TDeviceVideo(Screen.Device).Lock.Acquire;
     84  try
     85    for Y := Rect.Top to Rect.Bottom do
     86    for X := Rect.Left to Rect.Right do
     87      SetPixelInternal(TPoint.Create(X, Y), Color);
     88  finally
     89    TDeviceVideo(Screen.Device).Lock.Release;
     90  end;
    7891  Screen.VideoMemoryUpdated;
    7992end;
     
    8396  X, Y: Integer;
    8497begin
    85   for Y := Rect.Top to Rect.Bottom do begin
    86     SetPixel(TPoint.Create(Rect.Left, Y), Color);
    87     SetPixel(TPoint.Create(Rect.Right, Y), Color);
    88   end;
    89   for X := Rect.Left to Rect.Right do begin
    90     SetPixel(TPoint.Create(X, Rect.Top), Color);
    91     SetPixel(TPoint.Create(X, Rect.Bottom), Color);
     98  TDeviceVideo(Screen.Device).Lock.Acquire;
     99  try
     100    for Y := Rect.Top to Rect.Bottom do begin
     101      SetPixelInternal(TPoint.Create(Rect.Left, Y), Color);
     102      SetPixelInternal(TPoint.Create(Rect.Right, Y), Color);
     103    end;
     104    for X := Rect.Left to Rect.Right do begin
     105      SetPixelInternal(TPoint.Create(X, Rect.Top), Color);
     106      SetPixelInternal(TPoint.Create(X, Rect.Bottom), Color);
     107    end;
     108  finally
     109    TDeviceVideo(Screen.Device).Lock.Release;
    92110  end;
    93111  Screen.VideoMemoryUpdated;
     
    95113
    96114procedure TScreenCanvas.SetPixel(P: TPoint; Color: TColor);
     115begin
     116  TDeviceVideo(Screen.Device).Lock.Acquire;
     117  try
     118    if Assigned(Screen.VideoMemory) and
     119      TRectangle.Create(TPoint.Create(0, 0), Screen.Size).PointInside(P) then
     120      PInteger(Screen.VideoMemory + P.X * Screen.BytesPerPixel + P.Y * Screen.BytesPerLine)^ := Color;
     121  finally
     122    TDeviceVideo(Screen.Device).Lock.Release;
     123  end;
     124end;
     125
     126procedure TScreenCanvas.SetPixelInternal(P: TPoint; Color: TColor);
    97127begin
    98128  if Assigned(Screen.VideoMemory) and
  • trunk/Platform/Base/UPlatformBase.pas

    r18 r19  
    9393    (VideoMode.Size.Y <> Mode.Size.Y) or
    9494    (VideoMode.ColorFormat <> Mode.ColorFormat) then begin
    95       VideoMode.Size := Mode.Size;
    96       VideoMode.ColorFormat := Mode.ColorFormat;
    97       ReAllocMem(VideoMemory, VideoMode.GetBytesPerImage);
    98       FillDWord(VideoMemory^, VideoMode.GetBytesPerImage div 4, $ffffff);
     95      Lock.Acquire;
     96      try
     97        if Mode.GetBytesPerImage <> VideoMode.GetBytesPerImage then begin
     98          ReAllocMem(VideoMemory, Mode.GetBytesPerImage);
     99          FillDWord(VideoMemory^, Mode.GetBytesPerImage div 4, $ffffff);
     100        end;
     101        VideoMode.Size := Mode.Size;
     102        VideoMode.ColorFormat := Mode.ColorFormat;
     103      finally
     104        Lock.Release;
     105      end;
    99106      if Assigned(FOnModeChanged) then
    100107        FOnModeChanged(Self);
     
    151158destructor TDeviceVideoBase.Destroy;
    152159begin
    153   VideoMode.Free;
    154160  inherited Destroy;
    155161end;
Note: See TracChangeset for help on using the changeset viewer.