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/TouchVideoFrame.h>
18 
19 namespace android {
20 
TouchVideoFrame(uint32_t height,uint32_t width,std::vector<int16_t> data,const struct timeval & timestamp)21 TouchVideoFrame::TouchVideoFrame(uint32_t height, uint32_t width, std::vector<int16_t> data,
22         const struct timeval& timestamp) :
23          mHeight(height), mWidth(width),mData(std::move(data)), mTimestamp(timestamp) {
24 }
25 
operator ==(const TouchVideoFrame & rhs) const26 bool TouchVideoFrame::operator==(const TouchVideoFrame& rhs) const {
27     return mHeight == rhs.mHeight
28             && mWidth == rhs.mWidth
29             && mData == rhs.mData
30             && mTimestamp.tv_sec == rhs.mTimestamp.tv_sec
31             && mTimestamp.tv_usec == rhs.mTimestamp.tv_usec;
32 }
33 
getHeight() const34 uint32_t TouchVideoFrame::getHeight() const { return mHeight; }
35 
getWidth() const36 uint32_t TouchVideoFrame::getWidth() const { return mWidth; }
37 
getData() const38 const std::vector<int16_t>& TouchVideoFrame::getData() const { return mData; }
39 
getTimestamp() const40 const struct timeval& TouchVideoFrame::getTimestamp() const { return mTimestamp; }
41 
rotate(int32_t orientation)42 void TouchVideoFrame::rotate(int32_t orientation) {
43     switch (orientation) {
44         case DISPLAY_ORIENTATION_90:
45             rotateQuarterTurn(false /*clockwise*/);
46             break;
47         case DISPLAY_ORIENTATION_180:
48             rotate180();
49             break;
50         case DISPLAY_ORIENTATION_270:
51             rotateQuarterTurn(true /*clockwise*/);
52             break;
53     }
54 }
55 
56 /**
57  * Rotate once clockwise by a quarter turn === rotate 90 degrees
58  * Rotate once counterclockwise by a quarter turn === rotate 270 degrees
59  * For a clockwise rotation:
60  *     An element at position (i, j) is rotated to (j, height - i - 1)
61  * For a counterclockwise rotation:
62  *     An element at position (i, j) is rotated to (width - j - 1, i)
63  */
rotateQuarterTurn(bool clockwise)64 void TouchVideoFrame::rotateQuarterTurn(bool clockwise) {
65     std::vector<int16_t> rotated(mData.size());
66     for (size_t i = 0; i < mHeight; i++) {
67         for (size_t j = 0; j < mWidth; j++) {
68             size_t iRotated, jRotated;
69             if (clockwise) {
70                 iRotated = j;
71                 jRotated = mHeight - i - 1;
72             } else {
73                 iRotated = mWidth - j - 1;
74                 jRotated = i;
75             }
76             size_t indexRotated = iRotated * mHeight + jRotated;
77             rotated[indexRotated] = mData[i * mWidth + j];
78         }
79     }
80     mData = std::move(rotated);
81     std::swap(mHeight, mWidth);
82 }
83 
84 /**
85  * An element at position (i, j) is rotated to (height - i - 1, width - j - 1)
86  * This is equivalent to moving element [i] to position [height * width - i - 1]
87  * Since element at [height * width - i - 1] would move to position [i],
88  * we can just swap elements [i] and [height * width - i - 1].
89  */
rotate180()90 void TouchVideoFrame::rotate180() {
91     if (mData.size() == 0) {
92         return;
93     }
94     // Just need to swap elements i and (height * width - 1 - i)
95     for (size_t i = 0; i < mData.size() / 2; i++) {
96         std::swap(mData[i], mData[mHeight * mWidth - 1 - i]);
97     }
98 }
99 
100 } // namespace android
101