blob: c62e0985f19136b9960a8f5215a3b59888b1aa10 [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
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060043void TouchVideoFrame::rotate(int32_t orientation) {
44 switch (orientation) {
45 case DISPLAY_ORIENTATION_90:
Philip Quinn882bbf32020-02-26 18:48:28 -080046 rotateQuarterTurn(false /*clockwise*/);
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060047 break;
48 case DISPLAY_ORIENTATION_180:
49 rotate180();
50 break;
51 case DISPLAY_ORIENTATION_270:
Philip Quinn882bbf32020-02-26 18:48:28 -080052 rotateQuarterTurn(true /*clockwise*/);
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060053 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 */
65void 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 */
91void 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
Siarhei Vishniakou26cf29d2019-02-15 16:48:38 -0600101} // namespace android