fCanvas.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. unit fCanvas;
  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. GLS.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. procedure TFormCanvas.BULinesClick(Sender: TObject);
  73. begin
  74. vWhat := wLines;
  75. Bench;
  76. end;
  77. procedure TFormCanvas.BUArcClick(Sender: TObject);
  78. begin
  79. vWhat := wArcs;
  80. Bench;
  81. end;
  82. procedure TFormCanvas.BUEllipsesClick(Sender: TObject);
  83. begin
  84. vWhat := wEllipses;
  85. Bench;
  86. end;
  87. procedure TFormCanvas.BURectsClick(Sender: TObject);
  88. begin
  89. vWhat := wRects;
  90. Bench;
  91. end;
  92. procedure TFormCanvas.BUPointsClick(Sender: TObject);
  93. begin
  94. vWhat := wPoints;
  95. Bench;
  96. end;
  97. procedure TFormCanvas.BUTextOutClick(Sender: TObject);
  98. begin
  99. vWhat := wTextOut;
  100. Bench;
  101. end;
  102. procedure TFormCanvas.Bench;
  103. var
  104. t: Int64;
  105. begin
  106. if RBPenWidth1.Checked then
  107. vPenWidth := 1
  108. else
  109. vPenWidth := 2;
  110. Application.ProcessMessages;
  111. RandSeed := 0;
  112. t := StartPrecisionTimer;
  113. GLSceneViewer.Refresh;
  114. lbGLCanvas.Caption := Format('GLCanvas: %.2f msec',
  115. [StopPrecisionTimer(t) * 1000]);
  116. Application.ProcessMessages;
  117. RandSeed := 0;
  118. t := StartPrecisionTimer;
  119. PaintTheBox;
  120. lbGDI.Caption := Format('GDI: %.1f msec', [StopPrecisionTimer(t) * 1000]);
  121. end;
  122. procedure TFormCanvas.GLDirectOpenGL1Render(Sender: TObject;
  123. var rci: TGLRenderContextInfo);
  124. var
  125. i, x, y: Integer;
  126. GLCanvas: TGLCanvas;
  127. r: TRect;
  128. Color: TColor;
  129. begin
  130. GLCanvas := TGLCanvas.Create(256, 256);
  131. GLCanvas.PenWidth := vPenWidth;
  132. case vWhat of
  133. wLines:
  134. begin
  135. for i := 1 to cNbLines do
  136. begin
  137. GLCanvas.PenColor := Random(256 * 256 * 256);
  138. GLCanvas.MoveTo(Random(256), Random(256)); // first point
  139. GLCanvas.LineTo(Random(256), Random(256)); // second point
  140. end;
  141. end;
  142. wEllipses:
  143. for i := 1 to cNbEllipses do
  144. begin
  145. GLCanvas.PenColor := Random(256 * 256 * 256);
  146. GLCanvas.EllipseBB(Random(256), Random(256), Random(256), Random(256));
  147. end;
  148. wRects:
  149. for i := 1 to cNbRects do
  150. begin
  151. GLCanvas.PenColor := Random(256 * 256 * 256);
  152. r := Rect(Random(256), Random(256), Random(256), Random(256));
  153. GLCanvas.FillRect(r.Left, r.Top, r.Right, r.Bottom);
  154. end;
  155. wPoints:
  156. begin
  157. for i := 1 to cNbPoints do
  158. begin
  159. GLCanvas.PenColor := Random(256 * 256 * 256);
  160. GLCanvas.PlotPixel(Random(256), Random(256));
  161. end;
  162. end;
  163. wTextOut:
  164. begin
  165. for i := 1 to cNbTextOuts do
  166. begin
  167. Color := Random(256 * 256 * 256);
  168. x := Random(256);
  169. y := Random(256);
  170. WindowsBitmapFont.TextOut(rci, x, y, 'Hello', Color);
  171. end;
  172. end;
  173. wArcs:
  174. begin
  175. for i := 1 to cNbEllipses do
  176. begin
  177. GLCanvas.PenColor := Random(256 * 256 * 256);
  178. GLCanvas.Arc(Random(256), Random(256), Random(256), Random(256),
  179. Random(256), Random(256), Random(256), Random(256))
  180. end;
  181. end;
  182. end;
  183. GLCanvas.Free;
  184. end;
  185. procedure TFormCanvas.PaintTheBox;
  186. var
  187. i, x, y: Integer;
  188. r: TRect;
  189. b: TBitmap;
  190. begin
  191. // to be fair, use offscreen painting...
  192. b := TBitmap.Create;
  193. b.Width := 256;
  194. b.Height := 256;
  195. with b.Canvas do
  196. begin
  197. Brush.Style := bsClear;
  198. Pen.Width := vPenWidth;
  199. case vWhat of
  200. wLines:
  201. begin
  202. for i := 1 to cNbLines do
  203. begin
  204. Pen.color := Random(256 * 256 * 256);
  205. MoveTo(Random(256), Random(256));
  206. LineTo(Random(256), Random(256));
  207. end;
  208. end;
  209. wEllipses:
  210. begin
  211. for i := 1 to cNbEllipses do
  212. begin
  213. Pen.color := Random(256 * 256 * 256);
  214. Ellipse(Random(256), Random(256), Random(256), Random(256));
  215. end;
  216. end;
  217. wRects:
  218. begin
  219. Brush.Style := bsSolid;
  220. for i := 1 to cNbRects do
  221. begin
  222. Brush.color := Random(256 * 256 * 256);
  223. r := Rect(Random(256), Random(256), Random(256), Random(256));
  224. FillRect(r);
  225. end;
  226. end;
  227. wPoints:
  228. begin
  229. for i := 1 to cNbPoints do
  230. begin
  231. Pixels[Random(256), Random(256)] := Random(256 * 256 * 256);
  232. end;
  233. end;
  234. wTextOut:
  235. begin
  236. Font := WindowsBitmapFont.Font;
  237. for i := 1 to cNbTextOuts do
  238. begin
  239. Font.color := Random(256 * 256 * 256);
  240. x := Random(256);
  241. y := Random(256);
  242. TextOut(x, y, 'Hello');
  243. end
  244. end;
  245. wArcs:
  246. begin
  247. for i := 1 to cNbEllipses do
  248. begin
  249. Pen.color := Random(256 * 256 * 256);
  250. Arc(Random(256), Random(256), Random(256), Random(256), Random(256),
  251. Random(256), Random(256), Random(256))
  252. end;
  253. end;
  254. end;
  255. end;
  256. PaintBox.Canvas.Draw(0, 0, b);
  257. b.Free;
  258. end;
  259. end.