From f23a1b62cc7dde246eac33ca4509916518c862d4 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Tue, 22 Nov 2011 07:56:47 +0000 Subject: [PATCH] customdrawnws: Merges cocoa context and bitmap improvements from lcl-cocoa and advances a little bit the rendering git-svn-id: trunk@33686 - --- .../customdrawn/cocoagdiobjects.pas | 34 ++++++++++++++++--- lcl/interfaces/customdrawn/cocoaprivate.pas | 2 +- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lcl/interfaces/customdrawn/cocoagdiobjects.pas b/lcl/interfaces/customdrawn/cocoagdiobjects.pas index c314c91b8b..4d6852096b 100644 --- a/lcl/interfaces/customdrawn/cocoagdiobjects.pas +++ b/lcl/interfaces/customdrawn/cocoagdiobjects.pas @@ -8,8 +8,8 @@ interface {$modeswitch objectivec1} uses - MacOSAll, // for CGContextRef - LCLtype, + SysUtils, MacOSAll, // for CGContextRef + LCLtype, LCLProc, CocoaAll, CocoaUtils, Classes, Types; @@ -172,6 +172,7 @@ type procedure TextOut(X,Y: Integer; UTF8Chars: PChar; Count: Integer; CharsDelta: PInteger); function GetTextExtentPoint(AStr: PChar; ACount: Integer; var Size: TSize): Boolean; function GetTextMetrics(var TM: TTextMetric): Boolean; + procedure DrawBitmap(X,Y: Integer; ABitmap: TCocoaBitmap); procedure SetOrigin(X,Y: Integer); procedure GetOrigin(var X,Y: Integer); function CGContext: CGContextRef; virtual; @@ -239,7 +240,15 @@ type constructor TCocoaBitmap.Create(AWidth, AHeight, ADepth, ABitsPerPixel: Integer; AAlignment: TCocoaBitmapAlignment; AType: TCocoaBitmapType; AData: Pointer; ACopyData: Boolean); +var + HasAlpha: Boolean; + BitmapFormat: NSBitmapFormat; begin + {$ifdef VerboseBitmaps} + DebugLn(Format('[TCocoaBitmap.Create] AWidth=%d AHeight=%d ADepth=%d ABitsPerPixel=%d' + + ' AAlignment=%d AType=%d AData=? ACopyData=%d', + [AWidth, AHeight, ADepth, ABitsPerPixel, Integer(AAlignment), Integer(AType), Integer(ACopyData)])); + {$endif} SetInfo(AWidth, AHeight, ADepth, ABitsPerPixel, AAlignment, AType); // Copy the image data, if necessary @@ -258,6 +267,15 @@ begin FFreeData := False; end; + HasAlpha := AType in [cbtARGB, cbtRGBA, cbtBGRA]; + BitmapFormat := NSAlphaNonpremultipliedBitmapFormat; + if AType = cbtARGB then + BitmapFormat := BitmapFormat or NSAlphaFirstBitmapFormat; + + {$ifdef VerboseBitmaps} + DebugLn(Format('[TCocoaBitmap.Create] NSBitmapImageRep.alloc HasAlpha=%d', + [Integer(HasAlpha)])); + {$endif} // Create the associated NSImageRep imagerep := NSBitmapImageRep(NSBitmapImageRep.alloc.initWithBitmapDataPlanes_pixelsWide_pixelsHigh__colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel( @FData, // planes, BitmapDataPlanes @@ -265,10 +283,10 @@ begin FHeight,// height, PixelsHigh FbitsPerSample,// bitsPerSample, bps FsamplesPerPixel, // samplesPerPixel, sps - False, // hasAlpha + HasAlpha, // hasAlpha False, // isPlanar NSCalibratedRGBColorSpace, // colorSpaceName - NSAlphaNonpremultipliedBitmapFormat, // bitmapFormat + BitmapFormat, // bitmapFormat FBytesPerRow, // bytesPerRow FBitsPerPixel //bitsPerPixel )); @@ -683,6 +701,14 @@ begin Result := True; end; +procedure TCocoaContext.DrawBitmap(X,Y:Integer; ABitmap: TCocoaBitmap); +begin + NSGraphicsContext.saveGraphicsState(); + NSGraphicsContext.setCurrentContext(ctx); + ABitmap.imagerep.drawAtPoint(NSMakePoint(X, Y)); + NSGraphicsContext.restoreGraphicsState(); +end; + procedure TCocoaContext.SetOrigin(X,Y:Integer); var cg : CGContextRef; diff --git a/lcl/interfaces/customdrawn/cocoaprivate.pas b/lcl/interfaces/customdrawn/cocoaprivate.pas index f7f0de705a..1845dda5a6 100644 --- a/lcl/interfaces/customdrawn/cocoaprivate.pas +++ b/lcl/interfaces/customdrawn/cocoaprivate.pas @@ -701,7 +701,7 @@ begin // Now render it into the control TCocoaCustomControl(Owner).Image.GetRawImage(lRawImage); Cocoa_RawImage_CreateBitmaps(lRawImage, lBitmap, lMask, True); - //Context. <- ToDo Render the HBITMAP + Context.DrawBitmap(0, 0, TCocoaBitmap(lBitmap)); end; end;