| Siarhei Vishniakou | 26cf29d | 2019-02-15 16:48:38 -0600 | [diff] [blame] | 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 |  | 
| Dominik Laskowski | 718f960 | 2019-11-09 20:01:35 -0800 | [diff] [blame] | 17 | #include <input/DisplayViewport.h> | 
| Siarhei Vishniakou | 26cf29d | 2019-02-15 16:48:38 -0600 | [diff] [blame] | 18 | #include <input/TouchVideoFrame.h> | 
|  | 19 |  | 
|  | 20 | namespace android { | 
|  | 21 |  | 
| Siarhei Vishniakou | fda3aee | 2019-02-15 17:10:53 -0600 | [diff] [blame] | 22 | TouchVideoFrame::TouchVideoFrame(uint32_t height, uint32_t width, std::vector<int16_t> data, | 
| Siarhei Vishniakou | 26cf29d | 2019-02-15 16:48:38 -0600 | [diff] [blame] | 23 | const struct timeval& timestamp) : | 
| Siarhei Vishniakou | fda3aee | 2019-02-15 17:10:53 -0600 | [diff] [blame] | 24 | mHeight(height), mWidth(width),mData(std::move(data)), mTimestamp(timestamp) { | 
| Siarhei Vishniakou | 26cf29d | 2019-02-15 16:48:38 -0600 | [diff] [blame] | 25 | } | 
|  | 26 |  | 
|  | 27 | bool TouchVideoFrame::operator==(const TouchVideoFrame& rhs) const { | 
| Siarhei Vishniakou | fda3aee | 2019-02-15 17:10:53 -0600 | [diff] [blame] | 28 | return mHeight == rhs.mHeight | 
|  | 29 | && mWidth == rhs.mWidth | 
| Siarhei Vishniakou | 26cf29d | 2019-02-15 16:48:38 -0600 | [diff] [blame] | 30 | && mData == rhs.mData | 
|  | 31 | && mTimestamp.tv_sec == rhs.mTimestamp.tv_sec | 
|  | 32 | && mTimestamp.tv_usec == rhs.mTimestamp.tv_usec; | 
|  | 33 | } | 
|  | 34 |  | 
| Siarhei Vishniakou | 26cf29d | 2019-02-15 16:48:38 -0600 | [diff] [blame] | 35 | uint32_t TouchVideoFrame::getHeight() const { return mHeight; } | 
|  | 36 |  | 
| Siarhei Vishniakou | fda3aee | 2019-02-15 17:10:53 -0600 | [diff] [blame] | 37 | uint32_t TouchVideoFrame::getWidth() const { return mWidth; } | 
|  | 38 |  | 
| Siarhei Vishniakou | 26cf29d | 2019-02-15 16:48:38 -0600 | [diff] [blame] | 39 | const std::vector<int16_t>& TouchVideoFrame::getData() const { return mData; } | 
|  | 40 |  | 
|  | 41 | const struct timeval& TouchVideoFrame::getTimestamp() const { return mTimestamp; } | 
|  | 42 |  | 
| Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 43 | void TouchVideoFrame::rotate(ui::Rotation orientation) { | 
| Siarhei Vishniakou | 8154bbd | 2019-02-15 17:21:03 -0600 | [diff] [blame] | 44 | switch (orientation) { | 
| Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 45 | case ui::ROTATION_90: | 
| Philip Quinn | 882bbf3 | 2020-02-26 18:48:28 -0800 | [diff] [blame] | 46 | rotateQuarterTurn(false /*clockwise*/); | 
| Siarhei Vishniakou | 8154bbd | 2019-02-15 17:21:03 -0600 | [diff] [blame] | 47 | break; | 
| Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 48 | case ui::ROTATION_180: | 
| Siarhei Vishniakou | 8154bbd | 2019-02-15 17:21:03 -0600 | [diff] [blame] | 49 | rotate180(); | 
|  | 50 | break; | 
| Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 51 | case ui::ROTATION_270: | 
| Philip Quinn | 882bbf3 | 2020-02-26 18:48:28 -0800 | [diff] [blame] | 52 | rotateQuarterTurn(true /*clockwise*/); | 
| Siarhei Vishniakou | 8154bbd | 2019-02-15 17:21:03 -0600 | [diff] [blame] | 53 | break; | 
| Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 54 | case ui::ROTATION_0: | 
|  | 55 | // No need to rotate if there's no rotation. | 
|  | 56 | break; | 
| Siarhei Vishniakou | 8154bbd | 2019-02-15 17:21:03 -0600 | [diff] [blame] | 57 | } | 
|  | 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 | */ | 
|  | 68 | void 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 | */ | 
|  | 94 | void 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 Vishniakou | 26cf29d | 2019-02-15 16:48:38 -0600 | [diff] [blame] | 104 | } // namespace android |