1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <input/DisplayViewport.h>
18 #include <input/TouchVideoFrame.h>
19 
20 namespace android {
21 
TouchVideoFrame(uint32_t height,uint32_t width,std::vector<int16_t> data,const struct timeval & timestamp)22 TouchVideoFrame::TouchVideoFrame(uint32_t height, uint32_t width, std::vector<int16_t> data,
23         const struct timeval& timestamp) :
24          mHeight(height), mWidth(width),mData(std::move(data)), mTimestamp(timestamp) {
25 }
26 
operator ==(const TouchVideoFrame & rhs) const27 bool TouchVideoFrame::operator==(const TouchVideoFrame& rhs) const {
28     return mHeight == rhs.mHeight
29             && mWidth == rhs.mWidth
30             && mData == rhs.mData
31             && mTimestamp.tv_sec == rhs.mTimestamp.tv_sec
32             && mTimestamp.tv_usec == rhs.mTimestamp.tv_usec;
33 }
34 
getHeight() const35 uint32_t TouchVideoFrame::getHeight() const { return mHeight; }
36 
getWidth() const37 uint32_t TouchVideoFrame::getWidth() const { return mWidth; }
38 
getData() const39 const std::vector<int16_t>& TouchVideoFrame::getData() const { return mData; }
40 
getTimestamp() const41 const struct timeval& TouchVideoFrame::getTimestamp() const { return mTimestamp; }
42 
rotate(ui::Rotation orientation)43 void TouchVideoFrame::rotate(ui::Rotation orientation) {
44     switch (orientation) {
45         case ui::ROTATION_90:
46             rotateQuarterTurn(/*clockwise=*/false);
47             break;
48         case ui::ROTATION_180:
49             rotate180();
50             break;
51         case ui::ROTATION_270:
52             rotateQuarterTurn(/*clockwise=*/true);
53             break;
54         case ui::ROTATION_0:
55             // No need to rotate if there's no rotation.
56             break;
57     }
58 }
59 
60 /**
61  * Rotate once clockwise by a quarter turn === rotate 90 degrees
62  * Rotate once counterclockwise by a quarter turn === rotate 270 degrees
63  * For a clockwise rotation:
64  *     An element at position (i, j) is rotated to (j, height - i - 1)
65  * For a counterclockwise rotation:
66  *     An element at position (i, j) is rotated to (width - j - 1, i)
67  */
rotateQuarterTurn(bool clockwise)68 void TouchVideoFrame::rotateQuarterTurn(bool clockwise) {
69     std::vector<int16_t> rotated(mData.size());
70     for (size_t i = 0; i < mHeight; i++) {
71         for (size_t j = 0; j < mWidth; j++) {
72             size_t iRotated, jRotated;
73             if (clockwise) {
74                 iRotated = j;
75                 jRotated = mHeight - i - 1;
76             } else {
77                 iRotated = mWidth - j - 1;
78                 jRotated = i;
79             }
80             size_t indexRotated = iRotated * mHeight + jRotated;
81             rotated[indexRotated] = mData[i * mWidth + j];
82         }
83     }
84     mData = std::move(rotated);
85     std::swap(mHeight, mWidth);
86 }
87 
88 /**
89  * An element at position (i, j) is rotated to (height - i - 1, width - j - 1)
90  * This is equivalent to moving element [i] to position [height * width - i - 1]
91  * Since element at [height * width - i - 1] would move to position [i],
92  * we can just swap elements [i] and [height * width - i - 1].
93  */
rotate180()94 void TouchVideoFrame::rotate180() {
95     if (mData.size() == 0) {
96         return;
97     }
98     // Just need to swap elements i and (height * width - 1 - i)
99     for (size_t i = 0; i < mData.size() / 2; i++) {
100         std::swap(mData[i], mData[mHeight * mWidth - 1 - i]);
101     }
102 }
103 
104 } // namespace android
105