blob: 6d7d5614c6ca999b9b31d92dbf306ae73ac1f61e [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
Dominik Laskowski718f9602019-11-09 20:01:35 -080017#include <input/DisplayViewport.h>
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060018#include <input/TouchVideoFrame.h>
19
20namespace android {
21
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060022TouchVideoFrame::TouchVideoFrame(uint32_t height, uint32_t width, std::vector<int16_t> data,
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060023 const struct timeval& timestamp) :
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060024 mHeight(height), mWidth(width),mData(std::move(data)), mTimestamp(timestamp) {
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060025}
26
27bool TouchVideoFrame::operator==(const TouchVideoFrame& rhs) const {
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060028 return mHeight == rhs.mHeight
29 && mWidth == rhs.mWidth
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060030 && mData == rhs.mData
31 && mTimestamp.tv_sec == rhs.mTimestamp.tv_sec
32 && mTimestamp.tv_usec == rhs.mTimestamp.tv_usec;
33}
34
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060035uint32_t TouchVideoFrame::getHeight() const { return mHeight; }
36
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060037uint32_t TouchVideoFrame::getWidth() const { return mWidth; }
38
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -060039const std::vector<int16_t>& TouchVideoFrame::getData() const { return mData; }
40
41const struct timeval& TouchVideoFrame::getTimestamp() const { return mTimestamp; }
42
Michael Wrighta9cf4192022-12-01 23:46:39 +000043void TouchVideoFrame::rotate(ui::Rotation orientation) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060044 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +000045 case ui::ROTATION_90:
Harry Cutts82c791c2023-03-10 17:15:07 +000046 rotateQuarterTurn(/*clockwise=*/false);
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060047 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +000048 case ui::ROTATION_180:
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060049 rotate180();
50 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +000051 case ui::ROTATION_270:
Harry Cutts82c791c2023-03-10 17:15:07 +000052 rotateQuarterTurn(/*clockwise=*/true);
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060053 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +000054 case ui::ROTATION_0:
55 // No need to rotate if there's no rotation.
56 break;
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060057 }
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 */
68void 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 */
94void 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
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -0600104} // namespace android