jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/desktop_capture/cropped_desktop_frame.h" |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
| 16 | #include "rtc_base/constructormagic.h" |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 17 | |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
| 20 | // A DesktopFrame that is a sub-rect of another DesktopFrame. |
| 21 | class CroppedDesktopFrame : public DesktopFrame { |
| 22 | public: |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 23 | CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame, |
| 24 | const DesktopRect& rect); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 25 | |
| 26 | private: |
Zijie He | 5acd9d0 | 2017-08-24 12:41:53 -0700 | [diff] [blame] | 27 | const std::unique_ptr<DesktopFrame> frame_; |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 28 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 29 | RTC_DISALLOW_COPY_AND_ASSIGN(CroppedDesktopFrame); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 32 | std::unique_ptr<DesktopFrame> CreateCroppedDesktopFrame( |
| 33 | std::unique_ptr<DesktopFrame> frame, |
| 34 | const DesktopRect& rect) { |
Zijie He | 9c0914f | 2017-07-26 17:52:38 -0700 | [diff] [blame] | 35 | RTC_DCHECK(frame); |
| 36 | |
| 37 | if (!DesktopRect::MakeSize(frame->size()).ContainsRect(rect)) { |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 38 | return nullptr; |
Zijie He | 9c0914f | 2017-07-26 17:52:38 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | if (frame->size().equals(rect.size())) { |
| 42 | return frame; |
| 43 | } |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 44 | |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 45 | return std::unique_ptr<DesktopFrame>( |
| 46 | new CroppedDesktopFrame(std::move(frame), rect)); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 47 | } |
| 48 | |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 49 | CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame, |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 50 | const DesktopRect& rect) |
Zijie He | 51ce0c0 | 2017-09-05 14:39:52 -0700 | [diff] [blame] | 51 | : DesktopFrame(rect.size(), |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 52 | frame->stride(), |
| 53 | frame->GetFrameDataAtPos(rect.top_left()), |
Zijie He | 5acd9d0 | 2017-08-24 12:41:53 -0700 | [diff] [blame] | 54 | frame->shared_memory()), |
| 55 | frame_(std::move(frame)) { |
| 56 | MoveFrameInfoFrom(frame_.get()); |
| 57 | set_top_left(frame_->top_left().add(rect.top_left())); |
| 58 | mutable_updated_region()->IntersectWith(rect); |
| 59 | mutable_updated_region()->Translate(-rect.left(), -rect.top()); |
jiayl@webrtc.org | 0e71070 | 2014-11-11 18:15:55 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | } // namespace webrtc |