transform: Coalesce similar transforms

If somebody does a transform like
  scale(5) scale(10) translate(1,1) translate(5,0)
store it instead as
  scale(50) translate(6,1)
This way, less memory is consumed and transforms are easier to read.

In particular, this simplifies the typical transforms we do in GTK,
which are just one translation after another.
This commit is contained in:
Benjamin Otte 2019-06-02 14:16:45 +02:00
parent 69629ebb1a
commit 5a0c8805fc
2 changed files with 128 additions and 81 deletions

View File

@ -94,6 +94,13 @@ G_DEFINE_BOXED_TYPE (GskTransform, gsk_transform,
static gboolean
gsk_transform_is_identity (GskTransform *self);
static inline gboolean
gsk_transform_has_class (GskTransform *self,
const GskTransformClass *transform_class)
{
return self != NULL && self->transform_class == transform_class;
}
/*< private >
* gsk_transform_alloc:
* @transform_class: class structure for this self
@ -638,6 +645,17 @@ gsk_transform_translate_3d (GskTransform *next,
{
GskTranslateTransform *result;
if (gsk_transform_has_class (next, &GSK_TRANSLATE_TRANSFORM_CLASS))
{
GskTranslateTransform *t = (GskTranslateTransform *) next;
GskTransform *r = gsk_transform_translate_3d (gsk_transform_ref (next->next),
&GRAPHENE_POINT3D_INIT(t->point.x + point->x,
t->point.y + point->y,
t->point.z + point->z));
gsk_transform_unref (next);
return r;
}
result = gsk_transform_alloc (&GSK_TRANSLATE_TRANSFORM_CLASS,
point->z == 0.0 ? GSK_TRANSFORM_CATEGORY_2D_TRANSLATE
: GSK_TRANSFORM_CATEGORY_3D,
@ -777,9 +795,19 @@ GskTransform *
gsk_transform_rotate (GskTransform *next,
float angle)
{
GskRotateTransform *result = gsk_transform_alloc (&GSK_ROTATE_TRANSFORM_CLASS,
GSK_TRANSFORM_CATEGORY_2D,
next);
GskRotateTransform *result;
if (gsk_transform_has_class (next, &GSK_ROTATE_TRANSFORM_CLASS))
{
GskTransform *r = gsk_transform_rotate (gsk_transform_ref (next->next),
((GskRotateTransform *) next)->angle + angle);
gsk_transform_unref (next);
return r;
}
result = gsk_transform_alloc (&GSK_ROTATE_TRANSFORM_CLASS,
GSK_TRANSFORM_CATEGORY_2D,
next);
result->angle = angle;
@ -1083,6 +1111,17 @@ gsk_transform_scale_3d (GskTransform *next,
{
GskScaleTransform *result;
if (gsk_transform_has_class (next, &GSK_SCALE_TRANSFORM_CLASS))
{
GskScaleTransform *scale = (GskScaleTransform *) next;
GskTransform *r = gsk_transform_scale_3d (gsk_transform_ref (next->next),
scale->factor_x * factor_x,
scale->factor_y * factor_y,
scale->factor_z * factor_z);
gsk_transform_unref (next);
return r;
}
result = gsk_transform_alloc (&GSK_SCALE_TRANSFORM_CLASS,
factor_z != 1.0 ? GSK_TRANSFORM_CATEGORY_3D
: GSK_TRANSFORM_CATEGORY_2D_AFFINE,
@ -1200,6 +1239,14 @@ gsk_transform_perspective (GskTransform *next,
{
GskPerspectiveTransform *result;
if (gsk_transform_has_class (next, &GSK_PERSPECTIVE_TRANSFORM_CLASS))
{
GskTransform *r = gsk_transform_perspective (gsk_transform_ref (next->next),
((GskPerspectiveTransform *) next)->depth + depth);
gsk_transform_unref (next);
return r;
}
result = gsk_transform_alloc (&GSK_PERSPECTIVE_TRANSFORM_CLASS,
GSK_TRANSFORM_CATEGORY_ANY,
next);

View File

@ -104,7 +104,7 @@ transform {
transform: translate(28, 0);
}
}
transform: translate(133, 0) translate(17, 11);
transform: translate(150, 11);
}
transform {
child: container {
@ -129,7 +129,7 @@ transform {
transform: translate(28, 0);
}
}
transform: translate(266, 0) translate(17, 11);
transform: translate(283, 11);
}
}
transform: translate(526, 0);
@ -156,7 +156,7 @@ transform {
offset: 0.572549 0.584314 0.584314 0;
}
}
transform: translate(1367, 0) translate(6, 11);
transform: translate(1373, 11);
}
transform {
child: transform {
@ -168,12 +168,12 @@ transform {
matrix: matrix3d(-0.372549, 0.235294, -0.105882, 0, 0.388235, -0.109804, -0.584314, 0, 0.227451, -0.584314, -0.584314, 0, 0, 0, 0, 1);
offset: 0.572549 0.584314 0.584314 0;
}
transform: translate(7, 0) translate(10, 11);
transform: translate(17, 11);
}
transform: translate(1409, 0);
}
}
transform: translate(26, 23) translate(6, 0);
transform: translate(32, 23);
}
transform {
child: container {
@ -245,7 +245,7 @@ transform {
offset: 0.572549 0.584314 0.584314 0;
}
}
transform: translate(374, 0) translate(10, 5);
transform: translate(384, 5);
}
}
transform {
@ -298,7 +298,7 @@ transform {
offset: 0.831373 0.811765 0.792157 0;
}
}
transform: translate(374, 0) translate(10, 5);
transform: translate(384, 5);
}
}
transform: translate(0, 44);
@ -337,10 +337,10 @@ transform {
matrix: matrix3d(-0.454902, 0.152941, -0.188235, 0, 0.305882, -0.192157, -0.666667, 0, 0.145098, -0.666667, -0.666667, 0, 0, 0, 0, 1);
offset: 0.654902 0.666667 0.666667 0;
}
transform: translate(370, 0) translate(6, 0);
transform: translate(376, 0);
}
}
transform: translate(0, 88) translate(9, 1);
transform: translate(9, 89);
}
transform {
child: container {
@ -365,7 +365,7 @@ transform {
clip: 0 0 392 32;
}
}
transform: translate(0, 132) translate(9, 1);
transform: translate(9, 133);
}
transform {
child: container {
@ -417,7 +417,7 @@ transform {
offset: 0.572549 0.584314 0.584314 0;
}
}
transform: translate(374, 0) translate(6, 5);
transform: translate(380, 5);
}
}
transform: translate(0, 176);
@ -593,7 +593,7 @@ transform {
offset: 0.654902 0.666667 0.666667 0;
}
}
transform: translate(40, 0) translate(6, 0);
transform: translate(46, 0);
}
transform {
child: container {
@ -611,11 +611,11 @@ transform {
offset: 0.654902 0.666667 0.666667 0;
}
}
transform: translate(75, 0) translate(6, 0);
transform: translate(81, 0);
}
}
}
transform: translate(104, 0) translate(1, 1);
transform: translate(105, 1);
}
transform {
child: container {
@ -660,7 +660,7 @@ transform {
offset: 0.654902 0.666667 0.666667 0;
}
}
transform: translate(40, 0) translate(6, 0);
transform: translate(46, 0);
}
transform {
child: container {
@ -685,11 +685,11 @@ transform {
offset: 0.654902 0.666667 0.666667 0;
}
}
transform: translate(75, 0) translate(6, 0);
transform: translate(81, 0);
}
}
}
transform: translate(236, 0) translate(1, 1);
transform: translate(237, 1);
}
}
transform: translate(0, 265);
@ -756,7 +756,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(0, 29) translate(0, 2);
transform: translate(0, 31);
}
transform {
child: container {
@ -791,7 +791,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(0, 58) translate(0, 2);
transform: translate(0, 60);
}
transform {
child: container {
@ -826,7 +826,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(0, 87) translate(0, 2);
transform: translate(0, 89);
}
transform {
child: container {
@ -853,7 +853,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(0, 116) translate(0, 2);
transform: translate(0, 118);
}
transform {
child: container {
@ -888,7 +888,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(0, 145) translate(0, 2);
transform: translate(0, 147);
}
transform {
child: container {
@ -918,7 +918,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(125, 0) translate(0, 2);
transform: translate(125, 2);
}
transform {
child: container {
@ -948,7 +948,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(125, 29) translate(0, 2);
transform: translate(125, 31);
}
transform {
child: container {
@ -986,7 +986,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(125, 58) translate(0, 2);
transform: translate(125, 60);
}
transform {
child: container {
@ -1024,7 +1024,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(125, 87) translate(0, 2);
transform: translate(125, 89);
}
transform {
child: container {
@ -1054,7 +1054,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(125, 116) translate(0, 2);
transform: translate(125, 118);
}
transform {
child: container {
@ -1092,7 +1092,7 @@ transform {
transform: translate(24, 0);
}
}
transform: translate(125, 145) translate(0, 2);
transform: translate(125, 147);
}
transform {
child: color-matrix {
@ -1180,7 +1180,7 @@ transform {
offset: 2 17;
}
}
transform: translate(0, 44) translate(17, 5);
transform: translate(17, 49);
}
transform {
child: container {
@ -1202,7 +1202,7 @@ transform {
offset: 2 17;
}
}
transform: translate(0, 88) translate(17, 5);
transform: translate(17, 93);
}
transform {
child: container {
@ -1224,7 +1224,7 @@ transform {
offset: 2 17;
}
}
transform: translate(0, 132) translate(17, 5);
transform: translate(17, 137);
}
transform {
child: transform {
@ -1427,7 +1427,7 @@ transform {
color: rgb(53,132,228);
}
}
transform: translate(0, 396) translate(17, 5);
transform: translate(17, 401);
}
transform {
child: container {
@ -1456,7 +1456,7 @@ transform {
}
}
}
transform: translate(0, 440) translate(37, 1);
transform: translate(37, 441);
}
transform {
child: container {
@ -1485,7 +1485,7 @@ transform {
}
}
}
transform: translate(0, 476) translate(37, 1);
transform: translate(37, 477);
}
}
transform: translate(431, 0);
@ -1527,7 +1527,7 @@ transform {
outline: -1 -1 252 4 / 2 1.5 1.5 2;
}
}
transform: translate(0, -1) translate(0, 1);
transform: translate(0, 0);
}
}
transform: translate(1, 1);
@ -1560,7 +1560,7 @@ transform {
outline: -1 -1 252 4 / 1.5 2 2 1.5;
}
}
transform: translate(250, -1) translate(0, 1);
transform: translate(250, 0);
}
}
transform: translate(1, 1);
@ -1605,10 +1605,10 @@ transform {
outline: -1 -1 102 4 / 1.5;
}
}
transform: translate(43, -1) translate(0, 1);
transform: translate(43, 0);
}
}
transform: translate(0, 16) translate(1, 1);
transform: translate(1, 17);
}
}
transform: translate(0, 20);
@ -1699,28 +1699,28 @@ transform {
outline: -1 -1 97 3 / 1;
}
}
transform: translate(99, 0) translate(2, 1);
transform: translate(101, 1);
}
transform {
child: border {
colors: rgba(146,149,149,0.15);
outline: -1 -1 97 3 / 1;
}
transform: translate(198, 0) translate(2, 1);
transform: translate(200, 1);
}
transform {
child: border {
colors: rgba(146,149,149,0.15);
outline: -1 -1 97 3 / 1;
}
transform: translate(297, 0) translate(2, 1);
transform: translate(299, 1);
}
transform {
child: border {
colors: rgba(146,149,149,0.15);
outline: -1 -1 97 3 / 1;
}
transform: translate(396, 0) translate(2, 1);
transform: translate(398, 1);
}
}
transform: translate(3, 3);
@ -1771,10 +1771,10 @@ transform {
outline: -1 -1 20 20 / 10;
}
}
transform: translate(234, 0) translate(-8, -8);
transform: translate(226, -8);
}
}
transform: translate(0, 3) translate(1, 1);
transform: translate(1, 4);
}
transform: translate(12, 12);
}
@ -1806,12 +1806,12 @@ transform {
outline: -1 -1 20 20 / 10;
}
}
transform: translate(234, 0) translate(-8, -8);
transform: translate(226, -8);
}
}
transform: translate(0, 3) translate(1, 1);
transform: translate(1, 4);
}
transform: translate(0, 34) translate(12, 12);
transform: translate(12, 46);
}
}
transform: translate(0, 76);
@ -1856,7 +1856,7 @@ transform {
transform: translate(476, 0);
}
}
transform: translate(0, 4) translate(0, 6);
transform: translate(0, 10);
}
transform {
child: container {
@ -1876,13 +1876,13 @@ transform {
bounds: 1 1 20 25;
texture: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAYAAAAxFw7TAAAABmJLR0QA/wD/AP+gvaeTAAAC6ElEQVQ4jaXVy2sTQRwH8O/sI81mUrfd3ZYYUBGiZJu2eLCiiDQSUEFUTPWkR1MQjXgwei0UFGkRBf8EH4i09GTQdOOjqJv0oKKllcYoPTSXShrjbkqTGA+akEZTa/M9LfP48JuZ3R2Cv4f3+nx9Bw8cOOV2u3dRSlsB4LtppqenpmKR8fG7zzRtFEC+diKpbfB6vScuXLx4WxRF20ans5kKAjieBwDk83mYpolUKpVdzGSMm7dunZ94+nSkej5b9cwEg8EbZ/r7B1W3u9XpdDYJVitYlgUhBIQQcCwLwWpFW1tbkyzL9s7OzsOiKLZNxuMRAKUV4Llg8MZxvz+gqiptsljq7ETVnvA8FEWxtLe3dzVv2KBMxuOPK+C+/fv7AoHAoKqqlGWYf2KVJRECSZIskix3fUkmp+fm5mYIAH5kbGxuR3e3w2azrRmrjmGaePP27fxJv38L4/X5/KIo0vViAEBtNrS2tDT39vYeZwP9/Vd39fR0C1brukEA4DmuaWl5mWNUj2d3I9VVqqQUno6O3Yyd0hae4xoGOY4DtdslBqUSAfnj/V5XCCFgDMNIF/J/fEH/nUKhAMMwFpmPMzOTZi7XMGiaJqY/fNCZiKbdT6VS2UbB+VQqq2naA0Z78uRhJpPJNVJlzjTxLZtdikajoyyA4teFhfkOj+eQoigW5j8PqFgsYmZ21hgeHj775fPndywAJJPJ9w6HY6uiKG5Zlvm1kiUAs4mE+Sgcvnf/zp1rQNXf5tXLl2FVVfcIguCUZdnyL/Q3lovp+vPBgYHTAH6sAAH8GI9EHrhcrh5qt2+WJYkndZZfrkx//frFlVDoGIBCuY+tHRuNRh+6XK6d9dAyFtf1icuh0FHUXAO1YAXdtn17D6V0UzVaApBIJHJxXZ8IXbp0pBarB/5CNe0XardvkiWJB4DEp0+5mK6/qIetBpbREY/Hs1ew2Tam0+liLBZbFVtr+OtDQ+HrQ0NhAHwj0LryE/8bFCfO1plnAAAAAElFTkSuQmCC");
}
transform: translate(237, 0) translate(-10, -10);
transform: translate(227, -10);
}
}
transform: translate(1, 1);
}
}
transform: translate(0, 150) translate(12, 12);
transform: translate(12, 162);
}
transform {
child: container {
@ -1915,7 +1915,7 @@ transform {
outline: -1 -1 4 182 / 2 2 1.5 1.5;
}
}
transform: translate(-1, 0) translate(1, 0);
transform: translate(0, 0);
}
}
transform: translate(1, 1);
@ -1950,7 +1950,7 @@ transform {
outline: -1 -1 4 182 / 1.5 1.5 2 2;
}
}
transform: translate(-1, 180) translate(1, 0);
transform: translate(0, 180);
}
}
transform: translate(1, 1);
@ -2008,10 +2008,10 @@ transform {
outline: -1 -1 20 20 / 10;
}
}
transform: translate(0, 151) translate(-8, -8);
transform: translate(-8, 143);
}
}
transform: translate(39, 28) translate(1, 1);
transform: translate(40, 29);
}
}
transform: translate(28, 12);
@ -2051,13 +2051,13 @@ transform {
outline: -1 -1 20 20 / 10;
}
}
transform: translate(0, 151) translate(-8, -8);
transform: translate(-8, 143);
}
}
transform: translate(39, 28) translate(1, 1);
transform: translate(40, 29);
}
}
transform: translate(129, 0) translate(12, 12);
transform: translate(141, 12);
}
}
transform: translate(251, 0);
@ -2105,7 +2105,7 @@ transform {
offset: 0 15;
}
}
transform: translate(0, 141) translate(1, 1);
transform: translate(1, 142);
}
transform {
child: container {
@ -2131,7 +2131,7 @@ transform {
offset: 0 15;
}
}
transform: translate(0, 282) translate(2, 2);
transform: translate(2, 284);
}
transform {
child: container {
@ -2157,7 +2157,7 @@ transform {
offset: 0 15;
}
}
transform: translate(0, 423) translate(2, 2);
transform: translate(2, 425);
}
}
transform: translate(1097, 0);
@ -2499,7 +2499,7 @@ transform {
offset: 0 17;
}
}
transform: translate(42, 0) translate(6, 0);
transform: translate(48, 0);
}
transform {
child: container {
@ -2519,7 +2519,7 @@ transform {
offset: 0 17;
}
}
transform: translate(82, 0) translate(6, 0);
transform: translate(88, 0);
}
transform {
child: container {
@ -2539,7 +2539,7 @@ transform {
offset: 0 17;
}
}
transform: translate(153, 0) translate(6, 0);
transform: translate(159, 0);
}
}
clip: 0 0 210 25;
@ -2571,7 +2571,7 @@ transform {
}
}
}
transform: translate(0, 295) translate(1, 1);
transform: translate(1, 296);
}
}
transform: translate(1232, 0);
@ -2613,7 +2613,7 @@ transform {
glyphs: "page 3";
offset: 0 20;
}
transform: translate(149, 0) translate(16, 3);
transform: translate(165, 3);
}
transform {
child: text {
@ -2622,7 +2622,7 @@ transform {
glyphs: "page 2";
offset: 0 20;
}
transform: translate(74, 0) translate(16, 3);
transform: translate(90, 3);
}
transform {
child: container {
@ -2689,7 +2689,7 @@ transform {
glyphs: "page 3";
offset: 0 20;
}
transform: translate(0, 88) translate(12, 7);
transform: translate(12, 95);
}
transform {
child: text {
@ -2698,7 +2698,7 @@ transform {
glyphs: "page 2";
offset: 0 20;
}
transform: translate(0, 44) translate(12, 7);
transform: translate(12, 51);
}
transform {
child: container {
@ -2721,11 +2721,11 @@ transform {
transform: translate(-2, 3);
}
}
transform: translate(285, 0) translate(2, 1);
transform: translate(287, 1);
}
}
}
transform: translate(364, 0) translate(1, 1);
transform: translate(365, 1);
}
transform {
child: container {
@ -2758,7 +2758,7 @@ transform {
glyphs: "page 3";
offset: 0 20;
}
transform: translate(149, 0) translate(16, 4);
transform: translate(165, 4);
}
transform {
child: text {
@ -2767,7 +2767,7 @@ transform {
glyphs: "page 2";
offset: 0 20;
}
transform: translate(74, 0) translate(16, 4);
transform: translate(90, 4);
}
transform {
child: container {
@ -2790,11 +2790,11 @@ transform {
transform: translate(3, -2);
}
}
transform: translate(0, 103) translate(1, 2);
transform: translate(1, 105);
}
}
}
transform: translate(728, 0) translate(1, 1);
transform: translate(729, 1);
}
transform {
child: container {
@ -2823,7 +2823,7 @@ transform {
glyphs: "page 3";
offset: 0 20;
}
transform: translate(0, 88) translate(12, 7);
transform: translate(12, 95);
}
transform {
child: text {
@ -2832,7 +2832,7 @@ transform {
glyphs: "page 2";
offset: 0 20;
}
transform: translate(0, 44) translate(12, 7);
transform: translate(12, 51);
}
transform {
child: container {
@ -2866,13 +2866,13 @@ transform {
}
}
}
transform: translate(1091, 0) translate(1, 1);
transform: translate(1092, 1);
}
}
transform: translate(0, 579);
}
}
transform: translate(26, 70) translate(10, 10);
transform: translate(36, 80);
}
}
transform: translate(0, 0);