Chromium Code Reviews| Index: webrtc/modules/desktop_capture/desktop_frame_rotator.cc |
| diff --git a/webrtc/modules/desktop_capture/desktop_frame_rotator.cc b/webrtc/modules/desktop_capture/desktop_frame_rotator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e7af9eb8fef5e3147451e21794259c55771c7ab5 |
| --- /dev/null |
| +++ b/webrtc/modules/desktop_capture/desktop_frame_rotator.cc |
| @@ -0,0 +1,141 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/desktop_capture/desktop_frame_rotator.h" |
| + |
| +#include <string.h> |
| + |
| +#include "webrtc/base/checks.h" |
| + |
| +#include "third_party/libyuv/include/libyuv/rotate_argb.h" |
| + |
| +namespace webrtc { |
| + |
| +namespace { |
| + |
| +// Returns a rotated DesktopRect of |rect|. The |size| represents the size of |
| +// the DesktopFrame which |rect| belongs in. |
| +DesktopRect Rotate(DesktopRect rect, DesktopSize size, Rotation rotation) { |
|
Sergey Ulanov
2016/11/15 20:50:22
Maybe call it RotateRect()?
Hzj_jie
2016/11/16 01:18:33
Done.
|
| + switch (rotation) { |
| + case Rotation::CLOCK_WISE_0: |
| + return rect; |
| + case Rotation::CLOCK_WISE_90: |
| + return DesktopRect::MakeLTRB(size.height() - rect.bottom(), rect.left(), |
| + size.height() - rect.top(), rect.right()); |
| + case Rotation::CLOCK_WISE_180: |
| + return DesktopRect::MakeLTRB( |
|
Sergey Ulanov
2016/11/15 20:50:22
nit: this would be slightly simpler with MakeXYWH(
Hzj_jie
2016/11/16 01:18:33
Done.
|
| + size.width() - rect.right(), size.height() - rect.bottom(), |
| + size.width() - rect.left(), size.height() - rect.top()); |
| + case Rotation::CLOCK_WISE_270: |
| + return DesktopRect::MakeLTRB(rect.top(), size.width() - rect.right(), |
| + rect.bottom(), size.width() - rect.left()); |
| + default: |
|
Sergey Ulanov
2016/11/15 20:50:22
Don't need default case. Maybe add NOTREACHED afte
Hzj_jie
2016/11/16 01:18:33
Done.
|
| + RTC_NOTREACHED(); |
| + return DesktopRect(); |
| + } |
| +} |
| + |
| +// Returns a reverse-rotated Rotation of |rotation|. |
| +Rotation Reverse(Rotation rotation) { |
| + switch (rotation) { |
| + case Rotation::CLOCK_WISE_0: |
| + return rotation; |
| + case Rotation::CLOCK_WISE_90: |
| + return Rotation::CLOCK_WISE_270; |
| + case Rotation::CLOCK_WISE_180: |
| + return Rotation::CLOCK_WISE_180; |
| + case Rotation::CLOCK_WISE_270: |
| + return Rotation::CLOCK_WISE_90; |
| + default: |
|
Sergey Ulanov
2016/11/15 20:50:22
don't need default case.
Hzj_jie
2016/11/16 01:18:33
Done.
|
| + RTC_NOTREACHED(); |
| + return Rotation::CLOCK_WISE_0; |
| + } |
| +} |
| + |
| +// Returns a reverse-rotated DesktopRect of |rect|. The |size| represents the |
| +// size of the DesktopFrame which |rect| belongs in. |
| +DesktopRect Derotate(DesktopRect rect, DesktopSize size, Rotation rotation) { |
|
Sergey Ulanov
2016/11/15 20:50:22
I don't think you need this function, given that i
Hzj_jie
2016/11/16 01:18:33
Done.
|
| + return Rotate(rect, size, Reverse(rotation)); |
| +} |
| + |
| +// Returns a reverse-rotated DesktopSize of |size|. |
| +DesktopSize Derotate(DesktopSize size, Rotation rotation) { |
|
Sergey Ulanov
2016/11/15 20:50:22
GetRotatedSize()?
Hzj_jie
2016/11/16 01:18:33
Done.
|
| + switch (rotation) { |
| + case Rotation::CLOCK_WISE_0: |
| + case Rotation::CLOCK_WISE_180: |
| + return size; |
| + case Rotation::CLOCK_WISE_90: |
| + case Rotation::CLOCK_WISE_270: |
| + return DesktopSize(size.height(), size.width()); |
| + default: |
| + RTC_NOTREACHED(); |
| + return DesktopSize(); |
| + } |
| +} |
| + |
| +libyuv::RotationMode ToYuvRotationMode(Rotation rotation) { |
|
Sergey Ulanov
2016/11/15 20:50:22
ToLibyuvRotationMode
Hzj_jie
2016/11/16 01:18:33
Done.
|
| + switch (rotation) { |
| + case Rotation::CLOCK_WISE_0: |
| + return libyuv::kRotate0; |
| + case Rotation::CLOCK_WISE_90: |
| + return libyuv::kRotate90; |
| + case Rotation::CLOCK_WISE_180: |
| + return libyuv::kRotate180; |
| + case Rotation::CLOCK_WISE_270: |
| + return libyuv::kRotate270; |
| + default: |
| + RTC_NOTREACHED(); |
| + return libyuv::kRotate0; |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +bool CopyRotatedRectTo(const DesktopFrame& src, |
| + DesktopRect rect, |
| + Rotation rotation, |
| + DesktopFrame* target) { |
| + if (!target) { |
|
Sergey Ulanov
2016/11/15 20:50:22
Do you really need this? Maybe replace with a DCHE
Hzj_jie
2016/11/16 01:18:33
Done.
|
| + return false; |
| + } |
| + if (!DesktopRect::MakeSize(target->size()).ContainsRect(rect)) { |
| + return false; |
| + } |
| + if (rect.is_empty()) { |
|
Sergey Ulanov
2016/11/15 20:50:22
Do we need this special case? I think CopyPixelsFr
Hzj_jie
2016/11/16 01:18:33
When the input rectangle is empty, ARGBRotate will
|
| + return true; |
| + } |
| + |
| + target->mutable_updated_region()->AddRect(rect); |
| + if (rotation == Rotation::CLOCK_WISE_0) { |
| + // A shortcut: if |rotation| is 0, this function uses |
| + // DesktopFrame::CopyPixelsFrom() for a better performance. |
|
Sergey Ulanov
2016/11/15 20:50:22
Does it actually perform better? I would guess lib
Hzj_jie
2016/11/16 01:18:33
Done.
No, it's for old version, and should be remo
|
| + target->CopyPixelsFrom(src, rect.top_left(), rect); |
| + return true; |
| + } |
| + |
| + // The rectangle in |src|. |
| + const DesktopRect src_rect = |
| + Derotate(rect, Derotate(src.size(), rotation), rotation); |
| + return libyuv::ARGBRotate( |
| + src.GetFrameDataAtPos(src_rect.top_left()), src.stride(), |
| + target->GetFrameDataAtPos(rect.top_left()), target->stride(), |
| + src_rect.width(), src_rect.height(), |
| + ToYuvRotationMode(rotation)) == 0; |
| +} |
| + |
| +bool CopyUnrotatedRectTo(const DesktopFrame& src, |
| + DesktopRect rect, |
| + Rotation rotation, |
| + DesktopFrame* target) { |
| + return CopyRotatedRectTo(src, Rotate(rect, src.size(), rotation), rotation, |
| + target); |
| +} |
| + |
| +} // namespace webrtc |