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
20namespace Updater {
21UiRotation &UiRotation::GetInstance(void)
22{
23    static UiRotation uiRotation;
24    return uiRotation;
25}
26
27void 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
68void UiRotation::SetDegree(UI_ROTATION_DEGREE degree)
69{
70    degree_ = degree;
71}
72
73int UiRotation::GetWidth(void)
74{
75    return width_;
76}
77
78int UiRotation::GetHeight(void)
79{
80    return height_;
81}
82
83void 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
94void UiRotation::SetFlushRange(const OHOS::Rect &rect)
95{
96    rect_ = rect;
97}
98
99void 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 {}, y {};
109    const uint8_t *srcP = nullptr;
110    uint8_t *dstP = nullptr;
111    for (int h = rect_.GetTop(); h < rect_.GetBottom(); h++) {
112        for (int w = rect_.GetLeft(); w < rect_.GetRight(); w++) {
113            x = offsetX_ + w * cosR_ - h * sinR_;
114            y = offsetY_ + h * cosR_ + w * sinR_;
115            srcP = origBuf + h * oldRowBytes_ + w * pixelBytes_;
116            dstP = dstBuf + y * newRowBytes_ + x * pixelBytes_;
117            for (int j = 0; j < pixelBytes_; j++) {
118                *dstP++ = *srcP++;
119            }
120        }
121    }
122}
123
124std::pair<int, int> UiRotation::RotateXY(int x, int y)
125{
126    if (degree_ == UI_ROTATION_DEGREE::UI_ROTATION_0) {
127        return {x, y};
128    }
129    return {cosR_ * (x - offsetX_) + sinR_ * (y - offsetY_), cosR_ * (y - offsetY_) - sinR_ * (x - offsetX_)};
130}
131}