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