fCanvasD.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. unit fCanvasD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. System.Math,
  8. System.Types,
  9. Vcl.Graphics,
  10. Vcl.Controls,
  11. Vcl.Forms,
  12. Vcl.Dialogs,
  13. Vcl.ExtCtrls,
  14. Vcl.StdCtrls,
  15. GLS.Scene,
  16. GLS.SceneViewer,
  17. GLS.BitmapFont,
  18. GLS.WindowsFont,
  19. GLS.Coordinates,
  20. GLS.BaseClasses,
  21. GLS.Canvas,
  22. GLS.Texture,
  23. GLS.RenderContextInfo,
  24. GLScene.Utils;
  25. type
  26. TFormCanvas = class(TForm)
  27. BULines: TButton;
  28. BUEllipses: TButton;
  29. GLSceneViewer: TGLSceneViewer;
  30. PaintBox: TPaintBox;
  31. lbGLCanvas: TLabel;
  32. lbGDI: TLabel;
  33. Bevel1: TBevel;
  34. GLScene1: TGLScene;
  35. GLCamera1: TGLCamera;
  36. RBPenWidth1: TRadioButton;
  37. RBPenWidth2: TRadioButton;
  38. BUPoints: TButton;
  39. BURects: TButton;
  40. BUTextOut: TButton;
  41. WindowsBitmapFont: TGLWindowsBitmapFont;
  42. GLDirectOpenGL1: TGLDirectOpenGL;
  43. procedure BULinesClick(Sender: TObject);
  44. procedure BUEllipsesClick(Sender: TObject);
  45. procedure BUPointsClick(Sender: TObject);
  46. procedure BURectsClick(Sender: TObject);
  47. procedure BUTextOutClick(Sender: TObject);
  48. procedure GLDirectOpenGL1Render(Sender: TObject;
  49. var rci: TGLRenderContextInfo);
  50. procedure BUArcClick(Sender: TObject);
  51. private
  52. procedure PaintTheBox;
  53. procedure Bench;
  54. public
  55. end;
  56. var
  57. FormCanvas: TFormCanvas;
  58. implementation
  59. {$R *.dfm}
  60. type
  61. TWhat = (wLines, wEllipses, wRects, wPoints, wTextOut, wArcs);
  62. var
  63. vWhat: TWhat;
  64. vPenWidth: Integer;
  65. const
  66. cNbLines = 20000;
  67. cNbEllipses = 20000;
  68. cNbRects = 5000;
  69. cNbPoints = 200000;
  70. cNbTextOuts = 20000;
  71. cNbArcs = 20000;
  72. //-----------------------------------------------------------------
  73. procedure TFormCanvas.BULinesClick(Sender: TObject);
  74. begin
  75. vWhat := wLines;
  76. Bench;
  77. end;
  78. //-----------------------------------------------------------------
  79. procedure TFormCanvas.BUEllipsesClick(Sender: TObject);
  80. begin
  81. vWhat := wEllipses;
  82. Bench;
  83. end;
  84. //-----------------------------------------------------------------
  85. procedure TFormCanvas.BUArcClick(Sender: TObject);
  86. begin
  87. vWhat := wArcs;
  88. Bench;
  89. end;
  90. //-----------------------------------------------------------------
  91. procedure TFormCanvas.BURectsClick(Sender: TObject);
  92. begin
  93. vWhat := wRects;
  94. Bench;
  95. end;
  96. //-----------------------------------------------------------------
  97. procedure TFormCanvas.BUPointsClick(Sender: TObject);
  98. begin
  99. vWhat := wPoints;
  100. Bench;
  101. end;
  102. //-----------------------------------------------------------------
  103. procedure TFormCanvas.BUTextOutClick(Sender: TObject);
  104. begin
  105. vWhat := wTextOut;
  106. Bench;
  107. end;
  108. //-----------------------------------------------------------------
  109. procedure TFormCanvas.Bench;
  110. var
  111. t: Int64;
  112. begin
  113. if RBPenWidth1.Checked then
  114. vPenWidth := 1
  115. else
  116. vPenWidth := 2;
  117. Application.ProcessMessages;
  118. RandSeed := 0;
  119. t := StartPrecisionTimer;
  120. GLSceneViewer.Refresh;
  121. lbGLCanvas.Caption := Format('GLCanvas: %.2f msec',
  122. [StopPrecisionTimer(t) * 1000]);
  123. Application.ProcessMessages;
  124. RandSeed := 0;
  125. t := StartPrecisionTimer;
  126. PaintTheBox;
  127. lbGDI.Caption := Format('GDI: %.1f msec', [StopPrecisionTimer(t) * 1000]);
  128. end;
  129. //-----------------------------------------------------------------
  130. procedure TFormCanvas.GLDirectOpenGL1Render(Sender: TObject;
  131. var rci: TGLRenderContextInfo);
  132. var
  133. i, x, y: Integer;
  134. GLCanvas: TGLCanvas;
  135. r: TRect;
  136. Color: TColor;
  137. begin
  138. GLCanvas := TGLCanvas.Create(256, 256);
  139. GLCanvas.PenWidth := vPenWidth;
  140. case vWhat of
  141. wLines:
  142. begin
  143. for i := 1 to cNbLines do
  144. begin
  145. GLCanvas.PenColor := Random(256 * 256 * 256);
  146. GLCanvas.MoveTo(Random(256), Random(256)); // first point
  147. GLCanvas.LineTo(Random(256), Random(256)); // second point
  148. end;
  149. end;
  150. wEllipses:
  151. for i := 1 to cNbEllipses do
  152. begin
  153. GLCanvas.PenColor := Random(256 * 256 * 256);
  154. GLCanvas.EllipseBB(Random(256), Random(256), Random(256), Random(256));
  155. end;
  156. wRects:
  157. for i := 1 to cNbRects do
  158. begin
  159. GLCanvas.PenColor := Random(256 * 256 * 256);
  160. r := Rect(Random(256), Random(256), Random(256), Random(256));
  161. GLCanvas.FillRect(r.Left, r.Top, r.Right, r.Bottom);
  162. end;
  163. wPoints:
  164. begin
  165. for i := 1 to cNbPoints do
  166. begin
  167. GLCanvas.PenColor := Random(256 * 256 * 256);
  168. GLCanvas.PlotPixel(Random(256), Random(256));
  169. end;
  170. end;
  171. wTextOut:
  172. begin
  173. for i := 1 to cNbTextOuts do
  174. begin
  175. Color := Random(256 * 256 * 256);
  176. x := Random(256);
  177. y := Random(256);
  178. WindowsBitmapFont.TextOut(rci, x, y, 'Hello', Color);
  179. end;
  180. end;
  181. wArcs:
  182. begin
  183. for i := 1 to cNbEllipses do
  184. begin
  185. GLCanvas.PenColor := Random(256 * 256 * 256);
  186. GLCanvas.Arc(Random(256), Random(256), Random(256), Random(256),
  187. Random(256), Random(256), Random(256), Random(256))
  188. end;
  189. end;
  190. end;
  191. GLCanvas.Free;
  192. end;
  193. //-----------------------------------------------------------------
  194. procedure TFormCanvas.PaintTheBox;
  195. var
  196. i, x, y: Integer;
  197. r: TRect;
  198. b: TBitmap;
  199. begin
  200. // to be fair, use offscreen painting...
  201. b := TBitmap.Create;
  202. b.Width := 256;
  203. b.Height := 256;
  204. with b.Canvas do
  205. begin
  206. Brush.Style := bsClear;
  207. Pen.Width := vPenWidth;
  208. case vWhat of
  209. wLines:
  210. begin
  211. for i := 1 to cNbLines do
  212. begin
  213. Pen.color := Random(256 * 256 * 256);
  214. MoveTo(Random(256), Random(256));
  215. LineTo(Random(256), Random(256));
  216. end;
  217. end;
  218. wEllipses:
  219. begin
  220. for i := 1 to cNbEllipses do
  221. begin
  222. Pen.color := Random(256 * 256 * 256);
  223. Ellipse(Random(256), Random(256), Random(256), Random(256));
  224. end;
  225. end;
  226. wRects:
  227. begin
  228. Brush.Style := bsSolid;
  229. for i := 1 to cNbRects do
  230. begin
  231. Brush.color := Random(256 * 256 * 256);
  232. r := Rect(Random(256), Random(256), Random(256), Random(256));
  233. FillRect(r);
  234. end;
  235. end;
  236. wPoints:
  237. begin
  238. for i := 1 to cNbPoints do
  239. begin
  240. Pixels[Random(256), Random(256)] := Random(256 * 256 * 256);
  241. end;
  242. end;
  243. wTextOut:
  244. begin
  245. Font := WindowsBitmapFont.Font;
  246. for i := 1 to cNbTextOuts do
  247. begin
  248. Font.color := Random(256 * 256 * 256);
  249. x := Random(256);
  250. y := Random(256);
  251. TextOut(x, y, 'Hello');
  252. end
  253. end;
  254. wArcs:
  255. begin
  256. for i := 1 to cNbEllipses do
  257. begin
  258. Pen.color := Random(256 * 256 * 256);
  259. Arc(Random(256), Random(256), Random(256), Random(256), Random(256),
  260. Random(256), Random(256), Random(256))
  261. end;
  262. end;
  263. end;
  264. end;
  265. PaintBox.Canvas.Draw(0, 0, b);
  266. b.Free;
  267. end;
  268. end.