• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FRAMEWORKS_INNERKITSIMPL_EGL_IMAGE_INCLUDE_PIXEL_MAP_GL_COMMON_H
17 #define FRAMEWORKS_INNERKITSIMPL_EGL_IMAGE_INCLUDE_PIXEL_MAP_GL_COMMON_H
18 
19 #include <iostream>
20 #include "GLES/gl.h"
21 #include "image_trace.h"
22 #include "image_log.h"
23 #include "image_type.h"
24 #include "image_trace.h"
25 #include "image_utils.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace GlCommon {
30 static const int NUM_1 = 1;
31 static const int NUM_2 = 2;
32 static const int NUM_3 = 3;
33 static const int NUM_4 = 4;
34 static const int NUM_5 = 5;
35 static const int NUM_6 = 6;
36 static const float DEGREES_90 = 90.0f;
37 static const float DEGREES_180 = 180.0f;
38 
39 class Mat4 {
40 public:
Mat4()41     Mat4()
42     {
43         for (int i = 0; i < NUM_4; ++i) {
44             for (int j = 0; j < NUM_4; ++j) {
45                 data[i][j] = (i == j) ? 1.0f : 0.0f;
46             }
47         }
48     }
49 
Mat4(float value)50     Mat4(float value)
51     {
52         for (int i = 0; i < NUM_4; ++i) {
53             for (int j = 0; j < NUM_4; ++j) {
54                 data[i][j] = value;
55             }
56         }
57     }
58 
Mat4(const std::array<std::array<float,NUM_4>,NUM_4> & values)59     Mat4(const std::array<std::array<float, NUM_4>, NUM_4>& values)
60     {
61         data = values;
62     }
63 
Mat4(const Mat4 & m,float degrees,const std::array<float,3> & axis)64     Mat4(const Mat4& m, float degrees, const std::array<float, 3>& axis)
65     {
66         float radians = degrees * (M_PI / 180.0f);  // degrees to radians
67 
68         float length = std::sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
69         if (std::abs(length) < 1e-6 || length == 0.0f) {
70             length = 1.0f;
71         }
72         float x = axis[0] / length;
73         float y = axis[1] / length;
74         float z = axis[NUM_2] / length;
75 
76         float cos_theta = std::cos(radians);
77         float sin_theta = std::sin(radians);
78 
79         Mat4 rotation;
80         rotation.data[0][0] = cos_theta + x * x * (1 - cos_theta);
81         rotation.data[0][1] = x * y * (1 - cos_theta) - z * sin_theta;
82         rotation.data[0][NUM_2] = x * z * (1 - cos_theta) + y * sin_theta;
83         rotation.data[0][NUM_3] = 0;
84 
85         rotation.data[1][0] = y * x * (1 - cos_theta) + z * sin_theta;
86         rotation.data[1][1] = cos_theta + y * y * (1 - cos_theta);
87         rotation.data[1][NUM_2] = y * z * (1 - cos_theta) - x * sin_theta;
88         rotation.data[1][NUM_3] = 0;
89 
90         rotation.data[NUM_2][0] = z * x * (1 - cos_theta) - y * sin_theta;
91         rotation.data[NUM_2][1] = z * y * (1 - cos_theta) + x * sin_theta;
92         rotation.data[NUM_2][NUM_2] = cos_theta + z * z * (1 - cos_theta);
93         rotation.data[NUM_2][NUM_3] = 0;
94 
95         rotation.data[NUM_3][0] = 0;
96         rotation.data[NUM_3][1] = 0;
97         rotation.data[NUM_3][NUM_2] = 0;
98         rotation.data[NUM_3][NUM_3] = 1;
99 
100         *this = multiply(m, rotation);
101     }
102 
multiply(const Mat4 & a,const Mat4 & b)103     Mat4 multiply(const Mat4& a, const Mat4& b)
104     {
105         Mat4 result;
106         for (int i = 0; i < NUM_4; ++i) {
107             for (int j = 0; j < NUM_4; ++j) {
108                 result.data[i][j] = 0;
109                 for (int k = 0; k < NUM_4; ++k) {
110                     result.data[i][j] += a.data[i][k] * b.data[k][j];
111                 }
112             }
113         }
114         return result;
115     }
116 
117     Mat4 operator*(const Mat4& other) const
118     {
119         Mat4 result;
120 
121         for (int i = 0; i < NUM_4; ++i) {
122             for (int j = 0; j < NUM_4; ++j) {
123                 result.data[i][j] = 0.0f;
124                 for (int k = 0; k < NUM_4; ++k) {
125                     result.data[i][j] += data[i][k] * other.data[k][j];
126                 }
127             }
128         }
129         return result;
130     }
131 
at(int row,int col)132     float& at(int row, int col)
133     {
134         return data[row][col];
135     }
136 
at(int row,int col)137     const float& at(int row, int col) const
138     {
139         return data[row][col];
140     }
141 
GetDataPtr()142     const float* GetDataPtr()
143     {
144         return &data[0][0];
145     }
146 private:
147     std::array<std::array<float, NUM_4>, NUM_4> data;
148 };
149 } // GlCommon
150 
151 enum TransformationType {
152     SCALE = 1,
153     ROTATE = 2,
154 };
155 
156 struct GlImageInfo {
157     Size size;
158     int32_t stride;
159     int pixelBytes;
160     const uint8_t *addr = nullptr;
161     void *context = nullptr;
162     void *outdata = nullptr;
163 };
164 typedef struct GPUTransformData {
165     float rotateDegreeZ;
166     GlCommon::Mat4 rotateTrans;
167     TransformationType transformationType;
168     GLenum glFormat = GL_RGBA;
169     bool isDma = false;
170     GlImageInfo sourceInfo_;
171     GlImageInfo targetInfo_;
172 } GPUTransformData;
173 } // namespace Media
174 } // namespace OHOS
175 #endif // FRAMEWORKS_INNERKITSIMPL_EGL_IMAGE_INCLUDE_PIXEL_MAP_GL_COMMON_H
176