1 /*
2 * Copyright (c) 2021 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 #include "common/rs_obj_abs_geometry.h"
17 #include "utils/camera3d.h"
18
19 namespace OHOS {
20 namespace Rosen {
21 constexpr unsigned RECT_POINT_NUM = 4;
22 constexpr unsigned LEFT_TOP_POINT = 0;
23 constexpr unsigned RIGHT_TOP_POINT = 1;
24 constexpr unsigned RIGHT_BOTTOM_POINT = 2;
25 constexpr unsigned LEFT_BOTTOM_POINT = 3;
26 constexpr float INCH_TO_PIXEL = 72;
27 constexpr float EPSILON = 1e-4f;
28 constexpr float DEGREE_TO_RADIAN = M_PI / 180;
29
30 RSObjAbsGeometry::RSObjAbsGeometry() = default;
31 RSObjAbsGeometry::~RSObjAbsGeometry() = default;
32
ConcatMatrix(const Drawing::Matrix & matrix)33 void RSObjAbsGeometry::ConcatMatrix(const Drawing::Matrix& matrix)
34 {
35 if (matrix.IsIdentity()) {
36 return;
37 }
38 matrix_.PreConcat(matrix);
39 if (absMatrix_.has_value()) {
40 absMatrix_->PreConcat(matrix);
41 }
42 SetAbsRect();
43 }
44
45 /**
46 * @brief Updates the matrix of the view with respect to its parent view.
47 *
48 * @param parent The parent view of the current view.
49 * @param offset The offset of the current view with respect to its parent.
50 * @param clipRect The optional clipping rectangle of the current view.
51 */
UpdateMatrix(const Drawing::Matrix * parentMatrix,const std::optional<Drawing::Point> & offset)52 void RSObjAbsGeometry::UpdateMatrix(const Drawing::Matrix* parentMatrix, const std::optional<Drawing::Point>& offset)
53 {
54 // Initialize the absolute matrix with the absolute matrix of the parent view if the parent view exists
55 if (parentMatrix == nullptr) {
56 absMatrix_.reset();
57 } else {
58 absMatrix_ = *parentMatrix;
59 if (offset.has_value()) {
60 absMatrix_->PreTranslate(offset->GetX(), offset->GetY());
61 }
62 }
63 // Reset the matrix of the current view
64 matrix_.Reset();
65 // filter invalid width and height
66 if (IsEmpty()) {
67 return;
68 }
69 // If the view has no transformations or only 2D transformations, update the absolute matrix with 2D
70 // transformations
71 if (!trans_ || (ROSEN_EQ(trans_->translateZ_, 0.f) && ROSEN_EQ(trans_->rotationX_, 0.f) &&
72 ROSEN_EQ(trans_->rotationY_, 0.f) && trans_->quaternion_.IsIdentity())) {
73 UpdateAbsMatrix2D();
74 } else {
75 // Otherwise, update the absolute matrix with 3D transformations
76 UpdateAbsMatrix3D();
77 }
78 // If the context matrix of the current view exists, update the current matrix with it
79 if (contextMatrix_.has_value()) {
80 matrix_.PostConcat(*contextMatrix_);
81 }
82 // If the absolute matrix of the current view exists, update it with the context matrix and the current matrix
83 if (absMatrix_.has_value()) {
84 absMatrix_->PreConcat(matrix_);
85 }
86 // Update the absolute rectangle of the current view
87 SetAbsRect();
88 }
89
90 /**
91 * @brief Updates the matrix of the view without its parent view.
92 */
UpdateByMatrixFromSelf()93 void RSObjAbsGeometry::UpdateByMatrixFromSelf()
94 {
95 absMatrix_.reset();
96 matrix_.Reset();
97
98 // If the view has no transformations or only 2D transformations, update the absolute matrix with 2D transformations
99 if (!trans_ || (ROSEN_EQ(trans_->translateZ_, 0.f) && ROSEN_EQ(trans_->rotationX_, 0.f) &&
100 ROSEN_EQ(trans_->rotationY_, 0.f) && trans_->quaternion_.IsIdentity())) {
101 UpdateAbsMatrix2D();
102 } else {
103 // Otherwise, update the absolute matrix with 3D transformations
104 UpdateAbsMatrix3D();
105 }
106
107 // If the context matrix of the view exists, update the current matrix with it
108 if (contextMatrix_.has_value()) {
109 matrix_.PostConcat(*contextMatrix_);
110 }
111
112 // Update the absolute rectangle of the view
113 SetAbsRect();
114 }
115
IsNeedClientCompose() const116 bool RSObjAbsGeometry::IsNeedClientCompose() const
117 {
118 if (!trans_) {
119 return false;
120 }
121 // return false if rotation degree is times of 90
122 return !ROSEN_EQ(std::remainder(trans_->rotation_, 90.f), 0.f, EPSILON);
123 }
124
125 namespace {
ApplySkewToMatrix44(const RSTransform & trans,Drawing::Matrix44 & m44,bool preConcat)126 void ApplySkewToMatrix44(const RSTransform& trans, Drawing::Matrix44& m44, bool preConcat)
127 {
128 if (!ROSEN_EQ(trans.skewX_, 0.f, EPSILON) || !ROSEN_EQ(trans.skewY_, 0.f, EPSILON)) {
129 Drawing::Matrix44 skewM44 {};
130 skewM44.SetMatrix44RowMajor({1.f, trans.skewX_, 0.f, 0.f,
131 trans.skewY_, 1.f, 0.f, 0.f,
132 0.f, 0.f, 1.f, 0.f,
133 0.f, 0.f, 0.f, 1.f});
134 if (preConcat) {
135 m44 = skewM44 * m44;
136 } else {
137 m44 = m44 * skewM44;
138 }
139 }
140 }
141
ApplyPerspToMatrix(const RSTransform & trans,Drawing::Matrix & m,bool preConcat)142 void ApplyPerspToMatrix(const RSTransform& trans, Drawing::Matrix& m, bool preConcat)
143 {
144 if (!ROSEN_EQ(trans.perspX_, 0.f, EPSILON) || !ROSEN_EQ(trans.perspY_, 0.f, EPSILON)) {
145 Drawing::Matrix perspM {};
146 perspM.SetMatrix(1.f, 0.f, 0.f,
147 0.f, 1.f, 0.f,
148 trans.perspX_, trans.perspY_, 1.f);
149 if (preConcat) {
150 m = perspM * m;
151 } else {
152 m = m * perspM;
153 }
154 }
155 }
156
ApplyPerspToMatrix44(const RSTransform & trans,Drawing::Matrix44 & m44,bool preConcat)157 void ApplyPerspToMatrix44(const RSTransform& trans, Drawing::Matrix44& m44, bool preConcat)
158 {
159 if (!ROSEN_EQ(trans.perspX_, 0.f, EPSILON) || !ROSEN_EQ(trans.perspY_, 0.f, EPSILON)) {
160 Drawing::Matrix44 perspM44 {};
161 perspM44.SetMatrix44RowMajor({1.f, 0.f, 0.f, 0.f,
162 0.f, 1.f, 0.f, 0.f,
163 0.f, 0.f, 1.f, 0.f,
164 trans.perspX_, trans.perspY_, 0.f, 1.f});
165 if (preConcat) {
166 m44 = perspM44 * m44;
167 } else {
168 m44 = m44 * perspM44;
169 }
170 }
171 }
172 }
173
UpdateAbsMatrix2D()174 void RSObjAbsGeometry::UpdateAbsMatrix2D()
175 {
176 if (!trans_) {
177 matrix_.PreTranslate(x_, y_);
178 } else {
179 // Translate
180 if (!ROSEN_EQ(x_ + trans_->translateX_, 0.f, EPSILON) || !ROSEN_EQ(y_ + trans_->translateY_, 0.f, EPSILON)) {
181 matrix_.PreTranslate(x_ + trans_->translateX_, y_ + trans_->translateY_);
182 }
183 // Persp
184 if (!ROSEN_EQ(trans_->perspX_, 0.f, EPSILON) || !ROSEN_EQ(trans_->perspY_, 0.f, EPSILON)) {
185 matrix_.PreTranslate(trans_->pivotX_ * width_, trans_->pivotY_ * height_);
186 ApplyPerspToMatrix(trans_.value(), matrix_, false);
187 matrix_.PreTranslate(-trans_->pivotX_ * width_, -trans_->pivotY_ * height_);
188 }
189 // rotation
190 if (!ROSEN_EQ(trans_->rotation_, 0.f, EPSILON)) {
191 matrix_.PreRotate(trans_->rotation_, trans_->pivotX_ * width_, trans_->pivotY_ * height_);
192 }
193 // Skew
194 if (!ROSEN_EQ(trans_->skewX_, 0.f, EPSILON) || !ROSEN_EQ(trans_->skewY_, 0.f, EPSILON)) {
195 matrix_.PreSkew(trans_->skewX_, trans_->skewY_, trans_->pivotX_ * width_, trans_->pivotY_ * height_);
196 }
197 // Scale
198 if (!ROSEN_EQ(trans_->scaleX_, 1.f) || !ROSEN_EQ(trans_->scaleY_, 1.f)) {
199 matrix_.PreScale(trans_->scaleX_, trans_->scaleY_, trans_->pivotX_ * width_, trans_->pivotY_ * height_);
200 }
201 }
202 }
203
204 /**
205 * Update the absolute matrix in 3D space
206 */
UpdateAbsMatrix3D()207 void RSObjAbsGeometry::UpdateAbsMatrix3D()
208 {
209 // If the view has a non-identity quaternion, apply 3D transformations
210 if (!trans_->quaternion_.IsIdentity()) {
211 Drawing::Matrix44 matrix3D;
212 // Pivot
213 matrix3D.Translate(trans_->pivotX_ * width_, trans_->pivotY_ * height_, 0);
214 // Persp
215 ApplyPerspToMatrix44(trans_.value(), matrix3D, false);
216 // Translate
217 matrix3D.PreTranslate(x_ + trans_->translateX_, y_ + trans_->translateY_, z_ + trans_->translateZ_);
218 // Rotate
219 float x = trans_->quaternion_[0];
220 float y = trans_->quaternion_[1];
221 float z = trans_->quaternion_[2];
222 float w = trans_->quaternion_[3];
223 Drawing::Matrix44::Buffer buffer = {
224 1.f - 2.f * (y * y + z * z), 2.f * (x * y + z * w), 2.f * (x * z - y * w), 0, // m00 ~ m30
225 2.f * (x * y - z * w), 1.f - 2.f * (x * x + z * z), 2.f * (y * z + x * w), 0, // m01 ~ m31
226 2.f * (x * z + y * w), 2.f * (y * z - x * w), 1.f - 2.f * (x * x + y * y), 0, // m02 ~ m32
227 0, 0, 0, 1 }; // m03 ~ m33
228 Drawing::Matrix44 matrix4;
229 matrix4.SetMatrix44ColMajor(buffer);
230 matrix3D = matrix3D * matrix4;
231 // Skew
232 ApplySkewToMatrix44(trans_.value(), matrix3D, false);
233 // Scale
234 if (!ROSEN_EQ(trans_->scaleX_, 1.f) || !ROSEN_EQ(trans_->scaleY_, 1.f)) {
235 matrix3D.PreScale(trans_->scaleX_, trans_->scaleY_, 1.f);
236 }
237 // Translate
238 matrix3D.PreTranslate(-trans_->pivotX_ * width_, -trans_->pivotY_ * height_, 0);
239
240 // Concatenate the 3D matrix with the 2D matrix
241 matrix_.PreConcat(matrix3D);
242 } else {
243 Drawing::Matrix matrix3D;
244 Drawing::Camera3D camera;
245 // Z Position
246 camera.Translate(0, 0, z_ + trans_->translateZ_);
247
248 // Set camera distance
249 if (trans_->cameraDistance_ == 0) {
250 float zOffSet = sqrt(width_ * width_ + height_ * height_) / 2;
251 camera.SetCameraPos(0, 0, camera.GetCameraPosZ() - zOffSet / INCH_TO_PIXEL);
252 } else {
253 camera.SetCameraPos(0, 0, trans_->cameraDistance_);
254 }
255 // Rotate
256 if (!ROSEN_EQ(trans_->pivotZ_, 0.f, EPSILON)) {
257 camera.Translate(-sin(trans_->rotationY_ * DEGREE_TO_RADIAN) * trans_->pivotZ_,
258 -sin(trans_->rotationX_ * DEGREE_TO_RADIAN) * trans_->pivotZ_,
259 (1 - cos(trans_->rotationX_ * DEGREE_TO_RADIAN) * cos(trans_->rotationY_ * DEGREE_TO_RADIAN)) *
260 trans_->pivotZ_);
261 }
262 camera.RotateXDegrees(-trans_->rotationX_);
263 camera.RotateYDegrees(-trans_->rotationY_);
264 camera.RotateZDegrees(-trans_->rotation_);
265 camera.ApplyToMatrix(matrix3D);
266 // Skew
267 if (!ROSEN_EQ(trans_->skewX_, 0.f, EPSILON) || !ROSEN_EQ(trans_->skewY_, 0.f, EPSILON)) {
268 matrix3D.PreSkew(trans_->skewX_, trans_->skewY_);
269 }
270 // Scale
271 if (!ROSEN_EQ(trans_->scaleX_, 1.f) || !ROSEN_EQ(trans_->scaleY_, 1.f)) {
272 matrix3D.PreScale(trans_->scaleX_, trans_->scaleY_);
273 }
274 // Pivot
275 matrix3D.PreTranslate(-trans_->pivotX_ * width_, -trans_->pivotY_ * height_);
276
277 // Translate
278 matrix3D.PostTranslate(x_ + trans_->translateX_, y_ + trans_->translateY_);
279 // PostPersp
280 ApplyPerspToMatrix(trans_.value(), matrix3D, true);
281 // Pivot
282 matrix3D.PostTranslate(trans_->pivotX_ * width_, trans_->pivotY_ * height_);
283
284 // Concatenate the 3D matrix with the 2D matrix
285 matrix_.PreConcat(matrix3D);
286 }
287 }
288
SetAbsRect()289 void RSObjAbsGeometry::SetAbsRect()
290 {
291 absRect_ = MapAbsRect(RectF(0.f, 0.f, width_, height_));
292 }
293
294 /**
295 * Map the rectangle with specific matrix
296 * [planning] replaced by Drawing::MapRect
297 * @param rect the rectangle to map
298 * @param matrix the specific to map
299 * @return the mapped absolute rectangle
300 */
MapRect(const RectF & rect,const Drawing::Matrix & matrix)301 RectI RSObjAbsGeometry::MapRect(const RectF& rect, const Drawing::Matrix& matrix)
302 {
303 RectI absRect;
304 // Check if the matrix has skew or negative scaling
305 if (!ROSEN_EQ(matrix.Get(Drawing::Matrix::PERSP_0), 0.f, EPSILON) ||
306 !ROSEN_EQ(matrix.Get(Drawing::Matrix::PERSP_1), 0.f, EPSILON) ||
307 !ROSEN_EQ(matrix.Get(Drawing::Matrix::PERSP_2), 0.f, EPSILON)) {
308 Drawing::RectF src(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
309 Drawing::RectF dst;
310 matrix.MapRect(dst, src);
311 absRect.left_ = static_cast<int>(std::floor(dst.GetLeft()));
312 absRect.top_ = static_cast<int>(std::floor(dst.GetTop()));
313 absRect.width_ = static_cast<int>(std::ceil(dst.GetRight() - absRect.left_));
314 absRect.height_ = static_cast<int>(std::ceil(dst.GetBottom() - absRect.top_));
315 } else if (!ROSEN_EQ(matrix.Get(Drawing::Matrix::SKEW_X), 0.f) || (matrix.Get(Drawing::Matrix::SCALE_X) < 0) ||
316 !ROSEN_EQ(matrix.Get(Drawing::Matrix::SKEW_Y), 0.f) || (matrix.Get(Drawing::Matrix::SCALE_Y) < 0)) {
317 // Map the rectangle's points to the absolute matrix
318 std::vector<Drawing::Point> p(RECT_POINT_NUM);
319 p[LEFT_TOP_POINT] = {rect.left_, rect.top_};
320 p[RIGHT_TOP_POINT] = {rect.left_ + rect.width_, rect.top_};
321 p[RIGHT_BOTTOM_POINT] = {rect.left_ + rect.width_, rect.top_ + rect.height_};
322 p[LEFT_BOTTOM_POINT] = {rect.left_, rect.top_ + rect.height_};
323 matrix.MapPoints(p, p, RECT_POINT_NUM);
324
325 Vector2f xRange = GetDataRange(p[LEFT_TOP_POINT].GetX(), p[RIGHT_TOP_POINT].GetX(),
326 p[RIGHT_BOTTOM_POINT].GetX(), p[LEFT_BOTTOM_POINT].GetX());
327 Vector2f yRange = GetDataRange(p[LEFT_TOP_POINT].GetY(), p[RIGHT_TOP_POINT].GetY(),
328 p[RIGHT_BOTTOM_POINT].GetY(), p[LEFT_BOTTOM_POINT].GetY());
329
330 // Set the absolute rectangle's properties
331 absRect.left_ = static_cast<int>(std::floor(xRange[0]));
332 absRect.top_ = static_cast<int>(std::floor(yRange[0]));
333 absRect.width_ = static_cast<int>(std::ceil(xRange[1] - absRect.left_));
334 absRect.height_ = static_cast<int>(std::ceil(yRange[1] - absRect.top_));
335 } else {
336 // Calculate the absolute rectangle based on the matrix's translation and scaling
337 Drawing::scalar transX = matrix.Get(Drawing::Matrix::TRANS_X);
338 Drawing::scalar transY = matrix.Get(Drawing::Matrix::TRANS_Y);
339 Drawing::scalar scaleX = matrix.Get(Drawing::Matrix::SCALE_X);
340 Drawing::scalar scaleY = matrix.Get(Drawing::Matrix::SCALE_Y);
341 absRect.left_ = static_cast<int>(std::floor(rect.left_ * scaleX + transX));
342 absRect.top_ = static_cast<int>(std::floor(rect.top_ * scaleY + transY));
343 float right = (rect.left_ + rect.width_) * scaleX + transX;
344 float bottom = (rect.top_ + rect.height_) * scaleY + transY;
345 absRect.width_ = static_cast<int>(std::ceil(right - absRect.left_));
346 absRect.height_ = static_cast<int>(std::ceil(bottom - absRect.top_));
347 }
348 return absRect;
349 }
350
351 /**
352 * Map the absolute rectangle
353 * @param rect the rectangle to map
354 * @return the mapped absolute rectangle
355 */
MapAbsRect(const RectF & rect) const356 RectI RSObjAbsGeometry::MapAbsRect(const RectF& rect) const
357 {
358 return MapRect(rect, GetAbsMatrix());
359 }
360
GetDataRange(float d0,float d1,float d2,float d3)361 Vector2f RSObjAbsGeometry::GetDataRange(float d0, float d1, float d2, float d3)
362 {
363 float min = d0;
364 float max = d0;
365 if (d0 > d1) {
366 min = d1;
367 } else {
368 max = d1;
369 }
370 if (d2 > d3) {
371 if (min > d3) {
372 min = d3;
373 }
374 if (max < d2) {
375 max = d2;
376 }
377 } else {
378 if (min > d2) {
379 min = d2;
380 }
381 if (max < d3) {
382 max = d3;
383 }
384 }
385 return {min, max};
386 }
387
SetContextMatrix(const std::optional<Drawing::Matrix> & matrix)388 void RSObjAbsGeometry::SetContextMatrix(const std::optional<Drawing::Matrix>& matrix)
389 {
390 contextMatrix_ = matrix;
391 }
392
GetMatrix() const393 const Drawing::Matrix& RSObjAbsGeometry::GetMatrix() const
394 {
395 return matrix_;
396 }
397
GetAbsMatrix() const398 const Drawing::Matrix& RSObjAbsGeometry::GetAbsMatrix() const
399 {
400 // if absMatrix_ is empty, return matrix_ instead
401 return absMatrix_ ? *absMatrix_ : matrix_;
402 }
403 } // namespace Rosen
404 } // namespace OHOS
405