annotate doodle/dia/grid_layer.d @ 90:885914257e0e

Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause of the grid slowdown and the overall memory leak. No need for backbuffers. Back to the wonderful abstraction I originally anticipated.
author David Bryant <bagnose@gmail.com>
date Thu, 19 Aug 2010 00:28:57 +0930
parents 467febed7367
children 42766e14534e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
1754cb773d41 Part-way through getting to compile with configure/builder.
Graham St Jack <graham.stjack@internode.on.net>
parents: 27
diff changeset
1 module doodle.dia.grid_layer;
16
9e63308b749c * Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents: 12
diff changeset
2
24
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
3 public {
28
1754cb773d41 Part-way through getting to compile with configure/builder.
Graham St Jack <graham.stjack@internode.on.net>
parents: 27
diff changeset
4 import doodle.dia.icanvas;
24
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
5 }
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
6
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
7 private {
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
8 import std.math;
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
9 import std.stdio;
24
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
10 }
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
11
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
12 private {
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
13 double start(in double value, in double spacing) {
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
14 real r = floor(value / spacing);
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
15 return r * spacing;
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
16 }
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
17 }
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
18
57
9960c4fbd0dd I is for Interface
"David Bryant <bagnose@gmail.com>"
parents: 48
diff changeset
19 class GridLayer : Layer, IGrid {
58
c63719604adb Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents: 57
diff changeset
20 immutable double MIN_SPACING = 5.0; // pixels
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
21
58
c63719604adb Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents: 57
diff changeset
22 this(in string name = "Grid") {
24
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
23 super(name);
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
24 _zoomValid = false;
24
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
25 }
a24c13bb9c98 Builds again.
"David Bryant <bagnose@gmail.com>"
parents: 22
diff changeset
26
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
27 // Layer overrides:
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
28
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
29 override Rectangle bounds() const {
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
30 // We don't require any geometry
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
31 return Rectangle();
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
32 }
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
33
84
cdd4fc728d94 Renamed Drawable to Renderer
daveb
parents: 81
diff changeset
34 override void draw(in Rectangle screenDamage, scope Renderer screenRenderer,
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
35 in Rectangle modelDamage, scope Renderer modelRenderer,
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
36 in ScreenModel screenModel) const {
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
37 assert(_zoomValid);
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
38
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
39 auto z = screenModel.zoom;
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
40 // Is _zoom like screenModel.zoom ?
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
41
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
42 modelRenderer.pushState; {
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
43 modelRenderer.setColor(Color(0.0, 0.0, 0.0, 0.3));
90
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
44 modelRenderer.setLineWidth(0.5);
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
45
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
46 auto x = start(modelDamage.corner0.x, _spacing);
36
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
47
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
48 for (;;) {
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
49 writefln("Doing something %s", x);
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
50 modelRenderer.drawVLine(x, modelDamage.corner0.y, modelDamage.corner1.y);
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
51 x += _spacing;
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
52 if (x > modelDamage.corner1.x) break;
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
53 }
90
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
54
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
55 auto y = start(modelDamage.corner0.y, _spacing);
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
56
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
57 for (;;) {
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
58 writefln("Doing something %s", y);
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
59 modelRenderer.drawHLine(y, modelDamage.corner0.x, modelDamage.corner1.x);
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
60 y += _spacing;
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
61 if (y > modelDamage.corner1.y) break;
885914257e0e Ok, the cairo resource leak (and cost of reqaquisition) in GtkD is the cause
David Bryant <bagnose@gmail.com>
parents: 89
diff changeset
62 }
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
63 } modelRenderer.popState;
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
64
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
65 /+
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
66 modelCr.save(); {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
67 modelCr.setSourceRgba(0.0, 0.0, 0.0, 0.3);
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
68 modelCr.setLineWidth(0.5);
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
69
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
70 {
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
71 // vertical grid lines
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 58
diff changeset
72 double x = start(modelDamage.corner0.x, _spacing);
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
73
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
74 for (;;) {
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 58
diff changeset
75 vline(modelCr, x, modelDamage.corner0.y, modelDamage.corner1.y);
36
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
76
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
77 // Ensure 1 pixel wide FIXME is this naughty? We are sneaking
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
78 // through cairo to mix model and pixel coordinates...
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
79 modelCr.save(); {
89
467febed7367 * Ignore excre
David Bryant <bagnose@gmail.com>
parents: 84
diff changeset
80 modelCr.scale(1.0 / z, 1.0 / z);
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
81 modelCr.stroke();
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
82 } modelCr.restore();
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
83
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 58
diff changeset
84 if (x > modelDamage.corner1.x) {
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
85 break;
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
86 }
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
87
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
88 x += _spacing;
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
89 }
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
90 }
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
91
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
92 {
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
93 // horizontal grid lines
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 58
diff changeset
94 double y = start(modelDamage.corner0.y, _spacing);
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
95
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
96 for (;;) {
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 58
diff changeset
97 hline(modelCr, y, modelDamage.corner0.x, modelDamage.corner1.x);
36
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
98
41
daveb
parents: 38
diff changeset
99 // FIXME?
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
100 modelCr.save(); {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
101 modelCr.scale(1.0 / xx, 1.0 / yy);
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
102 modelCr.stroke();
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
103 } modelCr.restore();
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
104
70
0e61702c6ea6 Checkpoint
"David Bryant <bagnose@gmail.com>"
parents: 58
diff changeset
105 if (y > modelDamage.corner1.y) {
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
106 break;
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
107 }
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
108
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
109 y += _spacing;
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
110 }
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
111 }
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
112 } modelCr.restore();
80
b759414d2b72 Switched from cairo to Drawable abstraction
"David Bryant <bagnose@gmail.com>"
parents: 74
diff changeset
113 +/
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
114 }
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
115
57
9960c4fbd0dd I is for Interface
"David Bryant <bagnose@gmail.com>"
parents: 48
diff changeset
116 // IGrid overrides:
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
117
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
118 override void zoomChanged(double zoom) {
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
119 _zoom = zoom;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
120 _zoomValid = true;
36
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
121
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
122 // FIXME compute spacing properly
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
123 _spacing = 20.0 / _zoom; // mm
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
124 }
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
125
36
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
126 // FIXME use inout parameter?
188397ef9a12 Late night tinkering
David Bryant <bagnose@gmail.com>
parents: 35
diff changeset
127 override bool snap(in Point a, out Point b) const {
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
128 b = a;
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
129 return false;
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
130 }
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
131
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
132 private {
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
133 bool _zoomValid;
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
134 double _zoom; // pixels per millimetre
35
3f6bb0bb22dc Beginnings of grid
David Bryant <bagnose@gmail.com>
parents: 34
diff changeset
135
48
1b4c9ba58673 Stylistic overhaul.
daveb
parents: 41
diff changeset
136 double _spacing; // model spacing
11
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
137 }
fb571a3b1f0d Checkpoint
"David Bryant <bagnose@gmail.com>"
parents:
diff changeset
138 }