• 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 #include "ui_rotation.h"
17 #include "log/log.h"
18 #include "securec.h"
19 
20 namespace Updater {
GetInstance(void)21 UiRotation &UiRotation::GetInstance(void)
22 {
23     static UiRotation uiRotation;
24     return uiRotation;
25 }
26 
InitRotation(int realWidth,int realHeight,uint8_t pixelBytes)27 void UiRotation::InitRotation(int realWidth, int realHeight, uint8_t pixelBytes)
28 {
29     LOG(INFO) << "InitRotation realWidth = " << realWidth << ", realHeight = " << realHeight <<
30         ", pixelBytes = " << static_cast<int>(pixelBytes);
31     pixelBytes_ = pixelBytes;
32     RotateWidthHeight(realWidth, realHeight);
33     switch (degree_) {
34         case UI_ROTATION_DEGREE::UI_ROTATION_90: // 90: input number 90
35             sinR_ = 1;
36             cosR_ = 0;
37             offsetX_ = height_ - 1;
38             offsetY_ = 0;
39             oldRowBytes_ = width_ * pixelBytes_;
40             newRowBytes_ = height_ * pixelBytes_;
41             break;
42         case UI_ROTATION_DEGREE::UI_ROTATION_180: // 180: input number 180
43             sinR_ = 0;
44             cosR_ = -1;
45             offsetX_ = width_ - 1;
46             offsetY_ = height_ - 1;
47             oldRowBytes_ = width_ * pixelBytes_;
48             newRowBytes_ = width_ * pixelBytes_;
49             break;
50         case UI_ROTATION_DEGREE::UI_ROTATION_270: // 270: input number 270
51             sinR_ = -1;
52             cosR_ = 0;
53             offsetX_ = 0;
54             offsetY_ = width_ - 1;
55             oldRowBytes_ = width_ * pixelBytes_;
56             newRowBytes_ = height_ * pixelBytes_;
57             break;
58         default:
59             sinR_ = 0;
60             cosR_ = 1;
61             offsetX_ = 0;
62             offsetY_ = 0;
63             oldRowBytes_ = width_ * pixelBytes_;
64             newRowBytes_ = width_ * pixelBytes_;
65     }
66 }
67 
SetDegree(UI_ROTATION_DEGREE degree)68 void UiRotation::SetDegree(UI_ROTATION_DEGREE degree)
69 {
70     degree_ = degree;
71 }
72 
GetWidth(void)73 int UiRotation::GetWidth(void)
74 {
75     return width_;
76 }
77 
GetHeight(void)78 int UiRotation::GetHeight(void)
79 {
80     return height_;
81 }
82 
RotateWidthHeight(int realWidth,int realHeight)83 void UiRotation::RotateWidthHeight(int realWidth, int realHeight)
84 {
85     if (degree_ == UI_ROTATION_DEGREE::UI_ROTATION_0 || degree_ == UI_ROTATION_DEGREE::UI_ROTATION_180) {
86         width_ = realWidth;
87         height_ = realHeight;
88     } else {
89         width_ = realHeight;
90         height_ = realWidth;
91     }
92 }
93 
SetFlushRange(const OHOS::Rect & rect)94 void UiRotation::SetFlushRange(const OHOS::Rect &rect)
95 {
96     rect_ = rect;
97 }
98 
RotateBuffer(const uint8_t * origBuf,uint8_t * dstBuf,uint32_t size)99 void UiRotation::RotateBuffer(const uint8_t *origBuf, uint8_t *dstBuf, uint32_t size)
100 {
101     if (degree_ == UI_ROTATION_DEGREE::UI_ROTATION_0) {
102         if (memcpy_s(dstBuf, size, origBuf, size) != EOK) {
103             LOG(ERROR) << "flip memcpy_s fail";
104         }
105         return;
106     }
107 
108     int x {};
109     int y {};
110     const uint8_t *srcP = nullptr;
111     uint8_t *dstP = nullptr;
112     int top = rect_.GetTop();
113     int bottom = rect_.GetBottom();
114     int left = rect_.GetLeft();
115     int right = rect_.GetRight();
116     for (int h = top; h <= bottom; h++) {
117         for (int w = left; w <= right; w++) {
118             x = offsetX_ + w * cosR_ - h * sinR_;
119             y = offsetY_ + h * cosR_ + w * sinR_;
120             srcP = origBuf + h * oldRowBytes_ + w * pixelBytes_;
121             dstP = dstBuf + y * newRowBytes_ + x * pixelBytes_;
122             if (memcpy_s(dstP, pixelBytes_, srcP, pixelBytes_) != EOK) {
123                 LOG(ERROR) << "flip memcpy_s fail " << "w = " << w << " h = " << h;
124                 return;
125             } else {
126                 srcP += pixelBytes_;
127                 dstP += pixelBytes_;
128             }
129         }
130     }
131 }
132 
RotateXY(int x,int y)133 std::pair<int, int> UiRotation::RotateXY(int x, int y)
134 {
135     if (degree_ == UI_ROTATION_DEGREE::UI_ROTATION_0) {
136         return {x, y};
137     }
138     return {cosR_ * (x - offsetX_) + sinR_ * (y - offsetY_), cosR_ * (y - offsetY_) - sinR_ * (x - offsetX_)};
139 }
140 }