vg_lite_matrix.c 4.8 KB

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