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(int32_t orientation)43 void TouchVideoFrame::rotate(int32_t orientation) {
44 switch (orientation) {
45 case DISPLAY_ORIENTATION_90:
46 rotateQuarterTurn(false /*clockwise*/);
47 break;
48 case DISPLAY_ORIENTATION_180:
49 rotate180();
50 break;
51 case DISPLAY_ORIENTATION_270:
52 rotateQuarterTurn(true /*clockwise*/);
53 break;
54 }
55 }
56
57 /**
58 * Rotate once clockwise by a quarter turn === rotate 90 degrees
59 * Rotate once counterclockwise by a quarter turn === rotate 270 degrees
60 * For a clockwise rotation:
61 * An element at position (i, j) is rotated to (j, height - i - 1)
62 * For a counterclockwise rotation:
63 * An element at position (i, j) is rotated to (width - j - 1, i)
64 */
rotateQuarterTurn(bool clockwise)65 void TouchVideoFrame::rotateQuarterTurn(bool clockwise) {
66 std::vector<int16_t> rotated(mData.size());
67 for (size_t i = 0; i < mHeight; i++) {
68 for (size_t j = 0; j < mWidth; j++) {
69 size_t iRotated, jRotated;
70 if (clockwise) {
71 iRotated = j;
72 jRotated = mHeight - i - 1;
73 } else {
74 iRotated = mWidth - j - 1;
75 jRotated = i;
76 }
77 size_t indexRotated = iRotated * mHeight + jRotated;
78 rotated[indexRotated] = mData[i * mWidth + j];
79 }
80 }
81 mData = std::move(rotated);
82 std::swap(mHeight, mWidth);
83 }
84
85 /**
86 * An element at position (i, j) is rotated to (height - i - 1, width - j - 1)
87 * This is equivalent to moving element [i] to position [height * width - i - 1]
88 * Since element at [height * width - i - 1] would move to position [i],
89 * we can just swap elements [i] and [height * width - i - 1].
90 */
rotate180()91 void TouchVideoFrame::rotate180() {
92 if (mData.size() == 0) {
93 return;
94 }
95 // Just need to swap elements i and (height * width - 1 - i)
96 for (size_t i = 0; i < mData.size() / 2; i++) {
97 std::swap(mData[i], mData[mHeight * mWidth - 1 - i]);
98 }
99 }
100
101 } // namespace android
102