1 /****************************************************************************
2 *
3 *    The MIT License (MIT)
4 *
5 *    Copyright 2012 - 2020 Vivante Corporation, Santa Clara, California.
6 *    All Rights Reserved.
7 *
8 *    Permission is hereby granted, free of charge, to any person obtaining
9 *    a copy of this software and associated documentation files (the
10 *    'Software'), to deal in the Software without restriction, including
11 *    without limitation the rights to use, copy, modify, merge, publish,
12 *    distribute, sub license, and/or sell copies of the Software, and to
13 *    permit persons to whom the Software is furnished to do so, subject
14 *    to the following conditions:
15 *
16 *    The above copyright notice and this permission notice (including the
17 *    next paragraph) shall be included in all copies or substantial
18 *    portions of the Software.
19 *
20 *    THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
21 *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 *    IN NO EVENT SHALL VIVANTE AND/OR ITS SUPPLIERS BE LIABLE FOR ANY
24 *    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 *    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 *    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 *****************************************************************************/
29 
30 #include <math.h>
31 #include <string.h>
32 #include "vg_lite.h"
33 
34 
vg_lite_identity(vg_lite_matrix_t * matrix)35 void vg_lite_identity(vg_lite_matrix_t * matrix)
36 {
37     /* Set identify matrix. */
38     matrix->m[0][0] = 1.0f;
39     matrix->m[0][1] = 0.0f;
40     matrix->m[0][2] = 0.0f;
41     matrix->m[1][0] = 0.0f;
42     matrix->m[1][1] = 1.0f;
43     matrix->m[1][2] = 0.0f;
44     matrix->m[2][0] = 0.0f;
45     matrix->m[2][1] = 0.0f;
46     matrix->m[2][2] = 1.0f;
47 }
48 
multiply(vg_lite_matrix_t * matrix,vg_lite_matrix_t * mult)49 static void multiply(vg_lite_matrix_t * matrix, vg_lite_matrix_t * mult)
50 {
51     vg_lite_matrix_t temp;
52     int row, column;
53 
54     /* Process all rows. */
55     for (row = 0; row < 3; row++) {
56         /* Process all columns. */
57         for (column = 0; column < 3; column++) {
58             /* Compute matrix entry. */
59             temp.m[row][column] =  (matrix->m[row][0] * mult->m[0][column])
60             + (matrix->m[row][1] * mult->m[1][column])
61             + (matrix->m[row][2] * mult->m[2][column]);
62         }
63     }
64 
65     /* Copy temporary matrix into result. */
66     memcpy(matrix, &temp, sizeof(temp));
67 }
68 
vg_lite_translate(vg_lite_float_t x,vg_lite_float_t y,vg_lite_matrix_t * matrix)69 void vg_lite_translate(vg_lite_float_t x, vg_lite_float_t y, vg_lite_matrix_t * matrix)
70 {
71     /* Set translation matrix. */
72     vg_lite_matrix_t t = { { {1.0f, 0.0f, x},
73         {0.0f, 1.0f, y},
74         {0.0f, 0.0f, 1.0f}
75     } };
76 
77     /* Multiply with current matrix. */
78     multiply(matrix, &t);
79 }
80 
vg_lite_scale(vg_lite_float_t scale_x,vg_lite_float_t scale_y,vg_lite_matrix_t * matrix)81 void vg_lite_scale(vg_lite_float_t scale_x, vg_lite_float_t scale_y, vg_lite_matrix_t * matrix)
82 {
83     /* Set scale matrix. */
84     vg_lite_matrix_t s = { { {scale_x, 0.0f, 0.0f},
85         {0.0f, scale_y, 0.0f},
86         {0.0f, 0.0f, 1.0f}
87     } };
88 
89     /* Multiply with current matrix. */
90     multiply(matrix, &s);
91 }
92 
vg_lite_rotate(vg_lite_float_t degrees,vg_lite_matrix_t * matrix)93 void vg_lite_rotate(vg_lite_float_t degrees, vg_lite_matrix_t * matrix)
94 {
95 #ifndef M_PI
96 #define M_PI 3.1415926f
97 #endif
98     /* Convert degrees into radians. */
99     vg_lite_float_t angle = degrees / 180.0f * M_PI;
100 
101     /* Compuet cosine and sine values. */
102     vg_lite_float_t cos_angle = cosf(angle);
103     vg_lite_float_t sin_angle = sinf(angle);
104 
105     /* Set rotation matrix. */
106     vg_lite_matrix_t r = { { {cos_angle, -sin_angle, 0.0f},
107         {sin_angle, cos_angle, 0.0f},
108         {0.0f, 0.0f, 1.0f}
109     } };
110 
111     /* Multiply with current matrix. */
112     multiply(matrix, &r);
113 }
114