blob: c7ada7b61f4b68179c668fc258a2765412a17ff0 [file] [log] [blame]
jiayl@webrtc.org0e710702014-11-11 18:15:55 +00001/*
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
kwiberg2bb3afa2016-03-16 15:58:08 -070011#include <memory>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/desktop_capture/cropped_desktop_frame.h"
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
16#include "rtc_base/constructormagic.h"
kwiberg4485ffb2016-04-26 08:14:39 -070017
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000018namespace webrtc {
19
20// A DesktopFrame that is a sub-rect of another DesktopFrame.
21class CroppedDesktopFrame : public DesktopFrame {
22 public:
sergeyu5d910282016-06-07 16:41:58 -070023 CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,
24 const DesktopRect& rect);
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000025
26 private:
Zijie He5acd9d02017-08-24 12:41:53 -070027 const std::unique_ptr<DesktopFrame> frame_;
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000028
henrikg3c089d72015-09-16 05:37:44 -070029 RTC_DISALLOW_COPY_AND_ASSIGN(CroppedDesktopFrame);
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000030};
31
sergeyu5d910282016-06-07 16:41:58 -070032std::unique_ptr<DesktopFrame> CreateCroppedDesktopFrame(
33 std::unique_ptr<DesktopFrame> frame,
34 const DesktopRect& rect) {
Zijie He9c0914f2017-07-26 17:52:38 -070035 RTC_DCHECK(frame);
36
37 if (!DesktopRect::MakeSize(frame->size()).ContainsRect(rect)) {
sergeyu5d910282016-06-07 16:41:58 -070038 return nullptr;
Zijie He9c0914f2017-07-26 17:52:38 -070039 }
40
41 if (frame->size().equals(rect.size())) {
42 return frame;
43 }
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000044
sergeyu5d910282016-06-07 16:41:58 -070045 return std::unique_ptr<DesktopFrame>(
46 new CroppedDesktopFrame(std::move(frame), rect));
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000047}
48
sergeyu5d910282016-06-07 16:41:58 -070049CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000050 const DesktopRect& rect)
Zijie He51ce0c02017-09-05 14:39:52 -070051 : DesktopFrame(rect.size(),
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000052 frame->stride(),
53 frame->GetFrameDataAtPos(rect.top_left()),
Zijie He5acd9d02017-08-24 12:41:53 -070054 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.org0e710702014-11-11 18:15:55 +000060}
61
62} // namespace webrtc