zijiehe | 90ea736 | 2016-11-22 17:17:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/modules/desktop_capture/desktop_frame_rotation.h" |
| 12 | |
| 13 | #include <string.h> |
| 14 | |
zijiehe | 90ea736 | 2016-11-22 17:17:09 -0800 | [diff] [blame] | 15 | #include "third_party/libyuv/include/libyuv/rotate_argb.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 16 | #include "webrtc/rtc_base/checks.h" |
zijiehe | 90ea736 | 2016-11-22 17:17:09 -0800 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | libyuv::RotationMode ToLibyuvRotationMode(Rotation rotation) { |
| 23 | switch (rotation) { |
| 24 | case Rotation::CLOCK_WISE_0: |
| 25 | return libyuv::kRotate0; |
| 26 | case Rotation::CLOCK_WISE_90: |
| 27 | return libyuv::kRotate90; |
| 28 | case Rotation::CLOCK_WISE_180: |
| 29 | return libyuv::kRotate180; |
| 30 | case Rotation::CLOCK_WISE_270: |
| 31 | return libyuv::kRotate270; |
| 32 | } |
| 33 | RTC_NOTREACHED(); |
| 34 | return libyuv::kRotate0; |
| 35 | } |
| 36 | |
| 37 | DesktopRect RotateAndOffsetRect(DesktopRect rect, |
| 38 | DesktopSize size, |
| 39 | Rotation rotation, |
| 40 | DesktopVector offset) { |
| 41 | DesktopRect result = RotateRect(rect, size, rotation); |
| 42 | result.Translate(offset); |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | } // namespace |
| 47 | |
| 48 | Rotation ReverseRotation(Rotation rotation) { |
| 49 | switch (rotation) { |
| 50 | case Rotation::CLOCK_WISE_0: |
| 51 | return rotation; |
| 52 | case Rotation::CLOCK_WISE_90: |
| 53 | return Rotation::CLOCK_WISE_270; |
| 54 | case Rotation::CLOCK_WISE_180: |
| 55 | return Rotation::CLOCK_WISE_180; |
| 56 | case Rotation::CLOCK_WISE_270: |
| 57 | return Rotation::CLOCK_WISE_90; |
| 58 | } |
| 59 | RTC_NOTREACHED(); |
| 60 | return Rotation::CLOCK_WISE_0; |
| 61 | } |
| 62 | |
| 63 | DesktopSize RotateSize(DesktopSize size, Rotation rotation) { |
| 64 | switch (rotation) { |
| 65 | case Rotation::CLOCK_WISE_0: |
| 66 | case Rotation::CLOCK_WISE_180: |
| 67 | return size; |
| 68 | case Rotation::CLOCK_WISE_90: |
| 69 | case Rotation::CLOCK_WISE_270: |
| 70 | return DesktopSize(size.height(), size.width()); |
| 71 | } |
| 72 | RTC_NOTREACHED(); |
| 73 | return DesktopSize(); |
| 74 | } |
| 75 | |
| 76 | DesktopRect RotateRect(DesktopRect rect, DesktopSize size, Rotation rotation) { |
| 77 | switch (rotation) { |
| 78 | case Rotation::CLOCK_WISE_0: |
| 79 | return rect; |
| 80 | case Rotation::CLOCK_WISE_90: |
| 81 | return DesktopRect::MakeXYWH(size.height() - rect.bottom(), rect.left(), |
| 82 | rect.height(), rect.width()); |
| 83 | case Rotation::CLOCK_WISE_180: |
| 84 | return DesktopRect::MakeXYWH(size.width() - rect.right(), |
| 85 | size.height() - rect.bottom(), rect.width(), |
| 86 | rect.height()); |
| 87 | case Rotation::CLOCK_WISE_270: |
| 88 | return DesktopRect::MakeXYWH(rect.top(), size.width() - rect.right(), |
| 89 | rect.height(), rect.width()); |
| 90 | } |
| 91 | RTC_NOTREACHED(); |
| 92 | return DesktopRect(); |
| 93 | } |
| 94 | |
| 95 | void RotateDesktopFrame(const DesktopFrame& source, |
| 96 | const DesktopRect& source_rect, |
| 97 | const Rotation& rotation, |
| 98 | const DesktopVector& target_offset, |
| 99 | DesktopFrame* target) { |
| 100 | RTC_DCHECK(target); |
| 101 | RTC_DCHECK(DesktopRect::MakeSize(source.size()).ContainsRect(source_rect)); |
| 102 | // The rectangle in |target|. |
| 103 | const DesktopRect target_rect = |
| 104 | RotateAndOffsetRect(source_rect, source.size(), rotation, target_offset); |
| 105 | RTC_DCHECK(DesktopRect::MakeSize(target->size()).ContainsRect(target_rect)); |
| 106 | |
| 107 | if (target_rect.is_empty()) { |
| 108 | return; |
| 109 | } |
| 110 | |
zijiehe | 90ea736 | 2016-11-22 17:17:09 -0800 | [diff] [blame] | 111 | int result = libyuv::ARGBRotate( |
| 112 | source.GetFrameDataAtPos(source_rect.top_left()), source.stride(), |
| 113 | target->GetFrameDataAtPos(target_rect.top_left()), target->stride(), |
| 114 | source_rect.width(), source_rect.height(), |
| 115 | ToLibyuvRotationMode(rotation)); |
| 116 | RTC_DCHECK_EQ(result, 0); |
| 117 | } |
| 118 | |
| 119 | } // namespace webrtc |