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 if (isZApplicableCamera3D_) {
247 camera.Translate(0, 0, z_ + trans_->translateZ_);
248 } else {
249 camera.Translate(0, 0, trans_->translateZ_);
250 }
251
252 // Set camera distance
253 if (trans_->cameraDistance_ == 0) {
254 float zOffSet = sqrt(width_ * width_ + height_ * height_) / 2;
255 camera.SetCameraPos(0, 0, camera.GetCameraPosZ() - zOffSet / INCH_TO_PIXEL);
256 } else {
257 camera.SetCameraPos(0, 0, trans_->cameraDistance_);
258 }
259 // Rotate
260 if (!ROSEN_EQ(trans_->pivotZ_, 0.f, EPSILON)) {
261 camera.Translate(-sin(trans_->rotationY_ * DEGREE_TO_RADIAN) * trans_->pivotZ_,
262 -sin(trans_->rotationX_ * DEGREE_TO_RADIAN) * trans_->pivotZ_,
263 (1 - cos(trans_->rotationX_ * DEGREE_TO_RADIAN) * cos(trans_->rotationY_ * DEGREE_TO_RADIAN)) *
264 trans_->pivotZ_);
265 }
266 camera.RotateXDegrees(-trans_->rotationX_);
267 camera.RotateYDegrees(-trans_->rotationY_);
268 camera.RotateZDegrees(-trans_->rotation_);
269 camera.ApplyToMatrix(matrix3D);
270 // Skew
271 if (!ROSEN_EQ(trans_->skewX_, 0.f, EPSILON) || !ROSEN_EQ(trans_->skewY_, 0.f, EPSILON)) {
272 matrix3D.PreSkew(trans_->skewX_, trans_->skewY_);
273 }
274 // Scale
275 if (!ROSEN_EQ(trans_->scaleX_, 1.f) || !ROSEN_EQ(trans_->scaleY_, 1.f)) {
276 matrix3D.PreScale(trans_->scaleX_, trans_->scaleY_);
277 }
278 // Pivot
279 matrix3D.PreTranslate(-trans_->pivotX_ * width_, -trans_->pivotY_ * height_);
280
281 // Translate
282 matrix3D.PostTranslate(x_ + trans_->translateX_, y_ + trans_->translateY_);
283 // PostPersp
284 ApplyPerspToMatrix(trans_.value(), matrix3D, true);
285 // Pivot
286 matrix3D.PostTranslate(trans_->pivotX_ * width_, trans_->pivotY_ * height_);
287
288 // Concatenate the 3D matrix with the 2D matrix
289 matrix_.PreConcat(matrix3D);
290 }
291 }
292
SetAbsRect()293 void RSObjAbsGeometry::SetAbsRect()
294 {
295 absRect_ = MapAbsRect(RectF(0.f, 0.f, width_, height_));
296 }
297
298 /**
299 * Map the rectangle with specific matrix
300 * [planning] replaced by Drawing::MapRect
301 * @param rect the rectangle to map
302 * @param matrix the specific to map
303 * @return the mapped absolute rectangle
304 */
MapRect(const RectF & rect,const Drawing::Matrix & matrix)305 RectI RSObjAbsGeometry::MapRect(const RectF& rect, const Drawing::Matrix& matrix)
306 {
307 RectI absRect;
308 // Check if the matrix has skew or negative scaling
309 if (!ROSEN_EQ(matrix.Get(Drawing::Matrix::PERSP_0), 0.f, EPSILON) ||
310 !ROSEN_EQ(matrix.Get(Drawing::Matrix::PERSP_1), 0.f, EPSILON) ||
311 !ROSEN_EQ(matrix.Get(Drawing::Matrix::PERSP_2), 0.f, EPSILON)) {
312 Drawing::RectF src(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
313 Drawing::RectF dst;
314 matrix.MapRect(dst, src);
315 absRect.left_ = static_cast<int>(std::floor(dst.GetLeft()));
316 absRect.top_ = static_cast<int>(std::floor(dst.GetTop()));
317 absRect.width_ = static_cast<int>(std::ceil(dst.GetRight() - absRect.left_));
318 absRect.height_ = static_cast<int>(std::ceil(dst.GetBottom() - absRect.top_));
319 } else if (!ROSEN_EQ(matrix.Get(Drawing::Matrix::SKEW_X), 0.f) || (matrix.Get(Drawing::Matrix::SCALE_X) < 0) ||
320 !ROSEN_EQ(matrix.Get(Drawing::Matrix::SKEW_Y), 0.f) || (matrix.Get(Drawing::Matrix::SCALE_Y) < 0)) {
321 // Map the rectangle's points to the absolute matrix
322 std::vector<Drawing::Point> p(RECT_POINT_NUM);
323 p[LEFT_TOP_POINT] = {rect.left_, rect.top_};
324 p[RIGHT_TOP_POINT] = {rect.left_ + rect.width_, rect.top_};
325 p[RIGHT_BOTTOM_POINT] = {rect.left_ + rect.width_, rect.top_ + rect.height_};
326 p[LEFT_BOTTOM_POINT] = {rect.left_, rect.top_ + rect.height_};
327 matrix.MapPoints(p, p, RECT_POINT_NUM);
328
329 Vector2f xRange = GetDataRange(p[LEFT_TOP_POINT].GetX(), p[RIGHT_TOP_POINT].GetX(),
330 p[RIGHT_BOTTOM_POINT].GetX(), p[LEFT_BOTTOM_POINT].GetX());
331 Vector2f yRange = GetDataRange(p[LEFT_TOP_POINT].GetY(), p[RIGHT_TOP_POINT].GetY(),
332 p[RIGHT_BOTTOM_POINT].GetY(), p[LEFT_BOTTOM_POINT].GetY());
333
334 // Set the absolute rectangle's properties
335 absRect.left_ = static_cast<int>(std::floor(xRange[0]));
336 absRect.top_ = static_cast<int>(std::floor(yRange[0]));
337 absRect.width_ = static_cast<int>(std::ceil(xRange[1] - absRect.left_));
338 absRect.height_ = static_cast<int>(std::ceil(yRange[1] - absRect.top_));
339 } else {
340 // Calculate the absolute rectangle based on the matrix's translation and scaling
341 Drawing::scalar transX = matrix.Get(Drawing::Matrix::TRANS_X);
342 Drawing::scalar transY = matrix.Get(Drawing::Matrix::TRANS_Y);
343 Drawing::scalar scaleX = matrix.Get(Drawing::Matrix::SCALE_X);
344 Drawing::scalar scaleY = matrix.Get(Drawing::Matrix::SCALE_Y);
345 absRect.left_ = static_cast<int>(std::floor(rect.left_ * scaleX + transX));
346 absRect.top_ = static_cast<int>(std::floor(rect.top_ * scaleY + transY));
347 float right = (rect.left_ + rect.width_) * scaleX + transX;
348 float bottom = (rect.top_ + rect.height_) * scaleY + transY;
349 absRect.width_ = static_cast<int>(std::ceil(right - absRect.left_));
350 absRect.height_ = static_cast<int>(std::ceil(bottom - absRect.top_));
351 }
352 return absRect;
353 }
354
355 /**
356 * Map the absolute rectangle
357 * @param rect the rectangle to map
358 * @return the mapped absolute rectangle
359 */
MapAbsRect(const RectF & rect) const360 RectI RSObjAbsGeometry::MapAbsRect(const RectF& rect) const
361 {
362 return MapRect(rect, GetAbsMatrix());
363 }
364
GetDataRange(float d0,float d1,float d2,float d3)365 Vector2f RSObjAbsGeometry::GetDataRange(float d0, float d1, float d2, float d3)
366 {
367 float min = d0;
368 float max = d0;
369 if (d0 > d1) {
370 min = d1;
371 } else {
372 max = d1;
373 }
374 if (d2 > d3) {
375 if (min > d3) {
376 min = d3;
377 }
378 if (max < d2) {
379 max = d2;
380 }
381 } else {
382 if (min > d2) {
383 min = d2;
384 }
385 if (max < d3) {
386 max = d3;
387 }
388 }
389 return {min, max};
390 }
391
SetContextMatrix(const std::optional<Drawing::Matrix> & matrix)392 void RSObjAbsGeometry::SetContextMatrix(const std::optional<Drawing::Matrix>& matrix)
393 {
394 contextMatrix_ = matrix;
395 }
396
GetMatrix() const397 const Drawing::Matrix& RSObjAbsGeometry::GetMatrix() const
398 {
399 return matrix_;
400 }
401
GetAbsMatrix() const402 const Drawing::Matrix& RSObjAbsGeometry::GetAbsMatrix() const
403 {
404 // if absMatrix_ is empty, return matrix_ instead
405 return absMatrix_ ? *absMatrix_ : matrix_;
406 }
407 } // namespace Rosen
408 } // namespace OHOS
409