blob: 8a4298a36f8cbce246c1ada42f0e212e101326f1 [file] [log] [blame]
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -06001/*
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
19namespace android {
20
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060021TouchVideoFrame::TouchVideoFrame(uint32_t height, uint32_t width, std::vector<int16_t> data,
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060022 const struct timeval& timestamp) :
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060023 mHeight(height), mWidth(width),mData(std::move(data)), mTimestamp(timestamp) {
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060024}
25
26bool TouchVideoFrame::operator==(const TouchVideoFrame& rhs) const {
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060027 return mHeight == rhs.mHeight
28 && mWidth == rhs.mWidth
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060029 && mData == rhs.mData
30 && mTimestamp.tv_sec == rhs.mTimestamp.tv_sec
31 && mTimestamp.tv_usec == rhs.mTimestamp.tv_usec;
32}
33
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060034uint32_t TouchVideoFrame::getHeight() const { return mHeight; }
35
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060036uint32_t TouchVideoFrame::getWidth() const { return mWidth; }
37
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060038const std::vector<int16_t>& TouchVideoFrame::getData() const { return mData; }
39
40const struct timeval& TouchVideoFrame::getTimestamp() const { return mTimestamp; }
41
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060042void TouchVideoFrame::rotate(int32_t orientation) {
43 switch (orientation) {
44 case DISPLAY_ORIENTATION_90:
45 rotateQuarterTurn(true /*clockwise*/);
46 break;
47 case DISPLAY_ORIENTATION_180:
48 rotate180();
49 break;
50 case DISPLAY_ORIENTATION_270:
51 rotateQuarterTurn(false /*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 */
64void 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 */
90void 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
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -0600100} // namespace android